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 / 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 com.google.common.collect.Sets;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Objects;
31 import java.util.Optional;
32 import org.eclipse.jdt.annotation.NonNull;
33 import org.eclipse.jdt.annotation.Nullable;
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.service.DeviceManagerServiceProvider;
38 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.FaultService;
39 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.NotificationService;
40 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorService;
41 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESCommonEventHeaderPOJO;
42 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESPNFRegistrationFieldsPOJO;
43 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.Capabilities;
44 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfAccessor;
45 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfDomAccessor;
46 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.StreamKey;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Guicutthrough;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Inventory;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.NetworkElementDeviceType;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
53 import org.opendaylight.yangtools.yang.common.QName;
54 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
55 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
56 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.InstanceIdentifierBuilder;
57 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
58 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
59 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
60 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
61 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
64
65 public class ORanDOMNetworkElement implements NetworkElement {
66
67     private static final Logger LOG = LoggerFactory.getLogger(ORanDOMNetworkElement.class);
68
69     private final @NonNull NetconfDomAccessor netconfDomAccessor;
70     private final @NonNull DataProvider databaseService;
71     private final @NonNull FaultService faultService;
72     private final @NonNull NotificationService notificationService;
73     private final @NonNull ORanDOMChangeNotificationListener oranDomChangeNotificationListener;
74     private final @NonNull ORanDOMFaultNotificationListener oranDomFaultNotificationListener;
75     private final @NonNull VESCollectorService vesCollectorService;
76     private final @NonNull ORanRegistrationToVESpnfRegistrationMapper mapper;
77
78     public ORanDOMNetworkElement(@NonNull NetconfDomAccessor netconfDomAccessor,
79             @NonNull DeviceManagerServiceProvider serviceProvider) {
80         LOG.info("Create {}", ORanDOMNetworkElement.class.getSimpleName());
81         this.netconfDomAccessor = Objects.requireNonNull(netconfDomAccessor);
82         Objects.requireNonNull(serviceProvider);
83         this.databaseService = serviceProvider.getDataProvider();
84         this.vesCollectorService = serviceProvider.getVESCollectorService();
85         this.faultService = serviceProvider.getFaultService();
86         this.notificationService = serviceProvider.getNotificationService();
87
88         this.oranDomChangeNotificationListener =
89                 new ORanDOMChangeNotificationListener(netconfDomAccessor, vesCollectorService, databaseService);
90
91         this.oranDomFaultNotificationListener =
92                 new ORanDOMFaultNotificationListener(netconfDomAccessor, vesCollectorService,
93                         serviceProvider.getFaultService(), serviceProvider.getWebsocketService(), databaseService);
94
95         this.mapper = new ORanRegistrationToVESpnfRegistrationMapper(netconfDomAccessor, vesCollectorService);
96     }
97
98     @Override
99     public void register() {
100         Collection<MapEntryNode> componentList = initialReadFromNetworkElement();
101         oranDomFaultNotificationListener.setComponentList(componentList);
102         publishMountpointToVES(componentList);
103         QName[] notifications = {ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_NETCONF_CONFIG_CHANGE,
104                 ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_NETCONF_CONFIRMED_COMMIT,
105                 ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_NETCONF_SESSION_START,
106                 ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_NETCONF_SESSION_END,
107                 ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_NETCONF_CAPABILITY_CHANGE};
108         netconfDomAccessor.doRegisterNotificationListener(oranDomChangeNotificationListener, notifications);
109         QName[] faultNotification = {ORanDeviceManagerQNames.ORAN_FM_ALARM_NOTIF};
110         netconfDomAccessor.doRegisterNotificationListener(oranDomFaultNotificationListener, faultNotification);
111         // Output notification streams to LOG
112         @SuppressWarnings("unused")
113         Map<StreamKey, Stream> streams = netconfDomAccessor.getNotificationStreamsAsMap();
114         // Register to default stream
115         netconfDomAccessor.invokeCreateSubscription();
116     }
117
118     public Collection<MapEntryNode> initialReadFromNetworkElement() {
119         Collection<MapEntryNode> componentMapEntries = null;
120         NormalizedNode hwData = readHardware();
121
122         if (hwData != null) {
123             ContainerNode hwContainer = (ContainerNode) hwData;
124             MapNode componentMap = (MapNode) hwContainer
125                     .childByArg(new NodeIdentifier(ORanDeviceManagerQNames.IETF_HW_COMPONENT_LIST));
126             if (componentMap != null) {
127                 componentMapEntries = componentMap.body();
128                 List<Inventory> inventoryList =
129                         ORanDOMToInternalDataModel.getInventoryList(netconfDomAccessor.getNodeId(), hwData);
130                 databaseService.writeInventory(netconfDomAccessor.getNodeId().getValue(), inventoryList);
131             }
132         } else {
133             componentMapEntries = Collections.emptyList();
134         }
135
136         Optional<Guicutthrough> oGuicutthrough = ORanDOMToInternalDataModel.getGuicutthrough(getOnapSystemData());
137         if (oGuicutthrough.isPresent()) {
138             databaseService.writeGuiCutThroughData(oGuicutthrough.get(), netconfDomAccessor.getNodeId().getValue());
139         }
140         return componentMapEntries;
141     }
142
143     @Override
144     public void deregister() {
145         /*
146          * if (oranDomChangeNotificationListener != null) {
147          * this.oranDomChangeNotificationListener.close(); } if
148          * (oRanFaultListenerRegistrationResult != null) {
149          * this.oRanFaultListenerRegistrationResult.close(); } ;
150          */
151         databaseService.clearGuiCutThroughEntriesOfNode(getMountpointId());
152     }
153
154     @Override
155     public NodeId getNodeId() {
156         return netconfDomAccessor.getNodeId();
157     }
158
159     @Override
160     public NetworkElementDeviceType getDeviceType() {
161         return NetworkElementDeviceType.ORAN;
162     }
163
164     @Override
165     public <L extends NetworkElementService> Optional<L> getService(Class<L> clazz) {
166         return Optional.empty();
167     }
168
169     @Override
170     public void warmstart() {}
171
172     @Override
173     public Optional<NetconfAccessor> getAcessor() {
174         return Optional.of(netconfDomAccessor);
175     }
176
177     // Private functions
178
179     private String getMountpointId() {
180         return getNodeId().getValue();
181     }
182
183     private NormalizedNode readHardware() {
184         InstanceIdentifierBuilder hardwareIIDBuilder =
185                 YangInstanceIdentifier.builder().node(ORanDeviceManagerQNames.IETF_HW_CONTAINER);
186
187         Optional<NormalizedNode> oData =
188                 netconfDomAccessor.readDataNode(LogicalDatastoreType.OPERATIONAL, hardwareIIDBuilder.build());
189         if (oData.isPresent()) {
190             return oData.get();
191         }
192         return null;
193     }
194
195     // Read from device
196     /**
197      * Read system data with GUI cut through information from device if ONAP_SYSTEM YANG is supported.
198      *
199      * @return NormalizedNode data with GUI cut through information or null if not available.
200      */
201     private @Nullable NormalizedNode getOnapSystemData() {
202         LOG.info("Get System1 for mountpoint {}", netconfDomAccessor.getNodeId().getValue());
203         @NonNull
204         InstanceIdentifierBuilder ietfSystemIID =
205                 YangInstanceIdentifier.builder().node(ORanDeviceManagerQNames.IETF_SYSTEM_CONTAINER);
206         @NonNull
207         AugmentationIdentifier onapSystemIID = YangInstanceIdentifier.AugmentationIdentifier.create(
208                 Sets.newHashSet(ORanDeviceManagerQNames.ONAP_SYSTEM_NAME, ORanDeviceManagerQNames.ONAP_SYSTEM_WEB_UI));
209         InstanceIdentifierBuilder augmentedOnapSystem =
210                 YangInstanceIdentifier.builder(ietfSystemIID.build()).node(onapSystemIID);
211         Capabilities x = netconfDomAccessor.getCapabilites();
212         LOG.info("Capabilites: {}", x);
213         if (x.isSupportingNamespace(ORanDeviceManagerQNames.ONAP_SYSTEM_QNAME)) {
214             Optional<NormalizedNode> res =
215                     netconfDomAccessor.readDataNode(LogicalDatastoreType.OPERATIONAL, augmentedOnapSystem.build());
216             LOG.debug("Result of System1 = {}", res);
217             return res.isPresent() ? res.get() : null;
218         } else {
219             LOG.debug("No GUI cut through support");
220             return null;
221         }
222     }
223
224     // VES related
225     private void publishMountpointToVES(Collection<MapEntryNode> componentList) {
226         /*
227          * 1. Check if this device is in the list of allowed-devices. 2. If device
228          * exists in allowed-devices, then create VES pnfRegistration event and publish
229          * to VES
230          */
231         if (vesCollectorService.getConfig().isVESCollectorEnabled() && inAllowedDevices(getMountpointId())) {
232             for (MapEntryNode component : ORanDOMToInternalDataModel.getRootComponents(componentList)) {
233                 // Just get one component. At the moment we don't care which one. Also since
234                 // there is only one management address, we assume there will be only one
235                 // chassis.
236                 // If the device supports subtended configuration then it is assumed that the
237                 // Chassis containing the management interface will be the root component and
238                 // there will be only one root.
239                 VESCommonEventHeaderPOJO header = mapper.mapCommonEventHeader(component);
240                 VESPNFRegistrationFieldsPOJO body = mapper.mapPNFRegistrationFields(component);
241                 try {
242                     vesCollectorService.publishVESMessage(vesCollectorService.generateVESEvent(header, body));
243                 } catch (JsonProcessingException e) {
244                     LOG.warn("Error while serializing VES Event to String ", e);
245                 }
246             }
247         }
248     }
249
250     private boolean inAllowedDevices(String mountpointName) {
251         InstanceIdentifierBuilder callhomeServerIID =
252                 YangInstanceIdentifier.builder().node(ORanDeviceManagerQNames.CALLHOME_SERVER_CONTAINER);
253         final InstanceIdentifierBuilder allowedDevicesIID = YangInstanceIdentifier.builder(callhomeServerIID.build())
254                 .node(ORanDeviceManagerQNames.CALLHOME_SERVER_ALLOWED_DEVICE);
255
256         Optional<NormalizedNode> allowedDevices = netconfDomAccessor
257                 .readControllerDataNode(LogicalDatastoreType.CONFIGURATION, allowedDevicesIID.build());
258
259         if (allowedDevices.isPresent()) {
260             ContainerNode allowedDevicesNode = (ContainerNode) allowedDevices.get();
261             MapNode deviceList = (MapNode) allowedDevicesNode
262                     .childByArg(new NodeIdentifier(ORanDeviceManagerQNames.CALLHOME_SERVER_ALLOWED_DEVICE_DEVICE_LIST));
263             if (deviceList != null) {
264                 Collection<MapEntryNode> deviceListCollection = deviceList.body();
265                 for (MapEntryNode device : deviceListCollection) {
266                     //                                  String deviceName = device.getIdentifier()
267                     //                                                  .getValue(ORanDeviceManagerQNames.CALLHOME_SERVER_ALLOWED_DEVICE_KEY).toString();
268                     String deviceName = ORanDMDOMUtility.getLeafValue(device,
269                             ORanDeviceManagerQNames.CALLHOME_SERVER_ALLOWED_DEVICE_KEY);
270                     if (deviceName != null && deviceName.equals(mountpointName)) {
271                         LOG.info("Mountpoint {} is part of allowed-devices list", mountpointName);
272                         return true;
273                     }
274                 }
275             }
276         }
277
278         LOG.info("Mountpoint {} is not part of allowed-devices list", mountpointName);
279         return false;
280     }
281
282 }