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.aai.AaiNqResponseWrapper;
37 import org.onap.policy.appc.Request;
38 import org.onap.policy.appc.Response;
39 import org.onap.policy.appc.CommonHeader;
40 import org.onap.policy.appclcm.LcmRequestWrapper;
41 import org.onap.policy.appclcm.LcmResponseWrapper;
42 import org.onap.policy.appclcm.LcmRequest;
43 import org.onap.policy.appclcm.LcmResponse;
44 import org.onap.policy.appclcm.LcmCommonHeader;
45 import org.onap.policy.sdnr.PciRequestWrapper;
46 import org.onap.policy.sdnr.PciResponseWrapper;
47 import org.onap.policy.sdnr.PciRequest;
48 import org.onap.policy.sdnr.PciResponse;
49 import org.onap.policy.vfc.VFCRequest;
50 import org.onap.policy.vfc.VFCResponse;
51 import org.onap.policy.vfc.VFCManager;
52 import org.onap.policy.so.SOManager;
53 import org.onap.policy.so.SORequest;
54 import org.onap.policy.so.SORequestStatus;
55 import org.onap.policy.so.SORequestDetails;
56 import org.onap.policy.so.SOModelInfo;
57 import org.onap.policy.so.SOCloudConfiguration;
58 import org.onap.policy.so.SORequestInfo;
59 import org.onap.policy.so.SORequestParameters;
60 import org.onap.policy.so.SORelatedInstanceListElement;
61 import org.onap.policy.so.SORelatedInstance;
62 import org.onap.policy.so.SOResponse;
63 import org.onap.policy.so.SOResponseWrapper;
64 import org.onap.policy.sdnc.SdncRequest;
65 import org.onap.policy.sdnc.SdncManager;
66 import org.onap.policy.sdnc.SdncResponse;
67 import org.onap.policy.guard.PolicyGuard;
68 import org.onap.policy.guard.PolicyGuard.LockResult;
69 import org.onap.policy.guard.TargetLock;
70 import org.onap.policy.guard.GuardResult;
71 import org.onap.policy.guard.PolicyGuardRequest;
72 import org.onap.policy.guard.PolicyGuardResponse;
73 import org.onap.policy.guard.PolicyGuardXacmlRequestAttributes;
74 import org.onap.policy.guard.PolicyGuardXacmlHelper;
76 import org.yaml.snakeyaml.Yaml;
77 import org.yaml.snakeyaml.constructor.Constructor;
79 import org.slf4j.LoggerFactory;
80 import org.slf4j.Logger;
82 import java.time.Instant;
83 import java.util.LinkedList;
84 import java.util.Iterator;
86 import org.onap.policy.drools.system.PolicyEngine;
89 * This object is to provide support for timeouts
90 * due to a bug in drools' built-in timers
92 declare ControlLoopTimer
93 closedLoopControlName : String
97 //timerType is the type of timer: either "ClosedLoop" or "Operation"
103 * Called when the ControlLoopParams object has been inserted into working memory from the BRMSGW.
108 $params : ControlLoopParams()
111 // Note: globals have bad behavior when persistence is used,
112 // hence explicitly getting the logger vs using a global
114 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
115 logger.info("{}: {} : YAML=[{}]", $params.getClosedLoopControlName(), $params.getPolicyName() + "."
116 + drools.getRule().getName(), $params.getControlLoopYaml());
121 * This rule responds to DCAE Events where there is no manager yet. Either it is
122 * the first ONSET, or a subsequent badly formed Event (i.e. Syntax error, or is-closed-loop-disabled)
127 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
128 $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
129 not ( ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
130 requestID == $event.getRequestId() ) )
133 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
134 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
139 // Check the event, because we need it to not be null when
140 // we create the ControlLoopEventManager. The ControlLoopEventManager
141 // will do extra syntax checking as well check if the closed loop is disabled.
143 if ($event.getRequestId() == null) {
144 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
145 notification.setNotification(ControlLoopNotificationType.REJECTED);
146 notification.setFrom("policy");
147 notification.setMessage("Missing requestID");
148 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
149 notification.setPolicyScope($params.getPolicyScope());
150 notification.setPolicyVersion($params.getPolicyVersion());
153 // Let interested parties know
155 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
158 // Retract it from memory
161 } else if ($event.getClosedLoopEventStatus() != ControlLoopEventStatus.ONSET) {
162 throw new ControlLoopException($event.getClosedLoopEventStatus() + " received with no prior onset");
165 // Create an EventManager
167 ControlLoopEventManager manager = new ControlLoopEventManager($clName, $event.getRequestId());
169 // Determine if EventManager can actively process the event
170 // (i.e. syntax, is_closed_loop_disabled checks etc.)
172 VirtualControlLoopNotification notification = manager.activate($params.getControlLoopYaml(), $event);
173 notification.setFrom("pdp-0001-controller=controlloop"); // Engine.getInstanceName()
174 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
175 notification.setPolicyScope($params.getPolicyScope());
176 notification.setPolicyVersion($params.getPolicyVersion());
178 // Are we actively pursuing this event?
180 if (notification.getNotification() == ControlLoopNotificationType.ACTIVE) {
182 // Insert Event Manager into memory, this will now kick off processing.
186 // Let interested parties know
188 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
190 // Setup the Overall Control Loop timer
192 ControlLoopTimer clTimer = new ControlLoopTimer();
193 clTimer.setTimerType("ClosedLoop");
194 clTimer.setClosedLoopControlName($event.getClosedLoopControlName());
195 clTimer.setRequestID($event.getRequestId().toString());
196 clTimer.setDelay(manager.getControlLoopTimeout(1500) + "s");
203 // Let interested parties know
205 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
207 // Retract it from memory
213 // Now that the manager is inserted into Drools working memory, we'll wait for
214 // another rule to fire in order to continue processing. This way we can also
215 // then screen for additional ONSET and ABATED events for this RequestID.
218 } catch (Exception e) {
219 logger.warn("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName(), e);
221 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
222 notification.setNotification(ControlLoopNotificationType.REJECTED);
223 notification.setMessage("Exception occurred: " + e.getMessage());
224 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
225 notification.setPolicyScope($params.getPolicyScope());
226 notification.setPolicyVersion($params.getPolicyVersion());
230 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
240 * This rule happens when we got a valid ONSET, closed loop is enabled and an Event Manager
241 * is now created. We can start processing the yaml specification via the Event Manager.
246 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
247 $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
248 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
249 requestID == $event.getRequestId() )
250 $clTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
251 requestID == $event.getRequestId().toString(), timerType == "ClosedLoop", !expired )
254 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
255 logger.info("{}: {}: event={} manager={} clTimer={}",
256 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
257 $event, $manager, $clTimer);
261 // Check which event this is.
263 ControlLoopEventManager.NEW_EVENT_STATUS eventStatus = $manager.onNewEvent($event);
265 // Check what kind of event this is
267 if (eventStatus == NEW_EVENT_STATUS.SUBSEQUENT_ONSET) {
269 // We don't care about subsequent onsets
271 logger.info("{}: {}: subsequent onset",
272 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
276 if (eventStatus == NEW_EVENT_STATUS.SYNTAX_ERROR) {
278 // Ignore any bad syntax events
280 logger.warn("{}: {}: syntax error",
281 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
286 // We only want the initial ONSET event in memory,
287 // all the other events need to be retracted to support
288 // cleanup and avoid the other rules being fired for this event.
290 if (eventStatus != NEW_EVENT_STATUS.FIRST_ONSET) {
291 logger.warn("{}: {}: no first onset",
292 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
296 logger.debug("{}: {}: target={}", $clName,
297 $params.getPolicyName() + "." + drools.getRule().getName(), $event.getTarget());
299 // Now start seeing if we need to process this event
303 // Check if this is a Final Event
305 VirtualControlLoopNotification notification = $manager.isControlLoopFinal();
308 if (notification != null) {
310 // Its final, but are we waiting for abatement?
312 if ($manager.getNumAbatements() > 0) {
313 logger.info("{}: {}: abatement received for {}. Closing the control loop",
314 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
315 $event.getRequestId());
316 notification.setFrom("policy");
317 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
318 notification.setPolicyScope($params.getPolicyScope());
319 notification.setPolicyVersion($params.getPolicyVersion());
321 // In this case, we are done
323 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
327 TargetLock lock = $manager.unlockCurrentOperation();
329 logger.debug("{}: {}: retracting lock=", $clName,
330 $params.getPolicyName() + "." + drools.getRule().getName(), lock);
334 // Retract everything from memory
336 logger.info("{}: {}: retracting onset, manager, and timer",
337 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
339 retract($manager.getOnsetEvent());
343 // TODO - what if we get subsequent Events for this RequestID?
344 // By default, it will all start over again. May be confusing for Ruby.
345 // Or, we could track this and then subsequently ignore the events
349 // Check whether we need to wait for abatement
351 if ($manager.getProcessor().getControlLoop().getAbatement() == true && notification.getNotification() == ControlLoopNotificationType.FINAL_SUCCESS) {
352 logger.info("{}: {}: waiting for abatement ..",
353 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
355 logger.info("{}: {}: no abatement expect for {}. Closing the control loop",
356 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
357 $event.getRequestId());
359 notification.setFrom("policy");
360 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
361 notification.setPolicyScope($params.getPolicyScope());
362 notification.setPolicyVersion($params.getPolicyVersion());
365 // In this case, we are done
367 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
371 TargetLock lock = $manager.unlockCurrentOperation();
373 logger.debug("{}: {}: retracting lock=", $clName,
374 $params.getPolicyName() + "." + drools.getRule().getName(), lock);
378 // Retract everything from memory
380 logger.info("{}: {}: retracting onset, manager, and timer",
381 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
383 retract($manager.getOnsetEvent());
390 // NOT final, so let's ask for the next operation
392 ControlLoopOperationManager operation = $manager.processControlLoop();
393 if (operation != null) {
395 // Let's ask for a lock right away
397 LockResult<GuardResult, TargetLock> result = $manager.lockCurrentOperation();
398 logger.info("{}: {}: guard lock acquired={}",
399 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
401 if (result.getA().equals(GuardResult.LOCK_ACQUIRED)) {
403 // insert the operation into memory
408 // insert operation timeout object
410 ControlLoopTimer opTimer = new ControlLoopTimer();
411 opTimer.setTimerType("Operation");
412 opTimer.setClosedLoopControlName($event.getClosedLoopControlName());
413 opTimer.setRequestID($event.getRequestId().toString());
414 opTimer.setDelay(operation.getOperationTimeout().toString() + "s");
418 // Insert lock into memory
420 insert(result.getB());
423 logger.debug("The target resource {} is already processing",
424 $event.getAai().get($event.getTarget()));
425 notification = new VirtualControlLoopNotification($event);
426 notification.setNotification(ControlLoopNotificationType.REJECTED);
427 notification.setMessage("The target " + $event.getAai().get($event.getTarget())
428 + " is already locked");
429 notification.setFrom("policy");
430 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
431 notification.setPolicyScope($params.getPolicyScope());
432 notification.setPolicyVersion($params.getPolicyVersion());
434 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
440 if(result.getB() != null) {
441 retract(result.getB());
444 logger.info("{}: {}: starting operation={}",
445 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
449 // Probably waiting for abatement
451 logger.info("{}: {}: no operation, probably waiting for abatement",
452 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
455 } catch (Exception e) {
456 logger.warn("{}: {}: unexpected",
458 $params.getPolicyName() + "." + drools.getRule().getName(), e);
460 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
461 notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
462 notification.setMessage(e.getMessage());
463 notification.setFrom("policy");
464 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
465 notification.setPolicyScope($params.getPolicyScope());
466 notification.setPolicyVersion($params.getPolicyVersion());
468 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
479 * Guard Permitted, let's send request to the actor.
482 rule "EVENT.MANAGER.OPERATION.LOCKED.GUARD_PERMITTED"
484 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
485 $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
486 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
487 requestID == $event.getRequestId() )
488 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
489 onset.getRequestId() == $event.getRequestId(), "Permit".equalsIgnoreCase(getGuardApprovalStatus()) )
490 $lock : TargetLock (requestID == $event.getRequestId())
491 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
492 requestID == $event.getRequestId().toString(), timerType == "Operation", !expired )
495 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
496 logger.info("{}: {}: event={} manager={} operation={} lock={}",
497 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
498 $event, $manager, $operation, $lock);
500 Object request = null;
501 boolean caughtException = false;
504 request = $operation.startOperation($event);
506 if (request != null) {
507 logger.debug("{}: {}: starting operation ..",
509 $params.getPolicyName() + "." + drools.getRule().getName());
511 // Tell interested parties we are performing this Operation
513 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
514 notification.setNotification(ControlLoopNotificationType.OPERATION);
515 notification.setMessage($operation.getOperationMessage());
516 notification.setHistory($operation.getHistory());
517 notification.setFrom("policy");
518 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
519 notification.setPolicyScope($params.getPolicyScope());
520 notification.setPolicyVersion($params.getPolicyVersion());
522 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
524 switch ($operation.policy.getActor()){
528 if (request instanceof Request) {
529 PolicyEngine.manager.deliver("APPC-CL", request);
531 else if (request instanceof LcmRequestWrapper) {
532 PolicyEngine.manager.deliver("APPC-LCM-READ", request);
536 // at this point the AAI named query request should have already been made, the response
537 // recieved and used in the construction of the SO Request which is stored in operationRequest
539 if(request instanceof SORequest) {
540 // Call SO. The response will be inserted into memory once it's received
541 SoActorServiceProvider.sendRequest($event.getRequestId().toString(), drools.getWorkingMemory(), request);
545 if (request instanceof VFCRequest) {
547 Thread t = new Thread(new VFCManager(drools.getWorkingMemory(), (VFCRequest)request));
552 if (request instanceof PciRequestWrapper) {
553 PolicyEngine.manager.deliver("SDNR-CL", request);
558 if (request instanceof SdncRequest) {
560 Thread t = new Thread(new SdncManager(drools.getWorkingMemory(), (SdncRequest)request));
567 // What happens if its null?
569 logger.warn("{}: {}: unexpected null operation request",
571 $params.getPolicyName() + "." + drools.getRule().getName());
572 if ("SO".equals($operation.policy.getActor())) {
575 modify($manager) {finishOperation($operation)};
577 else if ("vfc".equalsIgnoreCase($operation.policy.getActor())) {
580 modify($manager) {finishOperation($operation)};
582 else if ("sdnc".equalsIgnoreCase($operation.policy.getActor())) {
585 modify($manager) {finishOperation($operation)};
589 } catch (Exception e) {
590 String msg = e.getMessage();
591 logger.warn("{}: {}: operation={}: AAI failure: {}",
593 $params.getPolicyName() + "." + drools.getRule().getName(),
595 $operation.setOperationHasException(msg);
597 if(request != null) {
599 // Create a notification for it ("DB Write - end operation")
601 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
602 notification.setFrom("policy");
603 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
604 notification.setPolicyScope($params.getPolicyScope());
605 notification.setPolicyVersion($params.getPolicyVersion());
606 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
607 notification.setMessage($operation.getOperationHistory());
608 notification.setHistory($operation.getHistory());
610 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
615 caughtException = true;
618 // Having the modify statement in the catch clause doesn't work for whatever reason
619 if (caughtException) {
620 modify($manager) {finishOperation($operation)};
627 * We were able to acquire a lock so now let's ask Xacml Guard whether
628 * we are allowed to proceed with the request to the actor.
631 rule "EVENT.MANAGER.OPERATION.LOCKED.GUARD_NOT_YET_QUERIED"
633 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
634 $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
635 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
636 requestID == $event.getRequestId() )
637 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
638 onset.getRequestId() == $event.getRequestId(), getGuardApprovalStatus() == "NONE" )
639 $lock : TargetLock (requestID == $event.getRequestId())
642 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
643 logger.info("{}: {}: event={} manager={} operation={} lock={}",
644 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
645 $event, $manager, $operation, $lock);
648 // Sending notification that we are about to query Guard ("DB write - start operation")
650 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
651 notification.setNotification(ControlLoopNotificationType.OPERATION);
652 notification.setMessage("Sending guard query for " + $operation.policy.getActor() + " "
653 + $operation.policy.getRecipe());
654 notification.setHistory($operation.getHistory());
655 notification.setFrom("policy");
656 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
657 notification.setPolicyScope($params.getPolicyScope());
658 notification.setPolicyVersion($params.getPolicyVersion());
660 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
663 // Now send Guard Request to XACML Guard. In order to bypass the call to Guard,
664 // just change guardEnabled to false.
666 // In order to use REST XACML, provide a URL instead of "" as a second argument
667 // to the CallGuardTask() and set the first argument to null
668 // (instead of XacmlPdpEngine).
671 // NOTE: The environment properties uses "guard.disabled" but the boolean is guardEnabled
672 boolean guardEnabled = "false".equalsIgnoreCase(PolicyEngine.manager.getEnvironmentProperty("guard.disabled"));
676 Thread t = new Thread(new org.onap.policy.guard.CallGuardTask(
677 drools.getWorkingMemory(),
678 $event.getClosedLoopControlName(),
679 $operation.policy.getActor().toString(),
680 $operation.policy.getRecipe(),
681 $operation.getTargetEntity(),
682 $event.getRequestId().toString(),
684 AaiNqResponseWrapper resp = $manager.getNqVserverFromAai();
685 return(resp == null ? null : resp.countVfModules());
690 insert(new PolicyGuardResponse("Permit", $event.getRequestId(), $operation.policy.getRecipe()));
696 // This rule will be triggered when a thread talking to the XACML Guard inserts a
697 // guardResponse object into the working memory
699 rule "GUARD.RESPONSE"
701 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
702 $event : VirtualControlLoopEvent( closedLoopControlName == $clName,
703 closedLoopEventStatus == ControlLoopEventStatus.ONSET )
704 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
705 requestID == $event.getRequestId() )
706 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
707 onset.getRequestId() == $event.getRequestId() )
708 $lock : TargetLock (requestID == $event.getRequestId())
709 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
710 requestID == $event.getRequestId().toString(), timerType == "Operation", !expired )
711 $guardResponse : PolicyGuardResponse(requestID == $event.getRequestId(), $operation.policy.recipe == operation)
714 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
715 logger.info("{}: {}: event={} manager={} operation={} lock={} opTimer={} guardResponse={}",
716 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
717 $event, $manager, $operation, $lock, $opTimer, $guardResponse);
720 //we will permit the operation if there was no Guard for it
721 if("Indeterminate".equalsIgnoreCase($guardResponse.getResult())){
722 $guardResponse.setResult("Permit");
726 // This notification has Guard result in "message". ("DB write - end operation in case of Guard Deny")
728 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
729 notification.setNotification(ControlLoopNotificationType.OPERATION);
730 notification.setMessage("Guard result for " + $operation.policy.getActor() + " " + $operation.policy.getRecipe()
731 + " is " + $guardResponse.getResult());
732 notification.setHistory($operation.getHistory());
733 notification.setFrom("policy");
734 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
735 notification.setPolicyScope($params.getPolicyScope());
736 notification.setPolicyVersion($params.getPolicyVersion());
738 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
740 if("Permit".equalsIgnoreCase($guardResponse.getResult())){
742 modify($operation){setGuardApprovalStatus($guardResponse.getResult())};
745 //This is the Deny case
746 $operation.startOperation($event);
747 $operation.setOperationHasGuardDeny();
750 modify($manager) {finishOperation($operation)};
753 retract($guardResponse);
759 * This rule responds to APPC Response Events
761 * I would have like to be consistent and write the Response like this:
762 * $response : Response( CommonHeader.RequestId == $onset.getRequestId() )
764 * However, no compile error was given. But a runtime error was given. I think
765 * because drools is confused between the classname CommonHeader vs the property CommonHeader.
770 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
771 $event : VirtualControlLoopEvent( closedLoopControlName == $clName,
772 closedLoopEventStatus == ControlLoopEventStatus.ONSET )
773 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
774 requestID == $event.getRequestId() )
775 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
776 onset.getRequestId() == $event.getRequestId() )
777 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
778 requestID == $event.getRequestId().toString(), timerType == "Operation", !expired )
779 $lock : TargetLock (requestID == $event.getRequestId())
780 $response : Response( getCommonHeader().RequestId == $event.getRequestId() )
783 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
784 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
785 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
786 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
787 $event, $manager, $operation, $lock, $opTimer, $response);
789 // Get the result of the operation
791 PolicyResult policyResult = $operation.onResponse($response);
792 if (policyResult != null) {
793 logger.debug("{}: {}: operation finished - result={}",
794 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
797 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
799 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
800 notification.setFrom("policy");
801 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
802 notification.setPolicyScope($params.getPolicyScope());
803 notification.setPolicyVersion($params.getPolicyVersion());
804 notification.setMessage($operation.getOperationHistory());
805 notification.setHistory($operation.getHistory());
806 if (policyResult.equals(PolicyResult.SUCCESS)) {
807 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
809 // Let interested parties know
811 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
813 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
815 // Let interested parties know
817 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
820 // Ensure the operation is complete
822 if ($operation.isOperationComplete() == true) {
824 // It is complete, remove it from memory
828 // We must also retract the timer object
829 // NOTE: We could write a Rule to do this
833 // Complete the operation
835 modify($manager) {finishOperation($operation)};
838 // Just doing this will kick off the LOCKED rule again
840 modify($operation) {};
844 // Its not finished yet (i.e. expecting more Response objects)
846 // Or possibly it is a leftover response that we timed the request out previously
850 // We are going to retract these objects from memory
857 * The problem with Responses is that they don't have a controlLoopControlName
858 * field in them, so the only way to attach them is via RequestID. If we have multiple
859 * control loop .drl's loaded in the same container, we need to be sure the cleanup
860 * rules don't remove Responses for other control loops.
863 rule "APPC.RESPONSE.CLEANUP"
865 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
866 $response : Response($id : getCommonHeader().RequestId )
867 not ( VirtualControlLoopEvent( requestId == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
870 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
871 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
872 logger.debug("{}: {}: orphan appc response={}",
873 $clName, $params.getPolicyName() + "." + drools.getRule().getName(), $id);
883 * This rule responds to APPC Response Events using the new LCM interface provided by appc
886 rule "APPC.LCM.RESPONSE"
888 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
889 $event : VirtualControlLoopEvent( closedLoopControlName == $clName,
890 closedLoopEventStatus == ControlLoopEventStatus.ONSET )
891 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
892 requestID == $event.getRequestId() )
893 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
894 onset.getRequestId() == $event.getRequestId() )
895 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
896 requestID == $event.getRequestId().toString(), timerType == "Operation", !expired )
897 $lock : TargetLock (requestID == $event.getRequestId())
898 $response : LcmResponseWrapper( getBody().getCommonHeader().getRequestId() == $event.getRequestId() )
901 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
902 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
903 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
904 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
905 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
908 // Get the result of the operation
910 PolicyResult policyResult = $operation.onResponse($response);
911 if (policyResult != null) {
912 logger.debug("{}: {}: operation finished - result={}",
913 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
917 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
919 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
920 notification.setFrom("policy");
921 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
922 notification.setPolicyScope($params.getPolicyScope());
923 notification.setPolicyVersion($params.getPolicyVersion());
924 notification.setMessage($operation.getOperationHistory());
925 notification.setHistory($operation.getHistory());
926 if (policyResult.equals(PolicyResult.SUCCESS)) {
927 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
929 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
931 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
933 // Ensure the operation is complete
935 if ($operation.isOperationComplete() == true) {
937 // It is complete, remove it from memory
941 // We must also retract the timer object
942 // NOTE: We could write a Rule to do this
946 // Complete the operation
948 modify($manager) {finishOperation($operation)};
951 // Just doing this will kick off the LOCKED rule again
953 modify($operation) {};
957 // Its not finished yet (i.e. expecting more Response objects)
959 // Or possibly it is a leftover response that we timed the request out previously
963 // We are going to retract these objects from memory
970 * Clean Up any lingering LCM reponses
973 rule "APPC.LCM.RESPONSE.CLEANUP"
975 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
976 $response : LcmResponseWrapper($id : getBody().getCommonHeader().getRequestId )
977 not ( VirtualControlLoopEvent( requestId == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
980 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
981 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
982 logger.debug("{}: {}: orphan appc response={}",
983 $clName, $params.getPolicyName() + "." + drools.getRule().getName(), $id);
992 * This rule responds to SO Response Events
997 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
998 $event : VirtualControlLoopEvent( closedLoopControlName == $clName,
999 closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1000 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1001 requestID == $event.getRequestId() )
1002 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
1003 onset.getRequestId() == $event.getRequestId() )
1004 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1005 requestID == $event.getRequestId().toString(), timerType == "Operation", !expired )
1006 $lock : TargetLock (requestID == $event.getRequestId())
1007 $response : SOResponseWrapper(requestID.toString() == $event.getRequestId().toString() )
1010 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1011 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
1012 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
1013 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
1014 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
1016 // Get the result of the operation
1018 PolicyResult policyResult = $operation.onResponse($response);
1019 if (policyResult != null) {
1020 logger.debug("{}: {}: operation finished - result={}",
1021 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
1025 // This Operation has completed, construct a notification showing our results
1027 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1028 notification.setFrom("policy");
1029 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
1030 notification.setPolicyScope($params.getPolicyScope());
1031 notification.setPolicyVersion($params.getPolicyVersion());
1032 notification.setMessage($operation.getOperationHistory());
1033 notification.setHistory($operation.getHistory());
1034 if (policyResult.equals(PolicyResult.SUCCESS)) {
1035 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
1037 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
1040 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1042 // Ensure the operation is complete
1044 if ($operation.isOperationComplete() == true) {
1046 // It is complete, remove it from memory
1048 retract($operation);
1050 // We must also retract the timer object
1051 // NOTE: We could write a Rule to do this
1055 // Complete the operation
1057 modify($manager) {finishOperation($operation)};
1060 // Just doing this will kick off the LOCKED rule again
1062 modify($operation) {};
1066 // Its not finished yet (i.e. expecting more Response objects)
1068 // Or possibly it is a leftover response that we timed the request out previously
1072 // We are going to retract these objects from memory
1080 * This rule responds to VFC Response Events
1085 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
1086 $event : VirtualControlLoopEvent( closedLoopControlName == $clName,
1087 closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1088 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1089 requestID == $event.getRequestId() )
1090 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
1091 onset.getRequestId() == $event.getRequestId() )
1092 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1093 requestID == $event.getRequestId().toString(), timerType == "Operation", !expired )
1094 $lock : TargetLock (requestID == $event.getRequestId())
1095 $response : VFCResponse( requestId.toString() == $event.getRequestId().toString() )
1097 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1098 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
1099 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
1100 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
1101 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
1103 // Get the result of the operation
1105 PolicyResult policyResult = $operation.onResponse($response);
1106 if (policyResult != null) {
1108 // This Operation has completed, construct a notification showing our results
1110 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1111 notification.setFrom("policy");
1112 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
1113 notification.setPolicyScope($params.getPolicyScope());
1114 notification.setPolicyVersion($params.getPolicyVersion());
1115 notification.setMessage($operation.getOperationHistory());
1116 notification.setHistory($operation.getHistory());
1118 // Ensure the operation is complete
1120 if ($operation.isOperationComplete() == true) {
1122 // It is complete, remove it from memory
1124 retract($operation);
1126 // We must also retract the timer object
1127 // NOTE: We could write a Rule to do this
1131 // Complete the operation
1133 modify($manager) {finishOperation($operation)};
1136 // Just doing this will kick off the LOCKED rule again
1138 modify($operation) {};
1142 // Its not finished yet (i.e. expecting more Response objects)
1144 // Or possibly it is a leftover response that we timed the request out previously
1148 // We are going to retract these objects from memory
1156 * This rule responds to SDNC Response Events
1159 rule "SDNC.RESPONSE"
1161 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
1162 $event : VirtualControlLoopEvent( closedLoopControlName == $clName, closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1163 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
1164 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() )
1165 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1166 requestID == $event.getRequestId().toString(), timerType == "Operation", !expired )
1167 $lock : TargetLock (requestID == $event.getRequestId())
1168 $response : SdncResponse( requestId.toString() == $event.getRequestId().toString() )
1170 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1171 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
1172 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
1173 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
1174 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
1176 // Get the result of the operation
1178 PolicyResult policyResult = $operation.onResponse($response);
1179 if (policyResult != null) {
1181 // This Operation has completed, construct a notification showing our results
1183 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1184 notification.setFrom("policy");
1185 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
1186 notification.setPolicyScope($params.getPolicyScope());
1187 notification.setPolicyVersion($params.getPolicyVersion());
1188 notification.setMessage($operation.getOperationHistory());
1189 notification.setHistory($operation.getHistory());
1191 // Ensure the operation is complete
1193 if ($operation.isOperationComplete()) {
1195 // It is complete, remove it from memory
1197 retract($operation);
1199 // We must also retract the timer object
1200 // NOTE: We could write a Rule to do this
1204 // Complete the operation
1206 modify($manager) {finishOperation($operation)};
1209 // Just doing this will kick off the LOCKED rule again
1211 modify($operation) {};
1215 // Its not finished yet (i.e. expecting more Response objects)
1217 // Or possibly it is a leftover response that we timed the request out previously
1221 // We are going to retract these objects from memory
1229 * This manages a single timer.
1230 * Due to a bug in the drools code, the drools timer needed to be split from most of the objects in the when clause
1234 timer (expr: $timeout)
1236 $timer : ControlLoopTimer($timeout : delay, !expired)
1238 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1239 logger.info("This is TIMER.FIRED");
1240 modify($timer){setExpired(true)};
1245 * This is the timer that manages the timeout for an individual operation.
1248 rule "EVENT.MANAGER.OPERATION.TIMEOUT"
1250 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
1251 $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
1252 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1253 requestID == $event.getRequestId() )
1254 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
1255 onset.getRequestId() == $event.getRequestId() )
1256 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1257 requestID == $event.getRequestId().toString(), timerType == "Operation", expired )
1258 $lock : TargetLock (requestID == $event.getRequestId())
1261 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1262 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
1263 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={}",
1264 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
1265 $event, $manager, $operation, $lock, $operation, $opTimer);
1268 // Tell it its timed out
1270 $operation.setOperationHasTimedOut();
1272 // Create a notification for it ("DB Write - end operation")
1274 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1275 notification.setFrom("policy");
1276 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
1277 notification.setPolicyScope($params.getPolicyScope());
1278 notification.setPolicyVersion($params.getPolicyVersion());
1279 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
1280 notification.setMessage($operation.getOperationHistory());
1281 notification.setHistory($operation.getHistory());
1283 // Let interested parties know
1285 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1287 // Get rid of the timer
1291 // Ensure the operation is complete
1293 if ($operation.isOperationComplete() == true) {
1295 // It is complete, remove it from memory
1297 retract($operation);
1299 // Complete the operation
1301 modify($manager) {finishOperation($operation)};
1304 // Just doing this will kick off the LOCKED rule again
1306 modify($operation) {};
1312 * This is the timer that manages the overall control loop timeout.
1315 rule "EVENT.MANAGER.TIMEOUT"
1317 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
1318 $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
1319 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1320 requestID == $event.getRequestId() )
1321 $clTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1322 requestID == $event.getRequestId().toString(), timerType == "ClosedLoop", expired )
1325 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1326 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
1328 logger.debug("{}: {}: event={}",
1329 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
1332 // Tell the Event Manager it has timed out
1334 VirtualControlLoopNotification notification = $manager.setControlLoopTimedOut();
1335 if (notification != null) {
1336 notification.setFrom("policy");
1337 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
1338 notification.setPolicyScope($params.getPolicyScope());
1339 notification.setPolicyVersion($params.getPolicyVersion());
1341 // Let interested parties know
1343 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1346 // Retract the event
1353 * This rule cleans up the manager and other objects after an event has
1357 rule "EVENT.MANAGER.CLEANUP"
1359 $manager : ControlLoopEventManager( $clName : getClosedLoopControlName(), $requestId : getRequestID() )
1360 $operations : LinkedList()
1361 from collect( ControlLoopOperationManager( onset.closedLoopControlName == $clName,
1362 onset.getRequestId() == $requestId ) )
1363 $timers : LinkedList()
1364 from collect( ControlLoopTimer( closedLoopControlName == $clName,
1365 requestID == $requestId.toString() ) )
1366 $locks : LinkedList()
1367 from collect( TargetLock (requestID == $requestId) )
1368 not( VirtualControlLoopEvent( closedLoopControlName == $clName, requestId == $requestId ) )
1371 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1372 logger.info("{}: {}", $clName, drools.getRule().getName());
1374 logger.debug("{}: {}: manager={} timers={} operations={}",
1375 $clName, drools.getRule().getName(),
1376 $manager, $timers.size(), $operations.size());
1379 // Retract EVERYTHING
1383 for(Object manager: $operations) {
1384 retract((ControlLoopOperationManager) manager);
1386 for(Object timer: $timers) {
1387 retract((ControlLoopTimer) timer);
1389 for(Object lock: $locks) {
1390 TargetLock tgt = (TargetLock) lock;
1392 // Ensure we release the lock
1394 PolicyGuard.unlockTarget(tgt);
1401 * This rule will clean up any rogue onsets where there is no
1402 * ControlLoopParams object corresponding to the onset event.
1405 rule "EVENT.CLEANUP"
1407 $event : VirtualControlLoopEvent( $clName: closedLoopControlName )
1408 not ( ControlLoopParams( getClosedLoopControlName() == $clName) )
1411 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1412 logger.info("{}: {}", $clName, drools.getRule().getName());
1413 logger.debug("{}: {}: orphan onset event={}",
1414 $clName, drools.getRule().getName(), $event);
1421 * This rule responds to SDNR Response Events.
1424 rule "SDNR.RESPONSE"
1426 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
1427 $event : VirtualControlLoopEvent( closedLoopControlName == $clName,
1428 closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1429 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1430 requestID == $event.getRequestId() )
1431 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
1432 onset.getRequestId() == $event.getRequestId() )
1433 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1434 requestID == $event.getRequestId().toString(), timerType == "Operation", !expired )
1435 $lock : TargetLock (requestID == $event.getRequestId())
1436 $response : PciResponseWrapper( getBody().getCommonHeader().getRequestId() == $event.getRequestId() )
1439 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1440 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
1441 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
1442 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
1443 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
1446 // Get the result of the operation
1448 PolicyResult policyResult = $operation.onResponse($response);
1449 if (policyResult != null) {
1450 logger.debug("{}: {}: operation finished - result={}",
1451 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
1455 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
1457 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1458 notification.setFrom("policy");
1459 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
1460 notification.setPolicyScope($params.getPolicyScope());
1461 notification.setPolicyVersion($params.getPolicyVersion());
1462 notification.setMessage($operation.getOperationHistory());
1463 notification.setHistory($operation.getHistory());
1464 if (policyResult.equals(PolicyResult.SUCCESS)) {
1465 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
1467 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
1469 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1471 // Ensure the operation is complete
1473 if ($operation.isOperationComplete() == true) {
1475 // It is complete, remove it from memory
1477 retract($operation);
1479 // We must also retract the timer object
1480 // NOTE: We could write a Rule to do this
1484 // Complete the operation
1486 modify($manager) {finishOperation($operation)};
1489 // Just doing this will kick off the LOCKED rule again
1491 modify($operation) {};
1495 // Its not finished yet (i.e. expecting more Response objects)
1497 // Or possibly it is a leftover response that we timed the request out previously
1501 // We are going to retract these objects from memory
1508 * Clean Up any lingering SDNR reponses.
1511 rule "SDNR.RESPONSE.CLEANUP"
1513 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
1514 $response : PciResponseWrapper($id : getBody().getCommonHeader().getRequestId )
1515 not ( VirtualControlLoopEvent( requestId == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
1518 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1519 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
1520 logger.debug("{}: {}: orphan sdnr response={}",
1521 $clName, $params.getPolicyName() + "." + drools.getRule().getName(), $id);