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