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