e9c5f7ab097504fe52a8192adc591dfc72c44fbb
[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
19 package org.onap.ccsdk.features.sdnr.wt.devicemanager.impl;
20
21 import java.util.List;
22 import java.util.Objects;
23 import java.util.Optional;
24 import java.util.concurrent.ConcurrentHashMap;
25 import javax.annotation.concurrent.GuardedBy;
26 import org.eclipse.jdt.annotation.NonNull;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.onap.ccsdk.features.sdnr.wt.devicemanager.devicemonitor.impl.DeviceMonitor;
29 import org.onap.ccsdk.features.sdnr.wt.devicemanager.eventdatahandler.ODLEventListenerHandler;
30 import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.factory.NetworkElementFactory;
31 import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.service.NetworkElement;
32 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.DeviceManagerServiceProvider;
33 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfAccessor;
34 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfNodeConnectListener;
35 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfNodeStateService;
36 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev221225.ConnectionOper.ConnectionStatus;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev221225.NetconfNode;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
40 import org.opendaylight.yangtools.concepts.ListenerRegistration;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 public class DeviceManagerNetconfConnectHandler extends DeviceManagerNetconfNotConnectHandler
45         implements NetconfNodeConnectListener {
46
47     private static final Logger LOG = LoggerFactory.getLogger(DeviceManagerNetconfConnectHandler.class);
48
49     private final Object networkelementLock;
50     /** Contains all connected devices */
51     @GuardedBy("networkelementLock")
52     private final ConcurrentHashMap<String, NetworkElement> connectedNetworkElementRepresentations;
53
54     private final @NonNull ListenerRegistration<DeviceManagerNetconfConnectHandler> registerNetconfNodeConnectListener;
55
56     public DeviceManagerNetconfConnectHandler(@NonNull NetconfNodeStateService netconfNodeStateService,
57             @NonNull ClusterSingletonServiceProvider clusterSingletonServiceProvider,
58             @NonNull ODLEventListenerHandler odlEventListenerHandler, @NonNull DeviceMonitor deviceMonitor,
59             @NonNull DeviceManagerServiceProvider serviceProvider, @NonNull List<NetworkElementFactory> factoryList) {
60
61         super(netconfNodeStateService, clusterSingletonServiceProvider, odlEventListenerHandler, deviceMonitor,
62                 serviceProvider, factoryList);
63
64         this.networkelementLock = new Object();
65         this.connectedNetworkElementRepresentations = new ConcurrentHashMap<>();
66
67         this.registerNetconfNodeConnectListener = netconfNodeStateService.registerNetconfNodeConnectListener(this);
68     }
69
70     @Override
71     public void onEnterConnected(@NonNull NetconfAccessor acessor) {
72         //@NonNull NodeId nNodeId, @NonNull NetconfNode netconfNode,
73         //@NonNull MountPoint mountPoint, @NonNull DataBroker netconfNodeDataBroker
74         String mountPointNodeName = acessor.getNodeId().getValue();
75         LOG.debug("onEnterConnected - starting Event listener on Netconf for mountpoint {}", mountPointNodeName);
76
77         LOG.debug("Master mountpoint {}", mountPointNodeName);
78
79         // It is master for mountpoint and all data are available.
80         // Make sure that specific mountPointNodeName is handled only once.
81         // be aware that startListenerOnNodeForConnectedState could be called multiple
82         // times for same mountPointNodeName.
83         // networkElementRepresentations contains handled NEs at master node.
84
85         if (isInNetworkElementRepresentations(mountPointNodeName)) {
86             LOG.warn("Mountpoint {} already registered. Leave startup procedure.", mountPointNodeName);
87             return;
88         }
89         // update db with connect status
90         NetconfNode netconfNode = acessor.getNetconfNode();
91         sendUpdateNotification(acessor.getNodeId(), netconfNode.getConnectionStatus(), netconfNode);
92         // Start devicemanager if possible
93         Optional<NetworkElement> optionalNe = createNetworkElement(acessor);
94         // Startup device
95         if (optionalNe.isPresent()) {
96             handleNeStartup(acessor.getNodeId(), optionalNe.get());
97         }
98     }
99
100     @Override
101     public void onLeaveConnected(@NonNull NodeId nNodeId, @NonNull Optional<NetconfNode> optionalNetconfNode) {
102
103         LOG.debug("onLeaveConnected {}", nNodeId);
104         String mountPointNodeName = nNodeId.getValue();
105
106         if (optionalNetconfNode.isPresent()) {
107             NetconfNode nNode = optionalNetconfNode.get();
108             ConnectionStatus csts = nNode.getConnectionStatus();
109             sendUpdateNotification(nNodeId, csts, nNode);
110         }
111
112         // Handling if mountpoint exist. connected -> connecting/UnableToConnect
113         stopListenerOnNodeForConnectedState(mountPointNodeName);
114         if (isDeviceMonitorEnabled()) {
115             getDeviceMonitor().deviceDisconnectIndication(mountPointNodeName);
116         }
117     }
118
119     @Override
120     public void close() {
121         if (Objects.nonNull(registerNetconfNodeConnectListener)) {
122             registerNetconfNodeConnectListener.close();
123         }
124         super.close();
125     }
126
127     public @Nullable NetworkElement getConnectedNeByMountpoint(String mountpoint) {
128         return this.connectedNetworkElementRepresentations.get(mountpoint);
129     }
130
131     /*--------------------------------------------
132      * Private functions
133      */
134
135     /**
136      * Get the NetworkElement from list
137      *
138      * @param accessor
139      * @return Optional<NetowrkElement>
140      */
141     private Optional<NetworkElement> createNetworkElement(NetconfAccessor accessor) {
142         Optional<NetworkElement> optionalNe = Optional.empty();
143         for (NetworkElementFactory f : getFactoryList()) {
144             optionalNe = f.create(accessor, getServiceProvider());
145             if (optionalNe.isPresent()) {
146                 return optionalNe; // Use the first provided
147             }
148         }
149         return Optional.empty();
150     }
151
152     /**
153      * Do all tasks necessary to move from mountpoint state connected -> connecting
154      *
155      * @param mountPointNodeName provided
156      */
157     private void stopListenerOnNodeForConnectedState(String mountPointNodeName) {
158         NetworkElement ne = connectedNetworkElementRepresentations.remove(mountPointNodeName);
159         if (ne != null) {
160             ne.deregister();
161         }
162     }
163
164     private boolean isInNetworkElementRepresentations(String mountPointNodeName) {
165         synchronized (networkelementLock) {
166             return connectedNetworkElementRepresentations.contains(mountPointNodeName);
167         }
168     }
169
170
171     private void handleNeStartup(NodeId nodeId, NetworkElement inNe) {
172
173         LOG.debug("NE Management for {} with {}", nodeId.getValue(), inNe.getClass().getName());
174         NetworkElement result;
175         synchronized (networkelementLock) {
176             result = connectedNetworkElementRepresentations.put(nodeId.getValue(), inNe);
177         }
178         if (result != null) {
179             LOG.warn("NE list was not empty as expected, but contained {} ", result.getNodeId());
180         } else {
181             LOG.debug("refresh necon entry for {} with type {}", nodeId.getValue(), inNe.getDeviceType());
182             if (isOdlEventListenerHandlerEnabled()) {
183                 getOdlEventListenerHandler().connectIndication(nodeId, inNe.getDeviceType());
184             }
185         }
186         if (isDeviceMonitorEnabled()) {
187             getDeviceMonitor().deviceConnectMasterIndication(nodeId.getValue(), inNe);
188         }
189
190         inNe.register();
191     }
192
193     private void sendUpdateNotification(NodeId nodeId, ConnectionStatus csts, NetconfNode nNode) {
194         LOG.debug("update ConnectedState for device :: Name : {} ConnectionStatus {}", nodeId.getValue(), csts);
195         if (isOdlEventListenerHandlerEnabled()) {
196             getOdlEventListenerHandler().updateRegistration(nodeId, ConnectionStatus.class.getSimpleName(),
197                     csts != null ? csts.getName() : "null", nNode);
198         }
199     }
200
201 }