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 java.io.StringWriter;
23 import java.util.Iterator;
24 import java.util.UUID;
26 import javax.xml.bind.Marshaller;
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import org.eclipse.persistence.dynamic.DynamicEntity;
31 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
32 import org.json.JSONException;
33 import org.json.JSONObject;
34 import org.onap.aai.config.SpringContextAware;
35 import org.onap.aai.dmaap.AAIDmaapEventJMSProducer;
36 import org.onap.aai.dmaap.JMSProducer;
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.beans.factory.NoSuchBeanDefinitionException;
44 import org.springframework.context.ApplicationContext;
45 import org.springframework.core.env.Environment;
47 public class StoreNotificationEvent {
49 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.context = SpringContextAware.getApplicationContext();
64 // If the context is null then this is being invoked from
65 // non spring context so creating the jms producer each time
66 // Otherwise, get the jms producer from the spring context so
67 // no need to create a instance of this each time
68 // Also check if the environment has the dmaap profile
69 // TODO - Add the constants for profile so do this when adding the https two-way ssl and one way with basic auth
70 if(this.context == null){
71 this.messageProducer = new AAIDmaapEventJMSProducer();
73 env = context.getEnvironment();
74 if(env.acceptsProfiles("dmaap")){
76 this.messageProducer = (JMSProducer)context.getBean("jmsProducer");
77 } catch(NoSuchBeanDefinitionException ex){
78 logger.error("Currently using the dmaap profile but still not able to find bean so check DmaapConfig", ex);
81 this.messageProducer = new AAIDmaapEventJMSProducer();
84 this.transactionId = transactionId;
85 this.sourceOfTruth = sourceOfTruth;
88 public StoreNotificationEvent(AAIDmaapEventJMSProducer producer, String transactionId, String sourceOfTruth) {
89 this.messageProducer = producer;
90 this.transactionId = transactionId;
91 this.sourceOfTruth = sourceOfTruth;
101 * @throws AAIException
104 public String storeEvent(NotificationEvent.EventHeader eh, Object obj) throws AAIException {
107 throw new AAIException("AAI_7350");
110 org.onap.aai.domain.notificationEvent.ObjectFactory factory = new org.onap.aai.domain.notificationEvent.ObjectFactory();
112 org.onap.aai.domain.notificationEvent.NotificationEvent ne = factory.createNotificationEvent();
114 if (eh.getId() == null) {
115 eh.setId(genDate2() + "-" + UUID.randomUUID().toString());
117 if (eh.getTimestamp() == null) {
118 eh.setTimestamp(genDate());
121 // there's no default, but i think we want to put this in hbase?
123 if (eh.getEntityLink() == null) {
124 eh.setEntityLink("UNK");
127 if (eh.getAction() == null) {
131 if (eh.getEventType() == null) {
132 eh.setEventType(AAIConfig.get("aai.notificationEvent.default.eventType", "UNK"));
135 if (eh.getDomain() == null) {
136 eh.setDomain(AAIConfig.get("aai.notificationEvent.default.domain", "UNK"));
139 if (eh.getSourceName() == null) {
140 eh.setSourceName(AAIConfig.get("aai.notificationEvent.default.sourceName", "UNK"));
143 if (eh.getSequenceNumber() == null) {
144 eh.setSequenceNumber(AAIConfig.get("aai.notificationEvent.default.sequenceNumber", "UNK"));
147 if (eh.getSeverity() == null) {
148 eh.setSeverity(AAIConfig.get("aai.notificationEvent.default.severity", "UNK"));
151 if (eh.getVersion() == null) {
152 eh.setVersion(AAIConfig.get("aai.notificationEvent.default.version", "UNK"));
155 ne.setCambriaPartition(AAIConstants.UEB_PUB_PARTITION_AAI);
156 ne.setEventHeader(eh);
160 PojoUtils pu = new PojoUtils();
161 String entityJson = pu.getJsonFromObject(ne);
162 sendToDmaapJmsQueue(entityJson);
164 } catch (Exception e) {
165 throw new AAIException("AAI_7350", e);
170 * Store dynamic event.
172 * @param notificationJaxbContext
173 * the notification jaxb context
174 * @param notificationVersion
175 * the notification version
180 * @throws AAIException
183 public void storeDynamicEvent(DynamicJAXBContext notificationJaxbContext, String notificationVersion, DynamicEntity eventHeader, DynamicEntity obj) throws AAIException {
186 throw new AAIException("AAI_7350");
189 DynamicEntity notificationEvent = notificationJaxbContext.getDynamicType("inventory.aai.onap.org." + notificationVersion + ".NotificationEvent").newDynamicEntity();
191 if (eventHeader.get("id") == null) {
192 eventHeader.set("id", genDate2() + "-" + UUID.randomUUID().toString());
195 if (eventHeader.get("timestamp") == null) {
196 eventHeader.set("timestamp", genDate());
199 if (eventHeader.get("entityLink") == null) {
200 eventHeader.set("entityLink", "UNK");
203 if (eventHeader.get("action") == null) {
204 eventHeader.set("action", "UNK");
207 if (eventHeader.get("eventType") == null) {
208 eventHeader.set("eventType", AAIConfig.get("aai.notificationEvent.default.eventType", "UNK"));
211 if (eventHeader.get("domain") == null) {
212 eventHeader.set("domain", AAIConfig.get("aai.notificationEvent.default.domain", "UNK"));
215 if (eventHeader.get("sourceName") == null) {
216 eventHeader.set("sourceName", AAIConfig.get("aai.notificationEvent.default.sourceName", "UNK"));
219 if (eventHeader.get("sequenceNumber") == null) {
220 eventHeader.set("sequenceNumber", AAIConfig.get("aai.notificationEvent.default.sequenceNumber", "UNK"));
223 if (eventHeader.get("severity") == null) {
224 eventHeader.set("severity", AAIConfig.get("aai.notificationEvent.default.severity", "UNK"));
227 if (eventHeader.get("version") == null) {
228 eventHeader.set("version", AAIConfig.get("aai.notificationEvent.default.version", "UNK"));
231 if (notificationEvent.get("cambriaPartition") == null) {
232 notificationEvent.set("cambriaPartition", AAIConstants.UEB_PUB_PARTITION_AAI);
235 notificationEvent.set("eventHeader", eventHeader);
236 notificationEvent.set("entity", obj);
239 StringWriter result = new StringWriter();
241 Marshaller marshaller = notificationJaxbContext.createMarshaller();
242 marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.MEDIA_TYPE, "application/json");
243 marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.JSON_INCLUDE_ROOT, false);
244 marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, false);
245 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
246 marshaller.marshal(notificationEvent, result);
248 this.sendToDmaapJmsQueue(result.toString());
250 } catch (Exception e) {
251 throw new AAIException("AAI_7350", e);
255 public String storeEvent(Loader loader, Introspector eventHeader, Introspector obj) throws AAIException {
257 throw new AAIException("AAI_7350");
261 final Introspector notificationEvent = loader.introspectorFromName("notification-event");
263 if (eventHeader.getValue("id") == null) {
264 eventHeader.setValue("id", genDate2() + "-" + UUID.randomUUID().toString());
267 if (eventHeader.getValue("timestamp") == null) {
268 eventHeader.setValue("timestamp", genDate());
271 if (eventHeader.getValue("entity-link") == null) {
272 eventHeader.setValue("entity-link", "UNK");
275 if (eventHeader.getValue("action") == null) {
276 eventHeader.setValue("action", "UNK");
279 if (eventHeader.getValue("event-type") == null) {
280 eventHeader.setValue("event-type", AAIConfig.get("aai.notificationEvent.default.eventType", "UNK"));
283 if (eventHeader.getValue("domain") == null) {
284 eventHeader.setValue("domain", AAIConfig.get("aai.notificationEvent.default.domain", "UNK"));
287 if (eventHeader.getValue("source-name") == null) {
288 eventHeader.setValue("source-name", AAIConfig.get("aai.notificationEvent.default.sourceName", "UNK"));
291 if (eventHeader.getValue("sequence-number") == null) {
292 eventHeader.setValue("sequence-number", AAIConfig.get("aai.notificationEvent.default.sequenceNumber", "UNK"));
295 if (eventHeader.getValue("severity") == null) {
296 eventHeader.setValue("severity", AAIConfig.get("aai.notificationEvent.default.severity", "UNK"));
299 if (eventHeader.getValue("version") == null) {
300 eventHeader.setValue("version", AAIConfig.get("aai.notificationEvent.default.version", "UNK"));
303 if (notificationEvent.getValue("cambria-partition") == null) {
304 notificationEvent.setValue("cambria-partition", AAIConstants.UEB_PUB_PARTITION_AAI);
307 notificationEvent.setValue("event-header", eventHeader.getUnderlyingObject());
308 notificationEvent.setValue("entity", obj.getUnderlyingObject());
310 String entityJson = notificationEvent.marshal(false);
311 sendToDmaapJmsQueue(entityJson);
313 } catch (JSONException e) {
314 throw new AAIException("AAI_7350", e);
315 } catch (AAIUnknownObjectException e) {
316 throw new AAIException("AAI_7350", e);
320 private void sendToDmaapJmsQueue(String entityString) throws JSONException {
322 JSONObject entityJsonObject = new JSONObject(entityString);
324 JSONObject entityJsonObjectUpdated = new JSONObject();
325 JSONObject finalJson = new JSONObject();
327 JSONObject entityHeader = entityJsonObject.getJSONObject("event-header");
328 String cambriaPartition = entityJsonObject.getString("cambria.partition");
330 entityJsonObject.remove("event-header");
331 entityJsonObject.remove("cambria.partition");
333 entityJsonObjectUpdated.put("event-header", entityHeader);
334 entityJsonObjectUpdated.put("cambria.partition", cambriaPartition);
336 String transId = entityHeader.getString("id");
337 String fromAppId = entityHeader.getString("source-name");
339 Iterator<String> iter = entityJsonObject.keys();
340 JSONObject entity = new JSONObject();
341 if (iter.hasNext()) {
342 entity = entityJsonObject.getJSONObject(iter.next());
345 entityJsonObjectUpdated.put("entity", entity);
347 finalJson.put("event-topic", "AAI-EVENT");
348 finalJson.put("transId", transId);
349 finalJson.put("fromAppId", fromAppId);
350 finalJson.put("fullId", "");
351 finalJson.put("aaiEventPayload", entityJsonObjectUpdated);
353 messageProducer.sendMessageToDefaultDestination(finalJson);
361 public static String genDate() {
362 FormatDate fd = new FormatDate("YYYYMMdd-HH:mm:ss:SSS");
363 return fd.getDateTime();
371 public static String genDate2() {
372 FormatDate fd = new FormatDate("YYYYMMddHHmmss");
373 return fd.getDateTime();