Skip to content

Commit d9bdabb

Browse files
authored
Merge pull request #33 from delphi-hub/feature/linksAsAttributes
Adapt Crawler to latest changes of the registry interface
2 parents 6e72f48 + abac46b commit d9bdabb

File tree

4 files changed

+141
-29
lines changed

4 files changed

+141
-29
lines changed

src/main/scala/de/upb/cs/swt/delphi/crawler/Configuration.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import java.net.URI
2121
import akka.stream.ThrottleMode
2222
import com.sksamuel.elastic4s.ElasticsearchClientUri
2323
import de.upb.cs.swt.delphi.crawler.instancemanagement.InstanceEnums.{ComponentType, InstanceState}
24-
import de.upb.cs.swt.delphi.crawler.instancemanagement.{Instance, InstanceRegistry}
24+
import de.upb.cs.swt.delphi.crawler.instancemanagement.{Instance, InstanceLink, InstanceRegistry}
2525

2626
import scala.concurrent.duration._
2727
import scala.util.{Failure, Success, Try}
@@ -43,7 +43,9 @@ class Configuration {
4343
ComponentType.ElasticSearch,
4444
None,
4545
InstanceState.Running,
46-
List.empty[String])
46+
List.empty[String],
47+
List.empty[InstanceLink],
48+
List.empty[InstanceLink])
4749
}
4850

4951
val mavenRepoBase: URI = new URI("http://repo1.maven.org/maven2/") // TODO: Create a local demo server "http://localhost:8881/maven2/"

src/main/scala/de/upb/cs/swt/delphi/crawler/instancemanagement/Instance.scala

Lines changed: 81 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,61 +16,111 @@
1616
package de.upb.cs.swt.delphi.crawler.instancemanagement
1717

1818
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
19+
import de.upb.cs.swt.delphi.crawler.instancemanagement.InstanceEnums.{ComponentType, InstanceState}
1920
import spray.json.{DefaultJsonProtocol, DeserializationException, JsString, JsValue, JsonFormat}
2021

