Use DOM API for ORAN Devicemanager
[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 / dom / ORanDOMChangeNotificationListener.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2021 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.devicemanager.oran.impl.dom;
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.dataprovider.model.DataProvider;
28 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.NetconfTimeStampImpl;
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.VESNotificationFieldsPOJO;
32 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfDomAccessor;
33 import org.opendaylight.mdsal.dom.api.DOMEvent;
34 import org.opendaylight.mdsal.dom.api.DOMNotification;
35 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.EventlogBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.EventlogEntity;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SourceType;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
41 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
43 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 public class ORanDOMChangeNotificationListener implements DOMNotificationListener {
48
49     private static final Logger log = LoggerFactory.getLogger(ORanDOMChangeNotificationListener.class);
50
51     private final NetconfDomAccessor netconfDomAccessor;
52     private final DataProvider databaseService;
53     private @NonNull VESCollectorService vesCollectorService;
54     private final DOMNotificationToXPath domNotificationXPath;
55     private ORanDOMNotifToVESEventAssembly mapper = null;
56     private static int sequenceNo = 0;
57
58     public ORanDOMChangeNotificationListener(NetconfDomAccessor netconfDomAccessor,
59             @NonNull VESCollectorService vesCollectorService, DataProvider databaseService) {
60         this.netconfDomAccessor = netconfDomAccessor;
61         this.databaseService = databaseService;
62         this.vesCollectorService = vesCollectorService;
63         domNotificationXPath = new DOMNotificationToXPath();
64     }
65
66     @Override
67     public void onNotification(@NonNull DOMNotification domNotification) {
68         if (domNotification.getType()
69                 .equals(Absolute.of(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_NETCONF_CONFIG_CHANGE))) {
70             handleNetconfConfigChange(domNotification);
71         }
72     }
73
74     private void handleNetconfConfigChange(@NonNull DOMNotification domNotification) {
75         DateAndTime eventTime;
76         Instant notificationEventTime = null;
77         if (domNotification instanceof DOMEvent) {
78             notificationEventTime = ((DOMEvent) domNotification).getEventInstant();
79             eventTime = NetconfTimeStampImpl.getConverter().getTimeStamp(notificationEventTime.toString());
80         } else {
81             eventTime = NetconfTimeStampImpl.getConverter().getTimeStamp();
82         }
83
84         ContainerNode cn = domNotification.getBody();
85
86         // Process the changed-by child
87         //              ContainerNode changedByContainerNode = (ContainerNode) cn
88         //                              .childByArg(new NodeIdentifier(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_CHANGEDBY));
89         //              ChoiceNode serverOrUserIDVal = (ChoiceNode) changedByContainerNode
90         //                              .childByArg(new NodeIdentifier(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_SERVERORUSER));
91         //              @SuppressWarnings("unused")
92         //              String userIDValue = serverOrUserIDVal
93         //                              .childByArg(new NodeIdentifier(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_USERNAME)).body()
94         //                              .toString();
95         //              @SuppressWarnings("unused")
96         //              Integer sessionIDVal = Integer.valueOf(serverOrUserIDVal
97         //                              .childByArg(new NodeIdentifier(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_SESSIONID)).body()
98         //                              .toString());
99         //
100         //              // Process the datastore child
101         //              @SuppressWarnings("unused")
102         //              String datastoreValue = cn
103         //                              .childByArg(new NodeIdentifier(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_DATASTORE)).body()
104         //                              .toString();
105
106         // Process the edit child
107         UnkeyedListNode editList = (UnkeyedListNode) cn
108                 .childByArg(new NodeIdentifier(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_EDITNODE));
109         if (editList != null) {
110             for (int listCnt = 0; listCnt < editList.size(); listCnt++) {
111                 String operationValue = editList.childAt(listCnt)
112                         .childByArg(new NodeIdentifier(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_OPERATION))
113                         .body().toString();
114                 String targetValue = editList.childAt(listCnt)
115                         .childByArg(new NodeIdentifier(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_TARGET))
116                         .body().toString();
117
118                 EventlogEntity eventLogEntity1 = new EventlogBuilder()
119                         .setNodeId(netconfDomAccessor.getNodeId().getValue()).setCounter(sequenceNo++)
120                         .setTimestamp(eventTime).setObjectId(targetValue).setAttributeName("N.A")
121                         .setSourceType(SourceType.Netconf).setNewValue(String.valueOf(operationValue)).build();
122                 databaseService.writeEventLog(eventLogEntity1);
123             }
124         }
125
126         if (vesCollectorService.getConfig().isVESCollectorEnabled()) {
127             if (mapper == null) {
128                 this.mapper = new ORanDOMNotifToVESEventAssembly(netconfDomAccessor, vesCollectorService);
129             }
130             VESCommonEventHeaderPOJO header =
131                     mapper.createVESCommonEventHeader(domNotificationXPath.getTime(domNotification),
132                             ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_NETCONF_CONFIG_CHANGE.getLocalName(),
133                             sequenceNo);
134             VESNotificationFieldsPOJO body =
135                     mapper.createVESNotificationFields(domNotificationXPath.convertDomNotifToXPath(domNotification),
136                             ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_NETCONF_CONFIG_CHANGE.getLocalName());
137             log.info("domNotification in XPath format = {}",
138                     domNotificationXPath.convertDomNotifToXPath(domNotification));
139             try {
140                 vesCollectorService.publishVESMessage(vesCollectorService.generateVESEvent(header, body));
141             } catch (JsonProcessingException e) {
142                 log.warn("Exception while generating JSON object ", e);
143
144             }
145         }
146
147     }
148 }