72cda1679901f687ec6cbdb02fd13eea7ad36c9c
[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.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     }
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
220     @Override
221     public Optional<NetconfAccessor> getAcessor() {
222         return Optional.of(netconfDomAccessor);
223     }
224
225     // Private functions
226
227     private String getMountpointId() {
228         return getNodeId().getValue();
229     }
230
231     private NormalizedNode readHardware() {
232         InstanceIdentifierBuilder hardwareIIDBuilder =
233                 YangInstanceIdentifier.builder().node(ORanDeviceManagerQNames.IETF_HW_CONTAINER);
234
235         Optional<NormalizedNode> oData =
236                 netconfDomAccessor.readDataNode(LogicalDatastoreType.OPERATIONAL, hardwareIIDBuilder.build());
237         if (oData.isPresent()) {
238             return oData.get();
239         }
240         return null;
241     }
242
243     // VES related
244     private void publishMountpointToVES(Collection<MapEntryNode> componentList) {
245         /*
246          * 1. Check if this device is in the list of allowed-devices. 2. If device
247          * exists in allowed-devices, then create VES pnfRegistration event and publish
248          * to VES
249          */
250         if (vesCollectorService.getConfig().isVESCollectorEnabled() && inAllowedDevices(getMountpointId())) {
251             for (MapEntryNode component : ORanDOMToInternalDataModel.getRootComponents(componentList)) {
252                 // Just get one component. At the moment we don't care which one. Also since
253                 // there is only one management address, we assume there will be only one
254                 // chassis.
255                 // If the device supports subtended configuration then it is assumed that the
256                 // Chassis containing the management interface will be the root component and
257                 // there will be only one root.
258                 VESCommonEventHeaderPOJO header = mapper.mapCommonEventHeader(component);
259                 VESPNFRegistrationFieldsPOJO body = mapper.mapPNFRegistrationFields(component);
260                 try {
261                     vesCollectorService.publishVESMessage(vesCollectorService.generateVESEvent(header, body));
262                 } catch (JsonProcessingException e) {
263                     LOG.warn("Error while serializing VES Event to String ", e);
264                 }
265             }
266         }
267     }
268
269     private boolean inAllowedDevices(String mountpointName) {
270         InstanceIdentifierBuilder callhomeServerIID =
271                 YangInstanceIdentifier.builder().node(ORanDeviceManagerQNames.CALLHOME_SERVER_CONTAINER);
272         final InstanceIdentifierBuilder allowedDevicesIID = YangInstanceIdentifier.builder(callhomeServerIID.build())
273                 .node(ORanDeviceManagerQNames.CALLHOME_SERVER_ALLOWED_DEVICE);
274
275         Optional<NormalizedNode> allowedDevices = netconfDomAccessor
276                 .readControllerDataNode(LogicalDatastoreType.CONFIGURATION, allowedDevicesIID.build());
277
278         if (allowedDevices.isPresent()) {
279             ContainerNode allowedDevicesNode = (ContainerNode) allowedDevices.get();
280             MapNode deviceList = (MapNode) allowedDevicesNode
281                     .childByArg(new NodeIdentifier(ORanDeviceManagerQNames.CALLHOME_SERVER_ALLOWED_DEVICE_DEVICE_LIST));
282             if (deviceList != null) {
283                 Collection<MapEntryNode> deviceListCollection = deviceList.body();
284                 for (MapEntryNode device : deviceListCollection) {
285                     //                                  String deviceName = device.getIdentifier()
286                     //                                                  .getValue(ORanDeviceManagerQNames.CALLHOME_SERVER_ALLOWED_DEVICE_KEY).toString();
287                     String deviceName = ORanDMDOMUtility.getLeafValue(device,
288                             ORanDeviceManagerQNames.CALLHOME_SERVER_ALLOWED_DEVICE_KEY);
289                     if (deviceName != null && deviceName.equals(mountpointName)) {
290                         LOG.debug("Mountpoint {} is part of allowed-devices list", mountpointName);
291                         return true;
292                     }
293                 }
294             }
295         }
296
297         LOG.debug("Mountpoint {} is not part of allowed-devices list", mountpointName);
298         return false;
299     }
300
301     private void getActiveAlarms() {
302         InstanceIdentifierBuilder activeAlarmListBuilder =
303                 YangInstanceIdentifier.builder().node(oranfm.get().getFaultActiveAlarmListQName());
304         Optional<NormalizedNode> oData =
305                 netconfDomAccessor.readDataNode(LogicalDatastoreType.OPERATIONAL, activeAlarmListBuilder.build());
306         if (oData.isPresent()) {
307             ContainerNode cn = (ContainerNode) oData.get();
308             UnkeyedListNode activeAlarmsList =
309                     (UnkeyedListNode) cn.childByArg(new NodeIdentifier(oranfm.get().getFaultActiveAlarmsQName()));
310             int counter = 0;
311             for (UnkeyedListEntryNode activeAlarmEntry : activeAlarmsList.body())
312                 faultService.faultNotification(ORanDOMToInternalDataModel.getFaultLog(activeAlarmEntry, oranfm.get(),
313                         netconfDomAccessor.getNodeId(), Integer.valueOf(counter++)));
314         }
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 }