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.VFCResponse;
45 import org.onap.policy.vfc.VFCManager;
46 import org.onap.policy.so.SOManager;
47 import org.onap.policy.so.SORequest;
48 import org.onap.policy.so.SORequestStatus;
49 import org.onap.policy.so.SORequestDetails;
50 import org.onap.policy.so.SOModelInfo;
51 import org.onap.policy.so.SOCloudConfiguration;
52 import org.onap.policy.so.SORequestInfo;
53 import org.onap.policy.so.SORequestParameters;
54 import org.onap.policy.so.SORelatedInstanceListElement;
55 import org.onap.policy.so.SORelatedInstance;
56 import org.onap.policy.so.SOResponse;
57 import org.onap.policy.guard.PolicyGuard;
58 import org.onap.policy.guard.PolicyGuard.LockResult;
59 import org.onap.policy.guard.TargetLock;
60 import org.onap.policy.guard.GuardResult;
61 import org.onap.policy.guard.PolicyGuardRequest;
62 import org.onap.policy.guard.PolicyGuardResponse;
63 import org.onap.policy.guard.PolicyGuardXacmlRequestAttributes;
64 import org.onap.policy.guard.PolicyGuardXacmlHelper;
66 import org.yaml.snakeyaml.Yaml;
67 import org.yaml.snakeyaml.constructor.Constructor;
69 import org.slf4j.LoggerFactory;
70 import org.slf4j.Logger;
72 import java.time.Instant;
73 import java.util.LinkedList;
74 import java.util.Iterator;
76 import org.onap.policy.drools.system.PolicyEngine;
79 * This structure mimics the Params structure.
80 * Its only purpose is to allow management of
81 * rules by the PAP component..
82 * It has no use at runtime since the rules go by
83 * Params for matching purposes.
86 closedLoopControlName : String
87 controlLoopYaml : String
91 * Control Loop Identity
94 closedLoopControlName : String
95 controlLoopYaml : String
102 declare OperationTimer
103 closedLoopControlName : String
111 declare ControlLoopTimer
112 closedLoopControlName : String
119 * Called once and only once to insert the parameters into working memory for this Closed Loop policy.
122 rule "${policyName}.SETUP"
126 Params params = new Params();
127 params.setClosedLoopControlName("${closedLoopControlName}");
128 params.setControlLoopYaml("${controlLoopYaml}");
131 // Note: globals have bad behavior when persistence is used,
132 // hence explicitly getting the logger vs using a global
134 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
135 logger.info("{}: {} : YAML=[{}]", params.getClosedLoopControlName(), drools.getRule().getName(), params.getControlLoopYaml());
137 String sqlDbUsername = PolicyEngine.manager.getEnvironmentProperty("sql.db.username");
139 String aaiUrl = PolicyEngine.manager.getEnvironmentProperty("aai.url");
140 String aaiUsername = PolicyEngine.manager.getEnvironmentProperty("aai.username");
141 String aaiPassword = PolicyEngine.manager.getEnvironmentProperty("aai.password");
143 String soUrl =PolicyEngine.manager.getEnvironmentProperty("so.url");
144 String soUsername = PolicyEngine.manager.getEnvironmentProperty("so.username");
145 String soPassword = PolicyEngine.manager.getEnvironmentProperty("so.password");
147 String vfcUrl =PolicyEngine.manager.getEnvironmentProperty("vfc.url");
148 String vfcUsername = PolicyEngine.manager.getEnvironmentProperty("vfc.username");
149 String vfcPassword = PolicyEngine.manager.getEnvironmentProperty("vfc.password");
151 String guardUrl = PolicyEngine.manager.getEnvironmentProperty("guard.url");
152 String guardUsername = PolicyEngine.manager.getEnvironmentProperty("pdpx.username");
153 String guardPassword = PolicyEngine.manager.getEnvironmentProperty("pdpx.password");
154 String guardJdbcUrl = PolicyEngine.manager.getEnvironmentProperty("guard.jdbc.url");
155 String guardDisabled = PolicyEngine.manager.getEnvironmentProperty("guard.disabled");
157 logger.info("{}: {} : AAI=[{}:{}]", params.getClosedLoopControlName(), drools.getRule().getName(), aaiUrl, aaiUsername);
158 logger.info("{}: {} : SO=[{}:{}]", params.getClosedLoopControlName(), drools.getRule().getName(), soUrl, soUsername);
159 logger.info("{}: {} : VFC=[{}:{}]", params.getClosedLoopControlName(), drools.getRule().getName(), vfcUrl, vfcUsername);
160 logger.info("{}: {} : GUARD=[{}:{}:{}:{}]", params.getClosedLoopControlName(), drools.getRule().getName(), guardUrl, guardUsername, guardJdbcUrl, guardDisabled);
161 logger.info("{}: {} : DB=[{}]", params.getClosedLoopControlName(), drools.getRule().getName(), sqlDbUsername);
166 * This rule responds to DCAE Events where there is no manager yet. Either it is
167 * the first ONSET, or a subsequent badly formed Event (i.e. Syntax error, or is-closed-loop-disabled)
170 rule "${policyName}.EVENT"
172 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
173 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
174 not ( ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID ) )
177 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
178 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
183 // Check the event, because we need it to not be null when
184 // we create the ControlLoopEventManager. The ControlLoopEventManager
185 // will do extra syntax checking as well check if the closed loop is disabled.
187 if ($event.requestID == null) {
188 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
189 notification.notification = ControlLoopNotificationType.REJECTED;
190 notification.from = "policy";
191 notification.message = "Missing requestID";
192 notification.policyName = drools.getRule().getName();
193 notification.policyScope = "${policyScope}";
194 notification.policyVersion = "${policyVersion}";
197 // Let interested parties know
199 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
202 // Retract it from memory
207 // Create an EventManager
209 ControlLoopEventManager manager = new ControlLoopEventManager($params.getClosedLoopControlName(), $event.requestID);
211 // Determine if EventManager can actively process the event (i.e. syntax, is_closed_loop_disabled checks etc.)
213 VirtualControlLoopNotification notification = manager.activate($params.getControlLoopYaml(), $event);
214 notification.from = "pdp-0001-controller=controlloop"; // Engine.getInstanceName()
215 notification.policyName = drools.getRule().getName();
216 notification.policyScope = "${policyScope}";
217 notification.policyVersion = "${policyVersion}";
219 // Are we actively pursuing this event?
221 if (notification.notification == ControlLoopNotificationType.ACTIVE) {
223 // Insert Event Manager into memory, this will now kick off processing.
227 // Let interested parties know
229 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
231 // Setup the Overall Control Loop timer
233 ControlLoopTimer clTimer = new ControlLoopTimer();
234 clTimer.setClosedLoopControlName($event.closedLoopControlName);
235 clTimer.setRequestID($event.requestID.toString());
236 clTimer.setDelay(manager.getControlLoopTimeout(1500) + "s");
243 // Let interested parties know
245 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
247 // Retract it from memory
253 // Now that the manager is inserted into Drools working memory, we'll wait for
254 // another rule to fire in order to continue processing. This way we can also
255 // then screen for additional ONSET and ABATED events for this RequestID.
258 } catch (Exception e) {
259 logger.warn("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName(), e);
261 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
262 notification.notification = ControlLoopNotificationType.REJECTED;
263 notification.message = "Exception occurred " + e.getMessage();
264 notification.policyName = drools.getRule().getName();
265 notification.policyScope = "${policyScope}";
266 notification.policyVersion = "${policyVersion}";
270 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
280 * This rule happens when we got a valid ONSET, closed loop is enabled and an Event Manager
281 * is now created. We can start processing the yaml specification via the Event Manager.
284 rule "${policyName}.EVENT.MANAGER"
286 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
287 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
288 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
289 $clTimer : ControlLoopTimer ( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
292 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
293 logger.info("{}: {}: event={} manager={} clTimer={}",
294 $params.getClosedLoopControlName(), drools.getRule().getName(),
295 $event, $manager, $clTimer);
297 // Check which event this is.
299 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("{}: {}: no first onset",
329 $params.getClosedLoopControlName(), drools.getRule().getName());
333 logger.debug("{}: {}: target={}", $params.getClosedLoopControlName(),
334 drools.getRule().getName(), $event.target);
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(),
353 notification.from = "policy";
354 notification.policyName = drools.getRule().getName();
355 notification.policyScope = "${policyScope}";
356 notification.policyVersion = "${policyVersion}";
358 // In this case, we are done
360 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
364 TargetLock lock = $manager.unlockCurrentOperation();
366 logger.debug("{}: {}: retracting lock=", $params.getClosedLoopControlName(),
367 drools.getRule().getName(), lock);
371 // Retract everything from memory
373 logger.info("{}: {}: retracting onset, manager, and timer",
374 $params.getClosedLoopControlName(), drools.getRule().getName());
376 retract($manager.getOnsetEvent());
380 // TODO - what if we get subsequent Events for this RequestID?
381 // By default, it will all start over again. May be confusing for Ruby.
382 // Or, we could track this and then subsequently ignore the events
386 // Check whether we need to wait for abatement
388 if ($manager.getProcessor().getControlLoop().getAbatement() == true && notification.notification == ControlLoopNotificationType.FINAL_SUCCESS) {
389 logger.info("{}: {}: waiting for abatement ..",
390 $params.getClosedLoopControlName(), drools.getRule().getName());
392 logger.info("{}: {}: no abatement expect for {}. Closing the control loop",
393 $params.getClosedLoopControlName(), drools.getRule().getName(),
396 notification.from = "policy";
397 notification.policyName = drools.getRule().getName();
398 notification.policyScope = "${policyScope}";
399 notification.policyVersion = "${policyVersion}";
402 // In this case, we are done
404 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
408 TargetLock lock = $manager.unlockCurrentOperation();
410 logger.debug("{}: {}: retracting lock=", $params.getClosedLoopControlName(),
411 drools.getRule().getName(), lock);
415 // Retract everything from memory
417 logger.info("{}: {}: retracting onset, manager, and timer",
418 $params.getClosedLoopControlName(), drools.getRule().getName());
420 retract($manager.getOnsetEvent());
427 // NOT final, so let's ask for the next operation
429 ControlLoopOperationManager operation = $manager.processControlLoop();
430 if (operation != null) {
431 logger.info("{}: {}: starting operation={}",
432 $params.getClosedLoopControlName(), drools.getRule().getName(),
435 // insert into memory
439 // insert operation timeout object
441 OperationTimer opTimer = new OperationTimer();
442 opTimer.setClosedLoopControlName($event.closedLoopControlName);
443 opTimer.setRequestID($event.requestID.toString());
444 opTimer.setDelay(operation.getOperationTimeout().toString() + "s");
448 // Let's ask for a lock right away
450 LockResult<GuardResult, TargetLock> result = $manager.lockCurrentOperation();
451 if (result.getA().equals(GuardResult.LOCK_ACQUIRED)) {
452 logger.info("{}: {}: guard lock acquired={}",
453 $params.getClosedLoopControlName(), drools.getRule().getName(),
457 // Insert into memory
459 insert(result.getB());
463 // Probably waiting for abatement
465 logger.info("{}: {}: no operation, probably waiting for abatement",
466 $params.getClosedLoopControlName(), drools.getRule().getName());
469 } catch (Exception e) {
470 logger.warn("{}: {}: unexpected",
471 $params.getClosedLoopControlName(),
472 drools.getRule().getName(), e);
475 // TODO should we abort if we get an exception?
488 rule "${policyName}.EVENT.MANAGER.OPERATION.NOT_LOCKED.TIMEOUT"
491 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
492 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
493 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
494 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
495 not ( TargetLock (requestID == $event.requestID) )
498 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
499 logger.info("{}: {}: event={} manager={} operation={}",
500 $params.getClosedLoopControlName(), drools.getRule().getName(),
501 $event, $manager, $operation);
504 // Need to ask for a Lock
506 LockResult<GuardResult, TargetLock> result = $manager.lockCurrentOperation();
507 if (result.getA().equals(GuardResult.LOCK_ACQUIRED)) {
508 logger.info("{}: {}: guard lock acquired={}",
509 $params.getClosedLoopControlName(), drools.getRule().getName(),
513 // Insert into memory
515 insert(result.getB());
522 * Guard Permitted, let's send request to the actor.
525 rule "${policyName}.EVENT.MANAGER.OPERATION.LOCKED.GUARD_PERMITTED"
527 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
528 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
529 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
530 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID, "Permit".equalsIgnoreCase(getGuardApprovalStatus()) )
531 $lock : TargetLock (requestID == $event.requestID)
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 = $operation.startOperation($event);
541 if (request != null) {
542 logger.debug("{}: {}: starting operation ..",
543 $params.getClosedLoopControlName(), drools.getRule().getName());
545 // Tell interested parties we are performing this Operation
547 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
548 notification.notification = ControlLoopNotificationType.OPERATION;
549 notification.message = $operation.getOperationMessage();
550 notification.history = $operation.getHistory();
551 notification.from = "policy";
552 notification.policyName = drools.getRule().getName();
553 notification.policyScope = "${policyScope}";
554 notification.policyVersion = "${policyVersion}";
556 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
558 switch ($operation.policy.getActor()){
562 if (request instanceof Request) {
563 PolicyEngine.manager.deliver("APPC-CL", request);
565 else if (request instanceof LCMRequestWrapper) {
566 PolicyEngine.manager.deliver("APPC-LCM-READ", request);
570 // at this point the AAI named query request should have already been made, the response recieved and used
571 // in the construction of the SO Request which is stored in operationRequest
573 if(request instanceof SORequest) {
574 // Call SO. The response will be inserted into memory once it's received
575 SOActorServiceProvider.sendRequest(drools.getWorkingMemory(), request);
579 if (request instanceof VFCRequest) {
581 Thread t = new Thread(new VFCManager(drools.getWorkingMemory(), (VFCRequest)request));
588 // What happens if its null?
590 logger.warn("{}: {}: unexpected null operation request",
591 $params.getClosedLoopControlName(),
592 drools.getRule().getName());
593 if ("SO".equals($operation.policy.getActor())) {
595 modify($manager) {finishOperation($operation)};
604 * We were able to acquire a lock so now let's ask Xacml Guard whether
605 * we are allowed to proceed with the request to the actor.
608 rule "${policyName}.EVENT.MANAGER.OPERATION.LOCKED.GUARD_NOT_YET_QUERIED"
610 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
611 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
612 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
613 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID, getGuardApprovalStatus() == "NONE" )
614 $lock : TargetLock (requestID == $event.requestID)
617 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
618 logger.info("{}: {}: event={} manager={} operation={} lock={}",
619 $params.getClosedLoopControlName(), drools.getRule().getName(),
620 $event, $manager, $operation, $lock);
623 // Sending notification that we are about to query Guard ("DB write - start operation")
625 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
626 notification.notification = ControlLoopNotificationType.OPERATION;
627 notification.message = "Sending guard query for " + $operation.policy.getActor() + " " + $operation.policy.getRecipe();
628 notification.history = $operation.getHistory();
629 notification.from = "policy";
630 notification.policyName = drools.getRule().getName();
631 notification.policyScope = "${policyScope}";
632 notification.policyVersion = "${policyVersion}";
634 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
637 // Now send Guard Request to XACML Guard. In order to bypass the call to Guard,
638 // just change guardEnabled to false.
640 // In order to use REST XACML, provide a URL instead of "" as a second argument
641 // to the CallGuardTask() and set the first argument to null
642 // (instead of XacmlPdpEngine).
645 // NOTE: The environment properties uses "guard.disabled" but the boolean is guardEnabled
646 boolean guardEnabled = "false".equalsIgnoreCase(PolicyEngine.manager.getEnvironmentProperty("guard.disabled"));
650 Thread t = new Thread(new org.onap.policy.guard.CallGuardTask(
651 drools.getWorkingMemory(),
652 $event.closedLoopControlName,
653 $operation.policy.getActor().toString(),
654 $operation.policy.getRecipe(),
655 $manager.getTargetInstance($operation.policy),
657 $event.requestID.toString()
662 insert(new PolicyGuardResponse("Permit", $event.requestID, $operation.policy.getRecipe()));
668 // This rule will be triggered when a thread talking to the XACML Guard inserts a
669 // guardResponse object into the working memory
671 rule "${policyName}.GUARD.RESPONSE"
673 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
674 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
675 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
676 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
677 $lock : TargetLock (requestID == $event.requestID)
678 $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
679 $guardResponse : PolicyGuardResponse(requestID == $event.requestID, $operation.policy.recipe == operation)
682 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
683 logger.info("{}: {}: event={} manager={} operation={} lock={} opTimer={} guardResponse={}",
684 $params.getClosedLoopControlName(), drools.getRule().getName(),
685 $event, $manager, $operation, $lock, $opTimer, $guardResponse);
688 //we will permit the operation if there was no Guard for it
689 if("Indeterminate".equalsIgnoreCase($guardResponse.result)){
690 $guardResponse.result = "Permit";
694 // This notification has Guard result in "message". ("DB write - end operation in case of Guard Deny")
696 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
697 notification.notification = ControlLoopNotificationType.OPERATION;
698 notification.message = "Guard result for " + $operation.policy.getActor() + " " + $operation.policy.getRecipe() + " is " + $guardResponse.result;
699 notification.history = $operation.getHistory();
700 notification.from = "policy";
701 notification.policyName = drools.getRule().getName();
702 notification.policyScope = "${policyScope}";
703 notification.policyVersion = "${policyVersion}";
705 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
707 if("Permit".equalsIgnoreCase($guardResponse.result)){
709 modify($operation){setGuardApprovalStatus($guardResponse.result)};
712 //This is the Deny case
713 $operation.setOperationHasGuardDeny();
716 modify($manager) {finishOperation($operation)};
719 retract($guardResponse);
725 * This rule responds to APPC Response Events
727 * I would have like to be consistent and write the Response like this:
728 * $response : Response( CommonHeader.RequestID == $onset.requestID )
730 * However, no compile error was given. But a runtime error was given. I think
731 * because drools is confused between the classname CommonHeader vs the property CommonHeader.
734 rule "${policyName}.APPC.RESPONSE"
736 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
737 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
738 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
739 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
740 $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
741 $lock : TargetLock (requestID == $event.requestID)
742 $response : Response( getCommonHeader().RequestID == $event.requestID )
745 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
746 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
747 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
748 $params.getClosedLoopControlName(), drools.getRule().getName(),
749 $event, $manager, $operation, $lock, $opTimer, $response);
751 // Get the result of the operation
753 PolicyResult policyResult = $operation.onResponse($response);
754 if (policyResult != null) {
755 logger.debug("{}: {}: operation finished - result={}",
756 $params.getClosedLoopControlName(), drools.getRule().getName(),
759 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
761 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
762 notification.from = "policy";
763 notification.policyName = drools.getRule().getName();
764 notification.policyScope = "${policyScope}";
765 notification.policyVersion = "${policyVersion}";
766 notification.message = $operation.getOperationHistory();
767 notification.history = $operation.getHistory();
768 if (policyResult.equals(PolicyResult.SUCCESS)) {
769 notification.notification = ControlLoopNotificationType.OPERATION_SUCCESS;
771 // Let interested parties know
773 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
775 notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
777 // Let interested parties know
779 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
782 // Ensure the operation is complete
784 if ($operation.isOperationComplete() == true) {
786 // It is complete, remove it from memory
790 // We must also retract the timer object
791 // NOTE: We could write a Rule to do this
795 // Complete the operation
797 modify($manager) {finishOperation($operation)};
800 // Just doing this will kick off the LOCKED rule again
802 modify($operation) {};
806 // Its not finished yet (i.e. expecting more Response objects)
808 // Or possibly it is a leftover response that we timed the request out previously
812 // We are going to retract these objects from memory
819 * The problem with Responses is that they don't have a controlLoopControlName
820 * field in them, so the only way to attach them is via RequestID. If we have multiple
821 * control loop .drl's loaded in the same container, we need to be sure the cleanup
822 * rules don't remove Responses for other control loops.
825 rule "${policyName}.APPC.RESPONSE.CLEANUP"
827 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
828 $response : Response($id : getCommonHeader().RequestID )
829 not ( VirtualControlLoopEvent( requestID == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
832 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
833 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
834 logger.debug("{}: {}: orphan appc response={}",
835 $params.getClosedLoopControlName(), drools.getRule().getName(), $id);
845 * This rule responds to APPC Response Events using the new LCM interface provided by appc
848 rule "${policyName}.APPC.LCM.RESPONSE"
850 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
851 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
852 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
853 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
854 $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
855 $lock : TargetLock (requestID == $event.requestID)
856 $response : LCMResponseWrapper( getBody().getCommonHeader().getRequestId() == $event.requestID )
859 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
860 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
861 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
862 $params.getClosedLoopControlName(), drools.getRule().getName(),
863 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
866 // Get the result of the operation
868 PolicyResult policyResult = $operation.onResponse($response);
869 if (policyResult != null) {
870 logger.debug("{}: {}: operation finished - result={}",
871 $params.getClosedLoopControlName(), drools.getRule().getName(),
875 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
877 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
878 notification.from = "policy";
879 notification.policyName = drools.getRule().getName();
880 notification.policyScope = "${policyScope}";
881 notification.policyVersion = "${policyVersion}";
882 notification.message = $operation.getOperationHistory();
883 notification.history = $operation.getHistory();
884 if (policyResult.equals(PolicyResult.SUCCESS)) {
885 notification.notification = ControlLoopNotificationType.OPERATION_SUCCESS;
887 notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
889 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
891 // Ensure the operation is complete
893 if ($operation.isOperationComplete() == true) {
895 // It is complete, remove it from memory
899 // We must also retract the timer object
900 // NOTE: We could write a Rule to do this
904 // Complete the operation
906 modify($manager) {finishOperation($operation)};
909 // Just doing this will kick off the LOCKED rule again
911 modify($operation) {};
915 // Its not finished yet (i.e. expecting more Response objects)
917 // Or possibly it is a leftover response that we timed the request out previously
921 // We are going to retract these objects from memory
928 * Clean Up any lingering LCM reponses
931 rule "${policyName}.APPC.LCM.RESPONSE.CLEANUP"
933 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
934 $response : LCMResponseWrapper($id : getBody().getCommonHeader().getRequestId )
935 not ( VirtualControlLoopEvent( requestID == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
938 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
939 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
940 logger.debug("{}: {}: orphan appc response={}",
941 $params.getClosedLoopControlName(), drools.getRule().getName(), $id);
950 * This rule responds to SO Response Events
953 rule "${policyName}.SO.RESPONSE"
955 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
956 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
957 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName )
958 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
959 $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
960 $lock : TargetLock (requestID == $event.requestID)
961 $response : SOResponse( requestReferences.requestId.toString() == $event.requestID.toString() )
964 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
965 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
966 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
967 $params.getClosedLoopControlName(), drools.getRule().getName(),
968 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
970 // Get the result of the operation
972 PolicyResult policyResult = $operation.onResponse($response);
973 if (policyResult != null) {
974 logger.debug("{}: {}: operation finished - result={}",
975 $params.getClosedLoopControlName(), drools.getRule().getName(),
979 // This Operation has completed, construct a notification showing our results
981 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
982 notification.from = "policy";
983 notification.policyName = drools.getRule().getName();
984 notification.policyScope = "${policyScope}";
985 notification.policyVersion = "${policyVersion}";
986 notification.message = $operation.getOperationHistory();
987 notification.history = $operation.getHistory();
988 if (policyResult.equals(PolicyResult.SUCCESS)) {
989 notification.notification = ControlLoopNotificationType.OPERATION_SUCCESS;
991 notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
994 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
996 // Ensure the operation is complete
998 if ($operation.isOperationComplete() == true) {
1000 // It is complete, remove it from memory
1002 retract($operation);
1004 // We must also retract the timer object
1005 // NOTE: We could write a Rule to do this
1009 // Complete the operation
1011 modify($manager) {finishOperation($operation)};
1014 // Just doing this will kick off the LOCKED rule again
1016 modify($operation) {};
1020 // Its not finished yet (i.e. expecting more Response objects)
1022 // Or possibly it is a leftover response that we timed the request out previously
1026 // We are going to retract these objects from memory
1034 * This rule responds to VFC Response Events
1037 rule "${policyName}.VFC.RESPONSE"
1039 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1040 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1041 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName )
1042 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
1043 $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
1044 $lock : TargetLock (requestID == $event.requestID)
1045 $response : VFCResponse( requestId.toString() == $event.requestID.toString() )
1047 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1048 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1049 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
1050 $params.getClosedLoopControlName(), drools.getRule().getName(),
1051 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
1053 // Get the result of the operation
1055 PolicyResult policyResult = $operation.onResponse($response);
1056 if (policyResult != null) {
1058 // This Operation has completed, construct a notification showing our results
1060 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1061 notification.from = "policy";
1062 notification.policyName = drools.getRule().getName();
1063 notification.policyScope = "${policyScope}";
1064 notification.policyVersion = "${policyVersion}";
1065 notification.message = $operation.getOperationHistory();
1066 notification.history = $operation.getHistory();
1068 // Ensure the operation is complete
1070 if ($operation.isOperationComplete() == true) {
1072 // It is complete, remove it from memory
1074 retract($operation);
1076 // We must also retract the timer object
1077 // NOTE: We could write a Rule to do this
1081 // Complete the operation
1083 modify($manager) {finishOperation($operation)};
1086 // Just doing this will kick off the LOCKED rule again
1088 modify($operation) {};
1092 // Its not finished yet (i.e. expecting more Response objects)
1094 // Or possibly it is a leftover response that we timed the request out previously
1098 // We are going to retract these objects from memory
1106 * This is the timer that manages the timeout for an individual operation.
1109 rule "${policyName}.EVENT.MANAGER.OPERATION.TIMEOUT"
1112 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1113 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
1114 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
1115 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
1116 $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString(), $to : getDelay() )
1117 $lock : TargetLock (requestID == $event.requestID)
1120 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1121 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1122 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={}",
1123 $params.getClosedLoopControlName(), drools.getRule().getName(),
1124 $event, $manager, $operation, $lock, $operation, $opTimer);
1127 // Tell it its timed out
1129 $operation.setOperationHasTimedOut();
1131 // Create a notification for it ("DB Write - end operation")
1133 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1134 notification.from = "policy";
1135 notification.policyName = drools.getRule().getName();
1136 notification.policyScope = "${policyScope}";
1137 notification.policyVersion = "${policyVersion}";
1138 notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
1139 notification.message = $operation.getOperationHistory();
1140 notification.history = $operation.getHistory();
1142 // Let interested parties know
1144 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1146 // Get rid of the timer
1150 // Ensure the operation is complete
1152 if ($operation.isOperationComplete() == true) {
1154 // It is complete, remove it from memory
1156 retract($operation);
1158 // Complete the operation
1160 modify($manager) {finishOperation($operation)};
1163 // Just doing this will kick off the LOCKED rule again
1165 modify($operation) {};
1171 * This is the timer that manages the overall control loop timeout.
1174 rule "${policyName}.EVENT.MANAGER.TIMEOUT"
1177 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1178 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
1179 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
1180 $clTimer : ControlLoopTimer ( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString(), $to : getDelay() )
1181 $operations : LinkedList()
1182 from collect( ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID ) )
1183 $opTimers : LinkedList()
1184 from collect( OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() ) )
1185 $locks : LinkedList()
1186 from collect( TargetLock (requestID == $event.requestID) )
1189 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1190 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1192 if ($operations == null) {
1193 logger.debug("{}: {}: event={} manager={} clTimer={} operations=0",
1194 $params.getClosedLoopControlName(), drools.getRule().getName(),
1195 $event, $manager, $clTimer);
1197 logger.debug("{}: {}: event={} manager={} clTimer={} operations={}",
1198 $params.getClosedLoopControlName(), drools.getRule().getName(),
1199 $event, $manager, $clTimer, $operations.size());
1202 // Tell the Event Manager it has timed out
1204 VirtualControlLoopNotification notification = $manager.setControlLoopTimedOut();
1205 if (notification != null) {
1206 notification.from = "policy";
1207 notification.policyName = drools.getRule().getName();
1208 notification.policyScope = "${policyScope}";
1209 notification.policyVersion = "${policyVersion}";
1211 // Let interested parties know
1213 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1216 // Retract EVERYTHING
1221 if ($operations != null && $operations.size() > 0) {
1222 Iterator<ControlLoopOperationManager> iter = $operations.iterator();
1223 while (iter.hasNext()) {
1224 ControlLoopOperationManager manager = iter.next();
1228 if ($opTimers != null && $opTimers.size() > 0) {
1229 Iterator<OperationTimer> iter = $opTimers.iterator();
1230 while (iter.hasNext()) {
1231 OperationTimer opTimer = iter.next();
1235 if ($locks != null && $locks.size() > 0) {
1236 Iterator<TargetLock> iter = $locks.iterator();
1237 while (iter.hasNext()) {
1238 TargetLock lock = iter.next();
1240 // Ensure we release the lock
1242 PolicyGuard.unlockTarget(lock);