bc6a068fbea817d88f7f65f3c8dee2dbb6790c14
[ccsdk/features.git] / sdnr / wt / devicemanager / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / housekeeping / ResyncNetworkElementHouskeepingService.java
1 /*******************************************************************************
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk feature sdnr wt
4  *  ================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  ******************************************************************************/
21 package org.onap.ccsdk.features.sdnr.wt.devicemanager.housekeeping;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Optional;
26 import org.eclipse.jdt.annotation.NonNull;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider;
29 import org.onap.ccsdk.features.sdnr.wt.devicemanager.devicemonitor.impl.DeviceMonitor;
30 import org.onap.ccsdk.features.sdnr.wt.devicemanager.eventdatahandler.ODLEventListenerHandler;
31 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.DeviceManagerImpl;
32 import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.service.NetworkElement;
33 import org.opendaylight.mdsal.binding.api.MountPoint;
34 import org.opendaylight.mdsal.binding.api.MountPointService;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.network.topology.topology.topology.types.TopologyNetconf;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 public class ResyncNetworkElementHouskeepingService implements ResyncNetworkElementsListener {
48
49     private static final Logger LOG = LoggerFactory.getLogger(ResyncNetworkElementHouskeepingService.class);
50
51     private static final InstanceIdentifier<Topology> NETCONF_TOPO_IID =
52             InstanceIdentifier.create(NetworkTopology.class).child(Topology.class,
53                     new TopologyKey(new TopologyId(TopologyNetconf.QNAME.getLocalName())));
54
55     // Services to use
56     private final MountPointService mountPointService;
57     private final ODLEventListenerHandler odlEventListenerHandler;
58     private final DataProvider databaseClientEvents;
59     private final DeviceMonitor deviceMonitor;
60     private final DeviceManagerImpl deviceManager;
61
62     /** Thread is started to du the clean up action **/
63     private Thread threadDoClearCurrentFaultByNodename;
64     /** Indicate number of refresh activities for log **/
65     private int refreshCounter = 0;
66
67     /**
68      * @param deviceManager to provide devices information
69      * @param mountPointService service
70      * @param odlEventListenerHandler handler for events
71      * @param databaseClientEvents database to clean
72      * @param deviceMonitor devicemonitor
73      */
74     public ResyncNetworkElementHouskeepingService(
75             DeviceManagerImpl deviceManager,
76             MountPointService mountPointService, ODLEventListenerHandler odlEventListenerHandler,
77             DataProvider databaseClientEvents, DeviceMonitor deviceMonitor) {
78         super();
79         this.deviceManager = deviceManager;
80         this.mountPointService = mountPointService;
81         this.odlEventListenerHandler = odlEventListenerHandler;
82         this.databaseClientEvents = databaseClientEvents;
83         this.deviceMonitor = deviceMonitor;
84     }
85
86     /**
87      * Async RPC Interface implementation
88      */
89     @Override
90     public @NonNull List<String> doClearCurrentFaultByNodename(@Nullable List<String> nodeNames)
91             throws IllegalStateException {
92
93         if (this.databaseClientEvents == null) {
94             throw new IllegalStateException("dbEvents service not instantiated");
95         }
96
97         if (threadDoClearCurrentFaultByNodename != null && threadDoClearCurrentFaultByNodename.isAlive()) {
98             throw new IllegalStateException("A clear task is already active");
99         } else {
100             List<String> nodeNamesInput;
101
102             // Create list of mountpoints if input is empty, using the content in ES
103             if (nodeNames == null || nodeNames.size() <= 0) {
104                 nodeNamesInput = this.databaseClientEvents.getAllNodesWithCurrentAlarms();
105             } else {
106                                 nodeNamesInput = nodeNames;
107                         }
108
109             // Filter all mountpoints from input that were found and are known to this Cluster-node instance of
110             // DeviceManager
111             final List<String> nodeNamesHandled = new ArrayList<>();
112             for (String mountpointName : nodeNamesInput) {
113                 LOG.info("Work with mountpoint {}", mountpointName);
114
115                 if (odlEventListenerHandler != null && mountpointName.equals(odlEventListenerHandler.getOwnKeyName())) {
116
117                     // SDN Controller related alarms
118                     // -- can not be recreated on all nodes in connected state
119                     // -- would result in a DCAE/AAI Notification
120                     // Conclusion for 1810 Delivery ... not covered by RPC function (See issue #43)
121                     LOG.info("Ignore SDN Controller related alarms for {}", mountpointName);
122                     // this.databaseClientEvents.clearFaultsCurrentOfNode(mountpointName);
123                     // nodeNamesHandled.add(mountpointName);
124
125                 } else {
126
127                     if (mountPointService != null) {
128                         InstanceIdentifier<Node> instanceIdentifier =
129                                 NETCONF_TOPO_IID.child(Node.class, new NodeKey(new NodeId(mountpointName)));
130                         Optional<MountPoint> optionalMountPoint = mountPointService.getMountPoint(instanceIdentifier);
131
132                         if (!optionalMountPoint.isPresent()) {
133                             LOG.info("Remove Alarms for unknown mountpoint {}", mountpointName);
134                             this.databaseClientEvents.clearFaultsCurrentOfNode(mountpointName);
135                             nodeNamesHandled.add(mountpointName);
136                         } else {
137                             if (deviceManager.getNeByMountpoint(mountpointName) != null) {
138                                 LOG.info("At node known mountpoint {}", mountpointName);
139                                 nodeNamesHandled.add(mountpointName);
140                             } else {
141                                 LOG.info("At node unknown mountpoint {}", mountpointName);
142                             }
143                         }
144                     }
145                 }
146             }
147
148             // Force a sync
149             deviceMonitor.refreshAlarmsInDb();
150
151             threadDoClearCurrentFaultByNodename = new Thread(() -> {
152                 refreshCounter++;
153                 LOG.info("Start refresh mountpoint task {}", refreshCounter);
154                 // for(String nodeName:nodeNamesOutput) {
155                 for (String nodeName : nodeNamesHandled) {
156                     NetworkElement ne = deviceManager.getNeByMountpoint(nodeName);
157                     if (ne != null) {
158                         LOG.info("Refresh mountpoint {}", nodeName);
159                         ne.warmstart();
160                     } else {
161                         LOG.info("Unhandled mountpoint {}", nodeName);
162                     }
163                 }
164                 LOG.info("End refresh mountpoint task {}", refreshCounter);
165             });
166             threadDoClearCurrentFaultByNodename.start();
167             return nodeNamesHandled;
168         }
169     };
170
171 }