2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.controlloop.eventmanager;
23 import static org.onap.policy.controlloop.ControlLoopTargetType.PNF;
24 import static org.onap.policy.controlloop.ControlLoopTargetType.VM;
25 import static org.onap.policy.controlloop.ControlLoopTargetType.VNF;
27 import java.io.Serializable;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.LinkedList;
33 import java.util.UUID;
34 import java.util.concurrent.CompletableFuture;
35 import java.util.concurrent.Executor;
36 import java.util.concurrent.ExecutorService;
37 import java.util.concurrent.ForkJoinPool;
38 import java.util.concurrent.TimeUnit;
39 import java.util.concurrent.atomic.AtomicReference;
40 import java.util.function.Consumer;
41 import java.util.stream.Collectors;
42 import java.util.stream.Stream;
44 import lombok.ToString;
45 import org.apache.commons.lang3.StringUtils;
46 import org.drools.core.WorkingMemory;
47 import org.kie.api.runtime.rule.FactHandle;
48 import org.onap.policy.controlloop.ControlLoopEventStatus;
49 import org.onap.policy.controlloop.ControlLoopException;
50 import org.onap.policy.controlloop.ControlLoopNotificationType;
51 import org.onap.policy.controlloop.ControlLoopOperation;
52 import org.onap.policy.controlloop.VirtualControlLoopEvent;
53 import org.onap.policy.controlloop.VirtualControlLoopNotification;
54 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
55 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
56 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
57 import org.onap.policy.controlloop.drl.legacy.ControlLoopParams;
58 import org.onap.policy.controlloop.ophistory.OperationHistoryDataManager;
59 import org.onap.policy.controlloop.policy.FinalResult;
60 import org.onap.policy.controlloop.policy.Policy;
61 import org.onap.policy.controlloop.processor.ControlLoopProcessor;
62 import org.onap.policy.drools.core.lock.LockCallback;
63 import org.onap.policy.drools.system.PolicyEngineConstants;
64 import org.slf4j.Logger;
65 import org.slf4j.LoggerFactory;
68 * Manager for a single control loop event. Once this has been created, the event can be
69 * retracted from working memory. Once this has been created, {@link #start()} should be
70 * invoked, and then {@link #nextStep()} should be invoked continually until
71 * {@link #isActive()} returns {@code false}, indicating that all steps have completed.
73 @ToString(onlyExplicitlyIncluded = true)
74 public class ControlLoopEventManager2 implements ManagerContext, Serializable {
75 private static final Logger logger = LoggerFactory.getLogger(ControlLoopEventManager2.class);
76 private static final long serialVersionUID = -1216568161322872641L;
78 private static final String EVENT_MANAGER_SERVICE_CONFIG = "config/event-manager.properties";
79 public static final String PROV_STATUS_ACTIVE = "ACTIVE";
80 private static final String VM_NAME = "VM_NAME";
81 private static final String VNF_NAME = "VNF_NAME";
82 public static final String GENERIC_VNF_VNF_ID = "generic-vnf.vnf-id";
83 public static final String GENERIC_VNF_VNF_NAME = "generic-vnf.vnf-name";
84 public static final String VSERVER_VSERVER_NAME = "vserver.vserver-name";
85 public static final String GENERIC_VNF_IS_CLOSED_LOOP_DISABLED = "generic-vnf.is-closed-loop-disabled";
86 public static final String VSERVER_IS_CLOSED_LOOP_DISABLED = "vserver.is-closed-loop-disabled";
87 public static final String PNF_IS_IN_MAINT = "pnf.in-maint";
88 public static final String GENERIC_VNF_PROV_STATUS = "generic-vnf.prov-status";
89 public static final String VSERVER_PROV_STATUS = "vserver.prov-status";
90 public static final String PNF_ID = "pnf.pnf-id";
91 public static final String PNF_NAME = "pnf.pnf-name";
93 private static final Set<String> VALID_TARGETS = Stream
94 .of(VM_NAME, VNF_NAME, VSERVER_VSERVER_NAME, GENERIC_VNF_VNF_ID, GENERIC_VNF_VNF_NAME, PNF_NAME)
95 .map(String::toLowerCase).collect(Collectors.toSet());
97 private static final Set<String> TRUE_VALUES = Set.of("true", "t", "yes", "y");
99 public enum NewEventStatus {
100 FIRST_ONSET, SUBSEQUENT_ONSET, FIRST_ABATEMENT, SUBSEQUENT_ABATEMENT, SYNTAX_ERROR
103 // TODO limit the number of policies that may be executed for a single event?
106 * {@code True} if this object was created by this JVM instance, {@code false}
107 * otherwise. This will be {@code false} if this object is reconstituted from a
108 * persistent store or by transfer from another server.
110 private transient boolean createdByThisJvmInstance;
114 public final String closedLoopControlName;
117 private final UUID requestId;
118 private final ControlLoopEventContext context;
120 private int numOnsets = 1;
122 private int numAbatements = 0;
123 private VirtualControlLoopEvent abatement = null;
126 * Time, in milliseconds, when the control loop will time out.
129 private final long endTimeMs;
131 // fields extracted from the ControlLoopParams
133 private final String policyName;
134 private final String policyScope;
135 private final String policyVersion;
137 private final LinkedList<ControlLoopOperation> controlLoopHistory = new LinkedList<>();
140 * Maps a target entity to its lock.
142 private final transient Map<String, LockData> target2lock = new HashMap<>();
144 private final ControlLoopProcessor processor;
145 private final AtomicReference<ControlLoopOperationManager2> currentOperation = new AtomicReference<>();
147 private FinalResult finalResult = null;
150 private VirtualControlLoopNotification notification;
153 private boolean updated = false;
155 private final transient WorkingMemory workMem;
156 private transient FactHandle factHandle;
160 * Constructs the object.
162 * @param params control loop parameters
163 * @param event event to be managed by this object
164 * @param workMem working memory to update if this changes
165 * @throws ControlLoopException if the event is invalid or if a YAML processor cannot
168 public ControlLoopEventManager2(ControlLoopParams params, VirtualControlLoopEvent event, WorkingMemory workMem)
169 throws ControlLoopException {
171 checkEventSyntax(event);
173 if (isClosedLoopDisabled(event)) {
174 throw new IllegalStateException("is-closed-loop-disabled is set to true on VServer or VNF");
177 if (isProvStatusInactive(event)) {
178 throw new IllegalStateException("prov-status is not ACTIVE on VServer or VNF");
181 this.createdByThisJvmInstance = true;
182 this.closedLoopControlName = params.getClosedLoopControlName();
183 this.requestId = event.getRequestId();
184 this.context = new ControlLoopEventContext(event);
185 this.policyName = params.getPolicyName();
186 this.policyScope = params.getPolicyScope();
187 this.policyVersion = params.getPolicyVersion();
188 this.processor = new ControlLoopProcessor(params.getToscaPolicy());
189 this.workMem = workMem;
190 this.endTimeMs = System.currentTimeMillis() + detmControlLoopTimeoutMs();
194 * Starts the manager.
196 * @throws ControlLoopException if the processor cannot get a policy
198 public void start() throws ControlLoopException {
200 throw new IllegalStateException("manager is no longer active");
203 if ((factHandle = workMem.getFactHandle(this)) == null) {
204 throw new IllegalStateException("manager is not in working memory");
207 if (currentOperation.get() != null) {
208 throw new IllegalStateException("manager already started");
215 * Starts an operation for the current processor policy.
217 * @throws ControlLoopException if the processor cannot get a policy
219 private synchronized void startOperation() throws ControlLoopException {
221 if ((finalResult = processor.checkIsCurrentPolicyFinal()) == null) {
222 // not final - start the next operation
223 currentOperation.set(makeOperationManager(context, processor.getCurrentPolicy()));
224 currentOperation.get().start(endTimeMs - System.currentTimeMillis());
228 logger.info("final={} oper state={} for {}", finalResult, currentOperation.get().getState(), requestId);
230 notification = makeNotification();
231 notification.setHistory(controlLoopHistory);
233 switch (finalResult) {
234 case FINAL_FAILURE_EXCEPTION:
235 notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
236 notification.setMessage("Exception in processing closed loop");
239 notification.setNotification(ControlLoopNotificationType.FINAL_SUCCESS);
242 notification.setNotification(ControlLoopNotificationType.FINAL_OPENLOOP);
246 notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
252 * Starts the next step, whatever that may be.
254 public void nextStep() {
262 if (!currentOperation.get().nextStep()) {
263 // current operation is done - try the next
264 controlLoopHistory.addAll(currentOperation.get().getHistory());
265 processor.nextPolicyForResult(currentOperation.get().getOperationResult());
269 } catch (ControlLoopException | RuntimeException e) {
270 // processor problem - this is fatal
271 logger.warn("{}: cannot start next step for {}", closedLoopControlName, requestId, e);
272 finalResult = FinalResult.FINAL_FAILURE_EXCEPTION;
273 notification = makeNotification();
274 notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
275 notification.setMessage("Policy processing aborted due to policy error");
276 notification.setHistory(controlLoopHistory);
281 * Determines if the manager is still active.
283 * @return {@code true} if the manager is still active, {@code false} otherwise
285 public boolean isActive() {
286 return (createdByThisJvmInstance && finalResult == null);
290 * Updates working memory if this changes.
292 * @param operation operation manager that was updated
295 public synchronized void updated(ControlLoopOperationManager2 operation) {
296 if (!isActive() || operation != currentOperation.get()) {
297 // no longer working on the given operation
301 notification = makeNotification();
303 VirtualControlLoopEvent event = context.getEvent();
305 switch (operation.getState()) {
307 notification.setNotification(ControlLoopNotificationType.REJECTED);
308 notification.setMessage("The target " + event.getAai().get(event.getTarget()) + " is already locked");
311 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
312 notification.setMessage("The target " + event.getAai().get(event.getTarget()) + " is no longer locked");
315 notification.setNotification(ControlLoopNotificationType.OPERATION);
316 notification.setMessage(
317 "Sending guard query for " + operation.getActor() + " " + operation.getOperation());
319 case GUARD_PERMITTED:
320 notification.setNotification(ControlLoopNotificationType.OPERATION);
321 notification.setMessage("Guard result for " + operation.getActor() + " " + operation.getOperation()
325 notification.setNotification(ControlLoopNotificationType.OPERATION);
326 notification.setMessage("Guard result for " + operation.getActor() + " " + operation.getOperation()
329 case OPERATION_STARTED:
330 notification.setNotification(ControlLoopNotificationType.OPERATION);
331 notification.setMessage(operation.getOperationMessage());
332 notification.setHistory(Collections.emptyList());
334 case OPERATION_SUCCESS:
335 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
338 case CONTROL_LOOP_TIMEOUT:
339 logger.warn("{}: control loop timed out for {}", closedLoopControlName, requestId);
340 controlLoopHistory.addAll(currentOperation.get().getHistory());
341 notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
342 notification.setMessage("Control Loop timed out");
343 notification.setHistory(controlLoopHistory);
344 finalResult = FinalResult.FINAL_FAILURE;
347 case OPERATION_FAILURE:
349 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
354 workMem.update(factHandle, this);
358 * Cancels the current operation and frees all locks.
360 public void destroy() {
361 ControlLoopOperationManager2 oper = currentOperation.get();
366 getBlockingExecutor().execute(this::freeAllLocks);
372 private void freeAllLocks() {
373 target2lock.values().forEach(LockData::free);
377 * Makes a notification message for the current operation.
379 * @return a new notification
381 public VirtualControlLoopNotification makeNotification() {
382 VirtualControlLoopNotification notif = new VirtualControlLoopNotification(context.getEvent());
383 notif.setNotification(ControlLoopNotificationType.OPERATION);
384 notif.setFrom("policy");
385 notif.setPolicyScope(policyScope);
386 notif.setPolicyVersion(policyVersion);
388 if (finalResult == null) {
389 ControlLoopOperationManager2 oper = currentOperation.get();
391 notif.setMessage(oper.getOperationHistory());
392 notif.setHistory(oper.getHistory());
400 * An event onset/abatement.
402 * @param event the event
405 public NewEventStatus onNewEvent(VirtualControlLoopEvent event) {
407 checkEventSyntax(event);
409 if (event.getClosedLoopEventStatus() == ControlLoopEventStatus.ONSET) {
410 if (event.equals(context.getEvent())) {
411 return NewEventStatus.FIRST_ONSET;
415 return NewEventStatus.SUBSEQUENT_ONSET;
418 if (abatement == null) {
421 return NewEventStatus.FIRST_ABATEMENT;
424 return NewEventStatus.SUBSEQUENT_ABATEMENT;
427 } catch (ControlLoopException e) {
428 logger.error("{}: onNewEvent threw an exception", this, e);
429 return NewEventStatus.SYNTAX_ERROR;
434 * Determines the overall control loop timeout.
436 * @return the policy timeout, in milliseconds, if specified, a default timeout
439 private long detmControlLoopTimeoutMs() {
440 // validation checks preclude null or 0 timeout values in the policy
441 Integer timeout = processor.getControlLoop().getTimeout();
442 return TimeUnit.MILLISECONDS.convert(timeout, TimeUnit.SECONDS);
446 * Check an event syntax.
448 * @param event the event syntax
449 * @throws ControlLoopException if an error occurs
451 public void checkEventSyntax(VirtualControlLoopEvent event) throws ControlLoopException {
452 validateStatus(event);
453 if (StringUtils.isBlank(event.getClosedLoopControlName())) {
454 throw new ControlLoopException("No control loop name");
456 if (event.getRequestId() == null) {
457 throw new ControlLoopException("No request ID");
459 if (event.getClosedLoopEventStatus() == ControlLoopEventStatus.ABATED) {
462 if (StringUtils.isBlank(event.getTarget())) {
463 throw new ControlLoopException("No target field");
464 } else if (!VALID_TARGETS.contains(event.getTarget().toLowerCase())) {
465 throw new ControlLoopException("target field invalid");
467 validateAaiData(event);
470 private void validateStatus(VirtualControlLoopEvent event) throws ControlLoopException {
471 if (event.getClosedLoopEventStatus() != ControlLoopEventStatus.ONSET
472 && event.getClosedLoopEventStatus() != ControlLoopEventStatus.ABATED) {
473 throw new ControlLoopException("Invalid value in closedLoopEventStatus");
477 private void validateAaiData(VirtualControlLoopEvent event) throws ControlLoopException {
478 Map<String, String> eventAai = event.getAai();
479 if (eventAai == null) {
480 throw new ControlLoopException("AAI is null");
482 if (event.getTargetType() == null) {
483 throw new ControlLoopException("The Target type is null");
485 switch (event.getTargetType()) {
488 validateAaiVmVnfData(eventAai);
491 validateAaiPnfData(eventAai);
494 throw new ControlLoopException("The target type is not supported");
498 private void validateAaiVmVnfData(Map<String, String> eventAai) throws ControlLoopException {
499 if (eventAai.get(GENERIC_VNF_VNF_ID) == null && eventAai.get(VSERVER_VSERVER_NAME) == null
500 && eventAai.get(GENERIC_VNF_VNF_NAME) == null) {
501 throw new ControlLoopException(
502 "generic-vnf.vnf-id or generic-vnf.vnf-name or vserver.vserver-name information missing");
506 private void validateAaiPnfData(Map<String, String> eventAai) throws ControlLoopException {
507 if (eventAai.get(PNF_NAME) == null) {
508 throw new ControlLoopException("AAI PNF object key pnf-name is missing");
513 * Is closed loop disabled for an event.
515 * @param event the event
516 * @return <code>true</code> if the control loop is disabled, <code>false</code>
519 public static boolean isClosedLoopDisabled(VirtualControlLoopEvent event) {
520 Map<String, String> aai = event.getAai();
521 return (isAaiTrue(aai.get(VSERVER_IS_CLOSED_LOOP_DISABLED))
522 || isAaiTrue(aai.get(GENERIC_VNF_IS_CLOSED_LOOP_DISABLED))
523 || isAaiTrue(aai.get(PNF_IS_IN_MAINT)));
527 * Does provisioning status, for an event, have a value other than ACTIVE.
529 * @param event the event
530 * @return {@code true} if the provisioning status is neither ACTIVE nor {@code null},
531 * {@code false} otherwise
533 protected static boolean isProvStatusInactive(VirtualControlLoopEvent event) {
534 Map<String, String> aai = event.getAai();
535 return !(PROV_STATUS_ACTIVE.equals(aai.getOrDefault(VSERVER_PROV_STATUS, PROV_STATUS_ACTIVE))
536 && PROV_STATUS_ACTIVE.equals(aai.getOrDefault(GENERIC_VNF_PROV_STATUS, PROV_STATUS_ACTIVE)));
540 * Determines the boolean value represented by the given AAI field value.
542 * @param aaiValue value to be examined
543 * @return the boolean value represented by the field value, or {@code false} if the
544 * value is {@code null}
546 protected static boolean isAaiTrue(String aaiValue) {
547 return (aaiValue != null && TRUE_VALUES.contains(aaiValue.toLowerCase()));
551 * Requests a lock. This requests the lock for the time that remains before the
552 * timeout expires. This avoids having to extend the lock.
554 * @param targetEntity entity to be locked
555 * @param lockUnavailableCallback function to be invoked if the lock is
557 * @return a future that can be used to await the lock
560 public synchronized CompletableFuture<OperationOutcome> requestLock(String targetEntity,
561 Consumer<OperationOutcome> lockUnavailableCallback) {
563 long remainingMs = endTimeMs - System.currentTimeMillis();
564 int remainingSec = 15 + Math.max(0, (int) TimeUnit.SECONDS.convert(remainingMs, TimeUnit.MILLISECONDS));
566 LockData data = target2lock.computeIfAbsent(targetEntity, key -> {
567 LockData data2 = new LockData(key, requestId);
568 makeLock(targetEntity, requestId.toString(), remainingSec, data2);
572 data.addUnavailableCallback(lockUnavailableCallback);
574 return data.getFuture();
578 * Initializes various components, on demand.
580 private static class LazyInitData {
581 private static final OperationHistoryDataManager DATA_MANAGER;
582 private static final ActorService ACTOR_SERVICE;
585 EventManagerServices services = new EventManagerServices(EVENT_MANAGER_SERVICE_CONFIG);
586 ACTOR_SERVICE = services.getActorService();
587 DATA_MANAGER = services.getDataManager();
591 // the following methods may be overridden by junit tests
593 protected ControlLoopOperationManager2 makeOperationManager(ControlLoopEventContext ctx, Policy policy) {
594 return new ControlLoopOperationManager2(this, ctx, policy, getExecutor());
597 protected Executor getExecutor() {
598 return ForkJoinPool.commonPool();
601 protected ExecutorService getBlockingExecutor() {
602 return PolicyEngineConstants.getManager().getExecutorService();
605 protected void makeLock(String targetEntity, String requestId, int holdSec, LockCallback callback) {
606 PolicyEngineConstants.getManager().createLock(targetEntity, requestId, holdSec, callback, false);
610 public ActorService getActorService() {
611 return LazyInitData.ACTOR_SERVICE;
615 public OperationHistoryDataManager getDataManager() {
616 return LazyInitData.DATA_MANAGER;