4842c3edd6edfb4a829017d4f3f06d7c271e5839
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.participant.intermediary.handler;
22
23 import java.io.Closeable;
24 import java.util.LinkedHashMap;
25 import java.util.Map;
26 import java.util.Objects;
27 import java.util.UUID;
28 import lombok.Getter;
29 import lombok.Setter;
30 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantHealthStatus;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatistics;
35 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate;
36 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopStateChange;
37 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregister;
38 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregisterAck;
39 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantHealthCheck;
40 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessage;
41 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegister;
42 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegisterAck;
43 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantResponseDetails;
44 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantResponseStatus;
45 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStateChange;
46 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus;
47 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
48 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdateAck;
49 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.MessageSender;
50 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantMessagePublisher;
51 import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantParameters;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55 import org.springframework.stereotype.Component;
56
57 /**
58  * This class is responsible for managing the state of a participant.
59  */
60 @Getter
61 @Component
62 public class ParticipantHandler implements Closeable {
63     private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantHandler.class);
64
65     private final ToscaConceptIdentifier participantType;
66     private final ToscaConceptIdentifier participantId;
67     private final MessageSender sender;
68     private final ControlLoopHandler controlLoopHandler;
69     private final ParticipantStatistics participantStatistics;
70
71     @Setter
72     private ParticipantState state = ParticipantState.UNKNOWN;
73
74     @Setter
75     private ParticipantHealthStatus healthStatus = ParticipantHealthStatus.UNKNOWN;
76
77     private final Map<UUID, ControlLoopElementDefinition> clElementDefsOnThisParticipant = new LinkedHashMap<>();
78
79     /**
80      * Constructor, set the participant ID and sender.
81      *
82      * @param parameters the parameters of the participant
83      * @param publisher the publisher for sending responses to messages
84      */
85     public ParticipantHandler(ParticipantParameters parameters, ParticipantMessagePublisher publisher) {
86         this.participantType = parameters.getIntermediaryParameters().getParticipantType();
87         this.participantId = parameters.getIntermediaryParameters().getParticipantId();
88         this.sender =
89                 new MessageSender(this, publisher,
90                         parameters.getIntermediaryParameters().getReportingTimeIntervalMs());
91         this.controlLoopHandler = new ControlLoopHandler(parameters.getIntermediaryParameters(), sender);
92         this.participantStatistics = new ParticipantStatistics();
93     }
94
95     @Override
96     public void close() {
97         sender.close();
98     }
99
100     /**
101      * Method which handles a participant state change event from clamp.
102      *
103      * @param stateChangeMsg participant state change message
104      */
105     public void handleParticipantStateChange(final ParticipantStateChange stateChangeMsg) {
106
107         if (!stateChangeMsg.appliesTo(participantType, participantId)) {
108             return;
109         }
110
111         var response = new ParticipantResponseDetails(stateChangeMsg);
112
113         switch (stateChangeMsg.getState()) {
114             case PASSIVE:
115                 handlePassiveState(response);
116                 break;
117             case ACTIVE:
118                 handleActiveState(response);
119                 break;
120             case SAFE:
121                 handleSafeState(response);
122                 break;
123             case TEST:
124                 handleTestState(response);
125                 break;
126             case TERMINATED:
127                 handleTerminatedState(response);
128                 break;
129             default:
130                 LOGGER.debug("StateChange message has no state, state is null {}", stateChangeMsg.getParticipantId());
131                 response.setResponseStatus(ParticipantResponseStatus.FAIL);
132                 response.setResponseMessage(
133                         "StateChange message has invalid state for participantId " + stateChangeMsg.getParticipantId());
134                 break;
135         }
136
137         sender.sendResponse(response);
138     }
139
140     /**
141      * Method which handles a participant health check event from clamp.
142      *
143      * @param healthCheckMsg participant health check message
144      */
145     public void handleParticipantHealthCheck(final ParticipantHealthCheck healthCheckMsg) {
146         var response = new ParticipantResponseDetails(healthCheckMsg);
147         response.setResponseStatus(ParticipantResponseStatus.SUCCESS);
148         response.setResponseMessage(healthStatus.toString());
149
150         sender.sendResponse(response);
151     }
152
153     /**
154      * Handle a control loop update message.
155      *
156      * @param updateMsg the update message
157      */
158     public void handleControlLoopUpdate(ControlLoopUpdate updateMsg) {
159         controlLoopHandler.handleControlLoopUpdate(updateMsg);
160     }
161
162     /**
163      * Handle a control loop state change message.
164      *
165      * @param stateChangeMsg the state change message
166      */
167     public void handleControlLoopStateChange(ParticipantControlLoopStateChange stateChangeMsg) {
168         controlLoopHandler.handleControlLoopStateChange(stateChangeMsg);
169     }
170
171     /**
172      * Method to handle when the new state from participant is active.
173      *
174      * @param response participant response
175      */
176     private void handleActiveState(final ParticipantResponseDetails response) {
177         handleStateChange(ParticipantState.ACTIVE, response);
178     }
179
180     /**
181      * Method to handle when the new state from participant is passive.
182      *
183      * @param response participant response
184      */
185     private void handlePassiveState(final ParticipantResponseDetails response) {
186         handleStateChange(ParticipantState.PASSIVE, response);
187     }
188
189     /**
190      * Method to handle when the new state from participant is safe.
191      *
192      * @param response participant response
193      */
194     private void handleSafeState(final ParticipantResponseDetails response) {
195         handleStateChange(ParticipantState.SAFE, response);
196     }
197
198     /**
199      * Method to handle when the new state from participant is TEST.
200      *
201      * @param response participant response
202      */
203     private void handleTestState(final ParticipantResponseDetails response) {
204         handleStateChange(ParticipantState.TEST, response);
205     }
206
207     /**
208      * Method to handle when the new state from participant is Terminated.
209      *
210      * @param response participant response
211      */
212     private void handleTerminatedState(final ParticipantResponseDetails response) {
213         handleStateChange(ParticipantState.TERMINATED, response);
214     }
215
216     private void handleStateChange(ParticipantState newParticipantState, ParticipantResponseDetails response) {
217         if (state.equals(newParticipantState)) {
218             response.setResponseStatus(ParticipantResponseStatus.SUCCESS);
219             response.setResponseMessage("Participant already in state " + newParticipantState);
220         } else {
221             response.setResponseStatus(ParticipantResponseStatus.SUCCESS);
222             response.setResponseMessage("Participant state changed from " + state + " to " + newParticipantState);
223             state = newParticipantState;
224         }
225     }
226
227     /**
228      * Method to update participant state.
229      *
230      * @param definition participant definition
231      * @param participantState participant state
232      * @return the participant
233      */
234     public Participant updateParticipantState(ToscaConceptIdentifier definition, ParticipantState participantState) {
235         if (!Objects.equals(definition, participantId)) {
236             LOGGER.debug("No participant with this ID {}", definition.getName());
237             return null;
238         }
239         var response = new ParticipantResponseDetails();
240         handleStateChange(participantState, response);
241         sender.sendResponse(response);
242         return getParticipant(definition.getName(), definition.getVersion());
243     }
244
245     /**
246      * Get participants as a {@link Participant} class.
247      *
248      * @param name the participant name to get
249      * @param version the version of the participant to get
250      * @return the participant
251      */
252     public Participant getParticipant(String name, String version) {
253         if (participantId.getName().equals(name)) {
254             var participant = new Participant();
255             participant.setDefinition(participantId);
256             participant.setParticipantState(state);
257             participant.setHealthStatus(healthStatus);
258             return participant;
259         }
260         return null;
261     }
262
263     /**
264      * Check if a participant message applies to this participant handler.
265      *
266      * @param participantMsg the message to check
267      * @return true if it applies, false otherwise
268      */
269     public boolean appliesTo(ParticipantMessage participantMsg) {
270         return participantMsg.appliesTo(participantType, participantId);
271     }
272
273     /**
274      * Method to send ParticipantRegister message to controlloop runtime.
275      */
276     public void sendParticipantRegister() {
277         var participantRegister = new ParticipantRegister();
278         participantRegister.setParticipantId(participantId);
279         participantRegister.setParticipantType(participantType);
280
281         sender.sendParticipantRegister(participantRegister);
282     }
283
284     /**
285      * Handle a participantRegister Ack message.
286      *
287      * @param participantRegisterAckMsg the participantRegisterAck message
288      */
289     public void handleParticipantRegisterAck(ParticipantRegisterAck participantRegisterAckMsg) {
290         LOGGER.debug("ParticipantRegisterAck message received as responseTo {}",
291             participantRegisterAckMsg.getResponseTo());
292     }
293
294     /**
295      * Method to send ParticipantDeregister message to controlloop runtime.
296      */
297     public void sendParticipantDeregister() {
298         var participantDeregister = new ParticipantDeregister();
299         participantDeregister.setParticipantId(participantId);
300         participantDeregister.setParticipantType(participantType);
301
302         sender.sendParticipantDeregister(participantDeregister);
303     }
304
305     /**
306      * Handle a participantDeregister Ack message.
307      *
308      * @param participantDeregisterAckMsg the participantDeregisterAck message
309      */
310     public void handleParticipantDeregisterAck(ParticipantDeregisterAck participantDeregisterAckMsg) {
311         LOGGER.debug("ParticipantDeregisterAck message received as responseTo {}",
312             participantDeregisterAckMsg.getResponseTo());
313     }
314
315     /**
316      * Handle a ParticipantUpdate message.
317      *
318      * @param participantUpdateMsg the ParticipantUpdate message
319      */
320     public void handleParticipantUpdate(ParticipantUpdate participantUpdateMsg) {
321         LOGGER.debug("ParticipantUpdate message received for participantId {}",
322             participantUpdateMsg.getParticipantId());
323
324         if (!participantUpdateMsg.appliesTo(participantType, participantId)) {
325             return;
326         }
327
328         Map<UUID, ControlLoopElementDefinition> clDefinitionMap =
329                 participantUpdateMsg.getParticipantDefinitionUpdateMap().get(participantUpdateMsg.getParticipantId());
330
331         for (ControlLoopElementDefinition element : clDefinitionMap.values()) {
332             clElementDefsOnThisParticipant.put(element.getId(), element);
333         }
334
335         sendParticipantUpdateAck(participantUpdateMsg.getMessageId());
336     }
337
338     /**
339      * Method to send ParticipantUpdateAck message to controlloop runtime.
340      */
341     public void sendParticipantUpdateAck(UUID messageId) {
342         var participantUpdateAck = new ParticipantUpdateAck();
343         participantUpdateAck.setResponseTo(messageId);
344         participantUpdateAck.setMessage("Participant Update Ack message");
345         participantUpdateAck.setResult(true);
346
347         sender.sendParticipantUpdateAck(participantUpdateAck);
348     }
349
350     /**
351      * Method to send heartbeat to controlloop runtime.
352      */
353     public ParticipantStatus makeHeartbeat() {
354         ParticipantStatus heartbeat = new ParticipantStatus();
355         heartbeat.setParticipantId(participantId);
356         heartbeat.setParticipantStatistics(participantStatistics);
357         heartbeat.setParticipantType(participantType);
358         heartbeat.setHealthStatus(healthStatus);
359         heartbeat.setMessage("Participant heartbeat message sent from -> " + participantId.getName());
360         return heartbeat;
361     }
362 }