6dcf93357924abf568300fc30f8bc7ce68e38577
[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.time.Instant;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Objects;
28 import java.util.UUID;
29 import lombok.Getter;
30 import lombok.Setter;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantDefinition;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantHealthStatus;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatistics;
37 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopStateChange;
38 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate;
39 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregister;
40 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregisterAck;
41 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessage;
42 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegister;
43 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegisterAck;
44 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus;
45 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatusReq;
46 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
47 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdateAck;
48 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.MessageSender;
49 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantMessagePublisher;
50 import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantParameters;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
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 List<ControlLoopElementDefinition> clElementDefsOnThisParticipant =
78             new ArrayList<>();
79
80     public ToscaServiceTemplate toscaServiceTemplate = new ToscaServiceTemplate();
81
82     /**
83      * Constructor, set the participant ID and sender.
84      *
85      * @param parameters the parameters of the participant
86      * @param publisher the publisher for sending responses to messages
87      */
88     public ParticipantHandler(ParticipantParameters parameters, ParticipantMessagePublisher publisher) {
89         this.participantType = parameters.getIntermediaryParameters().getParticipantType();
90         this.participantId = parameters.getIntermediaryParameters().getParticipantId();
91         this.sender =
92                 new MessageSender(this, publisher,
93                         parameters.getIntermediaryParameters().getReportingTimeIntervalMs());
94         this.controlLoopHandler = new ControlLoopHandler(parameters.getIntermediaryParameters(), sender);
95         this.participantStatistics = new ParticipantStatistics();
96         this.participantStatistics.setParticipantId(participantId);
97         this.participantStatistics.setState(state);
98         this.participantStatistics.setHealthStatus(healthStatus);
99         this.participantStatistics.setTimeStamp(Instant.now());
100     }
101
102     @Override
103     public void close() {
104         sender.close();
105     }
106
107     /**
108      * Method which handles a participant health check event from clamp.
109      *
110      * @param participantStatusReqMsg participant participantStatusReq message
111      */
112     public void handleParticipantStatusReq(final ParticipantStatusReq participantStatusReqMsg) {
113         sender.sendParticipantStatus(makeHeartbeat());
114     }
115
116     /**
117      * Handle a control loop update message.
118      *
119      * @param updateMsg the update message
120      */
121     public void handleControlLoopUpdate(ControlLoopUpdate updateMsg) {
122         controlLoopHandler.handleControlLoopUpdate(updateMsg, clElementDefsOnThisParticipant);
123     }
124
125     /**
126      * Handle a control loop state change message.
127      *
128      * @param stateChangeMsg the state change message
129      */
130     public void handleControlLoopStateChange(ControlLoopStateChange stateChangeMsg) {
131         controlLoopHandler.handleControlLoopStateChange(stateChangeMsg);
132     }
133
134     private void handleStateChange(ParticipantState newParticipantState, ParticipantUpdateAck response) {
135         if (state.equals(newParticipantState)) {
136             response.setResult(false);
137             response.setMessage("Participant already in state " + newParticipantState);
138         } else {
139             response.setResult(true);
140             response.setMessage("Participant state changed from " + state + " to " + newParticipantState);
141             state = newParticipantState;
142         }
143     }
144
145     /**
146      * Method to update participant state.
147      *
148      * @param definition participant definition
149      * @param participantState participant state
150      * @return the participant
151      */
152     public Participant updateParticipantState(ToscaConceptIdentifier definition, ParticipantState participantState) {
153         if (!Objects.equals(definition, participantId)) {
154             LOGGER.debug("No participant with this ID {}", definition.getName());
155             return null;
156         }
157
158         var participantUpdateAck = new ParticipantUpdateAck();
159         handleStateChange(participantState, participantUpdateAck);
160         sender.sendParticipantUpdateAck(participantUpdateAck);
161         return getParticipant(definition.getName(), definition.getVersion());
162     }
163
164     /**
165      * Get participants as a {@link Participant} class.
166      *
167      * @param name the participant name to get
168      * @param version the version of the participant to get
169      * @return the participant
170      */
171     public Participant getParticipant(String name, String version) {
172         if (participantId.getName().equals(name)) {
173             var participant = new Participant();
174             participant.setDefinition(participantId);
175             participant.setParticipantState(state);
176             participant.setHealthStatus(healthStatus);
177             return participant;
178         }
179         return null;
180     }
181
182     /**
183      * Check if a participant message applies to this participant handler.
184      *
185      * @param participantMsg the message to check
186      * @return true if it applies, false otherwise
187      */
188     public boolean appliesTo(ParticipantMessage participantMsg) {
189         return participantMsg.appliesTo(participantType, participantId);
190     }
191
192     /**
193      * Method to send ParticipantRegister message to controlloop runtime.
194      */
195     public void sendParticipantRegister() {
196         var participantRegister = new ParticipantRegister();
197         participantRegister.setParticipantId(participantId);
198         participantRegister.setParticipantType(participantType);
199
200         sender.sendParticipantRegister(participantRegister);
201     }
202
203     /**
204      * Handle a participantRegister Ack message.
205      *
206      * @param participantRegisterAckMsg the participantRegisterAck message
207      */
208     public void handleParticipantRegisterAck(ParticipantRegisterAck participantRegisterAckMsg) {
209         LOGGER.debug("ParticipantRegisterAck message received as responseTo {}",
210                 participantRegisterAckMsg.getResponseTo());
211     }
212
213     /**
214      * Method to send ParticipantDeregister message to controlloop runtime.
215      */
216     public void sendParticipantDeregister() {
217         var participantDeregister = new ParticipantDeregister();
218         participantDeregister.setParticipantId(participantId);
219         participantDeregister.setParticipantType(participantType);
220
221         sender.sendParticipantDeregister(participantDeregister);
222     }
223
224     /**
225      * Handle a participantDeregister Ack message.
226      *
227      * @param participantDeregisterAckMsg the participantDeregisterAck message
228      */
229     public void handleParticipantDeregisterAck(ParticipantDeregisterAck participantDeregisterAckMsg) {
230         LOGGER.debug("ParticipantDeregisterAck message received as responseTo {}",
231                 participantDeregisterAckMsg.getResponseTo());
232     }
233
234     /**
235      * Handle a ParticipantUpdate message.
236      *
237      * @param participantUpdateMsg the ParticipantUpdate message
238      */
239     public void handleParticipantUpdate(ParticipantUpdate participantUpdateMsg) {
240         LOGGER.debug("ParticipantUpdate message received for participantId {}",
241                 participantUpdateMsg.getParticipantId());
242
243         if (!participantUpdateMsg.appliesTo(participantType, participantId)) {
244             return;
245         }
246
247         toscaServiceTemplate = participantUpdateMsg.getToscaServiceTemplate();
248         for (ParticipantDefinition participantDefinition : participantUpdateMsg.getParticipantDefinitionUpdates()) {
249             if (participantDefinition.getParticipantId().equals(participantType)) {
250                 clElementDefsOnThisParticipant = participantDefinition.getControlLoopElementDefinitionList();
251             }
252         }
253         sendParticipantUpdateAck(participantUpdateMsg.getMessageId());
254     }
255
256     /**
257      * Method to send ParticipantUpdateAck message to controlloop runtime.
258      */
259     public void sendParticipantUpdateAck(UUID messageId) {
260         var participantUpdateAck = new ParticipantUpdateAck();
261         participantUpdateAck.setResponseTo(messageId);
262         participantUpdateAck.setMessage("Participant Update Ack message");
263         participantUpdateAck.setResult(true);
264         participantUpdateAck.setParticipantId(participantId);
265         participantUpdateAck.setParticipantType(participantType);
266
267         sender.sendParticipantUpdateAck(participantUpdateAck);
268     }
269
270     /**
271      * Method to send heartbeat to controlloop runtime.
272      */
273     public ParticipantStatus makeHeartbeat() {
274         this.participantStatistics.setState(state);
275         this.participantStatistics.setHealthStatus(healthStatus);
276         this.participantStatistics.setTimeStamp(Instant.now());
277
278         ParticipantStatus heartbeat = new ParticipantStatus();
279         heartbeat.setParticipantId(participantId);
280         heartbeat.setParticipantStatistics(participantStatistics);
281         heartbeat.setParticipantType(participantType);
282         heartbeat.setHealthStatus(healthStatus);
283         heartbeat.setState(state);
284         return heartbeat;
285     }
286 }