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