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