2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 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.ControlLoopException;
27 import org.onap.policy.controlloop.ControlLoopNotificationType;
28 import org.onap.policy.controlloop.ControlLoopLogger;
29 import org.onap.policy.controlloop.policy.PolicyResult;
30 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
31 import org.onap.policy.controlloop.policy.Policy;
32 import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager;
33 import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager.NEW_EVENT_STATUS;
34 import org.onap.policy.controlloop.eventmanager.ControlLoopOperationManager;
35 import org.onap.policy.controlloop.actor.so.SoActorServiceProvider;
36 import org.onap.policy.aai.AaiNqResponseWrapper;
37 import org.onap.policy.appc.Request;
38 import org.onap.policy.appc.Response;
39 import org.onap.policy.appc.CommonHeader;
40 import org.onap.policy.appclcm.LcmRequestWrapper;
41 import org.onap.policy.appclcm.LcmResponseWrapper;
42 import org.onap.policy.appclcm.LcmRequest;
43 import org.onap.policy.appclcm.LcmResponse;
44 import org.onap.policy.appclcm.LcmCommonHeader;
45 import org.onap.policy.vfc.VFCRequest;
46 import org.onap.policy.vfc.VFCResponse;
47 import org.onap.policy.vfc.VFCManager;
48 import org.onap.policy.so.SOManager;
49 import org.onap.policy.so.SORequest;
50 import org.onap.policy.so.SORequestStatus;
51 import org.onap.policy.so.SORequestDetails;
52 import org.onap.policy.so.SOModelInfo;
53 import org.onap.policy.so.SOCloudConfiguration;
54 import org.onap.policy.so.SORequestInfo;
55 import org.onap.policy.so.SORequestParameters;
56 import org.onap.policy.so.SORelatedInstanceListElement;
57 import org.onap.policy.so.SORelatedInstance;
58 import org.onap.policy.so.SOResponse;
59 import org.onap.policy.so.SOResponseWrapper;
60 import org.onap.policy.guard.PolicyGuard;
61 import org.onap.policy.guard.PolicyGuard.LockResult;
62 import org.onap.policy.guard.TargetLock;
63 import org.onap.policy.guard.GuardResult;
64 import org.onap.policy.guard.PolicyGuardRequest;
65 import org.onap.policy.guard.PolicyGuardResponse;
66 import org.onap.policy.guard.PolicyGuardXacmlRequestAttributes;
67 import org.onap.policy.guard.CallGuardTaskEmbedded;
69 import org.yaml.snakeyaml.Yaml;
70 import org.yaml.snakeyaml.constructor.Constructor;
72 import org.slf4j.LoggerFactory;
73 import org.slf4j.Logger;
75 import java.time.Instant;
76 import java.util.LinkedList;
77 import java.util.Iterator;
79 import org.onap.policy.drools.system.PolicyEngine;
82 * This structure mimics the Params structure.
83 * Its only purpose is to allow management of
84 * rules by the PAP component..
85 * It has no use at runtime since the rules go by
86 * Params for matching purposes.
89 closedLoopControlName : String
90 controlLoopYaml : String
94 * Control Loop Identity
97 closedLoopControlName : String
98 controlLoopYaml : String
102 * Used to clean up Params that no longer have associated rules.
104 declare ParamsCleaner
105 closedLoopControlName : String
106 controlLoopYaml : String
113 declare OperationTimer
114 closedLoopControlName : String
122 declare ControlLoopTimer
123 closedLoopControlName : String
130 * Called once and only once to insert the parameters into working memory for this Closed Loop policy.
131 * This has a higher salience so we can ensure that the Params is created before we have a chance to
132 * discard any events.
135 rule "${policyName}.SETUP"
138 not( Params( getClosedLoopControlName() == "${closedLoopControlName}", getControlLoopYaml() == "${controlLoopYaml}" ) )
141 Params params = new Params();
142 params.setClosedLoopControlName("${closedLoopControlName}");
143 params.setControlLoopYaml("${controlLoopYaml}");
146 // Note: globals have bad behavior when persistence is used,
147 // hence explicitly getting the logger vs using a global
149 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
150 logger.info("{}: {} : YAML=[{}]", params.getClosedLoopControlName(), drools.getRule().getName(), params.getControlLoopYaml());
155 * This rule responds to DCAE Events where there is no manager yet. Either it is
156 * the first ONSET, or a subsequent badly formed Event (i.e. Syntax error, or is-closed-loop-disabled)
159 rule "${policyName}.EVENT"
161 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
162 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
163 not ( ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() ) )
166 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
167 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
172 // Check the event, because we need it to not be null when
173 // we create the ControlLoopEventManager. The ControlLoopEventManager
174 // will do extra syntax checking as well check if the closed loop is disabled.
176 if ($event.getRequestId() == null) {
177 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
178 notification.setNotification(ControlLoopNotificationType.REJECTED);
179 notification.setFrom("policy");
180 notification.setMessage("Missing requestId");
181 notification.setPolicyName(drools.getRule().getName());
182 notification.setPolicyScope("${policyScope}");
183 notification.setPolicyVersion("${policyVersion}");
186 // Let interested parties know
188 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
191 // Retract it from memory
194 } else if ($event.getClosedLoopEventStatus() != ControlLoopEventStatus.ONSET) {
195 throw new ControlLoopException($event.getClosedLoopEventStatus() + " received with no prior onset");
198 // Create an EventManager
200 ControlLoopEventManager manager = new ControlLoopEventManager($params.getClosedLoopControlName(), $event.getRequestId());
202 // Disable target locking
204 manager.setUseTargetLock(false);
206 // Determine if EventManager can actively process the event (i.e. syntax, is_closed_loop_disabled checks etc.)
208 VirtualControlLoopNotification notification = manager.activate($params.getControlLoopYaml(), $event);
209 notification.setFrom("pdp-0001-controller=controlloop"); // Engine.getInstanceName()
210 notification.setPolicyName(drools.getRule().getName());
211 notification.setPolicyScope("${policyScope}");
212 notification.setPolicyVersion("${policyVersion}");
214 // Are we actively pursuing this event?
216 if (notification.getNotification() == ControlLoopNotificationType.ACTIVE) {
218 // Insert Event Manager into memory, this will now kick off processing.
222 // Let interested parties know
224 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
226 // Setup the Overall Control Loop timer
228 ControlLoopTimer clTimer = new ControlLoopTimer();
229 clTimer.setClosedLoopControlName($event.getClosedLoopControlName());
230 clTimer.setRequestID($event.getRequestId().toString());
231 clTimer.setDelay(manager.getControlLoopTimeout(1500) + "s");
238 // Let interested parties know
240 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
242 // Retract it from memory
248 // Now that the manager is inserted into Drools working memory, we'll wait for
249 // another rule to fire in order to continue processing. This way we can also
250 // then screen for additional ONSET and ABATED events for this RequestId.
253 } catch (Exception e) {
254 logger.warn("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName(), e);
256 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
257 notification.setNotification(ControlLoopNotificationType.REJECTED);
258 notification.setMessage("Exception occurred: " + e.getMessage());
259 notification.setPolicyName(drools.getRule().getName());
260 notification.setPolicyScope("${policyScope}");
261 notification.setPolicyVersion("${policyVersion}");
265 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
275 * This rule happens when we got a valid ONSET, closed loop is enabled and an Event Manager
276 * is now created. We can start processing the yaml specification via the Event Manager.
279 rule "${policyName}.EVENT.MANAGER"
281 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
282 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
283 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
284 $clTimer : ControlLoopTimer ( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
287 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
288 logger.info("{}: {}: event={} manager={} clTimer={}",
289 $params.getClosedLoopControlName(), drools.getRule().getName(),
290 $event, $manager, $clTimer);
294 // Check which event this is.
296 ControlLoopEventManager.NEW_EVENT_STATUS eventStatus = $manager.onNewEvent($event);
298 // Check what kind of event this is
300 if (eventStatus == NEW_EVENT_STATUS.SUBSEQUENT_ONSET) {
302 // We don't care about subsequent onsets
304 logger.info("{}: {}: subsequent onset",
305 $params.getClosedLoopControlName(), drools.getRule().getName());
309 if (eventStatus == NEW_EVENT_STATUS.SYNTAX_ERROR) {
311 // Ignore any bad syntax events
313 logger.warn("{}: {}: syntax error",
314 $params.getClosedLoopControlName(), drools.getRule().getName());
319 // We only want the initial ONSET event in memory,
320 // all the other events need to be retracted to support
321 // cleanup and avoid the other rules being fired for this event.
323 if (eventStatus != NEW_EVENT_STATUS.FIRST_ONSET) {
324 logger.warn("{}: {}: not first onset",
325 $params.getClosedLoopControlName(), drools.getRule().getName());
329 logger.debug("{}: {}: target={}", $params.getClosedLoopControlName(),
330 drools.getRule().getName(), $event.getTarget());
332 // Now start seeing if we need to process this event
336 // Check if this is a Final Event
338 VirtualControlLoopNotification notification = $manager.isControlLoopFinal();
340 if (notification != null) {
342 // Set common notification fields
344 notification.setFrom("policy");
345 notification.setPolicyName(drools.getRule().getName());
346 notification.setPolicyScope("${policyScope}");
347 notification.setPolicyVersion("${policyVersion}");
349 // Its final, but are we waiting for abatement?
351 if ($manager.getNumAbatements() > 0) {
353 // We have received an abatement and are done
355 logger.info("{}: {}: abatement received for {}. Closing the control loop",
356 $params.getClosedLoopControlName(), drools.getRule().getName(),
357 $event.getRequestId());
359 // Set notification message
361 notification.setMessage("Abatement received. Closing the control loop.");
363 /// DB Write---end event processing for this RequestId()
365 $manager.commitAbatement("Event Abated","Closed");
369 TargetLock lock = $manager.unlockCurrentOperation();
371 logger.debug("{}: {}: retracting lock=", $params.getClosedLoopControlName(),
372 drools.getRule().getName(), lock);
376 // Retract everything from memory
378 logger.info("{}: {}: retracting onset, manager, and timer",
379 $params.getClosedLoopControlName(), drools.getRule().getName());
381 retract($manager.getOnsetEvent());
385 // TODO - what if we get subsequent Events for this RequestId?
386 // By default, it will all start over again. May be confusing for Ruby.
387 // Or, we could track this and then subsequently ignore the events
391 // Check whether we need to wait for abatement
393 if ($manager.getProcessor().getControlLoop().getAbatement() == true && notification.getNotification() == ControlLoopNotificationType.FINAL_SUCCESS) {
397 logger.info("{}: {}: waiting for abatement ..",
398 $params.getClosedLoopControlName(), drools.getRule().getName());
400 // Set notification message
402 notification.setMessage("Waiting for abatement");
407 logger.info("{}: {}: no abatement expected for {}. Closing the control loop",
408 $params.getClosedLoopControlName(), drools.getRule().getName(),
409 $event.getRequestId());
411 // Set notification message
413 notification.setMessage("No abatement expected. Closing the control loop.");
417 TargetLock lock = $manager.unlockCurrentOperation();
419 logger.debug("{}: {}: retracting lock=", $params.getClosedLoopControlName(),
420 drools.getRule().getName(), lock);
424 // Retract everything from memory
426 logger.info("{}: {}: retracting onset, manager, and timer",
427 $params.getClosedLoopControlName(), drools.getRule().getName());
429 retract($manager.getOnsetEvent());
435 // Send the notification
437 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
440 // NOT final, so let's ask for the next operation
442 ControlLoopOperationManager operation = $manager.processControlLoop();
443 if (operation != null) {
445 // Let's ask for a lock right away
447 LockResult<GuardResult, TargetLock> result = $manager.lockCurrentOperation();
448 logger.info("{}: {}: guard lock acquired={}",
449 $params.getClosedLoopControlName(), drools.getRule().getName(),
451 if (result.getA().equals(GuardResult.LOCK_ACQUIRED)) {
453 // insert the operation into memory
458 // insert operation timeout object
460 OperationTimer opTimer = new OperationTimer();
461 opTimer.setClosedLoopControlName($event.getClosedLoopControlName());
462 opTimer.setRequestID($event.getRequestId().toString());
463 opTimer.setDelay(operation.getOperationTimeout().toString() + "s");
467 // Insert lock into memory
469 insert(result.getB());
472 logger.debug("The target resource {} is already processing",
473 $event.getAai().get($event.getTarget()));
474 notification = new VirtualControlLoopNotification($event);
475 notification.setNotification(ControlLoopNotificationType.REJECTED);
476 notification.setMessage("The target " + $event.getAai().get($event.getTarget()) + " is already locked");
477 notification.setFrom("policy");
478 notification.setPolicyName(drools.getRule().getName());
479 notification.setPolicyScope("${policyScope}");
480 notification.setPolicyVersion("${policyVersion}");
482 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
488 if(result.getB() != null) {
489 retract(result.getB());
492 logger.info("{}: {}: starting operation={}",
493 $params.getClosedLoopControlName(), drools.getRule().getName(),
497 // Probably waiting for abatement
499 logger.info("{}: {}: no operation, probably waiting for abatement",
500 $params.getClosedLoopControlName(), drools.getRule().getName());
503 } catch (Exception e) {
504 logger.warn("{}: {}: unexpected",
505 $params.getClosedLoopControlName(),
506 drools.getRule().getName(), e);
508 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
509 notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
510 notification.setMessage(e.getMessage());
511 notification.setFrom("policy");
512 notification.setPolicyName(drools.getRule().getName());
513 notification.setPolicyScope("${policyScope}");
514 notification.setPolicyVersion("${policyVersion}");
516 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
527 * Guard Permitted, let's send request to the actor.
530 rule "${policyName}.EVENT.MANAGER.OPERATION.LOCKED.GUARD_PERMITTED"
532 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
533 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
534 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
535 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId(), "Permit".equalsIgnoreCase(getGuardApprovalStatus()) )
536 $lock : TargetLock (requestID == $event.getRequestId())
537 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
540 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
541 logger.info("{}: {}: event={} manager={} operation={} lock={}",
542 $params.getClosedLoopControlName(), drools.getRule().getName(),
543 $event, $manager, $operation, $lock);
545 Object request = null;
546 boolean caughtException = false;
549 request = $operation.startOperation($event);
551 if (request != null) {
552 logger.debug("{}: {}: starting operation ..",
553 $params.getClosedLoopControlName(), drools.getRule().getName());
555 // Tell interested parties we are performing this Operation
557 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
558 notification.setNotification(ControlLoopNotificationType.OPERATION);
559 notification.setMessage($operation.getOperationMessage());
560 notification.setHistory($operation.getHistory());
561 notification.setFrom("policy");
562 notification.setPolicyName(drools.getRule().getName());
563 notification.setPolicyScope("${policyScope}");
564 notification.setPolicyVersion("${policyVersion}");
566 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
568 switch ($operation.policy.getActor()){
572 if (request instanceof Request) {
573 PolicyEngine.manager.deliver("APPC-CL", request);
575 else if (request instanceof LcmRequestWrapper) {
576 PolicyEngine.manager.deliver("APPC-LCM-READ", request);
580 // at this point the AAI named query request should have already been made, the response received
581 // and used in the construction of the SO Request which is stored in operationRequest
583 if(request instanceof SORequest) {
584 // Call SO. The response will be inserted into memory once it's received
585 SoActorServiceProvider.sendRequest($event.getRequestId().toString(), drools.getWorkingMemory(),
590 if (request instanceof VFCRequest) {
592 Thread t = new Thread(new VFCManager(drools.getWorkingMemory(), (VFCRequest)request));
599 // What happens if its null?
601 logger.warn("{}: {}: unexpected null operation request",
602 $params.getClosedLoopControlName(),
603 drools.getRule().getName());
604 if ("SO".equals($operation.policy.getActor())) {
607 modify($manager) {finishOperation($operation)};
609 else if ("vfc".equalsIgnoreCase($operation.policy.getActor())) {
612 modify($manager) {finishOperation($operation)};
616 } catch (Exception e) {
617 String msg = e.getMessage();
618 logger.warn("{}: {}: operation={}: AAI failure: {}",
619 $params.getClosedLoopControlName(), drools.getRule().getName(),
621 $operation.setOperationHasException(msg);
623 if(request != null) {
625 // Create a notification for it ("DB Write - end operation")
627 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
628 notification.setFrom("policy");
629 notification.setPolicyName(drools.getRule().getName());
630 notification.setPolicyScope("${policyScope}");
631 notification.setPolicyVersion("${policyVersion}");
632 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
633 notification.setMessage($operation.getOperationHistory());
634 notification.setHistory($operation.getHistory());
636 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
641 caughtException = true;
644 // Having the modify statement in the catch clause doesn't work for whatever reason
645 if (caughtException) {
646 modify($manager) {finishOperation($operation)};
653 * We were able to acquire a lock so now let's ask Xacml Guard whether
654 * we are allowed to proceed with the request to the actor.
657 rule "${policyName}.EVENT.MANAGER.OPERATION.LOCKED.GUARD_NOT_YET_QUERIED"
659 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
660 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
661 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
662 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId(), getGuardApprovalStatus() == "NONE" )
663 $lock : TargetLock (requestID == $event.getRequestId())
666 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
667 logger.info("{}: {}: event={} manager={} operation={} lock={}",
668 $params.getClosedLoopControlName(), drools.getRule().getName(),
669 $event, $manager, $operation, $lock);
672 // Sending notification that we are about to query Guard ("DB write - start operation")
674 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
675 notification.setNotification(ControlLoopNotificationType.OPERATION);
676 notification.setMessage("Sending guard query for " + $operation.policy.getActor() + " " + $operation.policy.getRecipe());
677 notification.setHistory($operation.getHistory());
678 notification.setFrom("policy");
679 notification.setPolicyName(drools.getRule().getName());
680 notification.setPolicyScope("${policyScope}");
681 notification.setPolicyVersion("${policyVersion}");
683 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
686 // Now send Guard Request to XACML Guard. In order to bypass the call to Guard,
687 // just change guardEnabled to false.
691 // NOTE: The environment properties uses "guard.disabled" but the boolean is guardEnabled
692 boolean guardEnabled = "false".equalsIgnoreCase(PolicyEngine.manager.getEnvironmentProperty("guard.disabled"));
696 Thread t = new Thread(new CallGuardTaskEmbedded(
697 drools.getWorkingMemory(),
698 $event.getClosedLoopControlName(),
699 $operation.policy.getActor().toString(),
700 $operation.policy.getRecipe(),
701 $operation.getTargetEntity(),
702 $event.getRequestId().toString(),
704 AaiNqResponseWrapper resp = $manager.getNqVserverFromAai();
705 return(resp == null ? null : resp.countVfModules());
710 insert(new PolicyGuardResponse("Permit", $event.getRequestId(), $operation.policy.getRecipe()));
716 // This rule will be triggered when a thread talking to the XACML Guard inserts a
717 // guardResponse object into the working memory
719 rule "${policyName}.GUARD.RESPONSE"
721 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
722 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
723 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
724 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() )
725 $lock : TargetLock (requestID == $event.getRequestId())
726 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
727 $guardResponse : PolicyGuardResponse(requestID == $event.getRequestId(), $operation.policy.recipe == operation)
730 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
731 logger.info("{}: {}: event={} manager={} operation={} lock={} opTimer={} guardResponse={}",
732 $params.getClosedLoopControlName(), drools.getRule().getName(),
733 $event, $manager, $operation, $lock, $opTimer, $guardResponse);
736 //we will permit the operation if there was no Guard for it
737 if("Indeterminate".equalsIgnoreCase($guardResponse.getResult())){
738 $guardResponse.setResult("Permit");
742 // This notification has Guard result in "message". ("DB write - end operation in case of Guard Deny")
744 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
745 notification.setNotification(ControlLoopNotificationType.OPERATION);
746 notification.setMessage("Guard result for " + $operation.policy.getActor() + " " + $operation.policy.getRecipe() + " is " + $guardResponse.getResult());
747 notification.setHistory($operation.getHistory());
748 notification.setFrom("policy");
749 notification.setPolicyName(drools.getRule().getName());
750 notification.setPolicyScope("${policyScope}");
751 notification.setPolicyVersion("${policyVersion}");
753 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
755 if("Permit".equalsIgnoreCase($guardResponse.getResult())){
757 modify($operation){setGuardApprovalStatus($guardResponse.getResult())};
760 //This is the Deny case
761 $operation.startOperation($event);
762 $operation.setOperationHasGuardDeny();
765 modify($manager) {finishOperation($operation)};
768 retract($guardResponse);
774 * This rule responds to APPC Response Events
776 * I would have like to be consistent and write the Response like this:
777 * $response : Response( CommonHeader.RequestId == $onset.getRequestId() )
779 * However, no compile error was given. But a runtime error was given. I think
780 * because drools is confused between the classname CommonHeader vs the property CommonHeader.
783 rule "${policyName}.APPC.RESPONSE"
785 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
786 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
787 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
788 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() )
789 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
790 $lock : TargetLock (requestID == $event.getRequestId())
791 $response : Response( getCommonHeader().RequestId == $event.getRequestId() )
794 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
795 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
796 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
797 $params.getClosedLoopControlName(), drools.getRule().getName(),
798 $event, $manager, $operation, $lock, $opTimer, $response);
800 // Get the result of the operation
802 PolicyResult policyResult = $operation.onResponse($response);
803 if (policyResult != null) {
804 logger.debug("{}: {}: operation finished - result={}",
805 $params.getClosedLoopControlName(), drools.getRule().getName(),
808 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
810 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
811 notification.setFrom("policy");
812 notification.setPolicyName(drools.getRule().getName());
813 notification.setPolicyScope("${policyScope}");
814 notification.setPolicyVersion("${policyVersion}");
815 notification.setMessage($operation.getOperationHistory());
816 notification.setHistory($operation.getHistory());
817 if (policyResult.equals(PolicyResult.SUCCESS)) {
818 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
820 // Let interested parties know
822 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
824 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
826 // Let interested parties know
828 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
831 // Ensure the operation is complete
833 if ($operation.isOperationComplete() == true) {
835 // It is complete, remove it from memory
839 // We must also retract the timer object
840 // NOTE: We could write a Rule to do this
844 // Complete the operation
846 modify($manager) {finishOperation($operation)};
849 // Just doing this will kick off the LOCKED rule again
851 modify($operation) {};
855 // Its not finished yet (i.e. expecting more Response objects)
857 // Or possibly it is a leftover response that we timed the request out previously
861 // We are going to retract these objects from memory
868 * The problem with Responses is that they don't have a controlLoopControlName
869 * field in them, so the only way to attach them is via RequestId. If we have multiple
870 * control loop .drl's loaded in the same container, we need to be sure the cleanup
871 * rules don't remove Responses for other control loops.
874 rule "${policyName}.APPC.RESPONSE.CLEANUP"
876 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
877 $response : Response($id : getCommonHeader().RequestId )
878 not ( VirtualControlLoopEvent( requestId == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
881 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
882 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
883 logger.debug("{}: {}: orphan appc response={}",
884 $params.getClosedLoopControlName(), drools.getRule().getName(), $id);
894 * This rule responds to APPC Response Events using the new LCM interface provided by appc
897 rule "${policyName}.APPC.LCM.RESPONSE"
899 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
900 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
901 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
902 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() )
903 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
904 $lock : TargetLock (requestID == $event.getRequestId())
905 $response : LcmResponseWrapper( getBody().getCommonHeader().getRequestId() == $event.getRequestId() )
908 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
909 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
910 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
911 $params.getClosedLoopControlName(), drools.getRule().getName(),
912 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
915 // Get the result of the operation
917 PolicyResult policyResult = $operation.onResponse($response);
918 if (policyResult != null) {
919 logger.debug("{}: {}: operation finished - result={}",
920 $params.getClosedLoopControlName(), drools.getRule().getName(),
924 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
926 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
927 notification.setFrom("policy");
928 notification.setPolicyName(drools.getRule().getName());
929 notification.setPolicyScope("${policyScope}");
930 notification.setPolicyVersion("${policyVersion}");
931 notification.setMessage($operation.getOperationHistory());
932 notification.setHistory($operation.getHistory());
933 if (policyResult.equals(PolicyResult.SUCCESS)) {
934 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
936 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
938 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
940 // Ensure the operation is complete
942 if ($operation.isOperationComplete() == true) {
944 // It is complete, remove it from memory
948 // We must also retract the timer object
949 // NOTE: We could write a Rule to do this
953 // Complete the operation
955 modify($manager) {finishOperation($operation)};
958 // Just doing this will kick off the LOCKED rule again
960 modify($operation) {};
964 // Its not finished yet (i.e. expecting more Response objects)
966 // Or possibly it is a leftover response that we timed the request out previously
970 // We are going to retract these objects from memory
977 * Clean Up any lingering LCM reponses
980 rule "${policyName}.APPC.LCM.RESPONSE.CLEANUP"
982 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
983 $response : LcmResponseWrapper($id : getBody().getCommonHeader().getRequestId )
984 not ( VirtualControlLoopEvent( requestId == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
987 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
988 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
989 logger.debug("{}: {}: orphan appc response={}",
990 $params.getClosedLoopControlName(), drools.getRule().getName(), $id);
999 * This rule responds to SO Response Events
1002 rule "${policyName}.SO.RESPONSE"
1004 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1005 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1006 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
1007 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() )
1008 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
1009 $lock : TargetLock (requestID == $event.getRequestId())
1010 $response : SOResponseWrapper(requestID.toString() == $event.getRequestId().toString() )
1013 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1014 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1015 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
1016 $params.getClosedLoopControlName(), drools.getRule().getName(),
1017 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
1019 // Get the result of the operation
1021 PolicyResult policyResult = $operation.onResponse($response);
1022 if (policyResult != null) {
1023 logger.debug("{}: {}: operation finished - result={}",
1024 $params.getClosedLoopControlName(), drools.getRule().getName(),
1028 // This Operation has completed, construct a notification showing our results
1030 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1031 notification.setFrom("policy");
1032 notification.setPolicyName(drools.getRule().getName());
1033 notification.setPolicyScope("${policyScope}");
1034 notification.setPolicyVersion("${policyVersion}");
1035 notification.setMessage($operation.getOperationHistory());
1036 notification.setHistory($operation.getHistory());
1037 if (policyResult.equals(PolicyResult.SUCCESS)) {
1038 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
1040 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
1043 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1045 // Ensure the operation is complete
1047 if ($operation.isOperationComplete() == true) {
1049 // It is complete, remove it from memory
1051 retract($operation);
1053 // We must also retract the timer object
1054 // NOTE: We could write a Rule to do this
1058 // Complete the operation
1060 modify($manager) {finishOperation($operation)};
1063 // Just doing this will kick off the LOCKED rule again
1065 modify($operation) {};
1069 // Its not finished yet (i.e. expecting more Response objects)
1071 // Or possibly it is a leftover response that we timed the request out previously
1075 // We are going to retract these objects from memory
1083 * This rule responds to VFC Response Events
1086 rule "${policyName}.VFC.RESPONSE"
1088 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1089 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1090 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
1091 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() )
1092 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
1093 $lock : TargetLock (requestID == $event.getRequestId())
1094 $response : VFCResponse( requestId.toString() == $event.getRequestId().toString() )
1096 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1097 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1098 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
1099 $params.getClosedLoopControlName(), drools.getRule().getName(),
1100 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
1102 // Get the result of the operation
1104 PolicyResult policyResult = $operation.onResponse($response);
1105 if (policyResult != null) {
1107 // This Operation has completed, construct a notification showing our results
1109 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1110 notification.setFrom("policy");
1111 notification.setPolicyName(drools.getRule().getName());
1112 notification.setPolicyScope("${policyScope}");
1113 notification.setPolicyVersion("${policyVersion}");
1114 notification.setMessage($operation.getOperationHistory());
1115 notification.setHistory($operation.getHistory());
1117 // Ensure the operation is complete
1119 if ($operation.isOperationComplete() == true) {
1121 // It is complete, remove it from memory
1123 retract($operation);
1125 // We must also retract the timer object
1126 // NOTE: We could write a Rule to do this
1130 // Complete the operation
1132 modify($manager) {finishOperation($operation)};
1135 // Just doing this will kick off the LOCKED rule again
1137 modify($operation) {};
1141 // Its not finished yet (i.e. expecting more Response objects)
1143 // Or possibly it is a leftover response that we timed the request out previously
1147 // We are going to retract these objects from memory
1155 * This is the timer that manages the timeout for an individual operation.
1158 rule "${policyName}.EVENT.MANAGER.OPERATION.TIMEOUT"
1161 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1162 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
1163 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
1164 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() )
1165 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString(), $to : getDelay() )
1166 $lock : TargetLock (requestID == $event.getRequestId())
1169 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1170 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1171 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={}",
1172 $params.getClosedLoopControlName(), drools.getRule().getName(),
1173 $event, $manager, $operation, $lock, $operation, $opTimer);
1176 // Tell it its timed out
1178 $operation.setOperationHasTimedOut();
1180 // Create a notification for it ("DB Write - end operation")
1182 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1183 notification.setFrom("policy");
1184 notification.setPolicyName(drools.getRule().getName());
1185 notification.setPolicyScope("${policyScope}");
1186 notification.setPolicyVersion("${policyVersion}");
1187 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
1188 notification.setMessage($operation.getOperationHistory());
1189 notification.setHistory($operation.getHistory());
1191 // Let interested parties know
1193 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1195 // Get rid of the timer
1199 // Ensure the operation is complete
1201 if ($operation.isOperationComplete() == true) {
1203 // It is complete, remove it from memory
1205 retract($operation);
1207 // Complete the operation
1209 modify($manager) {finishOperation($operation)};
1212 // Just doing this will kick off the LOCKED rule again
1214 modify($operation) {};
1220 * This is the timer that manages the overall control loop timeout.
1223 rule "${policyName}.EVENT.MANAGER.TIMEOUT"
1226 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1227 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
1228 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
1229 $clTimer : ControlLoopTimer ( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString(), $to : getDelay() )
1232 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1233 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1235 logger.debug("{}: {}: event={}",
1236 $params.getClosedLoopControlName(), drools.getRule().getName(),
1239 // Tell the Event Manager it has timed out
1241 VirtualControlLoopNotification notification = $manager.setControlLoopTimedOut();
1242 if (notification != null) {
1243 notification.setFrom("policy");
1244 notification.setPolicyName(drools.getRule().getName());
1245 notification.setPolicyScope("${policyScope}");
1246 notification.setPolicyVersion("${policyVersion}");
1248 // Let interested parties know
1250 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1253 // Retract the event
1260 * This rule cleans up the manager and other objects after an event has
1264 rule "${policyName}.EVENT.MANAGER.CLEANUP"
1266 $manager : ControlLoopEventManager( $clName : getClosedLoopControlName(), $requestId : getRequestID() )
1267 $clTimer : ControlLoopTimer ( closedLoopControlName == $clName, requestID == $requestId.toString() )
1268 $operations : LinkedList()
1269 from collect( ControlLoopOperationManager( onset.closedLoopControlName == $clName, onset.getRequestId() == $requestId ) )
1270 $opTimers : LinkedList()
1271 from collect( OperationTimer( closedLoopControlName == $clName, requestID == $requestId.toString() ) )
1272 $locks : LinkedList()
1273 from collect( TargetLock (requestID == $requestId) )
1274 not( VirtualControlLoopEvent( closedLoopControlName == $clName, requestId == $requestId ) )
1277 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1278 logger.info("{}: {}", $clName, drools.getRule().getName());
1280 logger.debug("{}: {}: manager={} clTimer={} operations={}",
1281 $clName, drools.getRule().getName(),
1282 $manager, $clTimer, $operations.size());
1285 // Retract EVERYTHING
1290 for(Object manager: $operations) {
1291 retract((ControlLoopOperationManager) manager);
1293 for(Object opTimer: $opTimers) {
1294 retract((OperationTimer) opTimer);
1296 for(Object lock: $locks) {
1297 TargetLock tgt = (TargetLock) lock;
1299 // Ensure we release the lock
1301 PolicyGuard.unlockTarget(tgt);
1308 * This rule will clean up any rogue onsets where there is no
1309 * ControlLoopParams object corresponding to the onset event.
1312 rule "${policyName}.EVENT.CLEANUP"
1314 $event : VirtualControlLoopEvent( $clName: closedLoopControlName )
1315 not ( Params( getClosedLoopControlName() == $clName) )
1318 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1319 logger.info("{}: {}", $clName, drools.getRule().getName());
1320 logger.debug("{}: {}: orphan onset event={}",
1321 $clName, drools.getRule().getName(), $event);
1328 * When rules are deleted, the associated Params (and its subordinate objects)
1329 * remain in working memory, because there are no longer any rules to clean
1330 * them up. However, ANY time new rules are loaded, this rule will trigger
1331 * a clean-up of ALL Params, regardless of their name & yaml, thus removing
1332 * any that no longer have associated rules.
1333 * This has a higher salience so that we immediately check Params when the
1334 * rules change, before processing any events.
1337 rule "${policyName}.PARAMS.CHECKUP"
1340 Params( $clName: closedLoopControlName, $yaml: controlLoopYaml )
1343 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1344 logger.info("{}: {} : YAML=[{}]", $clName, drools.getRule().getName(), $yaml);
1346 ParamsCleaner cleaner = new ParamsCleaner();
1347 cleaner.setClosedLoopControlName($clName);
1348 cleaner.setControlLoopYaml($yaml);
1355 * This rule removes "cleaner" objects for rules that are still active, thus
1356 * preventing the associated Params objects from being removed. Any cleaners
1357 * that are left after this rule has fired will cause their corresponding Params
1359 * This has a higher salience so that we discard the cleaner before it has
1360 * a chance to force the removal of the associated Params.
1363 rule "${policyName}.CLEANER.ACTIVE"
1366 $cleaner: ParamsCleaner( getClosedLoopControlName() == "${closedLoopControlName}", getControlLoopYaml() == "${controlLoopYaml}" )
1369 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1370 logger.info("{}: {} : YAML=[{}]", $cleaner.getClosedLoopControlName(), drools.getRule().getName(), $cleaner.getControlLoopYaml());
1377 * This rule removes Params objects that no longer have associated rules; if a
1378 * Params still had associated rules, then the cleaner would have been removed
1379 * by those rules and thus this rule would not fire.
1380 * This has a higher salience so that we remove old Params before it causes any
1381 * events to be processed.
1384 rule "${policyName}.PARAMS.CLEANUP"
1387 $params: Params( $clName: closedLoopControlName, $yaml: controlLoopYaml )
1388 ParamsCleaner( getClosedLoopControlName() == $clName, getControlLoopYaml() == $yaml )
1391 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1392 logger.info("{}: {} : YAML=[{}]", $params.getClosedLoopControlName(), drools.getRule().getName(), $params.getControlLoopYaml());
1396 // Note: the cleaner may be needed for cleaning additional params, thus
1397 // we do not retract it here - we'll leave that to another rule
1402 * This rule removes "cleaner" objects when they're no longer needed.
1405 rule "${policyName}.CLEANER.CLEANUP"
1407 $cleaner: ParamsCleaner( )
1410 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1411 logger.info("{}: {} : YAML=[{}]", $cleaner.getClosedLoopControlName(), drools.getRule().getName(), $cleaner.getControlLoopYaml());