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.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;
 
  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<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, @NonNull List<NetworkElementFactory> factoryList) {
 
  61         HtAssert.nonnull(netconfNodeStateService, this.odlEventListenerHandler = odlEventListenerHandler,
 
  62                 this.deviceMonitor = deviceMonitor, this.serviceProvider = serviceProvider,
 
  63                 this.factoryList = factoryList);
 
  65         this.networkelementLock = new Object();
 
  66         this.networkElementRepresentations = new ConcurrentHashMap<>();
 
  68         this.registerNetconfNodeConnectListener = netconfNodeStateService.registerNetconfNodeConnectListener(this);
 
  69         this.registerNetconfNodeStateListener = netconfNodeStateService.registerNetconfNodeStateListener(this);
 
  73     public void onEnterConnected(@NonNull NetconfAccessor acessor) {
 
  74         //@NonNull NodeId nNodeId, @NonNull NetconfNode netconfNode,
 
  75         //@NonNull MountPoint mountPoint, @NonNull DataBroker netconfNodeDataBroker
 
  76         String mountPointNodeName = acessor.getNodeId().getValue();
 
  77         LOG.info("onEnterConnected - starting Event listener on Netconf for mountpoint {}", mountPointNodeName);
 
  79         LOG.info("Master mountpoint {}", mountPointNodeName);
 
  81         // It is master for mountpoint and all data are available.
 
  82         // Make sure that specific mountPointNodeName is handled only once.
 
  83         // be aware that startListenerOnNodeForConnectedState could be called multiple
 
  84         // times for same mountPointNodeName.
 
  85         // networkElementRepresentations contains handled NEs at master node.
 
  87         synchronized (networkelementLock) {
 
  88             if (networkElementRepresentations.containsKey(mountPointNodeName)) {
 
  89                 LOG.warn("Mountpoint {} already registered. Leave startup procedure.", mountPointNodeName);
 
  93         // update db with connect status
 
  94         NetconfNode netconfNode = acessor.getNetconfNode();
 
  95         sendUpdateNotification(mountPointNodeName, netconfNode.getConnectionStatus(), netconfNode);
 
  97         for (NetworkElementFactory f : factoryList) {
 
  98             Optional<NetworkElement> optionalNe = f.create(acessor, serviceProvider);
 
  99             if (optionalNe.isPresent()) {
 
 100                 // sendUpdateNotification(mountPointNodeName, nNode.getConnectionStatus(), nNode);
 
 101                 NetworkElement inNe = optionalNe.get();
 
 102                 LOG.info("NE Management for {} with {}", mountPointNodeName, inNe.getClass().getName());
 
 103                 putToNetworkElementRepresentations(mountPointNodeName, inNe);
 
 104                 deviceMonitor.deviceConnectMasterIndication(mountPointNodeName, inNe);
 
 107                 break; // Use the first provided
 
 113     public void onLeaveConnected(@NonNull NodeId nNodeId, @NonNull Optional<NetconfNode> optionalNetconfNode) {
 
 115         LOG.info("onLeaveConnected {}", nNodeId);
 
 116         String mountPointNodeName = nNodeId.getValue();
 
 118         if (optionalNetconfNode.isPresent()) {
 
 119             NetconfNode nNode = optionalNetconfNode.get();
 
 120             ConnectionStatus csts = nNode.getConnectionStatus();
 
 121             sendUpdateNotification(mountPointNodeName, csts, nNode);
 
 124         // Handling if mountpoint exist. connected -> connecting/UnableToConnect
 
 125         stopListenerOnNodeForConnectedState(mountPointNodeName);
 
 126         deviceMonitor.deviceDisconnectIndication(mountPointNodeName);
 
 130     public void onCreated(NodeId nNodeId, NetconfNode netconfNode) {
 
 131         LOG.info("onCreated {}", nNodeId);
 
 132         odlEventListenerHandler.mountpointCreatedIndication(nNodeId.getValue(), netconfNode);
 
 137     public void onStateChange(NodeId nNodeId, NetconfNode netconfNode) {
 
 138         LOG.info("onStateChange {}", nNodeId);
 
 139         odlEventListenerHandler.onStateChangeIndication(nNodeId.getValue(), netconfNode);
 
 143     public void onRemoved(NodeId nNodeId) {
 
 144         String mountPointNodeName = nNodeId.getValue();
 
 145         LOG.info("mountpointNodeRemoved {}", nNodeId.getValue());
 
 147         stopListenerOnNodeForConnectedState(mountPointNodeName);
 
 148         deviceMonitor.removeMountpointIndication(mountPointNodeName);
 
 149         odlEventListenerHandler.deRegistration(mountPointNodeName); //Additional indication for log
 
 153     public void close() {
 
 154         if (registerNetconfNodeConnectListener != null) {
 
 155             registerNetconfNodeConnectListener.close();
 
 157         if (registerNetconfNodeStateListener != null) {
 
 158             registerNetconfNodeStateListener.close();
 
 162     /*--------------------------------------------
 
 167      * Do all tasks necessary to move from mountpoint state connected -> connecting
 
 169      * @param mountPointNodeName provided
 
 170      * @param ne representing the device connected to mountpoint
 
 172     private void stopListenerOnNodeForConnectedState(String mountPointNodeName) {
 
 173         NetworkElement ne = networkElementRepresentations.remove(mountPointNodeName);
 
 179     private void putToNetworkElementRepresentations(String mountPointNodeName, NetworkElement ne) {
 
 180         NetworkElement result;
 
 181         synchronized (networkelementLock) {
 
 182             result = networkElementRepresentations.put(mountPointNodeName, ne);
 
 184         if (result != null) {
 
 185             LOG.warn("NE list was not empty as expected, but contained {} ", result.getNodeId());
 
 187             LOG.debug("refresh necon entry for {} with type {}", mountPointNodeName, ne.getDeviceType());
 
 188             odlEventListenerHandler.connectIndication(mountPointNodeName, ne.getDeviceType());
 
 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);