2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk feature sdnr wt
4 * ================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.ccsdk.features.sdnr.wt.devicemanager.housekeeping;
23 import com.google.common.util.concurrent.FluentFuture;
24 import com.google.common.util.concurrent.Futures;
25 import com.google.common.util.concurrent.ListenableFuture;
27 import java.util.List;
28 import java.util.NoSuchElementException;
29 import java.util.Optional;
30 import java.util.concurrent.ExecutionException;
31 import java.util.concurrent.Executors;
32 import java.util.concurrent.Future;
33 import java.util.concurrent.ScheduledExecutorService;
34 import java.util.concurrent.TimeUnit;
35 import org.eclipse.jdt.annotation.NonNull;
36 import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation;
37 import org.onap.ccsdk.features.sdnr.wt.common.configuration.filechange.IConfigChangedListener;
38 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider;
39 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.NetconfTimeStamp;
40 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.NetconfTimeStampImpl;
41 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.InternalConnectionStatus;
42 import org.opendaylight.mdsal.binding.api.DataBroker;
43 import org.opendaylight.mdsal.binding.api.ReadTransaction;
44 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
45 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
46 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
47 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
48 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.network.topology.topology.topology.types.TopologyNetconf;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.ConnectionLogStatus;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.EventlogBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.NetworkElementConnectionBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.NetworkElementConnectionEntity;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.SourceType;
57 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
58 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
59 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
60 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
61 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
62 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
63 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
64 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
68 public class ConnectionStatusHousekeepingService
69 implements ClusterSingletonService, AutoCloseable, IConfigChangedListener {
71 private static final Logger LOG = LoggerFactory.getLogger(ConnectionStatusHousekeepingService.class);
73 private static final long INTERVAL_SECONDS = 30;
74 private static final InstanceIdentifier<Topology> NETCONF_TOPO_IID =
75 InstanceIdentifier.create(NetworkTopology.class).child(Topology.class,
76 new TopologyKey(new TopologyId(TopologyNetconf.QNAME.getLocalName())));
77 private static final ServiceGroupIdentifier IDENT =
78 ServiceGroupIdentifier.create("ConnectionStatusHousekeepingService");
80 private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(3);
81 private final DataBroker dataBroker;
82 private final DataProvider dataProvider;
83 private final Runnable runner = () -> doClean();
84 private final HouseKeepingConfig config;
85 private final ConfigurationFileRepresentation cfg;
87 private final ClusterSingletonServiceRegistration cssRegistration2;
88 private boolean isMaster;
89 private Future<?> taskReference;
90 private int eventNumber;
91 private volatile boolean enabled;
93 public ConnectionStatusHousekeepingService(ConfigurationFileRepresentation cfg,
94 ClusterSingletonServiceProvider clusterSingletonServiceProvider, DataBroker dataBroker,
95 DataProvider dataProvider) {
96 this.config = new HouseKeepingConfig(cfg);
98 cfg.registerConfigChangedListener(this);
99 this.dataBroker = dataBroker;
100 this.dataProvider = dataProvider;
101 this.eventNumber = 0;
103 setEnabled(this.config.isEnabled());
106 this.cssRegistration2 = clusterSingletonServiceProvider.registerClusterSingletonService(this);
109 private void setEnabled(boolean pEnabled) {
110 LOG.info("ConnectionStatusHousekeepingService status change from {} to {}", enabled, pEnabled);
111 this.enabled = pEnabled;
114 private boolean isEnabled() {
118 private void start() {
119 if (taskReference != null) {
120 taskReference.cancel(false);
123 LOG.info("Do not start. not the master node");
125 LOG.info("Starting scheduler with interval {}", INTERVAL_SECONDS);
127 this.scheduler.scheduleAtFixedRate(runner, INTERVAL_SECONDS, INTERVAL_SECONDS, TimeUnit.SECONDS);
131 private void doClean() {
133 LOG.debug("service is disabled by config");
136 LOG.debug("start housekeeping");
137 // get all devices from networkelement-connection index
139 List<NetworkElementConnectionEntity> list = this.dataProvider.getNetworkElementConnections();
141 ConnectionLogStatus dbStatus;
142 ConnectionLogStatus mdsalStatus;
144 if (list == null || list.size() <= 0) {
145 LOG.trace("no items in list.");
147 NetconfTimeStamp ts = NetconfTimeStampImpl.getConverter();
148 //check all db entries and sync connection status
149 for (NetworkElementConnectionEntity item : list) {
151 // compare with MD-SAL
152 nodeId = item.getNodeId();
153 LOG.trace("check status of {}", nodeId);
154 dbStatus = item.getStatus();
155 mdsalStatus = this.getMDSalConnectionStatus(nodeId);
156 if (mdsalStatus == null) {
157 LOG.trace("unable to get connection status. jump over");
160 // if different then update db
161 if (dbStatus != mdsalStatus) {
162 LOG.trace("status is inconsistent db={}, mdsal={}. updating db", dbStatus, mdsalStatus);
163 this.dataProvider.writeEventLog(new EventlogBuilder().setNodeId("SDN-Controller")
164 .setTimestamp(new DateAndTime(ts.getTimeStamp())).setObjectId(item.getNodeId())
165 .setAttributeName("status").setNewValue(String.valueOf(mdsalStatus))
166 .setCounter(popEvntNumber()).setSourceType(SourceType.Controller).build());
167 if ((item.isIsRequired() == null || item.isIsRequired() == false)
168 && mdsalStatus == ConnectionLogStatus.Disconnected) {
169 LOG.info("removing entry for node {} ({}) from database due missing MD-SAL entry",
170 item.getNodeId(), mdsalStatus);
171 this.dataProvider.removeNetworkConnection(nodeId);
173 NetworkElementConnectionBuilder ne =
174 new NetworkElementConnectionBuilder().setStatus(mdsalStatus);
176 this.dataProvider.updateNetworkConnection22(ne.build(), nodeId);
179 LOG.trace("no difference");
184 } catch (Exception e) {
185 LOG.warn("problem executing housekeeping task: {}", e);
187 LOG.debug("finish housekeeping");
190 private Integer popEvntNumber() {
191 return eventNumber++;
194 private ConnectionLogStatus getMDSalConnectionStatus(String nodeId) {
196 @SuppressWarnings("null")
198 InstanceIdentifier<Node> instanceIdentifier =
199 NETCONF_TOPO_IID.child(Node.class, new NodeKey(new NodeId(nodeId)));
200 ReadTransaction trans = this.dataBroker.newReadOnlyTransaction();
201 FluentFuture<Optional<Node>> optionalNode = trans.read(LogicalDatastoreType.OPERATIONAL, instanceIdentifier);
203 //Node node = optionalNode.get(5, TimeUnit.SECONDS).get();
204 Node node = optionalNode.get().get();
205 LOG.debug("node is {}", node);
206 NetconfNode nNode = node.augmentation(NetconfNode.class);
207 LOG.debug("nnode is {}", nNode);
209 return InternalConnectionStatus.statusFromNodeStatus(nNode.getConnectionStatus());
211 } catch (NoSuchElementException e) {
212 return ConnectionLogStatus.Disconnected;
213 } catch (ExecutionException | InterruptedException e) {// | TimeoutException e) {
214 LOG.warn("unable to get node info: {}", e);
223 public void close() throws Exception {
224 if (taskReference != null) {
225 taskReference.cancel(false);
227 if (this.cfg != null) {
228 this.cfg.unregisterConfigChangedListener(this);
230 this.scheduler.shutdown();
231 this.cssRegistration2.close();
234 @SuppressWarnings("null")
236 public @NonNull ServiceGroupIdentifier getIdentifier() {
241 public void instantiateServiceInstance() {
242 LOG.info("We take Leadership");
243 this.isMaster = true;
248 public ListenableFuture<? extends Object> closeServiceInstance() {
249 LOG.info("We lost Leadership");
250 this.isMaster = false;
252 return Futures.immediateFuture(null);
256 public void onConfigChanged() {
258 setEnabled(this.config.isEnabled());