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 com.google.gson.*;
25 import java.text.DateFormat;
26 import java.text.SimpleDateFormat;
27 import java.util.Date;
30 import org.onap.aai.db.props.AAIProperties;
31 import org.onap.aai.kafka.AAIKafkaEventJMSProducer;
32 import org.onap.aai.kafka.MessageProducer;
33 import org.onap.aai.util.AAIConfig;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public class DeltaEvents {
39 private static final Logger LOGGER = LoggerFactory.getLogger(DeltaEvents.class);
41 private static final Gson gson =
42 new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES).create();
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 AAIKafkaEventJMSProducer());
56 public DeltaEvents(String transId, String sourceName, String schemaVersion, Map<String, ObjectDelta> objectDeltas,
57 MessageProducer messageProducer) {
58 this.transId = transId;
59 this.sourceName = sourceName;
60 this.schemaVersion = schemaVersion;
61 this.objectDeltas = objectDeltas;
62 this.messageProducer = messageProducer;
65 public boolean triggerEvents() {
66 if (objectDeltas.isEmpty()) {
70 JsonObject finalJson = new JsonObject();
71 finalJson.addProperty("event-topic", "DELTA");
72 finalJson.addProperty("transId", transId);
73 finalJson.addProperty("fromAppId", sourceName);
74 finalJson.addProperty("fullId", "");
75 finalJson.add("aaiEventPayload", buildEvent());
77 this.messageProducer.sendMessageToDefaultDestination(finalJson.toString());
81 private JsonObject buildEvent() {
82 JsonObject event = new JsonObject();
83 event.addProperty("cambria.partition", this.getPartition());
84 event.add("event-header", getHeader());
85 event.add("entities", gson.toJsonTree(objectDeltas.values()));
89 private String getPartition() {
93 private JsonObject getHeader() {
94 ObjectDelta first = objectDeltas.values().iterator().next();
95 JsonObject header = new JsonObject();
96 header.addProperty("id", this.transId);
97 header.addProperty("timestamp", this.getTimeStamp(first.getTimestamp()));
98 header.addProperty("source-name", this.sourceName);
99 header.addProperty("domain", this.getDomain());
100 header.addProperty("event-type", this.getEventType());
101 header.addProperty("event-version", this.eventVersion);
102 header.addProperty("schema-version", this.schemaVersion);
103 header.addProperty("action", first.getAction().toString());
104 header.addProperty("entity-type", this.getEntityType(first));
105 header.addProperty("entity-link", first.getUri());
106 header.addProperty("entity-uuid", this.getUUID(first));
111 private String getUUID(ObjectDelta objectDelta) {
112 return (String) objectDelta.getPropertyDeltas().get(AAIProperties.AAI_UUID).getValue();
115 private String getEntityType(ObjectDelta objectDelta) {
116 return (String) objectDelta.getPropertyDeltas().get(AAIProperties.NODE_TYPE).getValue();
119 private String getEventType() {
123 private String getDomain() {
124 return AAIConfig.get("aai.notificationEvent.default.domain", "UNK");
128 * Given Long timestamp convert to format YYYYMMdd-HH:mm:ss:SSS
130 * @param timestamp milliseconds since epoc
131 * @return long timestamp in format YYYYMMdd-HH:mm:ss:SSS
133 private String getTimeStamp(long timestamp) {
134 // SimpleDateFormat is not thread safe new instance needed
135 DateFormat df = new SimpleDateFormat("yyyyMMdd-HH:mm:ss:SSS");
136 return df.format(new Date(timestamp));