8d33b3ed6a4210b9eaa0354c84efb706babdfae7
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2023 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 java.util.ArrayList;
27 import java.util.List;
28 import lombok.RequiredArgsConstructor;
29 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher;
30 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
31 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
32 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
33 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionDeploy;
34 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionMigration;
35 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionStateChange;
36 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantAckMessage;
37 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregister;
38 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregisterAck;
39 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessage;
40 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantPrime;
41 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegister;
42 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegisterAck;
43 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRestart;
44 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatus;
45 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatusReq;
46 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.PropertiesUpdate;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.springframework.stereotype.Component;
50
51 /**
52  * This class is responsible for managing the state of a participant.
53  */
54 @Component
55 @RequiredArgsConstructor
56 public class ParticipantHandler {
57     private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantHandler.class);
58
59     private final AutomationCompositionHandler automationCompositionHandler;
60     private final ParticipantMessagePublisher publisher;
61     private final CacheProvider cacheProvider;
62
63     /**
64      * Method which handles a participant health check event from clamp.
65      *
66      * @param participantStatusReqMsg participant participantStatusReq message
67      */
68     @Timed(value = "listener.participant_status_req", description = "PARTICIPANT_STATUS_REQ messages received")
69     public void handleParticipantStatusReq(final ParticipantStatusReq participantStatusReqMsg) {
70         publisher.sendParticipantStatus(makeHeartbeat());
71     }
72
73     /**
74      * Handle a automation composition update message.
75      *
76      * @param updateMsg the update message
77      */
78     @Timed(
79             value = "listener.automation_composition_update",
80             description = "AUTOMATION_COMPOSITION_UPDATE messages received")
81     public void handleAutomationCompositionDeploy(AutomationCompositionDeploy updateMsg) {
82         automationCompositionHandler.handleAutomationCompositionDeploy(updateMsg);
83     }
84
85     /**
86      * Handle a automation composition state change message.
87      *
88      * @param stateChangeMsg the state change message
89      */
90     @Timed(
91             value = "listener.automation_composition_state_change",
92             description = "AUTOMATION_COMPOSITION_STATE_CHANGE messages received")
93     public void handleAutomationCompositionStateChange(AutomationCompositionStateChange stateChangeMsg) {
94         automationCompositionHandler.handleAutomationCompositionStateChange(stateChangeMsg);
95     }
96
97     /**
98      * Handle a automation composition migration message.
99      *
100      * @param migrationMsg the migration message
101      */
102     @Timed(
103             value = "listener.automation_composition_migration",
104             description = "AUTOMATION_COMPOSITION_MIGRATION messages received")
105     public void handleAutomationCompositionMigration(AutomationCompositionMigration migrationMsg) {
106         automationCompositionHandler.handleAutomationCompositionMigration(migrationMsg);
107     }
108
109     /**
110      * Handle a automation composition property update message.
111      *
112      * @param propertyUpdateMsg the property update message
113      */
114     @Timed(value = "listener.properties_update", description = "PROPERTIES_UPDATE message received")
115     public void handleAcPropertyUpdate(PropertiesUpdate propertyUpdateMsg) {
116         automationCompositionHandler.handleAcPropertyUpdate(propertyUpdateMsg);
117     }
118
119     /**
120      * Check if a participant message applies to this participant handler.
121      *
122      * @param participantMsg the message to check
123      * @return true if it applies, false otherwise
124      */
125     public boolean appliesTo(ParticipantMessage participantMsg) {
126         return participantMsg.appliesTo(cacheProvider.getParticipantId());
127     }
128
129     /**
130      * Check if a participant message applies to this participant handler.
131      *
132      * @param participantMsg the message to check
133      * @return true if it applies, false otherwise
134      */
135     public boolean appliesTo(ParticipantAckMessage participantMsg) {
136         return participantMsg.appliesTo(cacheProvider.getParticipantId());
137     }
138
139     /**
140      * Method to send ParticipantRegister message to automation composition runtime.
141      */
142     public void sendParticipantRegister() {
143         var participantRegister = new ParticipantRegister();
144         participantRegister.setParticipantId(cacheProvider.getParticipantId());
145         participantRegister.setParticipantSupportedElementType(cacheProvider.getSupportedAcElementTypes());
146
147         publisher.sendParticipantRegister(participantRegister);
148     }
149
150     /**
151      * Handle a participantRegister Ack message.
152      *
153      * @param participantRegisterAckMsg the participantRegisterAck message
154      */
155     @Timed(value = "listener.participant_register_ack", description = "PARTICIPANT_REGISTER_ACK messages received")
156     public void handleParticipantRegisterAck(ParticipantRegisterAck participantRegisterAckMsg) {
157         LOGGER.debug("ParticipantRegisterAck message received as responseTo {}",
158                 participantRegisterAckMsg.getResponseTo());
159         publisher.sendParticipantStatus(makeHeartbeat());
160     }
161
162     /**
163      * Method to send ParticipantDeregister message to automation composition runtime.
164      */
165     public void sendParticipantDeregister() {
166         var participantDeregister = new ParticipantDeregister();
167         participantDeregister.setParticipantId(cacheProvider.getParticipantId());
168         publisher.sendParticipantDeregister(participantDeregister);
169     }
170
171     /**
172      * Handle a participantDeregister Ack message.
173      *
174      * @param participantDeregisterAckMsg the participantDeregisterAck message
175      */
176     @Timed(value = "listener.participant_deregister_ack", description = "PARTICIPANT_DEREGISTER_ACK messages received")
177     public void handleParticipantDeregisterAck(ParticipantDeregisterAck participantDeregisterAckMsg) {
178         LOGGER.debug("ParticipantDeregisterAck message received as responseTo {}",
179                 participantDeregisterAckMsg.getResponseTo());
180     }
181
182     /**
183      * Handle a ParticipantPrime message.
184      *
185      * @param participantPrimeMsg the ParticipantPrime message
186      */
187     @Timed(value = "listener.participant_prime", description = "PARTICIPANT_PRIME messages received")
188     public void handleParticipantPrime(ParticipantPrime participantPrimeMsg) {
189         LOGGER.debug("ParticipantPrime message received for participantId {}", participantPrimeMsg.getParticipantId());
190
191         if (!participantPrimeMsg.getParticipantDefinitionUpdates().isEmpty()) {
192             // prime
193             List<AutomationCompositionElementDefinition> list = new ArrayList<>();
194             for (var participantDefinition : participantPrimeMsg.getParticipantDefinitionUpdates()) {
195                 if (participantDefinition.getParticipantId().equals(cacheProvider.getParticipantId())) {
196                     list.addAll(participantDefinition.getAutomationCompositionElementDefinitionList());
197                 }
198             }
199             cacheProvider.addElementDefinition(participantPrimeMsg.getCompositionId(), list);
200             automationCompositionHandler.prime(participantPrimeMsg.getMessageId(),
201                     participantPrimeMsg.getCompositionId(), list);
202         } else {
203             // deprime
204             automationCompositionHandler.deprime(participantPrimeMsg.getMessageId(),
205                     participantPrimeMsg.getCompositionId());
206         }
207     }
208
209     /**
210      * Handle a ParticipantRestart message.
211      *
212      * @param participantRestartMsg the participantRestart message
213      */
214     @Timed(value = "listener.participant_restart", description = "PARTICIPANT_RESTART messages received")
215     public void handleParticipantRestart(ParticipantRestart participantRestartMsg) {
216         LOGGER.debug("ParticipantRestart message received for participantId {}",
217                 participantRestartMsg.getParticipantId());
218         List<AutomationCompositionElementDefinition> list = new ArrayList<>();
219         for (var participantDefinition : participantRestartMsg.getParticipantDefinitionUpdates()) {
220             list.addAll(participantDefinition.getAutomationCompositionElementDefinitionList());
221         }
222         if (!AcTypeState.COMMISSIONED.equals(participantRestartMsg.getState())) {
223             cacheProvider.addElementDefinition(participantRestartMsg.getCompositionId(), list);
224         }
225         automationCompositionHandler.restarted(participantRestartMsg.getMessageId(),
226                 participantRestartMsg.getCompositionId(), list, participantRestartMsg.getState(),
227                 participantRestartMsg.getAutomationcompositionList());
228     }
229
230     /**
231      * Dispatch a heartbeat for this participant.
232      */
233     public void sendHeartbeat() {
234         if (publisher.isActive()) {
235             publisher.sendHeartbeat(makeHeartbeat());
236         }
237     }
238
239     /**
240      * Method to send heartbeat to automation composition runtime.
241      */
242     private ParticipantStatus makeHeartbeat() {
243         var heartbeat = new ParticipantStatus();
244         heartbeat.setParticipantId(cacheProvider.getParticipantId());
245         heartbeat.setState(ParticipantState.ON_LINE);
246         heartbeat.setParticipantSupportedElementType(cacheProvider.getSupportedAcElementTypes());
247
248         return heartbeat;
249     }
250 }