Merge "Optimize cm-handle registration with CPS-DMI Plugin to upload yang model"
[ccsdk/features.git] / sdnr / wt / devicemanager-onap / onf14 / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / onf14 / 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.impl;
19
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Optional;
23 import org.eclipse.jdt.annotation.NonNull;
24 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider;
25 import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.service.NetworkElement;
26 import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.service.NetworkElementService;
27 import org.onap.ccsdk.features.sdnr.wt.devicemanager.onf14.impl.dataprovider.Onf14ToInternalDataModel;
28 import org.onap.ccsdk.features.sdnr.wt.devicemanager.onf14.impl.equipment.Onf14DomEquipmentManager;
29 import org.onap.ccsdk.features.sdnr.wt.devicemanager.onf14.impl.interfaces.Onf14DomInterfacePacManager;
30 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.DeviceManagerServiceProvider;
31 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.FaultService;
32 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.NotificationService;
33 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.FaultData;
34 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfAccessor;
35 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfDomAccessor;
36 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.StreamKey;
39 import org.opendaylight.yang.gen.v1.urn.onf.yang.core.model._1._4.rev191127.ControlConstruct;
40 import org.opendaylight.yang.gen.v1.urn.onf.yang.core.model._1._4.rev191127.UniversalId;
41 import org.opendaylight.yang.gen.v1.urn.onf.yang.core.model._1._4.rev191127.control.construct.Equipment;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.NetworkElementConnectionBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.NetworkElementDeviceType;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
45 import org.opendaylight.yangtools.yang.binding.DataObject;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 /**
51  * Representation of ONF Core model 1.4 device Top level element is "ControlConstruct" (replaces "NetworkElement" of
52  * older ONF Version) NOTE: This class is still under development due to unmet dependencies (especially the ones related
53  * to DOM notifications) in ODL. Once the dependencies are complete, this class will replace the ONF14NetworkElement
54  */
55 public class Onf14DomNetworkElement implements NetworkElement {
56
57     private static final Logger log = LoggerFactory.getLogger(Onf14DomNetworkElement.class);
58
59     //    protected static final InstanceIdentifier<ControlConstruct> CONTROLCONSTRUCT_IID =
60     //            InstanceIdentifier.builder(ControlConstruct.class).build();
61     protected static final YangInstanceIdentifier CONTROLCONSTRUCT_IID =
62             YangInstanceIdentifier.builder().node(ControlConstruct.QNAME).build();
63
64     private final NetconfDomAccessor netconfDomAccessor;
65     private final DataProvider databaseService;
66     private final @NonNull FaultService faultService;
67     private final @NonNull NotificationService notificationService;
68
69     private final Onf14ToInternalDataModel onf14Mapper;
70
71     private final @NonNull Onf14DomEquipmentManager equipmentManager;
72     private final @NonNull Onf14DomInterfacePacManager interfacePacManager;
73     private final @NonNull String namespaceRevision;
74
75     private boolean experimental;
76
77
78     public Onf14DomNetworkElement(NetconfDomAccessor netconfDomAccessor, DeviceManagerServiceProvider serviceProvider,
79             String namespaceRevision) {
80         log.info("Create {}", Onf14DomNetworkElement.class.getSimpleName());
81         this.netconfDomAccessor = netconfDomAccessor;
82         this.databaseService = serviceProvider.getDataProvider();
83         this.notificationService = serviceProvider.getNotificationService();
84         this.faultService = serviceProvider.getFaultService();
85         this.namespaceRevision = namespaceRevision;
86         this.onf14Mapper = new Onf14ToInternalDataModel();
87         this.equipmentManager = new Onf14DomEquipmentManager(netconfDomAccessor, databaseService, onf14Mapper);
88
89         this.interfacePacManager = new Onf14DomInterfacePacManager(netconfDomAccessor, serviceProvider);
90         this.experimental = false;
91     }
92
93     /**
94      * reading the inventory (CoreModel 1.4 Equipment Model) and adding it to the DB
95      */
96     public void initialReadFromNetworkElement() {
97
98         //Read complete device tree
99         Optional<ControlConstruct> oControlConstruct = readControlConstruct(netconfDomAccessor);
100
101         if (oControlConstruct.isPresent()) {
102             ControlConstruct controlConstruct = oControlConstruct.get();
103
104             equipmentManager.setEquipmentData(controlConstruct);
105
106             //-- Start for experimental purpose
107             if (experimental) {
108                 log.warn("Experimental code activated");
109                 for (UniversalId uuid : equipmentManager.getEquipmentUuidList()) {
110                     log.info("Read data with id {}", uuid);
111                     Optional<Equipment> res1 = equipmentManager.readEquipmentInstance(netconfDomAccessor, uuid);
112                     log.info("Res1: {}", res1.isPresent() ? res1.get() : "No data1");
113
114                     /*List<DataObject> res2 = equipmentManager.readEquipmentList(netconfDomAccessor);
115                     log.info("Res2: {}", res2.isPresent() ? res2.get() : "No data2");*/
116
117                     equipmentManager.readTopLevelEquipment(netconfDomAccessor);
118                     //Do it only once for test purpose and break
119                     break;
120                 }
121                 List<DataObject> res2 = equipmentManager.readEquipmentList(netconfDomAccessor);
122                 //log.info("Res2: {}", res2.isPresent() ? res2.get() : "No data2");
123                 for (DataObject dobj : res2) {
124                     Equipment eqpt = (Equipment) dobj;
125                     log.info("Equipment local ID is : {}", eqpt.getLocalId());
126                 }
127
128                 equipmentManager.readTopLevelEquipment(netconfDomAccessor);
129             }
130             //-- End for experimental purpose
131
132             // storing all the LTP UUIDs internally, for later usage, for air-interface and ethernet-container
133             interfacePacManager.readKeys(controlConstruct);
134
135             // Read all fault data
136             FaultData resultList = new FaultData();
137
138             int problems = faultService.removeAllCurrentProblemsOfNode(netconfDomAccessor.getNodeId());
139             log.debug("Removed all {} problems from database at registration", problems);
140
141             interfacePacManager.readAllAirInterfaceCurrentProblems(netconfDomAccessor, controlConstruct, resultList);
142             problems = resultList.size();
143             log.debug("NETCONF read air interface current problems completed. Got back {} problems.", problems);
144             /*
145             readAllEhernetContainerCurrentProblems(resultList);
146             problems = resultList.size() - problems;
147             log.debug("NETCONF read current problems completed. Got back {} problems.", resultList.size());
148
149             readAllWireInterfaceCurrentProblems(resultList);
150             problems = resultList.size();
151             log.debug("NETCONF read wire interface current problems completed. Got back {} problems.", problems);
152             */
153             faultService.initCurrentProblemStatus(netconfDomAccessor.getNodeId(), resultList);
154             log.debug("DB write current problems completed");
155         }
156     }
157
158     /**
159      * @param nNode set core-model-capability
160      */
161     public void setCoreModel() {
162         NetworkElementConnectionBuilder eb = new NetworkElementConnectionBuilder();
163         eb.setCoreModelCapability(namespaceRevision);
164         databaseService.updateNetworkConnection22(eb.build(), netconfDomAccessor.getNodeId().getValue());
165     }
166
167     @Override
168     public void register() {
169         // Set core-model revision value in "core-model-capability" field
170         setCoreModel();
171         initialReadFromNetworkElement();
172
173         if (netconfDomAccessor.isNotificationsRFC5277Supported()) {
174             // register listener
175             interfacePacManager.subscribeNotifications();
176             // Output notification streams to LOG
177             Map<StreamKey, Stream> streams = netconfDomAccessor.getNotificationStreamsAsMap();
178             log.info("Available notifications streams: {}", streams);
179             // Register to default stream
180             netconfDomAccessor.invokeCreateSubscription();
181         }
182     }
183
184     @Override
185     public void deregister() {}
186
187
188     @Override
189     public NodeId getNodeId() {
190         return netconfDomAccessor.getNodeId();
191     }
192
193     @Override
194     public <L extends NetworkElementService> Optional<L> getService(Class<L> clazz) {
195         return Optional.empty();
196     }
197
198     @Override
199     public void warmstart() {}
200
201     @Override
202     public Optional<NetconfAccessor> getAcessor() {
203         return Optional.of(netconfDomAccessor);
204     }
205
206     @Override
207     public NetworkElementDeviceType getDeviceType() {
208         return NetworkElementDeviceType.Wireless;
209     }
210
211     private static Optional<ControlConstruct> readControlConstruct(NetconfDomAccessor netconfDomAccessor) {
212         return netconfDomAccessor.readData(LogicalDatastoreType.CONFIGURATION, CONTROLCONSTRUCT_IID,
213                 ControlConstruct.class);
214     }
215
216
217
218 }