6edf1ffd49703730f11ea828bfd3c604ed8784ca
[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.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.UUID;
31 import lombok.Getter;
32 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher;
33 import org.onap.policy.clamp.acm.participant.intermediary.parameters.ParticipantParameters;
34 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
35 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionInfo;
36 import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition;
37 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
38 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionStateChange;
39 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionUpdate;
40 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantAckMessage;
41 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregister;
42 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregisterAck;
43 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessage;
44 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegister;
45 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegisterAck;
46 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatus;
47 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatusReq;
48 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantUpdate;
49 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantUpdateAck;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53 import org.springframework.stereotype.Component;
54
55 /**
56  * This class is responsible for managing the state of a participant.
57  */
58 @Component
59 public class ParticipantHandler {
60     private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantHandler.class);
61
62     @Getter
63     private final ToscaConceptIdentifier participantType;
64
65     @Getter
66     private final ToscaConceptIdentifier participantId;
67
68     private final AutomationCompositionHandler automationCompositionHandler;
69     private final ParticipantMessagePublisher publisher;
70
71     private final Map<UUID, List<AutomationCompositionElementDefinition>> acElementDefsMap = new HashMap<>();
72
73     /**
74      * Constructor, set the participant ID and sender.
75      *
76      * @param parameters the parameters of the participant
77      * @param publisher the publisher for sending responses to messages
78      */
79     public ParticipantHandler(ParticipantParameters parameters, ParticipantMessagePublisher publisher,
80             AutomationCompositionHandler automationCompositionHandler) {
81         this.participantType = parameters.getIntermediaryParameters().getParticipantType();
82         this.participantId = parameters.getIntermediaryParameters().getParticipantId();
83         this.publisher = publisher;
84         this.automationCompositionHandler = automationCompositionHandler;
85     }
86
87     /**
88      * Method which handles a participant health check event from clamp.
89      *
90      * @param participantStatusReqMsg participant participantStatusReq message
91      */
92     @Timed(value = "listener.participant_status_req", description = "PARTICIPANT_STATUS_REQ messages received")
93     public void handleParticipantStatusReq(final ParticipantStatusReq participantStatusReqMsg) {
94         var participantStatus = makeHeartbeat(true);
95         publisher.sendParticipantStatus(participantStatus);
96     }
97
98     /**
99      * Handle a automation composition update message.
100      *
101      * @param updateMsg the update message
102      */
103     @Timed(
104             value = "listener.automation_composition_update",
105             description = "AUTOMATION_COMPOSITION_UPDATE messages received")
106     public void handleAutomationCompositionUpdate(AutomationCompositionUpdate updateMsg) {
107         automationCompositionHandler.handleAutomationCompositionUpdate(updateMsg,
108                 acElementDefsMap.get(updateMsg.getCompositionId()));
109     }
110
111     /**
112      * Handle a automation composition state change message.
113      *
114      * @param stateChangeMsg the state change message
115      */
116     @Timed(
117             value = "listener.automation_composition_state_change",
118             description = "AUTOMATION_COMPOSITION_STATE_CHANGE messages received")
119     public void handleAutomationCompositionStateChange(AutomationCompositionStateChange stateChangeMsg) {
120         automationCompositionHandler.handleAutomationCompositionStateChange(stateChangeMsg,
121                 acElementDefsMap.get(stateChangeMsg.getCompositionId()));
122     }
123
124     /**
125      * Check if a participant message applies to this participant handler.
126      *
127      * @param participantMsg the message to check
128      * @return true if it applies, false otherwise
129      */
130     public boolean appliesTo(ParticipantMessage participantMsg) {
131         return participantMsg.appliesTo(participantType, participantId);
132     }
133
134     /**
135      * Check if a participant message applies to this participant handler.
136      *
137      * @param participantMsg the message to check
138      * @return true if it applies, false otherwise
139      */
140     public boolean appliesTo(ParticipantAckMessage participantMsg) {
141         return participantMsg.appliesTo(participantType, participantId);
142     }
143
144     /**
145      * Method to send ParticipantRegister message to automation composition runtime.
146      */
147     public void sendParticipantRegister() {
148         var participantRegister = new ParticipantRegister();
149         participantRegister.setParticipantId(participantId);
150         participantRegister.setParticipantType(participantType);
151
152         publisher.sendParticipantRegister(participantRegister);
153     }
154
155     /**
156      * Handle a participantRegister Ack message.
157      *
158      * @param participantRegisterAckMsg the participantRegisterAck message
159      */
160     @Timed(value = "listener.participant_register_ack", description = "PARTICIPANT_REGISTER_ACK messages received")
161     public void handleParticipantRegisterAck(ParticipantRegisterAck participantRegisterAckMsg) {
162         LOGGER.debug("ParticipantRegisterAck message received as responseTo {}",
163                 participantRegisterAckMsg.getResponseTo());
164         publisher.sendParticipantStatus(makeHeartbeat(false));
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(participantId);
173         participantDeregister.setParticipantType(participantType);
174
175         publisher.sendParticipantDeregister(participantDeregister);
176     }
177
178     /**
179      * Handle a participantDeregister Ack message.
180      *
181      * @param participantDeregisterAckMsg the participantDeregisterAck message
182      */
183     @Timed(value = "listener.participant_deregister_ack", description = "PARTICIPANT_DEREGISTER_ACK messages received")
184     public void handleParticipantDeregisterAck(ParticipantDeregisterAck participantDeregisterAckMsg) {
185         LOGGER.debug("ParticipantDeregisterAck message received as responseTo {}",
186                 participantDeregisterAckMsg.getResponseTo());
187     }
188
189     /**
190      * Handle a ParticipantUpdate message.
191      *
192      * @param participantUpdateMsg the ParticipantUpdate message
193      */
194     @Timed(value = "listener.participant_update", description = "PARTICIPANT_UPDATE messages received")
195     public void handleParticipantUpdate(ParticipantUpdate participantUpdateMsg) {
196         LOGGER.debug("ParticipantUpdate message received for participantId {}",
197                 participantUpdateMsg.getParticipantId());
198
199         acElementDefsMap.putIfAbsent(participantUpdateMsg.getCompositionId(), new ArrayList<>());
200         if (!participantUpdateMsg.getParticipantDefinitionUpdates().isEmpty()) {
201             // This message is to commission the automation composition
202             for (var participantDefinition : participantUpdateMsg.getParticipantDefinitionUpdates()) {
203                 if (participantDefinition.getParticipantType().equals(participantType)) {
204                     acElementDefsMap.get(participantUpdateMsg.getCompositionId())
205                             .addAll(participantDefinition.getAutomationCompositionElementDefinitionList());
206                     break;
207                 }
208             }
209         } else {
210             // This message is to decommission the automation composition
211             acElementDefsMap.get(participantUpdateMsg.getCompositionId()).clear();
212         }
213         sendParticipantUpdateAck(participantUpdateMsg.getMessageId());
214     }
215
216     /**
217      * Method to send ParticipantUpdateAck message to automation composition runtime.
218      */
219     public void sendParticipantUpdateAck(UUID messageId) {
220         var participantUpdateAck = new ParticipantUpdateAck();
221         participantUpdateAck.setResponseTo(messageId);
222         participantUpdateAck.setMessage("Participant Update Ack message");
223         participantUpdateAck.setResult(true);
224         participantUpdateAck.setParticipantId(participantId);
225         participantUpdateAck.setParticipantType(participantType);
226         participantUpdateAck.setState(ParticipantState.ON_LINE);
227         publisher.sendParticipantUpdateAck(participantUpdateAck);
228     }
229
230     /**
231      * Dispatch a heartbeat for this participant.
232      */
233     public void sendHeartbeat() {
234         publisher.sendHeartbeat(makeHeartbeat(false));
235     }
236
237     /**
238      * Method to send heartbeat to automation composition runtime.
239      */
240     public ParticipantStatus makeHeartbeat(boolean responseToParticipantStatusReq) {
241         var heartbeat = new ParticipantStatus();
242         heartbeat.setParticipantId(participantId);
243         heartbeat.setParticipantType(participantType);
244         heartbeat.setState(ParticipantState.ON_LINE);
245         heartbeat.setAutomationCompositionInfoList(getAutomationCompositionInfoList());
246
247         if (responseToParticipantStatusReq) {
248             List<ParticipantDefinition> participantDefinitionList = new ArrayList<>(acElementDefsMap.size());
249             for (var acElementDefsOnThisParticipant : acElementDefsMap.values()) {
250                 var participantDefinition = new ParticipantDefinition();
251                 participantDefinition.setParticipantId(participantId);
252                 participantDefinition.setParticipantType(participantType);
253                 participantDefinition.setAutomationCompositionElementDefinitionList(acElementDefsOnThisParticipant);
254                 participantDefinitionList.add(participantDefinition);
255             }
256             heartbeat.setParticipantDefinitionUpdates(participantDefinitionList);
257         }
258
259         return heartbeat;
260     }
261
262     private List<AutomationCompositionInfo> getAutomationCompositionInfoList() {
263         List<AutomationCompositionInfo> automationCompositionInfoList = new ArrayList<>();
264         for (var entry : automationCompositionHandler.getAutomationCompositionMap().entrySet()) {
265             var acInfo = new AutomationCompositionInfo();
266             acInfo.setAutomationCompositionId(entry.getKey());
267             acInfo.setState(entry.getValue().getState());
268             automationCompositionInfoList.add(acInfo);
269         }
270         return automationCompositionInfoList;
271     }
272 }