2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.controlloop;
23 import org.onap.policy.controlloop.params.ControlLoopParams;
24 import org.onap.policy.controlloop.VirtualControlLoopEvent;
25 import org.onap.policy.controlloop.VirtualControlLoopNotification;
26 import org.onap.policy.controlloop.ControlLoopEventStatus;
27 import org.onap.policy.controlloop.ControlLoopNotificationType;
28 import org.onap.policy.controlloop.ControlLoopLogger;
29 import org.onap.policy.controlloop.policy.PolicyResult;
30 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
31 import org.onap.policy.controlloop.policy.Policy;
32 import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager;
33 import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager.NEW_EVENT_STATUS;
34 import org.onap.policy.controlloop.eventmanager.ControlLoopOperationManager;
35 import org.onap.policy.controlloop.actor.so.SOActorServiceProvider;
36 import org.onap.policy.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;
83 declare OperationTimer
84 closedLoopControlName : String
92 declare ControlLoopTimer
93 closedLoopControlName : String
100 * Called when the ControlLoopParams object has been inserted into working memory from the BRMSGW.
105 $params : ControlLoopParams()
108 // Note: globals have bad behavior when persistence is used,
109 // hence explicitly getting the logger vs using a global
111 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
112 logger.info("{}: {} : YAML=[{}]", $params.getClosedLoopControlName(), $params.getPolicyName() + "." + drools.getRule().getName(), $params.getControlLoopYaml());
117 * This rule responds to DCAE Events where there is no manager yet. Either it is
118 * the first ONSET, or a subsequent badly formed Event (i.e. Syntax error, or is-closed-loop-disabled)
123 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
124 $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
125 not ( ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() ) )
128 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
129 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
134 // Check the event, because we need it to not be null when
135 // we create the ControlLoopEventManager. The ControlLoopEventManager
136 // will do extra syntax checking as well check if the closed loop is disabled.
138 if ($event.getRequestId() == null) {
139 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
140 notification.setNotification(ControlLoopNotificationType.REJECTED);
141 notification.setFrom("policy");
142 notification.setMessage("Missing requestID");
143 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
144 notification.setPolicyScope($params.getPolicyScope());
145 notification.setPolicyVersion($params.getPolicyVersion());
148 // Let interested parties know
150 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
153 // Retract it from memory
156 } else if ($event.getClosedLoopEventStatus() != ControlLoopEventStatus.ONSET) {
157 throw new ControlLoopException($event.getClosedLoopEventStatus() + " received with no prior onset");
160 // Create an EventManager
162 ControlLoopEventManager manager = new ControlLoopEventManager($clName, $event.getRequestId());
164 // Determine if EventManager can actively process the event (i.e. syntax, is_closed_loop_disabled checks etc.)
166 VirtualControlLoopNotification notification = manager.activate($params.getControlLoopYaml(), $event);
167 notification.setFrom("pdp-0001-controller=controlloop"); // Engine.getInstanceName()
168 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
169 notification.setPolicyScope($params.getPolicyScope());
170 notification.setPolicyVersion($params.getPolicyVersion());
172 // Are we actively pursuing this event?
174 if (notification.getNotification() == ControlLoopNotificationType.ACTIVE) {
176 // Insert Event Manager into memory, this will now kick off processing.
180 // Let interested parties know
182 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
184 // Setup the Overall Control Loop timer
186 ControlLoopTimer clTimer = new ControlLoopTimer();
187 clTimer.setClosedLoopControlName($event.getClosedLoopControlName());
188 clTimer.setRequestID($event.getRequestId().toString());
189 clTimer.setDelay(manager.getControlLoopTimeout(1500) + "s");
196 // Let interested parties know
198 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
200 // Retract it from memory
206 // Now that the manager is inserted into Drools working memory, we'll wait for
207 // another rule to fire in order to continue processing. This way we can also
208 // then screen for additional ONSET and ABATED events for this RequestID.
211 } catch (Exception e) {
212 logger.warn("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName(), e);
214 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
215 notification.setNotification(ControlLoopNotificationType.REJECTED);
216 notification.setMessage("Exception occurred: " + e.getMessage());
217 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
218 notification.setPolicyScope($params.getPolicyScope());
219 notification.setPolicyVersion($params.getPolicyVersion());
223 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
233 * This rule happens when we got a valid ONSET, closed loop is enabled and an Event Manager
234 * is now created. We can start processing the yaml specification via the Event Manager.
239 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
240 $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
241 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
242 $clTimer : ControlLoopTimer ( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
245 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
246 logger.info("{}: {}: event={} manager={} clTimer={}",
247 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
248 $event, $manager, $clTimer);
252 // Check which event this is.
254 ControlLoopEventManager.NEW_EVENT_STATUS eventStatus = $manager.onNewEvent($event);
256 // Check what kind of event this is
258 if (eventStatus == NEW_EVENT_STATUS.SUBSEQUENT_ONSET) {
260 // We don't care about subsequent onsets
262 logger.info("{}: {}: subsequent onset",
263 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
267 if (eventStatus == NEW_EVENT_STATUS.SYNTAX_ERROR) {
269 // Ignore any bad syntax events
271 logger.warn("{}: {}: syntax error",
272 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
277 // We only want the initial ONSET event in memory,
278 // all the other events need to be retracted to support
279 // cleanup and avoid the other rules being fired for this event.
281 if (eventStatus != NEW_EVENT_STATUS.FIRST_ONSET) {
282 logger.warn("{}: {}: no first onset",
283 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
287 logger.debug("{}: {}: target={}", $clName,
288 $params.getPolicyName() + "." + drools.getRule().getName(), $event.getTarget());
290 // Now start seeing if we need to process this event
294 // Check if this is a Final Event
296 VirtualControlLoopNotification notification = $manager.isControlLoopFinal();
299 if (notification != null) {
301 // Its final, but are we waiting for abatement?
303 if ($manager.getNumAbatements() > 0) {
304 logger.info("{}: {}: abatement received for {}. Closing the control loop",
305 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
306 $event.getRequestId());
307 notification.setFrom("policy");
308 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
309 notification.setPolicyScope($params.getPolicyScope());
310 notification.setPolicyVersion($params.getPolicyVersion());
312 // In this case, we are done
314 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
318 TargetLock lock = $manager.unlockCurrentOperation();
320 logger.debug("{}: {}: retracting lock=", $clName,
321 $params.getPolicyName() + "." + drools.getRule().getName(), lock);
325 // Retract everything from memory
327 logger.info("{}: {}: retracting onset, manager, and timer",
328 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
330 retract($manager.getOnsetEvent());
334 // TODO - what if we get subsequent Events for this RequestID?
335 // By default, it will all start over again. May be confusing for Ruby.
336 // Or, we could track this and then subsequently ignore the events
340 // Check whether we need to wait for abatement
342 if ($manager.getProcessor().getControlLoop().getAbatement() == true && notification.getNotification() == ControlLoopNotificationType.FINAL_SUCCESS) {
343 logger.info("{}: {}: waiting for abatement ..",
344 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
346 logger.info("{}: {}: no abatement expect for {}. Closing the control loop",
347 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
348 $event.getRequestId());
350 notification.setFrom("policy");
351 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
352 notification.setPolicyScope($params.getPolicyScope());
353 notification.setPolicyVersion($params.getPolicyVersion());
356 // In this case, we are done
358 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
362 TargetLock lock = $manager.unlockCurrentOperation();
364 logger.debug("{}: {}: retracting lock=", $clName,
365 $params.getPolicyName() + "." + drools.getRule().getName(), lock);
369 // Retract everything from memory
371 logger.info("{}: {}: retracting onset, manager, and timer",
372 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
374 retract($manager.getOnsetEvent());
381 // NOT final, so let's ask for the next operation
383 ControlLoopOperationManager operation = $manager.processControlLoop();
384 if (operation != null) {
386 // Let's ask for a lock right away
388 LockResult<GuardResult, TargetLock> result = $manager.lockCurrentOperation();
389 logger.info("{}: {}: guard lock acquired={}",
390 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
392 if (result.getA().equals(GuardResult.LOCK_ACQUIRED)) {
394 // insert the operation into memory
399 // insert operation timeout object
401 OperationTimer opTimer = new OperationTimer();
402 opTimer.setClosedLoopControlName($event.getClosedLoopControlName());
403 opTimer.setRequestID($event.getRequestId().toString());
404 opTimer.setDelay(operation.getOperationTimeout().toString() + "s");
408 // Insert lock into memory
410 insert(result.getB());
413 logger.debug("The target resource {} is already processing",
414 $event.getAai().get($event.getTarget()));
415 notification = new VirtualControlLoopNotification($event);
416 notification.setNotification(ControlLoopNotificationType.REJECTED);
417 notification.setMessage("The target " + $event.getAai().get($event.getTarget()) + " is already locked");
418 notification.setFrom("policy");
419 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
420 notification.setPolicyScope($params.getPolicyScope());
421 notification.setPolicyVersion($params.getPolicyVersion());
423 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
429 if(result.getB() != null) {
430 retract(result.getB());
433 logger.info("{}: {}: starting operation={}",
434 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
438 // Probably waiting for abatement
440 logger.info("{}: {}: no operation, probably waiting for abatement",
441 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
444 } catch (Exception e) {
445 logger.warn("{}: {}: unexpected",
447 $params.getPolicyName() + "." + drools.getRule().getName(), e);
449 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
450 notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
451 notification.setMessage(e.getMessage());
452 notification.setFrom("policy");
453 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
454 notification.setPolicyScope($params.getPolicyScope());
455 notification.setPolicyVersion($params.getPolicyVersion());
457 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
468 * Guard Permitted, let's send request to the actor.
471 rule "EVENT.MANAGER.OPERATION.LOCKED.GUARD_PERMITTED"
473 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
474 $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
475 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
476 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId(), "Permit".equalsIgnoreCase(getGuardApprovalStatus()) )
477 $lock : TargetLock (requestID == $event.getRequestId())
478 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
481 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
482 logger.info("{}: {}: event={} manager={} operation={} lock={}",
483 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
484 $event, $manager, $operation, $lock);
486 Object request = null;
487 boolean caughtException = false;
490 request = $operation.startOperation($event);
492 if (request != null) {
493 logger.debug("{}: {}: starting operation ..",
495 $params.getPolicyName() + "." + drools.getRule().getName());
497 // Tell interested parties we are performing this Operation
499 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
500 notification.setNotification(ControlLoopNotificationType.OPERATION);
501 notification.setMessage($operation.getOperationMessage());
502 notification.setHistory($operation.getHistory());
503 notification.setFrom("policy");
504 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
505 notification.setPolicyScope($params.getPolicyScope());
506 notification.setPolicyVersion($params.getPolicyVersion());
508 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
510 switch ($operation.policy.getActor()){
514 if (request instanceof Request) {
515 PolicyEngine.manager.deliver("APPC-CL", request);
517 else if (request instanceof LcmRequestWrapper) {
518 PolicyEngine.manager.deliver("APPC-LCM-READ", request);
522 // at this point the AAI named query request should have already been made, the response recieved and used
523 // in the construction of the SO Request which is stored in operationRequest
525 if(request instanceof SORequest) {
526 // Call SO. The response will be inserted into memory once it's received
527 SOActorServiceProvider.sendRequest($event.getRequestId().toString(), drools.getWorkingMemory(), request);
531 if (request instanceof VFCRequest) {
533 Thread t = new Thread(new VFCManager(drools.getWorkingMemory(), (VFCRequest)request));
540 // What happens if its null?
542 logger.warn("{}: {}: unexpected null operation request",
544 $params.getPolicyName() + "." + drools.getRule().getName());
545 if ("SO".equals($operation.policy.getActor())) {
548 modify($manager) {finishOperation($operation)};
550 else if ("vfc".equalsIgnoreCase($operation.policy.getActor())) {
553 modify($manager) {finishOperation($operation)};
557 } catch (Exception e) {
558 String msg = e.getMessage();
559 logger.warn("{}: {}: operation={}: AAI failure: {}",
561 $params.getPolicyName() + "." + drools.getRule().getName(),
563 $operation.setOperationHasException(msg);
565 if(request != null) {
567 // Create a notification for it ("DB Write - end operation")
569 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
570 notification.setFrom("policy");
571 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
572 notification.setPolicyScope($params.getPolicyScope());
573 notification.setPolicyVersion($params.getPolicyVersion());
574 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
575 notification.setMessage($operation.getOperationHistory());
576 notification.setHistory($operation.getHistory());
578 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
583 caughtException = true;
586 // Having the modify statement in the catch clause doesn't work for whatever reason
587 if (caughtException) {
588 modify($manager) {finishOperation($operation)};
595 * We were able to acquire a lock so now let's ask Xacml Guard whether
596 * we are allowed to proceed with the request to the actor.
599 rule "EVENT.MANAGER.OPERATION.LOCKED.GUARD_NOT_YET_QUERIED"
601 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
602 $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
603 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
604 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId(), getGuardApprovalStatus() == "NONE" )
605 $lock : TargetLock (requestID == $event.getRequestId())
608 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
609 logger.info("{}: {}: event={} manager={} operation={} lock={}",
610 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
611 $event, $manager, $operation, $lock);
614 // Sending notification that we are about to query Guard ("DB write - start operation")
616 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
617 notification.setNotification(ControlLoopNotificationType.OPERATION);
618 notification.setMessage("Sending guard query for " + $operation.policy.getActor() + " " + $operation.policy.getRecipe());
619 notification.setHistory($operation.getHistory());
620 notification.setFrom("policy");
621 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
622 notification.setPolicyScope($params.getPolicyScope());
623 notification.setPolicyVersion($params.getPolicyVersion());
625 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
628 // Now send Guard Request to XACML Guard. In order to bypass the call to Guard,
629 // just change guardEnabled to false.
631 // In order to use REST XACML, provide a URL instead of "" as a second argument
632 // to the CallGuardTask() and set the first argument to null
633 // (instead of XacmlPdpEngine).
636 // NOTE: The environment properties uses "guard.disabled" but the boolean is guardEnabled
637 boolean guardEnabled = "false".equalsIgnoreCase(PolicyEngine.manager.getEnvironmentProperty("guard.disabled"));
641 Thread t = new Thread(new org.onap.policy.guard.CallGuardTask(
642 drools.getWorkingMemory(),
643 $event.getClosedLoopControlName(),
644 $operation.policy.getActor().toString(),
645 $operation.policy.getRecipe(),
646 $operation.getTargetEntity(),
647 $event.getRequestId().toString()
652 insert(new PolicyGuardResponse("Permit", $event.getRequestId(), $operation.policy.getRecipe()));
658 // This rule will be triggered when a thread talking to the XACML Guard inserts a
659 // guardResponse object into the working memory
661 rule "GUARD.RESPONSE"
663 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
664 $event : VirtualControlLoopEvent( closedLoopControlName == $clName, closedLoopEventStatus == ControlLoopEventStatus.ONSET )
665 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
666 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() )
667 $lock : TargetLock (requestID == $event.getRequestId())
668 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
669 $guardResponse : PolicyGuardResponse(requestID == $event.getRequestId(), $operation.policy.recipe == operation)
672 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
673 logger.info("{}: {}: event={} manager={} operation={} lock={} opTimer={} guardResponse={}",
674 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
675 $event, $manager, $operation, $lock, $opTimer, $guardResponse);
678 //we will permit the operation if there was no Guard for it
679 if("Indeterminate".equalsIgnoreCase($guardResponse.getResult())){
680 $guardResponse.setResult("Permit");
684 // This notification has Guard result in "message". ("DB write - end operation in case of Guard Deny")
686 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
687 notification.setNotification(ControlLoopNotificationType.OPERATION);
688 notification.setMessage("Guard result for " + $operation.policy.getActor() + " " + $operation.policy.getRecipe() + " is " + $guardResponse.getResult());
689 notification.setHistory($operation.getHistory());
690 notification.setFrom("policy");
691 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
692 notification.setPolicyScope($params.getPolicyScope());
693 notification.setPolicyVersion($params.getPolicyVersion());
695 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
697 if("Permit".equalsIgnoreCase($guardResponse.getResult())){
699 modify($operation){setGuardApprovalStatus($guardResponse.getResult())};
702 //This is the Deny case
703 $operation.startOperation($event);
704 $operation.setOperationHasGuardDeny();
707 modify($manager) {finishOperation($operation)};
710 retract($guardResponse);
716 * This rule responds to APPC Response Events
718 * I would have like to be consistent and write the Response like this:
719 * $response : Response( CommonHeader.RequestId == $onset.getRequestId() )
721 * However, no compile error was given. But a runtime error was given. I think
722 * because drools is confused between the classname CommonHeader vs the property CommonHeader.
727 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
728 $event : VirtualControlLoopEvent( closedLoopControlName == $clName, closedLoopEventStatus == ControlLoopEventStatus.ONSET )
729 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
730 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() )
731 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
732 $lock : TargetLock (requestID == $event.getRequestId())
733 $response : Response( getCommonHeader().RequestId == $event.getRequestId() )
736 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
737 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
738 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
739 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
740 $event, $manager, $operation, $lock, $opTimer, $response);
742 // Get the result of the operation
744 PolicyResult policyResult = $operation.onResponse($response);
745 if (policyResult != null) {
746 logger.debug("{}: {}: operation finished - result={}",
747 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
750 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
752 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
753 notification.setFrom("policy");
754 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
755 notification.setPolicyScope($params.getPolicyScope());
756 notification.setPolicyVersion($params.getPolicyVersion());
757 notification.setMessage($operation.getOperationHistory());
758 notification.setHistory($operation.getHistory());
759 if (policyResult.equals(PolicyResult.SUCCESS)) {
760 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
762 // Let interested parties know
764 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
766 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
768 // Let interested parties know
770 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
773 // Ensure the operation is complete
775 if ($operation.isOperationComplete() == true) {
777 // It is complete, remove it from memory
781 // We must also retract the timer object
782 // NOTE: We could write a Rule to do this
786 // Complete the operation
788 modify($manager) {finishOperation($operation)};
791 // Just doing this will kick off the LOCKED rule again
793 modify($operation) {};
797 // Its not finished yet (i.e. expecting more Response objects)
799 // Or possibly it is a leftover response that we timed the request out previously
803 // We are going to retract these objects from memory
810 * The problem with Responses is that they don't have a controlLoopControlName
811 * field in them, so the only way to attach them is via RequestID. If we have multiple
812 * control loop .drl's loaded in the same container, we need to be sure the cleanup
813 * rules don't remove Responses for other control loops.
816 rule "APPC.RESPONSE.CLEANUP"
818 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
819 $response : Response($id : getCommonHeader().RequestId )
820 not ( VirtualControlLoopEvent( requestId == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
823 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
824 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
825 logger.debug("{}: {}: orphan appc response={}",
826 $clName, $params.getPolicyName() + "." + drools.getRule().getName(), $id);
836 * This rule responds to APPC Response Events using the new LCM interface provided by appc
839 rule "APPC.LCM.RESPONSE"
841 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
842 $event : VirtualControlLoopEvent( closedLoopControlName == $clName, closedLoopEventStatus == ControlLoopEventStatus.ONSET )
843 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
844 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() )
845 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
846 $lock : TargetLock (requestID == $event.getRequestId())
847 $response : LcmResponseWrapper( getBody().getCommonHeader().getRequestId() == $event.getRequestId() )
850 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
851 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
852 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
853 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
854 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
857 // Get the result of the operation
859 PolicyResult policyResult = $operation.onResponse($response);
860 if (policyResult != null) {
861 logger.debug("{}: {}: operation finished - result={}",
862 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
866 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
868 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
869 notification.setFrom("policy");
870 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
871 notification.setPolicyScope($params.getPolicyScope());
872 notification.setPolicyVersion($params.getPolicyVersion());
873 notification.setMessage($operation.getOperationHistory());
874 notification.setHistory($operation.getHistory());
875 if (policyResult.equals(PolicyResult.SUCCESS)) {
876 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
878 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
880 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
882 // Ensure the operation is complete
884 if ($operation.isOperationComplete() == true) {
886 // It is complete, remove it from memory
890 // We must also retract the timer object
891 // NOTE: We could write a Rule to do this
895 // Complete the operation
897 modify($manager) {finishOperation($operation)};
900 // Just doing this will kick off the LOCKED rule again
902 modify($operation) {};
906 // Its not finished yet (i.e. expecting more Response objects)
908 // Or possibly it is a leftover response that we timed the request out previously
912 // We are going to retract these objects from memory
919 * Clean Up any lingering LCM reponses
922 rule "APPC.LCM.RESPONSE.CLEANUP"
924 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
925 $response : LcmResponseWrapper($id : getBody().getCommonHeader().getRequestId )
926 not ( VirtualControlLoopEvent( requestId == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
929 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
930 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
931 logger.debug("{}: {}: orphan appc response={}",
932 $clName, $params.getPolicyName() + "." + drools.getRule().getName(), $id);
941 * This rule responds to SO Response Events
946 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
947 $event : VirtualControlLoopEvent( closedLoopControlName == $clName, closedLoopEventStatus == ControlLoopEventStatus.ONSET )
948 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
949 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() )
950 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
951 $lock : TargetLock (requestID == $event.getRequestId())
952 $response : SOResponseWrapper(requestID.toString() == $event.getRequestId().toString() )
955 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
956 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
957 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
958 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
959 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
961 // Get the result of the operation
963 PolicyResult policyResult = $operation.onResponse($response);
964 if (policyResult != null) {
965 logger.debug("{}: {}: operation finished - result={}",
966 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
970 // This Operation has completed, construct a notification showing our results
972 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
973 notification.setFrom("policy");
974 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
975 notification.setPolicyScope($params.getPolicyScope());
976 notification.setPolicyVersion($params.getPolicyVersion());
977 notification.setMessage($operation.getOperationHistory());
978 notification.setHistory($operation.getHistory());
979 if (policyResult.equals(PolicyResult.SUCCESS)) {
980 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
982 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
985 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
987 // Ensure the operation is complete
989 if ($operation.isOperationComplete() == true) {
991 // It is complete, remove it from memory
995 // We must also retract the timer object
996 // NOTE: We could write a Rule to do this
1000 // Complete the operation
1002 modify($manager) {finishOperation($operation)};
1005 // Just doing this will kick off the LOCKED rule again
1007 modify($operation) {};
1011 // Its not finished yet (i.e. expecting more Response objects)
1013 // Or possibly it is a leftover response that we timed the request out previously
1017 // We are going to retract these objects from memory
1025 * This rule responds to VFC Response Events
1030 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
1031 $event : VirtualControlLoopEvent( closedLoopControlName == $clName, closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1032 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
1033 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() )
1034 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
1035 $lock : TargetLock (requestID == $event.getRequestId())
1036 $response : VFCResponse( requestId.toString() == $event.getRequestId().toString() )
1038 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1039 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
1040 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
1041 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
1042 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
1044 // Get the result of the operation
1046 PolicyResult policyResult = $operation.onResponse($response);
1047 if (policyResult != null) {
1049 // This Operation has completed, construct a notification showing our results
1051 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1052 notification.setFrom("policy");
1053 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
1054 notification.setPolicyScope($params.getPolicyScope());
1055 notification.setPolicyVersion($params.getPolicyVersion());
1056 notification.setMessage($operation.getOperationHistory());
1057 notification.setHistory($operation.getHistory());
1059 // Ensure the operation is complete
1061 if ($operation.isOperationComplete() == true) {
1063 // It is complete, remove it from memory
1065 retract($operation);
1067 // We must also retract the timer object
1068 // NOTE: We could write a Rule to do this
1072 // Complete the operation
1074 modify($manager) {finishOperation($operation)};
1077 // Just doing this will kick off the LOCKED rule again
1079 modify($operation) {};
1083 // Its not finished yet (i.e. expecting more Response objects)
1085 // Or possibly it is a leftover response that we timed the request out previously
1089 // We are going to retract these objects from memory
1097 * This is the timer that manages the timeout for an individual operation.
1100 rule "EVENT.MANAGER.OPERATION.TIMEOUT"
1103 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
1104 $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
1105 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
1106 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() )
1107 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString(), $to : getDelay() )
1108 $lock : TargetLock (requestID == $event.getRequestId())
1111 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1112 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
1113 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={}",
1114 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
1115 $event, $manager, $operation, $lock, $operation, $opTimer);
1118 // Tell it its timed out
1120 $operation.setOperationHasTimedOut();
1122 // Create a notification for it ("DB Write - end operation")
1124 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1125 notification.setFrom("policy");
1126 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
1127 notification.setPolicyScope($params.getPolicyScope());
1128 notification.setPolicyVersion($params.getPolicyVersion());
1129 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
1130 notification.setMessage($operation.getOperationHistory());
1131 notification.setHistory($operation.getHistory());
1133 // Let interested parties know
1135 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1137 // Get rid of the timer
1141 // Ensure the operation is complete
1143 if ($operation.isOperationComplete() == true) {
1145 // It is complete, remove it from memory
1147 retract($operation);
1149 // Complete the operation
1151 modify($manager) {finishOperation($operation)};
1154 // Just doing this will kick off the LOCKED rule again
1156 modify($operation) {};
1162 * This is the timer that manages the overall control loop timeout.
1165 rule "EVENT.MANAGER.TIMEOUT"
1168 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
1169 $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
1170 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
1171 $clTimer : ControlLoopTimer ( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString(), $to : getDelay() )
1174 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1175 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
1177 logger.debug("{}: {}: event={}",
1178 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
1181 // Tell the Event Manager it has timed out
1183 VirtualControlLoopNotification notification = $manager.setControlLoopTimedOut();
1184 if (notification != null) {
1185 notification.setFrom("policy");
1186 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
1187 notification.setPolicyScope($params.getPolicyScope());
1188 notification.setPolicyVersion($params.getPolicyVersion());
1190 // Let interested parties know
1192 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1195 // Retract the event
1202 * This rule cleans up the manager and other objects after an event has
1206 rule "EVENT.MANAGER.CLEANUP"
1208 $manager : ControlLoopEventManager( $clName : getClosedLoopControlName(), $requestId : getRequestID() )
1209 $clTimer : ControlLoopTimer ( closedLoopControlName == $clName, requestID == $requestId.toString() )
1210 $operations : LinkedList()
1211 from collect( ControlLoopOperationManager( onset.closedLoopControlName == $clName, onset.getRequestId() == $requestId ) )
1212 $opTimers : LinkedList()
1213 from collect( OperationTimer( closedLoopControlName == $clName, requestID == $requestId.toString() ) )
1214 $locks : LinkedList()
1215 from collect( TargetLock (requestID == $requestId) )
1216 not( VirtualControlLoopEvent( closedLoopControlName == $clName, requestId == $requestId ) )
1219 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1220 logger.info("{}: {}", $clName, drools.getRule().getName());
1222 logger.debug("{}: {}: manager={} clTimer={} operations={}",
1223 $clName, drools.getRule().getName(),
1224 $manager, $clTimer, $operations.size());
1227 // Retract EVERYTHING
1232 for(Object manager: $operations) {
1233 retract((ControlLoopOperationManager) manager);
1235 for(Object opTimer: $opTimers) {
1236 retract((OperationTimer) opTimer);
1238 for(Object lock: $locks) {
1239 TargetLock tgt = (TargetLock) lock;
1241 // Ensure we release the lock
1243 PolicyGuard.unlockTarget(tgt);
1250 * This rule will clean up any rogue onsets where there is no
1251 * ControlLoopParams object corresponding to the onset event.
1254 rule "EVENT.CLEANUP"
1256 $event : VirtualControlLoopEvent( $clName: closedLoopControlName )
1257 not ( ControlLoopParams( getClosedLoopControlName() == $clName) )
1260 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1261 logger.info("{}: {}", $clName, drools.getRule().getName());
1262 logger.debug("{}: {}: orphan onset event={}",
1263 $clName, drools.getRule().getName(), $event);