197b34ef54bc2c1b96d9e37f07ed1581e1d13217
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21
22 package org.onap.policy.apex.starter.handler;
23
24 import java.util.List;
25
26 import org.onap.policy.apex.starter.parameters.PdpStatusParameters;
27 import org.onap.policy.common.capabilities.Startable;
28 import org.onap.policy.common.endpoints.event.comm.TopicSink;
29 import org.onap.policy.common.endpoints.event.comm.TopicSource;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * This class manages the communication between apex pdp and pap.
35  *
36  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
37  */
38 public class CommunicationHandler implements Startable {
39
40     private static final Logger LOGGER = LoggerFactory.getLogger(CommunicationHandler.class);
41
42     private List<TopicSink> topicSinks; // topics to which apex-pdp sends pdp status
43     private List<TopicSource> topicSources; // topics to which apex-pdp listens to for messages from pap.
44
45     private PdpStatusPublisher pdpStatusPublisher;
46     private PdpStatusParameters pdpStatusParameters;
47
48     /**
49      * Constructor for instantiating CommunicationHandler
50      *
51      * @param topicSinks topics to which apex-pdp sends pdp status
52      * @param topicSources topics to which apex-pdp listens to for messages from pap.
53      * @param pdpStatusParameters pdp status parameters read from the configuration file
54      */
55     public CommunicationHandler(final List<TopicSink> topicSinks, final List<TopicSource> topicSources,
56             final PdpStatusParameters pdpStatusParameters) {
57         this.topicSinks = topicSinks;
58         this.topicSources = topicSources;
59         this.pdpStatusParameters = pdpStatusParameters;
60     }
61
62     @Override
63     public boolean start() {
64         try {
65             pdpStatusPublisher = new PdpStatusPublisher(topicSinks, pdpStatusParameters.getTimeInterval());
66         } catch (final Exception e) {
67             LOGGER.error("Failed to start communication handler for apex-pdp", e);
68             return false;
69         }
70         return true;
71     }
72
73     @Override
74     public boolean stop() {
75         try {
76             pdpStatusPublisher.terminate();
77         } catch (final Exception e) {
78             LOGGER.error("Failed to stop communication handler for apex-pdp", e);
79             return false;
80         }
81         return true;
82     }
83
84     @Override
85     public void shutdown() {
86         stop();
87     }
88
89     @Override
90     public boolean isAlive() {
91         return pdpStatusPublisher.isAlive();
92     }
93
94 }