aboutsummaryrefslogtreecommitdiffstats
path: root/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ControlLoopHandler.java
blob: cc2a66b58b450c7aef972f839513c98a2ec7c2e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2021 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.clamp.controlloop.participant.intermediary.handler;

import java.io.Closeable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import lombok.Getter;
import org.apache.commons.collections4.CollectionUtils;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopStateChange;
import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopUpdate;
import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantResponseDetails;
import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantResponseStatus;
import org.onap.policy.clamp.controlloop.participant.intermediary.api.ControlLoopElementListener;
import org.onap.policy.clamp.controlloop.participant.intermediary.comm.MessageSender;
import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantIntermediaryParameters;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*
 * This class is responsible for managing the state of all control loops in the participant.
 */
public class ControlLoopHandler implements Closeable {
    private static final Logger LOGGER = LoggerFactory.getLogger(ControlLoopHandler.class);

    private ToscaConceptIdentifier participantType = null;
    private ToscaConceptIdentifier participantId = null;
    private MessageSender messageSender = null;

    private final Map<ToscaConceptIdentifier, ControlLoop> controlLoopMap = new LinkedHashMap<>();

    private final Map<UUID, ControlLoopElement> elementsOnThisParticipant = new LinkedHashMap<>();

    @Getter
    private List<ControlLoopElementListener> listeners = new ArrayList<>();

    public ControlLoopHandler() {}

    /**
     * Constructor, set the participant ID and messageSender.
     *
     * @param parameters the parameters of the participant
     * @param messageSender the messageSender for sending responses to messages
     */
    public ControlLoopHandler(ParticipantIntermediaryParameters parameters, MessageSender messageSender) {
        this.participantType = parameters.getParticipantType();
        this.participantId = parameters.getParticipantId();
        this.messageSender = messageSender;
    }

    @Override
    public void close() {
        // No explicit action on this class
    }

    public void registerControlLoopElementListener(ControlLoopElementListener listener) {
        listeners.add(listener);
    }
    
    /**
     * Handle a control loop element state change message.
     *
     * @param id controlloop element id
     * @param orderedState the current state
     * @param newState the ordered state
     * @return controlLoopElement the updated controlloop element
     */
    public ControlLoopElement updateControlLoopElementState(UUID id, ControlLoopOrderedState orderedState,
            ControlLoopState newState) {

        if (id == null) {
            return null;
        }

        ControlLoopElement clElement = elementsOnThisParticipant.get(id);
        if (clElement != null) {
            clElement.setOrderedState(orderedState);
            clElement.setState(newState);
            LOGGER.debug("Control loop element {} state changed to {}", id, newState);
            ParticipantResponseDetails response = new ParticipantResponseDetails();
            response.setResponseStatus(ParticipantResponseStatus.SUCCESS);
            response.setResponseMessage("ControlLoopElement state changed to {} " + newState);
            messageSender.sendResponse(response);
            return clElement;
        }

        return null;
    }

    /**
     * Handle a control loop element statistics.
     *
     * @param id controlloop element id
     * @param elementStatistics control loop element Statistics
     */
    public void updateControlLoopElementStatistics(UUID id, ClElementStatistics elementStatistics) {
        ControlLoopElement clElement = elementsOnThisParticipant.get(id);
        if (clElement != null) {
            elementStatistics.setParticipantId(participantId);
            elementStatistics.setId(id);
            clElement.setClElementStatistics(elementStatistics);
        }
    }

    /**
     * Handle a control loop state change message.
     *
     * @param stateChangeMsg the state change message
     */
    public void handleControlLoopStateChange(ParticipantControlLoopStateChange stateChangeMsg) {
        if (stateChangeMsg.getControlLoopId() == null) {
            return;
        }

        ControlLoop controlLoop = controlLoopMap.get(stateChangeMsg.getControlLoopId());

        if (controlLoop == null) {
            LOGGER.debug("Control loop {} does not use this participant", stateChangeMsg.getControlLoopId());
            return;
        }

        ParticipantResponseDetails response = new ParticipantResponseDetails(stateChangeMsg);
        handleState(controlLoop, response, stateChangeMsg.getOrderedState());
        messageSender.sendResponse(response);
    }

    /**
     * Method to handle state changes.
     *
     * @param controlLoop participant response
     * @param response participant response
     * @param orderedState controlloop ordered state
     */
    private void handleState(final ControlLoop controlLoop, final ParticipantResponseDetails response,
            ControlLoopOrderedState orderedState) {
        switch (orderedState) {
            case UNINITIALISED:
                handleUninitialisedState(controlLoop, orderedState, response);
                break;
            case PASSIVE:
                handlePassiveState(controlLoop, orderedState, response);
                break;
            case RUNNING:
                handleRunningState(controlLoop, orderedState, response);
                break;
            default:
                LOGGER.debug("StateChange message has no state, state is null {}", controlLoop.getDefinition());
                break;
        }
    }

