a0a075d0bc17363ed031598af276ab01c2c257c9
[oam/oam-controller.git] /
1 package org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.notification;
2
3 import com.fasterxml.jackson.core.JsonProcessingException;
4 import java.time.Instant;
5 import java.time.format.DateTimeParseException;
6 import java.util.ArrayList;
7 import java.util.Collection;
8 import java.util.Iterator;
9 import java.util.List;
10 import org.eclipse.jdt.annotation.NonNull;
11 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider;
12 import org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.config.ORanDMConfig;
13 import org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.dataprovider.ORanDOMToInternalDataModel;
14 import org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.util.ORanDMDOMUtility;
15 import org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.util.ORanDeviceManagerQNames;
16 import org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.vesmapper.ORanDOMSupervisionNotifToVESMapper;
17 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorService;
18 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESCommonEventHeaderPOJO;
19 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESMessage;
20 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESStndDefinedFieldsPOJO;
21 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfDomAccessor;
22 import org.opendaylight.mdsal.dom.api.DOMNotification;
23 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
24 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class ORanDOMSupervisionNotificationListener implements DOMNotificationListener, ORanNotificationReceivedService {
29     private static final Logger LOG = LoggerFactory.getLogger(ORanDOMSupervisionNotificationListener.class);
30
31     private @NonNull NetconfDomAccessor netconfDomAccessor;
32     private @NonNull DataProvider databaseService;
33     private @NonNull VESCollectorService vesCollectorService;
34     private List<ORanNotificationObserver> notificationObserverList;
35     private Integer counter; //Local counter is assigned to Notifications
36
37     private @NonNull ORanDOMSupervisionNotifToVESMapper mapper;
38
39     public ORanDOMSupervisionNotificationListener(@NonNull NetconfDomAccessor netconfDomAccessor,
40             @NonNull VESCollectorService vesCollectorService, @NonNull DataProvider databaseService,
41             ORanDMConfig oranSupervisionConfig) {
42         this.netconfDomAccessor = netconfDomAccessor;
43         this.databaseService = databaseService;
44         this.vesCollectorService = vesCollectorService;
45         this.mapper = new ORanDOMSupervisionNotifToVESMapper(netconfDomAccessor.getNodeId(), vesCollectorService, "o-ran-supervision");
46         notificationObserverList = new ArrayList<>();
47         this.counter = 0;
48     }
49
50     public void setComponentList(Collection<MapEntryNode> componentList) {
51         for (MapEntryNode component : ORanDOMToInternalDataModel.getRootComponents(componentList)) {
52             mapper.setMfgName(
53                     ORanDMDOMUtility.getLeafValue(component, ORanDeviceManagerQNames.IETF_HW_COMPONENT_LIST_MFG_NAME));
54             mapper.setUuid(ORanDMDOMUtility.getLeafValue(component,
55                     ORanDeviceManagerQNames.IETF_HW_COMPONENT_LIST_UUID) != null
56                             ? ORanDMDOMUtility.getLeafValue(component,
57                                     ORanDeviceManagerQNames.IETF_HW_COMPONENT_LIST_UUID)
58                             : netconfDomAccessor.getNodeId().getValue());
59             mapper.setModelName(ORanDMDOMUtility.getLeafValue(component,
60                     ORanDeviceManagerQNames.IETF_HW_COMPONENT_LIST_MODEL_NAME));
61         }
62     }
63
64     @Override
65     public void onNotification(@NonNull DOMNotification notification) {
66         LOG.trace("Notification Type = {}", notification.getType().toString());
67         notifyObservers();
68         Instant eventTimeInstant = ORanDMDOMUtility.getNotificationInstant(notification);
69         try {
70             if (vesCollectorService.getConfig().isVESCollectorEnabled()) {
71                 VESCommonEventHeaderPOJO header = mapper.mapCommonEventHeader(notification, eventTimeInstant, counter);
72                 VESStndDefinedFieldsPOJO body = mapper.mapStndDefinedFields(eventTimeInstant);
73                 VESMessage vesMsg = vesCollectorService.generateVESEvent(header, body);
74                 vesCollectorService.publishVESMessage(vesMsg);
75                 LOG.debug("VES Message is  {}", vesMsg.getMessage());
76             }
77         } catch (JsonProcessingException | DateTimeParseException e) {
78             LOG.debug("Cannot convert event into VES message {}", notification, e);
79         }
80     }
81
82     private void notifyObservers() {
83         Iterator<ORanNotificationObserver> it = notificationObserverList.iterator();
84         while (it.hasNext()) {
85             ORanNotificationObserver o = it.next();
86             new Thread() {
87                 @Override
88                 public void run() {
89                     o.observer();
90                 }
91             }.start();
92         }
93     }
94
95     @Override
96     public void registerForNotificationReceivedEvent(ORanNotificationObserver o) {
97         notificationObserverList.add(o);
98     }
99
100     @Override
101     public void deregisterNotificationReceivedEvent(ORanNotificationObserver o) {
102         notificationObserverList.remove(o);
103     }
104
105 }