6a0e758dd4c4fe2ace005a400d75bb80e28316f7
[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.ControlLoopAck;
36 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopStateChange;
37 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate;
38 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregister;
39 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregisterAck;
40 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessage;
41 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessageType;
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.ParticipantResponseStatus;
45 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus;
46 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatusReq;
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 health check event from clamp.
102      *
103      * @param participantStatusReqMsg participant participantStatusReq message
104      */
105     public void handleParticipantStatusReq(final ParticipantStatusReq participantStatusReqMsg) {
106         ParticipantStatus participantStatus = new ParticipantStatus();
107         participantStatus.setParticipantId(participantId);
108         participantStatus.setParticipantStatistics(participantStatistics);
109         participantStatus.setParticipantType(participantType);
110         participantStatus.setHealthStatus(healthStatus);
111         sender.sendParticipantStatus(participantStatus);
112     }
113
114     /**
115      * Handle a control loop update message.
116      *
117      * @param updateMsg the update message
118      */
119     public void handleControlLoopUpdate(ControlLoopUpdate updateMsg) {
120         controlLoopHandler.handleControlLoopUpdate(updateMsg, clElementDefsOnThisParticipant);
121     }
122
123     /**
124      * Handle a control loop state change message.
125      *
126      * @param stateChangeMsg the state change message
127      */
128     public void handleControlLoopStateChange(ControlLoopStateChange stateChangeMsg) {
129         controlLoopHandler.handleControlLoopStateChange(stateChangeMsg);
130     }
131
132     private void handleStateChange(ParticipantState newParticipantState, ParticipantUpdateAck response) {
133         if (state.equals(newParticipantState)) {
134             response.setResult(false);
135             response.setMessage("Participant already in state " + newParticipantState);
136         } else {
137             response.setResult(true);
138             response.setMessage("Participant state changed from " + state + " to " + newParticipantState);
139             state = newParticipantState;
140         }
141     }
142
143     /**
144      * Method to update participant state.
145      *
146      * @param definition participant definition
147      * @param participantState participant state
148      * @return the participant
149      */
150     public Participant updateParticipantState(ToscaConceptIdentifier definition, ParticipantState participantState) {
151         if (!Objects.equals(definition, participantId)) {
152             LOGGER.debug("No participant with this ID {}", definition.getName());
153             return null;
154         }
155
156         var participantUpdateAck = new ParticipantUpdateAck();
157         handleStateChange(participantState, participantUpdateAck);
158         sender.sendParticipantUpdateAck(participantUpdateAck);
159         return getParticipant(definition.getName(), definition.getVersion());
160     }
161
162     /**
163      * Get participants as a {@link Participant} class.
164      *
165      * @param name the participant name to get
166      * @param version the version of the participant to get
167      * @return the participant
168      */
169     public Participant getParticipant(String name, String version) {
170         if (participantId.getName().equals(name)) {
171             var participant = new Participant();
172             participant.setDefinition(participantId);
173             participant.setParticipantState(state);
174             participant.setHealthStatus(healthStatus);
175             return participant;
176         }
177         return null;
178     }
179
180     /**
181      * Check if a participant message applies to this participant handler.
182      *
183      * @param participantMsg the message to check
184      * @return true if it applies, false otherwise
185      */
186     public boolean appliesTo(ParticipantMessage participantMsg) {
187         return participantMsg.appliesTo(participantType, participantId);
188     }
189
190     /**
191      * Method to send ParticipantRegister message to controlloop runtime.
192      */
193     public void sendParticipantRegister() {
194         var participantRegister = new ParticipantRegister();
195         participantRegister.setParticipantId(participantId);
196         participantRegister.setParticipantType(participantType);
197
198         sender.sendParticipantRegister(participantRegister);
199     }
200
201     /**
202      * Handle a participantRegister Ack message.
203      *
204      * @param participantRegisterAckMsg the participantRegisterAck message
205      */
206     public void handleParticipantRegisterAck(ParticipantRegisterAck participantRegisterAckMsg) {
207         LOGGER.debug("ParticipantRegisterAck message received as responseTo {}",
208             participantRegisterAckMsg.getResponseTo());
209     }
210
211     /**
212      * Method to send ParticipantDeregister message to controlloop runtime.
213      */
214     public void sendParticipantDeregister() {
215         var participantDeregister = new ParticipantDeregister();
216         participantDeregister.setParticipantId(participantId);
217         participantDeregister.setParticipantType(participantType);
218
219         sender.sendParticipantDeregister(participantDeregister);
220     }
221
222     /**
223      * Handle a participantDeregister Ack message.
224      *
225      * @param participantDeregisterAckMsg the participantDeregisterAck message
226      */
227     public void handleParticipantDeregisterAck(ParticipantDeregisterAck participantDeregisterAckMsg) {
228         LOGGER.debug("ParticipantDeregisterAck message received as responseTo {}",
229             participantDeregisterAckMsg.getResponseTo());
230     }
231
232     /**
233      * Handle a ParticipantUpdate message.
234      *
235      * @param participantUpdateMsg the ParticipantUpdate message
236      */
237     public void handleParticipantUpdate(ParticipantUpdate participantUpdateMsg) {
238         LOGGER.debug("ParticipantUpdate message received for participantId {}",
239             participantUpdateMsg.getParticipantId());
240
241         if (!participantUpdateMsg.appliesTo(participantType, participantId)) {
242             return;
243         }
244
245         Map<UUID, ControlLoopElementDefinition> clDefinitionMap =
246                 participantUpdateMsg.getParticipantDefinitionUpdateMap().get(participantUpdateMsg.getParticipantId());
247
248         for (ControlLoopElementDefinition element : clDefinitionMap.values()) {
249             clElementDefsOnThisParticipant.put(element.getId(), element);
250         }
251
252         sendParticipantUpdateAck(participantUpdateMsg.getMessageId());
253     }
254
255     /**
256      * Method to send ParticipantUpdateAck message to controlloop runtime.
257      */
258     public void sendParticipantUpdateAck(UUID messageId) {
259         var participantUpdateAck = new ParticipantUpdateAck();
260         participantUpdateAck.setResponseTo(messageId);
261         participantUpdateAck.setMessage("Participant Update Ack message");
262         participantUpdateAck.setResult(true);
263         participantUpdateAck.setParticipantId(participantId);
264         participantUpdateAck.setParticipantType(participantType);
265
266         sender.sendParticipantUpdateAck(participantUpdateAck);
267     }
268
269     /**
270      * Method to send heartbeat to controlloop runtime.
271      */
272     public ParticipantStatus makeHeartbeat() {
273         ParticipantStatus heartbeat = new ParticipantStatus();
274         heartbeat.setParticipantId(participantId);
275         heartbeat.setParticipantStatistics(participantStatistics);
276         heartbeat.setParticipantType(participantType);
277         heartbeat.setHealthStatus(healthStatus);
278         return heartbeat;
279     }
280 }