2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.appc.Request;
36 import org.onap.policy.appc.Response;
37 import org.onap.policy.appc.CommonHeader;
38 import org.onap.policy.appclcm.LCMRequestWrapper;
39 import org.onap.policy.appclcm.LCMResponseWrapper;
40 import org.onap.policy.appclcm.LCMRequest;
41 import org.onap.policy.appclcm.LCMResponse;
42 import org.onap.policy.appclcm.LCMCommonHeader;
43 import org.onap.policy.vfc.VFCRequest;
44 import org.onap.policy.vfc.VFCManager;
45 import org.onap.policy.so.SOManager;
46 import org.onap.policy.so.SORequest;
47 import org.onap.policy.so.SORequestStatus;
48 import org.onap.policy.so.SORequestDetails;
49 import org.onap.policy.so.SOModelInfo;
50 import org.onap.policy.so.SOCloudConfiguration;
51 import org.onap.policy.so.SORequestInfo;
52 import org.onap.policy.so.SORequestParameters;
53 import org.onap.policy.so.SORelatedInstanceListElement;
54 import org.onap.policy.so.SORelatedInstance;
55 import org.onap.policy.so.SOResponse;
56 import org.onap.policy.guard.PolicyGuard;
57 import org.onap.policy.guard.PolicyGuard.LockResult;
58 import org.onap.policy.guard.TargetLock;
59 import org.onap.policy.guard.GuardResult;
60 import org.onap.policy.guard.PolicyGuardRequest;
61 import org.onap.policy.guard.PolicyGuardResponse;
62 import org.onap.policy.guard.PolicyGuardXacmlRequestAttributes;
63 import org.onap.policy.guard.PolicyGuardXacmlHelper;
65 import org.yaml.snakeyaml.Yaml;
66 import org.yaml.snakeyaml.constructor.Constructor;
68 import org.slf4j.LoggerFactory;
69 import org.slf4j.Logger;
71 import java.time.Instant;
72 import java.util.LinkedList;
73 import java.util.Iterator;
75 import org.onap.policy.drools.system.PolicyEngine;
78 closedLoopControlName : String
79 controlLoopYaml : String
83 declare OperationTimer
84 closedLoopControlName : String
89 declare ControlLoopTimer
90 closedLoopControlName : String
97 * Called once and only once to insert the parameters into working memory for this Closed Loop policy.
100 rule "${policyName}.SETUP"
104 Params params = new Params();
105 params.setClosedLoopControlName("${closedLoopControlName}");
106 params.setControlLoopYaml("${controlLoopYaml}");
109 // Note: globals have bad behavior when persistence is used,
110 // hence explicitly getting the logger vs using a global
112 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
113 logger.info("{}: {} : YAML=[{}]", params.getClosedLoopControlName(), drools.getRule().getName(), params.getControlLoopYaml());
115 String sqlDbUsername = PolicyEngine.manager.getEnvironmentProperty("sql.db.username");
116 String sqlDbPassword = PolicyEngine.manager.getEnvironmentProperty("sql.db.password");
118 String aaiUrl = PolicyEngine.manager.getEnvironmentProperty("aai.url");
119 String aaiUsername = PolicyEngine.manager.getEnvironmentProperty("aai.username");
120 String aaiPassword = PolicyEngine.manager.getEnvironmentProperty("aai.password");
122 String soUrl =PolicyEngine.manager.getEnvironmentProperty("so.url");
123 String soUsername = PolicyEngine.manager.getEnvironmentProperty("so.username");
124 String soPassword = PolicyEngine.manager.getEnvironmentProperty("so.password");
126 String vfcUrl =PolicyEngine.manager.getEnvironmentProperty("vfc.url");
127 String vfcUsername = PolicyEngine.manager.getEnvironmentProperty("vfc.username");
128 String vfcPassword = PolicyEngine.manager.getEnvironmentProperty("vfc.password");
130 String guardUrl = PolicyEngine.manager.getEnvironmentProperty("guard.url");
131 String guardUsername = PolicyEngine.manager.getEnvironmentProperty("guard.username");
132 String guardPassword = PolicyEngine.manager.getEnvironmentProperty("guard.password");
133 String guardJdbcUrl = PolicyEngine.manager.getEnvironmentProperty("guard.jdbc.url");
135 logger.info("{}: {} : AAI=[{}:{}]", params.getClosedLoopControlName(), drools.getRule().getName(), aaiUrl, aaiUsername);
136 logger.info("{}: {} : SO=[{}:{}]", params.getClosedLoopControlName(), drools.getRule().getName(), soUrl, soUsername);
137 logger.info("{}: {} : VFC=[{}:{}]", params.getClosedLoopControlName(), drools.getRule().getName(), vfcUrl, vfcUsername);
138 logger.info("{}: {} : GUARD=[{}:{}:{}]", params.getClosedLoopControlName(), drools.getRule().getName(), guardUrl, guardUsername, guardJdbcUrl);
139 logger.info("{}: {} : DB=[{}:{}]", params.getClosedLoopControlName(), drools.getRule().getName(), sqlDbUsername, sqlDbPassword);
144 * This rule responds to DCAE Events where there is no manager yet. Either it is
145 * the first ONSET, or a subsequent badly formed Event (i.e. Syntax error, or is-closed-loop-disabled)
148 rule "${policyName}.EVENT"
150 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
151 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
152 not ( ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID ) )
155 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
156 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
161 // Check the event, because we need it to not be null when
162 // we create the ControlLoopEventManager. The ControlLoopEventManager
163 // will do extra syntax checking as well check if the closed loop is disabled.
165 if ($event.requestID == null) {
166 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
167 notification.notification = ControlLoopNotificationType.REJECTED;
168 notification.from = "policy";
169 notification.message = "Missing requestID";
170 notification.policyName = drools.getRule().getName();
171 notification.policyScope = "${policyScope}";
172 notification.policyVersion = "${policyVersion}";
175 // Let interested parties know
177 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
180 // Retract it from memory
185 // Create an EventManager
187 ControlLoopEventManager manager = new ControlLoopEventManager($params.getClosedLoopControlName(), $event.requestID);
189 // Determine if EventManager can actively process the event (i.e. syntax, is_closed_loop_disabled checks etc.)
191 VirtualControlLoopNotification notification = manager.activate($params.getControlLoopYaml(), $event);
192 notification.from = "pdp-0001-controller=controlloop"; // Engine.getInstanceName()
193 notification.policyName = drools.getRule().getName();
194 notification.policyScope = "${policyScope}";
195 notification.policyVersion = "${policyVersion}";
197 // Are we actively pursuing this event?
199 if (notification.notification == ControlLoopNotificationType.ACTIVE) {
201 // Insert Event Manager into memory, this will now kick off processing.
205 // Let interested parties know
207 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
209 // Setup the Overall Control Loop timer
211 ControlLoopTimer clTimer = new ControlLoopTimer();
212 clTimer.setClosedLoopControlName($event.closedLoopControlName);
213 clTimer.setRequestID($event.requestID.toString());
214 clTimer.setDelay(manager.getControlLoopTimeout(1500) + "s");
221 // Let interested parties know
223 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
225 // Retract it from memory
231 // Now that the manager is inserted into Drools working memory, we'll wait for
232 // another rule to fire in order to continue processing. This way we can also
233 // then screen for additional ONSET and ABATED events for this RequestID.
236 } catch (Exception e) {
237 logger.warn("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName(), e);
239 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
240 notification.notification = ControlLoopNotificationType.REJECTED;
241 notification.message = "Exception occurred " + e.getMessage();
242 notification.policyName = drools.getRule().getName();
243 notification.policyScope = "${policyScope}";
244 notification.policyVersion = "${policyVersion}";
248 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
258 * This rule happens when we got a valid ONSET, closed loop is enabled and an Event Manager
259 * is now created. We can start processing the yaml specification via the Event Manager.
262 rule "${policyName}.EVENT.MANAGER"
264 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
265 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
266 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
267 $clTimer : ControlLoopTimer ( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
270 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
271 logger.info("{}: {}: event={} manager={} clTimer={}",
272 $params.getClosedLoopControlName(), drools.getRule().getName(),
273 $event, $manager, $clTimer);
275 // Check which event this is.
277 ControlLoopEventManager.NEW_EVENT_STATUS eventStatus = $manager.onNewEvent($event);
280 // Check what kind of event this is
282 if (eventStatus == NEW_EVENT_STATUS.SUBSEQUENT_ONSET) {
284 // We don't care about subsequent onsets
286 logger.info("{}: {}: subsequent onset",
287 $params.getClosedLoopControlName(), drools.getRule().getName());
291 if (eventStatus == NEW_EVENT_STATUS.SYNTAX_ERROR) {
293 // Ignore any bad syntax events
295 logger.warn("{}: {}: syntax error",
296 $params.getClosedLoopControlName(), drools.getRule().getName());
301 // We only want the initial ONSET event in memory,
302 // all the other events need to be retracted to support
303 // cleanup and avoid the other rules being fired for this event.
305 if (eventStatus != NEW_EVENT_STATUS.FIRST_ONSET) {
306 logger.warn("{}: {}: no first onset",
307 $params.getClosedLoopControlName(), drools.getRule().getName());
311 logger.debug("{}: {}: target={}", $params.getClosedLoopControlName(),
312 drools.getRule().getName(), $event.target);
314 // Now start seeing if we need to process this event
318 // Check if this is a Final Event
320 VirtualControlLoopNotification notification = $manager.isControlLoopFinal();
323 if (notification != null) {
325 // Its final, but are we waiting for abatement?
327 if ($manager.getNumAbatements() > 0) {
328 logger.info("{}: {}: abatement received for {}. Closing the control loop",
329 $params.getClosedLoopControlName(), drools.getRule().getName(),
331 notification.from = "policy";
332 notification.policyName = drools.getRule().getName();
333 notification.policyScope = "${policyScope}";
334 notification.policyVersion = "${policyVersion}";
336 // In this case, we are done
338 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
342 TargetLock lock = $manager.unlockCurrentOperation();
344 logger.debug("{}: {}: retracting lock=", $params.getClosedLoopControlName(),
345 drools.getRule().getName(), lock);
349 // Retract everything from memory
351 logger.info("{}: {}: retracting onset, manager, and timer",
352 $params.getClosedLoopControlName(), drools.getRule().getName());
354 retract($manager.getOnsetEvent());
358 // TODO - what if we get subsequent Events for this RequestID?
359 // By default, it will all start over again. May be confusing for Ruby.
360 // Or, we could track this and then subsequently ignore the events
364 // Check whether we need to wait for abatement
366 if ($manager.getProcessor().getControlLoop().getAbatement() == true && notification.notification == ControlLoopNotificationType.FINAL_SUCCESS) {
367 logger.info("{}: {}: waiting for abatement ..",
368 $params.getClosedLoopControlName(), drools.getRule().getName());
370 logger.info("{}: {}: no abatement expect for {}. Closing the control loop",
371 $params.getClosedLoopControlName(), drools.getRule().getName(),
374 notification.from = "policy";
375 notification.policyName = drools.getRule().getName();
376 notification.policyScope = "${policyScope}";
377 notification.policyVersion = "${policyVersion}";
380 // In this case, we are done
382 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
386 TargetLock lock = $manager.unlockCurrentOperation();
388 logger.debug("{}: {}: retracting lock=", $params.getClosedLoopControlName(),
389 drools.getRule().getName(), lock);
393 // Retract everything from memory
395 logger.info("{}: {}: retracting onset, manager, and timer",
396 $params.getClosedLoopControlName(), drools.getRule().getName());
398 retract($manager.getOnsetEvent());
405 // NOT final, so let's ask for the next operation
407 ControlLoopOperationManager operation = $manager.processControlLoop();
408 if (operation != null) {
409 logger.info("{}: {}: starting operation={}",
410 $params.getClosedLoopControlName(), drools.getRule().getName(),
413 // insert into memory
417 // insert operation timeout object
419 OperationTimer opTimer = new OperationTimer();
420 opTimer.setClosedLoopControlName($event.closedLoopControlName);
421 opTimer.setRequestID($event.requestID.toString());
422 opTimer.setDelay(operation.getOperationTimeout().toString() + "s");
426 // Let's ask for a lock right away
428 LockResult<GuardResult, TargetLock> result = $manager.lockCurrentOperation();
429 if (result.getA().equals(GuardResult.LOCK_ACQUIRED)) {
430 logger.info("{}: {}: guard lock acquired={}",
431 $params.getClosedLoopControlName(), drools.getRule().getName(),
435 // Insert into memory
437 insert(result.getB());
441 // Probably waiting for abatement
443 logger.info("{}: {}: no operation, probably waiting for abatement",
444 $params.getClosedLoopControlName(), drools.getRule().getName());
447 } catch (Exception e) {
448 logger.warn("{}: {}: unexpected",
449 $params.getClosedLoopControlName(),
450 drools.getRule().getName(), e);
453 // TODO should we abort if we get an exception?
466 rule "${policyName}.EVENT.MANAGER.OPERATION.NOT_LOCKED.TIMEOUT"
469 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
470 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
471 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
472 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
473 not ( TargetLock (requestID == $event.requestID) )
476 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
477 logger.info("{}: {}: event={} manager={} operation={}",
478 $params.getClosedLoopControlName(), drools.getRule().getName(),
479 $event, $manager, $operation);
482 // Need to ask for a Lock
484 LockResult<GuardResult, TargetLock> result = $manager.lockCurrentOperation();
485 if (result.getA().equals(GuardResult.LOCK_ACQUIRED)) {
486 logger.info("{}: {}: guard lock acquired={}",
487 $params.getClosedLoopControlName(), drools.getRule().getName(),
491 // Insert into memory
493 insert(result.getB());
500 * Guard Permitted, let's send request to the actor.
503 rule "${policyName}.EVENT.MANAGER.OPERATION.LOCKED.GUARD_PERMITTED"
505 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
506 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
507 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
508 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID, getGuardApprovalStatus() == "Permit" )
509 $lock : TargetLock (requestID == $event.requestID)
512 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
513 logger.info("{}: {}: event={} manager={} operation={} lock={}",
514 $params.getClosedLoopControlName(), drools.getRule().getName(),
515 $event, $manager, $operation, $lock);
517 Object request = $operation.getOperationRequest();
519 if (request != null) {
520 logger.debug("{}: {}: starting operation ..",
521 $params.getClosedLoopControlName(), drools.getRule().getName());
523 // Tell interested parties we are performing this Operation
525 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
526 notification.notification = ControlLoopNotificationType.OPERATION;
527 notification.message = $operation.getOperationMessage();
528 notification.history = $operation.getHistory();
529 notification.from = "policy";
530 notification.policyName = drools.getRule().getName();
531 notification.policyScope = "${policyScope}";
532 notification.policyVersion = "${policyVersion}";
534 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
536 switch ($operation.policy.getActor()){
540 if (request instanceof Request) {
541 PolicyEngine.manager.deliver("APPC-CL", request);
543 else if (request instanceof LCMRequestWrapper) {
544 PolicyEngine.manager.deliver("APPC-LCM-READ", request);
548 // at this point the AAI named query request should have already been made, the response recieved and used
549 // in the construction of the SO Request which is stored in operationRequest
551 if(request instanceof SORequest) {
552 // Call SO. The response will be inserted into memory once it's received
553 SOActorServiceProvider.sendRequest(drools.getWorkingMemory(), request);
557 if (request instanceof VFCRequest) {
559 VFCManager vfcManager = new VFCManager((VFCRequest) request);
560 vfcManager.setVFCParams(
561 PolicyEngine.manager.getEnvironmentProperty("vfc.url"),
562 PolicyEngine.manager.getEnvironmentProperty("vfc.user"),
563 PolicyEngine.manager.getEnvironmentProperty("vfc.password"));
565 Thread t = new Thread(vfcManager);
572 // What happens if its null?
574 logger.warn("{}: {}: unexpected null operation request",
575 $params.getClosedLoopControlName(),
576 drools.getRule().getName());
583 * We were able to acquire a lock so now let's ask Xacml Guard whether
584 * we are allowed to proceed with the request to the actor.
587 rule "${policyName}.EVENT.MANAGER.OPERATION.LOCKED.GUARD_NOT_YET_QUERIED"
589 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
590 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
591 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
592 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID, getGuardApprovalStatus() == "NONE" )
593 $lock : TargetLock (requestID == $event.requestID)
596 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
597 logger.info("{}: {}: event={} manager={} operation={} lock={}",
598 $params.getClosedLoopControlName(), drools.getRule().getName(),
599 $event, $manager, $operation, $lock);
602 // We are starting the operation but the actor won't be contacted until Guard is queried and permitted.
604 $operation.startOperation($event);
607 // Sending notification that we are about to query Guard ("DB write - start operation")
609 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
610 notification.notification = ControlLoopNotificationType.OPERATION;
611 notification.message = $operation.getOperationMessage();
612 notification.history = $operation.getHistory();
613 notification.from = "policy";
614 notification.policyName = drools.getRule().getName();
615 notification.policyScope = "${policyScope}";
616 notification.policyVersion = "${policyVersion}";
618 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
621 // Now send Guard Request to XACML Guard. In order to bypass the call to Guard,
622 // just change guardEnabled to false.
624 // In order to use REST XACML, provide a URL instead of "" as a second argument
625 // to the CallGuardTask() and set the first argument to null
626 // (instead of XacmlPdpEngine).
628 boolean guardEnabled = false;
632 Thread t = new Thread(new org.onap.policy.guard.CallGuardTask(
633 PolicyEngine.manager.getEnvironmentProperty("guard.url"),
634 drools.getWorkingMemory(),
635 $event.closedLoopControlName,
636 $operation.policy.getActor().toString(),
637 $operation.policy.getRecipe(),
638 $manager.getTargetInstance($operation.policy),
640 $event.requestID.toString()
645 insert(new PolicyGuardResponse("Permit", $event.requestID, $operation.policy.getRecipe()));
651 // This rule will be triggered when a thread talking to the XACML Guard inserts a
652 // guardResponse object into the working memory
654 rule "${policyName}.GUARD.RESPONSE"
656 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
657 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
658 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
659 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
660 $lock : TargetLock (requestID == $event.requestID)
661 $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
662 $guardResponse : PolicyGuardResponse(requestID == $event.requestID, $operation.policy.recipe == operation)
665 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
666 logger.info("{}: {}: event={} manager={} operation={} lock={} opTimer={} guardResponse={}",
667 $params.getClosedLoopControlName(), drools.getRule().getName(),
668 $event, $manager, $operation, $lock, $opTimer, $guardResponse);
671 //we will permit the operation if there was no Guard for it
672 if("Indeterminate".equals($guardResponse.result)){
673 $guardResponse.result = "Permit";
677 // This notification has Guard result in "message". ("DB write - end operation in case of Guard Deny")
679 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
680 notification.notification = ControlLoopNotificationType.OPERATION;
681 notification.message = $operation.getOperationMessage($guardResponse.result);
682 notification.history = $operation.getHistory();
683 notification.from = "policy";
684 notification.policyName = drools.getRule().getName();
685 notification.policyScope = "${policyScope}";
686 notification.policyVersion = "${policyVersion}";
688 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
690 if("Permit".equals($guardResponse.result)){
692 modify($operation){setGuardApprovalStatus($guardResponse.result)};
695 //This is the Deny case
696 $operation.setOperationHasGuardDeny();
699 modify($manager) {finishOperation($operation)};
702 retract($guardResponse);
708 * This rule responds to APPC Response Events
710 * I would have like to be consistent and write the Response like this:
711 * $response : Response( CommonHeader.RequestID == $onset.requestID )
713 * However, no compile error was given. But a runtime error was given. I think
714 * because drools is confused between the classname CommonHeader vs the property CommonHeader.
717 rule "${policyName}.APPC.RESPONSE"
719 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
720 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
721 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
722 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
723 $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
724 $lock : TargetLock (requestID == $event.requestID)
725 $response : Response( getCommonHeader().RequestID == $event.requestID )
728 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
729 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
730 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
731 $params.getClosedLoopControlName(), drools.getRule().getName(),
732 $event, $manager, $operation, $lock, $opTimer, $response);
734 // Get the result of the operation
736 PolicyResult policyResult = $operation.onResponse($response);
737 if (policyResult != null) {
738 logger.debug("{}: {}: operation finished - result={}",
739 $params.getClosedLoopControlName(), drools.getRule().getName(),
742 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
744 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
745 notification.from = "policy";
746 notification.policyName = drools.getRule().getName();
747 notification.policyScope = "${policyScope}";
748 notification.policyVersion = "${policyVersion}";
749 notification.message = $operation.getOperationHistory();
750 notification.history = $operation.getHistory();
751 if (policyResult.equals(PolicyResult.SUCCESS)) {
752 notification.notification = ControlLoopNotificationType.OPERATION_SUCCESS;
754 // Let interested parties know
756 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
758 notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
760 // Let interested parties know
762 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
765 // Ensure the operation is complete
767 if ($operation.isOperationComplete() == true) {
769 // It is complete, remove it from memory
773 // We must also retract the timer object
774 // NOTE: We could write a Rule to do this
778 // Complete the operation
780 modify($manager) {finishOperation($operation)};
783 // Just doing this will kick off the LOCKED rule again
785 modify($operation) {};
789 // Its not finished yet (i.e. expecting more Response objects)
791 // Or possibly it is a leftover response that we timed the request out previously
795 // We are going to retract these objects from memory
802 * The problem with Responses is that they don't have a controlLoopControlName
803 * field in them, so the only way to attach them is via RequestID. If we have multiple
804 * control loop .drl's loaded in the same container, we need to be sure the cleanup
805 * rules don't remove Responses for other control loops.
808 rule "${policyName}.APPC.RESPONSE.CLEANUP"
810 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
811 $response : Response($id : getCommonHeader().RequestID )
812 not ( VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), requestID == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
815 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
816 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
817 logger.debug("{}: {}: orphan appc response={}",
818 $params.getClosedLoopControlName(), drools.getRule().getName(), $id);
828 * This rule responds to APPC Response Events using the new LCM interface provided by appc
831 rule "${policyName}.APPC.LCM.RESPONSE"
833 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
834 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
835 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
836 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
837 $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
838 $lock : TargetLock (requestID == $event.requestID)
839 $response : LCMResponseWrapper( getBody().getCommonHeader().getRequestId() == $event.requestID )
842 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
843 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
844 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
845 $params.getClosedLoopControlName(), drools.getRule().getName(),
846 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
849 // Get the result of the operation
851 PolicyResult policyResult = $operation.onResponse($response);
852 if (policyResult != null) {
853 logger.debug("{}: {}: operation finished - result={}",
854 $params.getClosedLoopControlName(), drools.getRule().getName(),
858 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
860 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
861 notification.from = "policy";
862 notification.policyName = drools.getRule().getName();
863 notification.policyScope = "${policyScope}";
864 notification.policyVersion = "${policyVersion}";
865 notification.message = $operation.getOperationHistory();
866 notification.history = $operation.getHistory();
867 if (policyResult.equals(PolicyResult.SUCCESS)) {
868 notification.notification = ControlLoopNotificationType.OPERATION_SUCCESS;
870 notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
872 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
874 // Ensure the operation is complete
876 if ($operation.isOperationComplete() == true) {
878 // It is complete, remove it from memory
882 // We must also retract the timer object
883 // NOTE: We could write a Rule to do this
887 // Complete the operation
889 modify($manager) {finishOperation($operation)};
892 // Just doing this will kick off the LOCKED rule again
894 modify($operation) {};
898 // Its not finished yet (i.e. expecting more Response objects)
900 // Or possibly it is a leftover response that we timed the request out previously
904 // We are going to retract these objects from memory
911 * Clean Up any lingering LCM reponses
914 rule "${policyName}.APPC.LCM.RESPONSE.CLEANUP"
916 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
917 $response : LCMResponseWrapper($id : getBody().getCommonHeader().getRequestId )
918 not ( VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), requestID == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
921 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
922 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
923 logger.debug("{}: {}: orphan appc response={}",
924 $params.getClosedLoopControlName(), drools.getRule().getName(), $id);
933 * This rule responds to SO Response Events
936 rule "${policyName}.SO.RESPONSE"
938 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
939 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
940 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName )
941 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
942 $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
943 $lock : TargetLock (requestID == $event.requestID)
944 $response : SOResponse( request.requestId == $event.requestID.toString() )
947 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
948 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
949 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
950 $params.getClosedLoopControlName(), drools.getRule().getName(),
951 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
953 // Get the result of the operation
955 PolicyResult policyResult = $operation.onResponse($response);
956 if (policyResult != null) {
957 logger.debug("{}: {}: operation finished - result={}",
958 $params.getClosedLoopControlName(), drools.getRule().getName(),
962 // This Operation has completed, construct a notification showing our results
964 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
965 notification.from = "policy";
966 notification.policyName = drools.getRule().getName();
967 notification.policyScope = "${policyScope}";
968 notification.policyVersion = "${policyVersion}";
969 notification.message = $operation.getOperationHistory();
970 notification.history = $operation.getHistory();
971 if (policyResult.equals(PolicyResult.SUCCESS)) {
972 notification.notification = ControlLoopNotificationType.OPERATION_SUCCESS;
974 notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
977 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
979 // Ensure the operation is complete
981 if ($operation.isOperationComplete() == true) {
983 // It is complete, remove it from memory
987 // We must also retract the timer object
988 // NOTE: We could write a Rule to do this
992 // Complete the operation
994 modify($manager) {finishOperation($operation)};
997 // Just doing this will kick off the LOCKED rule again
999 modify($operation) {};
1003 // Its not finished yet (i.e. expecting more Response objects)
1005 // Or possibly it is a leftover response that we timed the request out previously
1009 // We are going to retract these objects from memory
1017 * This is the timer that manages the timeout for an individual operation.
1020 rule "${policyName}.EVENT.MANAGER.OPERATION.TIMEOUT"
1023 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1024 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
1025 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
1026 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
1027 $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString(), $to : getDelay() )
1028 $lock : TargetLock (requestID == $event.requestID)
1031 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1032 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1033 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={}",
1034 $params.getClosedLoopControlName(), drools.getRule().getName(),
1035 $event, $manager, $operation, $lock, $operation, $opTimer);
1038 // Tell it its timed out
1040 $operation.setOperationHasTimedOut();
1042 // Create a notification for it ("DB Write - end operation")
1044 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1045 notification.from = "policy";
1046 notification.policyName = drools.getRule().getName();
1047 notification.policyScope = "${policyScope}";
1048 notification.policyVersion = "${policyVersion}";
1049 notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
1050 notification.message = $operation.getOperationHistory();
1051 notification.history = $operation.getHistory();
1053 // Let interested parties know
1055 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1057 // Get rid of the timer
1061 // Ensure the operation is complete
1063 if ($operation.isOperationComplete() == true) {
1065 // It is complete, remove it from memory
1067 retract($operation);
1069 // Complete the operation
1071 modify($manager) {finishOperation($operation)};
1074 // Just doing this will kick off the LOCKED rule again
1076 modify($operation) {};
1082 * This is the timer that manages the overall control loop timeout.
1085 rule "${policyName}.EVENT.MANAGER.TIMEOUT"
1088 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1089 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
1090 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
1091 $clTimer : ControlLoopTimer ( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString(), $to : getDelay() )
1092 $operations : LinkedList()
1093 from collect( ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID ) )
1094 $opTimers : LinkedList()
1095 from collect( OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() ) )
1096 $locks : LinkedList()
1097 from collect( TargetLock (requestID == $event.requestID) )
1100 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1101 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1103 if ($operations == null) {
1104 logger.debug("{}: {}: event={} manager={} clTimer={} operations=0",
1105 $params.getClosedLoopControlName(), drools.getRule().getName(),
1106 $event, $manager, $clTimer);
1108 logger.debug("{}: {}: event={} manager={} clTimer={} operations={}",
1109 $params.getClosedLoopControlName(), drools.getRule().getName(),
1110 $event, $manager, $clTimer, $operations.size());
1113 // Tell the Event Manager it has timed out
1115 VirtualControlLoopNotification notification = $manager.setControlLoopTimedOut();
1116 if (notification != null) {
1117 notification.from = "policy";
1118 notification.policyName = drools.getRule().getName();
1119 notification.policyScope = "${policyScope}";
1120 notification.policyVersion = "${policyVersion}";
1122 // Let interested parties know
1124 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1127 // Retract EVERYTHING
1132 if ($operations != null && $operations.size() > 0) {
1133 Iterator<ControlLoopOperationManager> iter = $operations.iterator();
1134 while (iter.hasNext()) {
1135 ControlLoopOperationManager manager = iter.next();
1139 if ($opTimers != null && $opTimers.size() > 0) {
1140 Iterator<OperationTimer> iter = $opTimers.iterator();
1141 while (iter.hasNext()) {
1142 OperationTimer opTimer = iter.next();
1146 if ($locks != null && $locks.size() > 0) {
1147 Iterator<TargetLock> iter = $locks.iterator();
1148 while (iter.hasNext()) {
1149 TargetLock lock = iter.next();
1151 // Ensure we release the lock
1153 PolicyGuard.unlockTarget(lock);