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