Skip to content

Commit 4dbb9df

Browse files
authored
Merge pull request #36 from delphi-hub/feature/linksAsAttributes
Adapt WebApi to latest changes of the registry interface
2 parents 37c836d + d2ddb34 commit 4dbb9df

File tree

4 files changed

+147
-31
lines changed

4 files changed

+147
-31
lines changed

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

+88-27
Original file line numberDiff line numberDiff line change
@@ -15,73 +15,134 @@
1515
// limitations under the License.
1616

1717
package de.upb.cs.swt.delphi.instancemanagement
18+
1819
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
20+
import de.upb.cs.swt.delphi.instancemanagement.InstanceEnums.{ComponentType, InstanceState}
1921
import spray.json.{DefaultJsonProtocol, DeserializationException, JsString, JsValue, JsonFormat}
2022

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

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

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

27-
def read(value: JsValue) : InstanceEnums.ComponentType = value match {
38+
/**
39+
* Custom read method for deserialization of an ComponentType
40+
* @param value JsValue to deserialize (must be a JsString)
41+
* @return ComponentType that has been read
42+
* @throws DeserializationException Exception thrown when JsValue is in incorrect format
43+
*/
44+
def read(value: JsValue) : ComponentType = value match {
2845
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
46+
case "Crawler" => ComponentType.Crawler
47+
case "WebApi" => ComponentType.WebApi
48+
case "WebApp" => ComponentType.WebApp
49+
case "DelphiManagement" => ComponentType.DelphiManagement
50+
case "ElasticSearch" => ComponentType.ElasticSearch
3451
case x => throw DeserializationException(s"Unexpected string value $x for component type.")
3552
}
36-
case y => throw DeserializationException(s"Unexpected type $y while deserializing component type.")
53+
case y => throw DeserializationException(s"Unexpected type $y during deserialization component type.")
3754
}
3855
}
3956

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

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

44-
def read(value: JsValue) : InstanceEnums.State = value match {
67+
/**
68+
* Custom read method for deserialization of an InstanceState
69+
* @param value JsValue to deserialize (must be a JsString)
70+
* @return InstanceState that has been read
71+
* @throws DeserializationException Exception thrown when JsValue is in incorrect format
72+
*/
73+
def read(value: JsValue) : InstanceState = value match {
4574
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
75+
case "Running" => InstanceState.Running
76+
case "Stopped" => InstanceState.Stopped
77+
case "Failed" => InstanceState.Failed
78+
case "Paused" => InstanceState.Paused
79+
case "NotReachable" => InstanceState.NotReachable
80+
case "Deploying" => InstanceState.Deploying
5181
case x => throw DeserializationException(s"Unexpected string value $x for instance state.")
5282
}
53-
case y => throw DeserializationException(s"Unexpected type $y while deserializing instance state.")
83+
case y => throw DeserializationException(s"Unexpected type $y during deserialization instance state.")
5484
}
5585
}
5686

57-
implicit val instanceFormat : JsonFormat[Instance] = jsonFormat8(Instance)
87+
//JSON format for Instances
88+
implicit val instanceFormat : JsonFormat[Instance] = jsonFormat10(Instance)
5889
}
5990

