9023d9460b9b5ccba5cc0d9dfb986a2c81ce9376
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2023 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.acm.participant.intermediary.comm;
23
24 import io.micrometer.core.annotation.Timed;
25 import java.util.List;
26 import javax.ws.rs.core.Response.Status;
27 import org.onap.policy.clamp.acm.participant.intermediary.handler.Publisher;
28 import org.onap.policy.clamp.common.acm.exception.AutomationCompositionRuntimeException;
29 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionAck;
30 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregister;
31 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegister;
32 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatus;
33 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantUpdateAck;
34 import org.onap.policy.common.endpoints.event.comm.TopicSink;
35 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.stereotype.Component;
39
40 /**
41  * This class is used to send Participant Status messages to clamp using TopicSinkClient.
42  *
43  */
44 @Component
45 public class ParticipantMessagePublisher implements Publisher {
46     private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantMessagePublisher.class);
47     private static final String NOT_ACTIVE_TEXT = "Not Active!";
48
49     private boolean active = false;
50     private TopicSinkClient topicSinkClient;
51
52     /**
53      * Constructor for instantiating ParticipantMessagePublisher.
54      *
55      * @param topicSinks the topic sinks
56      */
57     @Override
58     public void active(List<TopicSink> topicSinks) {
59         if (topicSinks.size() != 1) {
60             throw new IllegalArgumentException("Configuration unsupported, Topic sinks greater than 1");
61         }
62         this.topicSinkClient = new TopicSinkClient(topicSinks.get(0));
63         active = true;
64     }
65
66     /**
67      * Method to send Participant Status message to clamp on demand.
68      *
69      * @param participantStatus the Participant Status
70      */
71     @Timed(value = "publisher.participant_status", description = "PARTICIPANT_STATUS messages published")
72     public void sendParticipantStatus(final ParticipantStatus participantStatus) {
73         validate();
74         topicSinkClient.send(participantStatus);
75         LOGGER.info("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     @Timed(value = "publisher.participant_register", description = "PARTICIPANT_REGISTER messages published")
84     public void sendParticipantRegister(final ParticipantRegister participantRegister) {
85         validate();
86         topicSinkClient.send(participantRegister);
87         LOGGER.info("Sent Participant Register message to CLAMP - {}", participantRegister);
88     }
89
90     /**
91      * Method to send Participant Status message to clamp on demand.
92      *
93      * @param participantDeregister the Participant Status
94      */
95     @Timed(value = "publisher.participant_deregister", description = "PARTICIPANT_DEREGISTER messages published")
96     public void sendParticipantDeregister(final ParticipantDeregister participantDeregister) {
97         validate();
98         topicSinkClient.send(participantDeregister);
99         LOGGER.debug("Sent Participant Deregister message to CLAMP - {}", participantDeregister);
100     }
101
102     /**
103      * Method to send Participant Update Ack message to runtime.
104      *
105      * @param participantUpdateAck the Participant Update Ack
106      */
107     @Timed(value = "publisher.participant_update_ack", description = "PARTICIPANT_UPDATE_ACK messages published")
108     public void sendParticipantUpdateAck(final ParticipantUpdateAck participantUpdateAck) {
109         validate();
110         topicSinkClient.send(participantUpdateAck);
111         LOGGER.debug("Sent Participant Update Ack message to CLAMP - {}", participantUpdateAck);
112     }
113
114     /**
115      * Method to send AutomationComposition Update/StateChange Ack message to runtime.
116      *
117      * @param automationCompositionAck AutomationComposition Update/StateChange Ack
118      */
119     @Timed(value = "publisher.automation_composition_update_ack",
120             description = "AUTOMATION_COMPOSITION_UPDATE_ACK/AUTOMATION_COMPOSITION_STATECHANGE_ACK messages published")
121     public void sendAutomationCompositionAck(final AutomationCompositionAck automationCompositionAck) {
122         validate();
123         topicSinkClient.send(automationCompositionAck);
124         LOGGER.debug("Sent AutomationComposition Update/StateChange Ack to runtime - {}", automationCompositionAck);
125     }
126
127     /**
128      * Method to send Participant heartbeat to clamp on demand.
129      *
130      * @param participantStatus the Participant Status
131      */
132     @Timed(value = "publisher.participant_status", description = "PARTICIPANT_STATUS messages published")
133     public void sendHeartbeat(final ParticipantStatus participantStatus) {
134         validate();
135         topicSinkClient.send(participantStatus);
136         LOGGER.debug("Sent Participant heartbeat to CLAMP - {}", participantStatus);
137     }
138
139     private void validate() {
140         if (!active) {
141             throw new AutomationCompositionRuntimeException(Status.NOT_ACCEPTABLE, NOT_ACTIVE_TEXT);
142         }
143     }
144
145     @Override
146     public void stop() {
147         active = false;
148     }
149 }