bc82cd11d28053fa1b34f8bf0ebcc98ef1247a89
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2020 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.openroadm.impl;
23
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Hashtable;
27 import java.util.List;
28 import java.util.Optional;
29 import org.eclipse.jdt.annotation.NonNull;
30 import org.onap.ccsdk.features.sdnr.wt.common.YangHelper;
31 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider;
32 import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.service.NetworkElement;
33 import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.service.NetworkElementService;
34 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.DeviceManagerServiceProvider;
35 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfAccessor;
36 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev191129.OrgOpenroadmDevice;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev191129.circuit.packs.CircuitPacks;
39 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev191129.interfaces.grp.Interface;
40 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev191129.org.openroadm.device.Xponder;
41 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev191129.shelf.Slots;
42 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev191129.shelves.Shelves;
43 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev191129.xponder.XpdrPort;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.NetworkElementDeviceType;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.PmdataEntity;
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 /**
54  * @author Shabnam Sultana
55  *
56  *         Creating the openroadm device as an optical network element and writing inventory, fault, pm data to elastic
57  *         search db
58  *
59  **/
60 public class OpenroadmNetworkElement implements NetworkElement {
61
62     // variables
63     private final long equipmentLevel = 0;
64     private static final Logger log = LoggerFactory.getLogger(OpenroadmNetworkElement.class);
65     private final NetconfAccessor netconfAccessor;
66     private final DataProvider databaseService;
67     private Hashtable<String, Long> circuitPacksRecord;
68     private Hashtable<String, Long> shelfProvisionedcircuitPacks;
69     private ListenerRegistration<NotificationListener> openRdmListenerRegistrationResult;
70     private @NonNull final OpenroadmChangeNotificationListener openRdmListener;
71     private ListenerRegistration<NotificationListener> opnRdmFaultListenerRegistrationResult;
72     private @NonNull OpenroadmFaultNotificationListener opnRdmFaultListener;
73     private ListenerRegistration<NotificationListener> opnRdmDeviceListenerRegistrationResult;
74     private OpenroadmDeviceChangeNotificationListener opnRdmDeviceListener;
75     private OpenroadmInventoryInput opnRdmInventoryInput;
76     private PmDataBuilderOpenRoadm openRoadmPmData;
77     private InitialDeviceAlarmReader initialAlarmReader;
78     private List<PmdataEntity> pmDataEntity = new ArrayList<PmdataEntity>();
79     // end of variables
80
81     // constructors
82     public OpenroadmNetworkElement(NetconfAccessor netconfAccess, DeviceManagerServiceProvider serviceProvider) {
83
84         log.info("Create {}", OpenroadmNetworkElement.class.getSimpleName());
85         this.netconfAccessor = netconfAccess;
86         this.databaseService = serviceProvider.getDataProvider();
87         this.openRdmListenerRegistrationResult = null;
88         this.openRdmListener = new OpenroadmChangeNotificationListener(netconfAccessor, databaseService);
89         this.opnRdmFaultListenerRegistrationResult = null;
90         this.opnRdmFaultListener = new OpenroadmFaultNotificationListener(netconfAccessor, serviceProvider);
91         this.opnRdmDeviceListenerRegistrationResult = null;
92         this.opnRdmDeviceListener = new OpenroadmDeviceChangeNotificationListener(netconfAccessor, databaseService);
93         this.opnRdmInventoryInput = new OpenroadmInventoryInput(netconfAccess, readDevice(netconfAccess));
94         this.circuitPacksRecord = new Hashtable<String, Long>();
95         this.shelfProvisionedcircuitPacks = new Hashtable<String, Long>();
96         this.openRoadmPmData = new PmDataBuilderOpenRoadm(this.netconfAccessor);
97         this.initialAlarmReader = new InitialDeviceAlarmReader(this.netconfAccessor, serviceProvider);
98         log.info("NodeId {}", this.netconfAccessor.getNodeId().getValue());
99         log.info("oScaMapper details{}", this.opnRdmInventoryInput.getClass().getName());
100
101     }
102     // end of constructors
103
104     // public methods
105     public void initialReadFromNetworkElement() {
106         OrgOpenroadmDevice device = readDevice(this.netconfAccessor);
107         databaseService.writeInventory(this.opnRdmInventoryInput.getInventoryData(equipmentLevel));
108
109         readShelvesData(device);
110         readXpndrData(device);
111         readCircuitPacketData(device);
112         readInterfaceData(device);
113         // Writing initial alarms at the time of device registration
114         initialAlarmReader.faultService();
115         pmDataEntity = this.openRoadmPmData.buildPmDataEntity(this.openRoadmPmData.getPmData(this.netconfAccessor));
116         if (!pmDataEntity.isEmpty()) {
117             this.databaseService.doWritePerformanceData(pmDataEntity);
118             log.info("PmDatEntity is written with size {}", pmDataEntity.size());
119             for (PmdataEntity ent : pmDataEntity) {
120                 log.info("GetNode: {}, granPeriod: {}", ent.getNodeName(), ent.getGranularityPeriod().getName());
121             }
122         } else {
123             log.info("PmDatEntity is empty");
124         }
125
126     }
127
128     @Override
129     public NetworkElementDeviceType getDeviceType() {
130         return NetworkElementDeviceType.OROADM;
131     }
132
133     @Override
134     public void register() {
135         initialReadFromNetworkElement();
136
137         this.openRdmListenerRegistrationResult = netconfAccessor.doRegisterNotificationListener(openRdmListener);
138         this.opnRdmFaultListenerRegistrationResult =
139                 netconfAccessor.doRegisterNotificationListener(opnRdmFaultListener);
140         this.opnRdmDeviceListenerRegistrationResult =
141                 netconfAccessor.doRegisterNotificationListener(opnRdmDeviceListener);
142         // Register netconf stream
143         netconfAccessor.registerNotificationsStream(NetconfAccessor.DefaultNotificationsStream);
144
145     }
146
147     @Override
148     public void deregister() {
149         if (openRdmListenerRegistrationResult != null) {
150             this.openRdmListenerRegistrationResult.close();
151         }
152         if (opnRdmFaultListenerRegistrationResult != null) {
153             this.opnRdmFaultListenerRegistrationResult.close();
154         }
155         if (opnRdmDeviceListenerRegistrationResult != null) {
156             this.opnRdmDeviceListenerRegistrationResult.close();
157         }
158     }
159
160     @Override
161     public NodeId getNodeId() {
162         return netconfAccessor.getNodeId();
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(netconfAccessor);
176     }
177     // end of public methods
178
179     // private methods
180     private void readShelvesData(OrgOpenroadmDevice device) {
181         Collection<Shelves> shelves = YangHelper.getCollection(device.getShelves());
182         if (shelves != null) {
183             for (Shelves shelf : shelves) {
184                 log.info(
185                         "Shelf Name: {}, \n Serial Id:{}, \n Product Code;{}, \n Position:{}, \n EquipmetState: {}, \n Hardware version: {}"
186                                 + "\n ShelfType:{}, \n Vendor: {}, \n LifecycleState: {} ",
187                         shelf.getShelfName(), shelf.getSerialId(), shelf.getProductCode(), shelf.getShelfPosition(),
188                         shelf.getEquipmentState(), shelf.getHardwareVersion(), shelf.getShelfType(), shelf.getVendor(),
189                         shelf.getLifecycleState());
190                 databaseService
191                         .writeInventory(this.opnRdmInventoryInput.getShelvesInventory(shelf, equipmentLevel + 1));
192                 Collection<Slots> slotList = YangHelper.getCollection(shelf.getSlots());
193                 if (slotList != null) {
194                     for (Slots slot : slotList) {
195                         if (!slot.getProvisionedCircuitPack().isEmpty()) {
196                             this.shelfProvisionedcircuitPacks.put(slot.getProvisionedCircuitPack(), equipmentLevel + 2);
197                         }
198                         log.info("Slots for the shelf: {}", shelf.getShelfName());
199                         log.info("\n Slot Name: {}, \n Status: {}, \n Slot label: {} ", slot.getSlotName(),
200                                 slot.getSlotStatus(), slot.getLabel());
201                     }
202                 }
203                 log.info("size of shelfProvisionedcircuitPacks: {} ", shelfProvisionedcircuitPacks.size());
204
205             }
206
207         }
208     }
209
210     private void readXpndrData(OrgOpenroadmDevice device) {
211         Collection<Xponder> xponderList = YangHelper.getCollection(device.getXponder());
212         if (xponderList != null) {
213             for (Xponder xponder : xponderList) {
214                 databaseService
215                         .writeInventory(this.opnRdmInventoryInput.getXponderInventory(xponder, equipmentLevel + 1));
216                 log.info("Xponders: No.: {} , \n Port: {} ,\n Type: {}", xponder.getXpdrNumber(), xponder.getXpdrPort(),
217                         xponder.getXpdrType());
218                 Collection<XpdrPort> xpdrportlist = YangHelper.getCollection(xponder.getXpdrPort());
219                 if (xpdrportlist != null) {
220                     for (XpdrPort xpdrport : xpdrportlist)
221                         if (!xpdrport.getCircuitPackName().isEmpty()) {
222                             this.shelfProvisionedcircuitPacks.put(xpdrport.getCircuitPackName(), equipmentLevel + 2);
223                             log.info("Size of dict{}", this.shelfProvisionedcircuitPacks.size());
224                         }
225
226
227                 }
228             }
229
230         }
231     }
232
233     private void readCircuitPacketData(OrgOpenroadmDevice device) {
234         Collection<CircuitPacks> circuitpacklist = YangHelper.getCollection(device.getCircuitPacks());
235
236         if (circuitpacklist != null) {
237             for (CircuitPacks cp : circuitpacklist) {
238                 log.info("CP Name:{}", cp.getCircuitPackName());
239                 if (!this.shelfProvisionedcircuitPacks.isEmpty()
240                         && this.shelfProvisionedcircuitPacks.containsKey(cp.getCircuitPackName())) {
241
242                     this.circuitPacksRecord.put(cp.getCircuitPackName(), (equipmentLevel + 2));
243                     databaseService.writeInventory(this.opnRdmInventoryInput.getCircuitPackInventory(cp,
244                             this.shelfProvisionedcircuitPacks.get(cp.getCircuitPackName())));
245                     log.info("shelf has circuit pack");
246                 } else {
247                     if (cp.getParentCircuitPack() == null) {
248                         this.circuitPacksRecord.put(cp.getCircuitPackName(), (equipmentLevel + 1));
249                         databaseService.writeInventory(
250                                 this.opnRdmInventoryInput.getCircuitPackInventory(cp, equipmentLevel + 1));
251                         log.info("Cp has no parent circuit pack and no shelf");
252
253                     } else {
254                         if (this.shelfProvisionedcircuitPacks
255                                 .containsKey(cp.getParentCircuitPack().getCircuitPackName())) {
256                             this.circuitPacksRecord.put(cp.getCircuitPackName(), (equipmentLevel + 3));
257                             databaseService.writeInventory(
258                                     this.opnRdmInventoryInput.getCircuitPackInventory(cp, equipmentLevel + 3));
259                             log.info("Cp {} has parent circuit pack and shelf", cp.getCircuitPackName());
260                         } else {
261                             this.circuitPacksRecord.put(cp.getCircuitPackName(), (equipmentLevel + 2));
262                             databaseService.writeInventory(
263                                     this.opnRdmInventoryInput.getCircuitPackInventory(cp, equipmentLevel + 2));
264                             log.info("Cp {} has parent circuit pack but no shelf", cp.getCircuitPackName());
265
266                         }
267
268
269                     }
270                 }
271
272
273             }
274
275         }
276
277     }
278
279     private void readInterfaceData(OrgOpenroadmDevice device) {
280         Collection<Interface> interfaceList = YangHelper.getCollection(device.getInterface());
281         if (interfaceList != null) {
282             for (Interface deviceInterface : interfaceList) {
283
284                 log.info("\n InterfaceName: {}", deviceInterface.getName());
285                 log.info("Supporting CP {}", this.circuitPacksRecord.size());
286                 for (String s : this.circuitPacksRecord.keySet()) {
287                     log.info("{} value {}", s, this.circuitPacksRecord.get(s));
288                 }
289                 log.info("Interface {} and their supporting CP {}", deviceInterface.getName(),
290                         deviceInterface.getSupportingCircuitPackName());
291
292
293                 if (deviceInterface.getSupportingCircuitPackName() != null) {
294                     if (this.circuitPacksRecord.containsKey(deviceInterface.getSupportingCircuitPackName())) {
295                         databaseService.writeInventory(this.opnRdmInventoryInput.getInterfacesInventory(deviceInterface,
296                                 this.circuitPacksRecord.get(deviceInterface.getSupportingCircuitPackName()) + 1));
297                     }
298
299                 } else {
300                     databaseService.writeInventory(
301                             this.opnRdmInventoryInput.getInterfacesInventory(deviceInterface, equipmentLevel + 1));
302                 }
303             }
304         }
305
306     }
307
308     private OrgOpenroadmDevice readDevice(NetconfAccessor accessor) {
309
310         final Class<OrgOpenroadmDevice> openRoadmDev = OrgOpenroadmDevice.class;
311         InstanceIdentifier<OrgOpenroadmDevice> deviceId = InstanceIdentifier.builder(openRoadmDev).build();
312
313         OrgOpenroadmDevice device = accessor.getTransactionUtils().readData(accessor.getDataBroker(),
314                 LogicalDatastoreType.OPERATIONAL, deviceId);
315
316         return device;
317
318     }
319     // end of private methods
320
321
322 }