aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/devicemanager-core/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/housekeeping/ConnectionStatusHousekeepingService.java
blob: eefd189087a8467b275ea371d9970ce610be3075 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 * ============LICENSE_START=======================================================
 * ONAP : ccsdk feature sdnr wt
 *  ================================================================================
 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
 * All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.ccsdk.features.sdnr.wt.devicemanager.housekeeping;

import com.google.common.util.concurrent.FluentFuture;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.eclipse.jdt.annotation.NonNull;
import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation;
import org.onap.ccsdk.features.sdnr.wt.common.configuration.filechange.IConfigChangedListener;
import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider;
import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.NetconfTimeStamp;
import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.NetconfTimeStampImpl;
import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.InternalConnectionStatus;
import org.opendaylight.mdsal.binding.api.DataBroker;
import org.opendaylight.mdsal.binding.api.ReadTransaction;
import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev221225.NetconfNode;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev221225.network.topology.topology.topology.types.TopologyNetconf;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ConnectionLogStatus;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.EventlogBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.NetworkElementConnectionBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.NetworkElementConnectionEntity;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SourceType;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ConnectionStatusHousekeepingService
        implements ClusterSingletonService, AutoCloseable, IConfigChangedListener {

    private static final Logger LOG = LoggerFactory.getLogger(ConnectionStatusHousekeepingService.class);

    private static final long INTERVAL_SECONDS = 30;
    private static final InstanceIdentifier<Topology> NETCONF_TOPO_IID =
            InstanceIdentifier.create(NetworkTopology.class).child(Topology.class,
                    new TopologyKey(new TopologyId(TopologyNetconf.QNAME.getLocalName())));
    private static final ServiceGroupIdentifier IDENT =
            ServiceGroupIdentifier.create("ConnectionStatusHousekeepingService");

    private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(3);
    private final DataBroker dataBroker;
    private final DataProvider dataProvider;
    private final Runnable runner = () -> doClean();
    private final HouseKeepingConfig config;
    private final ConfigurationFileRepresentation cfg;

    private final ClusterSingletonServiceRegistration cssRegistration2;
    private boolean isMaster;
    private Future<?> taskReference;
    private int eventNumber;
    private volatile boolean enabled;

    public ConnectionStatusHousekeepingService(ConfigurationFileRepresentation cfg,
            ClusterSingletonServiceProvider clusterSingletonServiceProvider, DataBroker dataBroker,
            DataProvider dataProvider) {
        this.config = new HouseKeepingConfig(cfg);
        this.cfg = cfg;
        cfg.registerConfigChangedListener(this);
        this.dataBroker = dataBroker;
        this.dataProvider = dataProvider;
        this.eventNumber = 0;

        setEnabled(this.config.isEnabled());
        this.start();

        this.cssRegistration2 = clusterSingletonServiceProvider.registerClusterSingletonService(this);
    }

    private void setEnabled(boolean pEnabled) {
        LOG.info("ConnectionStatusHousekeepingService status change from {} to {}", enabled, pEnabled);
        this.enabled = pEnabled;
    }

    private boolean isEnabled() {
        return this.enabled;
    }

    private void start() {
        if (taskReference != null) {
            taskReference.cancel(false);
        }
        if (!isMaster) {
            LOG.info("Do not start. not the master node");
        } else {
            LOG.info("Starting scheduler with interval {}", INTERVAL_SECONDS);
            this.taskReference =
                    this.scheduler.scheduleAtFixedRate(runner, INTERVAL_SECONDS, INTERVAL_SECONDS, TimeUnit.SECONDS);
        }
    }

    private void doClean() {
        if (!isEnabled()) {
            LOG.debug("service is disabled by config");
            return;
        }
        LOG.debug("start housekeeping");
        // get all devices from networkelement-connection index
        try {
            List<NetworkElementConnectionEntity> list = this.dataProvider.getNetworkElementConnections();

            ConnectionLogStatus dbStatus;
            ConnectionLogStatus mdsalStatus;
            String nodeId;
            if (list == null || list.size() <= 0) {
                LOG.trace("no items in list.");
            } else {
                NetconfTimeStamp ts = NetconfTimeStampImpl.getConverter();
                //check all db entries and sync connection status
                for (NetworkElementConnectionEntity item : list) {

                    // compare with MD-SAL
                    nodeId = item.getNodeId();
                    LOG.trace("check status of {}", nodeId);
                    dbStatus = item.getStatus();
                    mdsalStatus = this.getMDSalConnectionStatus(nodeId);
                    if (mdsalStatus == null) {
                        LOG.trace("unable to get connection status. jump over");
                        continue;
                    }
                    // if different then update db
                    if (dbStatus != mdsalStatus) {
                        LOG.trace("status is inconsistent db={}, mdsal={}. updating db", dbStatus, mdsalStatus);
                        this.dataProvider.writeEventLog(new EventlogBuilder().setNodeId("SDN-Controller")
                                .setTimestamp(new DateAndTime(ts.getTimeStamp())).setObjectId(item.getNodeId())
                                .setAttributeName("status").setNewValue(String.valueOf(mdsalStatus))
                                .setCounter(popEvntNumber()).setSourceType(SourceType.Controller).build());
                        if ((item.requireIsRequired() == null || item.requireIsRequired() == false)
                                && mdsalStatus == ConnectionLogStatus.Disconnected) {
                            LOG.info("removing entry for node {} ({}) from database due missing MD-SAL entry",
                                    item.getNodeId(), mdsalStatus);
                            this.dataProvider.removeNetworkConnection(nodeId);
                        } else {
                            NetworkElementConnectionBuilder ne =
                                    new NetworkElementConnectionBuilder().setStatus(mdsalStatus);

                            this.dataProvider.updateNetworkConnection22(ne.build(), nodeId);
                        }
                    } else {
                        LOG.trace("no difference");
                    }
                }
            }

        } catch (Exception e) {
            LOG.warn("problem executing housekeeping task: {}", e);
        }
        LOG.debug("finish housekeeping");
    }

    private Integer popEvntNumber() {
        return eventNumber++;
    }

    private ConnectionLogStatus getMDSalConnectionStatus(String nodeId) {

        @NonNull
        InstanceIdentifier<Node> instanceIdentifier =
                NETCONF_TOPO_IID.child(Node.class, new NodeKey(new NodeId(nodeId)));
        //Implicit close of try with resource is not handled correctly by underlying opendaylight NETCONF service
        ReadTransaction trans = this.dataBroker.newReadOnlyTransaction();
        try {
            FluentFuture<Optional<Node>> optionalNode =
                    trans.read(LogicalDatastoreType.OPERATIONAL, instanceIdentifier);
            Optional<Node> nodeOpt = optionalNode.get();
            if (nodeOpt.isEmpty()) {
                LOG.warn("unable to get node info");
                return null;
            }
            Node node = nodeOpt.get();
            LOG.debug("node is {}", node);
            NetconfNode nNode = node.augmentation(NetconfNode.class);
            LOG.debug("nnode is {}", nNode);
            if (nNode != null) {
                return InternalConnectionStatus.statusFromNodeStatus(nNode.getConnectionStatus());
            }
        } catch (NoSuchElementException e) {
            return ConnectionLogStatus.Disconnected;
        } catch (InterruptedException e) {
            LOG.warn("InterruptedException unable to get node info: ", e);
            Thread.currentThread().interrupt();
        } catch (ExecutionException e) {// | TimeoutException e) {
            LOG.warn("ExecutionException unable to get node info: ", e);
        } finally {
            trans.close();
        }

        return null;
    }

    @Override
    public void close() throws Exception {
        if (taskReference != null) {
            taskReference.cancel(false);
        }
        if (this.cfg != null) {
            this.cfg.unregisterConfigChangedListener(this);
        }
        this.scheduler.shutdown();
        if (this.cssRegistration2 != null)
            this.cssRegistration2.close();
    }

    @Override
    public @NonNull ServiceGroupIdentifier getIdentifier() {
        return IDENT;
    }

    @Override
    public void instantiateServiceInstance() {
        LOG.info("We take Leadership");
        this.isMaster = true;
        this.start();
    }

    @Override
    public ListenableFuture<? extends Object> closeServiceInstance() {
        LOG.info("We lost Leadership");
        this.isMaster = false;
        this.start();
        return Futures.immediateFuture(null);
    }

    @Override
    public void onConfigChanged() {

        setEnabled(this.config.isEnabled());
    }
}