1150471aeb6264738dffb73cbb9a9357652d5f6c
[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.Objects;
25 import lombok.Getter;
26 import lombok.Setter;
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;
39
40 /**
41  * This class is responsible for managing the state of a participant.
42  */
43 @Getter
44 public class ParticipantHandler implements Closeable {
45     private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantHandler.class);
46
47     private final ToscaConceptIdentifier participantId;
48     private final MessageSender sender;
49     private final ControlLoopHandler controlLoopHandler;
50
51     @Setter
52     private ParticipantState state = ParticipantState.UNKNOWN;
53
54     @Setter
55     private ParticipantHealthStatus healthStatus = ParticipantHealthStatus.UNKNOWN;
56
57     /**
58      * Constructor, set the participant ID and sender.
59      *
60      * @param parameters the parameters of the participant
61      * @param publisher the publisher for sending responses to messages
62      */
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);
67     }
68
69     @Override
70     public void close() {
71         sender.close();
72         controlLoopHandler.close();
73     }
74
75     /**
76      * Method which handles a participant state change event from clamp.
77      *
78      * @param stateChangeMsg participant state change message
79      */
80     public void handleParticipantStateChange(final ParticipantStateChange stateChangeMsg) {
81
82         if (!stateChangeMsg.appliesTo(participantId)) {
83             return;
84         }
85
86         ParticipantResponseDetails response = new ParticipantResponseDetails(stateChangeMsg);
87
88         switch (stateChangeMsg.getState()) {
89             case PASSIVE:
90                 handlePassiveState(response);
91                 break;
92             case ACTIVE:
93                 handleActiveState(response);
94                 break;
95             case SAFE:
96                 handleSafeState(response);
97                 break;
98             case TEST:
99                 handleTestState(response);
100                 break;
101             case TERMINATED:
102                 handleTerminatedState(response);
103                 break;
104             default:
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());
109                 break;
110         }
111
112         sender.sendResponse(response);
113     }
114
115     /**
116      * Method to handle when the new state from participant is active.
117      *
118      * @param response participant response
119      */
120     private void handleActiveState(final ParticipantResponseDetails response) {
121         handleStateChange(ParticipantState.ACTIVE, response);
122     }
123
124     /**
125      * Method to handle when the new state from participant is passive.
126      *
127      * @param response participant response
128      */
129     private void handlePassiveState(final ParticipantResponseDetails response) {
130         handleStateChange(ParticipantState.PASSIVE, response);
131     }
132
133     /**
134      * Method to handle when the new state from participant is safe.
135      *
136      * @param response participant response
137      */
138     private void handleSafeState(final ParticipantResponseDetails response) {
139         handleStateChange(ParticipantState.SAFE, response);
140     }
141
142     /**
143      * Method to handle when the new state from participant is TEST.
144      *
145      * @param response participant response
146      */
147     private void handleTestState(final ParticipantResponseDetails response) {
148         handleStateChange(ParticipantState.TEST, response);
149     }
150
151     /**
152      * Method to handle when the new state from participant is Terminated.
153      *
154      * @param response participant response
155      */
156     private void handleTerminatedState(final ParticipantResponseDetails response) {
157         handleStateChange(ParticipantState.TERMINATED, response);
158     }
159
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);
164         } else {
165             response.setResponseStatus(ParticipantResponseStatus.SUCCESS);
166             response.setResponseMessage("Participant state changed from " + state + " to " + newParticipantState);
167             state = newParticipantState;
168         }
169     }
170
171     /**
172      * Method to update participant state.
173      *
174      * @param definition participant definition
175      * @param participantState participant state
176      */
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());
181             return null;
182         }
183         ParticipantResponseDetails response = new ParticipantResponseDetails();
184         handleStateChange(participantState, response);
185         sender.sendResponse(response);
186         return getParticipant(definition.getName(), definition.getVersion());
187     }
188
189     /**
190      * Get participants as a {@link Participant} class.
191      *
192      * @return the participant
193      */
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);
200             return participant;
201         }
202         return null;
203     }
204 }