21-
trait JsonSupport extends SprayJsonSupport with DefaultJsonProtocol {
22+
/**
23+
* Trait defining the implicit JSON formats needed to work with Instances
24+
*/
25+
trait InstanceJsonSupport extends SprayJsonSupport with DefaultJsonProtocol with InstanceLinkJsonSupport {
2226

23-
implicit val componentTypeFormat : JsonFormat[InstanceEnums.ComponentType] = new JsonFormat[InstanceEnums.ComponentType] {
27+
//Custom JSON format for an ComponentType
28+
implicit val componentTypeFormat : JsonFormat[ComponentType] = new JsonFormat[ComponentType] {
2429

25-
def write(compType : InstanceEnums.ComponentType) = JsString(compType.toString)
30+
/**
31+
* Custom write method for serializing an ComponentType
32+
* @param compType The ComponentType to serialize
33+
* @return JsString containing the serialized value
34+
*/
35+
def write(compType : ComponentType) = JsString(compType.toString)
2636

27-
def read(value: JsValue) : InstanceEnums.ComponentType = value match {
37+
/**
38+
* Custom read method for deserialization of an ComponentType
39+
* @param value JsValue to deserialize (must be a JsString)
40+
* @return ComponentType that has been read
41+
* @throws DeserializationException Exception thrown when JsValue is in incorrect format
42+
*/
43+
def read(value: JsValue) : ComponentType = value match {
2844
case JsString(s) => s match {
29-
case "Crawler" => InstanceEnums.ComponentType.Crawler
30-
case "WebApi" => InstanceEnums.ComponentType.WebApi
31-
case "WebApp" => InstanceEnums.ComponentType.WebApp
32-
case "DelphiManagement" => InstanceEnums.ComponentType.DelphiManagement
33-
case "ElasticSearch" => InstanceEnums.ComponentType.ElasticSearch
45+
case "Crawler" => ComponentType.Crawler
46+
case "WebApi" => ComponentType.WebApi
47+
case "WebApp" => ComponentType.WebApp
48+
case "DelphiManagement" => ComponentType.DelphiManagement
49+
case "ElasticSearch" => ComponentType.ElasticSearch
3450
case x => throw DeserializationException(s"Unexpected string value $x for component type.")
3551
}
36-
case y => throw DeserializationException(s"Unexpected type $y while deserializing component type.")
52+
case y => throw DeserializationException(s"Unexpected type $y during deserialization component type.")
3753
}
3854
}
3955

40-
implicit val stateFormat : JsonFormat[InstanceEnums.State] = new JsonFormat[InstanceEnums.State] {
56+
//Custom JSON format for an InstanceState
57+
implicit val stateFormat : JsonFormat[InstanceState] = new JsonFormat[InstanceState] {
4158

42-
def write(compType : InstanceEnums.State) = JsString(compType.toString)
59+
/**
60+
* Custom write method for serializing an InstanceState
61+
* @param state The InstanceState to serialize
62+
* @return JsString containing the serialized value
63+
*/
64+
def write(state : InstanceState) = JsString(state.toString)
4365

44-
def read(value: JsValue) : InstanceEnums.State = value match {
66+
/**
67+
* Custom read method for deserialization of an InstanceState
68+
* @param value JsValue to deserialize (must be a JsString)
69+
* @return InstanceState that has been read
70+
* @throws DeserializationException Exception thrown when JsValue is in incorrect format
71+
*/
72+
def read(value: JsValue) : InstanceState = value match {
4573
case JsString(s) => s match {
46-
case "Running" => InstanceEnums.InstanceState.Running
47-
case "Stopped" => InstanceEnums.InstanceState.Stopped
48-
case "Failed" => InstanceEnums.InstanceState.Failed
49-
case "Paused" => InstanceEnums.InstanceState.Paused
50-
case "NotReachable" => InstanceEnums.InstanceState.NotReachable
74+
case "Running" => InstanceState.Running
75+
case "Stopped" => InstanceState.Stopped
76+
case "Failed" => InstanceState.Failed
77+
case "Paused" => InstanceState.Paused
78+
case "NotReachable" => InstanceState.NotReachable
79+
case "Deploying" => InstanceState.Deploying
5180
case x => throw DeserializationException(s"Unexpected string value $x for instance state.")
5281
}
53-
case y => throw DeserializationException(s"Unexpected type $y while deserializing instance state.")
82+
case y => throw DeserializationException(s"Unexpected type $y during deserialization instance state.")
5483
}
5584
}
5685

57-
implicit val instanceFormat : JsonFormat[Instance] = jsonFormat8(Instance)
86+
//JSON format for Instances
87+
implicit val instanceFormat : JsonFormat[Instance] = jsonFormat10(Instance)
5888
}
5989

90+
/**
91+
* The instance type used for transmitting data about an instance from an to the registry
92+
* @param id Id of the instance. This is an Option[Long], as an registering instance will not yet have an id.
93+
* @param host Host of the instance.
94+
* @param portNumber Port the instance is reachable at.
95+
* @param name Name of the instance
96+
* @param componentType ComponentType of the instance.
97+
* @param dockerId The docker container id of the instance. This is an Option[String], as not all instance have to be docker containers.
98+
* @param instanceState State of the instance
99+
*/
60100
final case class Instance (
61101
id: Option[Long],
62102
host: String,
63103
portNumber: Long,
64104
name: String,
65-
componentType: InstanceEnums.ComponentType,
105+
componentType: ComponentType,
66106
dockerId: Option[String],
67-
instanceState: InstanceEnums.State,
68-
labels: List[String]
69-
) {}
107+
instanceState: InstanceState,
108+
labels: List[String],
109+
linksTo: List[InstanceLink],
110+
linksFrom: List[InstanceLink]
111+
)
70112

113+
/**
114+
* Enumerations concerning instances
115+
*/
71116
object InstanceEnums {
72117

118+
//Type to use when working with component types
73119
type ComponentType = ComponentType.Value
120+
121+
/**
122+
* ComponentType enumeration defining the valid types of delphi components
123+
*/
74124
object ComponentType extends Enumeration {
75125
val Crawler : Value = Value("Crawler")
76126
val WebApi : Value = Value("WebApi")
@@ -79,8 +129,14 @@ object InstanceEnums {
79129
val ElasticSearch : Value = Value("ElasticSearch")
80130
}
81131

82-
type State = InstanceState.Value
132+
//Type to use when working with instance states
133+
type InstanceState = InstanceState.Value
134+
135+
/**
136+
* InstanceState enumeration defining the valid states for instances of delphi components
137+
*/
83138
object InstanceState extends Enumeration {
139+
val Deploying : Value = Value("Deploying")
84140
val Running : Value = Value("Running")
85141
val Stopped : Value = Value("Stopped")
86142
val Failed : Value = Value("Failed")
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (C) 2018 The Delphi Team.
2+
// See the LICENCE file distributed with this work for additional
3+
// information regarding copyright ownership.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
package de.upb.cs.swt.delphi.crawler.instancemanagement
17+
18+
import LinkEnums.LinkState
19+
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
20+
import spray.json.{DefaultJsonProtocol, DeserializationException, JsString, JsValue, JsonFormat}
21+
22+
trait InstanceLinkJsonSupport extends SprayJsonSupport with DefaultJsonProtocol {
23+
24+
implicit val linkStateFormat: JsonFormat[LinkState] = new JsonFormat[LinkState] {
25+
override def read(value: JsValue): LinkState = value match {
26+
case JsString(s) => s match {
27+
case "Assigned" => LinkState.Assigned
28+
case "Outdated" => LinkState.Outdated
29+
case "Failed" => LinkState.Failed
30+
case x => throw DeserializationException(s"Unexpected string value $x for LinkState.")
31+
}
32+
case y => throw DeserializationException(s"Unexpected type $y during deserialization of LinkState")
33+
}
34+
35+
override def write(linkState: LinkState): JsValue = JsString(linkState.toString)
36+
}
37+
38+
implicit val instanceLinkFormat: JsonFormat[InstanceLink] =
39+
jsonFormat3(InstanceLink)
40+
}
41+
42+
43+
final case class InstanceLink(idFrom: Long, idTo:Long, linkState: LinkState)
44+
45+
object LinkEnums {
46+
type LinkState = LinkState.Value
47+
48+
object LinkState extends Enumeration {
49+
val Assigned: Value = Value("Assigned")
50+
val Failed: Value = Value("Failed")
51+
val Outdated: Value = Value("Outdated")
52+
}
53+
}

src/main/scala/de/upb/cs/swt/delphi/crawler/instancemanagement/InstanceRegistry.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import scala.concurrent.{Await, ExecutionContext, Future}
3232
import scala.util.{Failure, Success, Try}
3333
import spray.json._
3434

35-
object InstanceRegistry extends JsonSupport with AppLogging
35+
object InstanceRegistry extends InstanceJsonSupport with AppLogging
3636
{
3737

3838
implicit val system : ActorSystem = Crawler.system
@@ -251,7 +251,8 @@ object InstanceRegistry extends JsonSupport with AppLogging
251251

252252
private def createInstance(id: Option[Long], controlPort : Int, name : String) : Instance =
253253
Instance(id, InetAddress.getLocalHost.getHostAddress,
254-
controlPort, name, ComponentType.Crawler, None, InstanceState.Running, List.empty[String])
254+
controlPort, name, ComponentType.Crawler, None, InstanceState.Running,
255+
List.empty[String], List.empty[InstanceLink], List.empty[InstanceLink])
255256

256257

257258
object ReportOperationType extends Enumeration {

0 commit comments

Comments
 (0)