5ae8f0422cc0e44dea568e40b2f27350b7b338d4
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2024 Nordix Foundation.
4  * ================================================================================
5  * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.clamp.acm.participant.intermediary.handler;
24
25 import io.micrometer.core.annotation.Timed;
26 import lombok.RequiredArgsConstructor;
27 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher;
28 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
29 import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionDeploy;
30 import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionMigration;
31 import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionStateChange;
32 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantAckMessage;
33 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantDeregister;
34 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantDeregisterAck;
35 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantMessage;
36 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantPrime;
37 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantRegister;
38 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantRegisterAck;
39 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantStatus;
40 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantStatusReq;
41 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantSync;
42 import org.onap.policy.clamp.models.acm.messages.kafka.participant.PropertiesUpdate;
43 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.DeployOrder;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.stereotype.Component;
47
48 /**
49  * This class is responsible for managing the state of a participant.
50  */
51 @Component
52 @RequiredArgsConstructor
53 public class ParticipantHandler {
54     private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantHandler.class);
55
56     private final AutomationCompositionHandler automationCompositionHandler;
57     private final AcLockHandler acLockHandler;
58     private final AcDefinitionHandler acDefinitionHandler;
59     private final ParticipantMessagePublisher publisher;
60     private final CacheProvider cacheProvider;
61
62     /**
63      * Method which handles a participant health check event from clamp.
64      *
65      * @param participantStatusReqMsg participant participantStatusReq message
66      */
67     @Timed(value = "listener.participant_status_req", description = "PARTICIPANT_STATUS_REQ messages received")
68     public void handleParticipantStatusReq(final ParticipantStatusReq participantStatusReqMsg) {
69         sendHeartbeat();
70     }
71
72     /**
73      * Handle a automation composition update message.
74      *
75      * @param updateMsg the update message
76      */
77     @Timed(
78             value = "listener.automation_composition_update",
79             description = "AUTOMATION_COMPOSITION_UPDATE messages received")
80     public void handleAutomationCompositionDeploy(AutomationCompositionDeploy updateMsg) {
81         automationCompositionHandler.handleAutomationCompositionDeploy(updateMsg);
82     }
83
84     /**
85      * Handle a automation composition state change message.
86      *
87      * @param stateChangeMsg the state change message
88      */
89     @Timed(
90             value = "listener.automation_composition_state_change",
91             description = "AUTOMATION_COMPOSITION_STATE_CHANGE messages received")
92     public void handleAutomationCompositionStateChange(AutomationCompositionStateChange stateChangeMsg) {
93         if (DeployOrder.NONE.equals(stateChangeMsg.getDeployOrderedState())) {
94             acLockHandler.handleAutomationCompositionStateChange(stateChangeMsg);
95         } else {
96             automationCompositionHandler.handleAutomationCompositionStateChange(stateChangeMsg);
97         }
98     }
99
100     /**
101      * Handle a automation composition migration message.
102      *
103      * @param migrationMsg the migration message
104      */
105     @Timed(
106             value = "listener.automation_composition_migration",
107             description = "AUTOMATION_COMPOSITION_MIGRATION messages received")
108     public void handleAutomationCompositionMigration(AutomationCompositionMigration migrationMsg) {
109         automationCompositionHandler.handleAutomationCompositionMigration(migrationMsg);
110     }
111
112     /**
113      * Handle a automation composition property update message.
114      *
115      * @param propertyUpdateMsg the property update message
116      */
117     @Timed(value = "listener.properties_update", description = "PROPERTIES_UPDATE message received")
118     public void handleAcPropertyUpdate(PropertiesUpdate propertyUpdateMsg) {
119         automationCompositionHandler.handleAcPropertyUpdate(propertyUpdateMsg);
120     }
121
122     /**
123      * Check if a participant message applies to this participant handler.
124      *
125      * @param participantMsg the message to check
126      * @return true if it applies, false otherwise
127      */
128     public boolean appliesTo(ParticipantMessage participantMsg) {
129         return participantMsg.appliesTo(cacheProvider.getParticipantId(), cacheProvider.getReplicaId());
130     }
131
132     /**
133      * Check if a participant message applies to this participant handler.
134      *
135      * @param participantMsg the message to check
136      * @return true if it applies, false otherwise
137      */
138     public boolean appliesTo(ParticipantAckMessage participantMsg) {
139         return participantMsg.appliesTo(cacheProvider.getParticipantId(), cacheProvider.getReplicaId());
140     }
141
142     /**
143      * Method to send ParticipantRegister message to automation composition runtime.
144      */
145     public void sendParticipantRegister() {
146         var participantRegister = new ParticipantRegister();
147         participantRegister.setParticipantId(cacheProvider.getParticipantId());
148         participantRegister.setReplicaId(cacheProvider.getReplicaId());
149         participantRegister.setParticipantSupportedElementType(cacheProvider.getSupportedAcElementTypes());
150
151         publisher.sendParticipantRegister(participantRegister);
152     }
153
154     /**
155      * Handle a participantRegister Ack message.
156      *
157      * @param participantRegisterAckMsg the participantRegisterAck message
158      */
159     @Timed(value = "listener.participant_register_ack", description = "PARTICIPANT_REGISTER_ACK messages received")
160     public void handleParticipantRegisterAck(ParticipantRegisterAck participantRegisterAckMsg) {
161         LOGGER.debug("ParticipantRegisterAck message received as responseTo {}",
162                 participantRegisterAckMsg.getResponseTo());
163         cacheProvider.setRegistered(true);
164         publisher.sendParticipantStatus(makeHeartbeat());
165     }
166
167     /**
168      * Method to send ParticipantDeregister message to automation composition runtime.
169      */
170     public void sendParticipantDeregister() {
171         var participantDeregister = new ParticipantDeregister();
172         participantDeregister.setParticipantId(cacheProvider.getParticipantId());
173         participantDeregister.setReplicaId(cacheProvider.getReplicaId());
174         publisher.sendParticipantDeregister(participantDeregister);
175     }
176
177     /**
178      * Handle a participantDeregister Ack message.
179      *
180      * @param participantDeregisterAckMsg the participantDeregisterAck message
181      */
182     @Timed(value = "listener.participant_deregister_ack", description = "PARTICIPANT_DEREGISTER_ACK messages received")
183     public void handleParticipantDeregisterAck(ParticipantDeregisterAck participantDeregisterAckMsg) {
184         LOGGER.debug("ParticipantDeregisterAck message received as responseTo {}",
185                 participantDeregisterAckMsg.getResponseTo());
186     }
187
188     /**
189      * Handle a ParticipantPrime message.
190      *
191      * @param participantPrimeMsg the ParticipantPrime message
192      */
193     @Timed(value = "listener.participant_prime", description = "PARTICIPANT_PRIME messages received")
194     public void handleParticipantPrime(ParticipantPrime participantPrimeMsg) {
195         LOGGER.debug("ParticipantPrime message received for participantId {}", participantPrimeMsg.getParticipantId());
196         acDefinitionHandler.handlePrime(participantPrimeMsg);
197     }
198
199     /**
200      * Handle a ParticipantSync message.
201      *
202      * @param participantSyncMsg the participantSync message
203      */
204     @Timed(value = "listener.participant_sync_msg", description = "PARTICIPANT_SYNC messages received")
205     public void handleParticipantSync(ParticipantSync participantSyncMsg) {
206         if (participantSyncMsg.getExcludeReplicas().contains(cacheProvider.getReplicaId())) {
207             LOGGER.debug("Ignore ParticipantSync message {}", participantSyncMsg.getMessageId());
208             return;
209         }
210         LOGGER.debug("ParticipantSync message received for participantId {}", participantSyncMsg.getParticipantId());
211         acDefinitionHandler.handleParticipantSync(participantSyncMsg);
212     }
213
214     /**
215      * Dispatch a heartbeat for this participant.
216      */
217     public void sendHeartbeat() {
218         if (publisher.isActive()) {
219             if (!cacheProvider.isRegistered()) {
220                 sendParticipantRegister();
221             } else {
222                 publisher.sendParticipantStatus(makeHeartbeat());
223             }
224         }
225     }
226
227     /**
228      * Method to send heartbeat to automation composition runtime.
229      */
230     private ParticipantStatus makeHeartbeat() {
231         var heartbeat = new ParticipantStatus();
232         heartbeat.setParticipantId(cacheProvider.getParticipantId());
233         heartbeat.setReplicaId(cacheProvider.getReplicaId());
234         heartbeat.setState(ParticipantState.ON_LINE);
235         heartbeat.setParticipantSupportedElementType(cacheProvider.getSupportedAcElementTypes());
236
237         return heartbeat;
238     }
239 }