2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.aai.util;
23 import java.io.StringWriter;
24 import java.util.Iterator;
25 import java.util.UUID;
27 import javax.xml.bind.Marshaller;
29 import org.eclipse.persistence.dynamic.DynamicEntity;
30 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
31 import org.json.JSONException;
32 import org.json.JSONObject;
33 import org.openecomp.aai.dmaap.AAIDmaapEventJMSProducer;
34 import org.openecomp.aai.domain.notificationEvent.NotificationEvent;
35 import org.openecomp.aai.exceptions.AAIException;
36 import org.openecomp.aai.introspection.Introspector;
37 import org.openecomp.aai.introspection.Loader;
38 import org.openecomp.aai.introspection.exceptions.AAIUnknownObjectException;
40 public class StoreNotificationEvent {
42 private AAIDmaapEventJMSProducer messageProducer;
43 private String fromAppId = "";
44 private String transId = "";
45 private final String transactionId;
46 private final String sourceOfTruth;
48 * Instantiates a new store notification event.
50 public StoreNotificationEvent(String transactionId, String sourceOfTruth) {
51 this.messageProducer = new AAIDmaapEventJMSProducer();
52 this.transactionId = transactionId;
53 this.sourceOfTruth = sourceOfTruth;
63 * @throws AAIException
66 public void storeEvent(NotificationEvent.EventHeader eh, Object obj) throws AAIException {
69 throw new AAIException("AAI_7350");
72 org.openecomp.aai.domain.notificationEvent.ObjectFactory factory = new org.openecomp.aai.domain.notificationEvent.ObjectFactory();
74 org.openecomp.aai.domain.notificationEvent.NotificationEvent ne = factory.createNotificationEvent();
76 if (eh.getId() == null) {
77 eh.setId(genDate2() + "-" + UUID.randomUUID().toString());
79 if (eh.getTimestamp() == null) {
80 eh.setTimestamp(genDate());
83 // there's no default, but i think we want to put this in hbase?
85 if (eh.getEntityLink() == null) {
86 eh.setEntityLink("UNK");
89 if (eh.getAction() == null) {
93 if (eh.getEventType() == null) {
94 eh.setEventType(AAIConfig.get("aai.notificationEvent.default.eventType", "UNK"));
97 if (eh.getDomain() == null) {
98 eh.setDomain(AAIConfig.get("aai.notificationEvent.default.domain", "UNK"));
101 if (eh.getSourceName() == null) {
102 eh.setSourceName(AAIConfig.get("aai.notificationEvent.default.sourceName", "UNK"));
105 if (eh.getSequenceNumber() == null) {
106 eh.setSequenceNumber(AAIConfig.get("aai.notificationEvent.default.sequenceNumber", "UNK"));
109 if (eh.getSeverity() == null) {
110 eh.setSeverity(AAIConfig.get("aai.notificationEvent.default.severity", "UNK"));
113 if (eh.getVersion() == null) {
114 eh.setVersion(AAIConfig.get("aai.notificationEvent.default.version", "UNK"));
117 ne.setCambriaPartition(AAIConstants.UEB_PUB_PARTITION_AAI);
118 ne.setEventHeader(eh);
122 PojoUtils pu = new PojoUtils();
123 String entityJson = pu.getJsonFromObject(ne);
124 sendToDmaapJmsQueue(entityJson);
125 } catch (Exception e) {
126 throw new AAIException("AAI_7350", e);
131 * Store dynamic event.
133 * @param notificationJaxbContext
134 * the notification jaxb context
135 * @param notificationVersion
136 * the notification version
141 * @throws AAIException
144 public void storeDynamicEvent(DynamicJAXBContext notificationJaxbContext, String notificationVersion, DynamicEntity eventHeader, DynamicEntity obj) throws AAIException {
147 throw new AAIException("AAI_7350");
150 DynamicEntity notificationEvent = notificationJaxbContext.getDynamicType("inventory.aai.att.com." + notificationVersion + ".NotificationEvent").newDynamicEntity();
152 if (eventHeader.get("id") == null) {
153 eventHeader.set("id", genDate2() + "-" + UUID.randomUUID().toString());
156 if (eventHeader.get("timestamp") == null) {
157 eventHeader.set("timestamp", genDate());
160 if (eventHeader.get("entityLink") == null) {
161 eventHeader.set("entityLink", "UNK");
164 if (eventHeader.get("action") == null) {
165 eventHeader.set("action", "UNK");
168 if (eventHeader.get("eventType") == null) {
169 eventHeader.set("eventType", AAIConfig.get("aai.notificationEvent.default.eventType", "UNK"));
172 if (eventHeader.get("domain") == null) {
173 eventHeader.set("domain", AAIConfig.get("aai.notificationEvent.default.domain", "UNK"));
176 if (eventHeader.get("sourceName") == null) {
177 eventHeader.set("sourceName", AAIConfig.get("aai.notificationEvent.default.sourceName", "UNK"));
180 if (eventHeader.get("sequenceNumber") == null) {
181 eventHeader.set("sequenceNumber", AAIConfig.get("aai.notificationEvent.default.sequenceNumber", "UNK"));
184 if (eventHeader.get("severity") == null) {
185 eventHeader.set("severity", AAIConfig.get("aai.notificationEvent.default.severity", "UNK"));
188 if (eventHeader.get("version") == null) {
189 eventHeader.set("version", AAIConfig.get("aai.notificationEvent.default.version", "UNK"));
192 if (notificationEvent.get("cambriaPartition") == null) {
193 notificationEvent.set("cambriaPartition", AAIConstants.UEB_PUB_PARTITION_AAI);
196 notificationEvent.set("eventHeader", eventHeader);
197 notificationEvent.set("entity", obj);
200 StringWriter result = new StringWriter();
202 Marshaller marshaller = notificationJaxbContext.createMarshaller();
203 marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.MEDIA_TYPE, "application/json");
204 marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.JSON_INCLUDE_ROOT, false);
205 marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, false);
206 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
207 marshaller.marshal(notificationEvent, result);
209 this.sendToDmaapJmsQueue(result.toString());
211 } catch (Exception e) {
212 throw new AAIException("AAI_7350", e);
216 public void storeEvent(Loader loader, Introspector eventHeader, Introspector obj) throws AAIException {
218 throw new AAIException("AAI_7350");
222 final Introspector notificationEvent = loader.introspectorFromName("notification-event");
224 if (eventHeader.getValue("id") == null) {
225 eventHeader.setValue("id", genDate2() + "-" + UUID.randomUUID().toString());
228 if (eventHeader.getValue("timestamp") == null) {
229 eventHeader.setValue("timestamp", genDate());
232 if (eventHeader.getValue("entity-link") == null) {
233 eventHeader.setValue("entity-link", "UNK");
236 if (eventHeader.getValue("action") == null) {
237 eventHeader.setValue("action", "UNK");
240 if (eventHeader.getValue("event-type") == null) {
241 eventHeader.setValue("event-type", AAIConfig.get("aai.notificationEvent.default.eventType", "UNK"));
244 if (eventHeader.getValue("domain") == null) {
245 eventHeader.setValue("domain", AAIConfig.get("aai.notificationEvent.default.domain", "UNK"));
248 if (eventHeader.getValue("source-name") == null) {
249 eventHeader.setValue("source-name", AAIConfig.get("aai.notificationEvent.default.sourceName", "UNK"));
252 if (eventHeader.getValue("sequence-number") == null) {
253 eventHeader.setValue("sequence-number", AAIConfig.get("aai.notificationEvent.default.sequenceNumber", "UNK"));
256 if (eventHeader.getValue("severity") == null) {
257 eventHeader.setValue("severity", AAIConfig.get("aai.notificationEvent.default.severity", "UNK"));
260 if (eventHeader.getValue("version") == null) {
261 eventHeader.setValue("version", AAIConfig.get("aai.notificationEvent.default.version", "UNK"));
264 if (notificationEvent.getValue("cambria-partition") == null) {
265 notificationEvent.setValue("cambria-partition", AAIConstants.UEB_PUB_PARTITION_AAI);
268 notificationEvent.setValue("event-header", eventHeader.getUnderlyingObject());
269 notificationEvent.setValue("entity", obj.getUnderlyingObject());
271 String entityJson = notificationEvent.marshal(false);
272 sendToDmaapJmsQueue(entityJson);
273 } catch (JSONException e) {
274 throw new AAIException("AAI_7350", e);
275 } catch (AAIUnknownObjectException e) {
276 throw new AAIException("AAI_7350", e);
280 private void sendToDmaapJmsQueue(String entityString) throws JSONException {
282 JSONObject entityJsonObject = new JSONObject(entityString);
284 JSONObject entityJsonObjectUpdated = new JSONObject();
285 JSONObject finalJson = new JSONObject();
287 JSONObject entityHeader = entityJsonObject.getJSONObject("event-header");
288 String cambriaPartition = entityJsonObject.getString("cambria.partition");
290 entityJsonObject.remove("event-header");
291 entityJsonObject.remove("cambria.partition");
293 entityJsonObjectUpdated.put("event-header", entityHeader);
294 entityJsonObjectUpdated.put("cambria.partition", cambriaPartition);
296 String transId = entityHeader.getString("id");
297 String fromAppId = entityHeader.getString("source-name");
299 Iterator<String> iter = entityJsonObject.keys();
300 JSONObject entity = new JSONObject();
301 if (iter.hasNext()) {
302 entity = entityJsonObject.getJSONObject(iter.next());
305 entityJsonObjectUpdated.put("entity", entity);
307 finalJson.put("event-topic", "AAI-EVENT");
308 finalJson.put("transId", transId);
309 finalJson.put("fromAppId", fromAppId);
310 finalJson.put("fullId", "");
311 finalJson.put("aaiEventPayload", entityJsonObjectUpdated);
313 messageProducer.sendMessageToDefaultDestination(finalJson);
321 public static String genDate() {
322 FormatDate fd = new FormatDate("YYYYMMdd-HH:mm:ss:SSS");
323 return fd.getDateTime();
331 public static String genDate2() {
332 FormatDate fd = new FormatDate("YYYYMMddHHmmss");
333 return fd.getDateTime();