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