a4a92f8830f070383c7be25d324a542f7dbc6654
[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.ParticipantRestart;
40 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantStatus;
41 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantStatusReq;
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         publisher.sendParticipantStatus(makeHeartbeat());
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());
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());
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.setParticipantSupportedElementType(cacheProvider.getSupportedAcElementTypes());
149
150         publisher.sendParticipantRegister(participantRegister);
151     }
152
153     /**
154      * Handle a participantRegister Ack message.
155      *
156      * @param participantRegisterAckMsg the participantRegisterAck message
157      */
158     @Timed(value = "listener.participant_register_ack", description = "PARTICIPANT_REGISTER_ACK messages received")
159     public void handleParticipantRegisterAck(ParticipantRegisterAck participantRegisterAckMsg) {
160         LOGGER.debug("ParticipantRegisterAck message received as responseTo {}",
161                 participantRegisterAckMsg.getResponseTo());
162         publisher.sendParticipantStatus(makeHeartbeat());
163     }
164
165     /**
166      * Method to send ParticipantDeregister message to automation composition runtime.
167      */
168     public void sendParticipantDeregister() {
169         var participantDeregister = new ParticipantDeregister();
170         participantDeregister.setParticipantId(cacheProvider.getParticipantId());
171         publisher.sendParticipantDeregister(participantDeregister);
172     }
173
174     /**
175      * Handle a participantDeregister Ack message.
176      *
177      * @param participantDeregisterAckMsg the participantDeregisterAck message
178      */
179     @Timed(value = "listener.participant_deregister_ack", description = "PARTICIPANT_DEREGISTER_ACK messages received")
180     public void handleParticipantDeregisterAck(ParticipantDeregisterAck participantDeregisterAckMsg) {
181         LOGGER.debug("ParticipantDeregisterAck message received as responseTo {}",
182                 participantDeregisterAckMsg.getResponseTo());
183     }
184
185     /**
186      * Handle a ParticipantPrime message.
187      *
188      * @param participantPrimeMsg the ParticipantPrime message
189      */
190     @Timed(value = "listener.participant_prime", description = "PARTICIPANT_PRIME messages received")
191     public void handleParticipantPrime(ParticipantPrime participantPrimeMsg) {
192         LOGGER.debug("ParticipantPrime message received for participantId {}", participantPrimeMsg.getParticipantId());
193         acDefinitionHandler.handlePrime(participantPrimeMsg);
194     }
195
196     /**
197      * Handle a ParticipantRestart message.
198      *
199      * @param participantRestartMsg the participantRestart message
200      */
201     @Timed(value = "listener.participant_restart", description = "PARTICIPANT_RESTART messages received")
202     public void handleParticipantRestart(ParticipantRestart participantRestartMsg) {
203         LOGGER.debug("ParticipantRestart message received for participantId {}",
204                 participantRestartMsg.getParticipantId());
205         acDefinitionHandler.handleParticipantRestart(participantRestartMsg);
206     }
207
208     /**
209      * Dispatch a heartbeat for this participant.
210      */
211     public void sendHeartbeat() {
212         if (publisher.isActive()) {
213             publisher.sendHeartbeat(makeHeartbeat());
214         }
215     }
216
217     /**
218      * Method to send heartbeat to automation composition runtime.
219      */
220     private ParticipantStatus makeHeartbeat() {
221         var heartbeat = new ParticipantStatus();
222         heartbeat.setParticipantId(cacheProvider.getParticipantId());
223         heartbeat.setState(ParticipantState.ON_LINE);
224         heartbeat.setParticipantSupportedElementType(cacheProvider.getSupportedAcElementTypes());
225
226         return heartbeat;
227     }
228 }