79d62623c810634eeac2ac07a4d9dd6f6f54f563
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * 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
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.clamp.controlloop.participant.intermediary.comm;
23
24 import java.util.List;
25 import javax.ws.rs.core.Response.Status;
26 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
27 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopAck;
28 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregister;
29 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegister;
30 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus;
31 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdateAck;
32 import org.onap.policy.clamp.controlloop.participant.intermediary.handler.Publisher;
33 import org.onap.policy.common.endpoints.event.comm.TopicSink;
34 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.stereotype.Component;
38
39 /**
40  * This class is used to send Participant Status messages to clamp using TopicSinkClient.
41  *
42  */
43 @Component
44 public class ParticipantMessagePublisher implements Publisher {
45     private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantMessagePublisher.class);
46     private static final String NOT_ACTIVE_TEXT = "Not Active!";
47
48     private boolean active = false;
49     private TopicSinkClient topicSinkClient;
50
51     /**
52      * Constructor for instantiating ParticipantMessagePublisher.
53      *
54      * @param topicSinks the topic sinks
55      */
56     @Override
57     public void active(List<TopicSink> topicSinks) {
58         if (topicSinks.size() != 1) {
59             throw new IllegalArgumentException("Configuration unsupported, Topic sinks greater than 1");
60         }
61         this.topicSinkClient = new TopicSinkClient(topicSinks.get(0));
62         active = true;
63     }
64
65     /**
66      * Method to send Participant Status message to clamp on demand.
67      *
68      * @param participantStatus the Participant Status
69      */
70     public void sendParticipantStatus(final ParticipantStatus participantStatus) {
71         if (!active) {
72             throw new ControlLoopRuntimeException(Status.NOT_ACCEPTABLE, NOT_ACTIVE_TEXT);
73         }
74         topicSinkClient.send(participantStatus);
75         LOGGER.debug("Sent Participant Status message to CLAMP - {}", participantStatus);
76     }
77
78     /**
79      * Method to send Participant Status message to clamp on demand.
80      *
81      * @param participantRegister the Participant Status
82      */
83     public void sendParticipantRegister(final ParticipantRegister participantRegister) {
84         if (!active) {
85             throw new ControlLoopRuntimeException(Status.NOT_ACCEPTABLE, NOT_ACTIVE_TEXT);
86         }
87         topicSinkClient.send(participantRegister);
88         LOGGER.debug("Sent Participant Register message to CLAMP - {}", participantRegister);
89     }
90
91     /**
92      * Method to send Participant Status message to clamp on demand.
93      *
94      * @param participantDeregister the Participant Status
95      */
96     public void sendParticipantDeregister(final ParticipantDeregister participantDeregister) {
97         if (!active) {
98             throw new ControlLoopRuntimeException(Status.NOT_ACCEPTABLE, NOT_ACTIVE_TEXT);
99         }
100         topicSinkClient.send(participantDeregister);
101         LOGGER.debug("Sent Participant Deregister message to CLAMP - {}", participantDeregister);
102     }
103
104     /**
105      * Method to send Participant Update Ack message to runtime.
106      *
107      * @param participantUpdateAck the Participant Update Ack
108      */
109     public void sendParticipantUpdateAck(final ParticipantUpdateAck participantUpdateAck) {
110         if (!active) {
111             throw new ControlLoopRuntimeException(Status.NOT_ACCEPTABLE, NOT_ACTIVE_TEXT);
112         }
113         topicSinkClient.send(participantUpdateAck);
114         LOGGER.debug("Sent Participant Update Ack message to CLAMP - {}", participantUpdateAck);
115     }
116
117     /**
118      * Method to send ControlLoop Update/StateChange Ack message to runtime.
119      *
120      * @param controlLoopAck ControlLoop Update/StateChange Ack
121      */
122     public void sendControlLoopAck(final ControlLoopAck controlLoopAck) {
123         if (!active) {
124             throw new ControlLoopRuntimeException(Status.NOT_ACCEPTABLE, NOT_ACTIVE_TEXT);
125         }
126         topicSinkClient.send(controlLoopAck);
127         LOGGER.debug("Sent ControlLoop Update/StateChange Ack to runtime - {}", controlLoopAck);
128     }
129
130     /**
131      * Method to send Participant heartbeat to clamp on demand.
132      *
133      * @param participantStatus the Participant Status
134      */
135     public void sendHeartbeat(final ParticipantStatus participantStatus) {
136         if (!active) {
137             throw new ControlLoopRuntimeException(Status.NOT_ACCEPTABLE, NOT_ACTIVE_TEXT);
138         }
139         topicSinkClient.send(participantStatus);
140         LOGGER.debug("Sent Participant heartbeat to CLAMP - {}", participantStatus);
141     }
142
143     @Override
144     public void stop() {
145         active = false;
146     }
147 }