15
15
// limitations under the License.
16
16
17
17
package de .upb .cs .swt .delphi .instancemanagement
18
+
18
19
import akka .http .scaladsl .marshallers .sprayjson .SprayJsonSupport
20
+ import de .upb .cs .swt .delphi .instancemanagement .InstanceEnums .{ComponentType , InstanceState }
19
21
import spray .json .{DefaultJsonProtocol , DeserializationException , JsString , JsValue , JsonFormat }
20
22
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 {
22
27
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 ] {
24
30
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)
26
37
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 {
28
45
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
34
51
case x => throw DeserializationException (s " Unexpected string value $x for component type. " )
35
52
}
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. " )
37
54
}
38
55
}
39
56
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 ] {
41
59
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)
43
66
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 {
45
74
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
51
81
case x => throw DeserializationException (s " Unexpected string value $x for instance state. " )
52
82
}
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. " )
54
84
}
55
85
}
56
86
57
- implicit val instanceFormat : JsonFormat [Instance ] = jsonFormat8(Instance )
87
+ // JSON format for Instances
88
+ implicit val instanceFormat : JsonFormat [Instance ] = jsonFormat10(Instance )
58
89
}
59
90
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
+ */
60
101
final case class Instance (
61
102
id : Option [Long ],
62
103
host : String ,
63
104
portNumber : Long ,
64
105
name : String ,
65
- componentType : InstanceEnums . ComponentType ,
106
+ componentType : ComponentType ,
66
107
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 ]
69
112
)
113
+
114
+ /**
115
+ * Enumerations concerning instances
116
+ */
70
117
object InstanceEnums {
118
+
119
+ // Type to use when working with component types
71
120
type ComponentType = ComponentType .Value
121
+
122
+ /**
123
+ * ComponentType enumeration defining the valid types of delphi components
124
+ */
72
125
object ComponentType extends Enumeration {
73
126
val Crawler : Value = Value (" Crawler" )
74
- val ElasticSearch : Value = Value (" ElasticSearch" )
75
127
val WebApi : Value = Value (" WebApi" )
76
128
val WebApp : Value = Value (" WebApp" )
77
129
val DelphiManagement : Value = Value (" DelphiManagement" )
130
+ val ElasticSearch : Value = Value (" ElasticSearch" )
78
131
}
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
+ */
80
139
object InstanceState extends Enumeration {
140
+ val Deploying : Value = Value (" Deploying" )
81
141
val Running : Value = Value (" Running" )
82
- val Paused : Value = Value (" Paused" )
83
142
val Stopped : Value = Value (" Stopped" )
84
143
val Failed : Value = Value (" Failed" )
144
+ val Paused : Value = Value (" Paused" )
85
145
val NotReachable : Value = Value (" NotReachable" )
86
146
}
87
- }
147
+
148
+ }
0 commit comments