df833018d2add1558d22296e3cb1768124f7ee8a
[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.getValue(), 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.getValue(), netconfNode);
90         }
91     }
92
93     @Override
94     public void onRemoved(NodeId nNodeId) {
95         String mountPointNodeName = nNodeId.getValue();
96         LOG.info("mountpointNodeRemoved {}", nNodeId.getValue());
97
98         if (deviceMonitorEnabled) {
99             deviceMonitor.removeMountpointIndication(mountPointNodeName);
100         }
101         if (isOdlEventListenerHandlerMaster()) {
102             odlEventListenerHandler.deRegistration(mountPointNodeName); //Additional indication for log
103         }
104     }
105
106     @Override
107     public void close() {
108         if (Objects.nonNull(registerNetconfNodeStateListener)) {
109             registerNetconfNodeStateListener.close();
110         }
111     }
112
113     /*--------------------------------------------
114      * Private functions
115      */
116
117     private boolean isOdlEventListenerHandlerMaster() {
118         return odlEventListenerHandlerEnabled && singleton.isMaster();
119     }
120
121     protected @NonNull DeviceManagerServiceProvider getServiceProvider() {
122         return serviceProvider;
123     }
124
125     protected @NonNull List<NetworkElementFactory> getFactoryList() {
126         return factoryList;
127     }
128
129
130     protected boolean isDeviceMonitorEnabled() {
131         return deviceMonitorEnabled;
132     }
133
134     protected @NonNull DeviceMonitor getDeviceMonitor() {
135         return deviceMonitor;
136     }
137
138     protected boolean isOdlEventListenerHandlerEnabled() {
139         return odlEventListenerHandlerEnabled;
140     }
141
142     protected @NonNull ODLEventListenerHandler getOdlEventListenerHandler() {
143         return odlEventListenerHandler;
144     }
145
146 }