    /**
     * Handle a control loop update message.
     *
     * @param updateMsg the update message
     */
    public void handleControlLoopUpdate(ParticipantControlLoopUpdate updateMsg) {

        if (!updateMsg.appliesTo(participantType, participantId)) {
            return;
        }

        ControlLoop controlLoop = controlLoopMap.get(updateMsg.getControlLoopId());

        ParticipantResponseDetails response = new ParticipantResponseDetails(updateMsg);

        // TODO: Updates to existing ControlLoops are not supported yet (Addition/Removal of ControlLoop
        // elements to existing ControlLoop has to be supported).
        if (controlLoop != null) {
            response.setResponseStatus(ParticipantResponseStatus.FAIL);
            response.setResponseMessage("Control loop " + updateMsg.getControlLoopId()
                    + " already defined on participant " + participantId);

            messageSender.sendResponse(response);
            return;
        }

        controlLoop = updateMsg.getControlLoop();
        controlLoop.getElements().values().removeIf(element -> !participantType.equals(element.getParticipantType()));

        controlLoopMap.put(updateMsg.getControlLoopId(), controlLoop);
        for (ControlLoopElement element : updateMsg.getControlLoop().getElements().values()) {
            element.setState(element.getOrderedState().asState());
            element.setParticipantId(participantId);
            elementsOnThisParticipant.put(element.getId(), element);
        }

        for (ControlLoopElementListener clElementListener : listeners) {
            try {
                for (ControlLoopElement element : updateMsg.getControlLoop().getElements().values()) {
                    clElementListener.controlLoopElementUpdate(element, updateMsg.getControlLoopDefinition());
                }
            } catch (PfModelException e) {
                LOGGER.debug("Control loop element update failed {}", updateMsg.getControlLoopId());
            }
        }

        response.setResponseStatus(ParticipantResponseStatus.SUCCESS);
        response.setResponseMessage(
                "Control loop " + updateMsg.getControlLoopId() + " defined on participant " + participantId);

        messageSender.sendResponse(response);
    }

    /**
     * Method to handle when the new state from participant is UNINITIALISED state.
     *
     * @param controlLoop participant response
     * @param orderedState orderedState
     * @param response participant response
     */
    private void handleUninitialisedState(final ControlLoop controlLoop, final ControlLoopOrderedState orderedState,
            final ParticipantResponseDetails response) {
        handleStateChange(controlLoop, orderedState, ControlLoopState.UNINITIALISED, response);
        controlLoopMap.remove(controlLoop.getKey().asIdentifier());

        for (ControlLoopElementListener clElementListener : listeners) {
            try {
                for (ControlLoopElement element : controlLoop.getElements().values()) {
                    clElementListener.controlLoopElementStateChange(element.getId(), element.getState(),
                            orderedState);
                }
            } catch (PfModelException e) {
                LOGGER.debug("Control loop element update failed {}", controlLoop.getDefinition());
            }
        }
    }

    /**
     * Method to handle when the new state from participant is PASSIVE state.
     *
     * @param controlLoop participant response
     * @param orderedState orderedState
     * @param response participant response
     */
    private void handlePassiveState(final ControlLoop controlLoop, final ControlLoopOrderedState orderedState,
            final ParticipantResponseDetails response) {
        handleStateChange(controlLoop, orderedState, ControlLoopState.PASSIVE, response);
    }

    /**
     * Method to handle when the new state from participant is RUNNING state.
     *
     * @param controlLoop participant response
     * @param orderedState orderedState
     * @param response participant response
     */
    private void handleRunningState(final ControlLoop controlLoop, final ControlLoopOrderedState orderedState,
            final ParticipantResponseDetails response) {
        handleStateChange(controlLoop, orderedState, ControlLoopState.RUNNING, response);
    }
    
    /**
     * Method to update the state of control loop elements.
     *
     * @param controlLoop participant status in memory
     * @param orderedState orderedState
     * @param state new state of the control loop elements
     */
    private void handleStateChange(ControlLoop controlLoop, final ControlLoopOrderedState orderedState,
            ControlLoopState newState, ParticipantResponseDetails response) {

        if (orderedState.equals(controlLoop.getOrderedState())) {
            response.setResponseStatus(ParticipantResponseStatus.SUCCESS);
            response.setResponseMessage("Control loop is already in state " + orderedState);
            return;
        }

        if (!CollectionUtils.isEmpty(controlLoop.getElements().values())) {
            controlLoop.getElements().values().forEach(element -> {
                    element.setState(newState);
                    element.setOrderedState(orderedState);
                }
            );
        }

        response.setResponseStatus(ParticipantResponseStatus.SUCCESS);
        response.setResponseMessage("ControlLoop state changed from " + controlLoop.getOrderedState()
                        + " to " + orderedState);
        controlLoop.setOrderedState(orderedState);
    }


    /**
     * Get control loops as a {@link ConrolLoops} class.
     *
     * @return the control loops
     */
    public ControlLoops getControlLoops() {
        ControlLoops controlLoops = new ControlLoops();
        controlLoops.setControlLoopList(new ArrayList<>(controlLoopMap.values()));
        return controlLoops;
    }
}