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