91+
/**
92+
* The instance type used for transmitting data about an instance from an to the registry
93+
* @param id Id of the instance. This is an Option[Long], as an registering instance will not yet have an id.
94+
* @param host Host of the instance.
95+
* @param portNumber Port the instance is reachable at.
96+
* @param name Name of the instance
97+
* @param componentType ComponentType of the instance.
98+
* @param dockerId The docker container id of the instance. This is an Option[String], as not all instance have to be docker containers.
99+
* @param instanceState State of the instance
100+
*/
60101
final case class Instance (
61102
id: Option[Long],
62103
host: String,
63104
portNumber: Long,
64105
name: String,
65-
componentType: InstanceEnums.ComponentType,
106+
componentType: ComponentType,
66107
dockerId: Option[String],
67-
instanceState: InstanceEnums.State,
68-
labels: List[String]
108+
instanceState: InstanceState,
109+
labels: List[String],
110+
linksTo: List[InstanceLink],
111+
linksFrom: List[InstanceLink]
69112
)
113+
114+
/**
115+
* Enumerations concerning instances
116+
*/
70117
object InstanceEnums {
118+
119+
//Type to use when working with component types
71120
type ComponentType = ComponentType.Value
121+
122+
/**
123+
* ComponentType enumeration defining the valid types of delphi components
124+
*/
72125
object ComponentType extends Enumeration {
73126
val Crawler : Value = Value("Crawler")
74-
val ElasticSearch : Value = Value("ElasticSearch")
75127
val WebApi : Value = Value("WebApi")
76128
val WebApp : Value = Value("WebApp")
77129
val DelphiManagement : Value = Value("DelphiManagement")
130+
val ElasticSearch : Value = Value("ElasticSearch")
78131
}
79-
type State = InstanceState.Value
132+
133+
//Type to use when working with instance states
134+
type InstanceState = InstanceState.Value
135+
136+
/**
137+
* InstanceState enumeration defining the valid states for instances of delphi components
138+
*/
80139
object InstanceState extends Enumeration {
140+
val Deploying : Value = Value("Deploying")
81141
val Running : Value = Value("Running")
82-
val Paused : Value = Value("Paused")
83142
val Stopped : Value = Value("Stopped")
84143
val Failed : Value = Value("Failed")
144+
val Paused : Value = Value("Paused")
85145
val NotReachable : Value = Value("NotReachable")
86146
}
87-
}
147+
148+
}
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.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/instancemanagement/InstanceRegistry.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import scala.concurrent.duration._
3030
import scala.concurrent.{Await, Future}
3131
import scala.util.{Failure, Success, Try}
3232

33-
object InstanceRegistry extends JsonSupport with AppLogging {
33+
object InstanceRegistry extends InstanceJsonSupport with AppLogging {
3434

3535

3636
lazy val instanceIdFromEnv: Option[Long] = Try[Long](sys.env("INSTANCE_ID").toLong).toOption
@@ -248,7 +248,7 @@ object InstanceRegistry extends JsonSupport with AppLogging {
248248

249249
private def createInstance(id: Option[Long], controlPort: Int, name: String): Instance =
250250
Instance(id, InetAddress.getLocalHost.getHostAddress,
251-
controlPort, name, ComponentType.WebApi, None, InstanceState.Running, List.empty[String])
251+
controlPort, name, ComponentType.WebApi, None, InstanceState.Running, List.empty[String], List.empty[InstanceLink], List.empty[InstanceLink])
252252

253253
def reportStart(id: String, configuration: Configuration): Try[ResponseEntity] = {
254254
val request = HttpRequest(method = HttpMethods.GET, configuration.instanceRegistryUri + "/reportStart")

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package de.upb.cs.swt.delphi.webapi
1919
import com.sksamuel.elastic4s.http.ElasticDsl._
2020
import com.sksamuel.elastic4s.{ElasticsearchClientUri, Index, IndexAndType}
2121
import de.upb.cs.swt.delphi.instancemanagement.InstanceEnums.{ComponentType, InstanceState}
22-
import de.upb.cs.swt.delphi.instancemanagement.{Instance, InstanceRegistry}
22+
import de.upb.cs.swt.delphi.instancemanagement.{Instance, InstanceLink, InstanceRegistry}
2323

2424
import scala.util.{Failure, Success, Try}
2525

@@ -50,7 +50,9 @@ class Configuration( //Server and Elasticsearch configuration
5050
ComponentType.ElasticSearch,
5151
None,
5252
InstanceState.Running,
53-
List.empty[String])
53+
List.empty[String],
54+
List.empty[InstanceLink],
55+
List.empty[InstanceLink])
5456
}
5557
val defaultElasticSearchPort: Int = 9200
5658
val defaultElasticSearchHost: String = "elasticsearch://localhost"

0 commit comments

Comments
 (0)