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