918438e55d77881398c21e2609621ca429ebd149
[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     private static final String VES_DOMAIN = "notification";
35     private static final String VES_PRIORITY = "Normal";
36     private static final String VES_CHANGETYPE = "ConnectionState";
37
38     private VESCollectorCfgService vesCfg;
39     static long sequenceNo = 0;
40
41     public MountpointStateVESMessageFormatter(VESCollectorCfgService vesCfg) {
42         this.vesCfg = vesCfg;
43     }
44
45     public String createVESMessage(JSONObject obj) {
46         LOG.debug("JSON Object to format to VES is - {}", obj.toString());
47         String vesMsg = "{}";
48         sequenceNo++;
49
50         VESCommonEventHeaderPOJO vesCommonEventHeader = createVESCommonEventHeader(obj);
51         VESNotificationFieldsPOJO vesNotificationFields = createVESNotificationFields(obj);
52
53         VESEvent vesEvent = new VESEvent();
54         vesEvent.addEventObjects(vesCommonEventHeader);
55         vesEvent.addEventObjects(vesNotificationFields);
56
57         try {
58             ObjectMapper objMapper = new ObjectMapper();
59             vesMsg = objMapper.writeValueAsString(vesEvent);
60             LOG.debug("VES message to be published - {}", vesMsg);
61         } catch (JsonProcessingException e) {
62             e.printStackTrace();
63         }
64
65         return vesMsg;
66
67     }
68
69     private VESNotificationFieldsPOJO createVESNotificationFields(JSONObject obj) {
70         VESNotificationFieldsPOJO vesNotificationFields = new VESNotificationFieldsPOJO();
71
72         vesNotificationFields.setChangeIdentifier(obj.getString("NodeId"));
73         vesNotificationFields.setChangeType(VES_CHANGETYPE);
74         vesNotificationFields.setNewState(obj.getString("NetConfNodeState"));
75
76         return vesNotificationFields;
77     }
78
79     private VESCommonEventHeaderPOJO createVESCommonEventHeader(JSONObject obj) {
80         VESCommonEventHeaderPOJO vesCommonEventHeader = new VESCommonEventHeaderPOJO();
81
82         vesCommonEventHeader.setDomain(VES_DOMAIN);
83         vesCommonEventHeader
84                 .setEventId(obj.getString("NodeId") + "_" + obj.getString("NetConfNodeState") + "_" + sequenceNo);
85         vesCommonEventHeader
86                 .setEventName(obj.getString("NodeId") + "_" + obj.getString("NetConfNodeState") + "_" + sequenceNo);
87         vesCommonEventHeader.setSourceName(obj.getString("NodeId"));
88         vesCommonEventHeader.setPriority(VES_PRIORITY);
89         vesCommonEventHeader.setReportingEntityName(this.vesCfg.getReportingEntityName());
90         vesCommonEventHeader.setSequence(sequenceNo);
91
92         Instant time = (Instant) obj.get("TimeStamp");
93         vesCommonEventHeader.setLastEpochMicrosec(time.toEpochMilli() * 100);
94         vesCommonEventHeader.setStartEpochMicrosec(time.toEpochMilli() * 100);
95
96         return vesCommonEventHeader;
97     }
98 }