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