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