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 to insert the ControlLoopParams object into working memory from the BRMSGW.
105 $params : ControlLoopParams()
110 // Note: globals have bad behavior when persistence is used,
111 // hence explicitly getting the logger vs using a global
113 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
114 logger.info("{}: {} : YAML=[{}]", $params.getClosedLoopControlName(), $params.getPolicyName() + "." + drools.getRule().getName(), $params.getControlLoopYaml());
119 * This rule responds to DCAE Events where there is no manager yet. Either it is
120 * the first ONSET, or a subsequent badly formed Event (i.e. Syntax error, or is-closed-loop-disabled)
125 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
126 $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
127 not ( ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID() ) )
130 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
131 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
136 // Check the event, because we need it to not be null when
137 // we create the ControlLoopEventManager. The ControlLoopEventManager
138 // will do extra syntax checking as well check if the closed loop is disabled.
140 if ($event.getRequestID() == null) {
141 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
142 notification.setNotification(ControlLoopNotificationType.REJECTED);
143 notification.setFrom("policy");
144 notification.setMessage("Missing requestID");
145 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
146 notification.setPolicyScope($params.getPolicyScope());
147 notification.setPolicyVersion($params.getPolicyVersion());
150 // Let interested parties know
152 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
155 // Retract it from memory
158 } else if ($event.getClosedLoopEventStatus() != ControlLoopEventStatus.ONSET) {
159 throw new ControlLoopException($event.getClosedLoopEventStatus() + " received with no prior onset");
162 // Create an EventManager
164 ControlLoopEventManager manager = new ControlLoopEventManager($clName, $event.getRequestID());
166 // Determine if EventManager can actively process the event (i.e. syntax, is_closed_loop_disabled checks etc.)
168 VirtualControlLoopNotification notification = manager.activate($params.getControlLoopYaml(), $event);
169 notification.setFrom("pdp-0001-controller=controlloop"); // Engine.getInstanceName()
170 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
171 notification.setPolicyScope($params.getPolicyScope());
172 notification.setPolicyVersion($params.getPolicyVersion());
174 // Are we actively pursuing this event?
176 if (notification.getNotification() == ControlLoopNotificationType.ACTIVE) {
178 // Insert Event Manager into memory, this will now kick off processing.
182 // Let interested parties know
184 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
186 // Setup the Overall Control Loop timer
188 ControlLoopTimer clTimer = new ControlLoopTimer();
189 clTimer.setClosedLoopControlName($event.getClosedLoopControlName());
190 clTimer.setRequestID($event.getRequestID().toString());
191 clTimer.setDelay(manager.getControlLoopTimeout(1500) + "s");
198 // Let interested parties know
200 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
202 // Retract it from memory
208 // Now that the manager is inserted into Drools working memory, we'll wait for
209 // another rule to fire in order to continue processing. This way we can also
210 // then screen for additional ONSET and ABATED events for this RequestID.
213 } catch (Exception e) {
214 logger.warn("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName(), e);
216 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
217 notification.setNotification(ControlLoopNotificationType.REJECTED);
218 notification.setMessage("Exception occurred: " + e.getMessage());
219 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
220 notification.setPolicyScope($params.getPolicyScope());
221 notification.setPolicyVersion($params.getPolicyVersion());
225 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
235 * This rule happens when we got a valid ONSET, closed loop is enabled and an Event Manager
236 * is now created. We can start processing the yaml specification via the Event Manager.
241 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
242 $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
243 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID() )
244 $clTimer : ControlLoopTimer ( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID().toString() )
247 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
248 logger.info("{}: {}: event={} manager={} clTimer={}",
249 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
250 $event, $manager, $clTimer);
254 // Check which event this is.
256 ControlLoopEventManager.NEW_EVENT_STATUS eventStatus = $manager.onNewEvent($event);
258 // Check what kind of event this is
260 if (eventStatus == NEW_EVENT_STATUS.SUBSEQUENT_ONSET) {
262 // We don't care about subsequent onsets
264 logger.info("{}: {}: subsequent onset",
265 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
269 if (eventStatus == NEW_EVENT_STATUS.SYNTAX_ERROR) {
271 // Ignore any bad syntax events
273 logger.warn("{}: {}: syntax error",
274 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
279 // We only want the initial ONSET event in memory,
280 // all the other events need to be retracted to support
281 // cleanup and avoid the other rules being fired for this event.
283 if (eventStatus != NEW_EVENT_STATUS.FIRST_ONSET) {
284 logger.warn("{}: {}: no first onset",
285 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
289 logger.debug("{}: {}: target={}", $clName,
290 $params.getPolicyName() + "." + drools.getRule().getName(), $event.getTarget());
292 // Now start seeing if we need to process this event
296 // Check if this is a Final Event
298 VirtualControlLoopNotification notification = $manager.isControlLoopFinal();
301 if (notification != null) {
303 // Its final, but are we waiting for abatement?
305 if ($manager.getNumAbatements() > 0) {
306 logger.info("{}: {}: abatement received for {}. Closing the control loop",
307 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
308 $event.getRequestID());
309 notification.setFrom("policy");
310 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
311 notification.setPolicyScope($params.getPolicyScope());
312 notification.setPolicyVersion($params.getPolicyVersion());
314 // In this case, we are done
316 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
320 TargetLock lock = $manager.unlockCurrentOperation();
322 logger.debug("{}: {}: retracting lock=", $clName,
323 $params.getPolicyName() + "." + drools.getRule().getName(), lock);
327 // Retract everything from memory
329 logger.info("{}: {}: retracting onset, manager, and timer",
330 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
332 retract($manager.getOnsetEvent());
336 // TODO - what if we get subsequent Events for this RequestID?
337 // By default, it will all start over again. May be confusing for Ruby.
338 // Or, we could track this and then subsequently ignore the events
342 // Check whether we need to wait for abatement
344 if ($manager.getProcessor().getControlLoop().getAbatement() == true && notification.getNotification() == ControlLoopNotificationType.FINAL_SUCCESS) {
345 logger.info("{}: {}: waiting for abatement ..",
346 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
348 logger.info("{}: {}: no abatement expect for {}. Closing the control loop",
349 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
350 $event.getRequestID());
352 notification.setFrom("policy");
353 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
354 notification.setPolicyScope($params.getPolicyScope());
355 notification.setPolicyVersion($params.getPolicyVersion());
358 // In this case, we are done
360 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
364 TargetLock lock = $manager.unlockCurrentOperation();
366 logger.debug("{}: {}: retracting lock=", $clName,
367 $params.getPolicyName() + "." + drools.getRule().getName(), lock);
371 // Retract everything from memory
373 logger.info("{}: {}: retracting onset, manager, and timer",
374 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
376 retract($manager.getOnsetEvent());
383 // NOT final, so let's ask for the next operation
385 ControlLoopOperationManager operation = $manager.processControlLoop();
386 if (operation != null) {
388 // Let's ask for a lock right away
390 LockResult<GuardResult, TargetLock> result = $manager.lockCurrentOperation();
391 logger.info("{}: {}: guard lock acquired={}",
392 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
394 if (result.getA().equals(GuardResult.LOCK_ACQUIRED)) {
396 // insert the operation into memory
401 // insert operation timeout object
403 OperationTimer opTimer = new OperationTimer();
404 opTimer.setClosedLoopControlName($event.getClosedLoopControlName());
405 opTimer.setRequestID($event.getRequestID().toString());
406 opTimer.setDelay(operation.getOperationTimeout().toString() + "s");
410 // Insert lock into memory
412 insert(result.getB());
415 logger.debug("The target resource {} is already processing",
416 $event.getAAI().get($event.getTarget()));
417 notification = new VirtualControlLoopNotification($event);
418 notification.setNotification(ControlLoopNotificationType.REJECTED);
419 notification.setMessage("The target " + $event.getAAI().get($event.getTarget()) + " is already locked");
420 notification.setFrom("policy");
421 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
422 notification.setPolicyScope($params.getPolicyScope());
423 notification.setPolicyVersion($params.getPolicyVersion());
425 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
431 logger.info("{}: {}: starting operation={}",
432 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
436 // Probably waiting for abatement
438 logger.info("{}: {}: no operation, probably waiting for abatement",
439 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
442 } catch (Exception e) {
443 logger.warn("{}: {}: unexpected",
445 $params.getPolicyName() + "." + drools.getRule().getName(), e);
447 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
448 notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
449 notification.setMessage(e.getMessage());
450 notification.setFrom("policy");
451 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
452 notification.setPolicyScope($params.getPolicyScope());
453 notification.setPolicyVersion($params.getPolicyVersion());
455 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
466 * Guard Permitted, let's send request to the actor.
469 rule "EVENT.MANAGER.OPERATION.LOCKED.GUARD_PERMITTED"
471 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
472 $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
473 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID() )
474 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestID() == $event.getRequestID(), "Permit".equalsIgnoreCase(getGuardApprovalStatus()) )
475 $lock : TargetLock (requestID == $event.getRequestID())
476 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID().toString() )
479 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
480 logger.info("{}: {}: event={} manager={} operation={} lock={}",
481 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
482 $event, $manager, $operation, $lock);
484 Object request = null;
485 boolean caughtException = false;
487 request = $operation.startOperation($event);
489 catch (ControlLoopException e) {
490 String msg = e.getMessage();
491 logger.warn("{}: {}: operation={}: AAI failure: {}",
492 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
494 $operation.setOperationHasException(msg);
497 caughtException = true;
500 // Having the modify statement in the catch clause doesn't work for whatever reason
501 if (caughtException) {
502 modify($manager) {finishOperation($operation)};
504 else if (request != null) {
505 logger.debug("{}: {}: starting operation ..",
506 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
508 // Tell interested parties we are performing this Operation
510 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
511 notification.setNotification(ControlLoopNotificationType.OPERATION);
512 notification.setMessage($operation.getOperationMessage());
513 notification.setHistory($operation.getHistory());
514 notification.setFrom("policy");
515 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
516 notification.setPolicyScope($params.getPolicyScope());
517 notification.setPolicyVersion($params.getPolicyVersion());
519 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
521 switch ($operation.policy.getActor()){
525 if (request instanceof Request) {
526 PolicyEngine.manager.deliver("APPC-CL", request);
528 else if (request instanceof LCMRequestWrapper) {
529 PolicyEngine.manager.deliver("APPC-LCM-READ", request);
533 // at this point the AAI named query request should have already been made, the response recieved and used
534 // in the construction of the SO Request which is stored in operationRequest
536 if(request instanceof SORequest) {
537 // Call SO. The response will be inserted into memory once it's received
538 SOActorServiceProvider.sendRequest($event.getRequestID().toString(), drools.getWorkingMemory(), request);
542 if (request instanceof VFCRequest) {
544 Thread t = new Thread(new VFCManager(drools.getWorkingMemory(), (VFCRequest)request));
551 // What happens if its null?
553 logger.warn("{}: {}: unexpected null operation request",
555 $params.getPolicyName() + "." + drools.getRule().getName());
556 if ("SO".equals($operation.policy.getActor())) {
559 modify($manager) {finishOperation($operation)};
561 else if ("vfc".equalsIgnoreCase($operation.policy.getActor())) {
564 modify($manager) {finishOperation($operation)};
573 * We were able to acquire a lock so now let's ask Xacml Guard whether
574 * we are allowed to proceed with the request to the actor.
577 rule "EVENT.MANAGER.OPERATION.LOCKED.GUARD_NOT_YET_QUERIED"
579 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
580 $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
581 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID() )
582 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestID() == $event.getRequestID(), getGuardApprovalStatus() == "NONE" )
583 $lock : TargetLock (requestID == $event.getRequestID())
586 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
587 logger.info("{}: {}: event={} manager={} operation={} lock={}",
588 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
589 $event, $manager, $operation, $lock);
592 // Sending notification that we are about to query Guard ("DB write - start operation")
594 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
595 notification.setNotification(ControlLoopNotificationType.OPERATION);
596 notification.setMessage("Sending guard query for " + $operation.policy.getActor() + " " + $operation.policy.getRecipe());
597 notification.setHistory($operation.getHistory());
598 notification.setFrom("policy");
599 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
600 notification.setPolicyScope($params.getPolicyScope());
601 notification.setPolicyVersion($params.getPolicyVersion());
603 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
606 // Now send Guard Request to XACML Guard. In order to bypass the call to Guard,
607 // just change guardEnabled to false.
609 // In order to use REST XACML, provide a URL instead of "" as a second argument
610 // to the CallGuardTask() and set the first argument to null
611 // (instead of XacmlPdpEngine).
614 // NOTE: The environment properties uses "guard.disabled" but the boolean is guardEnabled
615 boolean guardEnabled = "false".equalsIgnoreCase(PolicyEngine.manager.getEnvironmentProperty("guard.disabled"));
619 Thread t = new Thread(new org.onap.policy.guard.CallGuardTask(
620 drools.getWorkingMemory(),
621 $event.getClosedLoopControlName(),
622 $operation.policy.getActor().toString(),
623 $operation.policy.getRecipe(),
624 $operation.getTargetEntity(),
625 $event.getRequestID().toString()
630 insert(new PolicyGuardResponse("Permit", $event.getRequestID(), $operation.policy.getRecipe()));
636 // This rule will be triggered when a thread talking to the XACML Guard inserts a
637 // guardResponse object into the working memory
639 rule "GUARD.RESPONSE"
641 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
642 $event : VirtualControlLoopEvent( closedLoopControlName == $clName, closedLoopEventStatus == ControlLoopEventStatus.ONSET )
643 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID() )
644 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestID() == $event.getRequestID() )
645 $lock : TargetLock (requestID == $event.getRequestID())
646 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID().toString() )
647 $guardResponse : PolicyGuardResponse(requestID == $event.getRequestID(), $operation.policy.recipe == operation)
650 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
651 logger.info("{}: {}: event={} manager={} operation={} lock={} opTimer={} guardResponse={}",
652 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
653 $event, $manager, $operation, $lock, $opTimer, $guardResponse);
656 //we will permit the operation if there was no Guard for it
657 if("Indeterminate".equalsIgnoreCase($guardResponse.getResult())){
658 $guardResponse.setResult("Permit");
662 // This notification has Guard result in "message". ("DB write - end operation in case of Guard Deny")
664 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
665 notification.setNotification(ControlLoopNotificationType.OPERATION);
666 notification.setMessage("Guard result for " + $operation.policy.getActor() + " " + $operation.policy.getRecipe() + " is " + $guardResponse.getResult());
667 notification.setHistory($operation.getHistory());
668 notification.setFrom("policy");
669 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
670 notification.setPolicyScope($params.getPolicyScope());
671 notification.setPolicyVersion($params.getPolicyVersion());
673 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
675 if("Permit".equalsIgnoreCase($guardResponse.getResult())){
677 modify($operation){setGuardApprovalStatus($guardResponse.getResult())};
680 //This is the Deny case
681 $operation.startOperation($event);
682 $operation.setOperationHasGuardDeny();
685 modify($manager) {finishOperation($operation)};
688 retract($guardResponse);
694 * This rule responds to APPC Response Events
696 * I would have like to be consistent and write the Response like this:
697 * $response : Response( CommonHeader.RequestID == $onset.getRequestID() )
699 * However, no compile error was given. But a runtime error was given. I think
700 * because drools is confused between the classname CommonHeader vs the property CommonHeader.
705 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
706 $event : VirtualControlLoopEvent( closedLoopControlName == $clName, closedLoopEventStatus == ControlLoopEventStatus.ONSET )
707 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID() )
708 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestID() == $event.getRequestID() )
709 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID().toString() )
710 $lock : TargetLock (requestID == $event.getRequestID())
711 $response : Response( getCommonHeader().RequestID == $event.getRequestID() )
714 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
715 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
716 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
717 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
718 $event, $manager, $operation, $lock, $opTimer, $response);
720 // Get the result of the operation
722 PolicyResult policyResult = $operation.onResponse($response);
723 if (policyResult != null) {
724 logger.debug("{}: {}: operation finished - result={}",
725 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
728 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
730 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
731 notification.setFrom("policy");
732 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
733 notification.setPolicyScope($params.getPolicyScope());
734 notification.setPolicyVersion($params.getPolicyVersion());
735 notification.setMessage($operation.getOperationHistory());
736 notification.setHistory($operation.getHistory());
737 if (policyResult.equals(PolicyResult.SUCCESS)) {
738 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
740 // Let interested parties know
742 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
744 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
746 // Let interested parties know
748 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
751 // Ensure the operation is complete
753 if ($operation.isOperationComplete() == true) {
755 // It is complete, remove it from memory
759 // We must also retract the timer object
760 // NOTE: We could write a Rule to do this
764 // Complete the operation
766 modify($manager) {finishOperation($operation)};
769 // Just doing this will kick off the LOCKED rule again
771 modify($operation) {};
775 // Its not finished yet (i.e. expecting more Response objects)
777 // Or possibly it is a leftover response that we timed the request out previously
781 // We are going to retract these objects from memory
788 * The problem with Responses is that they don't have a controlLoopControlName
789 * field in them, so the only way to attach them is via RequestID. If we have multiple
790 * control loop .drl's loaded in the same container, we need to be sure the cleanup
791 * rules don't remove Responses for other control loops.
794 rule "APPC.RESPONSE.CLEANUP"
796 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
797 $response : Response($id : getCommonHeader().RequestID )
798 not ( VirtualControlLoopEvent( requestID == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
801 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
802 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
803 logger.debug("{}: {}: orphan appc response={}",
804 $clName, $params.getPolicyName() + "." + drools.getRule().getName(), $id);
814 * This rule responds to APPC Response Events using the new LCM interface provided by appc
817 rule "APPC.LCM.RESPONSE"
819 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
820 $event : VirtualControlLoopEvent( closedLoopControlName == $clName, closedLoopEventStatus == ControlLoopEventStatus.ONSET )
821 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID() )
822 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestID() == $event.getRequestID() )
823 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID().toString() )
824 $lock : TargetLock (requestID == $event.getRequestID())
825 $response : LCMResponseWrapper( getBody().getCommonHeader().getRequestId() == $event.getRequestID() )
828 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
829 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
830 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
831 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
832 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
835 // Get the result of the operation
837 PolicyResult policyResult = $operation.onResponse($response);
838 if (policyResult != null) {
839 logger.debug("{}: {}: operation finished - result={}",
840 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
844 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
846 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
847 notification.setFrom("policy");
848 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
849 notification.setPolicyScope($params.getPolicyScope());
850 notification.setPolicyVersion($params.getPolicyVersion());
851 notification.setMessage($operation.getOperationHistory());
852 notification.setHistory($operation.getHistory());
853 if (policyResult.equals(PolicyResult.SUCCESS)) {
854 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
856 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
858 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
860 // Ensure the operation is complete
862 if ($operation.isOperationComplete() == true) {
864 // It is complete, remove it from memory
868 // We must also retract the timer object
869 // NOTE: We could write a Rule to do this
873 // Complete the operation
875 modify($manager) {finishOperation($operation)};
878 // Just doing this will kick off the LOCKED rule again
880 modify($operation) {};
884 // Its not finished yet (i.e. expecting more Response objects)
886 // Or possibly it is a leftover response that we timed the request out previously
890 // We are going to retract these objects from memory
897 * Clean Up any lingering LCM reponses
900 rule "APPC.LCM.RESPONSE.CLEANUP"
902 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
903 $response : LCMResponseWrapper($id : getBody().getCommonHeader().getRequestId )
904 not ( VirtualControlLoopEvent( requestID == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
907 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
908 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
909 logger.debug("{}: {}: orphan appc response={}",
910 $clName, $params.getPolicyName() + "." + drools.getRule().getName(), $id);
919 * This rule responds to SO Response Events
924 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
925 $event : VirtualControlLoopEvent( closedLoopControlName == $clName, closedLoopEventStatus == ControlLoopEventStatus.ONSET )
926 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName() )
927 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestID() == $event.getRequestID() )
928 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID().toString() )
929 $lock : TargetLock (requestID == $event.getRequestID())
930 $response : SOResponseWrapper(requestID.toString() == $event.getRequestID().toString() )
933 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
934 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
935 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
936 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
937 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
939 // Get the result of the operation
941 PolicyResult policyResult = $operation.onResponse($response);
942 if (policyResult != null) {
943 logger.debug("{}: {}: operation finished - result={}",
944 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
948 // This Operation has completed, construct a notification showing our results
950 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
951 notification.setFrom("policy");
952 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
953 notification.setPolicyScope($params.getPolicyScope());
954 notification.setPolicyVersion($params.getPolicyVersion());
955 notification.setMessage($operation.getOperationHistory());
956 notification.setHistory($operation.getHistory());
957 if (policyResult.equals(PolicyResult.SUCCESS)) {
958 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
960 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
963 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
965 // Ensure the operation is complete
967 if ($operation.isOperationComplete() == true) {
969 // It is complete, remove it from memory
973 // We must also retract the timer object
974 // NOTE: We could write a Rule to do this
978 // Complete the operation
980 modify($manager) {finishOperation($operation)};
983 // Just doing this will kick off the LOCKED rule again
985 modify($operation) {};
989 // Its not finished yet (i.e. expecting more Response objects)
991 // Or possibly it is a leftover response that we timed the request out previously
995 // We are going to retract these objects from memory
1003 * This rule responds to VFC Response Events
1008 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
1009 $event : VirtualControlLoopEvent( closedLoopControlName == $clName, closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1010 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName() )
1011 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestID() == $event.getRequestID() )
1012 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID().toString() )
1013 $lock : TargetLock (requestID == $event.getRequestID())
1014 $response : VFCResponse( requestId.toString() == $event.getRequestID().toString() )
1016 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1017 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
1018 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
1019 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
1020 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
1022 // Get the result of the operation
1024 PolicyResult policyResult = $operation.onResponse($response);
1025 if (policyResult != null) {
1027 // This Operation has completed, construct a notification showing our results
1029 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1030 notification.setFrom("policy");
1031 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
1032 notification.setPolicyScope($params.getPolicyScope());
1033 notification.setPolicyVersion($params.getPolicyVersion());
1034 notification.setMessage($operation.getOperationHistory());
1035 notification.setHistory($operation.getHistory());
1037 // Ensure the operation is complete
1039 if ($operation.isOperationComplete() == true) {
1041 // It is complete, remove it from memory
1043 retract($operation);
1045 // We must also retract the timer object
1046 // NOTE: We could write a Rule to do this
1050 // Complete the operation
1052 modify($manager) {finishOperation($operation)};
1055 // Just doing this will kick off the LOCKED rule again
1057 modify($operation) {};
1061 // Its not finished yet (i.e. expecting more Response objects)
1063 // Or possibly it is a leftover response that we timed the request out previously
1067 // We are going to retract these objects from memory
1075 * This is the timer that manages the timeout for an individual operation.
1078 rule "EVENT.MANAGER.OPERATION.TIMEOUT"
1081 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
1082 $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
1083 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID() )
1084 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestID() == $event.getRequestID() )
1085 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID().toString(), $to : getDelay() )
1086 $lock : TargetLock (requestID == $event.getRequestID())
1089 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1090 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
1091 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={}",
1092 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
1093 $event, $manager, $operation, $lock, $operation, $opTimer);
1096 // Tell it its timed out
1098 $operation.setOperationHasTimedOut();
1100 // Create a notification for it ("DB Write - end operation")
1102 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1103 notification.setFrom("policy");
1104 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
1105 notification.setPolicyScope($params.getPolicyScope());
1106 notification.setPolicyVersion($params.getPolicyVersion());
1107 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
1108 notification.setMessage($operation.getOperationHistory());
1109 notification.setHistory($operation.getHistory());
1111 // Let interested parties know
1113 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1115 // Get rid of the timer
1119 // Ensure the operation is complete
1121 if ($operation.isOperationComplete() == true) {
1123 // It is complete, remove it from memory
1125 retract($operation);
1127 // Complete the operation
1129 modify($manager) {finishOperation($operation)};
1132 // Just doing this will kick off the LOCKED rule again
1134 modify($operation) {};
1140 * This is the timer that manages the overall control loop timeout.
1143 rule "EVENT.MANAGER.TIMEOUT"
1146 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
1147 $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
1148 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID() )
1149 $clTimer : ControlLoopTimer ( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID().toString(), $to : getDelay() )
1150 $operations : LinkedList()
1151 from collect( ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestID() == $event.getRequestID() ) )
1152 $opTimers : LinkedList()
1153 from collect( OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID().toString() ) )
1154 $locks : LinkedList()
1155 from collect( TargetLock (requestID == $event.getRequestID()) )
1158 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1159 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
1161 if ($operations == null) {
1162 logger.debug("{}: {}: event={} manager={} clTimer={} operations=0",
1163 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
1164 $event, $manager, $clTimer);
1166 logger.debug("{}: {}: event={} manager={} clTimer={} operations={}",
1167 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
1168 $event, $manager, $clTimer, $operations.size());
1171 // Tell the Event Manager it has timed out
1173 VirtualControlLoopNotification notification = $manager.setControlLoopTimedOut();
1174 if (notification != null) {
1175 notification.setFrom("policy");
1176 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
1177 notification.setPolicyScope($params.getPolicyScope());
1178 notification.setPolicyVersion($params.getPolicyVersion());
1180 // Let interested parties know
1182 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1185 // Retract EVERYTHING
1190 if ($operations != null && $operations.size() > 0) {
1191 Iterator<ControlLoopOperationManager> iter = $operations.iterator();
1192 while (iter.hasNext()) {
1193 ControlLoopOperationManager manager = iter.next();
1197 if ($opTimers != null && $opTimers.size() > 0) {
1198 Iterator<OperationTimer> iter = $opTimers.iterator();
1199 while (iter.hasNext()) {
1200 OperationTimer opTimer = iter.next();
1204 if ($locks != null && $locks.size() > 0) {
1205 Iterator<TargetLock> iter = $locks.iterator();
1206 while (iter.hasNext()) {
1207 TargetLock lock = iter.next();
1209 // Ensure we release the lock
1211 PolicyGuard.unlockTarget(lock);
1222 * This rule will clean up any rogue onsets where there is no
1223 * ControlLoopParams object corresponding to the onset event.
1226 rule "EVENT.CLEANUP"
1228 $event : VirtualControlLoopEvent( $clName: closedLoopControlName )
1229 not ( ControlLoopParams( getClosedLoopControlName() == $clName) )
1232 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1233 logger.info("{}: {}", $clName, drools.getRule().getName());
1234 logger.debug("{}: {}: orphan onset event={}",
1235 $clName, drools.getRule().getName(), $event);