47ea3eaddc363bd0786dfb5ffcfa546e31633170
[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.oran.impl;
19
20 import com.fasterxml.jackson.core.JsonProcessingException;
21 import java.util.Collection;
22 import java.util.List;
23 import java.util.Optional;
24 import org.eclipse.jdt.annotation.NonNull;
25 import org.onap.ccsdk.features.sdnr.wt.common.YangHelper;
26 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider;
27 import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.service.NetworkElement;
28 import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.service.NetworkElementService;
29 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.NotificationProxyParser;
30 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorService;
31 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESCommonEventHeaderPOJO;
32 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESPNFRegistrationFieldsPOJO;
33 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfAccessor;
34 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfBindingAccessor;
35 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfNotifications;
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.yang.ietf.hardware.rev180313.Hardware;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.hardware.rev180313.hardware.Component;
40 import org.opendaylight.yang.gen.v1.urn.onap.system.rev201026.System1;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Guicutthrough;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Inventory;
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.concepts.ListenerRegistration;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.opendaylight.yangtools.yang.binding.NotificationListener;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 public class ORanNetworkElement implements NetworkElement {
52
53     private static final Logger log = LoggerFactory.getLogger(ORanNetworkElement.class);
54
55     private final NetconfBindingAccessor netconfAccessor;
56
57     private final DataProvider databaseService;
58
59     @SuppressWarnings("unused")
60     private final VESCollectorService vesCollectorService;
61
62     private ListenerRegistration<NotificationListener> oRanListenerRegistrationResult;
63     private @NonNull final ORanChangeNotificationListener oRanListener;
64     private ListenerRegistration<NotificationListener> oRanFaultListenerRegistrationResult;
65     private @NonNull final ORanFaultNotificationListener oRanFaultListener;
66     private final NotificationProxyParser notificationProxyParser;
67     private Collection<Component> componentList;
68
69     ORanNetworkElement(NetconfBindingAccessor netconfAccess, DataProvider databaseService,
70             VESCollectorService vesCollectorService) {
71         log.info("Create {}", ORanNetworkElement.class.getSimpleName());
72         this.netconfAccessor = netconfAccess;
73         this.databaseService = databaseService;
74         this.vesCollectorService = vesCollectorService;
75         this.notificationProxyParser = vesCollectorService.getNotificationProxyParser();
76
77         this.oRanListenerRegistrationResult = null;
78         this.oRanListener = new ORanChangeNotificationListener(netconfAccessor, databaseService, vesCollectorService,
79                 notificationProxyParser);
80
81         this.oRanFaultListenerRegistrationResult = null;
82         this.oRanFaultListener =
83                 new ORanFaultNotificationListener(netconfAccessor, databaseService, vesCollectorService);
84     }
85
86     private void initialReadFromNetworkElement() {
87         Hardware hardware = readHardware();
88         if (hardware != null) {
89             componentList = YangHelper.getCollection(hardware.nonnullComponent());
90             List<Inventory> inventoryList =
91                     ORanToInternalDataModel.getInventoryList(netconfAccessor.getNodeId(), componentList);
92             inventoryList.forEach(databaseService::writeInventory);
93         }
94
95         Optional<Guicutthrough> oGuicutthrough = ORanToInternalDataModel.getGuicutthrough(getOnapSystemData());
96         if (oGuicutthrough.isPresent()) {
97             databaseService.writeGuiCutThroughData(oGuicutthrough.get(), netconfAccessor.getNodeId().getValue());
98         }
99     }
100
101     @Override
102     public NetworkElementDeviceType getDeviceType() {
103         return NetworkElementDeviceType.ORAN;
104     }
105
106     @Override
107     public void register() {
108         initialReadFromNetworkElement();
109         // Register call back class for receiving notifications
110         Optional<NetconfNotifications> oNotifications = netconfAccessor.getNotificationAccessor();
111         if (oNotifications.isPresent()) {
112             NetconfNotifications notifications = oNotifications.get();
113             this.oRanListenerRegistrationResult = netconfAccessor.doRegisterNotificationListener(oRanListener);
114             this.oRanFaultListenerRegistrationResult =
115                     netconfAccessor.doRegisterNotificationListener(oRanFaultListener);
116             // Register notifications stream
117             if (notifications.isNCNotificationsSupported()) {
118                 List<Stream> streamList = notifications.getNotificationStreams();
119                 notifications.registerNotificationsStream(NetconfBindingAccessor.DefaultNotificationsStream); // Always register first to default stream
120                 notifications.registerNotificationsStream(streamList);
121             } else {
122                 notifications.registerNotificationsStream(NetconfBindingAccessor.DefaultNotificationsStream);
123             }
124         }
125     }
126
127     @Override
128     public void deregister() {
129         if (oRanListenerRegistrationResult != null) {
130             this.oRanListenerRegistrationResult.close();
131         }
132         if (oRanFaultListenerRegistrationResult != null) {
133             this.oRanFaultListenerRegistrationResult.close();
134         } ;
135     }
136
137
138     @Override
139     public NodeId getNodeId() {
140         return netconfAccessor.getNodeId();
141     }
142
143     @Override
144     public <L extends NetworkElementService> Optional<L> getService(Class<L> clazz) {
145         return Optional.empty();
146     }
147
148     @Override
149     public void warmstart() {}
150
151     @Override
152     public Optional<NetconfAccessor> getAcessor() {
153         return Optional.of(netconfAccessor);
154     }
155
156     // Read from device
157     private System1 getOnapSystemData() {
158         log.info("Get System1 for class {} from mountpoint {}", netconfAccessor.getNodeId().getValue());
159
160         InstanceIdentifier<System1> system1IID = InstanceIdentifier
161                 .builder(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.system.rev140806.System.class)
162                 .augmentation(System1.class).build();
163         System1 res = netconfAccessor.getTransactionUtils().readData(netconfAccessor.getDataBroker(),
164                 LogicalDatastoreType.OPERATIONAL, system1IID);
165         log.debug("Result of System1 = {}", res);
166         return res;
167     }
168
169     private Hardware readHardware() {
170         final Class<Hardware> clazzPac = Hardware.class;
171         log.info("DBRead Get hardware for class {} from mountpoint {}", clazzPac.getSimpleName(),
172                 netconfAccessor.getNodeId().getValue());
173         InstanceIdentifier<Hardware> hardwareIID = InstanceIdentifier.builder(clazzPac).build();
174         Hardware res = netconfAccessor.getTransactionUtils().readData(netconfAccessor.getDataBroker(),
175                 LogicalDatastoreType.OPERATIONAL, hardwareIID);
176         log.debug("Result of Hardware = {}", res);
177         return res;
178     }
179  
180 }