Removing blueprints-processor
[ccsdk/features.git] / sdnr / wt / devicemanager / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / base / onfcore / ONFCoreNetworkElement12Base.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.base.onfcore;
19
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Optional;
25 import java.util.concurrent.CopyOnWriteArrayList;
26 import org.eclipse.jdt.annotation.NonNull;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.onap.ccsdk.features.sdnr.wt.devicemanager.NetworkElementService;
29 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.NetworkElementCoreData;
30 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.onfcore.container.AllPm;
31 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.Capabilities;
32 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.INetconfAcessor;
33 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.onfcore.wrapperc.WrapperPTPModelRev170208;
34 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.util.InternalDateAndTime;
35 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.util.InternalSeverity;
36 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.ProblemNotificationXml;
37 import org.onap.ccsdk.features.sdnr.wt.devicemanager.legacy.InventoryInformation;
38 import org.opendaylight.mdsal.binding.api.DataBroker;
39 import org.opendaylight.mdsal.binding.api.MountPoint;
40 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ptp.dataset.rev170208.InstanceList;
42 import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.core.model.rev170320.NetworkElement;
43 import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.core.model.rev170320.UniversalId;
44 import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.core.model.rev170320.extension.g.Extension;
45 import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.core.model.rev170320.logical.termination.point.g.Lp;
46 import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.core.model.rev170320.network.element.Ltp;
47 import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.onf.core.model.conditional.packages.rev170402.NetworkElementPac;
48 import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.onf.core.model.conditional.packages.rev170402.network.element.pac.NetworkElementCurrentProblems;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53
54 /**
55  * This class contains the ONF Core model Version 1.2 related functions.<br>
56  * Provides the basic ONF Core Model function.<br>
57  * - initialReadFromNetworkElement is not implemented in child classes.
58  */
59 public abstract class ONFCoreNetworkElement12Base extends ONFCoreNetworkElementBase implements NetworkElementCoreData {
60
61     private static final Logger LOG = LoggerFactory.getLogger(ONFCoreNetworkElement12Base.class);
62
63     protected static final List<Extension> EMPTYLTPEXTENSIONLIST = new ArrayList<>();
64     // private static final List<Ltp> EMPTYLTPLIST = new ArrayList<>();
65
66     protected static final InstanceIdentifier<NetworkElement> NETWORKELEMENT_IID = InstanceIdentifier
67             .builder(NetworkElement.class).build();
68
69
70     /*-----------------------------------------------------------------------------
71      * Class members
72      */
73
74     // Non specific part. Used by all functions.
75     /** interfaceList is used by PM task and should be synchronized */
76     private final @NonNull List<Lp> interfaceList = Collections.synchronizedList(new CopyOnWriteArrayList<>());
77     private @Nullable NetworkElement optionalNe = null;
78
79     // Performance monitoring specific part
80     /** Lock for the PM access specific elements that could be null */
81     private final @NonNull Object pmLock = new Object();
82     protected @Nullable Iterator<Lp> interfaceListIterator = null;
83     /** Actual pmLp used during iteration over interfaces */
84     protected @Nullable Lp pmLp = null;
85
86     // Device monitoring specific part
87     /** Lock for the DM access specific elements that could be null */
88     protected final @NonNull Object dmLock = new Object();
89
90     protected final boolean isNetworkElementCurrentProblemsSupporting12;
91
92     protected final ONFCoreNetworkElement12Equipment equipment;
93
94     /*
95      * Constructor
96      */
97
98     protected ONFCoreNetworkElement12Base(INetconfAcessor acessor, String mountPointNodeName, DataBroker netconfNodeDataBroker,
99             Capabilities capabilities) {
100         super(acessor, mountPointNodeName, netconfNodeDataBroker, capabilities);
101         // TODO Auto-generated constructor stub
102         this.isNetworkElementCurrentProblemsSupporting12 = capabilities.isSupportingNamespaceAndRevision(NetworkElementPac.QNAME);
103         this.equipment = new ONFCoreNetworkElement12Equipment(acessor, this, capabilities);
104         WrapperPTPModelRev170208.initSynchronizationExtension(acessor);
105         LOG.debug("support necurrent-problem-list=" + this.isNetworkElementCurrentProblemsSupporting12);
106         LOG.info("Create NE instance {}", InstanceList.QNAME.getLocalName());
107     }
108
109     /*---------------------------------------------------------------
110      * Getter/ Setter
111      */
112
113     @Override
114     public NetworkElement getOptionalNetworkElement() {
115         return optionalNe;
116     }
117
118     List<Lp> getInterfaceList() {
119         return interfaceList;
120     }
121
122     public Object getPmLock() {
123         return pmLock;
124     }
125
126     /*---------------------------------------------------------------
127      * Core model related function
128      */
129
130     /**
131      * Get uuid of Optional NE.
132      *
133      * @return Uuid or EMPTY String if optionNE is not available
134      */
135     @SuppressWarnings("null")
136     protected String getUuId() {
137         String uuid = EMPTY;
138
139         try {
140             uuid = optionalNe != null ? optionalNe.getUuid() != null ? optionalNe.getUuid().getValue() : EMPTY : EMPTY;
141         } catch (NullPointerException e) {
142             // Unfortunately throws null pointer if not definied
143         }
144         return uuid;
145     }
146
147     /**
148      * Read from NetworkElement and verify LTPs have changed. If the NE has changed, update to the new
149      * structure. From initial state it changes also.
150      */
151     protected synchronized boolean readNetworkElementAndInterfaces() {
152
153         LOG.debug("Update mountpoint if changed {}", getMountPointNodeName());
154
155         optionalNe = getGenericTransactionUtils().readData(getNetconfNodeDataBroker(), LogicalDatastoreType.OPERATIONAL,
156                 NETWORKELEMENT_IID);;
157         synchronized (pmLock) {
158             boolean change = false;
159
160             if (optionalNe == null) {
161                 LOG.debug("Unable to read NE data for mountpoint {}", getMountPointNodeName());
162                 if (!interfaceList.isEmpty()) {
163                     interfaceList.clear();
164                     interfaceListIterator = null;
165                     change = true;
166                 }
167
168             } else {
169                 LOG.debug("Mountpoint '{}' NE-Name '{}'", getMountPointNodeName(), optionalNe.getName());
170                 List<Lp> actualInterfaceList = getLtpList(optionalNe);
171                 if (!interfaceList.equals(actualInterfaceList)) {
172                     LOG.debug("Mountpoint '{}' Update LTP List. Elements {}", getMountPointNodeName(),
173                             actualInterfaceList.size());
174                     interfaceList.clear();
175                     interfaceList.addAll(actualInterfaceList);
176                     interfaceListIterator = null;
177                     change = true;
178                 }
179             }
180             return change;
181         }
182     }
183
184     /**
185      * Get List of UUIDs for conditional packages from Networkelement<br>
186      * Possible interfaces are:<br>
187      * MWPS, LTP(MWPS-TTP), MWAirInterfacePac, MicrowaveModel-ObjectClasses-AirInterface<br>
188      * ETH-CTP,LTP(Client), MW_EthernetContainer_Pac<br>
189      * MWS, LTP(MWS-CTP-xD), MWAirInterfaceDiversityPac,
190      * MicrowaveModel-ObjectClasses-AirInterfaceDiversity<br>
191      * MWS, LTP(MWS-TTP), ,MicrowaveModel-ObjectClasses-HybridMwStructure<br>
192      * MWS, LTP(MWS-TTP), ,MicrowaveModel-ObjectClasses-PureEthernetStructure<br>
193      *
194      * @param ne NetworkElement
195      * @return Id List, never null.
196      */
197
198     private static List<Lp> getLtpList(@Nullable NetworkElement ne) {
199
200         List<Lp> res = Collections.synchronizedList(new ArrayList<Lp>());
201
202         if (ne != null) {
203             List<Ltp> ltpRefList = ne.getLtp();
204             if (ltpRefList == null) {
205                 LOG.debug("DBRead NE-Interfaces: null");
206             } else {
207                 for (Ltp ltRefListE : ltpRefList) {
208                     List<Lp> lpList = ltRefListE.getLp();
209                     if (lpList == null) {
210                         LOG.debug("DBRead NE-Interfaces Reference List: null");
211                     } else {
212                         for (Lp ltp : lpList) {
213                             res.add(ltp);
214                         }
215                     }
216                 }
217             }
218         } else {
219             LOG.debug("DBRead NE: null");
220         }
221
222         // ---- Debug
223         if (LOG.isDebugEnabled()) {
224             StringBuffer strBuf = new StringBuffer();
225             for (Lp ltp : res) {
226                 if (strBuf.length() > 0) {
227                     strBuf.append(", ");
228                 }
229                 strBuf.append(ltp.getLayerProtocolName().getValue());
230                 strBuf.append(':');
231                 strBuf.append(ltp.getUuid().getValue());
232             }
233             LOG.debug("DBRead NE-Interfaces: {}", strBuf.toString());
234         }
235         // ---- Debug end
236
237         return res;
238     }
239
240     /**
241      * Read current problems of AirInterfaces and EthernetContainer according to NE status into DB
242      *
243      * @return List with all problems
244      */
245     protected List<ProblemNotificationXml> readAllCurrentProblemsOfNode() {
246
247         // Step 2.3: read the existing faults and add to DB
248         List<ProblemNotificationXml> resultList = new ArrayList<>();
249         int idxStart; // Start index for debug messages
250         UniversalId uuid;
251
252         synchronized (pmLock) {
253             for (Lp lp : interfaceList) {
254
255                 idxStart = resultList.size();
256                 uuid = lp.getUuid();
257                 ProblemNotificationXml.debugResultList(LOG, uuid.getValue(), resultList, idxStart);
258
259             }
260         }
261
262         // Step 2.4: Read other problems from mountpoint
263         if (isNetworkElementCurrentProblemsSupporting12) {
264             idxStart = resultList.size();
265             readNetworkElementCurrentProblems12(resultList);
266             ProblemNotificationXml.debugResultList(LOG, "CurrentProblems12", resultList, idxStart);
267         }
268
269         return resultList;
270
271     }
272
273     /**
274      * Reading problems for the networkElement V1.2
275      * @param resultList
276      * @return
277      */
278     private List<ProblemNotificationXml> readNetworkElementCurrentProblems12(List<ProblemNotificationXml> resultList) {
279
280         LOG.info("DBRead Get {} NetworkElementCurrentProblems12", getMountPointNodeName());
281
282         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.onf.core.model.conditional.packages.rev170402.NetworkElementPac> networkElementCurrentProblemsIID =
283                 InstanceIdentifier.builder(
284                         org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.onf.core.model.conditional.packages.rev170402.NetworkElementPac.class)
285                         .build();
286
287         // Step 2.3: read to the config data store
288         NetworkElementPac problemPac;
289         NetworkElementCurrentProblems problems = null;
290         try {
291             problemPac = getGenericTransactionUtils().readData(getNetconfNodeDataBroker(), LogicalDatastoreType.OPERATIONAL,
292                     networkElementCurrentProblemsIID);
293             if (problemPac != null) {
294                 problems = problemPac.getNetworkElementCurrentProblems();
295             }
296             if (problems == null) {
297                 LOG.debug("DBRead no NetworkElementCurrentProblems12");
298             } else {
299                 for (org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.onf.core.model.conditional.packages.rev170402.network.element.current.problems.g.CurrentProblemList problem : problems
300                         .nonnullCurrentProblemList()) {
301                     resultList.add(new ProblemNotificationXml(getMountPointNodeName(), problem.getObjectReference(),
302                             problem.getProblemName(), InternalSeverity.valueOf(problem.getProblemSeverity()),
303                             problem.getSequenceNumber(),
304                             InternalDateAndTime.valueOf(problem.getTimeStamp())));
305                 }
306             }
307         } catch (Exception e) {
308             LOG.warn("DBRead {} NetworkElementCurrentProblems12 not supported. Message '{}' ", getMountPointNodeName(),
309                     e.getMessage());
310         }
311         return resultList;
312     }
313
314     /*---------------------------------------------------------------
315      * Device Monitor
316      */
317
318     @Override
319     public boolean checkIfConnectionToMediatorIsOk() {
320         synchronized (dmLock) {
321             return optionalNe != null;
322         }
323     }
324
325     /*
326      * New implementation to interpret status with empty LTP List as notConnected => return false
327      * 30.10.2018 Since this behavior is very specific and implicit for specific NE Types
328      *     it needs to be activated by extension or configuration. Change to be disabled at the moment
329      */
330     @Override
331     public boolean checkIfConnectionToNeIsOk() {
332         return true;
333     }
334
335     /*---------------------------------------------------------------
336      * Synchronization
337      */
338
339
340
341     /*---------------------------------------------------------------
342      * Equipment related functions
343      */
344
345     @Override
346     public @NonNull InventoryInformation getInventoryInformation(String layerProtocolFilter) {
347         LOG.debug("request inventory information. filter:" + layerProtocolFilter);
348         return this.equipment.getInventoryInformation(getFilteredInterfaceUuidsAsStringList(layerProtocolFilter));
349     }
350
351     @Override
352     public InventoryInformation getInventoryInformation() {
353         return getInventoryInformation(null);
354     }
355
356     protected List<String> getFilteredInterfaceUuidsAsStringList(String layerProtocolFilter) {
357         List<String> uuids = new ArrayList<>();
358
359         LOG.debug("request inventory information. filter:" + layerProtocolFilter);
360         if (optionalNe != null) {
361             // uuids
362             for (Lp lp : this.interfaceList) {
363                 if (layerProtocolFilter == null || layerProtocolFilter.isEmpty()) {
364                     uuids.add(lp.getUuid().getValue());
365                 } else if (lp.getLayerProtocolName() != null && lp.getLayerProtocolName().getValue() != null
366                         && lp.getLayerProtocolName().getValue().equals(layerProtocolFilter)) {
367                     uuids.add(lp.getUuid().getValue());
368                 }
369             }
370         }
371         LOG.debug("uuids found: {}", uuids);
372         return uuids;
373     }
374
375
376     /*---------------------------------------------------------------
377      * Performancemanagement specific interface
378      */
379
380     @Override
381     public void resetPMIterator() {
382         synchronized (pmLock) {
383             interfaceListIterator = interfaceList.iterator();
384         }
385         LOG.debug("PM reset iterator");
386     }
387
388     @Override
389     public boolean hasNext() {
390         boolean res;
391         synchronized (pmLock) {
392             res = interfaceListIterator != null ? interfaceListIterator.hasNext() : false;
393         }
394         LOG.debug("PM hasNext LTP {}", res);
395         return res;
396     }
397
398     @Override
399     public void next() {
400         synchronized (pmLock) {
401             if (interfaceListIterator == null) {
402                 pmLp = null;
403                 LOG.debug("PM next LTP null");
404             } else {
405                 pmLp = interfaceListIterator.next();
406                 LOG.debug("PM next LTP {}", pmLp.getLayerProtocolName().getValue());
407             }
408         }
409     }
410
411     @Override
412     public String pmStatusToString() {
413         StringBuffer res = new StringBuffer();
414         synchronized (pmLock) {
415             res.append(pmLp == null ? "no interface" : pmLp.getLayerProtocolName().getValue());
416             for (Lp lp : getInterfaceList()) {
417                 res.append("IF:");
418                 res.append(lp.getLayerProtocolName().getValue());
419                 res.append(" ");
420             }
421         }
422         return res.toString();
423     }
424
425     @Override
426     public AllPm getHistoricalPM() {
427         return AllPm.getEmpty();
428     }
429
430
431     @Override
432     public void doRegisterEventListener(MountPoint mountPoint) {
433         //Do nothing
434     }
435
436
437     @Override
438     public void register() {
439     }
440
441
442     @Override
443     public void deregister() {
444     }
445
446     @SuppressWarnings("unchecked")
447     @Override
448     public <L extends NetworkElementService> Optional<L> getService(Class<L> clazz) {
449         return clazz.isInstance(this) ? Optional.of((L)this) : Optional.empty();
450     }
451
452 }