aboutsummaryrefslogtreecommitdiffstats
path: root/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ControlLoopHandler.java
blob: 5a0f4989fc9fe33f8627596c653338e2caff36a2 (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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2021 Nordix Foundation.
 *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import lombok.Getter;
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.ControlLoopElementAck;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition;
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.controlloop.concepts.ParticipantUpdates;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUtils;
import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopAck;
import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopStateChange;
import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate;
import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessageType;
import org.onap.policy.clamp.controlloop.participant.intermediary.api.ControlLoopElementListener;
import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantMessagePublisher;
import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantParameters;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

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

    private final ToscaConceptIdentifier participantType;
    private final ToscaConceptIdentifier participantId;
    private final ParticipantMessagePublisher publisher;

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

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

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

    /**
     * Constructor, set the participant ID and messageSender.
     *
     * @param parameters the parameters of the participant
     * @param publisher the ParticipantMessage Publisher
     */
    public ControlLoopHandler(ParticipantParameters parameters, ParticipantMessagePublisher publisher) {
        this.participantType = parameters.getIntermediaryParameters().getParticipantType();
        this.participantId = parameters.getIntermediaryParameters().getParticipantId();
        this.publisher = publisher;
    }

    public void registerControlLoopElementListener(ControlLoopElementListener listener) {
        listeners.add(listener);
    }

    /**
     * Handle a control loop element state change message.
     *
     * @param controlLoopId the controlLoop Id
     * @param id the controlLoop UUID
     * @param orderedState the current state
     * @param newState the ordered state
     * @return controlLoopElement the updated controlloop element
     */
    public ControlLoopElement updateControlLoopElementState(ToscaConceptIdentifier controlLoopId, UUID id,
            ControlLoopOrderedState orderedState, ControlLoopState newState) {

        if (id == null) {
            LOGGER.warn("Cannot update Control loop element state, id is null");
            return null;
        }

        // Update states of ControlLoopElement in controlLoopMap
        for (var controlLoop : controlLoopMap.values()) {
            var element = controlLoop.getElements().get(id);
            if (element != null) {
                element.setOrderedState(orderedState);
                element.setState(newState);
            }
            var checkOpt = controlLoop.getElements().values().stream()
                    .filter(clElement -> !newState.equals(clElement.getState())).findAny();
            if (checkOpt.isEmpty()) {
                controlLoop.setState(newState);
                controlLoop.setOrderedState(orderedState);
            }
        }

        // Update states of ControlLoopElement in elementsOnThisParticipant
        var clElement = elementsOnThisParticipant.get(id);
        if (clElement != null) {
            var controlLoopStateChangeAck = new ControlLoopAck(ParticipantMessageType.CONTROLLOOP_STATECHANGE_ACK);
            controlLoopStateChangeAck.setParticipantId(participantId);
            controlLoopStateChangeAck.setParticipantType(participantType);
            controlLoopStateChangeAck.setControlLoopId(controlLoopId);
            clElement.setOrderedState(orderedState);
            clElement.setState(newState);
            controlLoopStateChangeAck.getControlLoopResultMap().put(clElement.getId(), new ControlLoopElementAck(
                    newState, true, "Control loop element {} state changed to {}\", id, newState)"));
            LOGGER.debug("Control loop element {} state changed to {}", id, newState);
            controlLoopStateChangeAck.setMessage("ControlLoopElement state changed to {} " + newState);
            controlLoopStateChangeAck.setResult(true);
            publisher.sendControlLoopAck(controlLoopStateChangeAck);
            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) {
        var 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
     * @param clElementDefinitions the list of ControlLoopElementDefinition
     */
    public void handleControlLoopStateChange(ControlLoopStateChange stateChangeMsg,
            List<ControlLoopElementDefinition> clElementDefinitions) {
        if (stateChangeMsg.getControlLoopId() == null) {
            return;
        }

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

        if (controlLoop == null) {
            var controlLoopAck = new ControlLoopAck(ParticipantMessageType.CONTROLLOOP_STATECHANGE_ACK);
            controlLoopAck.setParticipantId(participantId);
            controlLoopAck.setParticipantType(participantType);
            controlLoopAck.setMessage("Control loop " + stateChangeMsg.getControlLoopId()
                    + " does not use this participant " + participantId);
            controlLoopAck.setResult(false);
            controlLoopAck.setResponseTo(stateChangeMsg.getMessageId());
            controlLoopAck.setControlLoopId(stateChangeMsg.getControlLoopId());
            publisher.sendControlLoopAck(controlLoopAck);
            LOGGER.debug("Control loop {} does not use this participant", stateChangeMsg.getControlLoopId());
            return;
        }

        handleState(controlLoop, stateChangeMsg.getOrderedState(), stateChangeMsg.getStartPhase(),
                clElementDefinitions);
    }

    /**
     * Method to handle state changes.
     *
     * @param controlLoop participant response
     * @param orderedState controlloop ordered state
     * @param startPhaseMsg startPhase from message
     * @param clElementDefinitions the list of ControlLoopElementDefinition
     */
    private void handleState(final ControlLoop controlLoop, ControlLoopOrderedState orderedState, Integer startPhaseMsg,
            List<ControlLoopElementDefinition> clElementDefinitions) {
        switch (orderedState) {
            case UNINITIALISED:
                handleUninitialisedState(controlLoop, orderedState, startPhaseMsg, clElementDefinitions);
                break;
            case PASSIVE:
                handlePassiveState(controlLoop, orderedState, startPhaseMsg, clElementDefinitions);
                break;
            case RUNNING:
                handleRunningState(controlLoop, orderedState, startPhaseMsg, clElementDefinitions);
                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
     * @param clElementDefinitions the list of ControlLoopElementDefinition
     */
    public void handleControlLoopUpdate(ControlLoopUpdate updateMsg,
            List<ControlLoopElementDefinition> clElementDefinitions) {

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

        if (0 == updateMsg.getStartPhase()) {
            handleClUpdatePhase0(updateMsg, clElementDefinitions);
        } else {
            handleClUpdatePhaseN(updateMsg, clElementDefinitions);
        }
    }

    private void handleClUpdatePhase0(ControlLoopUpdate updateMsg,
            List<ControlLoopElementDefinition> clElementDefinitions) {
        var controlLoop = controlLoopMap.get(updateMsg.getControlLoopId());

        // TODO: Updates to existing ControlLoops are not supported yet (Addition/Removal of ControlLoop
        // elements to existing ControlLoop has to be supported).
        if (controlLoop != null) {
            var controlLoopUpdateAck = new ControlLoopAck(ParticipantMessageType.CONTROLLOOP_UPDATE_ACK);
            controlLoopUpdateAck.setParticipantId(participantId);
            controlLoopUpdateAck.setParticipantType(participantType);

            controlLoopUpdateAck.setMessage("Control loop " + updateMsg.getControlLoopId()
                    + " already defined on participant " + participantId);
            controlLoopUpdateAck.setResult(false);
            controlLoopUpdateAck.setResponseTo(updateMsg.getMessageId());
            controlLoopUpdateAck.setControlLoopId(updateMsg.getControlLoopId());
            publisher.sendControlLoopAck(controlLoopUpdateAck);
            return;
        }

        if (updateMsg.getParticipantUpdatesList().isEmpty()) {
            LOGGER.warn("No ControlLoopElement updates in message {}", updateMsg.getControlLoopId());
            return;
        }

        var clElements = storeElementsOnThisParticipant(updateMsg.getParticipantUpdatesList());

        var clElementMap = prepareClElementMap(clElements);
        controlLoop = new ControlLoop();
        controlLoop.setDefinition(updateMsg.getControlLoopId());
        controlLoop.setElements(clElementMap);
        controlLoopMap.put(updateMsg.getControlLoopId(), controlLoop);

        handleControlLoopElementUpdate(clElements, clElementDefinitions, updateMsg.getStartPhase(),
                updateMsg.getControlLoopId());
    }

    private void handleClUpdatePhaseN(ControlLoopUpdate updateMsg,
            List<ControlLoopElementDefinition> clElementDefinitions) {

        var clElementList = updateMsg.getParticipantUpdatesList().stream()
                .flatMap(participantUpdate -> participantUpdate.getControlLoopElementList().stream())
                .filter(element -> participantType.equals(element.getParticipantType())).collect(Collectors.toList());

        handleControlLoopElementUpdate(clElementList, clElementDefinitions, updateMsg.getStartPhase(),
                updateMsg.getControlLoopId());
    }

    private void handleControlLoopElementUpdate(List<ControlLoopElement> clElements,
            List<ControlLoopElementDefinition> clElementDefinitions, Integer startPhaseMsg,
            ToscaConceptIdentifier controlLoopId) {
        try {
            for (var element : clElements) {
                var clElementNodeTemplate = getClElementNodeTemplate(clElementDefinitions, element.getDefinition());
                if (clElementNodeTemplate != null) {
                    int startPhase = ParticipantUtils.findStartPhase(clElementNodeTemplate.getProperties());
                    if (startPhaseMsg.equals(startPhase)) {
                        for (var clElementListener : listeners) {
                            clElementListener.controlLoopElementUpdate(controlLoopId, element, clElementNodeTemplate);
                        }
                    }
                }
            }
        } catch (PfModelException e) {
            LOGGER.debug("Control loop element update failed {}", controlLoopId);
        }

    }

    private ToscaNodeTemplate getClElementNodeTemplate(List<ControlLoopElementDefinition> clElementDefinitions,
            ToscaConceptIdentifier clElementDefId) {

        for (var clElementDefinition : clElementDefinitions) {
            if (clElementDefId.getName().contains(clElementDefinition.getClElementDefinitionId().getName())) {
                return clElementDefinition.getControlLoopElementToscaNodeTemplate();
            }
        }
        return null;
    }

    private List<ControlLoopElement> storeElementsOnThisParticipant(List<ParticipantUpdates> participantUpdates) {
        var clElementList = participantUpdates.stream()
                .flatMap(participantUpdate -> participantUpdate.getControlLoopElementList().stream())
                .filter(element -> participantType.equals(element.getParticipantType())).collect(Collectors.toList());

        for (var element : clElementList) {
            elementsOnThisParticipant.put(element.getId(), element);
        }
        return clElementList;
    }

    private Map<UUID, ControlLoopElement> prepareClElementMap(List<ControlLoopElement> clElements) {
        Map<UUID, ControlLoopElement> clElementMap = new LinkedHashMap<>();
        for (var element : clElements) {
            clElementMap.put(element.getId(), element);
        }
        return clElementMap;
    }

    /**
     * Method to handle when the new state from participant is UNINITIALISED state.
     *
     * @param controlLoop participant response
     * @param orderedState orderedState
     * @param startPhaseMsg startPhase from message
     * @param clElementDefinitions the list of ControlLoopElementDefinition
     */
    private void handleUninitialisedState(final ControlLoop controlLoop, final ControlLoopOrderedState orderedState,
            Integer startPhaseMsg, List<ControlLoopElementDefinition> clElementDefinitions) {
        handleStateChange(controlLoop, orderedState, startPhaseMsg, clElementDefinitions);
        boolean isAllUninitialised = controlLoop.getElements().values().stream()
                .filter(element -> !ControlLoopState.UNINITIALISED.equals(element.getState())).findAny().isEmpty();
        if (isAllUninitialised) {
            controlLoopMap.remove(controlLoop.getDefinition());
            controlLoop.getElements().values().forEach(element -> elementsOnThisParticipant.remove(element.getId()));
        }
    }

    /**
     * Method to handle when the new state from participant is PASSIVE state.
     *
     * @param controlLoop participant response
     * @param orderedState orderedState
     * @param startPhaseMsg startPhase from message
     * @param clElementDefinitions the list of ControlLoopElementDefinition
     */
    private void handlePassiveState(final ControlLoop controlLoop, final ControlLoopOrderedState orderedState,
            Integer startPhaseMsg, List<ControlLoopElementDefinition> clElementDefinitions) {
        handleStateChange(controlLoop, orderedState, startPhaseMsg, clElementDefinitions);
    }

    /**
     * Method to handle when the new state from participant is RUNNING state.
     *
     * @param controlLoop participant response
     * @param orderedState orderedState
     * @param startPhaseMsg startPhase from message
     * @param clElementDefinitions the list of ControlLoopElementDefinition
     */
    private void handleRunningState(final ControlLoop controlLoop, final ControlLoopOrderedState orderedState,
            Integer startPhaseMsg, List<ControlLoopElementDefinition> clElementDefinitions) {
        handleStateChange(controlLoop, orderedState, startPhaseMsg, clElementDefinitions);
    }

    /**
     * Method to update the state of control loop elements.
     *
     * @param controlLoop participant status in memory
     * @param orderedState orderedState the new ordered state the participant should have
     * @param startPhaseMsg startPhase from message
     * @param clElementDefinitions the list of ControlLoopElementDefinition
     */
    private void handleStateChange(ControlLoop controlLoop, final ControlLoopOrderedState orderedState,
            Integer startPhaseMsg, List<ControlLoopElementDefinition> clElementDefinitions) {

        if (orderedState.equals(controlLoop.getOrderedState())) {
            var controlLoopAck = new ControlLoopAck(ParticipantMessageType.CONTROLLOOP_STATECHANGE_ACK);
            controlLoopAck.setParticipantId(participantId);
            controlLoopAck.setParticipantType(participantType);
            controlLoopAck.setMessage("Control loop is already in state " + orderedState);
            controlLoopAck.setResult(false);
            controlLoopAck.setControlLoopId(controlLoop.getDefinition());
            publisher.sendControlLoopAck(controlLoopAck);
            return;
        }

        controlLoop.getElements().values().stream().forEach(clElement -> controlLoopElementStateChange(controlLoop,
                orderedState, clElement, startPhaseMsg, clElementDefinitions));
    }

    private void controlLoopElementStateChange(ControlLoop controlLoop, ControlLoopOrderedState orderedState,
            ControlLoopElement clElement, Integer startPhaseMsg,
            List<ControlLoopElementDefinition> clElementDefinitions) {
        var clElementNodeTemplate = getClElementNodeTemplate(clElementDefinitions, clElement.getDefinition());
        if (clElementNodeTemplate != null) {
            int startPhase = ParticipantUtils.findStartPhase(clElementNodeTemplate.getProperties());
            if (startPhaseMsg.equals(startPhase)) {
                for (var clElementListener : listeners) {
                    try {
                        clElementListener.controlLoopElementStateChange(controlLoop.getDefinition(), clElement.getId(),
                                clElement.getState(), orderedState);
                    } catch (PfModelException e) {
                        LOGGER.debug("Control loop element update failed {}", controlLoop.getDefinition());
                    }
                }
            }
        }
    }

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

    /**
     * Get properties of a controlloopelement.
     *
     * @param id the control loop element id
     * @return the instance properties
     */
    public Map<String, ToscaProperty> getClElementInstanceProperties(UUID id) {
        Map<String, ToscaProperty> propertiesMap = new HashMap<>();
        for (var controlLoop : controlLoopMap.values()) {
            var element = controlLoop.getElements().get(id);
            if (element != null) {
                propertiesMap.putAll(element.getPropertiesMap());
            }
        }
        return propertiesMap;
    }
}