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