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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.participant.intermediary.handler;
23 import java.io.Closeable;
24 import java.util.Objects;
27 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant;
28 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantHealthStatus;
29 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
30 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatistics;
31 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantHealthCheck;
32 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessage;
33 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantResponseDetails;
34 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantResponseStatus;
35 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStateChange;
36 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.MessageSender;
37 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantStatusPublisher;
38 import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantIntermediaryParameters;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * This class is responsible for managing the state of a participant.
47 public class ParticipantHandler implements Closeable {
48 private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantHandler.class);
50 private final ToscaConceptIdentifier participantType;
51 private final ToscaConceptIdentifier participantId;
52 private final MessageSender sender;
53 private final ControlLoopHandler controlLoopHandler;
54 private final ParticipantStatistics participantStatistics;
57 private ParticipantState state = ParticipantState.UNKNOWN;
60 private ParticipantHealthStatus healthStatus = ParticipantHealthStatus.UNKNOWN;
63 * Constructor, set the participant ID and sender.
65 * @param parameters the parameters of the participant
66 * @param publisher the publisher for sending responses to messages
68 public ParticipantHandler(ParticipantIntermediaryParameters parameters, ParticipantStatusPublisher publisher) {
69 this.participantType = parameters.getParticipantType();
70 this.participantId = parameters.getParticipantId();
71 this.sender = new MessageSender(this, publisher, parameters.getReportingTimeInterval());
72 this.controlLoopHandler = new ControlLoopHandler(parameters, sender);
73 this.participantStatistics = new ParticipantStatistics();
79 controlLoopHandler.close();
83 * Method which handles a participant state change event from clamp.
85 * @param stateChangeMsg participant state change message
87 public void handleParticipantStateChange(final ParticipantStateChange stateChangeMsg) {
89 if (!stateChangeMsg.appliesTo(participantType, participantId)) {
93 ParticipantResponseDetails response = new ParticipantResponseDetails(stateChangeMsg);
95 switch (stateChangeMsg.getState()) {
97 handlePassiveState(response);
100 handleActiveState(response);
103 handleSafeState(response);
106 handleTestState(response);
109 handleTerminatedState(response);
112 LOGGER.debug("StateChange message has no state, state is null {}", stateChangeMsg.getParticipantId());
113 response.setResponseStatus(ParticipantResponseStatus.FAIL);
114 response.setResponseMessage("StateChange message has invalid state for participantId "
115 + stateChangeMsg.getParticipantId());
119 sender.sendResponse(response);
124 * Method which handles a participant health check event from clamp.
126 * @param healthCheckMsg participant health check message
128 public void handleParticipantHealthCheck(final ParticipantHealthCheck healthCheckMsg) {
129 ParticipantResponseDetails response = new ParticipantResponseDetails(healthCheckMsg);
130 response.setResponseStatus(ParticipantResponseStatus.SUCCESS);
131 response.setResponseMessage(healthStatus.toString());
133 sender.sendResponse(response);
137 * Method to handle when the new state from participant is active.
139 * @param response participant response
141 private void handleActiveState(final ParticipantResponseDetails response) {
142 handleStateChange(ParticipantState.ACTIVE, response);
146 * Method to handle when the new state from participant is passive.
148 * @param response participant response
150 private void handlePassiveState(final ParticipantResponseDetails response) {
151 handleStateChange(ParticipantState.PASSIVE, response);
155 * Method to handle when the new state from participant is safe.
157 * @param response participant response
159 private void handleSafeState(final ParticipantResponseDetails response) {
160 handleStateChange(ParticipantState.SAFE, response);
164 * Method to handle when the new state from participant is TEST.
166 * @param response participant response
168 private void handleTestState(final ParticipantResponseDetails response) {
169 handleStateChange(ParticipantState.TEST, response);
173 * Method to handle when the new state from participant is Terminated.
175 * @param response participant response
177 private void handleTerminatedState(final ParticipantResponseDetails response) {
178 handleStateChange(ParticipantState.TERMINATED, response);
181 private void handleStateChange(ParticipantState newParticipantState, ParticipantResponseDetails response) {
182 if (state.equals(newParticipantState)) {
183 response.setResponseStatus(ParticipantResponseStatus.SUCCESS);
184 response.setResponseMessage("Participant already in state " + newParticipantState);
186 response.setResponseStatus(ParticipantResponseStatus.SUCCESS);
187 response.setResponseMessage("Participant state changed from " + state + " to " + newParticipantState);
188 state = newParticipantState;
193 * Method to update participant state.
195 * @param definition participant definition
196 * @param participantState participant state
198 public Participant updateParticipantState(ToscaConceptIdentifier definition,
199 ParticipantState participantState) {
200 if (!Objects.equals(definition, participantId)) {
201 LOGGER.debug("No participant with this ID {}", definition.getName());
204 ParticipantResponseDetails response = new ParticipantResponseDetails();
205 handleStateChange(participantState, response);
206 sender.sendResponse(response);
207 return getParticipant(definition.getName(), definition.getVersion());
211 * Get participants as a {@link Participant} class.
213 * @return the participant
215 public Participant getParticipant(String name, String version) {
216 if (participantId.getName().equals(name)) {
217 Participant participant = new Participant();
218 participant.setDefinition(participantId);
219 participant.setParticipantState(state);
220 participant.setHealthStatus(healthStatus);
227 * Check if a participant message applies to this participant handler.
229 * @param partipantMsg the message to check
230 * @return true if it applies, false otherwise
232 public boolean canHandle(ParticipantMessage partipantMsg) {
233 return partipantMsg.appliesTo(participantType, participantId);