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.node.topology.rev150114.NetconfNode;
 
  38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus;
 
  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.info("onEnterConnected - starting Event listener on Netconf for mountpoint {}", mountPointNodeName);
 
  77         LOG.info("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);
 
  93         for (NetworkElementFactory f : getFactoryList()) {
 
  94             Optional<NetworkElement> optionalNe = f.create(acessor, getServiceProvider());
 
  95             if (optionalNe.isPresent()) {
 
  96                 // sendUpdateNotification(mountPointNodeName, nNode.getConnectionStatus(), nNode);
 
  97                 handleNeStartup(acessor.getNodeId(), optionalNe.get());
 
  98                 break; // Use the first provided
 
 104     public void onLeaveConnected(@NonNull NodeId nNodeId, @NonNull Optional<NetconfNode> optionalNetconfNode) {
 
 106         LOG.info("onLeaveConnected {}", nNodeId);
 
 107         String mountPointNodeName = nNodeId.getValue();
 
 109         if (optionalNetconfNode.isPresent()) {
 
 110             NetconfNode nNode = optionalNetconfNode.get();
 
 111             ConnectionStatus csts = nNode.getConnectionStatus();
 
 112             sendUpdateNotification(nNodeId, csts, nNode);
 
 115         // Handling if mountpoint exist. connected -> connecting/UnableToConnect
 
 116         stopListenerOnNodeForConnectedState(mountPointNodeName);
 
 117         if (isDeviceMonitorEnabled()) {
 
 118             getDeviceMonitor().deviceDisconnectIndication(mountPointNodeName);
 
 123     public void close() {
 
 124         if (Objects.nonNull(registerNetconfNodeConnectListener)) {
 
 125             registerNetconfNodeConnectListener.close();
 
 130     public @Nullable NetworkElement getConnectedNeByMountpoint(String mountpoint) {
 
 131         return this.connectedNetworkElementRepresentations.get(mountpoint);
 
 134     /*--------------------------------------------
 
 139      * Do all tasks necessary to move from mountpoint state connected -> connecting
 
 141      * @param mountPointNodeName provided
 
 143     private void stopListenerOnNodeForConnectedState(String mountPointNodeName) {
 
 144         NetworkElement ne = connectedNetworkElementRepresentations.remove(mountPointNodeName);
 
 150     private boolean isInNetworkElementRepresentations(String mountPointNodeName) {
 
 151         synchronized (networkelementLock) {
 
 152             return connectedNetworkElementRepresentations.contains(mountPointNodeName);
 
 157     private void handleNeStartup(NodeId nodeId, NetworkElement inNe) {
 
 159         LOG.info("NE Management for {} with {}", nodeId.getValue(), inNe.getClass().getName());
 
 160         NetworkElement result;
 
 161         synchronized (networkelementLock) {
 
 162             result = connectedNetworkElementRepresentations.put(nodeId.getValue(), inNe);
 
 164         if (result != null) {
 
 165             LOG.warn("NE list was not empty as expected, but contained {} ", result.getNodeId());
 
 167             LOG.debug("refresh necon entry for {} with type {}", nodeId.getValue(), inNe.getDeviceType());
 
 168             if (isOdlEventListenerHandlerEnabled()) {
 
 169                 getOdlEventListenerHandler().connectIndication(nodeId, inNe.getDeviceType());
 
 172         if (isDeviceMonitorEnabled()) {
 
 173             getDeviceMonitor().deviceConnectMasterIndication(nodeId.getValue(), inNe);
 
 179     private void sendUpdateNotification(NodeId nodeId, ConnectionStatus csts, NetconfNode nNode) {
 
 180         LOG.info("update ConnectedState for device :: Name : {} ConnectionStatus {}", nodeId.getValue(), csts);
 
 181         if (isOdlEventListenerHandlerEnabled()) {
 
 182             getOdlEventListenerHandler().updateRegistration(nodeId, ConnectionStatus.class.getSimpleName(),
 
 183                     csts != null ? csts.getName() : "null", nNode);