1515// limitations under the License.
1616
1717package de .upb .cs .swt .delphi .instancemanagement
18+
1819import akka .http .scaladsl .marshallers .sprayjson .SprayJsonSupport
20+ import de .upb .cs .swt .delphi .instancemanagement .InstanceEnums .{ComponentType , InstanceState }
1921import 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 ] = jsonFormat7(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+ */
60101final 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
108+ instanceState : InstanceState ,
109+ labels : List [String ],
110+ linksTo : List [InstanceLink ],
111+ linksFrom : List [InstanceLink ]
68112 )
113+
114+ /**
115+ * Enumerations concerning instances
116+ */
69117object InstanceEnums {
118+
119+ // Type to use when working with component types
70120 type ComponentType = ComponentType .Value
121+
122+ /**
123+ * ComponentType enumeration defining the valid types of delphi components
124+ */
71125 object ComponentType extends Enumeration {
72126 val Crawler : Value = Value (" Crawler" )
73- val ElasticSearch : Value = Value (" ElasticSearch" )
74127 val WebApi : Value = Value (" WebApi" )
75128 val WebApp : Value = Value (" WebApp" )
76129 val DelphiManagement : Value = Value (" DelphiManagement" )
130+ val ElasticSearch : Value = Value (" ElasticSearch" )
77131 }
78- 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+ */
79139 object InstanceState extends Enumeration {
140+ val Deploying : Value = Value (" Deploying" )
80141 val Running : Value = Value (" Running" )
81- val Paused : Value = Value (" Paused" )
82142 val Stopped : Value = Value (" Stopped" )
83143 val Failed : Value = Value (" Failed" )
144+ val Paused : Value = Value (" Paused" )
84145 val NotReachable : Value = Value (" NotReachable" )
85146 }
86- }
147+
148+ }
0 commit comments