7c69f832b1a086703dd8e3f19dedf0fdcb466a67
[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.opendaylight.mdsal.common.api.LogicalDatastoreType;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.hardware.rev180313.Hardware;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.hardware.rev180313.hardware.Component;
39 import org.opendaylight.yang.gen.v1.urn.onap.system.rev201026.System1;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Guicutthrough;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Inventory;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.NetworkElementDeviceType;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev161109.NetconfCallhomeServer;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev161109.netconf.callhome.server.AllowedDevices;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev161109.netconf.callhome.server.allowed.devices.Device;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
47 import org.opendaylight.yangtools.concepts.ListenerRegistration;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.opendaylight.yangtools.yang.binding.NotificationListener;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 public class ORanNetworkElement implements NetworkElement {
54
55     private static final Logger log = LoggerFactory.getLogger(ORanNetworkElement.class);
56
57     private final NetconfBindingAccessor netconfAccessor;
58
59     private final DataProvider databaseService;
60
61     @SuppressWarnings("unused")
62     private final VESCollectorService vesCollectorService;
63
64     private ListenerRegistration<NotificationListener> oRanListenerRegistrationResult;
65     private @NonNull final ORanChangeNotificationListener oRanListener;
66     private ListenerRegistration<NotificationListener> oRanFaultListenerRegistrationResult;
67     private @NonNull final ORanFaultNotificationListener oRanFaultListener;
68     private final NotificationProxyParser notificationProxyParser;
69     private @NonNull ORanRegistrationToVESpnfRegistrationMapper mapper;
70     private Collection<Component> componentList;
71     private static int sequenceNo = 0;
72
73     ORanNetworkElement(NetconfBindingAccessor netconfAccess, DataProvider databaseService,
74             VESCollectorService vesCollectorService) {
75         log.info("Create {}", ORanNetworkElement.class.getSimpleName());
76         this.netconfAccessor = netconfAccess;
77         this.databaseService = databaseService;
78         this.vesCollectorService = vesCollectorService;
79         this.notificationProxyParser = vesCollectorService.getNotificationProxyParser();
80
81         this.oRanListenerRegistrationResult = null;
82         this.oRanListener = new ORanChangeNotificationListener(netconfAccessor, databaseService, vesCollectorService,
83                 notificationProxyParser);
84
85         this.oRanFaultListenerRegistrationResult = null;
86         this.oRanFaultListener =
87                 new ORanFaultNotificationListener(netconfAccessor, databaseService, vesCollectorService);
88     }
89
90     private void initialReadFromNetworkElement() {
91         Hardware hardware = readHardware();
92         if (hardware != null) {
93             componentList = YangHelper.getCollection(hardware.nonnullComponent());
94             List<Inventory> inventoryList =
95                     ORanToInternalDataModel.getInventoryList(netconfAccessor.getNodeId(), componentList);
96             databaseService.writeInventory(netconfAccessor.getNodeId().getValue(), inventoryList);
97         }
98
99         Optional<Guicutthrough> oGuicutthrough = ORanToInternalDataModel.getGuicutthrough(getOnapSystemData());
100         if (oGuicutthrough.isPresent()) {
101             databaseService.writeGuiCutThroughData(oGuicutthrough.get(), netconfAccessor.getNodeId().getValue());
102         }
103     }
104
105     @Override
106     public NetworkElementDeviceType getDeviceType() {
107         return NetworkElementDeviceType.ORAN;
108     }
109
110     @Override
111     public void register() {
112         initialReadFromNetworkElement();
113         // Publish the mountpoint to VES if enabled
114         publishMountpointToVES();
115         // Register call back class for receiving notifications
116             this.oRanListenerRegistrationResult = netconfAccessor.doRegisterNotificationListener(oRanListener);
117             this.oRanFaultListenerRegistrationResult =
118                     netconfAccessor.doRegisterNotificationListener(oRanFaultListener);
119             // Register notifications stream
120             if (netconfAccessor.isNotificationsRFC5277Supported()) {
121                 List<Stream> streamList = netconfAccessor.getNotificationStreams();
122                 netconfAccessor.registerNotificationsStream(NetconfBindingAccessor.DefaultNotificationsStream); // Always register first to default stream
123                 netconfAccessor.registerNotificationsStream(streamList);
124             } else {
125                 netconfAccessor.registerNotificationsStream(NetconfBindingAccessor.DefaultNotificationsStream);
126             }
127     }
128
129     @Override
130     public void deregister() {
131         if (oRanListenerRegistrationResult != null) {
132             this.oRanListenerRegistrationResult.close();
133         }
134         if (oRanFaultListenerRegistrationResult != null) {
135             this.oRanFaultListenerRegistrationResult.close();
136         } ;
137     }
138
139
140     @Override
141     public NodeId getNodeId() {
142         return netconfAccessor.getNodeId();
143     }
144
145     @Override
146     public <L extends NetworkElementService> Optional<L> getService(Class<L> clazz) {
147         return Optional.empty();
148     }
149
150     @Override
151     public void warmstart() {}
152
153     @Override
154     public Optional<NetconfAccessor> getAcessor() {
155         return Optional.of(netconfAccessor);
156     }
157
158     // Read from device
159     private System1 getOnapSystemData() {
160         log.info("Get System1 for class {} from mountpoint {}", netconfAccessor.getNodeId().getValue());
161
162         InstanceIdentifier<System1> system1IID = InstanceIdentifier
163                 .builder(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.system.rev140806.System.class)
164                 .augmentation(System1.class).build();
165         System1 res = netconfAccessor.getTransactionUtils().readData(netconfAccessor.getDataBroker(),
166                 LogicalDatastoreType.OPERATIONAL, system1IID);
167         log.debug("Result of System1 = {}", res);
168         return res;
169     }
170
171     private Hardware readHardware() {
172         final Class<Hardware> clazzPac = Hardware.class;
173         log.info("DBRead Get hardware for class {} from mountpoint {}", clazzPac.getSimpleName(),
174                 netconfAccessor.getNodeId().getValue());
175         InstanceIdentifier<Hardware> hardwareIID = InstanceIdentifier.builder(clazzPac).build();
176         Hardware res = netconfAccessor.getTransactionUtils().readData(netconfAccessor.getDataBroker(),
177                 LogicalDatastoreType.OPERATIONAL, hardwareIID);
178         log.debug("Result of Hardware = {}", res);
179         return res;
180     }
181
182     private void publishMountpointToVES() {
183         log.debug("In publishMountpointToVES()");
184
185         /**
186          * 1. Check if this device is in the list of allowed-devices.
187          * 2. If device exists in allowed-devices, then create VES pnfRegistration event and publish to VES
188          */
189         if (inAllowedDevices(netconfAccessor.getNodeId().getValue())) {
190             if (vesCollectorService.getConfig().isVESCollectorEnabled()) {
191
192                 for (Component component : ORanToInternalDataModel.getRootComponents(componentList)) {
193                     //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.
194                     //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.
195                     this.mapper = new ORanRegistrationToVESpnfRegistrationMapper(netconfAccessor,
196                             vesCollectorService, component);
197                     VESCommonEventHeaderPOJO header =
198                             mapper.mapCommonEventHeader(sequenceNo++);
199                     VESPNFRegistrationFieldsPOJO body = mapper.mapPNFRegistrationFields();
200                     try {
201                         vesCollectorService.publishVESMessage(vesCollectorService.generateVESEvent(header, body));
202                     } catch (JsonProcessingException e) {
203                         log.warn("Error while serializing VES Event to String ", e);
204                         e.printStackTrace();
205                     }
206
207                 }
208             }
209
210         }
211
212     }
213
214     private boolean inAllowedDevices(String mountpointName) {
215         final InstanceIdentifier<AllowedDevices> ALL_DEVICES =
216                 InstanceIdentifier.create(NetconfCallhomeServer.class).child(AllowedDevices.class);
217
218         AllowedDevices allowedDevices;
219         allowedDevices = netconfAccessor.getTransactionUtils().readData(
220                 netconfAccessor.getControllerBindingDataBroker(), LogicalDatastoreType.CONFIGURATION, ALL_DEVICES);
221
222         if (allowedDevices != null) {
223             Collection<Device> deviceList = YangHelper.getCollection(allowedDevices.nonnullDevice());
224             for (Device device : deviceList) {
225                 log.info("Device in allowed-devices is - {}", device.getUniqueId());
226                 if (device.getUniqueId().equals(netconfAccessor.getNodeId().getValue())) {
227                     log.info("Mountpoint is part of allowed-devices list");
228                     return true;
229                 }
230             }
231         }
232
233         log.info("Mountpoint {} is not part of allowed-devices list", mountpointName);
234         return false;
235     }
236
237 }