2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 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=========================================================
20 package org.onap.aai.util;
22 import com.att.eelf.configuration.EELFLogger;
23 import com.att.eelf.configuration.EELFManager;
24 import org.eclipse.persistence.dynamic.DynamicEntity;
25 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
26 import org.json.JSONException;
27 import org.json.JSONObject;
28 import org.onap.aai.dmaap.AAIDmaapEventJMSProducer;
29 import org.onap.aai.dmaap.MessageProducer;
30 import org.onap.aai.domain.notificationEvent.NotificationEvent;
31 import org.onap.aai.exceptions.AAIException;
32 import org.onap.aai.introspection.Introspector;
33 import org.onap.aai.introspection.Loader;
34 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
35 import org.springframework.context.ApplicationContext;
36 import org.springframework.core.env.Environment;
38 import javax.xml.bind.Marshaller;
39 import java.io.StringWriter;
40 import java.util.Iterator;
41 import java.util.UUID;
43 public class StoreNotificationEvent {
45 private static final EELFLogger logger = EELFManager.getInstance().getLogger(StoreNotificationEvent.class);
46 private MessageProducer messageProducer;
47 private String fromAppId = "";
48 private String transId = "";
49 private final String transactionId;
50 private final String sourceOfTruth;
52 private ApplicationContext context;
53 private Environment env;
56 * Instantiates a new store notification event.
58 public StoreNotificationEvent(String transactionId, String sourceOfTruth) {
59 this.messageProducer = new AAIDmaapEventJMSProducer();
60 this.transactionId = transactionId;
61 this.sourceOfTruth = sourceOfTruth;
64 public StoreNotificationEvent(AAIDmaapEventJMSProducer producer, String transactionId, String sourceOfTruth) {
65 this.messageProducer = producer;
66 this.transactionId = transactionId;
67 this.sourceOfTruth = sourceOfTruth;
77 * @throws AAIException
80 public String storeEvent(NotificationEvent.EventHeader eh, Object obj) throws AAIException {
83 throw new AAIException("AAI_7350");
86 org.onap.aai.domain.notificationEvent.ObjectFactory factory = new org.onap.aai.domain.notificationEvent.ObjectFactory();
88 org.onap.aai.domain.notificationEvent.NotificationEvent ne = factory.createNotificationEvent();
90 if (eh.getId() == null) {
91 eh.setId(genDate2() + "-" + UUID.randomUUID().toString());
93 if (eh.getTimestamp() == null) {
94 eh.setTimestamp(genDate());
97 // there's no default, but i think we want to put this in hbase?
99 if (eh.getEntityLink() == null) {
100 eh.setEntityLink("UNK");
103 if (eh.getAction() == null) {
107 if (eh.getEventType() == null) {
108 eh.setEventType(AAIConfig.get("aai.notificationEvent.default.eventType", "UNK"));
111 if (eh.getDomain() == null) {
112 eh.setDomain(AAIConfig.get("aai.notificationEvent.default.domain", "UNK"));
115 if (eh.getSourceName() == null) {
116 eh.setSourceName(AAIConfig.get("aai.notificationEvent.default.sourceName", "UNK"));
119 if (eh.getSequenceNumber() == null) {
120 eh.setSequenceNumber(AAIConfig.get("aai.notificationEvent.default.sequenceNumber", "UNK"));
123 if (eh.getSeverity() == null) {
124 eh.setSeverity(AAIConfig.get("aai.notificationEvent.default.severity", "UNK"));
127 if (eh.getVersion() == null) {
128 eh.setVersion(AAIConfig.get("aai.notificationEvent.default.version", "UNK"));
131 ne.setCambriaPartition(AAIConstants.UEB_PUB_PARTITION_AAI);
132 ne.setEventHeader(eh);
136 PojoUtils pu = new PojoUtils();
137 String entityJson = pu.getJsonFromObject(ne);
138 sendToDmaapJmsQueue(entityJson);
140 } catch (Exception e) {
141 throw new AAIException("AAI_7350", e);
146 * Store dynamic event.
148 * @param notificationJaxbContext
149 * the notification jaxb context
150 * @param notificationVersion
151 * the notification version
156 * @throws AAIException
159 public void storeDynamicEvent(DynamicJAXBContext notificationJaxbContext, String notificationVersion, DynamicEntity eventHeader, DynamicEntity obj) throws AAIException {
162 throw new AAIException("AAI_7350");
165 DynamicEntity notificationEvent = notificationJaxbContext.getDynamicType("inventory.aai.onap.org." + notificationVersion + ".NotificationEvent").newDynamicEntity();
167 if (eventHeader.get("id") == null) {
168 eventHeader.set("id", genDate2() + "-" + UUID.randomUUID().toString());
171 if (eventHeader.get("timestamp") == null) {
172 eventHeader.set("timestamp", genDate());
175 if (eventHeader.get("entityLink") == null) {
176 eventHeader.set("entityLink", "UNK");
179 if (eventHeader.get("action") == null) {
180 eventHeader.set("action", "UNK");
183 if (eventHeader.get("eventType") == null) {
184 eventHeader.set("eventType", AAIConfig.get("aai.notificationEvent.default.eventType", "UNK"));
187 if (eventHeader.get("domain") == null) {
188 eventHeader.set("domain", AAIConfig.get("aai.notificationEvent.default.domain", "UNK"));
191 if (eventHeader.get("sourceName") == null) {
192 eventHeader.set("sourceName", AAIConfig.get("aai.notificationEvent.default.sourceName", "UNK"));
195 if (eventHeader.get("sequenceNumber") == null) {
196 eventHeader.set("sequenceNumber", AAIConfig.get("aai.notificationEvent.default.sequenceNumber", "UNK"));
199 if (eventHeader.get("severity") == null) {
200 eventHeader.set("severity", AAIConfig.get("aai.notificationEvent.default.severity", "UNK"));
203 if (eventHeader.get("version") == null) {
204 eventHeader.set("version", AAIConfig.get("aai.notificationEvent.default.version", "UNK"));
207 if (notificationEvent.get("cambriaPartition") == null) {
208 notificationEvent.set("cambriaPartition", AAIConstants.UEB_PUB_PARTITION_AAI);
211 notificationEvent.set("eventHeader", eventHeader);
212 notificationEvent.set("entity", obj);
215 StringWriter result = new StringWriter();
217 Marshaller marshaller = notificationJaxbContext.createMarshaller();
218 marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.MEDIA_TYPE, "application/json");
219 marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.JSON_INCLUDE_ROOT, false);
220 marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, false);
221 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
222 marshaller.marshal(notificationEvent, result);
224 this.sendToDmaapJmsQueue(result.toString());
226 } catch (Exception e) {
227 throw new AAIException("AAI_7350", e);
231 public String storeEvent(Loader loader, Introspector eventHeader, Introspector obj) throws AAIException {
233 throw new AAIException("AAI_7350");
237 final Introspector notificationEvent = loader.introspectorFromName("notification-event");
239 if (eventHeader.getValue("id") == null) {
240 eventHeader.setValue("id", genDate2() + "-" + UUID.randomUUID().toString());
243 if (eventHeader.getValue("timestamp") == null) {
244 eventHeader.setValue("timestamp", genDate());
247 if (eventHeader.getValue("entity-link") == null) {
248 eventHeader.setValue("entity-link", "UNK");
251 if (eventHeader.getValue("action") == null) {
252 eventHeader.setValue("action", "UNK");
255 if (eventHeader.getValue("event-type") == null) {
256 eventHeader.setValue("event-type", AAIConfig.get("aai.notificationEvent.default.eventType", "UNK"));
259 if (eventHeader.getValue("domain") == null) {
260 eventHeader.setValue("domain", AAIConfig.get("aai.notificationEvent.default.domain", "UNK"));
263 if (eventHeader.getValue("source-name") == null) {
264 eventHeader.setValue("source-name", AAIConfig.get("aai.notificationEvent.default.sourceName", "UNK"));
267 if (eventHeader.getValue("sequence-number") == null) {
268 eventHeader.setValue("sequence-number", AAIConfig.get("aai.notificationEvent.default.sequenceNumber", "UNK"));
271 if (eventHeader.getValue("severity") == null) {
272 eventHeader.setValue("severity", AAIConfig.get("aai.notificationEvent.default.severity", "UNK"));
275 if (eventHeader.getValue("version") == null) {
276 eventHeader.setValue("version", AAIConfig.get("aai.notificationEvent.default.version", "UNK"));
279 if (notificationEvent.getValue("cambria-partition") == null) {
280 notificationEvent.setValue("cambria-partition", AAIConstants.UEB_PUB_PARTITION_AAI);
283 notificationEvent.setValue("event-header", eventHeader.getUnderlyingObject());
284 notificationEvent.setValue("entity", obj.getUnderlyingObject());
286 String entityJson = notificationEvent.marshal(false);
287 sendToDmaapJmsQueue(entityJson);
289 } catch (JSONException e) {
290 throw new AAIException("AAI_7350", e);
291 } catch (AAIUnknownObjectException e) {
292 throw new AAIException("AAI_7350", e);
296 private void sendToDmaapJmsQueue(String entityString) throws JSONException {
298 JSONObject entityJsonObject = new JSONObject(entityString);
300 JSONObject entityJsonObjectUpdated = new JSONObject();
301 JSONObject finalJson = new JSONObject();
303 JSONObject entityHeader = entityJsonObject.getJSONObject("event-header");
304 String cambriaPartition = entityJsonObject.getString("cambria.partition");
306 entityJsonObject.remove("event-header");
307 entityJsonObject.remove("cambria.partition");
309 entityJsonObjectUpdated.put("event-header", entityHeader);
310 entityJsonObjectUpdated.put("cambria.partition", cambriaPartition);
312 String transId = entityHeader.getString("id");
313 String fromAppId = entityHeader.getString("source-name");
315 Iterator<String> iter = entityJsonObject.keys();
316 JSONObject entity = new JSONObject();
317 if (iter.hasNext()) {
318 entity = entityJsonObject.getJSONObject(iter.next());
321 entityJsonObjectUpdated.put("entity", entity);
323 finalJson.put("event-topic", "AAI-EVENT");
324 finalJson.put("transId", transId);
325 finalJson.put("fromAppId", fromAppId);
326 finalJson.put("fullId", "");
327 finalJson.put("aaiEventPayload", entityJsonObjectUpdated);
329 messageProducer.sendMessageToDefaultDestination(finalJson);
337 public static String genDate() {
338 FormatDate fd = new FormatDate("YYYYMMdd-HH:mm:ss:SSS");
339 return fd.getDateTime();
347 public static String genDate2() {
348 FormatDate fd = new FormatDate("YYYYMMddHHmmss");
349 return fd.getDateTime();