1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
/*-
* ============LICENSE_START=======================================================
* org.openecomp.aai
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package org.openecomp.aai.util;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.UUID;
import javax.xml.bind.Marshaller;
import org.eclipse.persistence.dynamic.DynamicEntity;
import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
import org.json.JSONException;
import org.json.JSONObject;
import org.openecomp.aai.dmaap.AAIDmaapEventJMSProducer;
import org.openecomp.aai.domain.notificationEvent.NotificationEvent;
import org.openecomp.aai.exceptions.AAIException;
import org.openecomp.aai.introspection.Introspector;
import org.openecomp.aai.introspection.Loader;
import org.openecomp.aai.introspection.exceptions.AAIUnknownObjectException;
public class StoreNotificationEvent {
private AAIDmaapEventJMSProducer messageProducer;
private String fromAppId = "";
private String transId = "";
private final String transactionId;
private final String sourceOfTruth;
/**
* Instantiates a new store notification event.
*/
public StoreNotificationEvent(String transactionId, String sourceOfTruth) {
this.messageProducer = new AAIDmaapEventJMSProducer();
this.transactionId = transactionId;
this.sourceOfTruth = sourceOfTruth;
}
/**
* Store event.
*
* @param eh
* the eh
* @param obj
* the obj
* @throws AAIException
* the AAI exception
*/
public void storeEvent(NotificationEvent.EventHeader eh, Object obj) throws AAIException {
if (obj == null) {
throw new AAIException("AAI_7350");
}
org.openecomp.aai.domain.notificationEvent.ObjectFactory factory = new org.openecomp.aai.domain.notificationEvent.ObjectFactory();
org.openecomp.aai.domain.notificationEvent.NotificationEvent ne = factory.createNotificationEvent();
if (eh.getId() == null) {
eh.setId(genDate2() + "-" + UUID.randomUUID().toString());
}
if (eh.getTimestamp() == null) {
eh.setTimestamp(genDate());
}
// there's no default, but i think we want to put this in hbase?
if (eh.getEntityLink() == null) {
eh.setEntityLink("UNK");
}
if (eh.getAction() == null) {
eh.setAction("UNK");
}
if (eh.getEventType() == null) {
eh.setEventType(AAIConfig.get("aai.notificationEvent.default.eventType", "UNK"));
}
if (eh.getDomain() == null) {
eh.setDomain(AAIConfig.get("aai.notificationEvent.default.domain", "UNK"));
}
if (eh.getSourceName() == null) {
eh.setSourceName(AAIConfig.get("aai.notificationEvent.default.sourceName", "UNK"));
}
if (eh.getSequenceNumber() == null) {
eh.setSequenceNumber(AAIConfig.get("aai.notificationEvent.default.sequenceNumber", "UNK"));
}
if (eh.getSeverity() == null) {
eh.setSeverity(AAIConfig.get("aai.notificationEvent.default.severity", "UNK"));
}
if (eh.getVersion() == null) {
eh.setVersion(AAIConfig.get("aai.notificationEvent.default.version", "UNK"));
}
ne.setCambriaPartition(AAIConstants.UEB_PUB_PARTITION_AAI);
ne.setEventHeader(eh);
ne.setEntity(obj);
try {
PojoUtils pu = new PojoUtils();
String entityJson = pu.getJsonFromObject(ne);
sendToDmaapJmsQueue(entityJson);
} catch (Exception e) {
throw new AAIException("AAI_7350", e);
}
}
/**
* Store dynamic event.
*
* @param notificationJaxbContext
* the notification jaxb context
* @param notificationVersion
* the notification version
* @param eventHeader
* the event header
* @param obj
* the obj
* @throws AAIException
* the AAI exception
*/
public void storeDynamicEvent(DynamicJAXBContext notificationJaxbContext, String notificationVersion, DynamicEntity eventHeader, DynamicEntity obj) throws AAIException {
if (obj == null) {
throw new AAIException("AAI_7350");
}
DynamicEntity notificationEvent = notificationJaxbContext.getDynamicType("inventory.aai.openecomp.org." + notificationVersion + ".NotificationEvent").newDynamicEntity();
if (eventHeader.get("id") == null) {
eventHeader.set("id", genDate2() + "-" + UUID.randomUUID().toString());
}
if (eventHeader.get("timestamp") == null) {
eventHeader.set("timestamp", genDate());
}
if (eventHeader.get("entityLink") == null) {
eventHeader.set("entityLink", "UNK");
}
if (eventHeader.get("action") == null) {
eventHeader.set("action", "UNK");
}
if (eventHeader.get("eventType") == null) {
eventHeader.set("eventType", AAIConfig.get("aai.notificationEvent.default.eventType", "UNK"));
}
if (eventHeader.get("domain") == null) {
eventHeader.set("domain", AAIConfig.get("aai.notificationEvent.default.domain", "UNK"));
}
if (eventHeader.get("sourceName") == null) {
eventHeader.set("sourceName", AAIConfig.get("aai.notificationEvent.default.sourceName", "UNK"));
}
if (eventHeader.get("sequenceNumber") == null) {
eventHeader.set("sequenceNumber", AAIConfig.get("aai.notificationEvent.default.sequenceNumber", "UNK"));
}
if (eventHeader.get("severity") == null) {
eventHeader.set("severity", AAIConfig.get("aai.notificationEvent.default.severity", "UNK"));
}
if (eventHeader.get("version") == null) {
eventHeader.set("version", AAIConfig.get("aai.notificationEvent.default.version", "UNK"));
}
if (notificationEvent.get("cambriaPartition") == null) {
notificationEvent.set("cambriaPartition", AAIConstants.UEB_PUB_PARTITION_AAI);
}
notificationEvent.set("eventHeader", eventHeader);
notificationEvent.set("entity", obj);
try {
StringWriter result = new StringWriter();
Marshaller marshaller = notificationJaxbContext.createMarshaller();
marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.MEDIA_TYPE, "application/json");
marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.JSON_INCLUDE_ROOT, false);
marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, false);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
marshaller.marshal(notificationEvent, result);
this.sendToDmaapJmsQueue(result.toString());
} catch (Exception e) {
throw new AAIException("AAI_7350", e);
}
}
public void storeEvent(Loader loader, Introspector eventHeader, Introspector obj) throws AAIException {
if (obj == null) {
throw new AAIException("AAI_7350");
}
try {
final Introspector notificationEvent = loader.introspectorFromName("notification-event");
if (eventHeader.getValue("id") == null) {
eventHeader.setValue("id", genDate2() + "-" + UUID.randomUUID().toString());
}
if (eventHeader.getValue("timestamp") == null) {
eventHeader.setValue("timestamp", genDate());
}
if (eventHeader.getValue("entity-link") == null) {
eventHeader.setValue("entity-link", "UNK");
}
if (eventHeader.getValue("action") == null) {
eventHeader.setValue("action", "UNK");
}
if (eventHeader.getValue("event-type") == null) {
eventHeader.setValue("event-type", AAIConfig.get("aai.notificationEvent.default.eventType", "UNK"));
}
if (eventHeader.getValue("domain") == null) {
eventHeader.setValue("domain", AAIConfig.get("aai.notificationEvent.default.domain", "UNK"));
}
if (eventHeader.getValue("source-name") == null) {
eventHeader.setValue("source-name", AAIConfig.get("aai.notificationEvent.default.sourceName", "UNK"));
}
if (eventHeader.getValue("sequence-number") == null) {
eventHeader.setValue("sequence-number", AAIConfig.get("aai.notificationEvent.default.sequenceNumber", "UNK"));
}
if (eventHeader.getValue("severity") == null) {
eventHeader.setValue("severity", AAIConfig.get("aai.notificationEvent.default.severity", "UNK"));
}
if (eventHeader.getValue("version") == null) {
eventHeader.setValue("version", AAIConfig.get("aai.notificationEvent.default.version", "UNK"));
}
if (notificationEvent.getValue("cambria-partition") == null) {
notificationEvent.setValue("cambria-partition", AAIConstants.UEB_PUB_PARTITION_AAI);
}
notificationEvent.setValue("event-header", eventHeader.getUnderlyingObject());
notificationEvent.setValue("entity", obj.getUnderlyingObject());
String entityJson = notificationEvent.marshal(false);
sendToDmaapJmsQueue(entityJson);
} catch (JSONException e) {
throw new AAIException("AAI_7350", e);
} catch (AAIUnknownObjectException e) {
throw new AAIException("AAI_7350", e);
}
}
private void sendToDmaapJmsQueue(String entityString) throws JSONException {
JSONObject entityJsonObject = new JSONObject(entityString);
JSONObject entityJsonObjectUpdated = new JSONObject();
JSONObject finalJson = new JSONObject();
JSONObject entityHeader = entityJsonObject.getJSONObject("event-header");
String cambriaPartition = entityJsonObject.getString("cambria.partition");
entityJsonObject.remove("event-header");
entityJsonObject.remove("cambria.partition");
entityJsonObjectUpdated.put("event-header", entityHeader);
entityJsonObjectUpdated.put("cambria.partition", cambriaPartition);
String transId = entityHeader.getString("id");
String fromAppId = entityHeader.getString("source-name");
Iterator<String> iter = entityJsonObject.keys();
JSONObject entity = new JSONObject();
if (iter.hasNext()) {
entity = entityJsonObject.getJSONObject(iter.next());
}
entityJsonObjectUpdated.put("entity", entity);
finalJson.put("event-topic", "AAI-EVENT");
finalJson.put("transId", transId);
finalJson.put("fromAppId", fromAppId);
finalJson.put("fullId", "");
finalJson.put("aaiEventPayload", entityJsonObjectUpdated);
messageProducer.sendMessageToDefaultDestination(finalJson);
}
/**
* Gen date.
*
* @return the string
*/
public static String genDate() {
FormatDate fd = new FormatDate("YYMMdd-HH:mm:ss:SSS");
return fd.getDateTime();
}
/**
* Gen date 2.
*
* @return the string
*/
public static String genDate2() {
FormatDate fd = new FormatDate("YYYYMMddHHmmss");
return fd.getDateTime();
}
}
|