Support for PM (Performance Management)
[ccsdk/features.git] / sdnr / wt / devicemanager-onap / onf14 / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / onf14 / dom / impl / Onf14DomNetworkElement.java
1 /*
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2020 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.onf14.dom.impl;
19
20 import java.util.Iterator;
21 import java.util.Map;
22 import java.util.Optional;
23 import org.eclipse.jdt.annotation.NonNull;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider;
26 import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.service.NetworkElement;
27 import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.service.NetworkElementService;
28 import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.service.PerformanceDataProvider;
29 import org.onap.ccsdk.features.sdnr.wt.devicemanager.onf14.dom.impl.dataprovider.Onf14DomToInternalDataModel;
30 import org.onap.ccsdk.features.sdnr.wt.devicemanager.onf14.dom.impl.equipment.Onf14DomEquipmentManager;
31 import org.onap.ccsdk.features.sdnr.wt.devicemanager.onf14.dom.impl.interfaces.Onf14DomInterfacePacManager;
32 import org.onap.ccsdk.features.sdnr.wt.devicemanager.onf14.dom.impl.interfaces.TechnologySpecificPacKeys;
33 import org.onap.ccsdk.features.sdnr.wt.devicemanager.onf14.dom.impl.util.Onf14DevicemanagerQNames;
34 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.DeviceManagerServiceProvider;
35 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.FaultService;
36 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.NotificationService;
37 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.PerformanceManager;
38 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.PerformanceDataLtp;
39 import org.onap.ccsdk.features.sdnr.wt.devicemanager.util.InconsistentPMDataException;
40 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfAccessor;
41 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfDomAccessor;
42 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.StreamKey;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.NetworkElementConnectionBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.NetworkElementDeviceType;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
49 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 /**
54  * Representation of ONF Core model 1.4 device Top level element is "ControlConstruct" (replaces "NetworkElement" of
55  * older ONF Version) NOTE:
56  */
57 public class Onf14DomNetworkElement implements NetworkElement, PerformanceDataProvider {
58
59     private static final Logger log = LoggerFactory.getLogger(Onf14DomNetworkElement.class);
60
61     protected static final YangInstanceIdentifier TOPLEVELEQUIPMENT_IID =
62             YangInstanceIdentifier.builder().node(Onf14DevicemanagerQNames.CORE_MODEL_CONTROL_CONSTRUCT_CONTAINER)
63                     .node(Onf14DevicemanagerQNames.CORE_MODEL_CC_TOP_LEVEL_EQPT).build();
64
65     private final @NonNull Object pmLock = new Object();
66     protected @Nullable TechnologySpecificPacKeys pmLp = null;
67     protected @Nullable Iterator<TechnologySpecificPacKeys> interfaceListIterator = null;
68     private final NetconfDomAccessor netconfDomAccessor;
69     private final DataProvider databaseService;
70     private final @NonNull FaultService faultService;
71     private final @NonNull PerformanceManager performanceManager;
72     private final @NonNull NotificationService notificationService;
73
74     private final Onf14DomToInternalDataModel onf14Mapper;
75
76     private final @NonNull Onf14DomEquipmentManager equipmentManager;
77     private final @NonNull Onf14DomInterfacePacManager interfacePacManager;
78     private final @NonNull String namespaceRevision;
79
80     public Onf14DomNetworkElement(NetconfDomAccessor netconfDomAccessor, DeviceManagerServiceProvider serviceProvider,
81             String namespaceRevision) {
82         log.info("Create {}", Onf14DomNetworkElement.class.getSimpleName());
83         this.netconfDomAccessor = netconfDomAccessor;
84         this.databaseService = serviceProvider.getDataProvider();
85         this.notificationService = serviceProvider.getNotificationService();
86         this.faultService = serviceProvider.getFaultService();
87         this.performanceManager = serviceProvider.getPerformanceManagerService();
88         this.namespaceRevision = namespaceRevision;
89         this.onf14Mapper = new Onf14DomToInternalDataModel();
90         this.equipmentManager = new Onf14DomEquipmentManager(netconfDomAccessor, databaseService, onf14Mapper);
91         this.interfacePacManager = new Onf14DomInterfacePacManager(netconfDomAccessor, serviceProvider);
92
93     }
94
95     /**
96      * reading the inventory (CoreModel 1.4 Equipment Model) and adding it to the DB
97      */
98     public void initialReadFromNetworkElement() {
99         log.debug("Calling read equipment");
100         // Read complete device tree
101         readEquipmentData();
102
103         int problems = faultService.removeAllCurrentProblemsOfNode(netconfDomAccessor.getNodeId());
104         log.debug("Removed all {} problems from database at registration", problems);
105
106         // Read fault data and subscribe for notifications
107         interfacePacManager.register();
108
109     }
110
111     /**
112      * @param nNode set core-model-capability
113      */
114     public void setCoreModel() {
115         NetworkElementConnectionBuilder eb = new NetworkElementConnectionBuilder();
116         eb.setCoreModelCapability(namespaceRevision);
117         databaseService.updateNetworkConnection22(eb.build(), netconfDomAccessor.getNodeId().getValue());
118     }
119
120     @Override
121     public void register() {
122         // Set core-model revision value in "core-model-capability" field
123         setCoreModel();
124         initialReadFromNetworkElement();
125
126         if (netconfDomAccessor.isNotificationsRFC5277Supported()) {
127             // Output notification streams to LOG
128             Map<StreamKey, Stream> streams = netconfDomAccessor.getNotificationStreamsAsMap();
129             log.debug("Available notifications streams: {}", streams);
130             // Register to default stream
131             netconfDomAccessor.invokeCreateSubscription();
132         }
133         // -- Register NE to performance manager
134         performanceManager.registration(netconfDomAccessor.getNodeId(), this);
135     }
136
137     @Override
138     public void deregister() {
139         performanceManager.deRegistration(netconfDomAccessor.getNodeId());
140     }
141
142     @Override
143     public NodeId getNodeId() {
144         return netconfDomAccessor.getNodeId();
145     }
146
147     @SuppressWarnings("unchecked")
148     @Override
149     public <L extends NetworkElementService> Optional<L> getService(Class<L> clazz) {
150         return clazz.isInstance(this) ? Optional.of((L) this) : Optional.empty();
151     }
152
153     @Override
154     public void warmstart() {}
155
156     @Override
157     public Optional<NetconfAccessor> getAcessor() {
158         return Optional.of(netconfDomAccessor);
159     }
160
161     @Override
162     public NetworkElementDeviceType getDeviceType() {
163         return NetworkElementDeviceType.Wireless;
164     }
165
166     private void readEquipmentData() {
167         Optional<NormalizedNode> topLevelEquipment = readTopLevelEquipment(netconfDomAccessor);
168         log.debug("Top level equipment data is {}", topLevelEquipment.isPresent() ? topLevelEquipment.get() : null);
169         if (topLevelEquipment.isPresent()) {
170             equipmentManager.setEquipmentData(topLevelEquipment.get());
171         }
172
173     }
174
175     private Optional<NormalizedNode> readTopLevelEquipment(NetconfDomAccessor netconfDomAccessor) {
176         return netconfDomAccessor.readDataNode(LogicalDatastoreType.CONFIGURATION, TOPLEVELEQUIPMENT_IID);
177     }
178
179     public Object getPmLock() {
180         return pmLock;
181     }
182
183     @Override
184     public void resetPMIterator() {
185         synchronized (pmLock) {
186             interfaceListIterator = interfacePacManager.getAirInterfaceList().iterator();
187         }
188         log.debug("PM reset iterator");
189     }
190
191     @Override
192     public boolean hasNext() {
193         boolean res;
194         synchronized (pmLock) {
195             res = interfaceListIterator != null ? interfaceListIterator.hasNext() : false;
196         }
197         log.debug("PM hasNext LTP {}", res);
198         return res;
199     }
200
201     @Override
202     public void next() {
203         synchronized (pmLock) {
204             if (interfaceListIterator == null) {
205                 pmLp = null;
206                 log.debug("PM next LTP null");
207             } else {
208                 pmLp = interfaceListIterator.next();
209             }
210         }
211
212     }
213
214     @Override
215     public Optional<PerformanceDataLtp> getLtpHistoricalPerformanceData() throws InconsistentPMDataException {
216         synchronized (getPmLock()) {
217             if (pmLp != null) {
218                 log.debug("Enter query PM");
219                 @NonNull
220                 TechnologySpecificPacKeys lp = pmLp;
221                 return Optional.of(interfacePacManager.getLtpHistoricalPerformanceData(lp));
222             }
223             return Optional.empty();
224         }
225     }
226
227     @Override
228     public String pmStatusToString() {
229         StringBuilder res = new StringBuilder();
230         synchronized (pmLock) {
231             if (pmLp == null) {
232                 res.append("no interface");
233             } else {
234                 res.append("ActualLP=");
235                 res.append(pmLp.getLocalId());
236             }
237             res.append(" IFList=");
238             int no = 0;
239             for (TechnologySpecificPacKeys lp : interfacePacManager.getAirInterfaceList()) {
240                 res.append("[");
241                 res.append(no++);
242                 res.append("]=");
243                 res.append(lp.getLocalId());
244                 res.append(" ");
245             }
246         }
247         return res.toString();
248     }
249
250 }