1
1
package de .upb .cs .swt .delphi .instanceregistry .io .swagger .client .model
2
2
3
3
import akka .http .scaladsl .marshallers .sprayjson .SprayJsonSupport
4
+ import akka .http .scaladsl .model .DateTime
4
5
import de .upb .cs .swt .delphi .instanceregistry .io .swagger .client .model .EventEnums .EventType
5
6
import de .upb .cs .swt .delphi .instanceregistry .io .swagger .client .model .InstanceEnums .ComponentType
6
7
import spray .json .{DefaultJsonProtocol , DeserializationException , JsObject , JsString , JsValue , JsonFormat }
@@ -79,9 +80,31 @@ trait EventJsonSupport extends SprayJsonSupport with DefaultJsonProtocol with In
79
80
}
80
81
81
82
}
83
+ // Custom JSON format for event TimeStamp.
84
+ implicit val timestampFormat : JsonFormat [DateTime ] = new JsonFormat [DateTime ] {
85
+ /**
86
+ * Custom write method for serialization of DateTime
87
+ * @param obj DateTime object to serialize
88
+ * @throws DeserializationException Exception in case of wrong input
89
+ */
90
+ override def write (obj : DateTime ) = JsString (obj.toIsoDateTimeString())
91
+ /**
92
+ * Custom read method for deserialization of DateTime
93
+ * @param json JsValue that is to be deserialized
94
+ * @throws DeserializationException Exception when JsValue is in incorrect format
95
+ */
96
+ override def read (json : JsValue ): DateTime = json match {
97
+ case JsString (value) =>
98
+ DateTime .fromIsoDateTimeString(value) match {
99
+ case Some (date) => date
100
+ case _ => throw DeserializationException (" Failed to parse date time [" + value + " ]." )
101
+ }
102
+ case _ => throw DeserializationException (" Failed to parse json string [" + json + " ]." )
103
+ }
104
+ }
82
105
83
106
// JSON format for RegistryEvents
84
- implicit val eventFormat : JsonFormat [RegistryEvent ] = jsonFormat2 (RegistryEvent )
107
+ implicit val eventFormat : JsonFormat [RegistryEvent ] = jsonFormat3 (RegistryEvent )
85
108
86
109
// JSON format for an NumbersChangedPayload
87
110
implicit val numbersChangedPayloadFormat : JsonFormat [NumbersChangedPayload ] = jsonFormat2(NumbersChangedPayload )
@@ -103,10 +126,12 @@ trait EventJsonSupport extends SprayJsonSupport with DefaultJsonProtocol with In
103
126
* The RegistryEvent used for communicating with the management application
104
127
* @param eventType Type of the event
105
128
* @param payload Payload of the event, depends on the type
129
+ * @param timestamp TimeStamp of the event
106
130
*/
107
131
final case class RegistryEvent (
108
132
eventType : EventType .Value ,
109
- payload : RegistryEventPayload
133
+ payload : RegistryEventPayload ,
134
+ timestamp : DateTime
110
135
)
111
136
112
137
/**
@@ -118,59 +143,59 @@ object RegistryEventFactory {
118
143
* Creates a new NumbersChangedEvent. Sets EventType and payload accordingly.
119
144
* @param componentType ComponentType which's numbers have been updated
120
145
* @param newNumber New number of components of the specified type
121
- * @return RegistryEvent with the respective type and payload .
146
+ * @return RegistryEvent with the respective respective type, payload and current timestamp .
122
147
*/
123
148
def createNumbersChangedEvent (componentType : ComponentType , newNumber : Int ) : RegistryEvent =
124
- RegistryEvent (EventType .NumbersChangedEvent , NumbersChangedPayload (componentType, newNumber))
149
+ RegistryEvent (EventType .NumbersChangedEvent , NumbersChangedPayload (componentType, newNumber), DateTime .now )
125
150
126
151
/**
127
152
* Creates a new InstanceAddedEvent. Sets EventType and payload accordingly.
128
153
* @param instance Instance that has been added.
129
- * @return RegistryEvent with the respective type and payload .
154
+ * @return RegistryEvent with the respective type, payload and current timestamp .
130
155
*/
131
156
def createInstanceAddedEvent (instance : Instance ) : RegistryEvent =
132
- RegistryEvent (EventType .InstanceAddedEvent , InstancePayload (instance))
157
+ RegistryEvent (EventType .InstanceAddedEvent , InstancePayload (instance), DateTime .now )
133
158
134
159
/**
135
160
* Creates a new InstanceRemovedEvent. Sets EventType and payload accordingly.
136
161
* @param instance Instance that has been removed.
137
- * @return RegistryEvent with the respective type and payload .
162
+ * @return RegistryEvent with the respective type, payload and current timestamp .
138
163
*/
139
164
def createInstanceRemovedEvent (instance : Instance ) : RegistryEvent =
140
- RegistryEvent (EventType .InstanceRemovedEvent , InstancePayload (instance))
165
+ RegistryEvent (EventType .InstanceRemovedEvent , InstancePayload (instance), DateTime .now )
141
166
142
167
/**
143
168
* Creates a new StateChangedEvent. Sets EventType and payload accordingly.
144
169
* @param instance Instance which's state was changed.
145
- * @return RegistryEvent with tht respective type and payload .
170
+ * @return RegistryEvent with tht respective type, payload and current timestamp .
146
171
*/
147
172
def createStateChangedEvent (instance : Instance ) : RegistryEvent =
148
- RegistryEvent (EventType .StateChangedEvent , InstancePayload (instance))
173
+ RegistryEvent (EventType .StateChangedEvent , InstancePayload (instance), DateTime .now )
149
174
150
175
/**
151
176
* Creates a new DockerOperationErrorEvent. Sets EventType and payload accordingly.
152
177
* @param affectedInstance Option[Instance] containing the instance that may be affected
153
178
* @param message Error message
154
- * @return RegistryEvent with the respective type and payload .
179
+ * @return RegistryEvent with the respective respective type, payload and current timestamp .
155
180
*/
156
181
def createDockerOperationErrorEvent (affectedInstance : Option [Instance ], message : String ) : RegistryEvent =
157
- RegistryEvent (EventType .DockerOperationErrorEvent , DockerOperationErrorPayload (affectedInstance, message))
182
+ RegistryEvent (EventType .DockerOperationErrorEvent , DockerOperationErrorPayload (affectedInstance, message), DateTime .now )
158
183
159
184
/**
160
185
* Creates a new LinkAddedEvent. Sets EventType and payload accordingly
161
186
* @param link Link that was added
162
- * @return RegistryEvent with the respective type and payload
187
+ * @return RegistryEvent with the respective type, payload and current timestamp.
163
188
*/
164
189
def createLinkAddedEvent (link : InstanceLink , instanceFrom : Instance , instanceTo : Instance ) : RegistryEvent =
165
- RegistryEvent (EventType .LinkAddedEvent , InstanceLinkPayload (link, instanceFrom, instanceTo))
190
+ RegistryEvent (EventType .LinkAddedEvent , InstanceLinkPayload (link, instanceFrom, instanceTo), DateTime .now )
166
191
167
192
/**
168
193
* Creates a new LinkStateChangedEvent. Sets EventType and payload accordingly.
169
194
* @param link Link whichs state has been changed
170
- * @return RegistryEvent with the respective type and payload
195
+ * @return RegistryEvent with the respective type, payload and current timestamp.
171
196
*/
172
197
def createLinkStateChangedEvent (link : InstanceLink , instanceFrom : Instance , instanceTo : Instance ) : RegistryEvent =
173
- RegistryEvent (EventType .LinkStateChangedEvent , InstanceLinkPayload (link, instanceFrom, instanceTo))
198
+ RegistryEvent (EventType .LinkStateChangedEvent , InstanceLinkPayload (link, instanceFrom, instanceTo), DateTime .now )
174
199
}
175
200
176
201
/**
0 commit comments