c933da4d7a82be457cd5e01b6374d6de12867be7
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / devicemanager / impl / src / main / java / org / opendaylight / mwtn / dcaeConnector / impl / DcaeProviderClient.java
1 package org.opendaylight.mwtn.dcaeConnector.impl;
2
3 import java.util.concurrent.Executors;
4 import java.util.concurrent.ScheduledExecutorService;
5 import java.util.concurrent.TimeUnit;
6
7 import org.opendaylight.mwtn.config.impl.DcaeConfig;
8 import org.opendaylight.mwtn.config.impl.HtDevicemanagerConfiguration;
9 import org.opendaylight.mwtn.config.impl.HtDevicemanagerConfiguration.IConfigChangedListener;
10 import org.opendaylight.mwtn.devicemanager.impl.DeviceManagerImpl;
11 import org.opendaylight.mwtn.devicemanager.impl.ProviderClient;
12 import org.opendaylight.mwtn.devicemanager.impl.xml.ProblemNotificationXml;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16
17 public class DcaeProviderClient implements AutoCloseable, ProviderClient {
18
19     private static final Logger LOG = LoggerFactory.getLogger(DcaeProviderClient.class);
20
21     private static final int MIN_HEARTBEAT_TIME_SECONDS = 30;
22
23         private final HtDevicemanagerConfiguration htConfig;
24         private final String entityName;
25         private final DeviceManagerImpl deviceManager;
26
27         private DcaeProviderWorker worker;
28
29     public DcaeProviderClient(HtDevicemanagerConfiguration cfg, String entityName, DeviceManagerImpl deviceManager) {
30
31         this.entityName = entityName;
32         this.deviceManager = deviceManager;
33         this.htConfig=cfg;
34                 this.htConfig.registerConfigChangedListener(configChangedListener );
35
36                 worker = new DcaeProviderWorker(this.htConfig.getDcae(), entityName, deviceManager);
37     }
38
39         @Override
40     public void sendProblemNotification(String mountPointName, ProblemNotificationXml notification) {
41         synchronized(worker) {
42                 worker.sendProblemNotification(mountPointName, notification);
43         }
44     }
45
46         @Override
47         public void sendProblemNotification(String mountPointName, ProblemNotificationXml notification, boolean neDeviceAlarm) {
48                 sendProblemNotification(mountPointName, notification);
49         }
50
51         @Override
52         public void close() {
53                 this.htConfig.unregisterConfigChangedListener(configChangedListener);
54         synchronized(worker) {
55                 worker.close();
56         }
57         }
58
59     /* ---------------------------------------------------------
60      * Private
61      */
62
63
64         private IConfigChangedListener configChangedListener = new IConfigChangedListener() {
65
66                 @Override
67                 public void onConfigChanged() {
68                         synchronized(worker) {
69                                 worker.close();
70                                 worker = new DcaeProviderWorker(DcaeConfig.reload(), entityName, deviceManager);
71                         }
72                 }
73         };
74
75         private static class DcaeProviderWorker implements AutoCloseable {
76
77
78             private final ScheduledExecutorService scheduler;
79             private final DcaeSenderImpl dcaepClient;
80             private final DcaeMessages dcaeMessages;
81
82             public DcaeProviderWorker(DcaeConfig configuration, String entityName, DeviceManagerImpl deviceManager) {
83
84
85                 //Start services
86                 LOG.info("Configuration: "+configuration);
87                 int heartbeatSeconds = configuration.getTimerPeriodSeconds();
88                 if ( heartbeatSeconds < MIN_HEARTBEAT_TIME_SECONDS ) {
89                         heartbeatSeconds = MIN_HEARTBEAT_TIME_SECONDS;
90                         LOG.info("Adjust heartbeat intervall to minimum of { } seconds.",heartbeatSeconds);
91                 }
92
93                 dcaepClient = new DcaeSenderImpl(configuration.getEventReveicerUrl(), configuration.getUserCredentials());
94                 dcaeMessages = new DcaeMessages(dcaepClient, entityName, heartbeatSeconds, deviceManager);
95
96                 //Activate task
97                 LOG.info("Create Fault manager client Task");
98                 this.scheduler = Executors.newSingleThreadScheduledExecutor();
99                 Runnable task = new DcaeProviderTask(dcaeMessages);
100
101                 LOG.info("Fault task created with "+heartbeatSeconds+" Seconds");
102
103                 this.scheduler.scheduleAtFixedRate(task, 0, heartbeatSeconds, TimeUnit.SECONDS);
104                 LOG.info("Fault task scheduled");
105             }
106
107             public void sendProblemNotification(String mountPointName, ProblemNotificationXml notification) {
108                 LOG.debug("Notification answer: {}", dcaeMessages.postNotification(mountPointName, notification));
109             }
110
111                 @Override
112                 public void close() {
113                         try {
114                                 this.scheduler.awaitTermination(5, TimeUnit.SECONDS);
115                         } catch (InterruptedException e) {
116                         }
117                 }
118
119
120         }
121
122 }
123
124
125