0141b2663c70d56318ee29b9d5ed827a850e929f
[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 public class DeviceMonitorImpl implements AutoCloseable {
65
66     private static final Logger LOG = LoggerFactory.getLogger(DeviceMonitorImpl.class);
67
68     private final ConcurrentHashMap<String, DeviceMonitorTask> queue;
69     private final ScheduledExecutorService scheduler;
70     private final ODLEventListener odlEventListener;
71     @SuppressWarnings("unused")
72     private final DataBroker dataBroker; //Future usage
73
74     /*-------------------------------------------------------------
75      * Construction/ destruction of service
76      */
77
78     /**
79      * Basic implementation of devicemonitoring
80      * @param odlEventListener as destination for problems
81      */
82     public DeviceMonitorImpl(DataBroker dataBroker, ODLEventListener odlEventListener) {
83         LOG.info("Construct {}", this.getClass().getSimpleName());
84
85         this.odlEventListener = odlEventListener;
86         this.dataBroker = dataBroker;
87         this.queue = new ConcurrentHashMap<>();
88         this.scheduler = Executors.newScheduledThreadPool(10);
89     }
90
91     /**
92      * Stop the service. Stop all running monitoring tasks.
93      */
94     @Override
95     synchronized public void close() {
96         LOG.info("Close {}", this.getClass().getSimpleName());
97
98         Enumeration<String> e = queue.keys();
99         while (e.hasMoreElements()) {
100             deviceDisconnectIndication(e.nextElement());
101         }
102
103         scheduler.shutdown();
104     }
105
106     /*-------------------------------------------------------------
107      * Start/ stop/ update service for Mountpoint
108      */
109
110     /**
111      * Notify of device state changes to "connected" for slave nodes
112      * @param mountPointNodeName name of mount point
113      */
114     synchronized public void deviceConnectSlaveIndication(String mountPointNodeName) {
115         deviceConnectMasterIndication(mountPointNodeName, null);
116     }
117
118     /**
119      * Notify of device state changes to "connected"
120      * @param mountPointNodeName name of mount point
121      * @param ne to monitor
122      */
123     synchronized public void deviceConnectMasterIndication(String mountPointNodeName, DeviceMonitoredNe ne) {
124
125         LOG.debug("ne changes to connected state {}",mountPointNodeName);
126         createMonitoringTask(mountPointNodeName);
127         if (queue.containsKey(mountPointNodeName)) {
128             DeviceMonitorTask task = queue.get(mountPointNodeName);
129             task.deviceConnectIndication(ne);
130         } else {
131             LOG.warn("Monitoring task not in queue: {} {} {}", mountPointNodeName, mountPointNodeName.hashCode(), queue.size());
132         }
133     }
134
135    /**
136     * Notify of device state change to "disconnected"
137     * Mount point supervision
138     * @param mountPointNodeName to deregister
139     */
140     synchronized public void deviceDisconnectIndication(String mountPointNodeName) {
141
142         LOG.debug("State changes to not connected state {}",mountPointNodeName);
143         createMonitoringTask(mountPointNodeName);
144         if (queue.containsKey(mountPointNodeName)) {
145             DeviceMonitorTask task = queue.get(mountPointNodeName);
146             task.deviceDisconnectIndication();
147         } else {
148             LOG.warn("Monitoring task not in queue: {} {} {}", mountPointNodeName, mountPointNodeName.hashCode(), queue.size());
149         }
150     }
151
152     /**
153      * removeMountpointIndication deregisters a mountpoint for registration services
154      * @param mountPointNodeName to deregister
155      */
156     synchronized public void removeMountpointIndication(String mountPointNodeName) {
157
158         if (queue.containsKey(mountPointNodeName)) {
159             DeviceMonitorTask task = queue.get(mountPointNodeName);
160             //Remove from here
161             queue.remove(mountPointNodeName);
162
163             //Clear all problems
164             task.removeMountpointIndication();
165             LOG.debug("Task stopped: {}", mountPointNodeName);
166         } else {
167             LOG.warn("Task not in queue: {}", mountPointNodeName);
168         }
169     }
170
171     /**
172      * Referesh database by raising all alarms again.
173      */
174     public void refreshAlarmsInDb() {
175         synchronized(queue) {
176             for (DeviceMonitorTask task : queue.values()) {
177                 task.refreshAlarms();
178             }
179         }
180     }
181
182     /*-------------------------------------------------------------
183      * Private functions
184      */
185
186     /**
187      * createMountpoint registers a new mountpoint monitoring service
188      * @param mountPointNodeName name of mountpoint
189      */
190     synchronized private DeviceMonitorTask createMonitoringTask(String mountPointNodeName) {
191
192         DeviceMonitorTask task;
193         LOG.debug("Register for monitoring {} {}",mountPointNodeName, mountPointNodeName.hashCode());
194
195         if (queue.containsKey(mountPointNodeName)) {
196             LOG.info("Monitoring task exists");
197             task = queue.get(mountPointNodeName);
198         } else {
199             LOG.info("Do start of DeviceMonitor task");
200             //Runnable task = new PerformanceManagerTask(queue, databaseService);
201             task = new DeviceMonitorTask(mountPointNodeName, this.odlEventListener);
202             queue.put(mountPointNodeName, task);
203             task.start(scheduler);
204         }
205         return task;
206     }
207
208 }