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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
16 * ============LICENSE_END==========================================================================
17 ******************************************************************************/
19 package org.onap.ccsdk.features.sdnr.wt.devicemanager.impl;
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;
42 public class DeviceManagerNetconfConnectHandler implements NetconfNodeConnectListener, NetconfNodeStateListener {
44 private static final Logger LOG = LoggerFactory.getLogger(DeviceManagerNetconfConnectHandler.class);
46 private final @NonNull ListenerRegistration<DeviceManagerNetconfConnectHandler> registerNetconfNodeConnectListener;
47 private final @NonNull ListenerRegistration<NetconfNodeStateListener> registerNetconfNodeStateListener;
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;
54 private final Object networkelementLock;
55 private final ConcurrentHashMap<String, NetworkElement> networkElementRepresentations;
57 public DeviceManagerNetconfConnectHandler(@NonNull NetconfNodeStateService netconfNodeStateService,
58 @NonNull ODLEventListenerHandler odlEventListenerHandler, @NonNull DeviceMonitor deviceMonitor,
59 @NonNull DeviceManagerServiceProvider serviceProvider,
60 @NonNull List<MyNetworkElementFactory<? extends NetworkElementFactory>> factoryList) {
62 HtAssert.nonnull(netconfNodeStateService, this.odlEventListenerHandler = odlEventListenerHandler,
63 this.deviceMonitor = deviceMonitor, this.serviceProvider = serviceProvider,
64 this.factoryList = factoryList);
66 this.networkelementLock = new Object();
67 this.networkElementRepresentations = new ConcurrentHashMap<>();
69 this.registerNetconfNodeConnectListener = netconfNodeStateService.registerNetconfNodeConnectListener(this);
70 this.registerNetconfNodeStateListener = netconfNodeStateService.registerNetconfNodeStateListener(this);
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);
80 LOG.info("Master mountpoint {}", mountPointNodeName);
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.
88 synchronized (networkelementLock) {
89 if (networkElementRepresentations.containsKey(mountPointNodeName)) {
90 LOG.warn("Mountpoint {} already registered. Leave startup procedure.", mountPointNodeName);
94 // update db with connect status
95 NetconfNode netconfNode = acessor.getNetconfNode();
96 sendUpdateNotification(mountPointNodeName, netconfNode.getConnectionStatus(), netconfNode);
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);
108 break; // Use the first provided
114 public void onLeaveConnected(@NonNull NodeId nNodeId, @NonNull Optional<NetconfNode> optionalNetconfNode) {
116 LOG.info("onLeaveConnected {}", nNodeId);
117 String mountPointNodeName = nNodeId.getValue();
119 if (optionalNetconfNode.isPresent()) {
120 NetconfNode nNode = optionalNetconfNode.get();
121 ConnectionStatus csts = nNode.getConnectionStatus();
122 sendUpdateNotification(mountPointNodeName, csts, nNode);
125 // Handling if mountpoint exist. connected -> connecting/UnableToConnect
126 stopListenerOnNodeForConnectedState(mountPointNodeName);
127 deviceMonitor.deviceDisconnectIndication(mountPointNodeName);
131 public void onCreated(NodeId nNodeId, NetconfNode netconfNode) {
132 LOG.info("onCreated {}", nNodeId);
136 public void onStateChange(NodeId nNodeId, NetconfNode netconfNode) {
137 LOG.info("onStateChange {}", nNodeId);
141 public void onRemoved(NodeId nNodeId) {
142 String mountPointNodeName = nNodeId.getValue();
143 LOG.info("mountpointNodeRemoved {}", nNodeId.getValue());
145 stopListenerOnNodeForConnectedState(mountPointNodeName);
146 deviceMonitor.removeMountpointIndication(mountPointNodeName);
147 odlEventListenerHandler.deRegistration(mountPointNodeName); //Additional indication for log
151 public void close() {
152 registerNetconfNodeConnectListener.close();
153 registerNetconfNodeStateListener.close();
156 /*--------------------------------------------
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
165 private void stopListenerOnNodeForConnectedState( String mountPointNodeName) {
166 NetworkElement ne = networkElementRepresentations.remove(mountPointNodeName);
172 private void putToNetworkElementRepresentations(String mountPointNodeName, NetworkElement ne) {
173 NetworkElement result;
174 synchronized (networkelementLock) {
175 result = networkElementRepresentations.put(mountPointNodeName, ne);
177 if (result != null) {
178 LOG.warn("NE list was not empty as expected, but contained {} ", result.getNodeId());
180 odlEventListenerHandler.connectIndication(mountPointNodeName, ne.getDeviceType());
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);