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==========================================================================
19 package org.onap.ccsdk.features.sdnr.wt.devicemanager.impl;
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.device.rev221225.ConnectionOper.ConnectionStatus;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev221225.NetconfNode;
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;
44 public class DeviceManagerNetconfConnectHandler extends DeviceManagerNetconfNotConnectHandler
45 implements NetconfNodeConnectListener {
47 private static final Logger LOG = LoggerFactory.getLogger(DeviceManagerNetconfConnectHandler.class);
49 private final Object networkelementLock;
50 /** Contains all connected devices */
51 @GuardedBy("networkelementLock")
52 private final ConcurrentHashMap<String, NetworkElement> connectedNetworkElementRepresentations;
54 private final @NonNull ListenerRegistration<DeviceManagerNetconfConnectHandler> registerNetconfNodeConnectListener;
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) {
61 super(netconfNodeStateService, clusterSingletonServiceProvider, odlEventListenerHandler, deviceMonitor,
62 serviceProvider, factoryList);
64 this.networkelementLock = new Object();
65 this.connectedNetworkElementRepresentations = new ConcurrentHashMap<>();
67 this.registerNetconfNodeConnectListener = netconfNodeStateService.registerNetconfNodeConnectListener(this);
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.debug("onEnterConnected - starting Event listener on Netconf for mountpoint {}", mountPointNodeName);
77 LOG.debug("Master mountpoint {}", mountPointNodeName);
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.
85 if (isInNetworkElementRepresentations(mountPointNodeName)) {
86 LOG.warn("Mountpoint {} already registered. Leave startup procedure.", mountPointNodeName);
89 // update db with connect status
90 NetconfNode netconfNode = acessor.getNetconfNode();
91 sendUpdateNotification(acessor.getNodeId(), netconfNode.getConnectionStatus(), netconfNode);
92 // Start devicemanager if possible
93 Optional<NetworkElement> optionalNe = createNetworkElement(acessor);
95 if (optionalNe.isPresent()) {
96 handleNeStartup(acessor.getNodeId(), optionalNe.get());
101 public void onLeaveConnected(@NonNull NodeId nNodeId, @NonNull Optional<NetconfNode> optionalNetconfNode) {
103 LOG.debug("onLeaveConnected {}", nNodeId);
104 String mountPointNodeName = nNodeId.getValue();
106 if (optionalNetconfNode.isPresent()) {
107 NetconfNode nNode = optionalNetconfNode.get();
108 ConnectionStatus csts = nNode.getConnectionStatus();
109 sendUpdateNotification(nNodeId, csts, nNode);
112 // Handling if mountpoint exist. connected -> connecting/UnableToConnect
113 stopListenerOnNodeForConnectedState(mountPointNodeName);
114 if (isDeviceMonitorEnabled()) {
115 getDeviceMonitor().deviceDisconnectIndication(mountPointNodeName);
120 public void close() {
121 if (Objects.nonNull(registerNetconfNodeConnectListener)) {
122 registerNetconfNodeConnectListener.close();
127 public @Nullable NetworkElement getConnectedNeByMountpoint(String mountpoint) {
128 return this.connectedNetworkElementRepresentations.get(mountpoint);
131 /*--------------------------------------------
136 * Get the NetworkElement from list
139 * @return Optional<NetowrkElement>
141 private Optional<NetworkElement> createNetworkElement(NetconfAccessor accessor) {
142 Optional<NetworkElement> optionalNe = Optional.empty();
143 for (NetworkElementFactory f : getFactoryList()) {
144 optionalNe = f.create(accessor, getServiceProvider());
145 if (optionalNe.isPresent()) {
146 return optionalNe; // Use the first provided
149 return Optional.empty();
153 * Do all tasks necessary to move from mountpoint state connected -> connecting
155 * @param mountPointNodeName provided
157 private void stopListenerOnNodeForConnectedState(String mountPointNodeName) {
158 NetworkElement ne = connectedNetworkElementRepresentations.remove(mountPointNodeName);
164 private boolean isInNetworkElementRepresentations(String mountPointNodeName) {
165 synchronized (networkelementLock) {
166 return connectedNetworkElementRepresentations.contains(mountPointNodeName);
171 private void handleNeStartup(NodeId nodeId, NetworkElement inNe) {
173 LOG.debug("NE Management for {} with {}", nodeId.getValue(), inNe.getClass().getName());
174 NetworkElement result;
175 synchronized (networkelementLock) {
176 result = connectedNetworkElementRepresentations.put(nodeId.getValue(), inNe);
178 if (result != null) {
179 LOG.warn("NE list was not empty as expected, but contained {} ", result.getNodeId());
181 LOG.debug("refresh necon entry for {} with type {}", nodeId.getValue(), inNe.getDeviceType());
182 if (isOdlEventListenerHandlerEnabled()) {
183 getOdlEventListenerHandler().connectIndication(nodeId, inNe.getDeviceType());
186 if (isDeviceMonitorEnabled()) {
187 getDeviceMonitor().deviceConnectMasterIndication(nodeId.getValue(), inNe);
193 private void sendUpdateNotification(NodeId nodeId, ConnectionStatus csts, NetconfNode nNode) {
194 LOG.debug("update ConnectedState for device :: Name : {} ConnectionStatus {}", nodeId.getValue(), csts);
195 if (isOdlEventListenerHandlerEnabled()) {
196 getOdlEventListenerHandler().updateRegistration(nodeId, ConnectionStatus.class.getSimpleName(),
197 csts != null ? csts.getName() : "null", nNode);