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=========================================================
21 package org.onap.aai.util;
23 import com.att.eelf.configuration.EELFLogger;
24 import com.att.eelf.configuration.EELFManager;
26 import java.io.StringWriter;
27 import java.util.Iterator;
28 import java.util.UUID;
30 import javax.xml.bind.Marshaller;
32 import org.eclipse.persistence.dynamic.DynamicEntity;
33 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
34 import org.json.JSONException;
35 import org.json.JSONObject;
36 import org.onap.aai.dmaap.AAIDmaapEventJMSProducer;
37 import org.onap.aai.dmaap.MessageProducer;
38 import org.onap.aai.domain.notificationEvent.NotificationEvent;
39 import org.onap.aai.exceptions.AAIException;
40 import org.onap.aai.introspection.Introspector;
41 import org.onap.aai.introspection.Loader;
42 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
43 import org.springframework.context.ApplicationContext;
44 import org.springframework.core.env.Environment;
46 public class StoreNotificationEvent {
48 private static final EELFLogger logger = EELFManager.getInstance().getLogger(StoreNotificationEvent.class);
50 private MessageProducer messageProducer;
51 private String fromAppId = "";
52 private String transId = "";
53 private final String transactionId;
54 private final String sourceOfTruth;
56 private ApplicationContext context;
57 private Environment env;
60 * Instantiates a new store notification event.
62 public StoreNotificationEvent(String transactionId, String sourceOfTruth) {
63 this.messageProducer = new AAIDmaapEventJMSProducer();
64 this.transactionId = transactionId;
65 this.sourceOfTruth = sourceOfTruth;
68 public StoreNotificationEvent(AAIDmaapEventJMSProducer producer, String transactionId, String sourceOfTruth) {
69 this.messageProducer = producer;
70 this.transactionId = transactionId;
71 this.sourceOfTruth = sourceOfTruth;
81 * @throws AAIException
84 public String storeEvent(NotificationEvent.EventHeader eh, Object obj) throws AAIException {
87 throw new AAIException("AAI_7350");
90 org.onap.aai.domain.notificationEvent.ObjectFactory factory =
91 new org.onap.aai.domain.notificationEvent.ObjectFactory();
93 org.onap.aai.domain.notificationEvent.NotificationEvent ne = factory.createNotificationEvent();
95 if (eh.getId() == null) {
96 eh.setId(genDate2() + "-" + UUID.randomUUID().toString());
98 if (eh.getTimestamp() == null) {
99 eh.setTimestamp(genDate());
102 // there's no default, but i think we want to put this in hbase?
104 if (eh.getEntityLink() == null) {
105 eh.setEntityLink("UNK");
108 if (eh.getAction() == null) {
112 if (eh.getEventType() == null) {
113 eh.setEventType(AAIConfig.get("aai.notificationEvent.default.eventType", "UNK"));
116 if (eh.getDomain() == null) {
117 eh.setDomain(AAIConfig.get("aai.notificationEvent.default.domain", "UNK"));
120 if (eh.getSourceName() == null) {
121 eh.setSourceName(AAIConfig.get("aai.notificationEvent.default.sourceName", "UNK"));
124 if (eh.getSequenceNumber() == null) {
125 eh.setSequenceNumber(AAIConfig.get("aai.notificationEvent.default.sequenceNumber", "UNK"));
128 if (eh.getSeverity() == null) {
129 eh.setSeverity(AAIConfig.get("aai.notificationEvent.default.severity", "UNK"));
132 if (eh.getVersion() == null) {
133 eh.setVersion(AAIConfig.get("aai.notificationEvent.default.version", "UNK"));
136 ne.setCambriaPartition(AAIConstants.UEB_PUB_PARTITION_AAI);
137 ne.setEventHeader(eh);
141 PojoUtils pu = new PojoUtils();
142 String entityJson = pu.getJsonFromObject(ne);
143 sendToDmaapJmsQueue(entityJson);
145 } catch (Exception e) {
146 throw new AAIException("AAI_7350", e);
151 * Store dynamic event.
153 * @param notificationJaxbContext
154 * the notification jaxb context
155 * @param notificationVersion
156 * the notification version
161 * @throws AAIException
164 public void storeDynamicEvent(DynamicJAXBContext notificationJaxbContext, String notificationVersion,
165 DynamicEntity eventHeader, DynamicEntity obj) throws AAIException {
168 throw new AAIException("AAI_7350");
171 DynamicEntity notificationEvent = notificationJaxbContext
172 .getDynamicType("inventory.aai.onap.org." + notificationVersion + ".NotificationEvent")
175 if (eventHeader.get("id") == null) {
176 eventHeader.set("id", genDate2() + "-" + UUID.randomUUID().toString());
179 if (eventHeader.get("timestamp") == null) {
180 eventHeader.set("timestamp", genDate());
183 if (eventHeader.get("entityLink") == null) {
184 eventHeader.set("entityLink", "UNK");
187 if (eventHeader.get("action") == null) {
188 eventHeader.set("action", "UNK");
191 if (eventHeader.get("eventType") == null) {
192 eventHeader.set("eventType", AAIConfig.get("aai.notificationEvent.default.eventType", "UNK"));
195 if (eventHeader.get("domain") == null) {
196 eventHeader.set("domain", AAIConfig.get("aai.notificationEvent.default.domain", "UNK"));
199 if (eventHeader.get("sourceName") == null) {
200 eventHeader.set("sourceName", AAIConfig.get("aai.notificationEvent.default.sourceName", "UNK"));
203 if (eventHeader.get("sequenceNumber") == null) {
204 eventHeader.set("sequenceNumber", AAIConfig.get("aai.notificationEvent.default.sequenceNumber", "UNK"));
207 if (eventHeader.get("severity") == null) {
208 eventHeader.set("severity", AAIConfig.get("aai.notificationEvent.default.severity", "UNK"));
211 if (eventHeader.get("version") == null) {
212 eventHeader.set("version", AAIConfig.get("aai.notificationEvent.default.version", "UNK"));
215 if (notificationEvent.get("cambriaPartition") == null) {
216 notificationEvent.set("cambriaPartition", AAIConstants.UEB_PUB_PARTITION_AAI);
219 notificationEvent.set("eventHeader", eventHeader);
220 notificationEvent.set("entity", obj);
223 StringWriter result = new StringWriter();
225 Marshaller marshaller = notificationJaxbContext.createMarshaller();
226 marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.MEDIA_TYPE, "application/json");
227 marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.JSON_INCLUDE_ROOT, false);
228 marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, false);
229 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
230 marshaller.marshal(notificationEvent, result);
231 this.sendToDmaapJmsQueue(result.toString());
233 } catch (Exception e) {
234 throw new AAIException("AAI_7350", e);
238 public String storeEvent(Loader loader, Introspector eventHeader, Introspector obj) throws AAIException {
240 throw new AAIException("AAI_7350");
244 final Introspector notificationEvent = loader.introspectorFromName("notification-event");
246 if (eventHeader.getValue("id") == null) {
247 eventHeader.setValue("id", genDate2() + "-" + UUID.randomUUID().toString());
250 if (eventHeader.getValue("timestamp") == null) {
251 eventHeader.setValue("timestamp", genDate());
254 if (eventHeader.getValue("entity-link") == null) {
255 eventHeader.setValue("entity-link", "UNK");
258 if (eventHeader.getValue("action") == null) {
259 eventHeader.setValue("action", "UNK");
262 if (eventHeader.getValue("event-type") == null) {
263 eventHeader.setValue("event-type", AAIConfig.get("aai.notificationEvent.default.eventType", "UNK"));
266 if (eventHeader.getValue("domain") == null) {
267 eventHeader.setValue("domain", AAIConfig.get("aai.notificationEvent.default.domain", "UNK"));
270 if (eventHeader.getValue("source-name") == null) {
271 eventHeader.setValue("source-name", AAIConfig.get("aai.notificationEvent.default.sourceName", "UNK"));
274 if (eventHeader.getValue("sequence-number") == null) {
275 eventHeader.setValue("sequence-number",
276 AAIConfig.get("aai.notificationEvent.default.sequenceNumber", "UNK"));
279 if (eventHeader.getValue("severity") == null) {
280 eventHeader.setValue("severity", AAIConfig.get("aai.notificationEvent.default.severity", "UNK"));
283 if (eventHeader.getValue("version") == null) {
284 eventHeader.setValue("version", AAIConfig.get("aai.notificationEvent.default.version", "UNK"));
287 if (notificationEvent.getValue("cambria-partition") == null) {
288 notificationEvent.setValue("cambria-partition",
289 AAIConfig.get("aai.notificationEvent.default.partition", AAIConstants.UEB_PUB_PARTITION_AAI));
292 notificationEvent.setValue("event-header", eventHeader.getUnderlyingObject());
293 notificationEvent.setValue("entity", obj.getUnderlyingObject());
295 String entityJson = notificationEvent.marshal(false);
296 sendToDmaapJmsQueue(entityJson);
298 } catch (JSONException e) {
299 throw new AAIException("AAI_7350", e);
300 } catch (AAIUnknownObjectException e) {
301 throw new AAIException("AAI_7350", e);
305 private void sendToDmaapJmsQueue(String entityString) throws JSONException {
307 JSONObject entityJsonObject = new JSONObject(entityString);
309 JSONObject entityJsonObjectUpdated = new JSONObject();
310 JSONObject finalJson = new JSONObject();
312 JSONObject entityHeader = entityJsonObject.getJSONObject("event-header");
313 String cambriaPartition = entityJsonObject.getString("cambria.partition");
315 entityJsonObject.remove("event-header");
316 entityJsonObject.remove("cambria.partition");
318 entityJsonObjectUpdated.put("event-header", entityHeader);
319 entityJsonObjectUpdated.put("cambria.partition", cambriaPartition);
321 String transId = entityHeader.getString("id");
322 String fromAppId = entityHeader.getString("source-name");
324 Iterator<String> iter = entityJsonObject.keys();
325 JSONObject entity = new JSONObject();
326 if (iter.hasNext()) {
327 entity = entityJsonObject.getJSONObject(iter.next());
330 entityJsonObjectUpdated.put("entity", entity);
332 finalJson.put("event-topic", "AAI-EVENT");
333 finalJson.put("transId", transId);
334 finalJson.put("fromAppId", fromAppId);
335 finalJson.put("fullId", "");
336 finalJson.put("aaiEventPayload", entityJsonObjectUpdated);
338 messageProducer.sendMessageToDefaultDestination(finalJson);
346 public static String genDate() {
347 FormatDate fd = new FormatDate("YYYYMMdd-HH:mm:ss:SSS");
348 return fd.getDateTime();
356 public static String genDate2() {
357 FormatDate fd = new FormatDate("YYYYMMddHHmmss");
358 return fd.getDateTime();