weekly sdnr code sync
[ccsdk/features.git] / sdnr / wt / devicemanager-onf / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / onf / ne / ONFCoreNetworkElement12Microwave.java
1 /*
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.onf.ne;
19
20 import java.util.List;
21 import java.util.Optional;
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.onap.ccsdk.features.sdnr.wt.common.YangHelper;
25 import org.onap.ccsdk.features.sdnr.wt.devicemanager.onf.ifpac.microwave.Helper;
26 import org.onap.ccsdk.features.sdnr.wt.devicemanager.onf.ifpac.microwave.OnfMicrowaveModel;
27 import org.onap.ccsdk.features.sdnr.wt.devicemanager.onf.impl.DeviceManagerOnfConfiguration;
28 import org.onap.ccsdk.features.sdnr.wt.devicemanager.onf.notifications.NotificationActor;
29 import org.onap.ccsdk.features.sdnr.wt.devicemanager.onf.notifications.NotificationWorker;
30 import org.onap.ccsdk.features.sdnr.wt.devicemanager.onf.util.ONFLayerProtocolName;
31 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.DeviceManagerServiceProvider;
32 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.EquipmentService;
33 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.FaultService;
34 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.FaultData;
35 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.PerformanceDataLtp;
36 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfBindingAccessor;
37 import org.opendaylight.mdsal.binding.api.MountPoint;
38 import org.opendaylight.mdsal.binding.api.NotificationService;
39 import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.core.model.rev170320.UniversalId;
40 import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.core.model.rev170320.extension.g.Extension;
41 import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.core.model.rev170320.logical.termination.point.g.Lp;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.EventlogEntity;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.NetworkElementDeviceType;
44 import org.opendaylight.yangtools.concepts.ListenerRegistration;
45 import org.opendaylight.yangtools.yang.binding.NotificationListener;
46 import org.opendaylight.yangtools.yang.common.QName;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 /**
51  * Get information over NETCONF device according to ONF Coremodel. Read networkelement and conditional packages.
52  *
53  * Get conditional packages from Networkelement Possible interfaces are: MWPS, LTP(MWPS-TTP), MWAirInterfacePac,
54  * MicrowaveModel-ObjectClasses-AirInterface ETH-CTP,LTP(Client), MW_EthernetContainer_Pac MWS, LTP(MWS-CTP-xD),
55  * MWAirInterfaceDiversityPac, MicrowaveModel-ObjectClasses-AirInterfaceDiversity MWS, LTP(MWS-TTP),
56  * ,MicrowaveModel-ObjectClasses-HybridMwStructure MWS, LTP(MWS-TTP),
57  * ,MicrowaveModel-ObjectClasses-PureEthernetStructure
58  *
59  * @author herbert
60  *
61  */
62 public class ONFCoreNetworkElement12Microwave extends ONFCoreNetworkElement12Basic
63         implements NotificationActor<EventlogEntity> {
64
65     private static final Logger LOG = LoggerFactory.getLogger(ONFCoreNetworkElement12Microwave.class);
66
67     private final @NonNull FaultService microwaveEventListener;
68     private final @NonNull EquipmentService equipmentService;
69     private final @NonNull OnfMicrowaveModel microwaveModel;
70     private final NotificationWorker<EventlogEntity> notificationQueue;
71
72     private ListenerRegistration<NotificationListener> listenerRegistrationresult = null;
73
74     /*-----------------------------------------------------------------------------
75      * Construction
76      */
77
78     /**
79      * Constructor
80      *
81      * @param acessor for device
82      * @param serviceProvider to get services
83      * @param onfMicrowaveModel handling ofmicrosoft model data
84      */
85     public ONFCoreNetworkElement12Microwave(@NonNull NetconfBindingAccessor acessor,
86             @NonNull DeviceManagerServiceProvider serviceProvider, DeviceManagerOnfConfiguration configuration,
87             OnfMicrowaveModel onfMicrowaveModel) {
88
89         super(acessor, serviceProvider, configuration);
90         this.microwaveModel = onfMicrowaveModel;
91
92         this.microwaveEventListener = serviceProvider.getFaultService();
93         this.equipmentService = serviceProvider.getEquipmentService();
94
95         this.notificationQueue = new NotificationWorker<>(1, 100, this);
96         this.microwaveModel.setNotificationQueue(notificationQueue);
97
98     }
99
100     /*-----------------------------------------------------------------------------
101      * Functions
102      */
103
104     /**
105      * @param acessor
106      * @param serviceProvider
107      * @param configuration
108      * @param onfMicrowaveModel
109      */
110     /**
111      * DeviceMonitor Prepare check by updating NE state and reading all interfaces.
112      */
113     @Override
114     public void prepareCheck() {
115         synchronized (dmLock) {
116             boolean change = readNetworkElementAndInterfaces();
117             if (change) {
118                 int problems = microwaveEventListener.removeAllCurrentProblemsOfNode(nodeId);
119                 FaultData resultList = readAllCurrentProblemsOfNode();
120                 microwaveEventListener.initCurrentProblemStatus(nodeId, resultList);
121                 LOG.info("Resync mountpoint {} for device {}. Removed {}. Current problems: {}", getMountpoint(),
122                         getUuId(), problems, resultList.size());
123             }
124         }
125     }
126
127     // public boolean checkIfConnectionToMediatorIsOk() -> Shifted to super class
128     // public boolean checkIfConnectionToNeIsOk() -> Shifted to super class
129
130     /*-----------------------------------------------------------------------------
131      * Synchronization
132      */
133
134     // public void initSynchronizationExtension() -> Shifted to super class
135     // private InstanceList readPTPClockInstances() -> Shifted to super class
136
137     /*-----------------------------------------------------------------------------
138      * Services for NE/Device synchronization
139      */
140
141     /**
142      * Handling of specific Notifications from NE, indicating changes and need for synchronization.
143      *
144      * <attribute-value-changed-notification xmlns="urn:onf:params:xml:ns:yang:microwave-model">
145      * <attribute-name>/equipment-pac/equipment-current-problems</attribute-name>
146      * <object-id-ref>CARD-1.1.1.0</object-id-ref> <new-value></new-value> </attribute-value-changed-notification>
147      * <attribute-value-changed-notification xmlns="urn:onf:params:xml:ns:yang:microwave-model">
148      * <attribute-name>/network-element/extension[value-name="top-level-equipment"]/value</attribute-name>
149      * <object-id-ref>Hybrid-Z</object-id-ref>
150      * <new-value>SHELF-1.1.0.0,IDU-1.55.0.0,ODU-1.56.0.0,IDU-1.65.0.0</new-value>
151      * </attribute-value-changed-notification>
152      */
153     @Override
154     public void notificationActor(@NonNull EventlogEntity notification) {
155
156         LOG.debug("Enter change notification listener");
157         if (LOG.isTraceEnabled()) {
158             LOG.trace("Notification: {}", notification);
159         }
160         String attributeName = notification.getAttributeName();
161         if (attributeName != null) {
162             if (attributeName.equals("/equipment-pac/equipment-current-problems")) {
163                 syncEquipmentPac(notification.getObjectId());
164             } else if (attributeName.equals("/network-element/extension[value-name=\"top-level-equipment\"]/value")) {
165                 initialReadFromNetworkElement();
166             }
167         }
168         LOG.debug("Leave change notification listener");
169     }
170
171     /**
172      * Synchronize problems for a specific equipment-pac
173      *
174      * @param uuidString of the equipment-pac
175      */
176     private void syncEquipmentPac(String uuidString) {
177
178         int problems = microwaveEventListener.removeObjectsCurrentProblemsOfNode(nodeId, uuidString);
179         LOG.debug("Removed {} problems for uuid {}", problems, uuidString);
180
181         FaultData resultList = equipment.addProblemsofNodeObject(uuidString);
182         microwaveEventListener.initCurrentProblemStatus(nodeId, resultList);
183         LOG.debug("Added {} problems for uuid {}", resultList.size(), uuidString);
184
185     }
186
187
188     /*-----------------------------------------------------------------------------
189      * Problem/Fault related functions
190      */
191
192     /**
193      * Read during startup all relevant structure and status parameters from device
194      */
195     @Override
196     public void initialReadFromNetworkElement() {
197         LOG.debug("Get info about {}", getMountpoint());
198
199         int problems = microwaveEventListener.removeAllCurrentProblemsOfNode(nodeId);
200         LOG.debug("Removed all {} problems from database at registration", problems);
201
202         // Step 2.1: access data broker within this mount point
203         LOG.debug("DBRead start");
204
205         // Step 2.2: read ne from data store
206         readNetworkElementAndInterfaces();
207         LOG.debug("NETCONF read network element and interfaces completed");
208         equipment.readNetworkElementEquipment();
209         LOG.debug("NETCONF read equipment completed");
210
211         // Step 2.3: read the existing faults and add to DB
212         FaultData resultList = readAllCurrentProblemsOfNode();
213         LOG.debug("NETCONF read current problems completed");
214         equipment.addProblemsofNode(resultList);
215
216         microwaveEventListener.initCurrentProblemStatus(nodeId, resultList);
217         LOG.debug("DB write current problems completed");
218         equipmentService.writeEquipment(nodeId, equipment.getEquipmentData());
219
220         LOG.info("Found info at {} for device {} number of problems: {}", getMountpoint(), getUuId(),
221                 resultList.size());
222     }
223
224     /**
225      * LOG the newly added problems of the interface pac
226      *
227      * @param idxStart
228      * @param uuid
229      * @param resultList
230      */
231     private void debugResultList(String uuid, FaultData resultList, int idxStart) {
232         if (LOG.isDebugEnabled()) {
233             StringBuilder sb = new StringBuilder();
234             int idx = 0;
235             for (int t = idxStart; t < resultList.size(); t++) {
236                 sb.append(idx++);
237                 sb.append(":{");
238                 sb.append(resultList.get(t));
239                 sb.append('}');
240             }
241             LOG.debug("Found problems {} {}", uuid, sb);
242         }
243     }
244
245     /**
246      * Read current problems of AirInterfaces and EthernetContainer according to NE status into DB
247      *
248      * @return List with all problems
249      */
250     @Override
251     protected FaultData readAllCurrentProblemsOfNode() {
252
253         // Step 2.3: read the existing faults and add to DB
254         FaultData resultList = new FaultData();
255         int idxStart; // Start index for debug messages
256         @NonNull
257         UniversalId uuid;
258
259         synchronized (getPmLock()) {
260             for (Lp lp : getInterfaceList()) {
261
262                 idxStart = resultList.size();
263                 uuid = Helper.nnGetUniversalId(lp.getUuid());
264                 @Nullable
265                 Class<?> lpClass = getLpExtension(lp);
266
267                 ONFLayerProtocolName lpName = ONFLayerProtocolName.valueOf(lp.getLayerProtocolName());
268                 microwaveModel.readTheFaultsOfMicrowaveModel(lpName, lpClass, uuid, resultList);
269                 debugResultList(uuid.getValue(), resultList, idxStart);
270             }
271         }
272
273         // Step 2.4: Read other problems from mountpoint
274         if (isNetworkElementCurrentProblemsSupporting12) {
275             idxStart = resultList.size();
276             readNetworkElementCurrentProblems12(resultList);
277             debugResultList("CurrentProblems12", resultList, idxStart);
278         }
279
280         return resultList;
281
282     }
283
284     /**
285      * Get from LayerProtocolExtensions the related generated ONF Interface PAC class which represents it.
286      *
287      * @param lp logical termination point
288      * @return Class of InterfacePac
289      */
290     @Nullable
291     private Class<?> getLpExtension(@Nullable Lp lp) {
292
293         String capability = EMPTY;
294         String revision = EMPTY;
295         String conditionalPackage = EMPTY;
296         Class<?> res = null;
297
298         if (lp != null) {
299             for (Extension e : getExtensionList(lp)) {
300                 String valueName = e.getValueName();
301                 if (valueName != null) {
302                     if (valueName.contentEquals("capability")) {
303                         capability = e.getValue();
304                         if (capability != null) {
305                             int idx = capability.indexOf('?');
306                             if (idx != -1) {
307                                 capability = capability.substring(0, idx);
308                             }
309                         }
310                     }
311                     if (valueName.contentEquals("revision")) {
312                         revision = e.getValue();
313                     }
314                     if (valueName.contentEquals("conditional-package")) {
315                         conditionalPackage = e.getValue();
316                     }
317                 }
318             }
319         }
320         // QName qName =
321         // org.opendaylight.yangtools.yang.common.QName.create("urn:onf:params:xml:ns:yang:microwave-model",
322         // "2017-03-24", "mw-air-interface-pac").intern();
323         LOG.info("LpExtension capability={} revision={} conditionalPackage={}", capability, revision,
324                 conditionalPackage);
325         if (capability != null && !capability.isEmpty() && !revision.isEmpty() && !conditionalPackage.isEmpty()) {
326             try {
327                 QName qName = QName.create(capability, revision, conditionalPackage);
328                 res = this.microwaveModel.getClassForLtpExtension(qName);
329             } catch (IllegalArgumentException e) {
330                 LOG.warn("Can not create QName from ({}{}{}): {}", capability, revision, conditionalPackage,
331                         e.getMessage());
332             }
333         }
334         return res;
335     }
336
337     /**
338      * Read element from class that could be not available
339      *
340      * @param ltp layer termination point
341      * @return List with extension parameters or empty list
342      */
343     @NonNull
344     private static List<Extension> getExtensionList(@Nullable Lp ltp) {
345         if (ltp != null) {
346             return YangHelper.getList(ltp.nonnullExtension());
347         } else {
348             return EMPTYLTPEXTENSIONLIST;
349         }
350     }
351
352     @Override
353     public Optional<PerformanceDataLtp> getLtpHistoricalPerformanceData() {
354         synchronized (getPmLock()) {
355             if (pmLp != null) {
356                 LOG.debug("Enter query PM");
357                 @SuppressWarnings("null")
358                 @NonNull
359                 Lp lp = pmLp;
360                 ONFLayerProtocolName lpName = ONFLayerProtocolName.valueOf(lp.getLayerProtocolName());
361                 return Optional.of(this.microwaveModel.getLtpHistoricalPerformanceData(lpName, lp));
362             }
363             return Optional.empty();
364         }
365     }
366
367     /**
368      * Remove all entries from list
369      */
370     @Override
371     public int removeAllCurrentProblemsOfNode() {
372         return microwaveEventListener.removeAllCurrentProblemsOfNode(nodeId);
373     }
374
375     /**
376      * Register the listener
377      */
378     @Override
379     public void doRegisterEventListener(MountPoint mountPoint) {
380         LOG.info("Begin registration listener for Mountpoint");
381         final Optional<NotificationService> optionalNotificationService =
382                 mountPoint.getService(NotificationService.class);
383         final NotificationService notificationService = optionalNotificationService.get();
384         // notificationService.registerNotificationListener(microwaveEventListener);
385         listenerRegistrationresult =
386                 notificationService.registerNotificationListener(microwaveModel.getNotificationListener());
387         LOG.info("End registration listener for Mountpoint Result: {}", listenerRegistrationresult);
388     }
389
390     /*------------------------------------------------------------
391      * private function to access database
392      */
393
394     /*-----------------------------------------------------------------------------
395      * Reading problems for the networkElement V1.2
396      */
397
398     @Override
399     public void close() throws Exception {
400         if (listenerRegistrationresult != null) {
401             listenerRegistrationresult.close();
402         }
403     }
404
405     @Override
406     public NetworkElementDeviceType getDeviceType() {
407         return NetworkElementDeviceType.Wireless;
408     }
409
410 }