5e94d293e1241e19f1179de0f9130060ef56913e
[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.runtime.supervision;
22
23 import java.time.Instant;
24 import java.util.LinkedHashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.UUID;
28 import javax.ws.rs.core.Response;
29 import lombok.AllArgsConstructor;
30 import org.apache.commons.collections4.CollectionUtils;
31 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException;
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.ControlLoopElementDefinition;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant;
37 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider;
38 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantProvider;
39 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopStateChange;
40 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopUpdate;
41 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregister;
42 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregisterAck;
43 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessageType;
44 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegister;
45 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegisterAck;
46 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus;
47 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
48 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdateAck;
49 import org.onap.policy.clamp.controlloop.runtime.commissioning.CommissioningProvider;
50 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
51 import org.onap.policy.clamp.controlloop.runtime.monitoring.MonitoringProvider;
52 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantControlLoopStateChangePublisher;
53 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantControlLoopUpdatePublisher;
54 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantDeregisterAckPublisher;
55 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantRegisterAckPublisher;
56 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantStateChangePublisher;
57 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantStatusListener;
58 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantUpdatePublisher;
59 import org.onap.policy.common.endpoints.event.comm.TopicSink;
60 import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
61 import org.onap.policy.common.utils.services.ServiceManager;
62 import org.onap.policy.common.utils.services.ServiceManagerException;
63 import org.onap.policy.models.base.PfModelException;
64 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
67 import org.springframework.stereotype.Component;
68
69 /**
70  * This class handles supervision of control loop instances, so only one object of this type should be built at a time.
71  *
72  * <p/>
73  * It is effectively a singleton that is started at system start.
74  */
75 @Component
76 @AllArgsConstructor
77 public class SupervisionHandler {
78     private static final Logger LOGGER = LoggerFactory.getLogger(SupervisionHandler.class);
79
80     private static final String CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE = "Control loop can't transition from state ";
81     private static final String CONTROL_LOOP_IS_ALREADY_IN_STATE = "Control loop is already in state ";
82     private static final String TO_STATE = " to state ";
83     private static final String AND_TRANSITIONING_TO_STATE = " and transitioning to state ";
84
85     private final ControlLoopProvider controlLoopProvider;
86     private final ParticipantProvider participantProvider;
87     private final MonitoringProvider monitoringProvider;
88     private final CommissioningProvider commissioningProvider;
89
90     // Publishers for participant communication
91     private final ParticipantControlLoopUpdatePublisher controlLoopUpdatePublisher;
92     private final ParticipantControlLoopStateChangePublisher controlLoopStateChangePublisher;
93     private final ParticipantRegisterAckPublisher participantRegisterAckPublisher;
94     private final ParticipantDeregisterAckPublisher participantDeregisterAckPublisher;
95     private final ParticipantUpdatePublisher participantUpdatePublisher;
96
97     /**
98      * Supervision trigger called when a command is issued on control loops.
99      *
100      * <p/>
101      * Causes supervision to start or continue supervision on the control loops in question.
102      *
103      * @param controlLoopIdentifierList the control loops for which the supervision command has been issued
104      * @throws ControlLoopException on supervision triggering exceptions
105      */
106     public void triggerControlLoopSupervision(List<ToscaConceptIdentifier> controlLoopIdentifierList)
107             throws ControlLoopException {
108
109         LOGGER.debug("triggering control loop supervision on control loops {}", controlLoopIdentifierList);
110
111         if (CollectionUtils.isEmpty(controlLoopIdentifierList)) {
112             // This is just to force throwing of the exception in certain circumstances.
113             exceptionOccured(Response.Status.NOT_ACCEPTABLE, "The list of control loops for supervision is empty");
114         }
115
116         for (ToscaConceptIdentifier controlLoopId : controlLoopIdentifierList) {
117             try {
118                 var controlLoop = controlLoopProvider.getControlLoop(controlLoopId);
119
120                 superviseControlLoop(controlLoop);
121
122                 controlLoopProvider.updateControlLoop(controlLoop);
123             } catch (PfModelException pfme) {
124                 throw new ControlLoopException(pfme.getErrorResponse().getResponseCode(), pfme.getMessage(), pfme);
125             }
126         }
127     }
128
129     /**
130      * Handle a ParticipantStatus message from a participant.
131      *
132      * @param participantStatusMessage the ParticipantStatus message received from a participant
133      */
134     public void handleParticipantMessage(ParticipantStatus participantStatusMessage) {
135         LOGGER.debug("Participant Status received {}", participantStatusMessage);
136         try {
137             superviseParticipant(participantStatusMessage);
138         } catch (PfModelException | ControlLoopException svExc) {
139             LOGGER.warn("error supervising participant {}", participantStatusMessage.getParticipantId(), svExc);
140             return;
141         }
142
143         try {
144             superviseControlLoops(participantStatusMessage);
145         } catch (PfModelException | ControlLoopException svExc) {
146             LOGGER.warn("error supervising participant {}", participantStatusMessage.getParticipantId(), svExc);
147         }
148     }
149
150     /**
151      * Handle a ParticipantRegister message from a participant.
152      *
153      * @param participantRegisterMessage the ParticipantRegister message received from a participant
154      */
155     public void handleParticipantMessage(ParticipantRegister participantRegisterMessage) {
156         LOGGER.debug("Participant Register received {}", participantRegisterMessage);
157         sendParticipantAckMessage(participantRegisterMessage);
158         sendParticipantUpdate(participantRegisterMessage);
159     }
160
161     /**
162      * Handle a ParticipantDeregister message from a participant.
163      *
164      * @param participantDeregisterMessage the ParticipantDeregister message received from a participant
165      */
166     public void handleParticipantMessage(ParticipantDeregister participantDeregisterMessage) {
167         LOGGER.debug("Participant Deregister received {}", participantDeregisterMessage);
168         sendParticipantAckMessage(participantDeregisterMessage);
169     }
170
171     /**
172      * Handle a ParticipantUpdateAck message from a participant.
173      *
174      * @param participantUpdateAckMessage the ParticipantUpdateAck message received from a participant
175      */
176     public void handleParticipantMessage(ParticipantUpdateAck participantUpdateAckMessage) {
177         LOGGER.debug("Participant Update Ack received {}", participantUpdateAckMessage);
178     }
179
180     /**
181      * Supervise a control loop, performing whatever actions need to be performed on the control loop.
182      *
183      * @param controlLoop the control loop to supervises
184      * @throws PfModelException on accessing models in the database
185      * @throws ControlLoopException on supervision errors
186      */
187     private void superviseControlLoop(ControlLoop controlLoop) throws ControlLoopException, PfModelException {
188         switch (controlLoop.getOrderedState()) {
189             case UNINITIALISED:
190                 superviseControlLoopUninitialization(controlLoop);
191                 break;
192
193             case PASSIVE:
194                 superviseControlLoopPassivation(controlLoop);
195                 break;
196
197             case RUNNING:
198                 superviseControlLoopActivation(controlLoop);
199                 break;
200
201             default:
202                 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
203                         "A control loop cannot be commanded to go into state " + controlLoop.getOrderedState().name());
204         }
205     }
206
207     /**
208      * Supervise a control loop uninitialisation, performing whatever actions need to be performed on the control loop,
209      * control loop ordered state is UNINITIALIZED.
210      *
211      * @param controlLoop the control loop to supervises
212      * @throws ControlLoopException on supervision errors
213      */
214     private void superviseControlLoopUninitialization(ControlLoop controlLoop) throws ControlLoopException {
215         switch (controlLoop.getState()) {
216             case UNINITIALISED:
217                 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
218                         CONTROL_LOOP_IS_ALREADY_IN_STATE + controlLoop.getState().name());
219                 break;
220
221             case UNINITIALISED2PASSIVE:
222             case PASSIVE:
223                 controlLoop.setState(ControlLoopState.PASSIVE2UNINITIALISED);
224                 controlLoopStateChangePublisher.send(controlLoop);
225                 break;
226
227             case PASSIVE2UNINITIALISED:
228                 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_IS_ALREADY_IN_STATE
229                         + controlLoop.getState().name() + AND_TRANSITIONING_TO_STATE + controlLoop.getOrderedState());
230                 break;
231
232             default:
233                 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE
234                         + controlLoop.getState().name() + TO_STATE + controlLoop.getOrderedState());
235                 break;
236         }
237     }
238
239     private void superviseControlLoopPassivation(ControlLoop controlLoop)
240             throws ControlLoopException, PfModelException {
241         switch (controlLoop.getState()) {
242             case PASSIVE:
243                 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
244                         CONTROL_LOOP_IS_ALREADY_IN_STATE + controlLoop.getState().name());
245                 break;
246             case UNINITIALISED:
247                 controlLoop.setState(ControlLoopState.UNINITIALISED2PASSIVE);
248                 controlLoopUpdatePublisher.send(controlLoop);
249                 break;
250
251             case UNINITIALISED2PASSIVE:
252             case RUNNING2PASSIVE:
253                 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_IS_ALREADY_IN_STATE
254                         + controlLoop.getState().name() + AND_TRANSITIONING_TO_STATE + controlLoop.getOrderedState());
255                 break;
256
257             case RUNNING:
258                 controlLoop.setState(ControlLoopState.RUNNING2PASSIVE);
259                 controlLoopStateChangePublisher.send(controlLoop);
260                 break;
261
262             default:
263                 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE
264                         + controlLoop.getState().name() + TO_STATE + controlLoop.getOrderedState());
265                 break;
266         }
267     }
268
269     private void superviseControlLoopActivation(ControlLoop controlLoop) throws ControlLoopException {
270         switch (controlLoop.getState()) {
271             case RUNNING:
272                 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
273                         CONTROL_LOOP_IS_ALREADY_IN_STATE + controlLoop.getState().name());
274                 break;
275
276             case PASSIVE2RUNNING:
277                 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_IS_ALREADY_IN_STATE
278                         + controlLoop.getState().name() + AND_TRANSITIONING_TO_STATE + controlLoop.getOrderedState());
279                 break;
280
281             case PASSIVE:
282                 controlLoop.setState(ControlLoopState.PASSIVE2RUNNING);
283                 controlLoopStateChangePublisher.send(controlLoop);
284                 break;
285
286             default:
287                 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE
288                         + controlLoop.getState().name() + TO_STATE + controlLoop.getOrderedState());
289                 break;
290         }
291     }
292
293     private void sendControlLoopUpdate(ControlLoop controlLoop) throws PfModelException {
294         var pclu = new ParticipantControlLoopUpdate();
295         pclu.setControlLoopId(controlLoop.getKey().asIdentifier());
296         pclu.setControlLoop(controlLoop);
297         // TODO: We should look up the correct TOSCA node template here for the control loop
298         // Tiny hack implemented to return the tosca service template entry from the database and be passed onto dmaap
299         pclu.setControlLoopDefinition(commissioningProvider.getToscaServiceTemplate(null, null));
300         controlLoopUpdatePublisher.send(pclu);
301     }
302
303     private void sendControlLoopStateChange(ControlLoop controlLoop) {
304         var clsc = new ParticipantControlLoopStateChange();
305         clsc.setControlLoopId(controlLoop.getKey().asIdentifier());
306         clsc.setMessageId(UUID.randomUUID());
307         clsc.setOrderedState(controlLoop.getOrderedState());
308         controlLoopStateChangePublisher.send(clsc);
309     }
310
311     private void sendParticipantUpdate(ParticipantRegister participantRegisterMessage) {
312         var message = new ParticipantUpdate();
313         message.setParticipantId(participantRegisterMessage.getParticipantId());
314         message.setParticipantType(participantRegisterMessage.getParticipantType());
315         message.setTimestamp(Instant.now());
316
317         ControlLoopElementDefinition clDefinition = new ControlLoopElementDefinition();
318         clDefinition.setId(UUID.randomUUID());
319
320         try {
321             clDefinition.setControlLoopElementToscaServiceTemplate(commissioningProvider
322                     .getToscaServiceTemplate(null, null));
323         } catch (PfModelException pfme) {
324             LOGGER.warn("Get of tosca service template failed, cannot send participantupdate", pfme);
325             return;
326         }
327
328         Map<UUID, ControlLoopElementDefinition> controlLoopElementDefinitionMap = new LinkedHashMap<>();
329         controlLoopElementDefinitionMap.put(UUID.randomUUID(), clDefinition);
330
331         Map<ToscaConceptIdentifier, Map<UUID, ControlLoopElementDefinition>>
332             participantDefinitionUpdateMap = new LinkedHashMap<>();
333         participantDefinitionUpdateMap.put(participantRegisterMessage.getParticipantId(),
334                       controlLoopElementDefinitionMap);
335         message.setParticipantDefinitionUpdateMap(participantDefinitionUpdateMap);
336
337         LOGGER.debug("Participant Update sent", message);
338         participantUpdatePublisher.send(message);
339     }
340
341     private void sendParticipantAckMessage(ParticipantRegister participantRegisterMessage) {
342         var message = new ParticipantRegisterAck();
343         message.setResponseTo(participantRegisterMessage.getMessageId());
344         message.setMessage("Participant Register Ack");
345         message.setResult(true);
346         participantRegisterAckPublisher.send(message);
347     }
348
349     private void sendParticipantAckMessage(ParticipantDeregister participantDeregisterMessage) {
350         var message = new ParticipantDeregisterAck();
351         message.setResponseTo(participantDeregisterMessage.getMessageId());
352         message.setMessage("Participant Deregister Ack");
353         message.setResult(true);
354         participantDeregisterAckPublisher.send(message);
355     }
356
357     private void superviseParticipant(ParticipantStatus participantStatusMessage)
358             throws PfModelException, ControlLoopException {
359         if (participantStatusMessage.getParticipantId() == null) {
360             exceptionOccured(Response.Status.NOT_FOUND, "Participant ID on PARTICIPANT_STATUS message is null");
361         }
362
363         List<Participant> participantList =
364                 participantProvider.getParticipants(participantStatusMessage.getParticipantId().getName(),
365                         participantStatusMessage.getParticipantId().getVersion());
366
367         if (CollectionUtils.isEmpty(participantList)) {
368             var participant = new Participant();
369             participant.setName(participantStatusMessage.getParticipantId().getName());
370             participant.setVersion(participantStatusMessage.getParticipantId().getVersion());
371             participant.setDefinition(new ToscaConceptIdentifier("unknown", "0.0.0"));
372             participant.setParticipantState(participantStatusMessage.getState());
373             participant.setHealthStatus(participantStatusMessage.getHealthStatus());
374
375             participantList.add(participant);
376             participantProvider.createParticipants(participantList);
377         } else {
378             for (Participant participant : participantList) {
379                 participant.setParticipantState(participantStatusMessage.getState());
380                 participant.setHealthStatus(participantStatusMessage.getHealthStatus());
381             }
382             participantProvider.updateParticipants(participantList);
383         }
384
385         monitoringProvider.createParticipantStatistics(List.of(participantStatusMessage.getParticipantStatistics()));
386     }
387
388     private void superviseControlLoops(ParticipantStatus participantStatusMessage)
389             throws PfModelException, ControlLoopException {
390         if (CollectionUtils.isEmpty(participantStatusMessage.getControlLoops().getControlLoopList())) {
391             return;
392         }
393
394         for (ControlLoop controlLoop : participantStatusMessage.getControlLoops().getControlLoopList()) {
395             if (controlLoop == null) {
396                 exceptionOccured(Response.Status.NOT_FOUND,
397                         "PARTICIPANT_STATUS message references unknown control loop: " + controlLoop);
398             }
399
400             var dbControlLoop = controlLoopProvider
401                     .getControlLoop(new ToscaConceptIdentifier(controlLoop.getName(), controlLoop.getVersion()));
402             if (dbControlLoop == null) {
403                 exceptionOccured(Response.Status.NOT_FOUND,
404                         "PARTICIPANT_STATUS control loop not found in database: " + controlLoop);
405             }
406
407             for (ControlLoopElement element : controlLoop.getElements().values()) {
408                 ControlLoopElement dbElement = dbControlLoop.getElements().get(element.getId());
409
410                 if (dbElement == null) {
411                     exceptionOccured(Response.Status.NOT_FOUND,
412                             "PARTICIPANT_STATUS message references unknown control loop element: " + element);
413                 }
414
415                 // Replace element entry in the database
416                 dbControlLoop.getElements().put(element.getId(), element);
417             }
418             controlLoopProvider.updateControlLoop(dbControlLoop);
419         }
420
421         for (ControlLoop controlLoop : participantStatusMessage.getControlLoops().getControlLoopList()) {
422             monitoringProvider.createClElementStatistics(controlLoop.getControlLoopElementStatisticsList(controlLoop));
423         }
424     }
425
426     private void exceptionOccured(Response.Status status, String reason) throws ControlLoopException {
427         throw new ControlLoopException(status, reason);
428     }
429 }