Add x-ran devicemanager
[oam.git] / features / devicemanager / x-ran / ru-fh / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / xran / impl / XRanNetworkElement.java
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.xran.impl;
19
20 import java.util.List;
21 import java.util.Optional;
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider;
24 import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.service.NetworkElement;
25 import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.service.NetworkElementService;
26 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfAccessor;
27 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.hardware.rev180313.Hardware;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.hardware.rev180313.hardware.Component;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.NetworkElementDeviceType;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
32 import org.opendaylight.yangtools.concepts.ListenerRegistration;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.opendaylight.yangtools.yang.binding.NotificationListener;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  */
40 public class XRanNetworkElement implements NetworkElement {
41
42     private static final Logger log = LoggerFactory.getLogger(XRanNetworkElement.class);
43
44     private final NetconfAccessor netconfAccessor;
45
46     private final DataProvider databaseService;
47
48     private final XRanToInternalDataModel xRanMapper;
49
50     private ListenerRegistration<NotificationListener> xRanListenerRegistrationResult;
51     private @NonNull final XRanChangeNotificationListener xRanListener;
52     private ListenerRegistration<NotificationListener> xRanFaultListenerRegistrationResult;
53     private @NonNull final XRanFaultNotificationListener xRanFaultListener;
54
55     XRanNetworkElement(NetconfAccessor netconfAccess, DataProvider databaseService) {
56         log.info("Create {}",XRanNetworkElement.class.getSimpleName());
57         this.netconfAccessor = netconfAccess;
58         this.databaseService = databaseService;
59
60         this.xRanListenerRegistrationResult = null;
61         this.xRanListener = new XRanChangeNotificationListener(netconfAccessor, databaseService);
62
63         this.xRanFaultListenerRegistrationResult = null;
64         this.xRanFaultListener = new XRanFaultNotificationListener();
65
66         this.xRanMapper = new XRanToInternalDataModel();
67
68     }
69
70     public void initialReadFromNetworkElement() {
71         Hardware hardware = readHardware(netconfAccessor);
72         if (hardware != null) {
73             List<Component> componentList = hardware.getComponent();
74             if (componentList != null) {
75                 for (Component component : componentList) {
76                     databaseService.writeInventory( xRanMapper.getInternalEquipment(netconfAccessor.getNodeId(), component));
77                 }
78             }
79         }
80     }
81
82     @Override
83     public NetworkElementDeviceType getDeviceType() {
84         return NetworkElementDeviceType.RAN;
85     }
86
87     private Hardware readHardware(NetconfAccessor accessData) {
88
89         final Class<Hardware> clazzPac = Hardware.class;
90
91         log.info("DBRead Get equipment for class {} from mountpoint {} for uuid {}", clazzPac.getSimpleName(),
92                 accessData.getNodeId().getValue());
93
94         InstanceIdentifier<Hardware> hardwareIID =
95                 InstanceIdentifier.builder(clazzPac).build();
96
97         Hardware res = accessData.getTransactionUtils().readData(accessData.getDataBroker(), LogicalDatastoreType.OPERATIONAL,
98                 hardwareIID);
99
100         return res;
101     }
102
103     @Override
104     public void register() {
105
106         initialReadFromNetworkElement();
107         // Register call back class for receiving notifications
108         this.xRanListenerRegistrationResult = netconfAccessor.doRegisterNotificationListener(xRanListener);
109         this.xRanFaultListenerRegistrationResult = netconfAccessor.doRegisterNotificationListener(xRanFaultListener);
110         // Register netconf stream
111         netconfAccessor.registerNotificationsStream(NetconfAccessor.DefaultNotificationsStream);
112
113
114     }
115
116     @Override
117     public void deregister() {
118         if (xRanListenerRegistrationResult != null) {
119             this.xRanListenerRegistrationResult.close();
120         }
121         if (xRanFaultListenerRegistrationResult != null) {
122             this.xRanFaultListenerRegistrationResult.close();
123         };
124     }
125
126
127     @Override
128     public NodeId getNodeId() {
129         return netconfAccessor.getNodeId();
130     }
131
132     @Override
133     public <L extends NetworkElementService> Optional<L> getService(Class<L> clazz) {
134         return Optional.empty();
135     }
136
137     @Override
138     public void warmstart() {
139     }
140
141     @Override
142     public Optional<NetconfAccessor> getAcessor() {
143         return Optional.of(netconfAccessor);
144     }
145
146 }