0149d6fca49827f732460e3a7a03287c0accbe55
[ccsdk/features.git] /
1 /*******************************************************************************
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 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  * (c) 2017 highstreet technologies GmbH
20  */
21
22 package org.onap.ccsdk.features.sdnr.wt.devicemanager.devicemonitor.impl;
23
24 import java.util.Enumeration;
25 import java.util.concurrent.ConcurrentHashMap;
26 import java.util.concurrent.Executors;
27 import java.util.concurrent.ScheduledExecutorService;
28
29 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.HtDevicemanagerConfiguration;
30 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.IConfigChangedListener;
31 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.impl.DmConfig;
32 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.listener.ODLEventListener;
33 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  *  Implementation of concept "Active monitoring" of a device.<br>
39  *    <br>
40  *  For each existing mountpoint a task runs with 120s cycle time. Every 120 seconds the check actions are performed.
41  *  The request is handled by the NETCONF layer with a (default)configured time-out of 60 seconds.<br>
42  *  Generated alarms, by the object/node "SDN-Controller" are (enum DeviceMonitorProblems):<br>
43  *      - notConnected(InternalSeverity.Warning)<br>
44  *      - noConnectionMediator(InternalSeverity.Minor)<br>
45  *      - noConnectionNe(InternalSeverity.Critical)<br>
46  *    <br>
47  *  1. Mountpoint does not exist<br>
48  *  If the mountpoint does not exists there are no related current alarms in the database.<br>
49  *    <br>
50  *  2. Created mountpoint with state "Connecting" or "UnableToConnect"<br>
51  *  If the Mountpoint is created and connection status is "Connecting" or "UnableToConnect".<br>
52  *  - After about 2..4 Minutes ... raise alarm "notConnected" with severity warning<br>
53  *    <br>
54  *  3. Created mountpoint with state "Connection"<br>
55  *  There are two monitor activities.<br>
56  *      3a. Check of Mediator connection by requesting (typical) cached data.<br>
57  *          - After about 60 seconds raise alarm: connection-loss-mediator with severity minor<br>
58  *          - Request from Mediator: network-element<br>
59  *    <br>
60  *      3b. Check connection to NEby requesting (typical) non-cached data.<br>
61  *          - Only if AirInterface available. The first one is used.<br>
62  *          - Requested are the currentAlarms<br>
63  *          - After about 60 seconds raise alarm: connection-loss-network-element with severity critical<br>
64  *    <br>
65  * @author herbert
66  */
67
68 @SuppressWarnings("deprecation")
69 public class DeviceMonitorImpl implements DeviceMonitor, IConfigChangedListener {
70
71     private static final Logger LOG = LoggerFactory.getLogger(DeviceMonitorImpl.class);
72
73     private final ConcurrentHashMap<String, DeviceMonitorTask> queue;
74     private final ScheduledExecutorService scheduler;
75     private final ODLEventListener odlEventListener;
76     @SuppressWarnings("unused")
77     private final DataBroker dataBroker; //Future usage
78
79     /*-------------------------------------------------------------
80      * Construction/ destruction of service
81      */
82
83     /**
84      * Basic implementation of devicemonitoring
85      * @param odlEventListener as destination for problems
86      */
87     public DeviceMonitorImpl(DataBroker dataBroker, ODLEventListener odlEventListener, HtDevicemanagerConfiguration htconfig) {
88         LOG.info("Construct {}", this.getClass().getSimpleName());
89
90         this.odlEventListener = odlEventListener;
91         this.dataBroker = dataBroker;
92
93         htconfig.registerConfigChangedListener(this);
94         DmConfig dmConfig = htconfig.getDmConfig();
95         setDmConfig(dmConfig);
96
97         this.queue = new ConcurrentHashMap<>();
98         this.scheduler = Executors.newScheduledThreadPool(10);
99     }
100
101     /**
102      * Stop the service. Stop all running monitoring tasks.
103      */
104     @Override
105     synchronized public void close() {
106         LOG.info("Close {}", this.getClass().getSimpleName());
107
108         Enumeration<String> e = queue.keys();
109         while (e.hasMoreElements()) {
110             deviceDisconnectIndication(e.nextElement());
111         }
112
113         scheduler.shutdown();
114     }
115
116         @Override
117         public void onConfigChanged() {
118         DmConfig cfg = DmConfig.reload();
119         setDmConfig(cfg);
120         }
121
122         private void setDmConfig(DmConfig dmConfig) {
123         for (DeviceMonitorProblems problem : DeviceMonitorProblems.values()) {
124                 problem.setSeverity(dmConfig.getSeverity(problem));
125         }
126         }
127
128     /*-------------------------------------------------------------
129      * Start/ stop/ update service for Mountpoint
130      */
131
132     /**
133      * Notify of device state changes to "connected" for slave nodes
134      * @param mountPointNodeName name of mount point
135      */
136     synchronized public void deviceConnectSlaveIndication(String mountPointNodeName) {
137         deviceConnectMasterIndication(mountPointNodeName, null);
138     }
139
140     /**
141      * Notify of device state changes to "connected"
142      * @param mountPointNodeName name of mount point
143      * @param ne to monitor
144      */
145     synchronized public void deviceConnectMasterIndication(String mountPointNodeName, DeviceMonitoredNe ne) {
146
147         LOG.debug("ne changes to connected state {}",mountPointNodeName);
148         createMonitoringTask(mountPointNodeName);
149         if (queue.containsKey(mountPointNodeName)) {
150             DeviceMonitorTask task = queue.get(mountPointNodeName);
151             task.deviceConnectIndication(ne);
152         } else {
153             LOG.warn("Monitoring task not in queue: {} {} {}", mountPointNodeName, mountPointNodeName.hashCode(), queue.size());
154         }
155     }
156
157    /**
158     * Notify of device state change to "disconnected"
159     * Mount point supervision
160     * @param mountPointNodeName to deregister
161     */
162     synchronized public void deviceDisconnectIndication(String mountPointNodeName) {
163
164         LOG.debug("State changes to not connected state {}",mountPointNodeName);
165         createMonitoringTask(mountPointNodeName);
166         if (queue.containsKey(mountPointNodeName)) {
167             DeviceMonitorTask task = queue.get(mountPointNodeName);
168             task.deviceDisconnectIndication();
169         } else {
170             LOG.warn("Monitoring task not in queue: {} {} {}", mountPointNodeName, mountPointNodeName.hashCode(), queue.size());
171         }
172     }
173
174     /**
175      * removeMountpointIndication deregisters a mountpoint for registration services
176      * @param mountPointNodeName to deregister
177      */
178     synchronized public void removeMountpointIndication(String mountPointNodeName) {
179
180         if (queue.containsKey(mountPointNodeName)) {
181             DeviceMonitorTask task = queue.get(mountPointNodeName);
182             //Remove from here
183             queue.remove(mountPointNodeName);
184             //Clear all problems
185             task.removeMountpointIndication();
186             LOG.debug("Task stopped: {}", mountPointNodeName);
187         } else {
188             LOG.warn("Task not in queue: {}", mountPointNodeName);
189         }
190     }
191
192     /**
193      * Referesh database by raising all alarms again.
194      */
195     public void refreshAlarmsInDb() {
196         synchronized(queue) {
197             for (DeviceMonitorTask task : queue.values()) {
198                 task.refreshAlarms();
199             }
200         }
201     }
202
203     /*-------------------------------------------------------------
204      * Private functions
205      */
206
207     /**
208      * createMountpoint registers a new mountpoint monitoring service
209      * @param mountPointNodeName name of mountpoint
210      */
211     synchronized private DeviceMonitorTask createMonitoringTask(String mountPointNodeName) {
212
213         DeviceMonitorTask task;
214         LOG.debug("Register for monitoring {} {}",mountPointNodeName, mountPointNodeName.hashCode());
215
216         if (queue.containsKey(mountPointNodeName)) {
217             LOG.info("Monitoring task exists");
218             task = queue.get(mountPointNodeName);
219         } else {
220             LOG.info("Do start of DeviceMonitor task");
221             //Runnable task = new PerformanceManagerTask(queue, databaseService);
222             task = new DeviceMonitorTask(mountPointNodeName, this.odlEventListener);
223             queue.put(mountPointNodeName, task);
224             task.start(scheduler);
225         }
226         return task;
227     }
228
229 }