2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 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.drools.apps.controller.usecases;
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;
26 import static org.onap.policy.drools.apps.controller.usecases.UsecasesConstants.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED;
27 import static org.onap.policy.drools.apps.controller.usecases.UsecasesConstants.GENERIC_VNF_PROV_STATUS;
28 import static org.onap.policy.drools.apps.controller.usecases.UsecasesConstants.GENERIC_VNF_VNF_ID;
29 import static org.onap.policy.drools.apps.controller.usecases.UsecasesConstants.GENERIC_VNF_VNF_NAME;
30 import static org.onap.policy.drools.apps.controller.usecases.UsecasesConstants.PNF_IS_IN_MAINT;
31 import static org.onap.policy.drools.apps.controller.usecases.UsecasesConstants.PNF_NAME;
32 import static org.onap.policy.drools.apps.controller.usecases.UsecasesConstants.PROV_STATUS_ACTIVE;
33 import static org.onap.policy.drools.apps.controller.usecases.UsecasesConstants.VM_NAME;
34 import static org.onap.policy.drools.apps.controller.usecases.UsecasesConstants.VNF_NAME;
35 import static org.onap.policy.drools.apps.controller.usecases.UsecasesConstants.VSERVER_IS_CLOSED_LOOP_DISABLED;
36 import static org.onap.policy.drools.apps.controller.usecases.UsecasesConstants.VSERVER_PROV_STATUS;
37 import static org.onap.policy.drools.apps.controller.usecases.UsecasesConstants.VSERVER_VSERVER_NAME;
39 import java.util.ArrayDeque;
40 import java.util.Deque;
41 import java.util.LinkedHashMap;
42 import java.util.LinkedList;
45 import java.util.stream.Collectors;
46 import java.util.stream.Stream;
48 import lombok.NonNull;
50 import lombok.ToString;
51 import org.apache.commons.lang3.StringUtils;
52 import org.drools.core.WorkingMemory;
53 import org.kie.api.runtime.rule.FactHandle;
54 import org.onap.policy.controlloop.ControlLoopEventStatus;
55 import org.onap.policy.controlloop.ControlLoopException;
56 import org.onap.policy.controlloop.ControlLoopNotificationType;
57 import org.onap.policy.controlloop.ControlLoopOperation;
58 import org.onap.policy.controlloop.ControlLoopResponse;
59 import org.onap.policy.controlloop.VirtualControlLoopEvent;
60 import org.onap.policy.controlloop.VirtualControlLoopNotification;
61 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
62 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
63 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
64 import org.onap.policy.controlloop.drl.legacy.ControlLoopParams;
65 import org.onap.policy.controlloop.eventmanager.ActorConstants;
66 import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager;
67 import org.onap.policy.controlloop.eventmanager.StepContext;
68 import org.onap.policy.controlloop.policy.FinalResult;
69 import org.onap.policy.controlloop.policy.Policy;
70 import org.onap.policy.controlloop.policy.PolicyResult;
71 import org.onap.policy.drools.apps.controller.usecases.step.AaiCqStep2;
72 import org.onap.policy.drools.apps.controller.usecases.step.AaiGetPnfStep2;
73 import org.onap.policy.drools.apps.controller.usecases.step.AaiGetTenantStep2;
74 import org.onap.policy.drools.apps.controller.usecases.step.GetTargetEntityStep2;
75 import org.onap.policy.drools.apps.controller.usecases.step.GuardStep2;
76 import org.onap.policy.drools.apps.controller.usecases.step.LockStep2;
77 import org.onap.policy.drools.apps.controller.usecases.step.Step2;
78 import org.onap.policy.drools.system.PolicyEngine;
79 import org.onap.policy.drools.system.PolicyEngineConstants;
80 import org.onap.policy.sdnr.PciMessage;
81 import org.slf4j.Logger;
82 import org.slf4j.LoggerFactory;
85 * Manager for a single control loop event. Once this has been created, the event can be
86 * retracted from working memory. Processing progresses through each policy, which
87 * involves at least one step. As a step is processed, additional preprocessor steps may
88 * be pushed onto the queue (e.g., locks, A&AI queries, guards).
90 @ToString(onlyExplicitlyIncluded = true)
91 public class UsecasesEventManager extends ControlLoopEventManager implements StepContext {
93 private static final Logger logger = LoggerFactory.getLogger(UsecasesEventManager.class);
94 private static final long serialVersionUID = -1216568161322872641L;
97 * Maximum number of steps, for a single policy, allowed in the queue at a time. This
98 * prevents an infinite loop occurring with calls to {@link #loadPreprocessorSteps()}.
100 public static final int MAX_STEPS = 30;
103 * If there's a failure from one of these actors, then the TOSCA processing should be
106 private static final Set<String> ABORT_ACTORS = Set.of(ActorConstants.CL_TIMEOUT_ACTOR, ActorConstants.LOCK_ACTOR);
108 private static final Set<String> VALID_TARGETS = Stream
109 .of(VM_NAME, VNF_NAME, VSERVER_VSERVER_NAME, GENERIC_VNF_VNF_ID, GENERIC_VNF_VNF_NAME, PNF_NAME)
110 .map(String::toLowerCase).collect(Collectors.toSet());
112 private static final Set<String> TRUE_VALUES = Set.of("true", "t", "yes", "y");
115 * Names of Operation properties for which A&AI PNF query is needed.
117 private static final Set<String> PNF_PROPERTIES = Set.of(OperationProperties.AAI_PNF);
120 * Names of Operation properties for which A&AI Tenant query is needed.
122 private static final Set<String> TENANT_PROPERTIES = Set.of(OperationProperties.AAI_VSERVER_LINK);
125 * Names of Operation properties for which A&AI custom query is needed.
127 private static final Set<String> CQ_PROPERTIES = Set.of(OperationProperties.AAI_DEFAULT_CLOUD_REGION,
128 OperationProperties.AAI_VNF, OperationProperties.AAI_SERVICE_MODEL,
129 OperationProperties.AAI_VNF_MODEL, OperationProperties.AAI_SERVICE,
130 OperationProperties.AAI_RESOURCE_VNF, UsecasesConstants.AAI_DEFAULT_GENERIC_VNF);
133 LOAD_POLICY, POLICY_LOADED, AWAITING_OUTCOME, DONE
136 public enum NewEventStatus {
137 FIRST_ONSET, SUBSEQUENT_ONSET, FIRST_ABATEMENT, SUBSEQUENT_ABATEMENT, SYNTAX_ERROR
141 private final VirtualControlLoopEvent event;
144 * Request ID, as a String.
146 private final String requestIdStr;
153 * {@code True} if the event has been accepted (i.e., an "ACTIVE" notification has
154 * been delivered), {@code false} otherwise.
158 private boolean accepted;
161 * Queue of steps waiting to be performed.
164 private final transient Deque<Step2> steps = new ArrayDeque<>(6);
167 * Number of attempts, so far, for the current step.
170 private int attempts;
173 * Policy currently being processed.
175 private Policy policy;
178 * Result of the last policy operation. This is just a place where the rules can store
179 * the value for passing to {@link #loadNextPolicy()}.
183 private PolicyResult result = PolicyResult.SUCCESS;
186 private int numOnsets = 1;
188 private int numAbatements = 0;
190 private VirtualControlLoopEvent abatement = null;
193 * Full history of operations that have been processed by the rules. This includes the
194 * items in {@link #partialHistory}.
197 private final transient Deque<OperationOutcome2> fullHistory = new LinkedList<>();
200 * History of operations that have been processed by the rules for the current policy.
201 * When a step is started, its "start" outcome is added. However, once it completes,
202 * its "start" outcome is removed and the "completed" outcome is added.
205 private final transient Deque<OperationOutcome2> partialHistory = new LinkedList<>();
208 private FinalResult finalResult = null;
211 * Message to be placed into the final notification. Typically used when something
212 * causes processing to abort.
215 private String finalMessage = null;
217 private final transient WorkingMemory workMem;
218 private transient FactHandle factHandle;
222 * Constructs the object.
224 * @param params control loop parameters
225 * @param event event to be managed by this object
226 * @param workMem working memory to update if this changes
227 * @throws ControlLoopException if the event is invalid or if a YAML processor cannot
230 public UsecasesEventManager(ControlLoopParams params, VirtualControlLoopEvent event, WorkingMemory workMem)
231 throws ControlLoopException {
233 super(params, event.getRequestId());
235 checkEventSyntax(event);
237 if (isClosedLoopDisabled(event)) {
238 throw new IllegalStateException("is-closed-loop-disabled is set to true on VServer or VNF");
241 if (isProvStatusInactive(event)) {
242 throw new IllegalStateException("prov-status is not ACTIVE on VServer or VNF");
246 this.workMem = workMem;
247 this.requestIdStr = getRequestId().toString();
251 public void destroy() {
252 for (Step2 step : steps) {
260 * Starts the manager and loads the first policy.
262 * @throws ControlLoopException if the processor cannot get a policy
264 public void start() throws ControlLoopException {
266 throw new IllegalStateException("manager is no longer active");
269 if ((factHandle = workMem.getFactHandle(this)) == null) {
270 throw new IllegalStateException("manager is not in working memory");
273 if (!steps.isEmpty()) {
274 throw new IllegalStateException("manager already started");
281 * Indicates that processing has been aborted.
283 * @param finalState final state
284 * @param finalResult final result
285 * @param finalMessage final message
287 public void abort(@NonNull State finalState, FinalResult finalResult, String finalMessage) {
288 this.state = finalState;
289 this.finalResult = finalResult;
290 this.finalMessage = finalMessage;
294 * Loads the next policy.
296 * @param lastResult result from the last policy
298 * @throws ControlLoopException if the processor cannot get a policy
300 public void loadNextPolicy(@NonNull PolicyResult lastResult) throws ControlLoopException {
301 getProcessor().nextPolicyForResult(lastResult);
306 * Loads the current policy.
308 * @throws ControlLoopException if the processor cannot get a policy
310 private void loadPolicy() throws ControlLoopException {
311 partialHistory.clear();
313 if ((finalResult = getProcessor().checkIsCurrentPolicyFinal()) != null) {
314 // final policy - nothing more to do
318 policy = getProcessor().getCurrentPolicy();
320 // convert policy payload from Map<String,String> to Map<String,Object>
321 Map<String, Object> payload = new LinkedHashMap<>();
322 if (policy.getPayload() != null) {
323 payload.putAll(policy.getPayload());
327 ControlLoopOperationParams params = ControlLoopOperationParams.builder()
328 .actorService(getActorService())
329 .actor(policy.getActor())
330 .operation(policy.getRecipe())
331 .requestId(event.getRequestId())
333 .executor(getExecutor())
334 .target(policy.getTarget())
335 .retry(policy.getRetry())
336 .timeoutSec(policy.getTimeout())
338 .startCallback(this::onStart)
339 .completeCallback(this::onComplete)
343 // load the policy's operation
344 steps.add(new Step2(this, params, event));
348 * Loads the preprocessor steps needed by the step that's at the front of the queue.
350 public void loadPreprocessorSteps() {
351 if (steps.size() >= MAX_STEPS) {
352 throw new IllegalStateException("too many steps");
355 final Step2 step = steps.peek();
357 // initialize the step so we can query its properties
360 // determine if any A&AI queries are needed
361 boolean needCq = false;
362 boolean needPnf = false;
363 boolean needTenant = false;
364 boolean needTargetEntity = false;
366 for (String propName : step.getPropertyNames()) {
367 needCq = needCq || CQ_PROPERTIES.contains(propName);
368 needPnf = needPnf || PNF_PROPERTIES.contains(propName);
369 needTenant = needTenant || TENANT_PROPERTIES.contains(propName);
370 needTargetEntity = needTargetEntity || OperationProperties.AAI_TARGET_ENTITY.equals(propName);
374 * The Policy's actual operation requires additional, implicit steps, such as
375 * locking and guards.
377 final boolean needPolicySteps = step.isPolicyStep();
381 * NOTE: need to push steps onto the queue in the OPPOSITE order in which they
386 // GUARD must be pushed first
387 if (needPolicySteps) {
388 steps.push(new GuardStep2(step, getClosedLoopControlName()));
393 steps.push(new AaiCqStep2(step));
397 steps.push(new AaiGetPnfStep2(step));
401 steps.push(new AaiGetTenantStep2(step));
404 // LOCK must be pushed after the queries
405 if (needPolicySteps) {
406 steps.push(new LockStep2(step));
409 // GET-TARGET-ENTITY should be pushed last
410 if (needTargetEntity) {
411 steps.push(new GetTargetEntityStep2(step));
416 * Executes the first step in the queue.
418 * @return {@code true} if the step was started, {@code false} if it is no longer
419 * needed (or if the queue is empty)
421 public boolean executeStep() {
424 Step2 step = steps.peek();
429 return step.start(getEndTimeMs() - System.currentTimeMillis());
433 * Discards the current step, if any.
435 public void nextStep() {
440 * Increments the number of attempts.
442 public void bumpAttempts() {
447 * Determines if the TOSCA should be aborted due to the given outcome.
449 * @param outcome outcome to examine
450 * @return {@code true} if the TOSCA should be aborted, {@code false} otherwise
452 public boolean isAbort(OperationOutcome outcome) {
453 return (outcome.getResult() != PolicyResult.SUCCESS && ABORT_ACTORS.contains(outcome.getActor()));
457 * Adds the outcome to the history.
459 * @param outcome outcome to add
461 public void addToHistory(OperationOutcome outcome) {
462 OperationOutcome2 last = partialHistory.peekLast();
464 if (last != null && last.getOutcome().getEnd() == null
465 && last.getOutcome().isFor(outcome.getActor(), outcome.getOperation())) {
466 // last item was a "start" - remove it
467 partialHistory.removeLast();
469 if (fullHistory.peekLast() == last) {
470 fullHistory.removeLast();
474 OperationOutcome2 outcome2 = new OperationOutcome2(outcome);
475 partialHistory.add(outcome2);
476 fullHistory.add(outcome2);
480 * Makes a notification message for the current operation.
482 * @return a new notification
484 public VirtualControlLoopNotification makeNotification() {
485 VirtualControlLoopNotification notif = new VirtualControlLoopNotification(event);
486 notif.setNotification(ControlLoopNotificationType.OPERATION);
487 notif.setFrom("policy");
488 notif.setPolicyVersion(getPolicyVersion());
490 if (finalResult != null) {
494 OperationOutcome2 last = partialHistory.peekLast();
499 notif.setMessage(last.getClOperation().toHistory());
500 notif.setHistory(partialHistory.stream().map(OperationOutcome2::getClOperation).collect(Collectors.toList()));
506 * Delivers a notification to a topic.
508 * @param sinkName name of the topic sink
509 * @param notification notification to be published, or {@code null} if nothing is to
511 * @param notificationType type of notification, used when logging error messages
512 * @param ruleName name of the rule doing the publishing
514 public <T> void deliver(String sinkName, T notification, String notificationType, String ruleName) {
516 if (notification != null) {
517 getPolicyEngineManager().deliver(sinkName, notification);
520 } catch (RuntimeException e) {
521 logger.warn("{}: {}.{}: manager={} exception publishing {}", getClosedLoopControlName(), getPolicyName(),
522 ruleName, this, notificationType, e);
527 * Get the last operation, as a message.
529 * @return the last operation, as a message
531 public String getOperationMessage() {
532 OperationOutcome2 last = fullHistory.peekLast();
533 return (last == null ? null : last.getClOperation().toMessage());
537 * Stores an operation outcome in the DB.
539 * @param outcome operation outcome to store
541 public void storeInDataBase(OperationOutcome2 outcome) {
542 String targetEntity = getProperty(OperationProperties.AAI_TARGET_ENTITY);
544 getDataManager().store(requestIdStr, event, targetEntity, outcome.getClOperation());
548 * Makes a control loop response.
550 * @param outcome operation outcome
551 * @return a new control loop response, or {@code null} if none is required
553 public ControlLoopResponse makeControlLoopResponse(OperationOutcome outcome) {
554 ControlLoopResponse clRsp = new ControlLoopResponse();
555 clRsp.setFrom(outcome.getActor());
556 clRsp.setTarget("DCAE");
557 clRsp.setClosedLoopControlName(event.getClosedLoopControlName());
558 clRsp.setPolicyName(event.getPolicyName());
559 clRsp.setPolicyVersion(event.getPolicyVersion());
560 clRsp.setRequestId(event.getRequestId());
561 clRsp.setVersion(event.getVersion());
563 Object obj = outcome.getResponse();
564 if (!(obj instanceof PciMessage)) {
568 PciMessage msg = (PciMessage) obj;
569 if (msg.getBody() != null && msg.getBody().getOutput() != null) {
570 clRsp.setPayload(msg.getBody().getOutput().getPayload());
577 * An event onset/abatement.
579 * @param newEvent the event
582 public NewEventStatus onNewEvent(VirtualControlLoopEvent newEvent) {
584 checkEventSyntax(newEvent);
586 if (newEvent.getClosedLoopEventStatus() == ControlLoopEventStatus.ONSET) {
587 if (newEvent.equals(event)) {
588 return NewEventStatus.FIRST_ONSET;
592 return NewEventStatus.SUBSEQUENT_ONSET;
595 if (abatement == null) {
596 abatement = newEvent;
598 return NewEventStatus.FIRST_ABATEMENT;
601 return NewEventStatus.SUBSEQUENT_ABATEMENT;
604 } catch (ControlLoopException e) {
605 logger.error("{}: onNewEvent threw an exception", this, e);
606 return NewEventStatus.SYNTAX_ERROR;
611 * Check an event syntax.
613 * @param event the event syntax
614 * @throws ControlLoopException if an error occurs
616 protected void checkEventSyntax(VirtualControlLoopEvent event) throws ControlLoopException {
617 validateStatus(event);
618 if (StringUtils.isBlank(event.getClosedLoopControlName())) {
619 throw new ControlLoopException("No control loop name");
621 if (event.getRequestId() == null) {
622 throw new ControlLoopException("No request ID");
624 if (event.getClosedLoopEventStatus() == ControlLoopEventStatus.ABATED) {
627 if (StringUtils.isBlank(event.getTarget())) {
628 throw new ControlLoopException("No target field");
629 } else if (!VALID_TARGETS.contains(event.getTarget().toLowerCase())) {
630 throw new ControlLoopException("target field invalid");
632 validateAaiData(event);
635 private void validateStatus(VirtualControlLoopEvent event) throws ControlLoopException {
636 if (event.getClosedLoopEventStatus() != ControlLoopEventStatus.ONSET
637 && event.getClosedLoopEventStatus() != ControlLoopEventStatus.ABATED) {
638 throw new ControlLoopException("Invalid value in closedLoopEventStatus");
642 private void validateAaiData(VirtualControlLoopEvent event) throws ControlLoopException {
643 Map<String, String> eventAai = event.getAai();
644 if (eventAai == null) {
645 throw new ControlLoopException("AAI is null");
647 if (event.getTargetType() == null) {
648 throw new ControlLoopException("The Target type is null");
650 switch (event.getTargetType()) {
653 validateAaiVmVnfData(eventAai);
656 validateAaiPnfData(eventAai);
659 throw new ControlLoopException("The target type is not supported");
663 private void validateAaiVmVnfData(Map<String, String> eventAai) throws ControlLoopException {
664 if (eventAai.get(GENERIC_VNF_VNF_ID) == null && eventAai.get(VSERVER_VSERVER_NAME) == null
665 && eventAai.get(GENERIC_VNF_VNF_NAME) == null) {
666 throw new ControlLoopException(
667 "generic-vnf.vnf-id or generic-vnf.vnf-name or vserver.vserver-name information missing");
671 private void validateAaiPnfData(Map<String, String> eventAai) throws ControlLoopException {
672 if (eventAai.get(PNF_NAME) == null) {
673 throw new ControlLoopException("AAI PNF object key pnf-name is missing");
678 * Is closed loop disabled for an event.
680 * @param event the event
681 * @return <code>true</code> if the control loop is disabled, <code>false</code>
684 private static boolean isClosedLoopDisabled(VirtualControlLoopEvent event) {
685 Map<String, String> aai = event.getAai();
686 return (isAaiTrue(aai.get(VSERVER_IS_CLOSED_LOOP_DISABLED))
687 || isAaiTrue(aai.get(GENERIC_VNF_IS_CLOSED_LOOP_DISABLED))
688 || isAaiTrue(aai.get(PNF_IS_IN_MAINT)));
692 * Does provisioning status, for an event, have a value other than ACTIVE.
694 * @param event the event
695 * @return {@code true} if the provisioning status is neither ACTIVE nor {@code null},
696 * {@code false} otherwise
698 private static boolean isProvStatusInactive(VirtualControlLoopEvent event) {
699 Map<String, String> aai = event.getAai();
700 return !(PROV_STATUS_ACTIVE.equals(aai.getOrDefault(VSERVER_PROV_STATUS, PROV_STATUS_ACTIVE))
701 && PROV_STATUS_ACTIVE.equals(aai.getOrDefault(GENERIC_VNF_PROV_STATUS, PROV_STATUS_ACTIVE)));
705 * Determines the boolean value represented by the given AAI field value.
707 * @param aaiValue value to be examined
708 * @return the boolean value represented by the field value, or {@code false} if the
709 * value is {@code null}
711 private static boolean isAaiTrue(String aaiValue) {
712 return (aaiValue != null && TRUE_VALUES.contains(aaiValue.toLowerCase()));
716 public void onStart(OperationOutcome outcome) {
717 super.onStart(outcome);
718 workMem.update(factHandle, this);
722 public void onComplete(OperationOutcome outcome) {
723 super.onComplete(outcome);
724 workMem.update(factHandle, this);
729 public class OperationOutcome2 {
730 private final int attempt;
731 private final OperationOutcome outcome;
732 private final ControlLoopOperation clOperation;
735 * Constructs the object.
737 * @param outcome outcome of the operation
739 public OperationOutcome2(OperationOutcome outcome) {
740 this.outcome = outcome;
741 this.attempt = attempts;
743 clOperation = outcome.toControlLoopOperation();
744 clOperation.setTarget(policy.getTarget().toString());
746 if (outcome.getEnd() == null) {
747 clOperation.setOutcome("Started");
748 } else if (clOperation.getOutcome() == null) {
749 clOperation.setOutcome("");
754 // these following methods may be overridden by junit tests
756 protected PolicyEngine getPolicyEngineManager() {
757 return PolicyEngineConstants.getManager();