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.messages.dmaap.participant.ParticipantResponseDetails;
31 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantResponseStatus;
32 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStateChange;
33 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.MessageSender;
34 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantStatusPublisher;
35 import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantIntermediaryParameters;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * This class is responsible for managing the state of a participant.
44 public class ParticipantHandler implements Closeable {
45 private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantHandler.class);
47 private final ToscaConceptIdentifier participantId;
48 private final MessageSender sender;
49 private final ControlLoopHandler controlLoopHandler;
52 private ParticipantState state = ParticipantState.UNKNOWN;
55 private ParticipantHealthStatus healthStatus = ParticipantHealthStatus.UNKNOWN;
58 * Constructor, set the participant ID and sender.
60 * @param parameters the parameters of the participant
61 * @param publisher the publisher for sending responses to messages
63 public ParticipantHandler(ParticipantIntermediaryParameters parameters, ParticipantStatusPublisher publisher) {
64 this.participantId = parameters.getParticipantId();
65 this.sender = new MessageSender(this, publisher, parameters.getReportingTimeInterval());
66 this.controlLoopHandler = new ControlLoopHandler(parameters, sender);
72 controlLoopHandler.close();
76 * Method which handles a participant state change event from clamp.
78 * @param stateChangeMsg participant state change message
80 public void handleParticipantStateChange(final ParticipantStateChange stateChangeMsg) {
82 if (!stateChangeMsg.appliesTo(participantId)) {
86 ParticipantResponseDetails response = new ParticipantResponseDetails(stateChangeMsg);
88 switch (stateChangeMsg.getState()) {
90 handlePassiveState(response);
93 handleActiveState(response);
96 handleSafeState(response);
99 handleTestState(response);
102 handleTerminatedState(response);
105 LOGGER.debug("StateChange message has no state, state is null {}", stateChangeMsg.getParticipantId());
106 response.setResponseStatus(ParticipantResponseStatus.FAIL);
107 response.setResponseMessage("StateChange message has invalid state for participantId "
108 + stateChangeMsg.getParticipantId());
112 sender.sendResponse(response);
116 * Method to handle when the new state from participant is active.
118 * @param response participant response
120 private void handleActiveState(final ParticipantResponseDetails response) {
121 handleStateChange(ParticipantState.ACTIVE, response);
125 * Method to handle when the new state from participant is passive.
127 * @param response participant response
129 private void handlePassiveState(final ParticipantResponseDetails response) {
130 handleStateChange(ParticipantState.PASSIVE, response);
134 * Method to handle when the new state from participant is safe.
136 * @param response participant response
138 private void handleSafeState(final ParticipantResponseDetails response) {
139 handleStateChange(ParticipantState.SAFE, response);
143 * Method to handle when the new state from participant is TEST.
145 * @param response participant response
147 private void handleTestState(final ParticipantResponseDetails response) {
148 handleStateChange(ParticipantState.TEST, response);
152 * Method to handle when the new state from participant is Terminated.
154 * @param response participant response
156 private void handleTerminatedState(final ParticipantResponseDetails response) {
157 handleStateChange(ParticipantState.TERMINATED, response);
160 private void handleStateChange(ParticipantState newParticipantState, ParticipantResponseDetails response) {
161 if (state.equals(newParticipantState)) {
162 response.setResponseStatus(ParticipantResponseStatus.SUCCESS);
163 response.setResponseMessage("Participant already in state " + newParticipantState);
165 response.setResponseStatus(ParticipantResponseStatus.SUCCESS);
166 response.setResponseMessage("Participant state changed from " + state + " to " + newParticipantState);
167 state = newParticipantState;
172 * Method to update participant state.
174 * @param definition participant definition
175 * @param participantState participant state
177 public Participant updateParticipantState(ToscaConceptIdentifier definition,
178 ParticipantState participantState) {
179 if (!Objects.equals(definition, participantId)) {
180 LOGGER.debug("No participant with this ID {}", definition.getName());
183 ParticipantResponseDetails response = new ParticipantResponseDetails();
184 handleStateChange(participantState, response);
185 sender.sendResponse(response);
186 return getParticipant(definition.getName(), definition.getVersion());
190 * Get participants as a {@link Participant} class.
192 * @return the participant
194 public Participant getParticipant(String name, String version) {
195 if (participantId.getName().equals(name)) {
196 Participant participant = new Participant();
197 participant.setDefinition(participantId);
198 participant.setParticipantState(state);
199 participant.setHealthStatus(healthStatus);