2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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;
23 import org.onap.policy.controlloop.VirtualControlLoopEvent;
24 import org.onap.policy.controlloop.VirtualControlLoopNotification;
25 import org.onap.policy.controlloop.ControlLoopEventStatus;
26 import org.onap.policy.controlloop.ControlLoopNotificationType;
27 import org.onap.policy.controlloop.ControlLoopLogger;
28 import org.onap.policy.controlloop.policy.PolicyResult;
29 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
30 import org.onap.policy.controlloop.policy.Policy;
31 import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager;
32 import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager.NEW_EVENT_STATUS;
33 import org.onap.policy.controlloop.eventmanager.ControlLoopOperationManager;
34 import org.onap.policy.controlloop.actor.so.SoActorServiceProvider;
35 import org.onap.policy.aai.AaiNqResponseWrapper;
36 import org.onap.policy.appc.Request;
37 import org.onap.policy.appc.Response;
38 import org.onap.policy.appc.CommonHeader;
39 import org.onap.policy.appclcm.LcmRequestWrapper;
40 import org.onap.policy.appclcm.LcmResponseWrapper;
41 import org.onap.policy.appclcm.LcmRequest;
42 import org.onap.policy.appclcm.LcmResponse;
43 import org.onap.policy.appclcm.LcmCommonHeader;
44 import org.onap.policy.vfc.VFCRequest;
45 import org.onap.policy.vfc.VFCResponse;
46 import org.onap.policy.vfc.VFCManager;
47 import org.onap.policy.so.SOManager;
48 import org.onap.policy.so.SORequest;
49 import org.onap.policy.so.SORequestStatus;
50 import org.onap.policy.so.SORequestDetails;
51 import org.onap.policy.so.SOModelInfo;
52 import org.onap.policy.so.SOCloudConfiguration;
53 import org.onap.policy.so.SORequestInfo;
54 import org.onap.policy.so.SORequestParameters;
55 import org.onap.policy.so.SORelatedInstanceListElement;
56 import org.onap.policy.so.SORelatedInstance;
57 import org.onap.policy.so.SOResponse;
58 import org.onap.policy.so.SOResponseWrapper;
59 import org.onap.policy.guard.PolicyGuard;
60 import org.onap.policy.guard.PolicyGuard.LockResult;
61 import org.onap.policy.guard.TargetLock;
62 import org.onap.policy.guard.GuardResult;
63 import org.onap.policy.guard.PolicyGuardRequest;
64 import org.onap.policy.guard.PolicyGuardResponse;
65 import org.onap.policy.guard.PolicyGuardXacmlRequestAttributes;
66 import org.onap.policy.guard.PolicyGuardXacmlHelper;
68 import org.yaml.snakeyaml.Yaml;
69 import org.yaml.snakeyaml.constructor.Constructor;
71 import org.slf4j.LoggerFactory;
72 import org.slf4j.Logger;
74 import java.time.Instant;
75 import java.util.LinkedList;
76 import java.util.Iterator;
78 import org.onap.policy.drools.system.PolicyEngine;
81 * This structure mimics the Params structure.
82 * Its only purpose is to allow management of
83 * rules by the PAP component..
84 * It has no use at runtime since the rules go by
85 * Params for matching purposes.
88 closedLoopControlName : String
89 controlLoopYaml : String
93 * Control Loop Identity
96 closedLoopControlName : String
97 controlLoopYaml : String
101 * Used to clean up Params that no longer have associated rules.
103 declare ParamsCleaner
104 closedLoopControlName : String
105 controlLoopYaml : String
109 * This object is to provide support for timeouts
110 * due to a bug in drools' built-in timers
112 declare ControlLoopTimer
113 closedLoopControlName : String
117 //timerType is the type of timer: either "ClosedLoop" or "Operation"
123 * Called once and only once to insert the parameters into working memory for this Closed Loop policy.
124 * This has a higher salience so we can ensure that the Params is created before we have a chance to
125 * discard any events.
128 rule "${policyName}.SETUP"
131 not( Params( getClosedLoopControlName() == "${closedLoopControlName}",
132 getControlLoopYaml() == "${controlLoopYaml}" ) )
135 Params params = new Params();
136 params.setClosedLoopControlName("${closedLoopControlName}");
137 params.setControlLoopYaml("${controlLoopYaml}");
140 // Note: globals have bad behavior when persistence is used,
141 // hence explicitly getting the logger vs using a global
143 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
144 logger.info("{}: {} : YAML=[{}]", params.getClosedLoopControlName(), drools.getRule().getName(),
145 params.getControlLoopYaml());
150 * This rule responds to DCAE Events where there is no manager yet. Either it is
151 * the first ONSET, or a subsequent badly formed Event (i.e. Syntax error, or is-closed-loop-disabled)
154 rule "${policyName}.EVENT"
156 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
157 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
158 not ( ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
159 requestID == $event.getRequestId() ) )
162 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
163 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
168 // Check the event, because we need it to not be null when
169 // we create the ControlLoopEventManager. The ControlLoopEventManager
170 // will do extra syntax checking as well check if the closed loop is disabled.
172 if ($event.getRequestId() == null) {
173 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
174 notification.setNotification(ControlLoopNotificationType.REJECTED);
175 notification.setFrom("policy");
176 notification.setMessage("Missing requestId");
177 notification.setPolicyName(drools.getRule().getName());
178 notification.setPolicyScope("${policyScope}");
179 notification.setPolicyVersion("${policyVersion}");
182 // Let interested parties know
184 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
187 // Retract it from memory
190 } else if ($event.getClosedLoopEventStatus() != ControlLoopEventStatus.ONSET) {
191 throw new ControlLoopException($event.getClosedLoopEventStatus() + " received with no prior onset");
194 // Create an EventManager
196 ControlLoopEventManager manager = new ControlLoopEventManager($params.getClosedLoopControlName(),
197 $event.getRequestId());
199 // Determine if EventManager can actively process the event
200 // (i.e. syntax, is_closed_loop_disabled checks etc.)
202 VirtualControlLoopNotification notification = manager.activate($params.getControlLoopYaml(), $event);
203 notification.setFrom("pdp-0001-controller=controlloop"); // Engine.getInstanceName()
204 notification.setPolicyName(drools.getRule().getName());
205 notification.setPolicyScope("${policyScope}");
206 notification.setPolicyVersion("${policyVersion}");
208 // Are we actively pursuing this event?
210 if (notification.getNotification() == ControlLoopNotificationType.ACTIVE) {
212 // Insert Event Manager into memory, this will now kick off processing.
216 // Let interested parties know
218 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
220 // Setup the Overall Control Loop timer
222 ControlLoopTimer clTimer = new ControlLoopTimer();
223 clTimer.setTimerType("ClosedLoop");
224 clTimer.setClosedLoopControlName($event.getClosedLoopControlName());
225 clTimer.setRequestID($event.getRequestId().toString());
226 clTimer.setDelay(manager.getControlLoopTimeout(1500) + "s");
233 // Let interested parties know
235 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
237 // Retract it from memory
243 // Now that the manager is inserted into Drools working memory, we'll wait for
244 // another rule to fire in order to continue processing. This way we can also
245 // then screen for additional ONSET and ABATED events for this RequestId.
248 } catch (Exception e) {
249 logger.warn("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName(), e);
251 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
252 notification.setNotification(ControlLoopNotificationType.REJECTED);
253 notification.setMessage("Exception occurred: " + e.getMessage());
254 notification.setPolicyName(drools.getRule().getName());
255 notification.setPolicyScope("${policyScope}");
256 notification.setPolicyVersion("${policyVersion}");
260 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
270 * This rule happens when we got a valid ONSET, closed loop is enabled and an Event Manager
271 * is now created. We can start processing the yaml specification via the Event Manager.
274 rule "${policyName}.EVENT.MANAGER"
276 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
277 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
278 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
279 requestID == $event.getRequestId() )
280 $clTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
281 requestID == $event.getRequestId().toString(), timerType == "ClosedLoop", !expired )
284 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
285 logger.info("{}: {}: event={} manager={} clTimer={}",
286 $params.getClosedLoopControlName(), drools.getRule().getName(),
287 $event, $manager, $clTimer);
291 // Check which event this is.
293 ControlLoopEventManager.NEW_EVENT_STATUS eventStatus = $manager.onNewEvent($event);
295 // Check what kind of event this is
297 if (eventStatus == NEW_EVENT_STATUS.SUBSEQUENT_ONSET) {
299 // We don't care about subsequent onsets
301 logger.info("{}: {}: subsequent onset",
302 $params.getClosedLoopControlName(), drools.getRule().getName());
306 if (eventStatus == NEW_EVENT_STATUS.SYNTAX_ERROR) {
308 // Ignore any bad syntax events
310 logger.warn("{}: {}: syntax error",
311 $params.getClosedLoopControlName(), drools.getRule().getName());
316 // We only want the initial ONSET event in memory,
317 // all the other events need to be retracted to support
318 // cleanup and avoid the other rules being fired for this event.
320 if (eventStatus != NEW_EVENT_STATUS.FIRST_ONSET) {
321 logger.warn("{}: {}: not first onset",
322 $params.getClosedLoopControlName(), drools.getRule().getName());
326 logger.debug("{}: {}: target={}", $params.getClosedLoopControlName(),
327 drools.getRule().getName(), $event.getTarget());
329 // Now start seeing if we need to process this event
333 // Check if this is a Final Event
335 VirtualControlLoopNotification notification = $manager.isControlLoopFinal();
338 if (notification != null) {
340 // Its final, but are we waiting for abatement?
342 if ($manager.getNumAbatements() > 0) {
343 logger.info("{}: {}: abatement received for {}. Closing the control loop",
344 $params.getClosedLoopControlName(), drools.getRule().getName(),
345 $event.getRequestId());
347 /// DB Write---end event processing for this RequestId()
348 $manager.commitAbatement("Event Abated","Closed");
350 notification.setFrom("policy");
351 notification.setPolicyName(drools.getRule().getName());
352 notification.setPolicyScope("${policyScope}");
353 notification.setPolicyVersion("${policyVersion}");
355 // In this case, we are done
357 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
361 TargetLock lock = $manager.unlockCurrentOperation();
363 logger.debug("{}: {}: retracting lock=", $params.getClosedLoopControlName(),
364 drools.getRule().getName(), lock);
368 // Retract everything from memory
370 logger.info("{}: {}: retracting onset, manager, and timer",
371 $params.getClosedLoopControlName(), drools.getRule().getName());
373 retract($manager.getOnsetEvent());
377 // TODO - what if we get subsequent Events for this RequestId?
378 // By default, it will all start over again. May be confusing for Ruby.
379 // Or, we could track this and then subsequently ignore the events
383 // Check whether we need to wait for abatement
385 if ($manager.getProcessor().getControlLoop().getAbatement() == true && notification.getNotification() == ControlLoopNotificationType.FINAL_SUCCESS) {
386 logger.info("{}: {}: waiting for abatement ..",
387 $params.getClosedLoopControlName(), drools.getRule().getName());
389 logger.info("{}: {}: no abatement expect for {}. Closing the control loop",
390 $params.getClosedLoopControlName(), drools.getRule().getName(),
391 $event.getRequestId());
393 notification.setFrom("policy");
394 notification.setPolicyName(drools.getRule().getName());
395 notification.setPolicyScope("${policyScope}");
396 notification.setPolicyVersion("${policyVersion}");
399 // In this case, we are done
401 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
405 TargetLock lock = $manager.unlockCurrentOperation();
407 logger.debug("{}: {}: retracting lock=", $params.getClosedLoopControlName(),
408 drools.getRule().getName(), lock);
412 // Retract everything from memory
414 logger.info("{}: {}: retracting onset, manager, and timer",
415 $params.getClosedLoopControlName(), drools.getRule().getName());
417 retract($manager.getOnsetEvent());
424 // NOT final, so let's ask for the next operation
426 ControlLoopOperationManager operation = $manager.processControlLoop();
427 if (operation != null) {
429 // Let's ask for a lock right away
431 LockResult<GuardResult, TargetLock> result = $manager.lockCurrentOperation();
432 logger.info("{}: {}: guard lock acquired={}",
433 $params.getClosedLoopControlName(), drools.getRule().getName(),
435 if (result.getA().equals(GuardResult.LOCK_ACQUIRED)) {
437 // insert the operation into memory
441 // insert operation timeout object
443 ControlLoopTimer opTimer = new ControlLoopTimer();
444 opTimer.setTimerType("Operation");
445 opTimer.setClosedLoopControlName($event.getClosedLoopControlName());
446 opTimer.setRequestID($event.getRequestId().toString());
447 opTimer.setDelay(operation.getOperationTimeout().toString() + "s");
451 // Insert lock into memory
453 insert(result.getB());
455 logger.debug("The target resource {} is already processing",
456 $event.getAai().get($event.getTarget()));
457 notification = new VirtualControlLoopNotification($event);
458 notification.setNotification(ControlLoopNotificationType.REJECTED);
459 notification.setMessage("The target " + $event.getAai().get($event.getTarget())
460 + " is already locked");
461 notification.setFrom("policy");
462 notification.setPolicyName(drools.getRule().getName());
463 notification.setPolicyScope("${policyScope}");
464 notification.setPolicyVersion("${policyVersion}");
466 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
472 if (result.getB() != null) {
473 retract(result.getB());
476 logger.info("{}: {}: starting operation={}",
477 $params.getClosedLoopControlName(), drools.getRule().getName(),
481 // Probably waiting for abatement
483 logger.info("{}: {}: no operation, probably waiting for abatement",
484 $params.getClosedLoopControlName(), drools.getRule().getName());
487 } catch (Exception e) {
488 logger.warn("{}: {}: unexpected",
489 $params.getClosedLoopControlName(),
490 drools.getRule().getName(), e);
492 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
493 notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
494 notification.setMessage(e.getMessage());
495 notification.setFrom("policy");
496 notification.setPolicyName(drools.getRule().getName());
497 notification.setPolicyScope("${policyScope}");
498 notification.setPolicyVersion("${policyVersion}");
500 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
511 * Guard Permitted, let's send request to the actor.
514 rule "${policyName}.EVENT.MANAGER.OPERATION.LOCKED.GUARD_PERMITTED"
516 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
517 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
518 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
519 requestID == $event.getRequestId() )
520 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
521 onset.getRequestId() == $event.getRequestId(), "Permit".equalsIgnoreCase(getGuardApprovalStatus()) )
522 $lock : TargetLock (requestID == $event.getRequestId())
523 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
524 requestID == $event.getRequestId().toString(), timerType == "Operation", !expired )
527 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
528 logger.info("{}: {}: event={} manager={} operation={} lock={}",
529 $params.getClosedLoopControlName(), drools.getRule().getName(),
530 $event, $manager, $operation, $lock);
532 Object request = null;
533 boolean caughtException = false;
536 request = $operation.startOperation($event);
538 if (request != null) {
539 logger.debug("{}: {}: starting operation ..",
540 $params.getClosedLoopControlName(), drools.getRule().getName());
542 // Tell interested parties we are performing this Operation
544 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
545 notification.setNotification(ControlLoopNotificationType.OPERATION);
546 notification.setMessage($operation.getOperationMessage());
547 notification.setHistory($operation.getHistory());
548 notification.setFrom("policy");
549 notification.setPolicyName(drools.getRule().getName());
550 notification.setPolicyScope("${policyScope}");
551 notification.setPolicyVersion("${policyVersion}");
553 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
555 switch ($operation.policy.getActor()){
558 if (request instanceof Request) {
559 PolicyEngine.manager.deliver("APPC-CL", request);
561 else if (request instanceof LcmRequestWrapper) {
562 PolicyEngine.manager.deliver("APPC-LCM-READ", request);
566 // at this point the AAI named query request should have already been made,
567 // the response recieved and used
568 // in the construction of the SO Request which is stored in operationRequest
570 if(request instanceof SORequest) {
571 // Call SO. The response will be inserted into memory once it's received
572 SoActorServiceProvider.sendRequest($event.getRequestId().toString(), drools.getWorkingMemory(),
577 if (request instanceof VFCRequest) {
579 Thread t = new Thread(new VFCManager(drools.getWorkingMemory(), (VFCRequest)request));
586 // What happens if its null?
588 logger.warn("{}: {}: unexpected null operation request",
589 $params.getClosedLoopControlName(),
590 drools.getRule().getName());
591 if ("SO".equals($operation.policy.getActor())) {
594 modify($manager) {finishOperation($operation)};
596 else if ("vfc".equalsIgnoreCase($operation.policy.getActor())) {
599 modify($manager) {finishOperation($operation)};
603 } catch (Exception e) {
604 String msg = e.getMessage();
605 logger.warn("{}: {}: operation={}: AAI failure: {}",
606 $params.getClosedLoopControlName(), drools.getRule().getName(),
608 $operation.setOperationHasException(msg);
610 if(request != null) {
612 // Create a notification for it ("DB Write - end operation")
614 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
615 notification.setFrom("policy");
616 notification.setPolicyName(drools.getRule().getName());
617 notification.setPolicyScope("${policyScope}");
618 notification.setPolicyVersion("${policyVersion}");
619 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
620 notification.setMessage($operation.getOperationHistory());
621 notification.setHistory($operation.getHistory());
623 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
628 caughtException = true;
631 // Having the modify statement in the catch clause doesn't work for whatever reason
632 if (caughtException) {
633 modify($manager) {finishOperation($operation)};
640 * We were able to acquire a lock so now let's ask Xacml Guard whether
641 * we are allowed to proceed with the request to the actor.
644 rule "${policyName}.EVENT.MANAGER.OPERATION.LOCKED.GUARD_NOT_YET_QUERIED"
646 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
647 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
648 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
649 requestID == $event.getRequestId() )
650 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
651 onset.getRequestId() == $event.getRequestId(), getGuardApprovalStatus() == "NONE" )
652 $lock : TargetLock (requestID == $event.getRequestId())
655 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
656 logger.info("{}: {}: event={} manager={} operation={} lock={}",
657 $params.getClosedLoopControlName(), drools.getRule().getName(),
658 $event, $manager, $operation, $lock);
661 // Sending notification that we are about to query Guard ("DB write - start operation")
663 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
664 notification.setNotification(ControlLoopNotificationType.OPERATION);
665 notification.setMessage("Sending guard query for " + $operation.policy.getActor() + " "
666 + $operation.policy.getRecipe());
667 notification.setHistory($operation.getHistory());
668 notification.setFrom("policy");
669 notification.setPolicyName(drools.getRule().getName());
670 notification.setPolicyScope("${policyScope}");
671 notification.setPolicyVersion("${policyVersion}");
673 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
676 // Now send Guard Request to XACML Guard. In order to bypass the call to Guard,
677 // just change guardEnabled to false.
679 // In order to use REST XACML, provide a URL instead of "" as a second argument
680 // to the CallGuardTask() and set the first argument to null
681 // (instead of XacmlPdpEngine).
684 // NOTE: The environment properties uses "guard.disabled" but the boolean is guardEnabled
685 boolean guardEnabled = "false".equalsIgnoreCase(PolicyEngine.manager.getEnvironmentProperty("guard.disabled"));
689 Thread t = new Thread(new org.onap.policy.guard.CallGuardTask(
690 drools.getWorkingMemory(),
691 $event.getClosedLoopControlName(),
692 $operation.policy.getActor().toString(),
693 $operation.policy.getRecipe(),
694 $operation.getTargetEntity(),
695 $event.getRequestId().toString(),
697 AaiNqResponseWrapper resp = $manager.getNqVserverFromAai();
698 return(resp == null ? null : resp.countVfModules());
703 insert(new PolicyGuardResponse("Permit", $event.getRequestId(), $operation.policy.getRecipe()));
709 // This rule will be triggered when a thread talking to the XACML Guard inserts a
710 // guardResponse object into the working memory
712 rule "${policyName}.GUARD.RESPONSE"
714 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
715 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
716 closedLoopEventStatus == ControlLoopEventStatus.ONSET )
717 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
718 requestID == $event.getRequestId() )
719 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
720 onset.getRequestId() == $event.getRequestId() )
721 $lock : TargetLock (requestID == $event.getRequestId())
722 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
723 requestID == $event.getRequestId().toString(), timerType == "Operation", !expired )
724 $guardResponse : PolicyGuardResponse(requestID == $event.getRequestId(), $operation.policy.recipe == operation)
727 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
728 logger.info("{}: {}: event={} manager={} operation={} lock={} opTimer={} guardResponse={}",
729 $params.getClosedLoopControlName(), drools.getRule().getName(),
730 $event, $manager, $operation, $lock, $opTimer, $guardResponse);
733 //we will permit the operation if there was no Guard for it
734 if("Indeterminate".equalsIgnoreCase($guardResponse.getResult())){
735 $guardResponse.setResult("Permit");
739 // This notification has Guard result in "message". ("DB write - end operation in case of Guard Deny")
741 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
742 notification.setNotification(ControlLoopNotificationType.OPERATION);
743 notification.setMessage("Guard result for " + $operation.policy.getActor() + " " + $operation.policy.getRecipe()
744 + " is " + $guardResponse.getResult());
745 notification.setHistory($operation.getHistory());
746 notification.setFrom("policy");
747 notification.setPolicyName(drools.getRule().getName());
748 notification.setPolicyScope("${policyScope}");
749 notification.setPolicyVersion("${policyVersion}");
751 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
753 if("Permit".equalsIgnoreCase($guardResponse.getResult())){
755 modify($operation){setGuardApprovalStatus($guardResponse.getResult())};
758 //This is the Deny case
759 $operation.startOperation($event);
760 $operation.setOperationHasGuardDeny();
763 modify($manager) {finishOperation($operation)};
766 retract($guardResponse);
772 * This rule responds to APPC Response Events
774 * I would have like to be consistent and write the Response like this:
775 * $response : Response( CommonHeader.RequestId == $onset.getRequestId() )
777 * However, no compile error was given. But a runtime error was given. I think
778 * because drools is confused between the classname CommonHeader vs the property CommonHeader.
781 rule "${policyName}.APPC.RESPONSE"
783 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
784 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
785 closedLoopEventStatus == ControlLoopEventStatus.ONSET )
786 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
787 requestID == $event.getRequestId() )
788 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
789 onset.getRequestId() == $event.getRequestId() )
790 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
791 requestID == $event.getRequestId().toString(), timerType == "Operation", !expired )
792 $lock : TargetLock (requestID == $event.getRequestId())
793 $response : Response( getCommonHeader().RequestId == $event.getRequestId() )
796 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
797 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
798 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
799 $params.getClosedLoopControlName(), drools.getRule().getName(),
800 $event, $manager, $operation, $lock, $opTimer, $response);
802 // Get the result of the operation
804 PolicyResult policyResult = $operation.onResponse($response);
805 if (policyResult != null) {
806 logger.debug("{}: {}: operation finished - result={}",
807 $params.getClosedLoopControlName(), drools.getRule().getName(),
810 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
812 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
813 notification.setFrom("policy");
814 notification.setPolicyName(drools.getRule().getName());
815 notification.setPolicyScope("${policyScope}");
816 notification.setPolicyVersion("${policyVersion}");
817 notification.setMessage($operation.getOperationHistory());
818 notification.setHistory($operation.getHistory());
819 if (policyResult.equals(PolicyResult.SUCCESS)) {
820 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
822 // Let interested parties know
824 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
826 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
828 // Let interested parties know
830 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
833 // Ensure the operation is complete
835 if ($operation.isOperationComplete() == true) {
837 // It is complete, remove it from memory
841 // We must also retract the timer object
842 // NOTE: We could write a Rule to do this
846 // Complete the operation
848 modify($manager) {finishOperation($operation)};
851 // Just doing this will kick off the LOCKED rule again
853 modify($operation) {};
857 // Its not finished yet (i.e. expecting more Response objects)
859 // Or possibly it is a leftover response that we timed the request out previously
863 // We are going to retract these objects from memory
870 * The problem with Responses is that they don't have a controlLoopControlName
871 * field in them, so the only way to attach them is via RequestId. If we have multiple
872 * control loop .drl's loaded in the same container, we need to be sure the cleanup
873 * rules don't remove Responses for other control loops.
876 rule "${policyName}.APPC.RESPONSE.CLEANUP"
878 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
879 $response : Response($id : getCommonHeader().RequestId )
880 not ( VirtualControlLoopEvent( requestId == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
883 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
884 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
885 logger.debug("{}: {}: orphan appc response={}",
886 $params.getClosedLoopControlName(), drools.getRule().getName(), $id);
896 * This rule responds to APPC Response Events using the new LCM interface provided by appc
899 rule "${policyName}.APPC.LCM.RESPONSE"
901 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
902 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
903 closedLoopEventStatus == ControlLoopEventStatus.ONSET )
904 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
905 requestID == $event.getRequestId() )
906 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
907 onset.getRequestId() == $event.getRequestId() )
908 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
909 requestID == $event.getRequestId().toString(), timerType == "Operation", !expired )
910 $lock : TargetLock (requestID == $event.getRequestId())
911 $response : LcmResponseWrapper( getBody().getCommonHeader().getRequestId() == $event.getRequestId() )
914 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
915 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
916 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
917 $params.getClosedLoopControlName(), drools.getRule().getName(),
918 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
921 // Get the result of the operation
923 PolicyResult policyResult = $operation.onResponse($response);
924 if (policyResult != null) {
925 logger.debug("{}: {}: operation finished - result={}",
926 $params.getClosedLoopControlName(), drools.getRule().getName(),
930 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
932 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
933 notification.setFrom("policy");
934 notification.setPolicyName(drools.getRule().getName());
935 notification.setPolicyScope("${policyScope}");
936 notification.setPolicyVersion("${policyVersion}");
937 notification.setMessage($operation.getOperationHistory());
938 notification.setHistory($operation.getHistory());
939 if (policyResult.equals(PolicyResult.SUCCESS)) {
940 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
942 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
944 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
946 // Ensure the operation is complete
948 if ($operation.isOperationComplete() == true) {
950 // It is complete, remove it from memory
954 // We must also retract the timer object
955 // NOTE: We could write a Rule to do this
959 // Complete the operation
961 modify($manager) {finishOperation($operation)};
964 // Just doing this will kick off the LOCKED rule again
966 modify($operation) {};
970 // Its not finished yet (i.e. expecting more Response objects)
972 // Or possibly it is a leftover response that we timed the request out previously
976 // We are going to retract these objects from memory
983 * Clean Up any lingering LCM reponses
986 rule "${policyName}.APPC.LCM.RESPONSE.CLEANUP"
988 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
989 $response : LcmResponseWrapper($id : getBody().getCommonHeader().getRequestId )
990 not ( VirtualControlLoopEvent( requestId == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
993 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
994 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
995 logger.debug("{}: {}: orphan appc response={}",
996 $params.getClosedLoopControlName(), drools.getRule().getName(), $id);
1005 * This rule responds to SO Response Events
1008 rule "${policyName}.SO.RESPONSE"
1010 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1011 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
1012 closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1013 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1014 requestID == $event.getRequestId() )
1015 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
1016 onset.getRequestId() == $event.getRequestId() )
1017 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1018 requestID == $event.getRequestId().toString(), timerType == "Operation", !expired )
1019 $lock : TargetLock (requestID == $event.getRequestId())
1020 $response : SOResponseWrapper(requestID.toString() == $event.getRequestId().toString() )
1023 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1024 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1025 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
1026 $params.getClosedLoopControlName(), drools.getRule().getName(),
1027 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
1029 // Get the result of the operation
1031 PolicyResult policyResult = $operation.onResponse($response);
1032 if (policyResult != null) {
1033 logger.debug("{}: {}: operation finished - result={}",
1034 $params.getClosedLoopControlName(), drools.getRule().getName(),
1038 // This Operation has completed, construct a notification showing our results
1040 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1041 notification.setFrom("policy");
1042 notification.setPolicyName(drools.getRule().getName());
1043 notification.setPolicyScope("${policyScope}");
1044 notification.setPolicyVersion("${policyVersion}");
1045 notification.setMessage($operation.getOperationHistory());
1046 notification.setHistory($operation.getHistory());
1047 if (policyResult.equals(PolicyResult.SUCCESS)) {
1048 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
1050 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
1053 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1055 // Ensure the operation is complete
1057 if ($operation.isOperationComplete() == true) {
1059 // It is complete, remove it from memory
1061 retract($operation);
1063 // We must also retract the timer object
1064 // NOTE: We could write a Rule to do this
1068 // Complete the operation
1070 modify($manager) {finishOperation($operation)};
1073 // Just doing this will kick off the LOCKED rule again
1075 modify($operation) {};
1079 // Its not finished yet (i.e. expecting more Response objects)
1081 // Or possibly it is a leftover response that we timed the request out previously
1085 // We are going to retract these objects from memory
1093 * This rule responds to VFC Response Events
1096 rule "${policyName}.VFC.RESPONSE"
1098 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1099 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
1100 closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1101 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1102 requestID == $event.getRequestId() )
1103 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
1104 onset.getRequestId() == $event.getRequestId() )
1105 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1106 requestID == $event.getRequestId().toString(), timerType == "Operation", !expired )
1107 $lock : TargetLock (requestID == $event.getRequestId())
1108 $response : VFCResponse( requestId.toString() == $event.getRequestId().toString() )
1110 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1111 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1112 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
1113 $params.getClosedLoopControlName(), drools.getRule().getName(),
1114 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
1116 // Get the result of the operation
1118 PolicyResult policyResult = $operation.onResponse($response);
1119 if (policyResult != null) {
1121 // This Operation has completed, construct a notification showing our results
1123 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1124 notification.setFrom("policy");
1125 notification.setPolicyName(drools.getRule().getName());
1126 notification.setPolicyScope("${policyScope}");
1127 notification.setPolicyVersion("${policyVersion}");
1128 notification.setMessage($operation.getOperationHistory());
1129 notification.setHistory($operation.getHistory());
1131 // Ensure the operation is complete
1133 if ($operation.isOperationComplete() == true) {
1135 // It is complete, remove it from memory
1137 retract($operation);
1139 // We must also retract the timer object
1140 // NOTE: We could write a Rule to do this
1144 // Complete the operation
1146 modify($manager) {finishOperation($operation)};
1149 // Just doing this will kick off the LOCKED rule again
1151 modify($operation) {};
1155 // Its not finished yet (i.e. expecting more Response objects)
1157 // Or possibly it is a leftover response that we timed the request out previously
1161 // We are going to retract these objects from memory
1169 * This manages a single timer.
1170 * Due to a bug in the drools code, the drools timer needed to be split from most of the objects in the when clause
1173 rule "${policyName}.TIMER.FIRED"
1174 timer (expr: $timeout)
1176 $timer : ControlLoopTimer($timeout : delay, !expired)
1178 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1179 logger.info("This is ${policyName}.TIMER.FIRED");
1180 modify($timer){setExpired(true)};
1185 * This is the timer that manages the timeout for an individual operation.
1188 rule "${policyName}.EVENT.MANAGER.OPERATION.TIMEOUT"
1190 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1191 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
1192 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1193 requestID == $event.getRequestId() )
1194 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
1195 onset.getRequestId() == $event.getRequestId() )
1196 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1197 requestID == $event.getRequestId().toString(), expired, timerType == "Operation" )
1198 $lock : TargetLock (requestID == $event.getRequestId())
1201 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1202 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1203 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={}",
1204 $params.getClosedLoopControlName(), drools.getRule().getName(),
1205 $event, $manager, $operation, $lock, $operation, $opTimer);
1208 // Tell it its timed out
1210 $operation.setOperationHasTimedOut();
1212 // Create a notification for it ("DB Write - end operation")
1214 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1215 notification.setFrom("policy");
1216 notification.setPolicyName(drools.getRule().getName());
1217 notification.setPolicyScope("${policyScope}");
1218 notification.setPolicyVersion("${policyVersion}");
1219 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
1220 notification.setMessage($operation.getOperationHistory());
1221 notification.setHistory($operation.getHistory());
1223 // Let interested parties know
1225 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1227 // Get rid of the timer
1231 // Ensure the operation is complete
1233 if ($operation.isOperationComplete() == true) {
1235 // It is complete, remove it from memory
1237 retract($operation);
1239 // Complete the operation
1241 modify($manager) {finishOperation($operation)};
1244 // Just doing this will kick off the LOCKED rule again
1246 modify($operation) {};
1252 * This is the timer that manages the overall control loop timeout.
1255 rule "${policyName}.EVENT.MANAGER.TIMEOUT"
1257 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1258 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
1259 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1260 requestID == $event.getRequestId() )
1261 $clTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1262 requestID == $event.getRequestId().toString(), expired, timerType == "ClosedLoop" )
1265 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1266 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1268 logger.debug("{}: {}: event={}",
1269 $params.getClosedLoopControlName(), drools.getRule().getName(),
1272 // Tell the Event Manager it has timed out
1274 VirtualControlLoopNotification notification = $manager.setControlLoopTimedOut();
1275 if (notification != null) {
1276 notification.setFrom("policy");
1277 notification.setPolicyName(drools.getRule().getName());
1278 notification.setPolicyScope("${policyScope}");
1279 notification.setPolicyVersion("${policyVersion}");
1281 // Let interested parties know
1283 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1286 // Retract the event
1293 * This rule cleans up the manager and other objects after an event has
1297 rule "${policyName}.EVENT.MANAGER.CLEANUP"
1299 $manager : ControlLoopEventManager( $clName : getClosedLoopControlName(), $requestId : getRequestID() )
1300 $operations : LinkedList()
1301 from collect( ControlLoopOperationManager( onset.closedLoopControlName == $clName,
1302 onset.getRequestId() == $requestId ) )
1303 $timers : LinkedList()
1304 from collect( ControlLoopTimer( closedLoopControlName == $clName,
1305 requestID == $requestId.toString() ) )
1306 $locks : LinkedList()
1307 from collect( TargetLock (requestID == $requestId) )
1308 not( VirtualControlLoopEvent( closedLoopControlName == $clName, requestId == $requestId ) )
1311 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1312 logger.info("{}: {}", $clName, drools.getRule().getName());
1314 logger.debug("{}: {}: manager={} timers={} operations={}",
1315 $clName, drools.getRule().getName(),
1316 $manager, $timers.size(), $operations.size());
1319 // Retract EVERYTHING
1323 for(Object manager: $operations) {
1324 retract((ControlLoopOperationManager) manager);
1326 for(Object timer: $timers) {
1327 retract((ControlLoopTimer) timer);
1329 for(Object lock: $locks) {
1330 TargetLock tgt = (TargetLock) lock;
1332 // Ensure we release the lock
1334 PolicyGuard.unlockTarget(tgt);
1341 * This rule will clean up any rogue onsets where there is no
1342 * ControlLoopParams object corresponding to the onset event.
1345 rule "${policyName}.EVENT.CLEANUP"
1347 $event : VirtualControlLoopEvent( $clName: closedLoopControlName )
1348 not ( Params( getClosedLoopControlName() == $clName) )
1351 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1352 logger.info("{}: {}", $clName, drools.getRule().getName());
1353 logger.debug("{}: {}: orphan onset event={}",
1354 $clName, drools.getRule().getName(), $event);
1361 * When rules are deleted, the associated Params (and its subordinate objects)
1362 * remain in working memory, because there are no longer any rules to clean
1363 * them up. However, ANY time new rules are loaded, this rule will trigger
1364 * a clean-up of ALL Params, regardless of their name & yaml, thus removing
1365 * any that no longer have associated rules.
1366 * This has a higher salience so that we immediately check Params when the
1367 * rules change, before processing any events.
1370 rule "${policyName}.PARAMS.CHECKUP"
1373 Params( $clName: closedLoopControlName, $yaml: controlLoopYaml )
1376 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1377 logger.info("{}: {} : YAML=[{}]", $clName, drools.getRule().getName(), $yaml);
1379 ParamsCleaner cleaner = new ParamsCleaner();
1380 cleaner.setClosedLoopControlName($clName);
1381 cleaner.setControlLoopYaml($yaml);
1388 * This rule removes "cleaner" objects for rules that are still active, thus
1389 * preventing the associated Params objects from being removed. Any cleaners
1390 * that are left after this rule has fired will cause their corresponding Params
1392 * This has a higher salience so that we discard the cleaner before it has
1393 * a chance to force the removal of the associated Params.
1396 rule "${policyName}.CLEANER.ACTIVE"
1399 $cleaner: ParamsCleaner( getClosedLoopControlName() == "${closedLoopControlName}",
1400 getControlLoopYaml() == "${controlLoopYaml}" )
1403 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1404 logger.info("{}: {} : YAML=[{}]", $cleaner.getClosedLoopControlName(), drools.getRule().getName(),
1405 $cleaner.getControlLoopYaml());
1412 * This rule removes Params objects that no longer have associated rules; if a
1413 * Params still had associated rules, then the cleaner would have been removed
1414 * by those rules and thus this rule would not fire.
1415 * This has a higher salience so that we remove old Params before it causes any
1416 * events to be processed.
1419 rule "${policyName}.PARAMS.CLEANUP"
1422 $params: Params( $clName: closedLoopControlName, $yaml: controlLoopYaml )
1423 ParamsCleaner( getClosedLoopControlName() == $clName, getControlLoopYaml() == $yaml )
1426 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1427 logger.info("{}: {} : YAML=[{}]", $params.getClosedLoopControlName(), drools.getRule().getName(),
1428 $params.getControlLoopYaml());
1432 // Note: the cleaner may be needed for cleaning additional params, thus
1433 // we do not retract it here - we'll leave that to another rule
1438 * This rule removes "cleaner" objects when they're no longer needed.
1441 rule "${policyName}.CLEANER.CLEANUP"
1443 $cleaner: ParamsCleaner( )
1446 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1447 logger.info("{}: {} : YAML=[{}]", $cleaner.getClosedLoopControlName(), drools.getRule().getName(),
1448 $cleaner.getControlLoopYaml());