757768573a452961a0c11aab2b2293513d05f2d5
[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 java.util.Collection;
21 import java.util.List;
22 import java.util.Optional;
23 import org.eclipse.jdt.annotation.NonNull;
24 import org.onap.ccsdk.features.sdnr.wt.common.YangHelper;
25 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider;
26 import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.service.NetworkElement;
27 import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.service.NetworkElementService;
28 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorService;
29 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfAccessor;
30 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfBindingAccessor;
31 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfNotifications;
32 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.hardware.rev180313.Hardware;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.hardware.rev180313.hardware.Component;
36 import org.opendaylight.yang.gen.v1.urn.onap.system.rev201026.System1;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.GuicutthroughBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.NetworkElementDeviceType;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
40 import org.opendaylight.yangtools.concepts.ListenerRegistration;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.opendaylight.yangtools.yang.binding.NotificationListener;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 /**
47  */
48 public class ORanNetworkElement implements NetworkElement {
49
50     private static final Logger log = LoggerFactory.getLogger(ORanNetworkElement.class);
51
52     private final NetconfBindingAccessor netconfAccessor;
53
54     private final DataProvider databaseService;
55
56     @SuppressWarnings("unused")
57     private final VESCollectorService vesCollectorService;
58
59     private final ORanToInternalDataModel oRanMapper;
60
61     private ListenerRegistration<NotificationListener> oRanListenerRegistrationResult;
62     private @NonNull final ORanChangeNotificationListener oRanListener;
63     private ListenerRegistration<NotificationListener> oRanFaultListenerRegistrationResult;
64     private @NonNull final ORanFaultNotificationListener oRanFaultListener;
65
66     ORanNetworkElement(NetconfBindingAccessor netconfAccess, DataProvider databaseService,
67             VESCollectorService vesCollectorService) {
68         log.info("Create {}", ORanNetworkElement.class.getSimpleName());
69         this.netconfAccessor = netconfAccess;
70         this.databaseService = databaseService;
71         this.vesCollectorService = vesCollectorService;
72
73         this.oRanListenerRegistrationResult = null;
74         this.oRanListener = new ORanChangeNotificationListener(netconfAccessor, databaseService, vesCollectorService);
75
76         this.oRanFaultListenerRegistrationResult = null;
77         this.oRanFaultListener = new ORanFaultNotificationListener();
78
79         this.oRanMapper = new ORanToInternalDataModel();
80
81     }
82
83     public void initialReadFromNetworkElement() {
84         Hardware hardware = readHardware(netconfAccessor);
85         if (hardware != null) {
86             Collection<Component> componentList = YangHelper.getCollection(hardware.getComponent());
87             if (componentList != null) {
88                 int componentListSize = componentList.size();
89                 int writeCount = 0;
90
91                 for (Component component : componentList) {
92                     if (component.getParent() == null) {
93                         writeCount += writeInventory(component, componentList, 0);
94                     }
95                 }
96                 if (componentListSize != writeCount) {
97                     log.warn("Not all data were written to the Inventory. Potential entries with missing "
98                             + "contained-child. Node Id = {}, Components Found = {}, Entries written to Database = {}",
99                             netconfAccessor.getNodeId().getValue(), componentListSize, writeCount);
100                 }
101             }
102         }
103
104         System1 sys = getOnapSystemData(netconfAccessor);
105         if (sys != null) {
106             GuicutthroughBuilder gcBuilder = new GuicutthroughBuilder();
107             gcBuilder.setId(sys.getName()).setName(sys.getName()).setWeburi(sys.getWebUi().getValue());
108             databaseService.writeGuiCutThroughData(gcBuilder.build());
109         }
110     }
111
112     private int writeInventory(Component component, Collection<Component> componentList, int treeLevel) {
113         databaseService
114                 .writeInventory(oRanMapper.getInternalEquipment(netconfAccessor.getNodeId(), component, treeLevel));
115         int count = 1;
116         if (component.getContainsChild() != null) {
117             List<String> containerHolderList = component.getContainsChild();
118             for (String containerHolder : containerHolderList) {
119                 for (Component c : componentList) {
120                     if (containerHolder.equals(c.getName())) {
121                         count += writeInventory(c, componentList, treeLevel + 1);
122                     }
123                 }
124             }
125         }
126         return count;
127     }
128
129     @Override
130     public NetworkElementDeviceType getDeviceType() {
131         return NetworkElementDeviceType.ORAN;
132     }
133
134     private System1 getOnapSystemData(NetconfBindingAccessor accessData) {
135         InstanceIdentifier<System1> system1IID = InstanceIdentifier
136                 .builder(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.system.rev140806.System.class)
137                 .augmentation(System1.class).build();
138
139         System1 res = accessData.getTransactionUtils().readData(accessData.getDataBroker(),
140                 LogicalDatastoreType.OPERATIONAL, system1IID);
141         log.debug("Result of getOnapSystemData = {}", res);
142         return res;
143     }
144
145     private Hardware readHardware(NetconfBindingAccessor accessData) {
146
147         final Class<Hardware> clazzPac = Hardware.class;
148
149         log.info("DBRead Get equipment for class {} from mountpoint {} for uuid {}", clazzPac.getSimpleName(),
150                 accessData.getNodeId().getValue());
151
152         InstanceIdentifier<Hardware> hardwareIID = InstanceIdentifier.builder(clazzPac).build();
153
154         Hardware res = accessData.getTransactionUtils().readData(accessData.getDataBroker(),
155                 LogicalDatastoreType.OPERATIONAL, hardwareIID);
156
157         return res;
158     }
159
160     @Override
161     public void register() {
162
163         initialReadFromNetworkElement();
164         // Register call back class for receiving notifications
165         Optional<NetconfNotifications> oNotifications = netconfAccessor.getNotificationAccessor();
166         if (oNotifications.isPresent()) {
167             NetconfNotifications notifications = oNotifications.get();
168             this.oRanListenerRegistrationResult = netconfAccessor.doRegisterNotificationListener(oRanListener);
169             this.oRanFaultListenerRegistrationResult =
170                     netconfAccessor.doRegisterNotificationListener(oRanFaultListener);
171             // Register notifications stream
172             if (notifications.isNCNotificationsSupported()) {
173                 List<Stream> streamList = notifications.getNotificationStreams();
174                 notifications.registerNotificationsStream(NetconfBindingAccessor.DefaultNotificationsStream); // Always register first to default stream
175                 notifications.registerNotificationsStream(streamList);
176             } else {
177                 notifications.registerNotificationsStream(NetconfBindingAccessor.DefaultNotificationsStream);
178             }
179         }
180     }
181
182     @Override
183     public void deregister() {
184         if (oRanListenerRegistrationResult != null) {
185             this.oRanListenerRegistrationResult.close();
186         }
187         if (oRanFaultListenerRegistrationResult != null) {
188             this.oRanFaultListenerRegistrationResult.close();
189         } ;
190     }
191
192
193     @Override
194     public NodeId getNodeId() {
195         return netconfAccessor.getNodeId();
196     }
197
198     @Override
199     public <L extends NetworkElementService> Optional<L> getService(Class<L> clazz) {
200         return Optional.empty();
201     }
202
203     @Override
204     public void warmstart() {}
205
206     @Override
207     public Optional<NetconfAccessor> getAcessor() {
208         return Optional.of(netconfAccessor);
209     }
210
211 }