Reorganization of devicemanager directory structure
[ccsdk/features.git] / sdnr / wt / devicemanager-core / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / impl / DeviceManagerNetconfConnectHandler.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
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.node.topology.rev150114.NetconfNode;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus;
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.info("onEnterConnected - starting Event listener on Netconf for mountpoint {}", mountPointNodeName);
76
77         LOG.info("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(mountPointNodeName, netconfNode.getConnectionStatus(), netconfNode);
92
93         for (NetworkElementFactory f : getFactoryList()) {
94             Optional<NetworkElement> optionalNe = f.create(acessor, getServiceProvider());
95             if (optionalNe.isPresent()) {
96                 // sendUpdateNotification(mountPointNodeName, nNode.getConnectionStatus(), nNode);
97                 handleNeStartup(mountPointNodeName, optionalNe.get());
98                 break; // Use the first provided
99             }
100         }
101     }
102
103     @Override
104     public void onLeaveConnected(@NonNull NodeId nNodeId, @NonNull Optional<NetconfNode> optionalNetconfNode) {
105
106         LOG.info("onLeaveConnected {}", nNodeId);
107         String mountPointNodeName = nNodeId.getValue();
108
109         if (optionalNetconfNode.isPresent()) {
110             NetconfNode nNode = optionalNetconfNode.get();
111             ConnectionStatus csts = nNode.getConnectionStatus();
112             sendUpdateNotification(mountPointNodeName, csts, nNode);
113         }
114
115         // Handling if mountpoint exist. connected -> connecting/UnableToConnect
116         stopListenerOnNodeForConnectedState(mountPointNodeName);
117         if (isDeviceMonitorEnabled()) {
118             getDeviceMonitor().deviceDisconnectIndication(mountPointNodeName);
119         }
120     }
121
122     @Override
123     public void close() {
124         if (Objects.nonNull(registerNetconfNodeConnectListener)) {
125             registerNetconfNodeConnectListener.close();
126         }
127         super.close();
128     }
129
130     public @Nullable NetworkElement getConnectedNeByMountpoint(String mountpoint) {
131         return this.connectedNetworkElementRepresentations.get(mountpoint);
132     }
133
134     /*--------------------------------------------
135      * Private functions
136      */
137
138     /**
139      * Do all tasks necessary to move from mountpoint state connected -> connecting
140      * 
141      * @param mountPointNodeName provided
142      */
143     private void stopListenerOnNodeForConnectedState(String mountPointNodeName) {
144         NetworkElement ne = connectedNetworkElementRepresentations.remove(mountPointNodeName);
145         if (ne != null) {
146             ne.deregister();
147         }
148     }
149
150     private boolean isInNetworkElementRepresentations(String mountPointNodeName) {
151         synchronized (networkelementLock) {
152             return connectedNetworkElementRepresentations.contains(mountPointNodeName);
153         }
154     }
155
156
157     private void handleNeStartup(String mountPointNodeName, NetworkElement inNe) {
158
159         LOG.info("NE Management for {} with {}", mountPointNodeName, inNe.getClass().getName());
160         NetworkElement result;
161         synchronized (networkelementLock) {
162             result = connectedNetworkElementRepresentations.put(mountPointNodeName, inNe);
163         }
164         if (result != null) {
165             LOG.warn("NE list was not empty as expected, but contained {} ", result.getNodeId());
166         } else {
167             LOG.debug("refresh necon entry for {} with type {}", mountPointNodeName, inNe.getDeviceType());
168             if (isOdlEventListenerHandlerEnabled()) {
169                 getOdlEventListenerHandler().connectIndication(mountPointNodeName, inNe.getDeviceType());
170             }
171         }
172         if (isDeviceMonitorEnabled()) {
173             getDeviceMonitor().deviceConnectMasterIndication(mountPointNodeName, inNe);
174         }
175
176         inNe.register();
177     }
178
179     private void sendUpdateNotification(String mountPointNodeName, ConnectionStatus csts, NetconfNode nNode) {
180         LOG.info("update ConnectedState for device :: Name : {} ConnectionStatus {}", mountPointNodeName, csts);
181         if (isOdlEventListenerHandlerEnabled()) {
182             getOdlEventListenerHandler().updateRegistration(mountPointNodeName, ConnectionStatus.class.getSimpleName(),
183                     csts != null ? csts.getName() : "null", nNode);
184         }
185     }
186
187 }