Assign image keyname and pubkey at vnf level
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / devicemanager / impl / src / main / java / org / opendaylight / mwtn / deviceMonitor / impl / DeviceMonitorImpl.java
1 /**
2  * (c) 2017 highstreet technologies GmbH
3  */
4
5 package org.opendaylight.mwtn.deviceMonitor.impl;
6
7 import java.util.Enumeration;
8 import java.util.concurrent.ConcurrentHashMap;
9 import java.util.concurrent.Executors;
10 import java.util.concurrent.ScheduledExecutorService;
11
12 import org.opendaylight.mwtn.base.netconf.ONFCoreNetworkElementRepresentation;
13 import org.opendaylight.mwtn.devicemanager.impl.listener.ODLEventListener;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 /**
18  *  Implementation of concept "Active monitoring" of a device.<br>
19  *    <br>
20  *  For each existing mountpoint a task runs with 120s cycle time. Every 120 seconds the check actions are performed.
21  *  The request is handled by the NETCONF layer with a (default)configured time-out of 60 seconds.<br>
22  *  Generated alarms, by the object/node "SDN-Controller" are (enum DeviceMonitorProblems):<br>
23  *      - notConnected(InternalSeverity.Warning)<br>
24  *      - noConnectionMediator(InternalSeverity.Minor)<br>
25  *      - noConnectionNe(InternalSeverity.Critical)<br>
26  *    <br>
27  *  1. Mountpoint does not exist<br>
28  *  If the mountpoint does not exists there are no related current alarms in the database.<br>
29  *    <br>
30  *  2. Created mountpoint with state "Connecting" or "UnableToConnect"<br>
31  *  If the Mountpoint is created and connection status is "Connecting" or "UnableToConnect".<br>
32  *  - After about 2..4 Minutes ... raise alarm "notConnected" with severity warning<br>
33  *    <br>
34  *  3. Created mountpoint with state "Connection"<br>
35  *  There are two monitor activities.<br>
36  *      3a. Check of Mediator connection by requesting (typical) cached data.<br>
37  *          - After about 60 seconds raise alarm: connection-loss-mediator with severity minor<br>
38  *          - Request from Mediator: network-element<br>
39  *    <br>
40  *      3b. Check connection to NEby requesting (typical) non-cached data.<br>
41  *          - Only if AirInterface available. The first one is used.<br>
42  *          - Requested are the currentAlarms<br>
43  *          - After about 60 seconds raise alarm: connection-loss-network-element with severity critical<br>
44  *    <br>
45  * @author herbert
46  */
47
48 public class DeviceMonitorImpl implements DeviceMonitor, AutoCloseable {
49
50     private static final Logger LOG = LoggerFactory.getLogger(DeviceMonitorImpl.class);
51
52     private final ConcurrentHashMap<String, DeviceMonitorTask> queue;
53     private final ScheduledExecutorService scheduler;
54     private final ODLEventListener odlEventListener;
55
56     /*-------------------------------------------------------------
57      * Construction/ destruction of service
58      */
59
60     /**
61      * Basic implementation of devicemonitoring
62      * @param odlEventListener as destination for problems
63      */
64     public DeviceMonitorImpl(ODLEventListener odlEventListener) {
65         LOG.info("Construct {}", this.getClass().getSimpleName());
66
67         this.odlEventListener = odlEventListener;
68         this.queue = new ConcurrentHashMap<>();
69         this.scheduler = Executors.newScheduledThreadPool(1);
70     }
71
72     /**
73      * Stop the service. Stop all running monitoring tasks.
74      */
75     @Override
76     synchronized public void close() {
77         LOG.info("Close {}", this.getClass().getSimpleName());
78
79         Enumeration<String> e = queue.keys();
80         while (e.hasMoreElements()) {
81             deviceDisconnectIndication(e.nextElement());
82         }
83
84         scheduler.shutdown();
85     }
86
87     /*-------------------------------------------------------------
88      * Register/ Deregister Mountpoint
89      */
90
91     @Override
92     synchronized public void createMountpointIndication(String mountPointNodeName) {
93
94         LOG.debug("Register for monitoring {} {}",mountPointNodeName, mountPointNodeName.hashCode());
95
96         LOG.info("Do start of DeviceMonitor task");
97         //Runnable task = new PerformanceManagerTask(queue, databaseService);
98         DeviceMonitorTask task = new DeviceMonitorTask(mountPointNodeName, this.odlEventListener);
99         queue.put(mountPointNodeName, task);
100         task.start(scheduler);
101     }
102
103     @Override
104     synchronized public void removeMountpointIndication(String mountPointNodeName) {
105
106         if (queue.containsKey(mountPointNodeName)) {
107             DeviceMonitorTask task = queue.get(mountPointNodeName);
108             //Remove from here
109             queue.remove(mountPointNodeName);
110
111             //Clear all problems
112             task.removeMountpointIndication();
113             LOG.debug("Task stopped: {}", mountPointNodeName);
114         } else {
115             LOG.warn("Task not in queue anymore: {}", mountPointNodeName);
116         }
117     }
118
119     /*-------------------------------------------------------------
120      * Register/ Deregister device
121      */
122
123     @Override
124     synchronized public void deviceConnectIndication(String mountPointNodeName, ONFCoreNetworkElementRepresentation ne) {
125
126         LOG.debug("ne changes to connected state {}",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 anymore: {} {} {}", mountPointNodeName, mountPointNodeName.hashCode(), queue.size());
132         }
133     }
134
135     @Override
136     synchronized public void deviceDisconnectIndication(String mountPointNodeName) {
137
138         LOG.debug("ne changes to disconnected state {}",mountPointNodeName);
139         if (queue.containsKey(mountPointNodeName)) {
140             DeviceMonitorTask task = queue.get(mountPointNodeName);
141             task.deviceDisconnectIndication();
142         } else {
143             LOG.warn("Monitoring task not in queue anymore: {} {} {}", mountPointNodeName, mountPointNodeName.hashCode(), queue.size());
144         }
145     }
146
147     /*-------------------------------------------------------------
148      * Private functions
149      */
150
151 }