10d47cd6849a9761d1dbeb402a84b5b38ee83fd3
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / util / delta / DeltaEvents.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.aai.util.delta;
22
23 import com.google.gson.*;
24
25 import java.text.DateFormat;
26 import java.text.SimpleDateFormat;
27 import java.util.Date;
28 import java.util.Map;
29
30 import org.onap.aai.db.props.AAIProperties;
31 import org.onap.aai.dmaap.AAIDmaapEventJMSProducer;
32 import org.onap.aai.dmaap.MessageProducer;
33 import org.onap.aai.util.AAIConfig;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public class DeltaEvents {
38
39     private static final Logger LOGGER = LoggerFactory.getLogger(DeltaEvents.class);
40
41     private static final Gson gson =
42             new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES).create();
43
44     private String transId;
45     private String sourceName;
46     private String eventVersion = "v1";
47     private String schemaVersion;
48     private Map<String, ObjectDelta> objectDeltas;
49
50     private MessageProducer messageProducer;
51
52     public DeltaEvents(String transId, String sourceName, String schemaVersion, Map<String, ObjectDelta> objectDeltas) {
53         this(transId, sourceName, schemaVersion, objectDeltas, new AAIDmaapEventJMSProducer());
54     }
55
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;
63     }
64
65     public boolean triggerEvents() {
66         if (objectDeltas.isEmpty()) {
67             return false;
68         }
69
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());
76
77         this.messageProducer.sendMessageToDefaultDestination(finalJson.toString());
78         return true;
79     }
80
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()));
86         return event;
87     }
88
89     private String getPartition() {
90         return "DELTA";
91     }
92
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));
107
108         return header;
109     }
110
111     private String getUUID(ObjectDelta objectDelta) {
112         return (String) objectDelta.getPropertyDeltas().get(AAIProperties.AAI_UUID).getValue();
113     }
114
115     private String getEntityType(ObjectDelta objectDelta) {
116         return (String) objectDelta.getPropertyDeltas().get(AAIProperties.NODE_TYPE).getValue();
117     }
118
119     private String getEventType() {
120         return "DELTA";
121     }
122
123     private String getDomain() {
124         return AAIConfig.get("aai.notificationEvent.default.domain", "UNK");
125     }
126
127     /**
128      * Given Long timestamp convert to format YYYYMMdd-HH:mm:ss:SSS
129      * 
130      * @param timestamp milliseconds since epoc
131      * @return long timestamp in format YYYYMMdd-HH:mm:ss:SSS
132      */
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));
137     }
138 }