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