weekly sdnr code sync
[ccsdk/features.git] / sdnr / wt / devicemanager / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / dcaeconnector / impl / DcaeProviderWorker.java
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 package org.onap.ccsdk.features.sdnr.wt.devicemanager.dcaeconnector.impl;
19
20 import java.util.concurrent.Executors;
21 import java.util.concurrent.ScheduledExecutorService;
22 import java.util.concurrent.ScheduledFuture;
23 import java.util.concurrent.TimeUnit;
24
25 import org.onap.ccsdk.features.sdnr.wt.devicemanager.dcaeconnector.impl.config.DcaeConfig;
26 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.DeviceManagerImpl;
27 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.ProblemNotificationXml;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 class DcaeProviderWorker implements AutoCloseable {
32
33     private static final Logger LOG = LoggerFactory.getLogger(DcaeProviderWorker.class);
34
35     private static final int MIN_HEARTBEAT_TIME_SECONDS = 30;
36
37     private final ScheduledExecutorService scheduler;
38     private final DcaeSenderImpl dcaepClient;
39     private final DcaeMessages dcaeMessages;
40     private final ScheduledFuture<?> taskReference;
41
42     public DcaeProviderWorker(DcaeConfig configuration, String entityName, DeviceManagerImpl deviceManager) {
43
44         //Start services
45         LOG.info("Configuration: " + configuration);
46         int heartbeatSeconds = configuration.getTimerPeriodSeconds();
47         if (heartbeatSeconds < MIN_HEARTBEAT_TIME_SECONDS) {
48             heartbeatSeconds = MIN_HEARTBEAT_TIME_SECONDS;
49             LOG.info("Adjust heartbeat intervall to minimum of { } seconds.", heartbeatSeconds);
50         }
51
52         dcaepClient = new DcaeSenderImpl(configuration.getEventReveicerUrl(), configuration.getUserCredentials());
53         dcaeMessages = new DcaeMessages(dcaepClient, entityName, heartbeatSeconds, deviceManager);
54
55         //Activate task
56         LOG.info("Create Fault manager client Task");
57         this.scheduler = Executors.newSingleThreadScheduledExecutor();
58         Runnable task = new DcaeProviderTask(dcaeMessages);
59
60         LOG.info("Fault task created with " + heartbeatSeconds + " Seconds");
61         this.taskReference = this.scheduler.scheduleAtFixedRate(task, 0, heartbeatSeconds, TimeUnit.SECONDS);
62         LOG.info("Fault task scheduled");
63     }
64
65     public void sendProblemNotification(String mountPointName, ProblemNotificationXml notification) {
66         LOG.debug("Notification answer: {}", dcaeMessages.postNotification(mountPointName, notification));
67     }
68
69     @Override
70     public void close() {
71         this.taskReference.cancel(false);
72         try {
73             this.scheduler.shutdown();
74             this.scheduler.awaitTermination(5, TimeUnit.SECONDS);
75         } catch (InterruptedException | SecurityException e) {
76             LOG.debug("Schedler shutdown interrupted with exception: ", e);
77             if (e instanceof InterruptedException) {
78                 Thread.currentThread().interrupt();
79             }
80         }
81     }
82
83
84 }