Add bounds to sphinx requirement
[ccsdk/features.git] / sdnr / wt / devicemanager-o-ran-sc / o-ran / ru-fh / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / oran / impl / binding / ORanFaultNotificationListener.java
1 /*
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.impl.binding;
19
20 import com.fasterxml.jackson.core.JsonProcessingException;
21 import java.time.Instant;
22 import java.time.format.DateTimeParseException;
23 import java.util.Collection;
24 import java.util.Date;
25 import java.util.Objects;
26 import org.eclipse.jdt.annotation.NonNull;
27 import org.json.JSONException;
28 import org.json.JSONObject;
29 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider;
30 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.NetconfTimeStamp;
31 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.NetconfTimeStampImpl;
32 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.FaultService;
33 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorService;
34 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESCommonEventHeaderPOJO;
35 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESFaultFieldsPOJO;
36 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESMessage;
37 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfBindingAccessor;
38 import org.onap.ccsdk.features.sdnr.wt.websocketmanager.model.WebsocketManagerService;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.hardware.rev180313.hardware.Component;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
41 import org.opendaylight.yang.gen.v1.urn.o.ran.fm._1._0.rev190204.AlarmNotif;
42 import org.opendaylight.yang.gen.v1.urn.o.ran.fm._1._0.rev190204.ORanFmListener;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.EventlogBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SourceType;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 public class ORanFaultNotificationListener implements ORanFmListener {
49
50     private static final Logger LOG = LoggerFactory.getLogger(ORanFaultNotificationListener.class);
51
52     private final @NonNull NetconfBindingAccessor netconfAccessor;
53     private final @NonNull VESCollectorService vesCollectorService;
54     private final @NonNull ORanFaultToVESFaultMapper mapper;
55     private final @NonNull FaultService faultService;
56     private final @NonNull WebsocketManagerService websocketManagerService;
57     private final @NonNull DataProvider databaseService;
58     private static final NetconfTimeStamp NETCONFTIME_CONVERTER = NetconfTimeStampImpl.getConverter();
59
60     private Integer counter; //Local counter is assigned to Notifications
61
62     public ORanFaultNotificationListener(@NonNull NetconfBindingAccessor netconfAccessor,
63             @NonNull VESCollectorService vesCollectorService, @NonNull FaultService faultService,
64             @NonNull WebsocketManagerService websocketManagerService, @NonNull DataProvider databaseService) {
65         this.netconfAccessor = Objects.requireNonNull(netconfAccessor);
66         this.vesCollectorService = Objects.requireNonNull(vesCollectorService);
67         this.faultService = Objects.requireNonNull(faultService);
68         this.websocketManagerService = Objects.requireNonNull(websocketManagerService);
69         this.databaseService = Objects.requireNonNull(databaseService);
70
71         this.mapper = new ORanFaultToVESFaultMapper(netconfAccessor.getNodeId(), vesCollectorService,
72                 AlarmNotif.class.getSimpleName());
73         this.counter = 0;
74     }
75
76     /**
77      * Gets the mfg name, mode-name and Uuid of the root component (Ex: Chassis.)
78      * In cases where there are multiple root components i.e., components with no parent,
79      * the Uuid of the last occurred component from the componentList will be considered.
80      * Till now we haven't seen Uuid set for root components, so not an issue for now.
81      * @param componentList
82      */
83     public void setComponentList(Collection<Component> componentList) {
84         for (Component component : ORanToInternalDataModel.getRootComponents(componentList)) {
85             mapper.setMfgName(component.getMfgName());
86             mapper.setUuid(component.getUuid()!=null?component.getUuid().getValue():netconfAccessor.getNodeId().getValue());
87             mapper.setModelName(component.getModelName());
88         }
89     }
90
91     @Override
92     public void onAlarmNotif(AlarmNotif notification) {
93
94         LOG.debug("onAlarmNotif {}", notification.getClass().getSimpleName());
95         counter++;
96
97         // Send devicemanager specific notification for database and ODLUX
98         Instant eventTimeInstant = ORanToInternalDataModel.getInstantTime(notification.getEventTime());
99         faultService.faultNotification(
100                 ORanToInternalDataModel.getFaultLog(notification, netconfAccessor.getNodeId(), counter));
101         // Send model specific notification to WebSocketManager
102         websocketManagerService.sendNotification(notification, netconfAccessor.getNodeId(), AlarmNotif.QNAME);
103
104         try {
105             if (vesCollectorService.getConfig().isVESCollectorEnabled()) {
106                 VESCommonEventHeaderPOJO header = mapper.mapCommonEventHeader(notification, eventTimeInstant, counter);
107                 VESFaultFieldsPOJO body = mapper.mapFaultFields(notification);
108                 VESMessage vesMsg = vesCollectorService.generateVESEvent(header, body);
109                 vesCollectorService.publishVESMessage(vesMsg);
110                 LOG.info("VES Message is  {}",vesMsg.getMessage());
111                 writeToEventLog(vesMsg.getMessage(), eventTimeInstant, AlarmNotif.QNAME.toString(), counter);
112             }
113         } catch (JsonProcessingException | DateTimeParseException e) {
114             LOG.debug("Can not convert event into VES message {}", notification, e);
115         }
116     }
117
118     private void writeToEventLog(String data, Instant instant, String notificationName,
119             int sequenceNo) {
120         EventlogBuilder eventlogBuilder = new EventlogBuilder();
121
122         eventlogBuilder.setObjectId("Device");
123         eventlogBuilder.setCounter(sequenceNo);
124         eventlogBuilder.setAttributeName(notificationName);
125         eventlogBuilder.setNodeId(netconfAccessor.getNodeId().getValue());
126         String eventLogMsgLvl = vesCollectorService.getConfig().getEventLogMsgDetail();
127         if (eventLogMsgLvl.equalsIgnoreCase("SHORT")) {
128             data = getShortEventLogMessage(data);
129         } else if (eventLogMsgLvl.equalsIgnoreCase("MEDIUM")) {
130             data = getMediumEventLogMessage(data);
131         } else if (eventLogMsgLvl.equalsIgnoreCase("LONG")) {
132             // do nothing, data already contains long message
133         } else { // Unknown value, default to "SHORT"
134             data = getShortEventLogMessage(data);
135         }
136         eventlogBuilder.setNewValue(data);
137         eventlogBuilder.setSourceType(SourceType.Netconf);
138
139         Date eventDate = Date.from(instant);
140         eventlogBuilder.setTimestamp(new DateAndTime(NETCONFTIME_CONVERTER.getTimeStamp(eventDate)));
141
142         databaseService.writeEventLog(eventlogBuilder.build());
143     }
144
145     private String getShortEventLogMessage(String data) {
146         try {
147             JSONObject jsonObj = new JSONObject(data);
148             String domain = jsonObj.getJSONObject("event").getJSONObject("commonEventHeader").getString("domain");
149             String eventId = jsonObj.getJSONObject("event").getJSONObject("commonEventHeader").getString("eventId");
150             return "domain:" + domain + " eventId:" + eventId;
151         } catch (JSONException e) {
152             LOG.debug("{}", e);
153             return "Invalid message received";
154         }
155     }
156
157     private String getMediumEventLogMessage(String data) {
158         try {
159             JSONObject jsonObj = new JSONObject(data);
160             return jsonObj.getJSONObject("event").getJSONObject("commonEventHeader").toString();
161         } catch (JSONException e) {
162             LOG.debug("{}", e);
163             return "Invalid message received";
164         }
165     }
166 }