1947fda1a1a8a586cf4b21ef0d78c189142bc0b1
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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.controlloop.participant.intermediary.handler;
24
25 import java.io.Closeable;
26 import java.time.Instant;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Objects;
31 import java.util.UUID;
32 import lombok.Getter;
33 import lombok.Setter;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatisticsList;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopInfo;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopStatistics;
39 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant;
40 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantDefinition;
41 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantHealthStatus;
42 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
43 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatistics;
44 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopStateChange;
45 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate;
46 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregister;
47 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregisterAck;
48 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessage;
49 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegister;
50 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegisterAck;
51 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus;
52 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatusReq;
53 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
54 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdateAck;
55 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.MessageSender;
56 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantMessagePublisher;
57 import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantParameters;
58 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
59 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
62 import org.springframework.stereotype.Component;
63
64 /**
65  * This class is responsible for managing the state of a participant.
66  */
67 @Getter
68 @Component
69 public class ParticipantHandler implements Closeable {
70     private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantHandler.class);
71
72     private final ToscaConceptIdentifier participantType;
73     private final ToscaConceptIdentifier participantId;
74     private final MessageSender sender;
75     private final ControlLoopHandler controlLoopHandler;
76     private final ParticipantStatistics participantStatistics;
77
78     @Setter
79     private ParticipantState state = ParticipantState.UNKNOWN;
80
81     @Setter
82     private ParticipantHealthStatus healthStatus = ParticipantHealthStatus.UNKNOWN;
83
84     private List<ControlLoopElementDefinition> clElementDefsOnThisParticipant =
85             new ArrayList<>();
86
87     public ToscaServiceTemplate toscaServiceTemplate = new ToscaServiceTemplate();
88
89     /**
90      * Constructor, set the participant ID and sender.
91      *
92      * @param parameters the parameters of the participant
93      * @param publisher the publisher for sending responses to messages
94      */
95     public ParticipantHandler(ParticipantParameters parameters, ParticipantMessagePublisher publisher) {
96         this.participantType = parameters.getIntermediaryParameters().getParticipantType();
97         this.participantId = parameters.getIntermediaryParameters().getParticipantId();
98         this.sender =
99                 new MessageSender(this, publisher,
100                         parameters.getIntermediaryParameters().getReportingTimeIntervalMs());
101         this.controlLoopHandler = new ControlLoopHandler(parameters.getIntermediaryParameters(), sender);
102         this.participantStatistics = new ParticipantStatistics();
103         this.participantStatistics.setParticipantId(participantId);
104         this.participantStatistics.setState(state);
105         this.participantStatistics.setHealthStatus(healthStatus);
106         this.participantStatistics.setTimeStamp(Instant.now());
107     }
108
109     @Override
110     public void close() {
111         sender.close();
112     }
113
114     /**
115      * Method which handles a participant health check event from clamp.
116      *
117      * @param participantStatusReqMsg participant participantStatusReq message
118      */
119     public void handleParticipantStatusReq(final ParticipantStatusReq participantStatusReqMsg) {
120         sender.sendParticipantStatus(makeHeartbeat(true));
121     }
122
123     /**
124      * Handle a control loop update message.
125      *
126      * @param updateMsg the update message
127      */
128     public void handleControlLoopUpdate(ControlLoopUpdate updateMsg) {
129         controlLoopHandler.handleControlLoopUpdate(updateMsg, clElementDefsOnThisParticipant);
130     }
131
132     /**
133      * Handle a control loop state change message.
134      *
135      * @param stateChangeMsg the state change message
136      */
137     public void handleControlLoopStateChange(ControlLoopStateChange stateChangeMsg) {
138         controlLoopHandler.handleControlLoopStateChange(stateChangeMsg);
139     }
140
141     private void handleStateChange(ParticipantState newParticipantState, ParticipantUpdateAck response) {
142         if (state.equals(newParticipantState)) {
143             response.setResult(false);
144             response.setMessage("Participant already in state " + newParticipantState);
145         } else {
146             response.setResult(true);
147             response.setMessage("Participant state changed from " + state + " to " + newParticipantState);
148             state = newParticipantState;
149         }
150     }
151
152     /**
153      * Method to update participant state.
154      *
155      * @param definition participant definition
156      * @param participantState participant state
157      * @return the participant
158      */
159     public Participant updateParticipantState(ToscaConceptIdentifier definition, ParticipantState participantState) {
160         if (!Objects.equals(definition, participantId)) {
161             LOGGER.debug("No participant with this ID {}", definition.getName());
162             return null;
163         }
164
165         var participantUpdateAck = new ParticipantUpdateAck();
166         handleStateChange(participantState, participantUpdateAck);
167         sender.sendParticipantUpdateAck(participantUpdateAck);
168         return getParticipant(definition.getName(), definition.getVersion());
169     }
170
171     /**
172      * Get participants as a {@link Participant} class.
173      *
174      * @param name the participant name to get
175      * @param version the version of the participant to get
176      * @return the participant
177      */
178     public Participant getParticipant(String name, String version) {
179         if (participantId.getName().equals(name)) {
180             var participant = new Participant();
181             participant.setDefinition(participantId);
182             participant.setParticipantState(state);
183             participant.setHealthStatus(healthStatus);
184             return participant;
185         }
186         return null;
187     }
188
189     /**
190      * Check if a participant message applies to this participant handler.
191      *
192      * @param participantMsg the message to check
193      * @return true if it applies, false otherwise
194      */
195     public boolean appliesTo(ParticipantMessage participantMsg) {
196         return participantMsg.appliesTo(participantType, participantId);
197     }
198
199     /**
200      * Method to send ParticipantRegister message to controlloop runtime.
201      */
202     public void sendParticipantRegister() {
203         var participantRegister = new ParticipantRegister();
204         participantRegister.setParticipantId(participantId);
205         participantRegister.setParticipantType(participantType);
206
207         sender.sendParticipantRegister(participantRegister);
208     }
209
210     /**
211      * Handle a participantRegister Ack message.
212      *
213      * @param participantRegisterAckMsg the participantRegisterAck message
214      */
215     public void handleParticipantRegisterAck(ParticipantRegisterAck participantRegisterAckMsg) {
216         LOGGER.debug("ParticipantRegisterAck message received as responseTo {}",
217                 participantRegisterAckMsg.getResponseTo());
218     }
219
220     /**
221      * Method to send ParticipantDeregister message to controlloop runtime.
222      */
223     public void sendParticipantDeregister() {
224         var participantDeregister = new ParticipantDeregister();
225         participantDeregister.setParticipantId(participantId);
226         participantDeregister.setParticipantType(participantType);
227
228         sender.sendParticipantDeregister(participantDeregister);
229     }
230
231     /**
232      * Handle a participantDeregister Ack message.
233      *
234      * @param participantDeregisterAckMsg the participantDeregisterAck message
235      */
236     public void handleParticipantDeregisterAck(ParticipantDeregisterAck participantDeregisterAckMsg) {
237         LOGGER.debug("ParticipantDeregisterAck message received as responseTo {}",
238                 participantDeregisterAckMsg.getResponseTo());
239     }
240
241     /**
242      * Handle a ParticipantUpdate message.
243      *
244      * @param participantUpdateMsg the ParticipantUpdate message
245      */
246     public void handleParticipantUpdate(ParticipantUpdate participantUpdateMsg) {
247         LOGGER.debug("ParticipantUpdate message received for participantId {}",
248                 participantUpdateMsg.getParticipantId());
249
250         if (!participantUpdateMsg.appliesTo(participantType, participantId)) {
251             return;
252         }
253
254         toscaServiceTemplate = participantUpdateMsg.getToscaServiceTemplate();
255         if (toscaServiceTemplate != null) {
256             // This message is to commission the controlloop
257             for (ParticipantDefinition participantDefinition : participantUpdateMsg.getParticipantDefinitionUpdates()) {
258                 if (participantDefinition.getParticipantId().equals(participantType)) {
259                     clElementDefsOnThisParticipant = participantDefinition.getControlLoopElementDefinitionList();
260                     break;
261                 }
262             }
263         } else {
264             // This message is to decommision the controlloop
265             clElementDefsOnThisParticipant.clear();
266         }
267         sendParticipantUpdateAck(participantUpdateMsg.getMessageId());
268     }
269
270     /**
271      * Method to send ParticipantUpdateAck message to controlloop runtime.
272      */
273     public void sendParticipantUpdateAck(UUID messageId) {
274         var participantUpdateAck = new ParticipantUpdateAck();
275         participantUpdateAck.setResponseTo(messageId);
276         participantUpdateAck.setMessage("Participant Update Ack message");
277         participantUpdateAck.setResult(true);
278         participantUpdateAck.setParticipantId(participantId);
279         participantUpdateAck.setParticipantType(participantType);
280
281         sender.sendParticipantUpdateAck(participantUpdateAck);
282     }
283
284     /**
285      * Method to send heartbeat to controlloop runtime.
286      */
287     public ParticipantStatus makeHeartbeat(boolean responseToParticipantStatusReq) {
288         this.participantStatistics.setState(state);
289         this.participantStatistics.setHealthStatus(healthStatus);
290         this.participantStatistics.setTimeStamp(Instant.now());
291
292         var heartbeat = new ParticipantStatus();
293         heartbeat.setParticipantId(participantId);
294         heartbeat.setParticipantStatistics(participantStatistics);
295         heartbeat.setParticipantType(participantType);
296         heartbeat.setHealthStatus(healthStatus);
297         heartbeat.setState(state);
298         heartbeat.setControlLoopInfoList(getControlLoopInfoList());
299
300         if (responseToParticipantStatusReq) {
301             List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>();
302             ParticipantDefinition participantDefinition = new ParticipantDefinition();
303             participantDefinition.setParticipantId(participantId);
304             participantDefinition.setControlLoopElementDefinitionList(clElementDefsOnThisParticipant);
305             participantDefinitionUpdates.add(participantDefinition);
306             heartbeat.setParticipantDefinitionUpdates(participantDefinitionUpdates);
307         }
308
309         return heartbeat;
310     }
311
312     private List<ControlLoopInfo> getControlLoopInfoList() {
313         List<ControlLoopInfo> controlLoopInfoList = new ArrayList<>();
314         for (Map.Entry<ToscaConceptIdentifier, ControlLoop> entry :
315                 controlLoopHandler.getControlLoopMap().entrySet()) {
316             ControlLoopInfo clInfo = new ControlLoopInfo();
317             clInfo.setControlLoopId(entry.getKey());
318             ControlLoopStatistics clStatitistics = new ControlLoopStatistics();
319             clStatitistics.setControlLoopId(entry.getKey());
320             ClElementStatisticsList clElementStatisticsList = new ClElementStatisticsList();
321             clElementStatisticsList.setClElementStatistics(
322                 entry.getValue().getControlLoopElementStatisticsList(entry.getValue()));
323             clStatitistics.setClElementStatisticsList(clElementStatisticsList);
324             clInfo.setControlLoopStatistics(clStatitistics);
325             clInfo.setState(entry.getValue().getState());
326         }
327         return controlLoopInfoList;
328     }
329 }