09978c2e8f5e2f5b1222dce657a41df5328f7769
[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 java.util.Collection;
21 import java.util.List;
22 import java.util.Optional;
23 import org.eclipse.jdt.annotation.NonNull;
24 import org.onap.ccsdk.features.sdnr.wt.common.YangHelper;
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.netconfnodestateservice.NetconfAccessor;
29 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.hardware.rev180313.Hardware;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.hardware.rev180313.hardware.Component;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
33 import org.opendaylight.yang.gen.v1.urn.onap.system.rev201026.System1;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.NetworkElementDeviceType;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.GuicutthroughBuilder;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
37 import org.opendaylight.yangtools.concepts.ListenerRegistration;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.opendaylight.yangtools.yang.binding.NotificationListener;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  */
45 public class ORanNetworkElement implements NetworkElement {
46
47     private static final Logger log = LoggerFactory.getLogger(ORanNetworkElement.class);
48
49     private final NetconfAccessor netconfAccessor;
50
51     private final DataProvider databaseService;
52
53     private final ORanToInternalDataModel oRanMapper;
54
55     private ListenerRegistration<NotificationListener> oRanListenerRegistrationResult;
56     private @NonNull final ORanChangeNotificationListener oRanListener;
57     private ListenerRegistration<NotificationListener> oRanFaultListenerRegistrationResult;
58     private @NonNull final ORanFaultNotificationListener oRanFaultListener;
59
60     ORanNetworkElement(NetconfAccessor netconfAccess, DataProvider databaseService) {
61         log.info("Create {}", ORanNetworkElement.class.getSimpleName());
62         this.netconfAccessor = netconfAccess;
63         this.databaseService = databaseService;
64
65         this.oRanListenerRegistrationResult = null;
66         this.oRanListener = new ORanChangeNotificationListener(netconfAccessor, databaseService);
67
68         this.oRanFaultListenerRegistrationResult = null;
69         this.oRanFaultListener = new ORanFaultNotificationListener();
70
71         this.oRanMapper = new ORanToInternalDataModel();
72
73     }
74
75     public void initialReadFromNetworkElement() {
76         Hardware hardware = readHardware(netconfAccessor);
77         if (hardware != null) {
78             Collection<Component> componentList = YangHelper.getCollection(hardware.getComponent());
79             if (componentList != null) {
80                 for (Component component : componentList) {
81                     databaseService
82                             .writeInventory(oRanMapper.getInternalEquipment(netconfAccessor.getNodeId(), component));
83                 }
84             }
85         }
86         System1 sys = getOnapSystemData(netconfAccessor);
87         if (sys != null) {
88             GuicutthroughBuilder gcBuilder = new GuicutthroughBuilder();
89             gcBuilder.setId(sys.getName()).setName(sys.getName()).setWeburi(sys.getWebUi().getValue());
90             databaseService.writeGuiCutThroughData(gcBuilder.build());
91         }
92     }
93
94     @Override
95     public NetworkElementDeviceType getDeviceType() {
96         return NetworkElementDeviceType.ORAN;
97     }
98
99     private System1 getOnapSystemData(NetconfAccessor accessData) {
100         InstanceIdentifier<System1> system1IID =
101         InstanceIdentifier.builder(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.system.rev140806.System.class)
102         .augmentation(System1.class).build();
103
104         System1 res = accessData.getTransactionUtils().readData(accessData.getDataBroker(), LogicalDatastoreType.OPERATIONAL, system1IID);
105         log.debug("Result of getOnapSystemData = {}", res);
106         return res;
107     }
108
109     private Hardware readHardware(NetconfAccessor accessData) {
110
111         final Class<Hardware> clazzPac = Hardware.class;
112
113         log.info("DBRead Get equipment for class {} from mountpoint {} for uuid {}", clazzPac.getSimpleName(),
114                 accessData.getNodeId().getValue());
115
116         InstanceIdentifier<Hardware> hardwareIID = InstanceIdentifier.builder(clazzPac).build();
117
118         Hardware res = accessData.getTransactionUtils().readData(accessData.getDataBroker(),
119                 LogicalDatastoreType.OPERATIONAL, hardwareIID);
120
121         return res;
122     }
123
124     @Override
125     public void register() {
126
127         initialReadFromNetworkElement();
128         // Register call back class for receiving notifications
129         this.oRanListenerRegistrationResult = netconfAccessor.doRegisterNotificationListener(oRanListener);
130         this.oRanFaultListenerRegistrationResult = netconfAccessor.doRegisterNotificationListener(oRanFaultListener);
131         // Register netconf stream
132         netconfAccessor.registerNotificationsStream(NetconfAccessor.DefaultNotificationsStream);
133     }
134
135     @Override
136     public void deregister() {
137         if (oRanListenerRegistrationResult != null) {
138             this.oRanListenerRegistrationResult.close();
139         }
140         if (oRanFaultListenerRegistrationResult != null) {
141             this.oRanFaultListenerRegistrationResult.close();
142         } ;
143     }
144
145
146     @Override
147     public NodeId getNodeId() {
148         return netconfAccessor.getNodeId();
149     }
150
151     @Override
152     public <L extends NetworkElementService> Optional<L> getService(Class<L> clazz) {
153         return Optional.empty();
154     }
155
156     @Override
157     public void warmstart() {}
158
159     @Override
160     public Optional<NetconfAccessor> getAcessor() {
161         return Optional.of(netconfAccessor);
162     }
163
164 }