aboutsummaryrefslogtreecommitdiffstats
path: root/aai-core/src/main/java/org/onap/aai/util/delta/DeltaEvents.java
blob: 10d47cd6849a9761d1dbeb402a84b5b38ee83fd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.aai.util.delta;

import com.google.gson.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.onap.aai.db.props.AAIProperties;
import org.onap.aai.dmaap.AAIDmaapEventJMSProducer;
import org.onap.aai.dmaap.MessageProducer;
import org.onap.aai.util.AAIConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DeltaEvents {

    private static final Logger LOGGER = LoggerFactory.getLogger(DeltaEvents.class);

    private static final Gson gson =
            new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES).create();

    private String transId;
    private String sourceName;
    private String eventVersion = "v1";
    private String schemaVersion;
    private Map<String, ObjectDelta> objectDeltas;

    private MessageProducer messageProducer;

    public DeltaEvents(String transId, String sourceName, String schemaVersion, Map<String, ObjectDelta> objectDeltas) {
        this(transId, sourceName, schemaVersion, objectDeltas, new AAIDmaapEventJMSProducer());
    }

    public DeltaEvents(String transId, String sourceName, String schemaVersion, Map<String, ObjectDelta> objectDeltas,
            MessageProducer messageProducer) {
        this.transId = transId;
        this.sourceName = sourceName;
        this.schemaVersion = schemaVersion;
        this.objectDeltas = objectDeltas;
        this.messageProducer = messageProducer;
    }

    public boolean triggerEvents() {
        if (objectDeltas.isEmpty()) {
            return false;
        }

        JsonObject finalJson = new JsonObject();
        finalJson.addProperty("event-topic", "DELTA");
        finalJson.addProperty("transId", transId);
        finalJson.addProperty("fromAppId", sourceName);
        finalJson.addProperty("fullId", "");
        finalJson.add("aaiEventPayload", buildEvent());

        this.messageProducer.sendMessageToDefaultDestination(finalJson.toString());
        return true;
    }

    private JsonObject buildEvent() {
        JsonObject event = new JsonObject();
        event.addProperty("cambria.partition", this.getPartition());
        event.add("event-header", getHeader());
        event.add("entities", gson.toJsonTree(objectDeltas.values()));
        return event;
    }

    private String getPartition() {
        return "DELTA";
    }

    private JsonObject getHeader() {
        ObjectDelta first = objectDeltas.values().iterator().next();
        JsonObject header = new JsonObject();
        header.addProperty("id", this.transId);
        header.addProperty("timestamp", this.getTimeStamp(first.getTimestamp()));
        header.addProperty("source-name", this.sourceName);
        header.addProperty("domain", this.getDomain());
        header.addProperty("event-type", this.getEventType());
        header.addProperty("event-version", this.eventVersion);
        header.addProperty("schema-version", this.schemaVersion);
        header.addProperty("action", first.getAction().toString());
        header.addProperty("entity-type", this.getEntityType(first));
        header.addProperty("entity-link", first.getUri());
        header.addProperty("entity-uuid", this.getUUID(first));

        return header;
    }

    private String getUUID(ObjectDelta objectDelta) {
        return (String) objectDelta.getPropertyDeltas().get(AAIProperties.AAI_UUID).getValue();
    }

    private String getEntityType(ObjectDelta objectDelta) {
        return (String) objectDelta.getPropertyDeltas().get(AAIProperties.NODE_TYPE).getValue();
    }

    private String getEventType() {
        return "DELTA";
    }

    private String getDomain() {
        return AAIConfig.get("aai.notificationEvent.default.domain", "UNK");
    }

    /**
     * Given Long timestamp convert to format YYYYMMdd-HH:mm:ss:SSS
     * 
     * @param timestamp milliseconds since epoc
     * @return long timestamp in format YYYYMMdd-HH:mm:ss:SSS
     */
    private String getTimeStamp(long timestamp) {
        // SimpleDateFormat is not thread safe new instance needed
        DateFormat df = new SimpleDateFormat("yyyyMMdd-HH:mm:ss:SSS");
        return df.format(new Date(timestamp));
    }
}