4ab1a9a85085958bff71f7fa9b1b29055616db12
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.mountpointstateprovider.impl;
23
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import java.time.Instant;
27 import org.json.JSONObject;
28 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorCfgService;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class MountpointStateVESMessageFormatter {
33     private static final Logger LOG = LoggerFactory.getLogger(MountpointStateVESMessageFormatter.class);
34
35     private VESCollectorCfgService vesCfg;
36     static long sequenceNo = 0;
37
38     public MountpointStateVESMessageFormatter(VESCollectorCfgService vesCfg) {
39         this.vesCfg = vesCfg;
40     }
41
42     private static void incrSequenceNo() {
43         sequenceNo++;
44     }
45
46     private long getSequenceNo() {
47         return sequenceNo;
48     }
49
50     public String createVESMessage(JSONObject obj) {
51         if (LOG.isDebugEnabled()) {
52             LOG.debug("JSON Object to format to VES is - {0}", obj);
53         }
54         String vesMsg = "{}";
55         MountpointStateVESMessageFormatter.incrSequenceNo();
56
57         VESCommonEventHeaderPOJO vesCommonEventHeader = createVESCommonEventHeader(obj);
58         VESNotificationFieldsPOJO vesNotificationFields = createVESNotificationFields(obj);
59
60         VESEvent vesEvent = new VESEvent();
61         vesEvent.addEventObjects(vesCommonEventHeader);
62         vesEvent.addEventObjects(vesNotificationFields);
63
64         try {
65             ObjectMapper objMapper = new ObjectMapper();
66             vesMsg = objMapper.writeValueAsString(vesEvent);
67             LOG.debug("VES message to be published - {}", vesMsg);
68         } catch (JsonProcessingException e) {
69             LOG.warn("Exception {} while processing JSON Message - {}", e, obj);
70         }
71
72         return vesMsg;
73
74     }
75
76     private VESNotificationFieldsPOJO createVESNotificationFields(JSONObject obj) {
77         VESNotificationFieldsPOJO vesNotificationFields = new VESNotificationFieldsPOJO();
78
79         vesNotificationFields.setChangeIdentifier(obj.getString(Constants.NODEID));
80         vesNotificationFields.setChangeType(Constants.VES_CHANGETYPE);
81         vesNotificationFields.setNewState(obj.getString(Constants.NETCONFNODESTATE));
82
83         return vesNotificationFields;
84     }
85
86     private VESCommonEventHeaderPOJO createVESCommonEventHeader(JSONObject obj) {
87         VESCommonEventHeaderPOJO vesCommonEventHeader = new VESCommonEventHeaderPOJO();
88
89         vesCommonEventHeader.setDomain(Constants.VES_DOMAIN);
90         vesCommonEventHeader
91                 .setEventId(obj.getString(Constants.NODEID) + "_" + obj.getString(Constants.NETCONFNODESTATE) + "_" + getSequenceNo());
92         vesCommonEventHeader
93                 .setEventName(obj.getString(Constants.NODEID) + "_" + obj.getString(Constants.NETCONFNODESTATE) + "_" + getSequenceNo());
94         vesCommonEventHeader.setSourceName(obj.getString(Constants.NODEID));
95         vesCommonEventHeader.setPriority(Constants.VES_PRIORITY);
96         vesCommonEventHeader.setReportingEntityName(this.vesCfg.getReportingEntityName());
97         vesCommonEventHeader.setSequence(getSequenceNo());
98
99         Instant time = (Instant) obj.get(Constants.TIMESTAMP);
100         vesCommonEventHeader.setLastEpochMicrosec(time.toEpochMilli() * 100);
101         vesCommonEventHeader.setStartEpochMicrosec(time.toEpochMilli() * 100);
102
103         return vesCommonEventHeader;
104     }
105 }