f9bbe1abfeb09f7264a2d92d87f0ab5e23bdb56d
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2020 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.Objects;
23 import org.eclipse.jdt.annotation.NonNull;
24 import org.onap.ccsdk.features.sdnr.wt.common.HtAssert;
25 import org.onap.ccsdk.features.sdnr.wt.devicemanager.devicemonitor.impl.DeviceMonitor;
26 import org.onap.ccsdk.features.sdnr.wt.devicemanager.eventdatahandler.ODLEventListenerHandler;
27 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.util.OdlClusterSingleton;
28 import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.factory.NetworkElementFactory;
29 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.DeviceManagerServiceProvider;
30 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfNodeStateListener;
31 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfNodeStateService;
32 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
35 import org.opendaylight.yangtools.concepts.ListenerRegistration;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class DeviceManagerNetconfNotConnectHandler implements NetconfNodeStateListener {
40
41     private static final Logger LOG = LoggerFactory.getLogger(DeviceManagerNetconfNotConnectHandler.class);
42
43     private final @NonNull ListenerRegistration<NetconfNodeStateListener> registerNetconfNodeStateListener;
44
45     private final @NonNull ODLEventListenerHandler odlEventListenerHandler;
46     private final @NonNull DeviceMonitor deviceMonitor;
47     private final @NonNull List<NetworkElementFactory> factoryList;
48     private final @NonNull DeviceManagerServiceProvider serviceProvider;
49
50
51     private final boolean odlEventListenerHandlerEnabled;
52     private final boolean deviceMonitorEnabled;
53
54     private final OdlClusterSingleton singleton;
55
56     public DeviceManagerNetconfNotConnectHandler(@NonNull NetconfNodeStateService netconfNodeStateService,
57             @NonNull ClusterSingletonServiceProvider clusterSingletonServiceProvider,
58             @NonNull ODLEventListenerHandler odlEventListenerHandler, @NonNull DeviceMonitor deviceMonitor,
59             @NonNull DeviceManagerServiceProvider serviceProvider, @NonNull List<NetworkElementFactory> factoryList) {
60
61         HtAssert.nonnull(netconfNodeStateService, this.odlEventListenerHandler = odlEventListenerHandler,
62                 this.deviceMonitor = deviceMonitor, this.serviceProvider = serviceProvider,
63                 this.factoryList = factoryList, odlEventListenerHandler);
64
65         /* Used for debug purpose */
66         this.odlEventListenerHandlerEnabled = true;
67         this.deviceMonitorEnabled = false;
68
69         this.singleton = new OdlClusterSingleton(clusterSingletonServiceProvider);
70         this.registerNetconfNodeStateListener = netconfNodeStateService.registerNetconfNodeStateListener(this);
71
72     }
73
74     @Override
75     public void onCreated(NodeId nNodeId, NetconfNode netconfNode) {
76         LOG.info("onCreated {}", nNodeId);
77         if (isOdlEventListenerHandlerMaster()) {
78             odlEventListenerHandler.registration(nNodeId, netconfNode);
79         }
80         if (deviceMonitorEnabled) {
81             deviceMonitor.deviceDisconnectIndication(nNodeId.getValue());
82         }
83     }
84
85     @Override
86     public void onStateChange(NodeId nNodeId, NetconfNode netconfNode) {
87         LOG.info("onStateChange {}", nNodeId);
88         if (isOdlEventListenerHandlerMaster()) {
89             odlEventListenerHandler.onStateChangeIndication(nNodeId, netconfNode);
90         }
91     }
92
93     @Override
94     public void onRemoved(NodeId nNodeId) {
95         LOG.info("mountpointNodeRemoved {}", nNodeId.getValue());
96
97         if (deviceMonitorEnabled) {
98             deviceMonitor.removeMountpointIndication(nNodeId.getValue());
99         }
100         if (isOdlEventListenerHandlerMaster()) {
101             odlEventListenerHandler.deRegistration(nNodeId); //Additional indication for log
102         }
103     }
104
105     @Override
106     public void close() {
107         if (Objects.nonNull(registerNetconfNodeStateListener)) {
108             registerNetconfNodeStateListener.close();
109         }
110     }
111
112     /*--------------------------------------------
113      * Private functions
114      */
115
116     private boolean isOdlEventListenerHandlerMaster() {
117         return odlEventListenerHandlerEnabled && singleton.isMaster();
118     }
119
120     protected @NonNull DeviceManagerServiceProvider getServiceProvider() {
121         return serviceProvider;
122     }
123
124     protected @NonNull List<NetworkElementFactory> getFactoryList() {
125         return factoryList;
126     }
127
128
129     protected boolean isDeviceMonitorEnabled() {
130         return deviceMonitorEnabled;
131     }
132
133     protected @NonNull DeviceMonitor getDeviceMonitor() {
134         return deviceMonitor;
135     }
136
137     protected boolean isOdlEventListenerHandlerEnabled() {
138         return odlEventListenerHandlerEnabled;
139     }
140
141     protected @NonNull ODLEventListenerHandler getOdlEventListenerHandler() {
142         return odlEventListenerHandler;
143     }
144
145 }