2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2019 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.delta;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import com.google.gson.*;
26 import org.onap.aai.db.props.AAIProperties;
27 import org.onap.aai.dmaap.AAIDmaapEventJMSProducer;
28 import org.onap.aai.dmaap.MessageProducer;
29 import org.onap.aai.util.AAIConfig;
31 import java.text.DateFormat;
32 import java.text.SimpleDateFormat;
33 import java.util.Date;
36 public class DeltaEvents {
38 private static final Logger LOGGER = LoggerFactory.getLogger(DeltaEvents.class);
40 private static final Gson gson = new GsonBuilder()
41 .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES)
44 private String transId;
45 private String sourceName;
46 private String eventVersion = "v1";
47 private String schemaVersion;
48 private Map<String, ObjectDelta> objectDeltas;
50 private MessageProducer messageProducer;
52 public DeltaEvents(String transId, String sourceName, String schemaVersion, Map<String, ObjectDelta> objectDeltas) {
53 this(transId, sourceName, schemaVersion, objectDeltas, new AAIDmaapEventJMSProducer());
56 public DeltaEvents(String transId, String sourceName, String schemaVersion, Map<String, ObjectDelta> objectDeltas, MessageProducer messageProducer) {
57 this.transId = transId;
58 this.sourceName = sourceName;
59 this.schemaVersion = schemaVersion;
60 this.objectDeltas = objectDeltas;
61 this.messageProducer = messageProducer;
64 public boolean triggerEvents() {
65 if (objectDeltas.isEmpty()) {
69 JsonObject finalJson = new JsonObject();
70 finalJson.addProperty("event-topic", "DELTA");
71 finalJson.addProperty("transId", transId);
72 finalJson.addProperty("fromAppId", sourceName);
73 finalJson.addProperty("fullId", "");
74 finalJson.add("aaiEventPayload", buildEvent());
76 this.messageProducer.sendMessageToDefaultDestination(finalJson.toString());
80 private JsonObject buildEvent() {
81 JsonObject event = new JsonObject();
82 event.addProperty("cambria.partition", this.getPartition());
83 event.add("event-header", getHeader());
84 event.add("entities", gson.toJsonTree(objectDeltas.values()));
88 private String getPartition() {
92 private JsonObject getHeader() {
93 ObjectDelta first = objectDeltas.values().iterator().next();
94 JsonObject header = new JsonObject();
95 header.addProperty("id", this.transId);
96 header.addProperty("timestamp", this.getTimeStamp(first.getTimestamp()));
97 header.addProperty("source-name", this.sourceName);
98 header.addProperty("domain", this.getDomain());
99 header.addProperty("event-type", this.getEventType());
100 header.addProperty("event-version", this.eventVersion);
101 header.addProperty("schema-version", this.schemaVersion);
102 header.addProperty("action", first.getAction().toString());
103 header.addProperty("entity-type", this.getEntityType(first));
104 header.addProperty("entity-link", first.getUri());
105 header.addProperty("entity-uuid", this.getUUID(first));
110 private String getUUID(ObjectDelta objectDelta) {
111 return (String) objectDelta.getPropertyDeltas().get(AAIProperties.AAI_UUID).getValue();
114 private String getEntityType(ObjectDelta objectDelta) {
115 return (String) objectDelta.getPropertyDeltas().get(AAIProperties.NODE_TYPE).getValue();
118 private String getEventType() {
122 private String getDomain() {
123 return AAIConfig.get("aai.notificationEvent.default.domain", "UNK");
127 * Given Long timestamp convert to format YYYYMMdd-HH:mm:ss:SSS
128 * @param timestamp milliseconds since epoc
129 * @return long timestamp in format YYYYMMdd-HH:mm:ss:SSS
131 private String getTimeStamp(long timestamp) {
132 //SimpleDateFormat is not thread safe new instance needed
133 DateFormat df = new SimpleDateFormat("yyyyMMdd-HH:mm:ss:SSS");
134 return df.format(new Date(timestamp));