f520efc982f3f3ce588aadeb42de457b4b3dd3d6
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2025 OpenInfra Foundation Europe. All rights reserved.
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.common.acm.utils.NetLoggerUtil;
31 import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionDeployAck;
32 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantDeregister;
33 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantPrimeAck;
34 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantRegister;
35 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantReqSync;
36 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantStatus;
37 import org.onap.policy.common.message.bus.event.TopicSink;
38 import org.onap.policy.common.message.bus.event.client.TopicSinkClient;
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 String NOT_ACTIVE_TEXT = "Not Active!";
48
49     @Getter
50     private boolean active = false;
51     private TopicSinkClient topicSinkClient;
52
53     /**
54      * Constructor for instantiating ParticipantMessagePublisher.
55      *
56      * @param topicSinks the topic sinks
57      */
58     @Override
59     public void active(List<TopicSink> topicSinks) {
60         if (topicSinks.size() != 1) {
61             throw new IllegalArgumentException("Configuration unsupported, Topic sinks greater than 1");
62         }
63         this.topicSinkClient = new TopicSinkClient(topicSinks.get(0));
64         active = true;
65     }
66
67     /**
68      * Method to send Participant Request Sync message to clamp.
69      *
70      * @param participantReqSync the Participant Request Sync
71      */
72     @Timed(value = "publisher.participant_req_sync", description = "PARTICIPANT_REQ_SYNC_MSG messages published")
73     public void sendParticipantReqSync(final ParticipantReqSync participantReqSync) {
74         validate();
75         topicSinkClient.send(participantReqSync);
76         NetLoggerUtil.log(NetLoggerUtil.EventType.OUT, topicSinkClient.getSink().getTopicCommInfrastructure(),
77                 topicSinkClient.getTopic(), "Sent Participant Request Sync to CLAMP - "
78                         + participantReqSync.toString());
79     }
80
81     /**
82      * Method to send Participant Status message to clamp on demand.
83      *
84      * @param participantStatus the Participant Status
85      */
86     @Timed(value = "publisher.participant_status", description = "PARTICIPANT_STATUS messages published")
87     public void sendParticipantStatus(final ParticipantStatus participantStatus) {
88         validate();
89         topicSinkClient.send(participantStatus);
90         NetLoggerUtil.log(NetLoggerUtil.EventType.OUT, topicSinkClient.getSink().getTopicCommInfrastructure(),
91                 topicSinkClient.getTopic(), "Sent Participant Status message to CLAMP - "
92                         + participantStatus.toString());
93     }
94
95     /**
96      * Method to send Participant Status message to clamp on demand.
97      *
98      * @param participantRegister the Participant Status
99      */
100     @Timed(value = "publisher.participant_register", description = "PARTICIPANT_REGISTER messages published")
101     public void sendParticipantRegister(final ParticipantRegister participantRegister) {
102         validate();
103         topicSinkClient.send(participantRegister);
104         NetLoggerUtil.log(NetLoggerUtil.EventType.OUT, topicSinkClient.getSink().getTopicCommInfrastructure(),
105                 topicSinkClient.getTopic(), "Sent Participant Register message to CLAMP - "
106                         + participantRegister.toString());
107     }
108
109     /**
110      * Method to send Participant Status message to clamp on demand.
111      *
112      * @param participantDeregister the Participant Status
113      */
114     @Timed(value = "publisher.participant_deregister", description = "PARTICIPANT_DEREGISTER messages published")
115     public void sendParticipantDeregister(final ParticipantDeregister participantDeregister) {
116         validate();
117         topicSinkClient.send(participantDeregister);
118         NetLoggerUtil.log(NetLoggerUtil.EventType.OUT, topicSinkClient.getSink().getTopicCommInfrastructure(),
119                 topicSinkClient.getTopic(), "Sent Participant Deregister message to CLAMP - "
120                         + participantDeregister.toString());
121     }
122
123     /**
124      * Method to send Participant Prime Ack message to runtime.
125      *
126      * @param participantPrimeAck the Participant Prime Ack
127      */
128     @Timed(value = "publisher.participant_prime_ack", description = "PARTICIPANT_PRIME_ACK messages published")
129     public void sendParticipantPrimeAck(final ParticipantPrimeAck participantPrimeAck) {
130         validate();
131         topicSinkClient.send(participantPrimeAck);
132         NetLoggerUtil.log(NetLoggerUtil.EventType.OUT, topicSinkClient.getSink().getTopicCommInfrastructure(),
133                 topicSinkClient.getTopic(), "Sent Participant Prime Ack message to CLAMP - "
134                         + participantPrimeAck.toString());
135     }
136
137     /**
138      * Method to send AutomationComposition Update/StateChange Ack message to runtime.
139      *
140      * @param automationCompositionAck AutomationComposition Update/StateChange Ack
141      */
142     @Timed(value = "publisher.automation_composition_update_ack",
143             description = "AUTOMATION_COMPOSITION_UPDATE_ACK/AUTOMATION_COMPOSITION_STATECHANGE_ACK messages published")
144     public void sendAutomationCompositionAck(final AutomationCompositionDeployAck automationCompositionAck) {
145         validate();
146         NetLoggerUtil.log(NetLoggerUtil.EventType.OUT, topicSinkClient.getSink().getTopicCommInfrastructure(),
147                 topicSinkClient.getTopic(), "Sent AutomationComposition Update/StateChange Ack to runtime - "
148                         + automationCompositionAck.toString());
149     }
150
151     private void validate() {
152         if (!active) {
153             throw new AutomationCompositionRuntimeException(Status.NOT_ACCEPTABLE, NOT_ACTIVE_TEXT);
154         }
155     }
156
157     @Override
158     public void stop() {
159         active = false;
160     }
161 }