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