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