Merge "Change log levels to reduce the amount of logs generated"
[ccsdk/features.git] / sdnr / wt / mountpoint-state-provider / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / mountpointstateprovider / impl / MountpointStateVESMessageFormatter.java
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 java.time.Instant;
26 import org.eclipse.jdt.annotation.NonNull;
27 import org.json.JSONObject;
28 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorCfgService;
29 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorService;
30 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESCommonEventHeaderPOJO;
31 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESMessage;
32 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESNotificationFieldsPOJO;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class MountpointStateVESMessageFormatter {
37     private static final Logger LOG = LoggerFactory.getLogger(MountpointStateVESMessageFormatter.class);
38
39     private final @NonNull VESCollectorCfgService vesCfg;
40     private final @NonNull VESCollectorService vesCollectorService;
41     static long sequenceNo = 0;
42
43     public MountpointStateVESMessageFormatter(VESCollectorCfgService vesCfg, VESCollectorService vesCollectorService) {
44         this.vesCfg = vesCfg;
45         this.vesCollectorService = vesCollectorService;
46     }
47
48     private static void incrSequenceNo() {
49         sequenceNo++;
50     }
51
52     private long getSequenceNo() {
53         return sequenceNo;
54     }
55
56     public VESMessage createVESMessage(JSONObject obj) {
57         if (LOG.isDebugEnabled()) {
58             LOG.debug("JSON Object to format to VES is - {0}", obj);
59         }
60
61         MountpointStateVESMessageFormatter.incrSequenceNo();
62
63         VESCommonEventHeaderPOJO vesCommonEventHeader = createVESCommonEventHeader(obj);
64         VESNotificationFieldsPOJO vesNotificationFields = createVESNotificationFields(obj);
65
66         VESMessage vesMsg = null;
67         try {
68             vesMsg = vesCollectorService.generateVESEvent(vesCommonEventHeader, vesNotificationFields);
69             LOG.debug("VES Message is - {}", vesMsg.getMessage());
70         } catch (JsonProcessingException e) {
71             LOG.error("Exception while generating VES Event - ", e);
72         }
73
74         return vesMsg;
75
76     }
77
78     private VESNotificationFieldsPOJO createVESNotificationFields(JSONObject obj) {
79         VESNotificationFieldsPOJO vesNotificationFields = new VESNotificationFieldsPOJO();
80
81         vesNotificationFields.setChangeIdentifier(obj.getString(Constants.NODEID));
82         vesNotificationFields.setChangeType(Constants.VES_CHANGETYPE);
83         vesNotificationFields.setNewState(obj.getString(Constants.NETCONFNODESTATE));
84
85         return vesNotificationFields;
86     }
87
88     private VESCommonEventHeaderPOJO createVESCommonEventHeader(JSONObject obj) {
89         VESCommonEventHeaderPOJO vesCommonEventHeader = new VESCommonEventHeaderPOJO();
90
91         vesCommonEventHeader.setDomain(Constants.VES_DOMAIN);
92         vesCommonEventHeader
93                 .setEventId(obj.getString(Constants.NODEID) + "_" + obj.getString(Constants.NETCONFNODESTATE) + "_" + getSequenceNo());
94         vesCommonEventHeader
95                 .setEventName(obj.getString(Constants.NODEID) + "_" + obj.getString(Constants.NETCONFNODESTATE) + "_" + getSequenceNo());
96         vesCommonEventHeader.setSourceName(obj.getString(Constants.NODEID));
97         vesCommonEventHeader.setPriority(Constants.VES_PRIORITY);
98         vesCommonEventHeader.setReportingEntityName(this.vesCfg.getReportingEntityName());
99         vesCommonEventHeader.setSequence(getSequenceNo());
100
101         Instant time = (Instant) obj.get(Constants.TIMESTAMP);
102         vesCommonEventHeader.setLastEpochMicrosec(time.toEpochMilli() * 100);
103         vesCommonEventHeader.setStartEpochMicrosec(time.toEpochMilli() * 100);
104
105         return vesCommonEventHeader;
106     }
107 }