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