Merge "Fix all blocker sonar issues and some checkstyle"
[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 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;
30
31 import java.text.DateFormat;
32 import java.text.SimpleDateFormat;
33 import java.util.Date;
34 import java.util.Map;
35
36 public class DeltaEvents {
37
38     private static final Logger LOGGER = LoggerFactory.getLogger(DeltaEvents.class);
39
40     private static final Gson gson = new GsonBuilder()
41         .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES)
42         .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, MessageProducer messageProducer) {
57         this.transId = transId;
58         this.sourceName = sourceName;
59         this.schemaVersion = schemaVersion;
60         this.objectDeltas = objectDeltas;
61         this.messageProducer = messageProducer;
62     }
63
64     public boolean triggerEvents() {
65         if (objectDeltas.isEmpty()) {
66             return false;
67         }
68
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());
75
76         this.messageProducer.sendMessageToDefaultDestination(finalJson.toString());
77         return true;
78     }
79
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()));
85         return event;
86     }
87
88     private String getPartition() {
89         return "DELTA";
90     }
91
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));
106
107         return header;
108     }
109
110     private String getUUID(ObjectDelta objectDelta) {
111         return (String) objectDelta.getPropertyDeltas().get(AAIProperties.AAI_UUID).getValue();
112     }
113
114     private String getEntityType(ObjectDelta objectDelta) {
115         return (String) objectDelta.getPropertyDeltas().get(AAIProperties.NODE_TYPE).getValue();
116     }
117
118     private String getEventType() {
119         return "DELTA";
120     }
121
122     private String getDomain() {
123         return AAIConfig.get("aai.notificationEvent.default.domain", "UNK");
124     }
125
126     /**
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
130      */
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));
135     }
136 }