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.sdnr.PciRequestWrapper;
45 import org.onap.policy.sdnr.PciResponseWrapper;
46 import org.onap.policy.sdnr.PciRequest;
47 import org.onap.policy.sdnr.PciResponse;
48 import org.onap.policy.vfc.VFCRequest;
49 import org.onap.policy.vfc.VFCResponse;
50 import org.onap.policy.vfc.VFCManager;
51 import org.onap.policy.so.SOManager;
52 import org.onap.policy.so.SORequest;
53 import org.onap.policy.so.SORequestStatus;
54 import org.onap.policy.so.SORequestDetails;
55 import org.onap.policy.so.SOModelInfo;
56 import org.onap.policy.so.SOCloudConfiguration;
57 import org.onap.policy.so.SORequestInfo;
58 import org.onap.policy.so.SORequestParameters;
59 import org.onap.policy.so.SORelatedInstanceListElement;
60 import org.onap.policy.so.SORelatedInstance;
61 import org.onap.policy.so.SOResponse;
62 import org.onap.policy.so.SOResponseWrapper;
63 import org.onap.policy.sdnc.SdncRequest;
64 import org.onap.policy.sdnc.SdncManager;
65 import org.onap.policy.sdnc.SdncResponse;
66 import org.onap.policy.guard.PolicyGuard;
67 import org.onap.policy.guard.PolicyGuard.LockResult;
68 import org.onap.policy.guard.TargetLock;
69 import org.onap.policy.guard.GuardResult;
70 import org.onap.policy.guard.PolicyGuardRequest;
71 import org.onap.policy.guard.PolicyGuardResponse;
72 import org.onap.policy.guard.PolicyGuardXacmlRequestAttributes;
73 import org.onap.policy.guard.PolicyGuardXacmlHelper;
75 import org.yaml.snakeyaml.Yaml;
76 import org.yaml.snakeyaml.constructor.Constructor;
78 import org.slf4j.LoggerFactory;
79 import org.slf4j.Logger;
81 import java.time.Instant;
82 import java.util.LinkedList;
83 import java.util.Iterator;
85 import org.onap.policy.drools.system.PolicyEngine;
88 * This structure mimics the Params structure.
89 * Its only purpose is to allow management of
90 * rules by the PAP component..
91 * It has no use at runtime since the rules go by
92 * Params for matching purposes.
95 closedLoopControlName : String
96 controlLoopYaml : String
100 * Control Loop Identity
103 closedLoopControlName : String
104 controlLoopYaml : String
108 * Used to clean up Params that no longer have associated rules.
110 declare ParamsCleaner
111 closedLoopControlName : String
112 controlLoopYaml : String
116 * This object is to provide support for timeouts
117 * due to a bug in drools' built-in timers
119 declare ControlLoopTimer
120 closedLoopControlName : String
124 //timerType is the type of timer: either "ClosedLoop" or "Operation"
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}",
139 getControlLoopYaml() == "${controlLoopYaml}" ) )
142 Params params = new Params();
143 params.setClosedLoopControlName("${closedLoopControlName}");
144 params.setControlLoopYaml("${controlLoopYaml}");
147 // Note: globals have bad behavior when persistence is used,
148 // hence explicitly getting the logger vs using a global
150 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
151 logger.info("{}: {} : YAML=[{}]", params.getClosedLoopControlName(), drools.getRule().getName(),
152 params.getControlLoopYaml());
157 * This rule responds to DCAE Events where there is no manager yet. Either it is
158 * the first ONSET, or a subsequent badly formed Event (i.e. Syntax error, or is-closed-loop-disabled)
161 rule "${policyName}.EVENT"
163 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
164 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
165 not ( ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
166 requestID == $event.getRequestId() ) )
169 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
170 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
175 // Check the event, because we need it to not be null when
176 // we create the ControlLoopEventManager. The ControlLoopEventManager
177 // will do extra syntax checking as well check if the closed loop is disabled.
179 if ($event.getRequestId() == null) {
180 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
181 notification.setNotification(ControlLoopNotificationType.REJECTED);
182 notification.setFrom("policy");
183 notification.setMessage("Missing requestId");
184 notification.setPolicyName(drools.getRule().getName());
185 notification.setPolicyScope("${policyScope}");
186 notification.setPolicyVersion("${policyVersion}");
189 // Let interested parties know
191 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
194 // Retract it from memory
197 } else if ($event.getClosedLoopEventStatus() != ControlLoopEventStatus.ONSET) {
198 throw new ControlLoopException($event.getClosedLoopEventStatus() + " received with no prior onset");
201 // Create an EventManager
203 ControlLoopEventManager manager = new ControlLoopEventManager($params.getClosedLoopControlName(),
204 $event.getRequestId());
206 // Determine if EventManager can actively process the event
207 // (i.e. syntax, is_closed_loop_disabled checks etc.)
209 VirtualControlLoopNotification notification = manager.activate($params.getControlLoopYaml(), $event);
210 notification.setFrom("pdp-0001-controller=controlloop"); // Engine.getInstanceName()
211 notification.setPolicyName(drools.getRule().getName());
212 notification.setPolicyScope("${policyScope}");
213 notification.setPolicyVersion("${policyVersion}");
215 // Are we actively pursuing this event?
217 if (notification.getNotification() == ControlLoopNotificationType.ACTIVE) {
219 // Insert Event Manager into memory, this will now kick off processing.
223 // Let interested parties know
225 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
227 // Setup the Overall Control Loop timer
229 ControlLoopTimer clTimer = new ControlLoopTimer();
230 clTimer.setTimerType("ClosedLoop");
231 clTimer.setClosedLoopControlName($event.getClosedLoopControlName());
232 clTimer.setRequestID($event.getRequestId().toString());
233 clTimer.setDelay(manager.getControlLoopTimeout(1500) + "s");
240 // Let interested parties know
242 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
244 // Retract it from memory
250 // Now that the manager is inserted into Drools working memory, we'll wait for
251 // another rule to fire in order to continue processing. This way we can also
252 // then screen for additional ONSET and ABATED events for this RequestId.
255 } catch (Exception e) {
256 logger.warn("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName(), e);
258 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
259 notification.setNotification(ControlLoopNotificationType.REJECTED);
260 notification.setMessage("Exception occurred: " + e.getMessage());
261 notification.setPolicyName(drools.getRule().getName());
262 notification.setPolicyScope("${policyScope}");
263 notification.setPolicyVersion("${policyVersion}");
267 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
277 * This rule happens when we got a valid ONSET, closed loop is enabled and an Event Manager
278 * is now created. We can start processing the yaml specification via the Event Manager.
281 rule "${policyName}.EVENT.MANAGER"
283 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
284 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
285 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
286 requestID == $event.getRequestId() )
287 $clTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
288 requestID == $event.getRequestId().toString(), timerType == "ClosedLoop", !expired )
291 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
292 logger.info("{}: {}: event={} manager={} clTimer={}",
293 $params.getClosedLoopControlName(), drools.getRule().getName(),
294 $event, $manager, $clTimer);
298 // Check which event this is.
300 ControlLoopEventManager.NEW_EVENT_STATUS eventStatus = $manager.onNewEvent($event);
302 // Check what kind of event this is
304 if (eventStatus == NEW_EVENT_STATUS.SUBSEQUENT_ONSET) {
306 // We don't care about subsequent onsets
308 logger.info("{}: {}: subsequent onset",
309 $params.getClosedLoopControlName(), drools.getRule().getName());
313 if (eventStatus == NEW_EVENT_STATUS.SYNTAX_ERROR) {
315 // Ignore any bad syntax events
317 logger.warn("{}: {}: syntax error",
318 $params.getClosedLoopControlName(), drools.getRule().getName());
323 // We only want the initial ONSET event in memory,
324 // all the other events need to be retracted to support
325 // cleanup and avoid the other rules being fired for this event.
327 if (eventStatus != NEW_EVENT_STATUS.FIRST_ONSET) {
328 logger.warn("{}: {}: not first onset",
329 $params.getClosedLoopControlName(), drools.getRule().getName());
333 logger.debug("{}: {}: target={}", $params.getClosedLoopControlName(),
334 drools.getRule().getName(), $event.getTarget());
336 // Now start seeing if we need to process this event
340 // Check if this is a Final Event
342 VirtualControlLoopNotification notification = $manager.isControlLoopFinal();
345 if (notification != null) {
347 // Its final, but are we waiting for abatement?
349 if ($manager.getNumAbatements() > 0) {
350 logger.info("{}: {}: abatement received for {}. Closing the control loop",
351 $params.getClosedLoopControlName(), drools.getRule().getName(),
352 $event.getRequestId());
354 /// DB Write---end event processing for this RequestId()
355 $manager.commitAbatement("Event Abated","Closed");
357 notification.setFrom("policy");
358 notification.setPolicyName(drools.getRule().getName());
359 notification.setPolicyScope("${policyScope}");
360 notification.setPolicyVersion("${policyVersion}");
362 // In this case, we are done
364 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
368 TargetLock lock = $manager.unlockCurrentOperation();
370 logger.debug("{}: {}: retracting lock=", $params.getClosedLoopControlName(),
371 drools.getRule().getName(), lock);
375 // Retract everything from memory
377 logger.info("{}: {}: retracting onset, manager, and timer",
378 $params.getClosedLoopControlName(), drools.getRule().getName());
380 retract($manager.getOnsetEvent());
384 // TODO - what if we get subsequent Events for this RequestId?
385 // By default, it will all start over again. May be confusing for Ruby.
386 // Or, we could track this and then subsequently ignore the events
390 // Check whether we need to wait for abatement
392 if ($manager.getProcessor().getControlLoop().getAbatement() == true && notification.getNotification() == ControlLoopNotificationType.FINAL_SUCCESS) {
393 logger.info("{}: {}: waiting for abatement ..",
394 $params.getClosedLoopControlName(), drools.getRule().getName());
396 logger.info("{}: {}: no abatement expect for {}. Closing the control loop",
397 $params.getClosedLoopControlName(), drools.getRule().getName(),
398 $event.getRequestId());
400 notification.setFrom("policy");
401 notification.setPolicyName(drools.getRule().getName());
402 notification.setPolicyScope("${policyScope}");
403 notification.setPolicyVersion("${policyVersion}");
406 // In this case, we are done
408 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
412 TargetLock lock = $manager.unlockCurrentOperation();
414 logger.debug("{}: {}: retracting lock=", $params.getClosedLoopControlName(),
415 drools.getRule().getName(), lock);
419 // Retract everything from memory
421 logger.info("{}: {}: retracting onset, manager, and timer",
422 $params.getClosedLoopControlName(), drools.getRule().getName());
424 retract($manager.getOnsetEvent());
431 // NOT final, so let's ask for the next operation
433 ControlLoopOperationManager operation = $manager.processControlLoop();
434 if (operation != null) {
436 // Let's ask for a lock right away
438 LockResult<GuardResult, TargetLock> result = $manager.lockCurrentOperation();
439 logger.info("{}: {}: guard lock acquired={}",
440 $params.getClosedLoopControlName(), drools.getRule().getName(),
442 if (result.getA().equals(GuardResult.LOCK_ACQUIRED)) {
444 // insert the operation into memory
448 // insert operation timeout object
450 ControlLoopTimer opTimer = new ControlLoopTimer();
451 opTimer.setTimerType("Operation");
452 opTimer.setClosedLoopControlName($event.getClosedLoopControlName());
453 opTimer.setRequestID($event.getRequestId().toString());
454 opTimer.setDelay(operation.getOperationTimeout().toString() + "s");
458 // Insert lock into memory
460 insert(result.getB());
462 logger.debug("The target resource {} is already processing",
463 $event.getAai().get($event.getTarget()));
464 notification = new VirtualControlLoopNotification($event);
465 notification.setNotification(ControlLoopNotificationType.REJECTED);
466 notification.setMessage("The target " + $event.getAai().get($event.getTarget())
467 + " is already locked");
468 notification.setFrom("policy");
469 notification.setPolicyName(drools.getRule().getName());
470 notification.setPolicyScope("${policyScope}");
471 notification.setPolicyVersion("${policyVersion}");
473 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
479 if (result.getB() != null) {
480 retract(result.getB());
483 logger.info("{}: {}: starting operation={}",
484 $params.getClosedLoopControlName(), drools.getRule().getName(),
488 // Probably waiting for abatement
490 logger.info("{}: {}: no operation, probably waiting for abatement",
491 $params.getClosedLoopControlName(), drools.getRule().getName());
494 } catch (Exception e) {
495 logger.warn("{}: {}: unexpected",
496 $params.getClosedLoopControlName(),
497 drools.getRule().getName(), e);
499 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
500 notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
501 notification.setMessage(e.getMessage());
502 notification.setFrom("policy");
503 notification.setPolicyName(drools.getRule().getName());
504 notification.setPolicyScope("${policyScope}");
505 notification.setPolicyVersion("${policyVersion}");
507 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
518 * Guard Permitted, let's send request to the actor.
521 rule "${policyName}.EVENT.MANAGER.OPERATION.LOCKED.GUARD_PERMITTED"
523 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
524 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
525 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
526 requestID == $event.getRequestId() )
527 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
528 onset.getRequestId() == $event.getRequestId(), "Permit".equalsIgnoreCase(getGuardApprovalStatus()) )
529 $lock : TargetLock (requestID == $event.getRequestId())
530 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
531 requestID == $event.getRequestId().toString(), timerType == "Operation", !expired )
534 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
535 logger.info("{}: {}: event={} manager={} operation={} lock={}",
536 $params.getClosedLoopControlName(), drools.getRule().getName(),
537 $event, $manager, $operation, $lock);
539 Object request = null;
540 boolean caughtException = false;
543 request = $operation.startOperation($event);
545 if (request != null) {
546 logger.debug("{}: {}: starting operation ..",
547 $params.getClosedLoopControlName(), drools.getRule().getName());
549 // Tell interested parties we are performing this Operation
551 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
552 notification.setNotification(ControlLoopNotificationType.OPERATION);
553 notification.setMessage($operation.getOperationMessage());
554 notification.setHistory($operation.getHistory());
555 notification.setFrom("policy");
556 notification.setPolicyName(drools.getRule().getName());
557 notification.setPolicyScope("${policyScope}");
558 notification.setPolicyVersion("${policyVersion}");
560 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
562 switch ($operation.policy.getActor()){
565 if (request instanceof Request) {
566 PolicyEngine.manager.deliver("APPC-CL", request);
568 else if (request instanceof LcmRequestWrapper) {
569 PolicyEngine.manager.deliver("APPC-LCM-READ", request);
573 // at this point the AAI named query request should have already been made,
574 // the response recieved and used
575 // in the construction of the SO Request which is stored in operationRequest
577 if(request instanceof SORequest) {
578 // Call SO. The response will be inserted into memory once it's received
579 SoActorServiceProvider.sendRequest($event.getRequestId().toString(), drools.getWorkingMemory(),
584 if (request instanceof VFCRequest) {
586 Thread t = new Thread(new VFCManager(drools.getWorkingMemory(), (VFCRequest)request));
592 if (request instanceof SdncRequest) {
594 Thread t = new Thread(new SdncManager(drools.getWorkingMemory(), (SdncRequest)request));
599 if (request instanceof PciRequestWrapper) {
600 PolicyEngine.manager.deliver("SDNR-CL", request);
606 // What happens if its null?
608 logger.warn("{}: {}: unexpected null operation request",
609 $params.getClosedLoopControlName(),
610 drools.getRule().getName());
611 if ("SO".equals($operation.policy.getActor())) {
614 modify($manager) {finishOperation($operation)};
616 else if ("vfc".equalsIgnoreCase($operation.policy.getActor())) {
619 modify($manager) {finishOperation($operation)};
621 else if ("sdnc".equalsIgnoreCase($operation.policy.getActor())) {
624 modify($manager) {finishOperation($operation)};
628 } catch (Exception e) {
629 String msg = e.getMessage();
630 logger.warn("{}: {}: operation={}: AAI failure: {}",
631 $params.getClosedLoopControlName(), drools.getRule().getName(),
633 $operation.setOperationHasException(msg);
635 if(request != null) {
637 // Create a notification for it ("DB Write - end operation")
639 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
640 notification.setFrom("policy");
641 notification.setPolicyName(drools.getRule().getName());
642 notification.setPolicyScope("${policyScope}");
643 notification.setPolicyVersion("${policyVersion}");
644 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
645 notification.setMessage($operation.getOperationHistory());
646 notification.setHistory($operation.getHistory());
648 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
653 caughtException = true;
656 // Having the modify statement in the catch clause doesn't work for whatever reason
657 if (caughtException) {
658 modify($manager) {finishOperation($operation)};
665 * We were able to acquire a lock so now let's ask Xacml Guard whether
666 * we are allowed to proceed with the request to the actor.
669 rule "${policyName}.EVENT.MANAGER.OPERATION.LOCKED.GUARD_NOT_YET_QUERIED"
671 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
672 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
673 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
674 requestID == $event.getRequestId() )
675 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
676 onset.getRequestId() == $event.getRequestId(), getGuardApprovalStatus() == "NONE" )
677 $lock : TargetLock (requestID == $event.getRequestId())
680 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
681 logger.info("{}: {}: event={} manager={} operation={} lock={}",
682 $params.getClosedLoopControlName(), drools.getRule().getName(),
683 $event, $manager, $operation, $lock);
686 // Sending notification that we are about to query Guard ("DB write - start operation")
688 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
689 notification.setNotification(ControlLoopNotificationType.OPERATION);
690 notification.setMessage("Sending guard query for " + $operation.policy.getActor() + " "
691 + $operation.policy.getRecipe());
692 notification.setHistory($operation.getHistory());
693 notification.setFrom("policy");
694 notification.setPolicyName(drools.getRule().getName());
695 notification.setPolicyScope("${policyScope}");
696 notification.setPolicyVersion("${policyVersion}");
698 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
701 // Now send Guard Request to XACML Guard. In order to bypass the call to Guard,
702 // just change guardEnabled to false.
704 // In order to use REST XACML, provide a URL instead of "" as a second argument
705 // to the CallGuardTask() and set the first argument to null
706 // (instead of XacmlPdpEngine).
709 // NOTE: The environment properties uses "guard.disabled" but the boolean is guardEnabled
710 boolean guardEnabled = "false".equalsIgnoreCase(PolicyEngine.manager.getEnvironmentProperty("guard.disabled"));
714 Thread t = new Thread(new org.onap.policy.guard.CallGuardTask(
715 drools.getWorkingMemory(),
716 $event.getClosedLoopControlName(),
717 $operation.policy.getActor().toString(),
718 $operation.policy.getRecipe(),
719 $operation.getTargetEntity(),
720 $event.getRequestId().toString(),
722 AaiNqResponseWrapper resp = $manager.getNqVserverFromAai();
723 return(resp == null ? null : resp.countVfModules());
728 insert(new PolicyGuardResponse("Permit", $event.getRequestId(), $operation.policy.getRecipe()));
734 // This rule will be triggered when a thread talking to the XACML Guard inserts a
735 // guardResponse object into the working memory
737 rule "${policyName}.GUARD.RESPONSE"
739 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
740 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
741 closedLoopEventStatus == ControlLoopEventStatus.ONSET )
742 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
743 requestID == $event.getRequestId() )
744 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
745 onset.getRequestId() == $event.getRequestId() )
746 $lock : TargetLock (requestID == $event.getRequestId())
747 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
748 requestID == $event.getRequestId().toString(), timerType == "Operation", !expired )
749 $guardResponse : PolicyGuardResponse(requestID == $event.getRequestId(), $operation.policy.recipe == operation)
752 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
753 logger.info("{}: {}: event={} manager={} operation={} lock={} opTimer={} guardResponse={}",
754 $params.getClosedLoopControlName(), drools.getRule().getName(),
755 $event, $manager, $operation, $lock, $opTimer, $guardResponse);
758 //we will permit the operation if there was no Guard for it
759 if("Indeterminate".equalsIgnoreCase($guardResponse.getResult())){
760 $guardResponse.setResult("Permit");
764 // This notification has Guard result in "message". ("DB write - end operation in case of Guard Deny")
766 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
767 notification.setNotification(ControlLoopNotificationType.OPERATION);
768 notification.setMessage("Guard result for " + $operation.policy.getActor() + " " + $operation.policy.getRecipe()
769 + " is " + $guardResponse.getResult());
770 notification.setHistory($operation.getHistory());
771 notification.setFrom("policy");
772 notification.setPolicyName(drools.getRule().getName());
773 notification.setPolicyScope("${policyScope}");
774 notification.setPolicyVersion("${policyVersion}");
776 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
778 if("Permit".equalsIgnoreCase($guardResponse.getResult())){
780 modify($operation){setGuardApprovalStatus($guardResponse.getResult())};
783 //This is the Deny case
784 $operation.startOperation($event);
785 $operation.setOperationHasGuardDeny();
788 modify($manager) {finishOperation($operation)};
791 retract($guardResponse);
797 * This rule responds to APPC Response Events
799 * I would have like to be consistent and write the Response like this:
800 * $response : Response( CommonHeader.RequestId == $onset.getRequestId() )
802 * However, no compile error was given. But a runtime error was given. I think
803 * because drools is confused between the classname CommonHeader vs the property CommonHeader.
806 rule "${policyName}.APPC.RESPONSE"
808 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
809 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
810 closedLoopEventStatus == ControlLoopEventStatus.ONSET )
811 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
812 requestID == $event.getRequestId() )
813 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
814 onset.getRequestId() == $event.getRequestId() )
815 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
816 requestID == $event.getRequestId().toString(), timerType == "Operation", !expired )
817 $lock : TargetLock (requestID == $event.getRequestId())
818 $response : Response( getCommonHeader().RequestId == $event.getRequestId() )
821 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
822 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
823 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
824 $params.getClosedLoopControlName(), drools.getRule().getName(),
825 $event, $manager, $operation, $lock, $opTimer, $response);
827 // Get the result of the operation
829 PolicyResult policyResult = $operation.onResponse($response);
830 if (policyResult != null) {
831 logger.debug("{}: {}: operation finished - result={}",
832 $params.getClosedLoopControlName(), drools.getRule().getName(),
835 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
837 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
838 notification.setFrom("policy");
839 notification.setPolicyName(drools.getRule().getName());
840 notification.setPolicyScope("${policyScope}");
841 notification.setPolicyVersion("${policyVersion}");
842 notification.setMessage($operation.getOperationHistory());
843 notification.setHistory($operation.getHistory());
844 if (policyResult.equals(PolicyResult.SUCCESS)) {
845 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
847 // Let interested parties know
849 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
851 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
853 // Let interested parties know
855 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
858 // Ensure the operation is complete
860 if ($operation.isOperationComplete() == true) {
862 // It is complete, remove it from memory
866 // We must also retract the timer object
867 // NOTE: We could write a Rule to do this
871 // Complete the operation
873 modify($manager) {finishOperation($operation)};
876 // Just doing this will kick off the LOCKED rule again
878 modify($operation) {};
882 // Its not finished yet (i.e. expecting more Response objects)
884 // Or possibly it is a leftover response that we timed the request out previously
888 // We are going to retract these objects from memory
895 * The problem with Responses is that they don't have a controlLoopControlName
896 * field in them, so the only way to attach them is via RequestId. If we have multiple
897 * control loop .drl's loaded in the same container, we need to be sure the cleanup
898 * rules don't remove Responses for other control loops.
901 rule "${policyName}.APPC.RESPONSE.CLEANUP"
903 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
904 $response : Response($id : getCommonHeader().RequestId )
905 not ( VirtualControlLoopEvent( requestId == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
908 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
909 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
910 logger.debug("{}: {}: orphan appc response={}",
911 $params.getClosedLoopControlName(), drools.getRule().getName(), $id);
921 * This rule responds to APPC Response Events using the new LCM interface provided by appc
924 rule "${policyName}.APPC.LCM.RESPONSE"
926 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
927 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
928 closedLoopEventStatus == ControlLoopEventStatus.ONSET )
929 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
930 requestID == $event.getRequestId() )
931 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
932 onset.getRequestId() == $event.getRequestId() )
933 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
934 requestID == $event.getRequestId().toString(), timerType == "Operation", !expired )
935 $lock : TargetLock (requestID == $event.getRequestId())
936 $response : LcmResponseWrapper( getBody().getCommonHeader().getRequestId() == $event.getRequestId() )
939 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
940 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
941 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
942 $params.getClosedLoopControlName(), drools.getRule().getName(),
943 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
946 // Get the result of the operation
948 PolicyResult policyResult = $operation.onResponse($response);
949 if (policyResult != null) {
950 logger.debug("{}: {}: operation finished - result={}",
951 $params.getClosedLoopControlName(), drools.getRule().getName(),
955 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
957 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
958 notification.setFrom("policy");
959 notification.setPolicyName(drools.getRule().getName());
960 notification.setPolicyScope("${policyScope}");
961 notification.setPolicyVersion("${policyVersion}");
962 notification.setMessage($operation.getOperationHistory());
963 notification.setHistory($operation.getHistory());
964 if (policyResult.equals(PolicyResult.SUCCESS)) {
965 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
967 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
969 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
971 // Ensure the operation is complete
973 if ($operation.isOperationComplete() == true) {
975 // It is complete, remove it from memory
979 // We must also retract the timer object
980 // NOTE: We could write a Rule to do this
984 // Complete the operation
986 modify($manager) {finishOperation($operation)};
989 // Just doing this will kick off the LOCKED rule again
991 modify($operation) {};
995 // Its not finished yet (i.e. expecting more Response objects)
997 // Or possibly it is a leftover response that we timed the request out previously
1001 // We are going to retract these objects from memory
1008 * Clean Up any lingering LCM reponses
1011 rule "${policyName}.APPC.LCM.RESPONSE.CLEANUP"
1013 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1014 $response : LcmResponseWrapper($id : getBody().getCommonHeader().getRequestId )
1015 not ( VirtualControlLoopEvent( requestId == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
1018 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1019 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1020 logger.debug("{}: {}: orphan appc response={}",
1021 $params.getClosedLoopControlName(), drools.getRule().getName(), $id);
1030 * This rule responds to SDNR Response Events using the new interface provided by SDNR
1033 rule "${policyName}.SDNR.RESPONSE"
1035 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1036 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
1037 closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1038 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1039 requestID == $event.getRequestId() )
1040 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
1041 onset.getRequestId() == $event.getRequestId() )
1042 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1043 requestID == $event.getRequestId().toString(), timerType == "Operation", !expired )
1044 $lock : TargetLock (requestID == $event.getRequestId())
1045 $response : PciResponseWrapper( getBody().getCommonHeader().getRequestId() == $event.getRequestId() )
1048 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1049 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1050 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
1051 $params.getClosedLoopControlName(), drools.getRule().getName(),
1052 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
1055 // Get the result of the operation
1057 PolicyResult policyResult = $operation.onResponse($response);
1058 if (policyResult != null) {
1059 logger.debug("{}: {}: operation finished - result={}",
1060 $params.getClosedLoopControlName(), drools.getRule().getName(),
1064 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
1066 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1067 notification.setFrom("policy");
1068 notification.setPolicyName(drools.getRule().getName());
1069 notification.setPolicyScope("${policyScope}");
1070 notification.setPolicyVersion("${policyVersion}");
1071 notification.setMessage($operation.getOperationHistory());
1072 notification.setHistory($operation.getHistory());
1073 if (policyResult.equals(PolicyResult.SUCCESS)) {
1074 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
1076 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
1078 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1080 // Ensure the operation is complete
1082 if ($operation.isOperationComplete()) {
1084 // It is complete, remove it from memory
1086 retract($operation);
1088 // We must also retract the timer object
1089 // NOTE: We could write a Rule to do this
1093 // Complete the operation
1095 modify($manager) {finishOperation($operation)};
1098 // Just doing this will kick off the LOCKED rule again
1100 modify($operation) {};
1104 // Its not finished yet (i.e. expecting more Response objects)
1106 // Or possibly it is a leftover response that we timed the request out previously
1110 // We are going to retract these objects from memory
1117 * Clean Up any lingering SDNR reponses
1120 rule "${policyName}.SDNR.RESPONSE.CLEANUP"
1122 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1123 $response : PciResponseWrapper($id : getBody().getCommonHeader().getRequestId )
1124 not ( VirtualControlLoopEvent( requestId == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
1127 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1128 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1129 logger.debug("{}: {}: orphan SDNR response={}",
1130 $params.getClosedLoopControlName(), drools.getRule().getName(), $id);
1139 * This rule responds to SO Response Events
1142 rule "${policyName}.SO.RESPONSE"
1144 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1145 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
1146 closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1147 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1148 requestID == $event.getRequestId() )
1149 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
1150 onset.getRequestId() == $event.getRequestId() )
1151 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1152 requestID == $event.getRequestId().toString(), timerType == "Operation", !expired )
1153 $lock : TargetLock (requestID == $event.getRequestId())
1154 $response : SOResponseWrapper(requestID.toString() == $event.getRequestId().toString() )
1157 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1158 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1159 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
1160 $params.getClosedLoopControlName(), drools.getRule().getName(),
1161 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
1163 // Get the result of the operation
1165 PolicyResult policyResult = $operation.onResponse($response);
1166 if (policyResult != null) {
1167 logger.debug("{}: {}: operation finished - result={}",
1168 $params.getClosedLoopControlName(), drools.getRule().getName(),
1172 // This Operation has completed, construct a notification showing our results
1174 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1175 notification.setFrom("policy");
1176 notification.setPolicyName(drools.getRule().getName());
1177 notification.setPolicyScope("${policyScope}");
1178 notification.setPolicyVersion("${policyVersion}");
1179 notification.setMessage($operation.getOperationHistory());
1180 notification.setHistory($operation.getHistory());
1181 if (policyResult.equals(PolicyResult.SUCCESS)) {
1182 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
1184 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
1187 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1189 // Ensure the operation is complete
1191 if ($operation.isOperationComplete() == true) {
1193 // It is complete, remove it from memory
1195 retract($operation);
1197 // We must also retract the timer object
1198 // NOTE: We could write a Rule to do this
1202 // Complete the operation
1204 modify($manager) {finishOperation($operation)};
1207 // Just doing this will kick off the LOCKED rule again
1209 modify($operation) {};
1213 // Its not finished yet (i.e. expecting more Response objects)
1215 // Or possibly it is a leftover response that we timed the request out previously
1219 // We are going to retract these objects from memory
1227 * This rule responds to VFC Response Events
1230 rule "${policyName}.VFC.RESPONSE"
1232 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1233 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
1234 closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1235 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1236 requestID == $event.getRequestId() )
1237 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
1238 onset.getRequestId() == $event.getRequestId() )
1239 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1240 requestID == $event.getRequestId().toString(), timerType == "Operation", !expired )
1241 $lock : TargetLock (requestID == $event.getRequestId())
1242 $response : VFCResponse( requestId.toString() == $event.getRequestId().toString() )
1244 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1245 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1246 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
1247 $params.getClosedLoopControlName(), drools.getRule().getName(),
1248 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
1250 // Get the result of the operation
1252 PolicyResult policyResult = $operation.onResponse($response);
1253 if (policyResult != null) {
1255 // This Operation has completed, construct a notification showing our results
1257 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1258 notification.setFrom("policy");
1259 notification.setPolicyName(drools.getRule().getName());
1260 notification.setPolicyScope("${policyScope}");
1261 notification.setPolicyVersion("${policyVersion}");
1262 notification.setMessage($operation.getOperationHistory());
1263 notification.setHistory($operation.getHistory());
1265 // Ensure the operation is complete
1267 if ($operation.isOperationComplete() == true) {
1269 // It is complete, remove it from memory
1271 retract($operation);
1273 // We must also retract the timer object
1274 // NOTE: We could write a Rule to do this
1278 // Complete the operation
1280 modify($manager) {finishOperation($operation)};
1283 // Just doing this will kick off the LOCKED rule again
1285 modify($operation) {};
1289 // Its not finished yet (i.e. expecting more Response objects)
1291 // Or possibly it is a leftover response that we timed the request out previously
1295 // We are going to retract these objects from memory
1303 * This rule responds to SDNC Response Events
1307 rule "${policyName}.SDNC.RESPONSE"
1309 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1310 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
1311 closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1312 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1313 requestID == $event.getRequestId() )
1314 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
1315 onset.getRequestId() == $event.getRequestId() )
1316 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1317 requestID == $event.getRequestId().toString(), timerType == "Operation", !expired )
1318 $lock : TargetLock (requestID == $event.getRequestId())
1319 $response : SdncResponse( requestId.toString() == $event.getRequestId().toString() )
1321 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1322 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1323 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
1324 $params.getClosedLoopControlName(), drools.getRule().getName(),
1325 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
1327 // Get the result of the operation
1329 PolicyResult policyResult = $operation.onResponse($response);
1330 if (policyResult != null) {
1332 // This Operation has completed, construct a notification showing our results
1334 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1335 notification.setFrom("policy");
1336 notification.setPolicyName(drools.getRule().getName());
1337 notification.setPolicyScope("${policyScope}");
1338 notification.setPolicyVersion("${policyVersion}");
1339 notification.setMessage($operation.getOperationHistory());
1340 notification.setHistory($operation.getHistory());
1342 // Ensure the operation is complete
1344 if ($operation.isOperationComplete()) {
1346 // It is complete, remove it from memory
1348 retract($operation);
1350 // We must also retract the timer object
1351 // NOTE: We could write a Rule to do this
1355 // Complete the operation
1357 modify($manager) {finishOperation($operation)};
1360 // Just doing this will kick off the LOCKED rule again
1362 modify($operation) {};
1366 // Its not finished yet (i.e. expecting more Response objects)
1368 // Or possibly it is a leftover response that we timed the request out previously
1372 // We are going to retract these objects from memory
1380 * This manages a single timer.
1381 * Due to a bug in the drools code, the drools timer needed to be split from most of the objects in the when clause
1384 rule "${policyName}.TIMER.FIRED"
1385 timer (expr: $timeout)
1387 $timer : ControlLoopTimer($timeout : delay, !expired)
1389 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1390 logger.info("This is ${policyName}.TIMER.FIRED");
1391 modify($timer){setExpired(true)};
1396 * This is the timer that manages the timeout for an individual operation.
1399 rule "${policyName}.EVENT.MANAGER.OPERATION.TIMEOUT"
1401 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1402 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
1403 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1404 requestID == $event.getRequestId() )
1405 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
1406 onset.getRequestId() == $event.getRequestId() )
1407 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1408 requestID == $event.getRequestId().toString(), expired, timerType == "Operation" )
1409 $lock : TargetLock (requestID == $event.getRequestId())
1412 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1413 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1414 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={}",
1415 $params.getClosedLoopControlName(), drools.getRule().getName(),
1416 $event, $manager, $operation, $lock, $operation, $opTimer);
1419 // Tell it its timed out
1421 $operation.setOperationHasTimedOut();
1423 // Create a notification for it ("DB Write - end operation")
1425 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1426 notification.setFrom("policy");
1427 notification.setPolicyName(drools.getRule().getName());
1428 notification.setPolicyScope("${policyScope}");
1429 notification.setPolicyVersion("${policyVersion}");
1430 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
1431 notification.setMessage($operation.getOperationHistory());
1432 notification.setHistory($operation.getHistory());
1434 // Let interested parties know
1436 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1438 // Get rid of the timer
1442 // Ensure the operation is complete
1444 if ($operation.isOperationComplete() == true) {
1446 // It is complete, remove it from memory
1448 retract($operation);
1450 // Complete the operation
1452 modify($manager) {finishOperation($operation)};
1455 // Just doing this will kick off the LOCKED rule again
1457 modify($operation) {};
1463 * This is the timer that manages the overall control loop timeout.
1466 rule "${policyName}.EVENT.MANAGER.TIMEOUT"
1468 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1469 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
1470 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1471 requestID == $event.getRequestId() )
1472 $clTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1473 requestID == $event.getRequestId().toString(), expired, timerType == "ClosedLoop" )
1476 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1477 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1479 logger.debug("{}: {}: event={}",
1480 $params.getClosedLoopControlName(), drools.getRule().getName(),
1483 // Tell the Event Manager it has timed out
1485 VirtualControlLoopNotification notification = $manager.setControlLoopTimedOut();
1486 if (notification != null) {
1487 notification.setFrom("policy");
1488 notification.setPolicyName(drools.getRule().getName());
1489 notification.setPolicyScope("${policyScope}");
1490 notification.setPolicyVersion("${policyVersion}");
1492 // Let interested parties know
1494 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1497 // Retract the event
1504 * This rule cleans up the manager and other objects after an event has
1508 rule "${policyName}.EVENT.MANAGER.CLEANUP"
1510 $manager : ControlLoopEventManager( $clName : getClosedLoopControlName(), $requestId : getRequestID() )
1511 $operations : LinkedList()
1512 from collect( ControlLoopOperationManager( onset.closedLoopControlName == $clName,
1513 onset.getRequestId() == $requestId ) )
1514 $timers : LinkedList()
1515 from collect( ControlLoopTimer( closedLoopControlName == $clName,
1516 requestID == $requestId.toString() ) )
1517 $locks : LinkedList()
1518 from collect( TargetLock (requestID == $requestId) )
1519 not( VirtualControlLoopEvent( closedLoopControlName == $clName, requestId == $requestId ) )
1522 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1523 logger.info("{}: {}", $clName, drools.getRule().getName());
1525 logger.debug("{}: {}: manager={} timers={} operations={}",
1526 $clName, drools.getRule().getName(),
1527 $manager, $timers.size(), $operations.size());
1530 // Retract EVERYTHING
1534 for(Object manager: $operations) {
1535 retract((ControlLoopOperationManager) manager);
1537 for(Object timer: $timers) {
1538 retract((ControlLoopTimer) timer);
1540 for(Object lock: $locks) {
1541 TargetLock tgt = (TargetLock) lock;
1543 // Ensure we release the lock
1545 PolicyGuard.unlockTarget(tgt);
1552 * This rule will clean up any rogue onsets where there is no
1553 * ControlLoopParams object corresponding to the onset event.
1556 rule "${policyName}.EVENT.CLEANUP"
1558 $event : VirtualControlLoopEvent( $clName: closedLoopControlName )
1559 not ( Params( getClosedLoopControlName() == $clName) )
1562 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1563 logger.info("{}: {}", $clName, drools.getRule().getName());
1564 logger.debug("{}: {}: orphan onset event={}",
1565 $clName, drools.getRule().getName(), $event);
1572 * When rules are deleted, the associated Params (and its subordinate objects)
1573 * remain in working memory, because there are no longer any rules to clean
1574 * them up. However, ANY time new rules are loaded, this rule will trigger
1575 * a clean-up of ALL Params, regardless of their name & yaml, thus removing
1576 * any that no longer have associated rules.
1577 * This has a higher salience so that we immediately check Params when the
1578 * rules change, before processing any events.
1581 rule "${policyName}.PARAMS.CHECKUP"
1584 Params( $clName: closedLoopControlName, $yaml: controlLoopYaml )
1587 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1588 logger.info("{}: {} : YAML=[{}]", $clName, drools.getRule().getName(), $yaml);
1590 ParamsCleaner cleaner = new ParamsCleaner();
1591 cleaner.setClosedLoopControlName($clName);
1592 cleaner.setControlLoopYaml($yaml);
1599 * This rule removes "cleaner" objects for rules that are still active, thus
1600 * preventing the associated Params objects from being removed. Any cleaners
1601 * that are left after this rule has fired will cause their corresponding Params
1603 * This has a higher salience so that we discard the cleaner before it has
1604 * a chance to force the removal of the associated Params.
1607 rule "${policyName}.CLEANER.ACTIVE"
1610 $cleaner: ParamsCleaner( getClosedLoopControlName() == "${closedLoopControlName}",
1611 getControlLoopYaml() == "${controlLoopYaml}" )
1614 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1615 logger.info("{}: {} : YAML=[{}]", $cleaner.getClosedLoopControlName(), drools.getRule().getName(),
1616 $cleaner.getControlLoopYaml());
1623 * This rule removes Params objects that no longer have associated rules; if a
1624 * Params still had associated rules, then the cleaner would have been removed
1625 * by those rules and thus this rule would not fire.
1626 * This has a higher salience so that we remove old Params before it causes any
1627 * events to be processed.
1630 rule "${policyName}.PARAMS.CLEANUP"
1633 $params: Params( $clName: closedLoopControlName, $yaml: controlLoopYaml )
1634 ParamsCleaner( getClosedLoopControlName() == $clName, getControlLoopYaml() == $yaml )
1637 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1638 logger.info("{}: {} : YAML=[{}]", $params.getClosedLoopControlName(), drools.getRule().getName(),
1639 $params.getControlLoopYaml());
1643 // Note: the cleaner may be needed for cleaning additional params, thus
1644 // we do not retract it here - we'll leave that to another rule
1649 * This rule removes "cleaner" objects when they're no longer needed.
1652 rule "${policyName}.CLEANER.CLEANUP"
1654 $cleaner: ParamsCleaner( )
1657 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1658 logger.info("{}: {} : YAML=[{}]", $cleaner.getClosedLoopControlName(), drools.getRule().getName(),
1659 $cleaner.getControlLoopYaml());