5fa4384973e2def9c4cc55134f740143867e2eb1
[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         pmDataEntity = this.openRoadmPmData.buildPmDataEntity(this.openRoadmPmData.getPmData(this.netconfAccessor));
114         if (!pmDataEntity.isEmpty()) {
115             this.databaseService.doWritePerformanceData(pmDataEntity);
116             log.info("PmDatEntity is written with size {}", pmDataEntity.size());
117             for (PmdataEntity ent : pmDataEntity) {
118                 log.info("GetNode: {}, granPeriod: {}", ent.getNodeName(), ent.getGranularityPeriod().getName());
119             }
120         } else {
121             log.info("PmDatEntity is empty");
122         }
123
124     }
125
126     @Override
127     public NetworkElementDeviceType getDeviceType() {
128         return NetworkElementDeviceType.OROADM;
129     }
130
131     @Override
132     public void register() {
133         initialReadFromNetworkElement();
134
135         this.openRdmListenerRegistrationResult = netconfAccessor.doRegisterNotificationListener(openRdmListener);
136         this.opnRdmFaultListenerRegistrationResult =
137                 netconfAccessor.doRegisterNotificationListener(opnRdmFaultListener);
138         this.opnRdmDeviceListenerRegistrationResult =
139                 netconfAccessor.doRegisterNotificationListener(opnRdmDeviceListener);
140         // Register netconf stream
141         netconfAccessor.registerNotificationsStream(NetconfAccessor.DefaultNotificationsStream);
142
143     }
144
145     @Override
146     public void deregister() {
147         if (openRdmListenerRegistrationResult != null) {
148             this.openRdmListenerRegistrationResult.close();
149         }
150         if (opnRdmFaultListenerRegistrationResult != null) {
151             this.opnRdmFaultListenerRegistrationResult.close();
152         }
153         if (opnRdmDeviceListenerRegistrationResult != null) {
154             this.opnRdmDeviceListenerRegistrationResult.close();
155         }
156     }
157
158     @Override
159     public NodeId getNodeId() {
160         return netconfAccessor.getNodeId();
161     }
162
163     @Override
164     public <L extends NetworkElementService> Optional<L> getService(Class<L> clazz) {
165         return Optional.empty();
166     }
167
168     @Override
169     public void warmstart() {}
170
171     @Override
172     public Optional<NetconfAccessor> getAcessor() {
173         return Optional.of(netconfAccessor);
174     }
175     // end of public methods
176
177     // private methods
178     private void readShelvesData(OrgOpenroadmDevice device) {
179         List<Shelves> shelves = device.getShelves();
180         if (shelves != null) {
181             for (Shelves shelf : shelves) {
182                 log.info(
183                         "Shelf Name: {}, \n Serial Id:{}, \n Product Code;{}, \n Position:{}, \n EquipmetState: {}, \n Hardware version: {}"
184                                 + "\n ShelfType:{}, \n Vendor: {}, \n LifecycleState: {} ",
185                         shelf.getShelfName(), shelf.getSerialId(), shelf.getProductCode(), shelf.getShelfPosition(),
186                         shelf.getEquipmentState(), shelf.getHardwareVersion(), shelf.getShelfType(), shelf.getVendor(),
187                         shelf.getLifecycleState());
188                 databaseService
189                         .writeInventory(this.opnRdmInventoryInput.getShelvesInventory(shelf, equipmentLevel + 1));
190                 List<Slots> slotList = shelf.getSlots();
191                 if (slotList != null) {
192                     for (Slots slot : slotList) {
193                         if (!slot.getProvisionedCircuitPack().isEmpty()) {
194                             this.shelfProvisionedcircuitPacks.put(slot.getProvisionedCircuitPack(), equipmentLevel + 2);
195                         }
196                         log.info("Slots for the shelf: {}", shelf.getShelfName());
197                         log.info("\n Slot Name: {}, \n Status: {}, \n Slot label: {} ", slot.getSlotName(),
198                                 slot.getSlotStatus(), slot.getLabel());
199                     }
200                 }
201                 log.info("size of shelfProvisionedcircuitPacks: {} ", shelfProvisionedcircuitPacks.size());
202
203             }
204
205         }
206     }
207
208     private void readXpndrData(OrgOpenroadmDevice device) {
209         List<Xponder> xponderList = device.getXponder();
210         if (xponderList != null) {
211             for (Xponder xponder : xponderList) {
212                 databaseService
213                         .writeInventory(this.opnRdmInventoryInput.getXponderInventory(xponder, equipmentLevel + 1));
214                 log.info("Xponders: No.: {} , \n Port: {} ,\n Type: {}", xponder.getXpdrNumber(), xponder.getXpdrPort(),
215                         xponder.getXpdrType());
216                 List<XpdrPort> xpdrportlist = xponder.getXpdrPort();
217                 if (xpdrportlist != null) {
218                     for (XpdrPort xpdrport : xpdrportlist)
219                         if (!xpdrport.getCircuitPackName().isEmpty()) {
220                             this.shelfProvisionedcircuitPacks.put(xpdrport.getCircuitPackName(), equipmentLevel + 2);
221                             log.info("Size of dict{}", this.shelfProvisionedcircuitPacks.size());
222                         }
223
224
225                 }
226             }
227
228         }
229     }
230
231     private void readCircuitPacketData(OrgOpenroadmDevice device) {
232         List<CircuitPacks> circuitpacklist = device.getCircuitPacks();
233
234         if (circuitpacklist != null) {
235             for (CircuitPacks cp : circuitpacklist) {
236                 log.info("CP Name:{}", cp.getCircuitPackName());
237                 if (!this.shelfProvisionedcircuitPacks.isEmpty()
238                         && this.shelfProvisionedcircuitPacks.containsKey(cp.getCircuitPackName())) {
239
240                     this.circuitPacksRecord.put(cp.getCircuitPackName(), (equipmentLevel + 2));
241                     databaseService.writeInventory(this.opnRdmInventoryInput.getCircuitPackInventory(cp,
242                             this.shelfProvisionedcircuitPacks.get(cp.getCircuitPackName())));
243                     log.info("shelf has circuit pack");
244                 } else {
245                     if (cp.getParentCircuitPack() == null) {
246                         this.circuitPacksRecord.put(cp.getCircuitPackName(), (equipmentLevel + 1));
247                         databaseService.writeInventory(
248                                 this.opnRdmInventoryInput.getCircuitPackInventory(cp, equipmentLevel + 1));
249                         log.info("Cp has no parent circuit pack and no shelf");
250
251                     } else {
252                         if (this.shelfProvisionedcircuitPacks
253                                 .containsKey(cp.getParentCircuitPack().getCircuitPackName())) {
254                             this.circuitPacksRecord.put(cp.getCircuitPackName(), (equipmentLevel + 3));
255                             databaseService.writeInventory(
256                                     this.opnRdmInventoryInput.getCircuitPackInventory(cp, equipmentLevel + 3));
257                             log.info("Cp {} has parent circuit pack and shelf", cp.getCircuitPackName());
258                         } else {
259                             this.circuitPacksRecord.put(cp.getCircuitPackName(), (equipmentLevel + 2));
260                             databaseService.writeInventory(
261                                     this.opnRdmInventoryInput.getCircuitPackInventory(cp, equipmentLevel + 2));
262                             log.info("Cp {} has parent circuit pack but no shelf", cp.getCircuitPackName());
263
264                         }
265
266
267                     }
268                 }
269
270
271             }
272
273         }
274
275     }
276
277     private void readInterfaceData(OrgOpenroadmDevice device) {
278         List<Interface> interfaceList = device.getInterface();
279         if (interfaceList != null) {
280             for (Interface deviceInterface : interfaceList) {
281
282                 log.info("\n InterfaceName: {}", deviceInterface.getName());
283                 log.info("Supporting CP {}", this.circuitPacksRecord.size());
284                 for (String s : this.circuitPacksRecord.keySet()) {
285                     log.info("{} value {}", s, this.circuitPacksRecord.get(s));
286                 }
287                 log.info("Interface {} and their supporting CP {}", deviceInterface.getName(),
288                         deviceInterface.getSupportingCircuitPackName());
289
290
291                 if (deviceInterface.getSupportingCircuitPackName() != null) {
292                     if (this.circuitPacksRecord.containsKey(deviceInterface.getSupportingCircuitPackName())) {
293                         databaseService.writeInventory(this.opnRdmInventoryInput.getInterfacesInventory(deviceInterface,
294                                 this.circuitPacksRecord.get(deviceInterface.getSupportingCircuitPackName()) + 1));
295                     }
296
297                 } else {
298                     databaseService.writeInventory(
299                             this.opnRdmInventoryInput.getInterfacesInventory(deviceInterface, equipmentLevel + 1));
300                 }
301             }
302         }
303
304     }
305
306     private OrgOpenroadmDevice readDevice(NetconfAccessor accessor) {
307
308         final Class<OrgOpenroadmDevice> openRoadmDev = OrgOpenroadmDevice.class;
309         InstanceIdentifier<OrgOpenroadmDevice> deviceId = InstanceIdentifier.builder(openRoadmDev).build();
310
311         OrgOpenroadmDevice device = accessor.getTransactionUtils().readData(accessor.getDataBroker(),
312                 LogicalDatastoreType.OPERATIONAL, deviceId);
313
314         return device;
315
316     }
317     // end of private methods
318
319
320 }