df81f60bf4289222a123e8848134ecf69cd30995
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.impl;
19
20 import com.fasterxml.jackson.core.JsonProcessingException;
21 import java.util.Collection;
22 import java.util.List;
23 import java.util.Optional;
24 import org.eclipse.jdt.annotation.NonNull;
25 import org.onap.ccsdk.features.sdnr.wt.common.YangHelper;
26 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider;
27 import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.service.NetworkElement;
28 import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.service.NetworkElementService;
29 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.NotificationProxyParser;
30 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorService;
31 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESCommonEventHeaderPOJO;
32 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESPNFRegistrationFieldsPOJO;
33 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfAccessor;
34 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfBindingAccessor;
35 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfNotifications;
36 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.hardware.rev180313.Hardware;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.hardware.rev180313.hardware.Component;
40 import org.opendaylight.yang.gen.v1.urn.onap.system.rev201026.System1;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Guicutthrough;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Inventory;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.NetworkElementDeviceType;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev161109.NetconfCallhomeServer;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev161109.netconf.callhome.server.AllowedDevices;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev161109.netconf.callhome.server.allowed.devices.Device;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
48 import org.opendaylight.yangtools.concepts.ListenerRegistration;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.opendaylight.yangtools.yang.binding.NotificationListener;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 public class ORanNetworkElement implements NetworkElement {
55
56     private static final Logger log = LoggerFactory.getLogger(ORanNetworkElement.class);
57
58     private final NetconfBindingAccessor netconfAccessor;
59
60     private final DataProvider databaseService;
61
62     @SuppressWarnings("unused")
63     private final VESCollectorService vesCollectorService;
64
65     private ListenerRegistration<NotificationListener> oRanListenerRegistrationResult;
66     private @NonNull final ORanChangeNotificationListener oRanListener;
67     private ListenerRegistration<NotificationListener> oRanFaultListenerRegistrationResult;
68     private @NonNull final ORanFaultNotificationListener oRanFaultListener;
69     private final NotificationProxyParser notificationProxyParser;
70     private @NonNull ORanRegistrationToVESpnfRegistrationMapper mapper;
71     private Collection<Component> componentList;
72     private static int sequenceNo = 0;
73
74     ORanNetworkElement(NetconfBindingAccessor netconfAccess, DataProvider databaseService,
75             VESCollectorService vesCollectorService) {
76         log.info("Create {}", ORanNetworkElement.class.getSimpleName());
77         this.netconfAccessor = netconfAccess;
78         this.databaseService = databaseService;
79         this.vesCollectorService = vesCollectorService;
80         this.notificationProxyParser = vesCollectorService.getNotificationProxyParser();
81
82         this.oRanListenerRegistrationResult = null;
83         this.oRanListener = new ORanChangeNotificationListener(netconfAccessor, databaseService, vesCollectorService,
84                 notificationProxyParser);
85
86         this.oRanFaultListenerRegistrationResult = null;
87         this.oRanFaultListener =
88                 new ORanFaultNotificationListener(netconfAccessor, databaseService, vesCollectorService);
89     }
90
91     private void initialReadFromNetworkElement() {
92         Hardware hardware = readHardware();
93         if (hardware != null) {
94             componentList = YangHelper.getCollection(hardware.nonnullComponent());
95             List<Inventory> inventoryList =
96                     ORanToInternalDataModel.getInventoryList(netconfAccessor.getNodeId(), componentList);
97             inventoryList.forEach(databaseService::writeInventory);
98         }
99
100         Optional<Guicutthrough> oGuicutthrough = ORanToInternalDataModel.getGuicutthrough(getOnapSystemData());
101         if (oGuicutthrough.isPresent()) {
102             databaseService.writeGuiCutThroughData(oGuicutthrough.get(), netconfAccessor.getNodeId().getValue());
103         }
104     }
105
106     @Override
107     public NetworkElementDeviceType getDeviceType() {
108         return NetworkElementDeviceType.ORAN;
109     }
110
111     @Override
112     public void register() {
113         initialReadFromNetworkElement();
114         // Publish the mountpoint to VES if enabled
115         publishMountpointToVES();
116         // Register call back class for receiving notifications
117         Optional<NetconfNotifications> oNotifications = netconfAccessor.getNotificationAccessor();
118         if (oNotifications.isPresent()) {
119             NetconfNotifications notifications = oNotifications.get();
120             this.oRanListenerRegistrationResult = netconfAccessor.doRegisterNotificationListener(oRanListener);
121             this.oRanFaultListenerRegistrationResult =
122                     netconfAccessor.doRegisterNotificationListener(oRanFaultListener);
123             // Register notifications stream
124             if (notifications.isNCNotificationsSupported()) {
125                 List<Stream> streamList = notifications.getNotificationStreams();
126                 notifications.registerNotificationsStream(NetconfBindingAccessor.DefaultNotificationsStream); // Always register first to default stream
127                 notifications.registerNotificationsStream(streamList);
128             } else {
129                 notifications.registerNotificationsStream(NetconfBindingAccessor.DefaultNotificationsStream);
130             }
131         }
132     }
133
134     @Override
135     public void deregister() {
136         if (oRanListenerRegistrationResult != null) {
137             this.oRanListenerRegistrationResult.close();
138         }
139         if (oRanFaultListenerRegistrationResult != null) {
140             this.oRanFaultListenerRegistrationResult.close();
141         } ;
142     }
143
144
145     @Override
146     public NodeId getNodeId() {
147         return netconfAccessor.getNodeId();
148     }
149
150     @Override
151     public <L extends NetworkElementService> Optional<L> getService(Class<L> clazz) {
152         return Optional.empty();
153     }
154
155     @Override
156     public void warmstart() {}
157
158     @Override
159     public Optional<NetconfAccessor> getAcessor() {
160         return Optional.of(netconfAccessor);
161     }
162
163     // Read from device
164     private System1 getOnapSystemData() {
165         log.info("Get System1 for class {} from mountpoint {}", netconfAccessor.getNodeId().getValue());
166
167         InstanceIdentifier<System1> system1IID = InstanceIdentifier
168                 .builder(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.system.rev140806.System.class)
169                 .augmentation(System1.class).build();
170         System1 res = netconfAccessor.getTransactionUtils().readData(netconfAccessor.getDataBroker(),
171                 LogicalDatastoreType.OPERATIONAL, system1IID);
172         log.debug("Result of System1 = {}", res);
173         return res;
174     }
175
176     private Hardware readHardware() {
177         final Class<Hardware> clazzPac = Hardware.class;
178         log.info("DBRead Get hardware for class {} from mountpoint {}", clazzPac.getSimpleName(),
179                 netconfAccessor.getNodeId().getValue());
180         InstanceIdentifier<Hardware> hardwareIID = InstanceIdentifier.builder(clazzPac).build();
181         Hardware res = netconfAccessor.getTransactionUtils().readData(netconfAccessor.getDataBroker(),
182                 LogicalDatastoreType.OPERATIONAL, hardwareIID);
183         log.debug("Result of Hardware = {}", res);
184         return res;
185     }
186
187     private void publishMountpointToVES() {
188         log.debug("In publishMountpointToVES()");
189
190         /**
191          * 1. Check if this device is in the list of allowed-devices.
192          * 2. If device exists in allowed-devices, then create VES pnfRegistration event and publish to VES
193          */
194         if (inAllowedDevices(netconfAccessor.getNodeId().getValue())) {
195             if (vesCollectorService.getConfig().isVESCollectorEnabled()) {
196
197                 for (Component component : ORanToInternalDataModel.getRootComponents(componentList)) {
198                     //Just get one component. At the moment we don't care which one. Also since there is only one management address, we assume there will be only one chassis.
199                     //If the device supports subtended configuration then it is assumed that the Chassis containing the management interface will be the root component and there will be only one root.
200                     this.mapper = new ORanRegistrationToVESpnfRegistrationMapper(netconfAccessor,
201                             vesCollectorService, component);
202                     VESCommonEventHeaderPOJO header =
203                             mapper.mapCommonEventHeader(sequenceNo++);
204                     VESPNFRegistrationFieldsPOJO body = mapper.mapPNFRegistrationFields();
205                     try {
206                         vesCollectorService.publishVESMessage(vesCollectorService.generateVESEvent(header, body));
207                     } catch (JsonProcessingException e) {
208                         log.warn("Error while serializing VES Event to String ", e);
209                         e.printStackTrace();
210                     }
211
212                 }
213             }
214
215         }
216
217     }
218
219     private boolean inAllowedDevices(String mountpointName) {
220         final InstanceIdentifier<AllowedDevices> ALL_DEVICES =
221                 InstanceIdentifier.create(NetconfCallhomeServer.class).child(AllowedDevices.class);
222
223         AllowedDevices allowedDevices;
224         allowedDevices = netconfAccessor.getTransactionUtils().readData(
225                 netconfAccessor.getControllerBindingDataBroker(), LogicalDatastoreType.CONFIGURATION, ALL_DEVICES);
226
227         if (allowedDevices != null) {
228             Collection<Device> deviceList = YangHelper.getCollection(allowedDevices.nonnullDevice());
229             for (Device device : deviceList) {
230                 log.info("Device in allowed-devices is - {}", device.getUniqueId());
231                 if (device.getUniqueId().equals(netconfAccessor.getNodeId().getValue())) {
232                     log.info("Mountpoint is part of allowed-devices list");
233                     return true;
234                 }
235             }
236         }
237
238         log.info("Mountpoint {} is not part of allowed-devices list", mountpointName);
239         return false;
240     }
241
242 }