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
455
456
457
|
/*-
* ============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.runtime.supervision;
import java.util.List;
import java.util.UUID;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.apache.commons.collections4.CollectionUtils;
import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException;
import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
import org.onap.policy.clamp.controlloop.common.handler.ControlLoopHandler;
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.ControlLoopState;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant;
import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider;
import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantProvider;
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.ParticipantMessageType;
import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus;
import org.onap.policy.clamp.controlloop.runtime.commissioning.CommissioningHandler;
import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
import org.onap.policy.clamp.controlloop.runtime.monitoring.MonitoringHandler;
import org.onap.policy.clamp.controlloop.runtime.monitoring.MonitoringProvider;
import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantControlLoopStateChangePublisher;
import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantControlLoopUpdatePublisher;
import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantStateChangePublisher;
import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantStatusListener;
import org.onap.policy.common.endpoints.event.comm.TopicSink;
import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
import org.onap.policy.common.utils.services.Registry;
import org.onap.policy.common.utils.services.ServiceManager;
import org.onap.policy.common.utils.services.ServiceManagerException;
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 handles supervision of control loop instances, so only one object of this type should be built at a time.
*
* <p/> It is effectively a singleton that is started at system start.
*/
public class SupervisionHandler extends ControlLoopHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(SupervisionHandler.class);
private static final String CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE = "Control loop can't transition from state ";
private static final String CONTROL_LOOP_IS_ALREADY_IN_STATE = "Control loop is already in state ";
private static final String TO_STATE = " to state ";
private static final String AND_TRANSITIONING_TO_STATE = " and transitioning to state ";
private ControlLoopProvider controlLoopProvider;
private ParticipantProvider participantProvider;
private MonitoringProvider monitoringProvider;
// Publishers for participant communication
private ParticipantStateChangePublisher stateChangePublisher;
private ParticipantControlLoopUpdatePublisher controlLoopUpdatePublisher;
private ParticipantControlLoopStateChangePublisher controlLoopStateChangePublisher;
private long supervisionScannerIntervalSec;
private long participantStateChangeIntervalSec;
private long participantClUpdateIntervalSec;
private long participantClStateChangeIntervalSec;
// Database scanner
private SupervisionScanner scanner;
/**
* Used to manage the services.
*/
private ServiceManager manager;
private ServiceManager publisherManager;
/**
* Gets the SupervisionHandler.
*
* @return SupervisionHandler
*/
public static SupervisionHandler getInstance() {
return Registry.get(SupervisionHandler.class.getName());
}
/**
* Create a handler.
*
* @param clRuntimeParameterGroup the parameters for the control loop runtime
*/
public SupervisionHandler(ClRuntimeParameterGroup clRuntimeParameterGroup) {
super(clRuntimeParameterGroup.getDatabaseProviderParameters());
// @formatter:off
this.manager = new ServiceManager()
.addAction("ControlLoop Provider",
() -> controlLoopProvider = new ControlLoopProvider(getDatabaseProviderParameters()),
() -> controlLoopProvider = null)
.addAction("Participant Provider",
() -> participantProvider = new ParticipantProvider(getDatabaseProviderParameters()),
() -> participantProvider = null);
// @formatter:on
supervisionScannerIntervalSec = clRuntimeParameterGroup.getSupervisionScannerIntervalSec();
participantStateChangeIntervalSec = clRuntimeParameterGroup.getParticipantClStateChangeIntervalSec();
participantClUpdateIntervalSec = clRuntimeParameterGroup.getParticipantClUpdateIntervalSec();
participantClStateChangeIntervalSec = clRuntimeParameterGroup.getParticipantClStateChangeIntervalSec();
}
/**
* Supervision trigger called when a command is issued on control loops.
*
* </p> Causes supervision to start or continue supervision on the control loops in question.
*
* @param controlLoopIdentifierList the control loops for which the supervision command has been issued
* @throws ControlLoopException on supervision triggering exceptions
*/
public void triggerControlLoopSupervision(List<ToscaConceptIdentifier> controlLoopIdentifierList)
throws ControlLoopException {
LOGGER.debug("triggering control loop supervision on control loops {}", controlLoopIdentifierList);
if (CollectionUtils.isEmpty(controlLoopIdentifierList)) {
// This is just to force throwing of the exception in certain circumstances.
exceptionOccured(Response.Status.NOT_ACCEPTABLE, "The list of control loops for supervision is empty");
}
for (ToscaConceptIdentifier controlLoopId : controlLoopIdentifierList) {
try {
var controlLoop = controlLoopProvider.getControlLoop(controlLoopId);
superviseControlLoop(controlLoop);
controlLoopProvider.updateControlLoop(controlLoop);
} catch (PfModelException pfme) {
throw new ControlLoopException(pfme.getErrorResponse().getResponseCode(), pfme.getMessage(), pfme);
}
}
}
@Override
public void startAndRegisterListeners(MessageTypeDispatcher msgDispatcher) {
msgDispatcher.register(ParticipantMessageType.PARTICIPANT_STATUS.name(), new ParticipantStatusListener());
}
@Override
public void startAndRegisterPublishers(List<TopicSink> topicSinks) {
// @formatter:off
this.publisherManager = new ServiceManager()
.addAction("Supervision scanner",
() -> scanner =
new SupervisionScanner(controlLoopProvider, supervisionScannerIntervalSec),
() -> scanner.close())
.addAction("ControlLoopUpdate publisher",
() -> controlLoopUpdatePublisher =
new ParticipantControlLoopUpdatePublisher(topicSinks, participantClUpdateIntervalSec),
() -> controlLoopUpdatePublisher.terminate())
.addAction("StateChange Publisher",
() -> stateChangePublisher =
new ParticipantStateChangePublisher(topicSinks, participantStateChangeIntervalSec),
() -> stateChangePublisher.terminate())
.addAction("ControlLoopStateChange Publisher",
() -> controlLoopStateChangePublisher =
new ParticipantControlLoopStateChangePublisher(topicSinks, participantClStateChangeIntervalSec),
() -> controlLoopStateChangePublisher.terminate());
// @formatter:on
try {
publisherManager.start();
} catch (final ServiceManagerException exp) {
throw new ControlLoopRuntimeException(Status.INTERNAL_SERVER_ERROR,
"Supervision handler start of publishers or scanner failed", exp);
}
}
@Override
public void stopAndUnregisterPublishers() {
try {
publisherManager.stop();
} catch (final ServiceManagerException exp) {
throw new ControlLoopRuntimeException(Status.INTERNAL_SERVER_ERROR,
"Supervision handler stop of publishers or scanner failed", exp);
}
}
@Override
public void stopAndUnregisterListeners(MessageTypeDispatcher msgDispatcher) {
msgDispatcher.unregister(ParticipantMessageType.PARTICIPANT_STATUS.name());
}
/**
* Handle a ParticipantStatus message from a participant.
*
* @param participantStatusMessage the ParticipantStatus message received from a participant
*/
public void handleParticipantStatusMessage(ParticipantStatus participantStatusMessage) {
LOGGER.debug("Participant Status received {}", participantStatusMessage);
try {
superviseParticipant(participantStatusMessage);
} catch (PfModelException | ControlLoopException svExc) {
LOGGER.warn("error supervising participant {}", participantStatusMessage.getParticipantId(), svExc);
return;
}
try {
superviseControlLoops(participantStatusMessage);
} catch (PfModelException | ControlLoopException svExc) {
LOGGER.warn("error supervising participant {}", participantStatusMessage.getParticipantId(), svExc);
}
}
/**
* Supervise a control loop, performing whatever actions need to be performed on the control loop.
*
* @param controlLoop the control loop to supervises
* @throws PfModelException on accessing models in the database
* @throws ControlLoopException on supervision errors
*/
private void superviseControlLoop(ControlLoop controlLoop) throws ControlLoopException, PfModelException {
switch (controlLoop.getOrderedState()) {
case UNINITIALISED:
superviseControlLoopUninitialization(controlLoop);
break;
case PASSIVE:
superviseControlLoopPassivation(controlLoop);
break;
case RUNNING:
superviseControlLoopActivation(controlLoop);
break;
default:
exceptionOccured(Response.Status.NOT_ACCEPTABLE,
"A control loop cannot be commanded to go into state " + controlLoop.getOrderedState().name());
}
}
/**
* Supervise a control loop uninitialisation, performing whatever actions need to be performed on the control loop,
* control loop ordered state is UNINITIALIZED.
*
* @param controlLoop the control loop to supervises
* @throws ControlLoopException on supervision errors
*/
private void superviseControlLoopUninitialization(ControlLoop controlLoop) throws ControlLoopException {
switch (controlLoop.getState()) {
case UNINITIALISED:
exceptionOccured(Response.Status.NOT_ACCEPTABLE,
CONTROL_LOOP_IS_ALREADY_IN_STATE + controlLoop.getState().name());
break;
case UNINITIALISED2PASSIVE:
case PASSIVE:
controlLoop.setState(ControlLoopState.PASSIVE2UNINITIALISED);
sendControlLoopStateChange(controlLoop);
break;
case PASSIVE2UNINITIALISED:
exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_IS_ALREADY_IN_STATE
+ controlLoop.getState().name() + AND_TRANSITIONING_TO_STATE + controlLoop.getOrderedState());
break;
default:
exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE
+ controlLoop.getState().name() + TO_STATE + controlLoop.getOrderedState());
break;
}
}
private void superviseControlLoopPassivation(ControlLoop controlLoop)
throws ControlLoopException, PfModelException {
switch (controlLoop.getState()) {
case PASSIVE:
exceptionOccured(Response.Status.NOT_ACCEPTABLE,
CONTROL_LOOP_IS_ALREADY_IN_STATE + controlLoop.getState().name());
break;
case UNINITIALISED:
controlLoop.setState(ControlLoopState.UNINITIALISED2PASSIVE);
sendControlLoopUpdate(controlLoop);
break;
case UNINITIALISED2PASSIVE:
case RUNNING2PASSIVE:
exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_IS_ALREADY_IN_STATE
+ controlLoop.getState().name() + AND_TRANSITIONING_TO_STATE + controlLoop.getOrderedState());
break;
case RUNNING:
controlLoop.setState(ControlLoopState.RUNNING2PASSIVE);
sendControlLoopStateChange(controlLoop);
break;
default:
exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE
+ controlLoop.getState().name() + TO_STATE + controlLoop.getOrderedState());
break;
}
}
private void superviseControlLoopActivation(ControlLoop controlLoop) throws ControlLoopException {
switch (controlLoop.getState()) {
case RUNNING:
exceptionOccured(Response.Status.NOT_ACCEPTABLE,
CONTROL_LOOP_IS_ALREADY_IN_STATE + controlLoop.getState().name());
break;
case PASSIVE2RUNNING:
exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_IS_ALREADY_IN_STATE
+ controlLoop.getState().name() + AND_TRANSITIONING_TO_STATE + controlLoop.getOrderedState());
break;
case PASSIVE:
controlLoop.setState(ControlLoopState.PASSIVE2RUNNING);
sendControlLoopStateChange(controlLoop);
break;
default:
exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE
+ controlLoop.getState().name() + TO_STATE + controlLoop.getOrderedState());
break;
}
}
private void sendControlLoopUpdate(ControlLoop controlLoop) throws PfModelException {
var pclu = new ParticipantControlLoopUpdate();
pclu.setControlLoopId(controlLoop.getKey().asIdentifier());
pclu.setControlLoop(controlLoop);
// TODO: We should look up the correct TOSCA node template here for the control loop
// Tiny hack implemented to return the tosca service template entry from the database and be passed onto dmaap
var commissioningProvider = CommissioningHandler.getInstance().getProvider();
pclu.setControlLoopDefinition(commissioningProvider.getToscaServiceTemplate(null, null));
controlLoopUpdatePublisher.send(pclu);
}
private void sendControlLoopStateChange(ControlLoop controlLoop) {
var clsc = new ParticipantControlLoopStateChange();
clsc.setControlLoopId(controlLoop.getKey().asIdentifier());
clsc.setMessageId(UUID.randomUUID());
clsc.setOrderedState(controlLoop.getOrderedState());
controlLoopStateChangePublisher.send(clsc);
}
private void superviseParticipant(ParticipantStatus participantStatusMessage)
throws PfModelException, ControlLoopException {
if (participantStatusMessage.getParticipantId() == null) {
exceptionOccured(Response.Status.NOT_FOUND, "Participant ID on PARTICIPANT_STATUS message is null");
}
List<Participant> participantList =
participantProvider.getParticipants(participantStatusMessage.getParticipantId().getName(),
participantStatusMessage.getParticipantId().getVersion());
if (CollectionUtils.isEmpty(participantList)) {
var participant = new Participant();
participant.setName(participantStatusMessage.getParticipantId().getName());
participant.setVersion(participantStatusMessage.getParticipantId().getVersion());
participant.setDefinition(new ToscaConceptIdentifier("unknown", "0.0.0"));
participant.setParticipantState(participantStatusMessage.getState());
participant.setHealthStatus(participantStatusMessage.getHealthStatus());
participantList.add(participant);
participantProvider.createParticipants(participantList);
} else {
for (Participant participant : participantList) {
participant.setParticipantState(participantStatusMessage.getState());
participant.setHealthStatus(participantStatusMessage.getHealthStatus());
}
participantProvider.updateParticipants(participantList);
}
monitoringProvider = MonitoringHandler.getInstance().getMonitoringProvider();
monitoringProvider.createParticipantStatistics(List.of(participantStatusMessage.getParticipantStatistics()));
}
private void superviseControlLoops(ParticipantStatus participantStatusMessage)
throws PfModelException, ControlLoopException {
if (CollectionUtils.isEmpty(participantStatusMessage.getControlLoops().getControlLoopList())) {
return;
}
for (ControlLoop controlLoop : participantStatusMessage.getControlLoops().getControlLoopList()) {
if (controlLoop == null) {
exceptionOccured(Response.Status.NOT_FOUND,
"PARTICIPANT_STATUS message references unknown control loop: " + controlLoop);
}
var dbControlLoop = controlLoopProvider
.getControlLoop(new ToscaConceptIdentifier(controlLoop.getName(), controlLoop.getVersion()));
if (dbControlLoop == null) {
exceptionOccured(Response.Status.NOT_FOUND,
"PARTICIPANT_STATUS control loop not found in database: " + controlLoop);
}
for (ControlLoopElement element : controlLoop.getElements().values()) {
ControlLoopElement dbElement = dbControlLoop.getElements().get(element.getId());
if (dbElement == null) {
exceptionOccured(Response.Status.NOT_FOUND,
"PARTICIPANT_STATUS message references unknown control loop element: " + element);
}
// Replace element entry in the database
dbControlLoop.getElements().put(element.getId(), element);
}
controlLoopProvider.updateControlLoop(dbControlLoop);
}
monitoringProvider = MonitoringHandler.getInstance().getMonitoringProvider();
for (ControlLoop controlLoop : participantStatusMessage.getControlLoops().getControlLoopList()) {
monitoringProvider.createClElementStatistics(controlLoop.getControlLoopElementStatisticsList(controlLoop));
}
}
@Override
public void startProviders() {
try {
manager.start();
} catch (final ServiceManagerException exp) {
throw new ControlLoopRuntimeException(Status.INTERNAL_SERVER_ERROR,
"Supervision handler start of providers failed", exp);
}
}
@Override
public void stopProviders() {
try {
manager.stop();
} catch (final ServiceManagerException exp) {
throw new ControlLoopRuntimeException(Status.INTERNAL_SERVER_ERROR,
"Supervision handler stop of providers failed", exp);
}
}
private void exceptionOccured(Response.Status status, String reason) throws ControlLoopException {
throw new ControlLoopException(status, reason);
}
}
|