8d906c07b50f3ee24a002a9c2fb44184b143956f
[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.AutomationCompositionInfo;
33 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
34 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionDeploy;
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 AutomationCompositionOutHandler automationCompositionOutHandler;
61     private final ParticipantMessagePublisher publisher;
62     private final CacheProvider cacheProvider;
63
64     /**
65      * Method which handles a participant health check event from clamp.
66      *
67      * @param participantStatusReqMsg participant participantStatusReq message
68      */
69     @Timed(value = "listener.participant_status_req", description = "PARTICIPANT_STATUS_REQ messages received")
70     public void handleParticipantStatusReq(final ParticipantStatusReq participantStatusReqMsg) {
71         publisher.sendParticipantStatus(makeHeartbeat());
72     }
73
74     /**
75      * Handle a automation composition update message.
76      *
77      * @param updateMsg the update message
78      */
79     @Timed(
80             value = "listener.automation_composition_update",
81             description = "AUTOMATION_COMPOSITION_UPDATE messages received")
82     public void handleAutomationCompositionDeploy(AutomationCompositionDeploy updateMsg) {
83         automationCompositionHandler.handleAutomationCompositionDeploy(updateMsg);
84     }
85
86     /**
87      * Handle a automation composition state change message.
88      *
89      * @param stateChangeMsg the state change message
90      */
91     @Timed(
92             value = "listener.automation_composition_state_change",
93             description = "AUTOMATION_COMPOSITION_STATE_CHANGE messages received")
94     public void handleAutomationCompositionStateChange(AutomationCompositionStateChange stateChangeMsg) {
95         automationCompositionHandler.handleAutomationCompositionStateChange(stateChangeMsg);
96     }
97
98     /**
99      * Handle a automation composition property update message.
100      *
101      * @param propertyUpdateMsg the property update message
102      */
103     @Timed(value = "listener.properties_update", description = "PROPERTIES_UPDATE message received")
104     public void handleAcPropertyUpdate(PropertiesUpdate propertyUpdateMsg) {
105         automationCompositionHandler.handleAcPropertyUpdate(propertyUpdateMsg);
106     }
107
108     /**
109      * Check if a participant message applies to this participant handler.
110      *
111      * @param participantMsg the message to check
112      * @return true if it applies, false otherwise
113      */
114     public boolean appliesTo(ParticipantMessage participantMsg) {
115         return participantMsg.appliesTo(cacheProvider.getParticipantId());
116     }
117
118     /**
119      * Check if a participant message applies to this participant handler.
120      *
121      * @param participantMsg the message to check
122      * @return true if it applies, false otherwise
123      */
124     public boolean appliesTo(ParticipantAckMessage participantMsg) {
125         return participantMsg.appliesTo(cacheProvider.getParticipantId());
126     }
127
128     /**
129      * Method to send ParticipantRegister message to automation composition runtime.
130      */
131     public void sendParticipantRegister() {
132         var participantRegister = new ParticipantRegister();
133         participantRegister.setParticipantId(cacheProvider.getParticipantId());
134         participantRegister.setParticipantSupportedElementType(cacheProvider.getSupportedAcElementTypes());
135
136         publisher.sendParticipantRegister(participantRegister);
137     }
138
139     /**
140      * Handle a participantRegister Ack message.
141      *
142      * @param participantRegisterAckMsg the participantRegisterAck message
143      */
144     @Timed(value = "listener.participant_register_ack", description = "PARTICIPANT_REGISTER_ACK messages received")
145     public void handleParticipantRegisterAck(ParticipantRegisterAck participantRegisterAckMsg) {
146         LOGGER.debug("ParticipantRegisterAck message received as responseTo {}",
147                 participantRegisterAckMsg.getResponseTo());
148         publisher.sendParticipantStatus(makeHeartbeat());
149     }
150
151     /**
152      * Method to send ParticipantDeregister message to automation composition runtime.
153      */
154     public void sendParticipantDeregister() {
155         var participantDeregister = new ParticipantDeregister();
156         participantDeregister.setParticipantId(cacheProvider.getParticipantId());
157         publisher.sendParticipantDeregister(participantDeregister);
158     }
159
160     /**
161      * Handle a participantDeregister Ack message.
162      *
163      * @param participantDeregisterAckMsg the participantDeregisterAck message
164      */
165     @Timed(value = "listener.participant_deregister_ack", description = "PARTICIPANT_DEREGISTER_ACK messages received")
166     public void handleParticipantDeregisterAck(ParticipantDeregisterAck participantDeregisterAckMsg) {
167         LOGGER.debug("ParticipantDeregisterAck message received as responseTo {}",
168                 participantDeregisterAckMsg.getResponseTo());
169     }
170
171     /**
172      * Handle a ParticipantPrime message.
173      *
174      * @param participantPrimeMsg the ParticipantPrime message
175      */
176     @Timed(value = "listener.participant_prime", description = "PARTICIPANT_PRIME messages received")
177     public void handleParticipantPrime(ParticipantPrime participantPrimeMsg) {
178         LOGGER.debug("ParticipantPrime message received for participantId {}", participantPrimeMsg.getParticipantId());
179
180         if (!participantPrimeMsg.getParticipantDefinitionUpdates().isEmpty()) {
181             // prime
182             List<AutomationCompositionElementDefinition> list = new ArrayList<>();
183             for (var participantDefinition : participantPrimeMsg.getParticipantDefinitionUpdates()) {
184                 if (participantDefinition.getParticipantId().equals(cacheProvider.getParticipantId())) {
185                     list.addAll(participantDefinition.getAutomationCompositionElementDefinitionList());
186                 }
187             }
188             cacheProvider.addElementDefinition(participantPrimeMsg.getCompositionId(), list);
189             automationCompositionHandler.prime(participantPrimeMsg.getMessageId(),
190                     participantPrimeMsg.getCompositionId(), list);
191         } else {
192             // deprime
193             automationCompositionHandler.deprime(participantPrimeMsg.getMessageId(),
194                     participantPrimeMsg.getCompositionId());
195         }
196     }
197
198     /**
199      * Handle a ParticipantRestart message.
200      *
201      * @param participantRestartMsg the participantRestart message
202      */
203     @Timed(value = "listener.participant_restart", description = "PARTICIPANT_RESTART messages received")
204     public void handleParticipantRestart(ParticipantRestart participantRestartMsg) {
205         LOGGER.debug("ParticipantRestart message received for participantId {}",
206                 participantRestartMsg.getParticipantId());
207         List<AutomationCompositionElementDefinition> list = new ArrayList<>();
208         for (var participantDefinition : participantRestartMsg.getParticipantDefinitionUpdates()) {
209             list.addAll(participantDefinition.getAutomationCompositionElementDefinitionList());
210         }
211         if (!AcTypeState.COMMISSIONED.equals(participantRestartMsg.getState())) {
212             cacheProvider.addElementDefinition(participantRestartMsg.getCompositionId(), list);
213         }
214         automationCompositionHandler.restarted(participantRestartMsg.getMessageId(),
215                 participantRestartMsg.getCompositionId(), list, participantRestartMsg.getState(),
216                 participantRestartMsg.getAutomationcompositionList());
217     }
218
219     /**
220      * Dispatch a heartbeat for this participant.
221      */
222     public void sendHeartbeat() {
223         if (publisher.isActive()) {
224             publisher.sendHeartbeat(makeHeartbeat());
225         }
226     }
227
228     /**
229      * Method to send heartbeat to automation composition runtime.
230      */
231     private ParticipantStatus makeHeartbeat() {
232         var heartbeat = new ParticipantStatus();
233         heartbeat.setParticipantId(cacheProvider.getParticipantId());
234         heartbeat.setState(ParticipantState.ON_LINE);
235         heartbeat.setParticipantSupportedElementType(cacheProvider.getSupportedAcElementTypes());
236
237         return heartbeat;
238     }
239
240     /**
241      * get AutomationComposition Info List.
242      *
243      * @return list of AutomationCompositionInfo
244      */
245     private List<AutomationCompositionInfo> getAutomationCompositionInfoList() {
246         List<AutomationCompositionInfo> automationCompositionInfoList = new ArrayList<>();
247         for (var entry : cacheProvider.getAutomationCompositions().entrySet()) {
248             var acInfo = new AutomationCompositionInfo();
249             acInfo.setAutomationCompositionId(entry.getKey());
250             acInfo.setDeployState(entry.getValue().getDeployState());
251             acInfo.setLockState(entry.getValue().getLockState());
252             for (var element : entry.getValue().getElements().values()) {
253                 acInfo.getElements().add(automationCompositionOutHandler.getAutomationCompositionElementInfo(element));
254             }
255             automationCompositionInfoList.add(acInfo);
256         }
257         return automationCompositionInfoList;
258     }
259 }