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.aai.util.AAIException;
36 import org.onap.policy.appc.Request;
37 import org.onap.policy.appc.Response;
38 import org.onap.policy.appc.CommonHeader;
39 import org.onap.policy.appclcm.LCMRequestWrapper;
40 import org.onap.policy.appclcm.LCMResponseWrapper;
41 import org.onap.policy.appclcm.LCMRequest;
42 import org.onap.policy.appclcm.LCMResponse;
43 import org.onap.policy.appclcm.LCMCommonHeader;
44 import org.onap.policy.vfc.VFCRequest;
45 import org.onap.policy.vfc.VFCResponse;
46 import org.onap.policy.vfc.VFCManager;
47 import org.onap.policy.so.SOManager;
48 import org.onap.policy.so.SORequest;
49 import org.onap.policy.so.SORequestStatus;
50 import org.onap.policy.so.SORequestDetails;
51 import org.onap.policy.so.SOModelInfo;
52 import org.onap.policy.so.SOCloudConfiguration;
53 import org.onap.policy.so.SORequestInfo;
54 import org.onap.policy.so.SORequestParameters;
55 import org.onap.policy.so.SORelatedInstanceListElement;
56 import org.onap.policy.so.SORelatedInstance;
57 import org.onap.policy.so.SOResponse;
58 import org.onap.policy.so.SOResponseWrapper;
59 import org.onap.policy.guard.PolicyGuard;
60 import org.onap.policy.guard.PolicyGuard.LockResult;
61 import org.onap.policy.guard.TargetLock;
62 import org.onap.policy.guard.GuardResult;
63 import org.onap.policy.guard.PolicyGuardRequest;
64 import org.onap.policy.guard.PolicyGuardResponse;
65 import org.onap.policy.guard.PolicyGuardXacmlRequestAttributes;
66 import org.onap.policy.guard.PolicyGuardXacmlHelper;
68 import org.yaml.snakeyaml.Yaml;
69 import org.yaml.snakeyaml.constructor.Constructor;
71 import org.slf4j.LoggerFactory;
72 import org.slf4j.Logger;
74 import java.time.Instant;
75 import java.util.LinkedList;
76 import java.util.Iterator;
78 import org.onap.policy.drools.system.PolicyEngine;
81 * This structure mimics the Params structure.
82 * Its only purpose is to allow management of
83 * rules by the PAP component..
84 * It has no use at runtime since the rules go by
85 * Params for matching purposes.
88 closedLoopControlName : String
89 controlLoopYaml : String
93 * Control Loop Identity
96 closedLoopControlName : String
97 controlLoopYaml : String
104 declare OperationTimer
105 closedLoopControlName : String
113 declare ControlLoopTimer
114 closedLoopControlName : String
121 * Called once and only once to insert the parameters into working memory for this Closed Loop policy.
124 rule "${policyName}.SETUP"
128 Params params = new Params();
129 params.setClosedLoopControlName("${closedLoopControlName}");
130 params.setControlLoopYaml("${controlLoopYaml}");
133 // Note: globals have bad behavior when persistence is used,
134 // hence explicitly getting the logger vs using a global
136 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
137 logger.info("{}: {} : YAML=[{}]", params.getClosedLoopControlName(), drools.getRule().getName(), params.getControlLoopYaml());
139 String sqlDbUsername = PolicyEngine.manager.getEnvironmentProperty("sql.db.username");
141 String aaiUrl = PolicyEngine.manager.getEnvironmentProperty("aai.url");
142 String aaiUsername = PolicyEngine.manager.getEnvironmentProperty("aai.username");
143 String aaiPassword = PolicyEngine.manager.getEnvironmentProperty("aai.password");
145 String soUrl =PolicyEngine.manager.getEnvironmentProperty("so.url");
146 String soUsername = PolicyEngine.manager.getEnvironmentProperty("so.username");
147 String soPassword = PolicyEngine.manager.getEnvironmentProperty("so.password");
149 String vfcUrl =PolicyEngine.manager.getEnvironmentProperty("vfc.url");
150 String vfcUsername = PolicyEngine.manager.getEnvironmentProperty("vfc.username");
151 String vfcPassword = PolicyEngine.manager.getEnvironmentProperty("vfc.password");
153 String guardUrl = PolicyEngine.manager.getEnvironmentProperty("guard.url");
154 String guardUsername = PolicyEngine.manager.getEnvironmentProperty("pdpx.username");
155 String guardPassword = PolicyEngine.manager.getEnvironmentProperty("pdpx.password");
156 String guardJdbcUrl = PolicyEngine.manager.getEnvironmentProperty("guard.jdbc.url");
157 String guardDisabled = PolicyEngine.manager.getEnvironmentProperty("guard.disabled");
159 logger.info("{}: {} : AAI=[{}:{}]", params.getClosedLoopControlName(), drools.getRule().getName(), aaiUrl, aaiUsername);
160 logger.info("{}: {} : SO=[{}:{}]", params.getClosedLoopControlName(), drools.getRule().getName(), soUrl, soUsername);
161 logger.info("{}: {} : VFC=[{}:{}]", params.getClosedLoopControlName(), drools.getRule().getName(), vfcUrl, vfcUsername);
162 logger.info("{}: {} : GUARD=[{}:{}:{}:{}]", params.getClosedLoopControlName(), drools.getRule().getName(), guardUrl, guardUsername, guardJdbcUrl, guardDisabled);
163 logger.info("{}: {} : DB=[{}]", params.getClosedLoopControlName(), drools.getRule().getName(), sqlDbUsername);
168 * This rule responds to DCAE Events where there is no manager yet. Either it is
169 * the first ONSET, or a subsequent badly formed Event (i.e. Syntax error, or is-closed-loop-disabled)
172 rule "${policyName}.EVENT"
174 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
175 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
176 not ( ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID ) )
179 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
180 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
185 // Check the event, because we need it to not be null when
186 // we create the ControlLoopEventManager. The ControlLoopEventManager
187 // will do extra syntax checking as well check if the closed loop is disabled.
189 if ($event.requestID == null) {
190 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
191 notification.notification = ControlLoopNotificationType.REJECTED;
192 notification.from = "policy";
193 notification.message = "Missing requestID";
194 notification.policyName = drools.getRule().getName();
195 notification.policyScope = "${policyScope}";
196 notification.policyVersion = "${policyVersion}";
199 // Let interested parties know
201 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
204 // Retract it from memory
207 } else if ($event.closedLoopEventStatus != ControlLoopEventStatus.ONSET) {
208 throw new ControlLoopException($event.closedLoopEventStatus + " received with no prior onset");
211 // Create an EventManager
213 ControlLoopEventManager manager = new ControlLoopEventManager($params.getClosedLoopControlName(), $event.requestID);
215 // Determine if EventManager can actively process the event (i.e. syntax, is_closed_loop_disabled checks etc.)
217 VirtualControlLoopNotification notification = manager.activate($params.getControlLoopYaml(), $event);
218 notification.from = "pdp-0001-controller=controlloop"; // Engine.getInstanceName()
219 notification.policyName = drools.getRule().getName();
220 notification.policyScope = "${policyScope}";
221 notification.policyVersion = "${policyVersion}";
223 // Are we actively pursuing this event?
225 if (notification.notification == ControlLoopNotificationType.ACTIVE) {
227 // Insert Event Manager into memory, this will now kick off processing.
231 // Let interested parties know
233 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
235 // Setup the Overall Control Loop timer
237 ControlLoopTimer clTimer = new ControlLoopTimer();
238 clTimer.setClosedLoopControlName($event.closedLoopControlName);
239 clTimer.setRequestID($event.requestID.toString());
240 clTimer.setDelay(manager.getControlLoopTimeout(1500) + "s");
247 // Let interested parties know
249 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
251 // Retract it from memory
257 // Now that the manager is inserted into Drools working memory, we'll wait for
258 // another rule to fire in order to continue processing. This way we can also
259 // then screen for additional ONSET and ABATED events for this RequestID.
262 } catch (Exception e) {
263 logger.warn("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName(), e);
265 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
266 notification.notification = ControlLoopNotificationType.REJECTED;
267 notification.message = "Exception occurred: " + e.getMessage();
268 notification.policyName = drools.getRule().getName();
269 notification.policyScope = "${policyScope}";
270 notification.policyVersion = "${policyVersion}";
274 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
284 * This rule happens when we got a valid ONSET, closed loop is enabled and an Event Manager
285 * is now created. We can start processing the yaml specification via the Event Manager.
288 rule "${policyName}.EVENT.MANAGER"
290 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
291 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
292 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
293 $clTimer : ControlLoopTimer ( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
296 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
297 logger.info("{}: {}: event={} manager={} clTimer={}",
298 $params.getClosedLoopControlName(), drools.getRule().getName(),
299 $event, $manager, $clTimer);
303 // Check which event this is.
305 ControlLoopEventManager.NEW_EVENT_STATUS eventStatus = $manager.onNewEvent($event);
307 // Check what kind of event this is
309 if (eventStatus == NEW_EVENT_STATUS.SUBSEQUENT_ONSET) {
311 // We don't care about subsequent onsets
313 logger.info("{}: {}: subsequent onset",
314 $params.getClosedLoopControlName(), drools.getRule().getName());
318 if (eventStatus == NEW_EVENT_STATUS.SYNTAX_ERROR) {
320 // Ignore any bad syntax events
322 logger.warn("{}: {}: syntax error",
323 $params.getClosedLoopControlName(), drools.getRule().getName());
328 // We only want the initial ONSET event in memory,
329 // all the other events need to be retracted to support
330 // cleanup and avoid the other rules being fired for this event.
332 if (eventStatus != NEW_EVENT_STATUS.FIRST_ONSET) {
333 logger.warn("{}: {}: no first onset",
334 $params.getClosedLoopControlName(), drools.getRule().getName());
338 logger.debug("{}: {}: target={}", $params.getClosedLoopControlName(),
339 drools.getRule().getName(), $event.target);
341 // Now start seeing if we need to process this event
345 // Check if this is a Final Event
347 VirtualControlLoopNotification notification = $manager.isControlLoopFinal();
350 if (notification != null) {
352 // Its final, but are we waiting for abatement?
354 if ($manager.getNumAbatements() > 0) {
355 logger.info("{}: {}: abatement received for {}. Closing the control loop",
356 $params.getClosedLoopControlName(), drools.getRule().getName(),
358 notification.from = "policy";
359 notification.policyName = drools.getRule().getName();
360 notification.policyScope = "${policyScope}";
361 notification.policyVersion = "${policyVersion}";
363 // In this case, we are done
365 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
369 TargetLock lock = $manager.unlockCurrentOperation();
371 logger.debug("{}: {}: retracting lock=", $params.getClosedLoopControlName(),
372 drools.getRule().getName(), lock);
376 // Retract everything from memory
378 logger.info("{}: {}: retracting onset, manager, and timer",
379 $params.getClosedLoopControlName(), drools.getRule().getName());
381 retract($manager.getOnsetEvent());
385 // TODO - what if we get subsequent Events for this RequestID?
386 // By default, it will all start over again. May be confusing for Ruby.
387 // Or, we could track this and then subsequently ignore the events
391 // Check whether we need to wait for abatement
393 if ($manager.getProcessor().getControlLoop().getAbatement() == true && notification.notification == ControlLoopNotificationType.FINAL_SUCCESS) {
394 logger.info("{}: {}: waiting for abatement ..",
395 $params.getClosedLoopControlName(), drools.getRule().getName());
397 logger.info("{}: {}: no abatement expect for {}. Closing the control loop",
398 $params.getClosedLoopControlName(), drools.getRule().getName(),
401 notification.from = "policy";
402 notification.policyName = drools.getRule().getName();
403 notification.policyScope = "${policyScope}";
404 notification.policyVersion = "${policyVersion}";
407 // In this case, we are done
409 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
413 TargetLock lock = $manager.unlockCurrentOperation();
415 logger.debug("{}: {}: retracting lock=", $params.getClosedLoopControlName(),
416 drools.getRule().getName(), lock);
420 // Retract everything from memory
422 logger.info("{}: {}: retracting onset, manager, and timer",
423 $params.getClosedLoopControlName(), drools.getRule().getName());
425 retract($manager.getOnsetEvent());
432 // NOT final, so let's ask for the next operation
434 ControlLoopOperationManager operation = $manager.processControlLoop();
435 if (operation != null) {
437 // Let's ask for a lock right away
439 LockResult<GuardResult, TargetLock> result = $manager.lockCurrentOperation();
440 logger.info("{}: {}: guard lock acquired={}",
441 $params.getClosedLoopControlName(), drools.getRule().getName(),
443 if (result.getA().equals(GuardResult.LOCK_ACQUIRED)) {
445 // insert the operation into memory
450 // insert operation timeout object
452 OperationTimer opTimer = new OperationTimer();
453 opTimer.setClosedLoopControlName($event.closedLoopControlName);
454 opTimer.setRequestID($event.requestID.toString());
455 opTimer.setDelay(operation.getOperationTimeout().toString() + "s");
459 // Insert lock into memory
461 insert(result.getB());
464 logger.debug("The target resource {} is already processing",
465 $event.AAI.get($event.target));
466 notification = new VirtualControlLoopNotification($event);
467 notification.notification = ControlLoopNotificationType.REJECTED;
468 notification.message = "The target " + $event.AAI.get($event.target) + " is already locked";
469 notification.from = "policy";
470 notification.policyName = drools.getRule().getName();
471 notification.policyScope = "${policyScope}";
472 notification.policyVersion = "${policyVersion}";
474 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
480 logger.info("{}: {}: starting operation={}",
481 $params.getClosedLoopControlName(), drools.getRule().getName(),
485 // Probably waiting for abatement
487 logger.info("{}: {}: no operation, probably waiting for abatement",
488 $params.getClosedLoopControlName(), drools.getRule().getName());
491 } catch (Exception e) {
492 logger.warn("{}: {}: unexpected",
493 $params.getClosedLoopControlName(),
494 drools.getRule().getName(), e);
496 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
497 notification.notification = ControlLoopNotificationType.FINAL_FAILURE;
498 notification.message = e.getMessage();
499 notification.from = "policy";
500 notification.policyName = drools.getRule().getName();
501 notification.policyScope = "${policyScope}";
502 notification.policyVersion = "${policyVersion}";
504 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
515 * Guard Permitted, let's send request to the actor.
518 rule "${policyName}.EVENT.MANAGER.OPERATION.LOCKED.GUARD_PERMITTED"
520 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
521 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
522 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
523 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID, "Permit".equalsIgnoreCase(getGuardApprovalStatus()) )
524 $lock : TargetLock (requestID == $event.requestID)
525 $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
528 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
529 logger.info("{}: {}: event={} manager={} operation={} lock={}",
530 $params.getClosedLoopControlName(), drools.getRule().getName(),
531 $event, $manager, $operation, $lock);
533 Object request = null;
534 boolean caughtException = false;
536 request = $operation.startOperation($event);
538 catch (AAIException e) {
539 String msg = e.getMessage();
540 logger.warn("{}: {}: operation={}: AAI failure: {}",
541 $params.getClosedLoopControlName(), drools.getRule().getName(),
543 $operation.setOperationHasException(msg);
546 caughtException = true;
549 // Having the modify statement in the catch clause doesn't work for whatever reason
550 if (caughtException) {
551 modify($manager) {finishOperation($operation)};
553 else if (request != null) {
554 logger.debug("{}: {}: starting operation ..",
555 $params.getClosedLoopControlName(), drools.getRule().getName());
557 // Tell interested parties we are performing this Operation
559 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
560 notification.notification = ControlLoopNotificationType.OPERATION;
561 notification.message = $operation.getOperationMessage();
562 notification.history = $operation.getHistory();
563 notification.from = "policy";
564 notification.policyName = drools.getRule().getName();
565 notification.policyScope = "${policyScope}";
566 notification.policyVersion = "${policyVersion}";
568 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
570 switch ($operation.policy.getActor()){
574 if (request instanceof Request) {
575 PolicyEngine.manager.deliver("APPC-CL", request);
577 else if (request instanceof LCMRequestWrapper) {
578 PolicyEngine.manager.deliver("APPC-LCM-READ", request);
582 // at this point the AAI named query request should have already been made, the response recieved and used
583 // in the construction of the SO Request which is stored in operationRequest
585 if(request instanceof SORequest) {
586 // Call SO. The response will be inserted into memory once it's received
587 SOActorServiceProvider.sendRequest($event.requestID.toString(), drools.getWorkingMemory(), request);
591 if (request instanceof VFCRequest) {
593 Thread t = new Thread(new VFCManager(drools.getWorkingMemory(), (VFCRequest)request));
600 // What happens if its null?
602 logger.warn("{}: {}: unexpected null operation request",
603 $params.getClosedLoopControlName(),
604 drools.getRule().getName());
605 if ("SO".equals($operation.policy.getActor())) {
608 modify($manager) {finishOperation($operation)};
610 else if ("vfc".equalsIgnoreCase($operation.policy.getActor())) {
613 modify($manager) {finishOperation($operation)};
622 * We were able to acquire a lock so now let's ask Xacml Guard whether
623 * we are allowed to proceed with the request to the actor.
626 rule "${policyName}.EVENT.MANAGER.OPERATION.LOCKED.GUARD_NOT_YET_QUERIED"
628 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
629 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
630 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
631 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID, getGuardApprovalStatus() == "NONE" )
632 $lock : TargetLock (requestID == $event.requestID)
635 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
636 logger.info("{}: {}: event={} manager={} operation={} lock={}",
637 $params.getClosedLoopControlName(), drools.getRule().getName(),
638 $event, $manager, $operation, $lock);
641 // Sending notification that we are about to query Guard ("DB write - start operation")
643 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
644 notification.notification = ControlLoopNotificationType.OPERATION;
645 notification.message = "Sending guard query for " + $operation.policy.getActor() + " " + $operation.policy.getRecipe();
646 notification.history = $operation.getHistory();
647 notification.from = "policy";
648 notification.policyName = drools.getRule().getName();
649 notification.policyScope = "${policyScope}";
650 notification.policyVersion = "${policyVersion}";
652 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
655 // Now send Guard Request to XACML Guard. In order to bypass the call to Guard,
656 // just change guardEnabled to false.
658 // In order to use REST XACML, provide a URL instead of "" as a second argument
659 // to the CallGuardTask() and set the first argument to null
660 // (instead of XacmlPdpEngine).
663 // NOTE: The environment properties uses "guard.disabled" but the boolean is guardEnabled
664 boolean guardEnabled = "false".equalsIgnoreCase(PolicyEngine.manager.getEnvironmentProperty("guard.disabled"));
668 Thread t = new Thread(new org.onap.policy.guard.CallGuardTask(
669 drools.getWorkingMemory(),
670 $event.closedLoopControlName,
671 $operation.policy.getActor().toString(),
672 $operation.policy.getRecipe(),
673 $operation.getTargetEntity(),
674 $event.requestID.toString()
679 insert(new PolicyGuardResponse("Permit", $event.requestID, $operation.policy.getRecipe()));
685 // This rule will be triggered when a thread talking to the XACML Guard inserts a
686 // guardResponse object into the working memory
688 rule "${policyName}.GUARD.RESPONSE"
690 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
691 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
692 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
693 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
694 $lock : TargetLock (requestID == $event.requestID)
695 $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
696 $guardResponse : PolicyGuardResponse(requestID == $event.requestID, $operation.policy.recipe == operation)
699 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
700 logger.info("{}: {}: event={} manager={} operation={} lock={} opTimer={} guardResponse={}",
701 $params.getClosedLoopControlName(), drools.getRule().getName(),
702 $event, $manager, $operation, $lock, $opTimer, $guardResponse);
705 //we will permit the operation if there was no Guard for it
706 if("Indeterminate".equalsIgnoreCase($guardResponse.result)){
707 $guardResponse.result = "Permit";
711 // This notification has Guard result in "message". ("DB write - end operation in case of Guard Deny")
713 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
714 notification.notification = ControlLoopNotificationType.OPERATION;
715 notification.message = "Guard result for " + $operation.policy.getActor() + " " + $operation.policy.getRecipe() + " is " + $guardResponse.result;
716 notification.history = $operation.getHistory();
717 notification.from = "policy";
718 notification.policyName = drools.getRule().getName();
719 notification.policyScope = "${policyScope}";
720 notification.policyVersion = "${policyVersion}";
722 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
724 if("Permit".equalsIgnoreCase($guardResponse.result)){
726 modify($operation){setGuardApprovalStatus($guardResponse.result)};
729 //This is the Deny case
730 $operation.startOperation($event);
731 $operation.setOperationHasGuardDeny();
734 modify($manager) {finishOperation($operation)};
737 retract($guardResponse);
743 * This rule responds to APPC Response Events
745 * I would have like to be consistent and write the Response like this:
746 * $response : Response( CommonHeader.RequestID == $onset.requestID )
748 * However, no compile error was given. But a runtime error was given. I think
749 * because drools is confused between the classname CommonHeader vs the property CommonHeader.
752 rule "${policyName}.APPC.RESPONSE"
754 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
755 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
756 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
757 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
758 $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
759 $lock : TargetLock (requestID == $event.requestID)
760 $response : Response( getCommonHeader().RequestID == $event.requestID )
763 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
764 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
765 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
766 $params.getClosedLoopControlName(), drools.getRule().getName(),
767 $event, $manager, $operation, $lock, $opTimer, $response);
769 // Get the result of the operation
771 PolicyResult policyResult = $operation.onResponse($response);
772 if (policyResult != null) {
773 logger.debug("{}: {}: operation finished - result={}",
774 $params.getClosedLoopControlName(), drools.getRule().getName(),
777 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
779 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
780 notification.from = "policy";
781 notification.policyName = drools.getRule().getName();
782 notification.policyScope = "${policyScope}";
783 notification.policyVersion = "${policyVersion}";
784 notification.message = $operation.getOperationHistory();
785 notification.history = $operation.getHistory();
786 if (policyResult.equals(PolicyResult.SUCCESS)) {
787 notification.notification = ControlLoopNotificationType.OPERATION_SUCCESS;
789 // Let interested parties know
791 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
793 notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
795 // Let interested parties know
797 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
800 // Ensure the operation is complete
802 if ($operation.isOperationComplete() == true) {
804 // It is complete, remove it from memory
808 // We must also retract the timer object
809 // NOTE: We could write a Rule to do this
813 // Complete the operation
815 modify($manager) {finishOperation($operation)};
818 // Just doing this will kick off the LOCKED rule again
820 modify($operation) {};
824 // Its not finished yet (i.e. expecting more Response objects)
826 // Or possibly it is a leftover response that we timed the request out previously
830 // We are going to retract these objects from memory
837 * The problem with Responses is that they don't have a controlLoopControlName
838 * field in them, so the only way to attach them is via RequestID. If we have multiple
839 * control loop .drl's loaded in the same container, we need to be sure the cleanup
840 * rules don't remove Responses for other control loops.
843 rule "${policyName}.APPC.RESPONSE.CLEANUP"
845 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
846 $response : Response($id : getCommonHeader().RequestID )
847 not ( VirtualControlLoopEvent( requestID == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
850 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
851 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
852 logger.debug("{}: {}: orphan appc response={}",
853 $params.getClosedLoopControlName(), drools.getRule().getName(), $id);
863 * This rule responds to APPC Response Events using the new LCM interface provided by appc
866 rule "${policyName}.APPC.LCM.RESPONSE"
868 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
869 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
870 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
871 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
872 $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
873 $lock : TargetLock (requestID == $event.requestID)
874 $response : LCMResponseWrapper( getBody().getCommonHeader().getRequestId() == $event.requestID )
877 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
878 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
879 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
880 $params.getClosedLoopControlName(), drools.getRule().getName(),
881 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
884 // Get the result of the operation
886 PolicyResult policyResult = $operation.onResponse($response);
887 if (policyResult != null) {
888 logger.debug("{}: {}: operation finished - result={}",
889 $params.getClosedLoopControlName(), drools.getRule().getName(),
893 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
895 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
896 notification.from = "policy";
897 notification.policyName = drools.getRule().getName();
898 notification.policyScope = "${policyScope}";
899 notification.policyVersion = "${policyVersion}";
900 notification.message = $operation.getOperationHistory();
901 notification.history = $operation.getHistory();
902 if (policyResult.equals(PolicyResult.SUCCESS)) {
903 notification.notification = ControlLoopNotificationType.OPERATION_SUCCESS;
905 notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
907 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
909 // Ensure the operation is complete
911 if ($operation.isOperationComplete() == true) {
913 // It is complete, remove it from memory
917 // We must also retract the timer object
918 // NOTE: We could write a Rule to do this
922 // Complete the operation
924 modify($manager) {finishOperation($operation)};
927 // Just doing this will kick off the LOCKED rule again
929 modify($operation) {};
933 // Its not finished yet (i.e. expecting more Response objects)
935 // Or possibly it is a leftover response that we timed the request out previously
939 // We are going to retract these objects from memory
946 * Clean Up any lingering LCM reponses
949 rule "${policyName}.APPC.LCM.RESPONSE.CLEANUP"
951 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
952 $response : LCMResponseWrapper($id : getBody().getCommonHeader().getRequestId )
953 not ( VirtualControlLoopEvent( requestID == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
956 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
957 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
958 logger.debug("{}: {}: orphan appc response={}",
959 $params.getClosedLoopControlName(), drools.getRule().getName(), $id);
968 * This rule responds to SO Response Events
971 rule "${policyName}.SO.RESPONSE"
973 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
974 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
975 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName )
976 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
977 $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
978 $lock : TargetLock (requestID == $event.requestID)
979 $response : SOResponseWrapper(requestID.toString() == $event.requestID.toString() )
982 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
983 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
984 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
985 $params.getClosedLoopControlName(), drools.getRule().getName(),
986 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
988 // Get the result of the operation
990 PolicyResult policyResult = $operation.onResponse($response);
991 if (policyResult != null) {
992 logger.debug("{}: {}: operation finished - result={}",
993 $params.getClosedLoopControlName(), drools.getRule().getName(),
997 // This Operation has completed, construct a notification showing our results
999 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1000 notification.from = "policy";
1001 notification.policyName = drools.getRule().getName();
1002 notification.policyScope = "${policyScope}";
1003 notification.policyVersion = "${policyVersion}";
1004 notification.message = $operation.getOperationHistory();
1005 notification.history = $operation.getHistory();
1006 if (policyResult.equals(PolicyResult.SUCCESS)) {
1007 notification.notification = ControlLoopNotificationType.OPERATION_SUCCESS;
1009 notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
1012 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1014 // Ensure the operation is complete
1016 if ($operation.isOperationComplete() == true) {
1018 // It is complete, remove it from memory
1020 retract($operation);
1022 // We must also retract the timer object
1023 // NOTE: We could write a Rule to do this
1027 // Complete the operation
1029 modify($manager) {finishOperation($operation)};
1032 // Just doing this will kick off the LOCKED rule again
1034 modify($operation) {};
1038 // Its not finished yet (i.e. expecting more Response objects)
1040 // Or possibly it is a leftover response that we timed the request out previously
1044 // We are going to retract these objects from memory
1052 * This rule responds to VFC Response Events
1055 rule "${policyName}.VFC.RESPONSE"
1057 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1058 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1059 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName )
1060 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
1061 $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
1062 $lock : TargetLock (requestID == $event.requestID)
1063 $response : VFCResponse( requestId.toString() == $event.requestID.toString() )
1065 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1066 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1067 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
1068 $params.getClosedLoopControlName(), drools.getRule().getName(),
1069 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
1071 // Get the result of the operation
1073 PolicyResult policyResult = $operation.onResponse($response);
1074 if (policyResult != null) {
1076 // This Operation has completed, construct a notification showing our results
1078 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1079 notification.from = "policy";
1080 notification.policyName = drools.getRule().getName();
1081 notification.policyScope = "${policyScope}";
1082 notification.policyVersion = "${policyVersion}";
1083 notification.message = $operation.getOperationHistory();
1084 notification.history = $operation.getHistory();
1086 // Ensure the operation is complete
1088 if ($operation.isOperationComplete() == true) {
1090 // It is complete, remove it from memory
1092 retract($operation);
1094 // We must also retract the timer object
1095 // NOTE: We could write a Rule to do this
1099 // Complete the operation
1101 modify($manager) {finishOperation($operation)};
1104 // Just doing this will kick off the LOCKED rule again
1106 modify($operation) {};
1110 // Its not finished yet (i.e. expecting more Response objects)
1112 // Or possibly it is a leftover response that we timed the request out previously
1116 // We are going to retract these objects from memory
1124 * This is the timer that manages the timeout for an individual operation.
1127 rule "${policyName}.EVENT.MANAGER.OPERATION.TIMEOUT"
1130 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1131 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
1132 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
1133 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
1134 $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString(), $to : getDelay() )
1135 $lock : TargetLock (requestID == $event.requestID)
1138 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1139 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1140 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={}",
1141 $params.getClosedLoopControlName(), drools.getRule().getName(),
1142 $event, $manager, $operation, $lock, $operation, $opTimer);
1145 // Tell it its timed out
1147 $operation.setOperationHasTimedOut();
1149 // Create a notification for it ("DB Write - end operation")
1151 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1152 notification.from = "policy";
1153 notification.policyName = drools.getRule().getName();
1154 notification.policyScope = "${policyScope}";
1155 notification.policyVersion = "${policyVersion}";
1156 notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
1157 notification.message = $operation.getOperationHistory();
1158 notification.history = $operation.getHistory();
1160 // Let interested parties know
1162 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1164 // Get rid of the timer
1168 // Ensure the operation is complete
1170 if ($operation.isOperationComplete() == true) {
1172 // It is complete, remove it from memory
1174 retract($operation);
1176 // Complete the operation
1178 modify($manager) {finishOperation($operation)};
1181 // Just doing this will kick off the LOCKED rule again
1183 modify($operation) {};
1189 * This is the timer that manages the overall control loop timeout.
1192 rule "${policyName}.EVENT.MANAGER.TIMEOUT"
1195 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1196 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
1197 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
1198 $clTimer : ControlLoopTimer ( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString(), $to : getDelay() )
1199 $operations : LinkedList()
1200 from collect( ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID ) )
1201 $opTimers : LinkedList()
1202 from collect( OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() ) )
1203 $locks : LinkedList()
1204 from collect( TargetLock (requestID == $event.requestID) )
1207 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1208 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1210 if ($operations == null) {
1211 logger.debug("{}: {}: event={} manager={} clTimer={} operations=0",
1212 $params.getClosedLoopControlName(), drools.getRule().getName(),
1213 $event, $manager, $clTimer);
1215 logger.debug("{}: {}: event={} manager={} clTimer={} operations={}",
1216 $params.getClosedLoopControlName(), drools.getRule().getName(),
1217 $event, $manager, $clTimer, $operations.size());
1220 // Tell the Event Manager it has timed out
1222 VirtualControlLoopNotification notification = $manager.setControlLoopTimedOut();
1223 if (notification != null) {
1224 notification.from = "policy";
1225 notification.policyName = drools.getRule().getName();
1226 notification.policyScope = "${policyScope}";
1227 notification.policyVersion = "${policyVersion}";
1229 // Let interested parties know
1231 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1234 // Retract EVERYTHING
1239 if ($operations != null && $operations.size() > 0) {
1240 Iterator<ControlLoopOperationManager> iter = $operations.iterator();
1241 while (iter.hasNext()) {
1242 ControlLoopOperationManager manager = iter.next();
1246 if ($opTimers != null && $opTimers.size() > 0) {
1247 Iterator<OperationTimer> iter = $opTimers.iterator();
1248 while (iter.hasNext()) {
1249 OperationTimer opTimer = iter.next();
1253 if ($locks != null && $locks.size() > 0) {
1254 Iterator<TargetLock> iter = $locks.iterator();
1255 while (iter.hasNext()) {
1256 TargetLock lock = iter.next();
1258 // Ensure we release the lock
1260 PolicyGuard.unlockTarget(lock);