5f9911ba87b4d4159928f625374e1bdd85aefc94
[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,
60             @NonNull List<NetworkElementFactory> factoryList) {
61
62         HtAssert.nonnull(netconfNodeStateService, this.odlEventListenerHandler = odlEventListenerHandler,
63                 this.deviceMonitor = deviceMonitor, this.serviceProvider = serviceProvider,
64                 this.factoryList = factoryList);
65
66         this.networkelementLock = new Object();
67         this.networkElementRepresentations = new ConcurrentHashMap<>();
68
69         this.registerNetconfNodeConnectListener = netconfNodeStateService.registerNetconfNodeConnectListener(this);
70         this.registerNetconfNodeStateListener = netconfNodeStateService.registerNetconfNodeStateListener(this);
71     }
72
73     @Override
74     public void onEnterConnected(@NonNull NetconfAccessor acessor) {
75         //@NonNull NodeId nNodeId, @NonNull NetconfNode netconfNode,
76         //@NonNull MountPoint mountPoint, @NonNull DataBroker netconfNodeDataBroker
77         String mountPointNodeName = acessor.getNodeId().getValue();
78         LOG.info("onEnterConnected - starting Event listener on Netconf for mountpoint {}", mountPointNodeName);
79
80         LOG.info("Master mountpoint {}", mountPointNodeName);
81
82         // It is master for mountpoint and all data are available.
83         // Make sure that specific mountPointNodeName is handled only once.
84         // be aware that startListenerOnNodeForConnectedState could be called multiple
85         // times for same mountPointNodeName.
86         // networkElementRepresentations contains handled NEs at master node.
87
88         synchronized (networkelementLock) {
89             if (networkElementRepresentations.containsKey(mountPointNodeName)) {
90                 LOG.warn("Mountpoint {} already registered. Leave startup procedure.", mountPointNodeName);
91                 return;
92             }
93         }
94         // update db with connect status
95         NetconfNode netconfNode = acessor.getNetconfNode();
96         sendUpdateNotification(mountPointNodeName, netconfNode.getConnectionStatus(), netconfNode);
97
98         for ( NetworkElementFactory f : factoryList) {
99             Optional<NetworkElement> optionalNe = f.create(acessor, serviceProvider);
100             if (optionalNe.isPresent()) {
101                 // sendUpdateNotification(mountPointNodeName, nNode.getConnectionStatus(), nNode);
102                 NetworkElement inNe = optionalNe.get();
103                 LOG.info("NE Management for {} with {}", mountPointNodeName, inNe.getClass().getName());
104                 putToNetworkElementRepresentations(mountPointNodeName, inNe);
105                 deviceMonitor.deviceConnectMasterIndication(mountPointNodeName, inNe);
106
107                 inNe.register();
108                 break; // Use the first provided
109             }
110         }
111     }
112
113     @Override
114     public void onLeaveConnected(@NonNull NodeId nNodeId, @NonNull Optional<NetconfNode> optionalNetconfNode) {
115
116         LOG.info("onLeaveConnected {}", nNodeId);
117         String mountPointNodeName = nNodeId.getValue();
118
119         if (optionalNetconfNode.isPresent()) {
120             NetconfNode nNode = optionalNetconfNode.get();
121             ConnectionStatus csts = nNode.getConnectionStatus();
122             sendUpdateNotification(mountPointNodeName, csts, nNode);
123         }
124
125         // Handling if mountpoint exist. connected -> connecting/UnableToConnect
126         stopListenerOnNodeForConnectedState(mountPointNodeName);
127         deviceMonitor.deviceDisconnectIndication(mountPointNodeName);
128     }
129
130     @Override
131     public void onCreated(NodeId nNodeId, NetconfNode netconfNode) {
132         LOG.info("onCreated {}", nNodeId);
133         odlEventListenerHandler.mountpointCreatedIndication(nNodeId.getValue(), netconfNode);
134         
135     }
136
137     @Override
138     public void onStateChange(NodeId nNodeId, NetconfNode netconfNode) {
139         LOG.info("onStateChange {}", nNodeId);
140         odlEventListenerHandler.onStateChangeIndication(nNodeId.getValue(),netconfNode);
141     }
142
143     @Override
144     public void onRemoved(NodeId nNodeId) {
145         String mountPointNodeName = nNodeId.getValue();
146         LOG.info("mountpointNodeRemoved {}", nNodeId.getValue());
147
148         stopListenerOnNodeForConnectedState(mountPointNodeName);
149         deviceMonitor.removeMountpointIndication(mountPointNodeName);
150         odlEventListenerHandler.deRegistration(mountPointNodeName); //Additional indication for log
151     }
152
153     @Override
154     public void close() {
155         if (registerNetconfNodeConnectListener != null) {
156             registerNetconfNodeConnectListener.close();
157         }
158         if (registerNetconfNodeStateListener != null) {
159             registerNetconfNodeStateListener.close();
160         }
161     }
162
163     /*--------------------------------------------
164      * Private functions
165      */
166
167     /**
168      * Do all tasks necessary to move from mountpoint state connected -> connecting
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 }