f59b454ba5adbca58677578f29f8f4eacfc45dec
[oam/oam-controller.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 java.time.Instant;
26 import org.eclipse.jdt.annotation.NonNull;
27 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorCfgService;
28 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorService;
29 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESCommonEventHeaderPOJO;
30 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESMessage;
31 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESNotificationFieldsPOJO;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class MountpointStateVESMessageFormatter {
36
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(VESCollectorService vesCollectorService) {
44         this.vesCfg = vesCollectorService.getConfig();
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(String nodeId, String connectionStatus,
57             Instant timestamp) {
58         if (LOG.isDebugEnabled()) {
59             LOG.debug("format to VES from {}, {}, {}", nodeId, connectionStatus, timestamp);
60         }
61
62         MountpointStateVESMessageFormatter.incrSequenceNo();
63
64         VESCommonEventHeaderPOJO vesCommonEventHeader = createVESCommonEventHeader(nodeId, connectionStatus, timestamp);
65         VESNotificationFieldsPOJO vesNotificationFields = createVESNotificationFields(nodeId, connectionStatus);
66
67         VESMessage vesMsg = null;
68         try {
69             vesMsg = vesCollectorService.generateVESEvent(vesCommonEventHeader, vesNotificationFields);
70             LOG.debug("VES Message is - {}", vesMsg.getMessage());
71         } catch (JsonProcessingException e) {
72             LOG.error("Exception while generating VES Event - ", e);
73         }
74
75         return vesMsg;
76
77     }
78
79     private VESNotificationFieldsPOJO createVESNotificationFields(String nodeId, String connectionStatus) {
80         VESNotificationFieldsPOJO vesNotificationFields = new VESNotificationFieldsPOJO();
81
82         vesNotificationFields.setChangeIdentifier(nodeId);
83         vesNotificationFields.setChangeType(Constants.VES_CHANGETYPE);
84         vesNotificationFields.setNewState(connectionStatus);
85
86         return vesNotificationFields;
87     }
88
89     private VESCommonEventHeaderPOJO createVESCommonEventHeader(String nodeId, String connectionStatus,
90             Instant timestamp) {
91         VESCommonEventHeaderPOJO vesCommonEventHeader = new VESCommonEventHeaderPOJO();
92
93         vesCommonEventHeader.setDomain(Constants.VES_DOMAIN);
94         vesCommonEventHeader.setEventId(nodeId + "_" + connectionStatus + "_" + getSequenceNo());
95         vesCommonEventHeader.setEventName(nodeId + "_" + connectionStatus + "_" + getSequenceNo());
96         vesCommonEventHeader.setSourceName(nodeId);
97         vesCommonEventHeader.setPriority(Constants.VES_PRIORITY);
98         vesCommonEventHeader.setReportingEntityId(this.vesCfg.getReportingEntityId());
99         vesCommonEventHeader.setReportingEntityName(this.vesCfg.getReportingEntityName());
100         vesCommonEventHeader.setSequence(getSequenceNo());
101         vesCommonEventHeader.setLastEpochMicrosec(timestamp.toEpochMilli() * 1000);
102         vesCommonEventHeader.setStartEpochMicrosec(timestamp.toEpochMilli() * 1000);
103
104         return vesCommonEventHeader;
105     }
106 }