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