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