02f8547aea6f93ad67ec9c9d1e440606e744f48d
[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 = 1;
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(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         this.opnRdmInventoryInput = new OpenroadmInventoryInput(this.netconfAccessor, device);
112         log.info("openroadmMapper details{}", this.opnRdmInventoryInput.getClass().getName());
113         List<Inventory> inventoryList = new ArrayList<>();
114         inventoryList.add(this.opnRdmInventoryInput.getInventoryData(Uint32.valueOf(equipmentLevel)));
115         readShelvesData(inventoryList, device);
116         readXpndrData(inventoryList, device);
117         readCircuitPacketData(inventoryList, device);
118         readInterfaceData(inventoryList, device);
119         this.databaseService.writeInventory(this.netconfAccessor.getNodeId().getValue(), inventoryList);
120         // Writing initial alarms at the time of device registration
121         initialAlarmReader.faultService();
122         //        Writing historical PM data at the time of device registration
123         List<PmdataEntity> pmDataEntity = new ArrayList<>();
124         pmDataEntity = this.openRoadmPmData.buildPmDataEntity(this.openRoadmPmData.getPmData(this.netconfAccessor));
125         if (!pmDataEntity.isEmpty()) {
126             this.databaseService.doWritePerformanceData(pmDataEntity);
127             log.info("PmDatEntity is written with size {}", pmDataEntity.size());
128             for (PmdataEntity ent : pmDataEntity) {
129                 log.info("GetNode: {}, granPeriod: {}", ent.getNodeName(), ent.getGranularityPeriod().getName());
130             }
131         } else {
132             log.info("PmDatEntity is empty");
133         }
134     }
135
136
137     @Override
138     public void register() {
139         initialReadFromNetworkElement();
140
141         this.openRdmListenerRegistrationResult = netconfAccessor.doRegisterNotificationListener(openRdmListener);
142         this.opnRdmFaultListenerRegistrationResult =
143                 netconfAccessor.doRegisterNotificationListener(opnRdmFaultListener);
144         this.opnRdmDeviceListenerRegistrationResult =
145                 netconfAccessor.doRegisterNotificationListener(opnRdmDeviceListener);
146         // Register netconf stream
147         netconfAccessor.registerNotificationsStream(NetconfAccessor.DefaultNotificationsStream);
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     // end of public methods
164
165     // private methods
166     private void readShelvesData(List<Inventory> inventoryList, OrgOpenroadmDevice device) {
167         Collection<Shelves> shelves = YangHelper.getCollection(device.getShelves());
168         if (shelves != null) {
169             for (Shelves shelf : shelves) {
170                 log.info(
171                         "Shelf Name: {}, \n Serial Id:{}, \n Product Code;{}, \n Position:{}, \n EquipmetState: {}, \n Hardware version: {}"
172                                 + "\n ShelfType:{}, \n Vendor: {}, \n LifecycleState: {} ",
173                         shelf.getShelfName(), shelf.getSerialId(), shelf.getProductCode(), shelf.getShelfPosition(),
174                         shelf.getEquipmentState(), shelf.getHardwareVersion(), shelf.getShelfType(), shelf.getVendor(),
175                         shelf.getLifecycleState());
176                 inventoryList
177                         .add(this.opnRdmInventoryInput.getShelvesInventory(shelf, Uint32.valueOf(equipmentLevel + 1)));
178                 Collection<Slots> slotList = YangHelper.getCollection(shelf.getSlots());
179                 if (slotList != null) {
180                     for (Slots slot : slotList) {
181                         if (slot.getProvisionedCircuitPack() != null) {
182                             this.shelfProvisionedcircuitPacks.put(slot.getProvisionedCircuitPack(), equipmentLevel + 2);
183                         }
184                         log.info("Slots for the shelf: {}", shelf.getShelfName());
185                         log.info("\n Slot Name: {}, \n Status: {}, \n Slot label: {} ", slot.getSlotName(),
186                                 slot.getSlotStatus(), slot.getLabel());
187                     }
188                 }
189             }
190             log.info("size of shelfProvisionedcircuitPacks: {} ", shelfProvisionedcircuitPacks.size());
191         }
192
193     }
194
195     private void readXpndrData(List<Inventory> inventoryList, OrgOpenroadmDevice device) {
196         Collection<Xponder> xponderList = YangHelper.getCollection(device.getXponder());
197
198         if (xponderList != null) {
199             for (Xponder xponder : xponderList) {
200
201                 inventoryList.add(
202                         this.opnRdmInventoryInput.getXponderInventory(xponder, Uint32.valueOf(equipmentLevel + 1)));
203                 log.info("Xponders: No.: {} , \n Port: {} ,\n Type: {}", xponder.getXpdrNumber(), xponder.getXpdrPort(),
204                         xponder.getXpdrType());
205                 Collection<XpdrPort> xpdrportlist = YangHelper.getCollection(xponder.getXpdrPort());
206                 if (xpdrportlist != null) {
207                     for (XpdrPort xpdrport : xpdrportlist)
208                         if (xpdrport.getCircuitPackName() != null) {
209                             this.shelfProvisionedcircuitPacks.put(xpdrport.getCircuitPackName(), equipmentLevel + 2);
210                             log.info("Size of dict{}", this.shelfProvisionedcircuitPacks.size());
211                         }
212                 }
213
214             }
215         }
216     }
217
218     private void readCircuitPacketData(List<Inventory> inventoryList, OrgOpenroadmDevice device) {
219         Collection<CircuitPacks> circuitpackCollection = YangHelper.getCollection(device.getCircuitPacks());
220         List<String> cpNameList = new ArrayList<>();
221
222         if (circuitpackCollection != null) {
223             //            collect all circuit pack names. Required to check for invalid parents later on
224             for (CircuitPacks cp : circuitpackCollection) {
225                 cpNameList.add(cp.getCircuitPackName());
226             }
227
228             for (CircuitPacks cp : circuitpackCollection) {
229                 log.info("CP Name:{}", cp.getCircuitPackName());
230
231                 if (cp.getParentCircuitPack() == null
232                         && !this.shelfProvisionedcircuitPacks.containsKey(cp.getCircuitPackName())) {
233                     log.info("cp has no parent and no shelf");
234                     this.circuitPacksRecord.put(cp.getCircuitPackName(), (equipmentLevel + 1));
235                     inventoryList.add(
236                             this.opnRdmInventoryInput.getCircuitPackInventory(cp, Uint32.valueOf(equipmentLevel + 1)));
237                 } else {
238                     //                check for missing valid parent circuit name
239                     if (cp.getParentCircuitPack().getCpSlotName() != null
240                             && cp.getParentCircuitPack().getCircuitPackName() == null) {
241
242                         log.info("Cp {} has slotname of the parent circuit pack  but no parent circuit pack name",
243                                 cp.getCircuitPackName());
244                         this.circuitPacksRecord.put(cp.getCircuitPackName(), (equipmentLevel + 3));
245                         inventoryList.add(this.opnRdmInventoryInput.getCircuitPackInventory(cp,
246                                 Uint32.valueOf(equipmentLevel + 3)));
247                         databaseService.writeEventLog(writeIncorrectParentLog(cp.getCircuitPackName(), counter)
248                                 .setObjectId(device.getInfo().getNodeId().getValue())
249                                 .setId(cp.getParentCircuitPack().getCpSlotName())
250                                 .setNewValue("Missing parent circuit pack name").build());
251                     } else if (cp.getParentCircuitPack().getCircuitPackName() != null
252                             && this.shelfProvisionedcircuitPacks
253                                     .containsKey(cp.getParentCircuitPack().getCircuitPackName())) {
254                         log.info("Cp {} has parent circuit pack and shelf", cp.getCircuitPackName());
255                         this.circuitPacksRecord.put(cp.getCircuitPackName(), (equipmentLevel + 3));
256                         inventoryList.add(this.opnRdmInventoryInput.getCircuitPackInventory(cp,
257                                 Uint32.valueOf(equipmentLevel + 3)));
258                     } else {
259                         //                      check for incorrect hierarchy
260                         if (cp.getParentCircuitPack().getCircuitPackName() != null
261                                 && !cpNameList.contains(cp.getParentCircuitPack().getCircuitPackName())) {
262                             databaseService.writeEventLog(writeIncorrectParentLog(cp.getCircuitPackName(), counter)
263                                     .setObjectId(device.getInfo().getNodeId().getValue())
264                                     .setId(cp.getParentCircuitPack().getCpSlotName()).build());
265                         }
266
267                         log.info("Cp has parent circuit pack but no shelf or a shelf but no parent circuit pack");
268                         this.circuitPacksRecord.put(cp.getCircuitPackName(), (equipmentLevel + 2));
269                         inventoryList.add(this.opnRdmInventoryInput.getCircuitPackInventory(cp,
270                                 Uint32.valueOf(equipmentLevel + 2)));
271                     }
272
273                 }
274             }
275
276         }
277     }
278
279     private void readInterfaceData(List<Inventory> inventoryList, OrgOpenroadmDevice device) {
280         Collection<Interface> interfaceList = YangHelper.getCollection(device.getInterface());
281         if (interfaceList != null) {
282             for (Interface deviceInterface : interfaceList) {
283                 log.info("\n InterfaceName: {}", deviceInterface.getName());
284                 log.info("Supporting CP {}", this.circuitPacksRecord.size());
285                 for (String s : this.circuitPacksRecord.keySet()) {
286                     log.info("{} value {}", s, this.circuitPacksRecord.get(s));
287                 }
288                 log.info("Interface {} and their supporting CP {}", deviceInterface.getName(),
289                         deviceInterface.getSupportingCircuitPackName());
290                 if (deviceInterface.getSupportingCircuitPackName() != null) {
291                     if (this.circuitPacksRecord.containsKey(deviceInterface.getSupportingCircuitPackName())) {
292                         inventoryList.add(this.opnRdmInventoryInput.getInterfacesInventory(deviceInterface,
293                                 Uint32.valueOf(
294                                         this.circuitPacksRecord.get(deviceInterface.getSupportingCircuitPackName())
295                                                 + 1)));
296                     }
297                 } else {
298                     inventoryList.add(this.opnRdmInventoryInput.getInterfacesInventory(deviceInterface,
299                             Uint32.valueOf(equipmentLevel + 1)));
300                 }
301             }
302         }
303     }
304
305     private OrgOpenroadmDevice readDevice(NetconfBindingAccessor accessor) {
306         final Class<OrgOpenroadmDevice> openRoadmDev = OrgOpenroadmDevice.class;
307         InstanceIdentifier<OrgOpenroadmDevice> deviceId = InstanceIdentifier.builder(openRoadmDev).build();
308         return accessor.getTransactionUtils().readData(accessor.getDataBroker(), LogicalDatastoreType.OPERATIONAL,
309                 deviceId);
310     }
311
312     private EventlogBuilder writeIncorrectParentLog(String attributeName, Integer counter) {
313         EventlogBuilder eventlogBuilder = new EventlogBuilder();
314         eventlogBuilder.setAttributeName(attributeName).setCounter(counter)
315                 .setNodeId(this.netconfAccessor.getNodeId().getValue()).setSourceType(SourceType.Netconf)
316                 .setNewValue("Invalid parent circuit-pack name")
317                 .setTimestamp(new DateAndTime(ncTimeConverter.getTimeStamp()));
318
319         return eventlogBuilder;
320
321     }
322     // end of private methods
323 }