1616package de .upb .cs .swt .delphi .crawler .instancemanagement
1717
1818import akka .http .scaladsl .marshallers .sprayjson .SprayJsonSupport
19+ import de .upb .cs .swt .delphi .crawler .instancemanagement .InstanceEnums .{ComponentType , InstanceState }
1920import 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+ */
60100final 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+ */
71116object 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" )
0 commit comments