Align initial alarms and notifications parsing
[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 / ORanDOMNetworkElement.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.util.Collection;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Objects;
30 import java.util.Optional;
31 import org.eclipse.jdt.annotation.NonNull;
32 import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation;
33 import org.onap.ccsdk.features.sdnr.wt.common.configuration.filechange.IConfigChangedListener;
34 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider;
35 import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.service.NetworkElement;
36 import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.service.NetworkElementService;
37 import org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.config.ORanDMConfig;
38 import org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.dataprovider.ORanDOMToInternalDataModel;
39 import org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.notification.ORanDOMChangeNotificationListener;
40 import org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.notification.ORanDOMFaultNotificationListener;
41 import org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.notification.ORanDOMSupervisionNotificationListener;
42 import org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.notification.ORanNotificationObserverImpl;
43 import org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.rpc.ORanSupervisionRPCImpl;
44 import org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.util.ORanDMDOMUtility;
45 import org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.util.ORanDeviceManagerQNames;
46 import org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.vesmapper.ORanRegistrationToVESpnfRegistrationMapper;
47 import org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.yangspecs.ORANFM;
48 import org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.yangspecs.OnapSystem;
49 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.DeviceManagerServiceProvider;
50 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.FaultService;
51 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.NotificationService;
52 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorService;
53 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESCommonEventHeaderPOJO;
54 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESPNFRegistrationFieldsPOJO;
55 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.Capabilities;
56 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfAccessor;
57 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfDomAccessor;
58 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
59 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
60 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.StreamKey;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Guicutthrough;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Inventory;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.NetworkElementDeviceType;
64 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
65 import org.opendaylight.yangtools.yang.common.QName;
66 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
67 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.InstanceIdentifierBuilder;
68 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
69 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
70 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
71 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
72 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
73 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
74 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
75 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
76 import org.slf4j.Logger;
77 import org.slf4j.LoggerFactory;
78
79 public class ORanDOMNetworkElement implements NetworkElement, IConfigChangedListener {
80
81     private static final Logger LOG = LoggerFactory.getLogger(ORanDOMNetworkElement.class);
82
83     private final @NonNull NetconfDomAccessor netconfDomAccessor;
84     private final @NonNull DataProvider databaseService;
85     private final @NonNull FaultService faultService;
86     private final @NonNull NotificationService notificationService;
87     private final @NonNull ORanDOMChangeNotificationListener oranDomChangeNotificationListener;
88     private final @NonNull ORanDOMFaultNotificationListener oranDomFaultNotificationListener;
89     private final @NonNull ORanDOMSupervisionNotificationListener oranDomSupervisionNotificationListener;
90     private final @NonNull VESCollectorService vesCollectorService;
91     private final @NonNull ORanRegistrationToVESpnfRegistrationMapper mapper;
92     private final Optional<OnapSystem> onapSystem;
93     private final Optional<ORANFM> oranfm;
94     private ORanDMConfig oranSupervisionConfig;
95
96     public ORanDOMNetworkElement(@NonNull NetconfDomAccessor netconfDomAccessor,
97             @NonNull DeviceManagerServiceProvider serviceProvider, ORanDMConfig oranSupervisionConfig,
98             ConfigurationFileRepresentation configFileRepresentation) {
99         LOG.debug("Create {}", ORanDOMNetworkElement.class.getSimpleName());
100         this.netconfDomAccessor = Objects.requireNonNull(netconfDomAccessor);
101         Objects.requireNonNull(serviceProvider);
102         this.databaseService = serviceProvider.getDataProvider();
103         this.vesCollectorService = serviceProvider.getVESCollectorService();
104         this.faultService = serviceProvider.getFaultService();
105         this.notificationService = serviceProvider.getNotificationService();
106         this.onapSystem = OnapSystem.getModule(netconfDomAccessor);
107         this.oranfm = ORANFM.getModule(netconfDomAccessor);
108         this.oranSupervisionConfig = oranSupervisionConfig;
109
110         configFileRepresentation.registerConfigChangedListener(this);
111         this.oranDomChangeNotificationListener =
112                 new ORanDOMChangeNotificationListener(netconfDomAccessor, vesCollectorService, databaseService);
113
114         this.oranDomFaultNotificationListener =
115                 new ORanDOMFaultNotificationListener(netconfDomAccessor, this.oranfm, vesCollectorService,
116                         serviceProvider.getFaultService(), serviceProvider.getWebsocketService(), databaseService);
117
118         this.oranDomSupervisionNotificationListener = new ORanDOMSupervisionNotificationListener(netconfDomAccessor,
119                 vesCollectorService, databaseService, oranSupervisionConfig);
120
121         this.mapper = new ORanRegistrationToVESpnfRegistrationMapper(netconfDomAccessor, vesCollectorService);
122     }
123
124     @Override
125     public void register() {
126         Collection<MapEntryNode> componentList = initialReadFromNetworkElement();
127         oranDomFaultNotificationListener.setComponentList(componentList);
128         publishMountpointToVES(componentList);
129         QName[] notifications = {ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_NETCONF_CONFIG_CHANGE,
130                 ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_NETCONF_CONFIRMED_COMMIT,
131                 ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_NETCONF_SESSION_START,
132                 ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_NETCONF_SESSION_END,
133                 ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_NETCONF_CAPABILITY_CHANGE};
134         netconfDomAccessor.doRegisterNotificationListener(oranDomChangeNotificationListener, notifications);
135
136         QName[] faultNotification = {oranfm.get().getAlarmNotifQName()};
137         netconfDomAccessor.doRegisterNotificationListener(oranDomFaultNotificationListener, faultNotification);
138
139         Capabilities x = netconfDomAccessor.getCapabilites();
140         if (x.isSupportingNamespaceAndRevision(ORanDeviceManagerQNames.ORAN_SUPERVISION_MODULE)) {
141             LOG.debug("Device {} supports oran-supervision", netconfDomAccessor.getNodeId().getValue());
142             oranDomSupervisionNotificationListener.setComponentList(componentList);
143             QName[] supervisionNotification = {ORanDeviceManagerQNames.ORAN_SUPERVISION_NOTIFICATION};
144             netconfDomAccessor.doRegisterNotificationListener(oranDomSupervisionNotificationListener,
145                     supervisionNotification);
146         }
147         // Output notification streams to LOG
148         @SuppressWarnings("unused")
149         Map<StreamKey, Stream> streams = netconfDomAccessor.getNotificationStreamsAsMap();
150         // Register to default stream
151         netconfDomAccessor.invokeCreateSubscription();
152         if (x.isSupportingNamespaceAndRevision(ORanDeviceManagerQNames.ORAN_SUPERVISION_MODULE)) {
153             ORanSupervisionRPCImpl.invokeWatchdogReset(netconfDomAccessor, oranSupervisionConfig);
154             oranDomSupervisionNotificationListener.registerForNotificationReceivedEvent(
155                     new ORanNotificationObserverImpl(netconfDomAccessor, oranSupervisionConfig));
156         }
157     }
158
159     public Collection<MapEntryNode> initialReadFromNetworkElement() {
160         Collection<MapEntryNode> componentMapEntries = null;
161         NormalizedNode hwData = readHardware();
162
163         if (hwData != null) {
164             ContainerNode hwContainer = (ContainerNode) hwData;
165             MapNode componentMap = (MapNode) hwContainer
166                     .childByArg(new NodeIdentifier(ORanDeviceManagerQNames.IETF_HW_COMPONENT_LIST));
167             if (componentMap != null) {
168                 componentMapEntries = componentMap.body();
169                 List<Inventory> inventoryList =
170                         ORanDOMToInternalDataModel.getInventoryList(netconfDomAccessor.getNodeId(), hwData);
171                 databaseService.writeInventory(netconfDomAccessor.getNodeId().getValue(), inventoryList);
172             }
173         } else {
174             componentMapEntries = Collections.emptyList();
175         }
176
177         if (oranfm.isPresent()) {
178             getActiveAlarms();
179         }
180         if (onapSystem.isPresent()) {
181             AugmentationNode gcData = (AugmentationNode) onapSystem.get().getOnapSystemData();
182             Optional<Guicutthrough> oGuicutthrough =
183                     ORanDOMToInternalDataModel.getGuicutthrough(gcData, onapSystem.get());
184             if (oGuicutthrough.isPresent()) {
185                 databaseService.writeGuiCutThroughData(oGuicutthrough.get(), netconfDomAccessor.getNodeId().getValue());
186             }
187         }
188         return componentMapEntries;
189     }
190
191     @Override
192     public void deregister() {
193         /*
194          * if (oranDomChangeNotificationListener != null) {
195          * this.oranDomChangeNotificationListener.close(); } if
196          * (oRanFaultListenerRegistrationResult != null) {
197          * this.oRanFaultListenerRegistrationResult.close(); } ;
198          */
199         databaseService.clearGuiCutThroughEntriesOfNode(getMountpointId());
200         faultService.removeAllCurrentProblemsOfNode(getNodeId());
201     }
202
203     @Override
204     public NodeId getNodeId() {
205         return netconfDomAccessor.getNodeId();
206     }
207
208     @Override
209     public NetworkElementDeviceType getDeviceType() {
210         return NetworkElementDeviceType.ORAN;
211     }
212
213     @Override
214     public <L extends NetworkElementService> Optional<L> getService(Class<L> clazz) {
215         return Optional.empty();
216     }
217
218     @Override
219     public void warmstart() {
220         faultService.removeAllCurrentProblemsOfNode(getNodeId());
221     }
222
223     @Override
224     public Optional<NetconfAccessor> getAcessor() {
225         return Optional.of(netconfDomAccessor);
226     }
227
228     // Private functions
229
230     private String getMountpointId() {
231         return getNodeId().getValue();
232     }
233
234     private NormalizedNode readHardware() {
235         InstanceIdentifierBuilder hardwareIIDBuilder =
236                 YangInstanceIdentifier.builder().node(ORanDeviceManagerQNames.IETF_HW_CONTAINER);
237
238         Optional<NormalizedNode> oData =
239                 netconfDomAccessor.readDataNode(LogicalDatastoreType.OPERATIONAL, hardwareIIDBuilder.build());
240         if (oData.isPresent()) {
241             return oData.get();
242         }
243         return null;
244     }
245
246     // VES related
247     private void publishMountpointToVES(Collection<MapEntryNode> componentList) {
248         /*
249          * 1. Check if this device is in the list of allowed-devices. 2. If device
250          * exists in allowed-devices, then create VES pnfRegistration event and publish
251          * to VES
252          */
253         if (vesCollectorService.getConfig().isVESCollectorEnabled() && inAllowedDevices(getMountpointId())) {
254             for (MapEntryNode component : ORanDOMToInternalDataModel.getRootComponents(componentList)) {
255                 // Just get one component. At the moment we don't care which one. Also since
256                 // there is only one management address, we assume there will be only one
257                 // chassis.
258                 // If the device supports subtended configuration then it is assumed that the
259                 // Chassis containing the management interface will be the root component and
260                 // there will be only one root.
261                 VESCommonEventHeaderPOJO header = mapper.mapCommonEventHeader(component);
262                 VESPNFRegistrationFieldsPOJO body = mapper.mapPNFRegistrationFields(component);
263                 try {
264                     vesCollectorService.publishVESMessage(vesCollectorService.generateVESEvent(header, body));
265                 } catch (JsonProcessingException e) {
266                     LOG.warn("Error while serializing VES Event to String ", e);
267                 }
268             }
269         }
270     }
271
272     private boolean inAllowedDevices(String mountpointName) {
273         InstanceIdentifierBuilder callhomeServerIID =
274                 YangInstanceIdentifier.builder().node(ORanDeviceManagerQNames.CALLHOME_SERVER_CONTAINER);
275         final InstanceIdentifierBuilder allowedDevicesIID = YangInstanceIdentifier.builder(callhomeServerIID.build())
276                 .node(ORanDeviceManagerQNames.CALLHOME_SERVER_ALLOWED_DEVICE);
277
278         Optional<NormalizedNode> allowedDevices = netconfDomAccessor
279                 .readControllerDataNode(LogicalDatastoreType.CONFIGURATION, allowedDevicesIID.build());
280
281         if (allowedDevices.isPresent()) {
282             ContainerNode allowedDevicesNode = (ContainerNode) allowedDevices.get();
283             MapNode deviceList = (MapNode) allowedDevicesNode
284                     .childByArg(new NodeIdentifier(ORanDeviceManagerQNames.CALLHOME_SERVER_ALLOWED_DEVICE_DEVICE_LIST));
285             if (deviceList != null) {
286                 Collection<MapEntryNode> deviceListCollection = deviceList.body();
287                 for (MapEntryNode device : deviceListCollection) {
288                     //                                  String deviceName = device.getIdentifier()
289                     //                                                  .getValue(ORanDeviceManagerQNames.CALLHOME_SERVER_ALLOWED_DEVICE_KEY).toString();
290                     String deviceName = ORanDMDOMUtility.getLeafValue(device,
291                             ORanDeviceManagerQNames.CALLHOME_SERVER_ALLOWED_DEVICE_KEY);
292                     if (deviceName != null && deviceName.equals(mountpointName)) {
293                         LOG.debug("Mountpoint {} is part of allowed-devices list", mountpointName);
294                         return true;
295                     }
296                 }
297             }
298         }
299
300         LOG.debug("Mountpoint {} is not part of allowed-devices list", mountpointName);
301         return false;
302     }
303
304     private void getActiveAlarms() {
305         InstanceIdentifierBuilder activeAlarmListBuilder =
306                 YangInstanceIdentifier.builder().node(oranfm.get().getFaultActiveAlarmListQName());
307         Optional<NormalizedNode> oData =
308                 netconfDomAccessor.readDataNode(LogicalDatastoreType.OPERATIONAL, activeAlarmListBuilder.build());
309         if (oData.isPresent()) {
310             ContainerNode cn = (ContainerNode) oData.get();
311             UnkeyedListNode activeAlarmsList =
312                     (UnkeyedListNode) cn.childByArg(new NodeIdentifier(oranfm.get().getFaultActiveAlarmsQName()));
313             for (UnkeyedListEntryNode activeAlarmEntry : activeAlarmsList.body())
314                 faultService.faultNotification(ORanDOMToInternalDataModel.getFaultLog(activeAlarmEntry, oranfm.get(),
315                         netconfDomAccessor.getNodeId()));
316         }
317     }
318
319     @Override
320     public void onConfigChanged() {
321         LOG.info("O-RU Supervision Watchdog timers changed, resetting in O-RU via RPC");
322         ORanSupervisionRPCImpl.invokeWatchdogReset(netconfDomAccessor, oranSupervisionConfig);
323     }
324
325 }