Fixing Copyright Header from earlier commit
[ccsdk/features.git] / sdnr / wt / devicemanager / 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.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.impl.handler.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<MyNetworkElementFactory<? extends 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<MyNetworkElementFactory<? extends 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 ( MyNetworkElementFactory<? extends NetworkElementFactory> f : factoryList) {
99             Optional<NetworkElement> optionalNe = f.getFactory().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     }
134
135     @Override
136     public void onStateChange(NodeId nNodeId, NetconfNode netconfNode) {
137         LOG.info("onStateChange {}", nNodeId);
138     }
139
140     @Override
141     public void onRemoved(NodeId nNodeId) {
142         String mountPointNodeName = nNodeId.getValue();
143         LOG.info("mountpointNodeRemoved {}", nNodeId.getValue());
144
145         stopListenerOnNodeForConnectedState(mountPointNodeName);
146         deviceMonitor.removeMountpointIndication(mountPointNodeName);
147         odlEventListenerHandler.deRegistration(mountPointNodeName); //Additional indication for log
148     }
149
150     @Override
151     public void close() {
152          registerNetconfNodeConnectListener.close();
153          registerNetconfNodeStateListener.close();
154     }
155
156     /*--------------------------------------------
157      * Private functions
158      */
159
160     /**
161      * Do all tasks necessary to move from mountpoint state connected -> connecting
162      * @param mountPointNodeName provided
163      * @param ne representing the device connected to mountpoint
164      */
165     private void stopListenerOnNodeForConnectedState( String mountPointNodeName) {
166         NetworkElement ne = networkElementRepresentations.remove(mountPointNodeName);
167         if (ne != null) {
168             ne.deregister();
169         }
170     }
171
172     private void putToNetworkElementRepresentations(String mountPointNodeName, NetworkElement ne) {
173         NetworkElement result;
174         synchronized (networkelementLock) {
175             result = networkElementRepresentations.put(mountPointNodeName, ne);
176         }
177         if (result != null) {
178             LOG.warn("NE list was not empty as expected, but contained {} ", result.getNodeId());
179         } else {
180             odlEventListenerHandler.connectIndication(mountPointNodeName, ne.getDeviceType());
181         }
182     }
183
184     private void sendUpdateNotification(String mountPointNodeName, ConnectionStatus csts, NetconfNode nNode) {
185         LOG.info("update ConnectedState for device :: Name : {} ConnectionStatus {}", mountPointNodeName, csts);
186         odlEventListenerHandler.updateRegistration(mountPointNodeName, ConnectionStatus.class.getSimpleName(),
187                     csts != null ? csts.getName() : "null", nNode);
188     }
189
190 }