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