7b45c3e42750a5b930651f000437fad237addecb
[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
30 import org.eclipse.jdt.annotation.NonNull;
31 import org.onap.ccsdk.features.sdnr.wt.common.YangHelper;
32 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.NetconfTimeStamp;
33 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.NetconfTimeStampImpl;
34 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.DeviceManagerServiceProvider;
35 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfAccessor;
36 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfBindingAccessor;
37 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfNotifications;
38 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
39 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev191129.OrgOpenroadmDevice;
40 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev191129.circuit.packs.CircuitPacks;
41 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev191129.interfaces.grp.Interface;
42 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev191129.org.openroadm.device.Xponder;
43 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev191129.shelf.Slots;
44 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev191129.shelves.Shelves;
45 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev191129.xponder.XpdrPort;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.EventlogBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.PmdataEntity;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SourceType;
50 import org.opendaylight.yangtools.concepts.ListenerRegistration;
51 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
52 import org.opendaylight.yangtools.yang.binding.NotificationListener;
53 import org.opendaylight.yangtools.yang.common.Uint32;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57 /**
58  * @author Shabnam Sultana
59  *
60  *         Creating the openroadm device as an optical network element and writing inventory, fault, pm data to elastic
61  *         search db
62  *
63  **/
64 public class OpenroadmNetworkElement extends OpenroadmNetworkElementBase {
65
66     // variables
67     private final long equipmentLevel = 1;
68     private static final Logger log = LoggerFactory.getLogger(OpenroadmNetworkElement.class);
69     private Hashtable<String, Long> circuitPacksRecord;
70     private Hashtable<String, Long> shelfProvisionedcircuitPacks;
71     private ListenerRegistration<NotificationListener> openRdmListenerRegistrationResult;
72     private @NonNull final OpenroadmChangeNotificationListener openRdmListener;
73     private ListenerRegistration<NotificationListener> opnRdmFaultListenerRegistrationResult;
74     private @NonNull OpenroadmFaultNotificationListener opnRdmFaultListener;
75     private ListenerRegistration<NotificationListener> opnRdmDeviceListenerRegistrationResult;
76     private OpenroadmDeviceChangeNotificationListener opnRdmDeviceListener;
77     private OpenroadmInventoryInput opnRdmInventoryInput;
78     private PmDataBuilderOpenRoadm openRoadmPmData;
79     private InitialDeviceAlarmReader initialAlarmReader;
80     private List<PmdataEntity> pmDataEntity = new ArrayList<PmdataEntity>();
81     private Optional<NetconfNotifications> notifications;
82     private static final NetconfTimeStamp ncTimeConverter = NetconfTimeStampImpl.getConverter();
83     private int counter = 1;
84     // end of variables
85
86     // constructors
87     public OpenroadmNetworkElement(NetconfBindingAccessor netconfAccess, DeviceManagerServiceProvider serviceProvider) {
88
89         super(netconfAccess, serviceProvider);
90         
91         this.notifications = netconfAccess.getNotificationAccessor();
92
93         log.info("Create {}", OpenroadmNetworkElement.class.getSimpleName());
94         this.openRdmListenerRegistrationResult = null;
95         this.openRdmListener = new OpenroadmChangeNotificationListener(netconfAccessor, databaseService);
96         this.opnRdmFaultListenerRegistrationResult = null;
97         this.opnRdmFaultListener = new OpenroadmFaultNotificationListener(netconfAccessor, serviceProvider);
98         this.opnRdmDeviceListenerRegistrationResult = null;
99         this.opnRdmDeviceListener = new OpenroadmDeviceChangeNotificationListener(netconfAccessor, databaseService);
100         this.circuitPacksRecord = new Hashtable<String, Long>();
101         this.shelfProvisionedcircuitPacks = new Hashtable<String, Long>();
102         this.openRoadmPmData = new PmDataBuilderOpenRoadm(this.netconfAccessor);
103         this.initialAlarmReader = new InitialDeviceAlarmReader(this.netconfAccessor, serviceProvider);
104         log.info("NodeId {}", this.netconfAccessor.getNodeId().getValue());
105
106
107     }
108     // end of constructors
109
110     // public methods
111     public void initialReadFromNetworkElement() {
112
113         OrgOpenroadmDevice device = readDevice(this.netconfAccessor);
114         this.opnRdmInventoryInput = new OpenroadmInventoryInput(this.netconfAccessor, device);
115         log.info("oScaMapper details{}", this.opnRdmInventoryInput.getClass().getName());
116         databaseService.writeInventory(this.opnRdmInventoryInput.getInventoryData(Uint32.valueOf(equipmentLevel)));
117
118         readShelvesData(device);
119         readXpndrData(device);
120         readCircuitPacketData(device);
121         readInterfaceData(device);
122         // Writing initial alarms at the time of device registration
123         initialAlarmReader.faultService();
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         notifications.get().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(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                 databaseService.writeInventory(
177                         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(OrgOpenroadmDevice device) {
196         Collection<Xponder> xponderList = YangHelper.getCollection(device.getXponder());
197
198         if (xponderList != null) {
199             for (Xponder xponder : xponderList) {
200
201                 databaseService.writeInventory(
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(OrgOpenroadmDevice device) {
219         Collection<CircuitPacks> circuitpacklist = YangHelper.getCollection(device.getCircuitPacks());
220         List<String> cpNames = new ArrayList<String>();
221
222         if (circuitpacklist != null) {
223             for (CircuitPacks cp : circuitpacklist) {
224                 cpNames.add(cp.getCircuitPackName());
225
226                 log.info("CP Name:{}", cp.getCircuitPackName());
227
228                 if (cp.getParentCircuitPack() == null
229                         && !this.shelfProvisionedcircuitPacks.containsKey(cp.getCircuitPackName())) {
230                     log.info("cp has no parent and no shelf");
231                     this.circuitPacksRecord.put(cp.getCircuitPackName(), (equipmentLevel + 1));
232                     databaseService.writeInventory(
233                             this.opnRdmInventoryInput.getCircuitPackInventory(cp, Uint32.valueOf(equipmentLevel + 1)));
234                 } else {
235                     //                check for missing valid parent circuit name
236                     if (cp.getParentCircuitPack().getCpSlotName() != null
237                             && cp.getParentCircuitPack().getCircuitPackName() == null) {
238
239                         log.info("Cp {} has slotname of the parent circuit pack  but no parent circuit pack name",
240                                 cp.getCircuitPackName());
241                         this.circuitPacksRecord.put(cp.getCircuitPackName(), (equipmentLevel + 3));
242                         databaseService.writeInventory(this.opnRdmInventoryInput.getCircuitPackInventory(cp,
243                                 Uint32.valueOf(equipmentLevel + 3)));
244                         databaseService.writeEventLog(writeIncorrectParentLog(cp.getCircuitPackName(), counter)
245                                 .setObjectId(device.getInfo().getNodeId().getValue())
246                                 .setId(cp.getParentCircuitPack().getCpSlotName())
247                                 .setNewValue("Missing parent circuit pack name").build());
248                     } else if (cp.getParentCircuitPack().getCircuitPackName() != null
249                             && this.shelfProvisionedcircuitPacks
250                                     .containsKey(cp.getParentCircuitPack().getCircuitPackName())) {
251                         log.info("Cp {} has parent circuit pack and shelf", cp.getCircuitPackName());
252                         this.circuitPacksRecord.put(cp.getCircuitPackName(), (equipmentLevel + 3));
253                         databaseService.writeInventory(this.opnRdmInventoryInput.getCircuitPackInventory(cp,
254                                 Uint32.valueOf(equipmentLevel + 3)));
255                     } else {
256                         //                check for incorrect hierarchy
257                         if (cp.getParentCircuitPack().getCircuitPackName() != null
258                                 && !cpNames.contains(cp.getParentCircuitPack().getCircuitPackName())) {
259                             databaseService.writeEventLog(writeIncorrectParentLog(cp.getCircuitPackName(), counter)
260                                     .setObjectId(device.getInfo().getNodeId().getValue())
261                                     .setId(cp.getParentCircuitPack().getCpSlotName()).build());
262                         }
263                         log.info("Cp has parent circuit pack but no shelf or a shelf but no parent circuit pack");
264                         this.circuitPacksRecord.put(cp.getCircuitPackName(), (equipmentLevel + 2));
265                         databaseService.writeInventory(this.opnRdmInventoryInput.getCircuitPackInventory(cp,
266                                 Uint32.valueOf(equipmentLevel + 2)));
267                     }
268
269                 }
270             }
271
272
273             //            for (String s : cpParentNames) {
274             //                log.info("Parent cps {}:", s);
275             //
276             //                if (!cpNames.contains(s)) {
277             //                    log.info("Invalid Hierarchy detected for {}", s);
278             //                    databaseService.writeEventLog(writeIncorrectParentLog(s, counter)
279             //                            .setObjectId(device.getInfo().getNodeId().getValue()).setId(s).build());
280             //                }
281             //            }
282         }
283     }
284
285     private void readInterfaceData(OrgOpenroadmDevice device) {
286         Collection<Interface> interfaceList = YangHelper.getCollection(device.getInterface());
287         if (interfaceList != null) {
288             for (Interface deviceInterface : interfaceList) {
289                 log.info("\n InterfaceName: {}", deviceInterface.getName());
290                 log.info("Supporting CP {}", this.circuitPacksRecord.size());
291                 for (String s : this.circuitPacksRecord.keySet()) {
292                     log.info("{} value {}", s, this.circuitPacksRecord.get(s));
293                 }
294                 log.info("Interface {} and their supporting CP {}", deviceInterface.getName(),
295                         deviceInterface.getSupportingCircuitPackName());
296                 if (deviceInterface.getSupportingCircuitPackName() != null) {
297                     if (this.circuitPacksRecord.containsKey(deviceInterface.getSupportingCircuitPackName())) {
298                         databaseService.writeInventory(this.opnRdmInventoryInput.getInterfacesInventory(deviceInterface,
299                                 Uint32.valueOf(
300                                         this.circuitPacksRecord.get(deviceInterface.getSupportingCircuitPackName())
301                                                 + 1)));
302                     }
303                 } else {
304                     databaseService.writeInventory(this.opnRdmInventoryInput.getInterfacesInventory(deviceInterface,
305                             Uint32.valueOf(equipmentLevel + 1)));
306                 }
307             }
308         }
309     }
310
311     private OrgOpenroadmDevice readDevice(NetconfBindingAccessor accessor) {
312         final Class<OrgOpenroadmDevice> openRoadmDev = OrgOpenroadmDevice.class;
313         InstanceIdentifier<OrgOpenroadmDevice> deviceId = InstanceIdentifier.builder(openRoadmDev).build();
314         OrgOpenroadmDevice device = accessor.getTransactionUtils().readData(accessor.getDataBroker(),
315                 LogicalDatastoreType.OPERATIONAL, deviceId);
316         return device;
317     }
318
319     private EventlogBuilder writeIncorrectParentLog(String attributeName, Integer counter) {
320         EventlogBuilder eventlogBuilder = new EventlogBuilder();
321         eventlogBuilder.setAttributeName(attributeName).setCounter(counter)
322                 .setNodeId(this.netconfAccessor.getNodeId().getValue()).setSourceType(SourceType.Netconf)
323                 .setNewValue("Invalid parent circuit-pack name")
324                 .setTimestamp(new DateAndTime(ncTimeConverter.getTimeStamp()));
325
326         return eventlogBuilder;
327
328     }
329     // end of private methods
330 }