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