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.ArrayList;
25 import java.util.LinkedHashMap;
26 import java.util.List;
28 import java.util.UUID;
30 import org.apache.commons.collections4.CollectionUtils;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
37 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopStateChange;
38 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopUpdate;
39 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantResponseDetails;
40 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantResponseStatus;
41 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ControlLoopElementListener;
42 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.MessageSender;
43 import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantIntermediaryParameters;
44 import org.onap.policy.models.base.PfModelException;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 * This class is responsible for managing the state of all control loops in the participant.
52 public class ControlLoopHandler implements Closeable {
53 private static final Logger LOGGER = LoggerFactory.getLogger(ControlLoopHandler.class);
55 private ToscaConceptIdentifier participantType = null;
56 private ToscaConceptIdentifier participantId = null;
57 private MessageSender messageSender = null;
59 private final Map<ToscaConceptIdentifier, ControlLoop> controlLoopMap = new LinkedHashMap<>();
61 private final Map<UUID, ControlLoopElement> elementsOnThisParticipant = new LinkedHashMap<>();
64 private List<ControlLoopElementListener> listeners = new ArrayList<>();
66 public ControlLoopHandler() {}
69 * Constructor, set the participant ID and messageSender.
71 * @param parameters the parameters of the participant
72 * @param messageSender the messageSender for sending responses to messages
74 public ControlLoopHandler(ParticipantIntermediaryParameters parameters, MessageSender messageSender) {
75 this.participantType = parameters.getParticipantType();
76 this.participantId = parameters.getParticipantId();
77 this.messageSender = messageSender;
82 // No explicit action on this class
85 public void registerControlLoopElementListener(ControlLoopElementListener listener) {
86 listeners.add(listener);
90 * Handle a control loop element state change message.
92 * @param id controlloop element id
93 * @param orderedState the current state
94 * @param newState the ordered state
95 * @return controlLoopElement the updated controlloop element
97 public ControlLoopElement updateControlLoopElementState(UUID id, ControlLoopOrderedState orderedState,
98 ControlLoopState newState) {
104 ControlLoopElement clElement = elementsOnThisParticipant.get(id);
105 if (clElement != null) {
106 clElement.setOrderedState(orderedState);
107 clElement.setState(newState);
108 LOGGER.debug("Control loop element {} state changed to {}", id, newState);
109 ParticipantResponseDetails response = new ParticipantResponseDetails();
110 response.setResponseStatus(ParticipantResponseStatus.SUCCESS);
111 response.setResponseMessage("ControlLoopElement state changed to {} " + newState);
112 messageSender.sendResponse(response);
120 * Handle a control loop element statistics.
122 * @param id controlloop element id
123 * @param elementStatistics control loop element Statistics
125 public void updateControlLoopElementStatistics(UUID id, ClElementStatistics elementStatistics) {
126 ControlLoopElement clElement = elementsOnThisParticipant.get(id);
127 if (clElement != null) {
128 elementStatistics.setParticipantId(participantId);
129 elementStatistics.setId(id);
130 clElement.setClElementStatistics(elementStatistics);
135 * Handle a control loop state change message.
137 * @param stateChangeMsg the state change message
139 public void handleControlLoopStateChange(ParticipantControlLoopStateChange stateChangeMsg) {
140 if (stateChangeMsg.getControlLoopId() == null) {
144 ControlLoop controlLoop = controlLoopMap.get(stateChangeMsg.getControlLoopId());
146 if (controlLoop == null) {
147 LOGGER.debug("Control loop {} does not use this participant", stateChangeMsg.getControlLoopId());
151 ParticipantResponseDetails response = new ParticipantResponseDetails(stateChangeMsg);
152 handleState(controlLoop, response, stateChangeMsg.getOrderedState());
153 messageSender.sendResponse(response);
157 * Method to handle state changes.
159 * @param controlLoop participant response
160 * @param response participant response
161 * @param orderedState controlloop ordered state
163 private void handleState(final ControlLoop controlLoop, final ParticipantResponseDetails response,
164 ControlLoopOrderedState orderedState) {
165 switch (orderedState) {
167 handleUninitialisedState(controlLoop, orderedState, response);
170 handlePassiveState(controlLoop, orderedState, response);
173 handleRunningState(controlLoop, orderedState, response);
176 LOGGER.debug("StateChange message has no state, state is null {}", controlLoop.getDefinition());
182 * Handle a control loop update message.
184 * @param updateMsg the update message
186 public void handleControlLoopUpdate(ParticipantControlLoopUpdate updateMsg) {
188 if (!updateMsg.appliesTo(participantType, participantId)) {
192 ControlLoop controlLoop = controlLoopMap.get(updateMsg.getControlLoopId());
194 ParticipantResponseDetails response = new ParticipantResponseDetails(updateMsg);
196 // TODO: Updates to existing ControlLoops are not supported yet (Addition/Removal of ControlLoop
197 // elements to existing ControlLoop has to be supported).
198 if (controlLoop != null) {
199 response.setResponseStatus(ParticipantResponseStatus.FAIL);
200 response.setResponseMessage("Control loop " + updateMsg.getControlLoopId()
201 + " already defined on participant " + participantId);
203 messageSender.sendResponse(response);
207 controlLoop = updateMsg.getControlLoop();
208 controlLoop.getElements().values().removeIf(element -> !participantType.equals(element.getParticipantType()));
210 controlLoopMap.put(updateMsg.getControlLoopId(), controlLoop);
211 for (ControlLoopElement element : updateMsg.getControlLoop().getElements().values()) {
212 element.setState(element.getOrderedState().asState());
213 element.setParticipantId(participantId);
214 elementsOnThisParticipant.put(element.getId(), element);
217 for (ControlLoopElementListener clElementListener : listeners) {
219 for (ControlLoopElement element : updateMsg.getControlLoop().getElements().values()) {
220 clElementListener.controlLoopElementUpdate(element, updateMsg.getControlLoopDefinition());
222 } catch (PfModelException e) {
223 LOGGER.debug("Control loop element update failed {}", updateMsg.getControlLoopId());
227 response.setResponseStatus(ParticipantResponseStatus.SUCCESS);
228 response.setResponseMessage(
229 "Control loop " + updateMsg.getControlLoopId() + " defined on participant " + participantId);
231 messageSender.sendResponse(response);
235 * Method to handle when the new state from participant is UNINITIALISED state.
237 * @param controlLoop participant response
238 * @param orderedState orderedState
239 * @param response participant response
241 private void handleUninitialisedState(final ControlLoop controlLoop, final ControlLoopOrderedState orderedState,
242 final ParticipantResponseDetails response) {
243 handleStateChange(controlLoop, orderedState, ControlLoopState.UNINITIALISED, response);
244 controlLoopMap.remove(controlLoop.getKey().asIdentifier());
246 for (ControlLoopElementListener clElementListener : listeners) {
248 for (ControlLoopElement element : controlLoop.getElements().values()) {
249 clElementListener.controlLoopElementStateChange(element.getId(), element.getState(),
252 } catch (PfModelException e) {
253 LOGGER.debug("Control loop element update failed {}", controlLoop.getDefinition());
259 * Method to handle when the new state from participant is PASSIVE state.
261 * @param controlLoop participant response
262 * @param orderedState orderedState
263 * @param response participant response
265 private void handlePassiveState(final ControlLoop controlLoop, final ControlLoopOrderedState orderedState,
266 final ParticipantResponseDetails response) {
267 handleStateChange(controlLoop, orderedState, ControlLoopState.PASSIVE, response);
271 * Method to handle when the new state from participant is RUNNING state.
273 * @param controlLoop participant response
274 * @param orderedState orderedState
275 * @param response participant response
277 private void handleRunningState(final ControlLoop controlLoop, final ControlLoopOrderedState orderedState,
278 final ParticipantResponseDetails response) {
279 handleStateChange(controlLoop, orderedState, ControlLoopState.RUNNING, response);
283 * Method to update the state of control loop elements.
285 * @param controlLoop participant status in memory
286 * @param orderedState orderedState
287 * @param state new state of the control loop elements
289 private void handleStateChange(ControlLoop controlLoop, final ControlLoopOrderedState orderedState,
290 ControlLoopState newState, ParticipantResponseDetails response) {
292 if (orderedState.equals(controlLoop.getOrderedState())) {
293 response.setResponseStatus(ParticipantResponseStatus.SUCCESS);
294 response.setResponseMessage("Control loop is already in state " + orderedState);
298 if (!CollectionUtils.isEmpty(controlLoop.getElements().values())) {
299 controlLoop.getElements().values().forEach(element -> {
300 element.setState(newState);
301 element.setOrderedState(orderedState);
306 response.setResponseStatus(ParticipantResponseStatus.SUCCESS);
307 response.setResponseMessage("ControlLoop state changed from " + controlLoop.getOrderedState()
308 + " to " + orderedState);
309 controlLoop.setOrderedState(orderedState);
314 * Get control loops as a {@link ConrolLoops} class.
316 * @return the control loops
318 public ControlLoops getControlLoops() {
319 ControlLoops controlLoops = new ControlLoops();
320 controlLoops.setControlLoopList(new ArrayList<>(controlLoopMap.values()));