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.util.AAIException;
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.vfc.VFCRequest;
46 import org.onap.policy.vfc.VFCResponse;
47 import org.onap.policy.vfc.VFCManager;
48 import org.onap.policy.so.SOManager;
49 import org.onap.policy.so.SORequest;
50 import org.onap.policy.so.SORequestStatus;
51 import org.onap.policy.so.SORequestDetails;
52 import org.onap.policy.so.SOModelInfo;
53 import org.onap.policy.so.SOCloudConfiguration;
54 import org.onap.policy.so.SORequestInfo;
55 import org.onap.policy.so.SORequestParameters;
56 import org.onap.policy.so.SORelatedInstanceListElement;
57 import org.onap.policy.so.SORelatedInstance;
58 import org.onap.policy.so.SOResponse;
59 import org.onap.policy.so.SOResponseWrapper;
60 import org.onap.policy.guard.PolicyGuard;
61 import org.onap.policy.guard.PolicyGuard.LockResult;
62 import org.onap.policy.guard.TargetLock;
63 import org.onap.policy.guard.GuardResult;
64 import org.onap.policy.guard.PolicyGuardRequest;
65 import org.onap.policy.guard.PolicyGuardResponse;
66 import org.onap.policy.guard.PolicyGuardXacmlRequestAttributes;
67 import org.onap.policy.guard.PolicyGuardXacmlHelper;
69 import org.yaml.snakeyaml.Yaml;
70 import org.yaml.snakeyaml.constructor.Constructor;
72 import org.slf4j.LoggerFactory;
73 import org.slf4j.Logger;
75 import java.time.Instant;
76 import java.util.LinkedList;
77 import java.util.Iterator;
79 import org.onap.policy.drools.system.PolicyEngine;
84 declare OperationTimer
85 closedLoopControlName : String
93 declare ControlLoopTimer
94 closedLoopControlName : String
101 * Called to insert the ControlLoopParams object into working memory from the BRMSGW.
106 $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() + "." + drools.getRule().getName(), $params.getControlLoopYaml());
120 * This rule responds to DCAE Events where there is no manager yet. Either it is
121 * the first ONSET, or a subsequent badly formed Event (i.e. Syntax error, or is-closed-loop-disabled)
126 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
127 $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
128 not ( ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID() ) )
131 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
132 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
137 // Check the event, because we need it to not be null when
138 // we create the ControlLoopEventManager. The ControlLoopEventManager
139 // will do extra syntax checking as well check if the closed loop is disabled.
141 if ($event.getRequestID() == null) {
142 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
143 notification.setNotification(ControlLoopNotificationType.REJECTED);
144 notification.setFrom("policy");
145 notification.setMessage("Missing requestID");
146 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
147 notification.setPolicyScope($params.getPolicyScope());
148 notification.setPolicyVersion($params.getPolicyVersion());
151 // Let interested parties know
153 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
156 // Retract it from memory
159 } else if ($event.getClosedLoopEventStatus() != ControlLoopEventStatus.ONSET) {
160 throw new ControlLoopException($event.getClosedLoopEventStatus() + " received with no prior onset");
163 // Create an EventManager
165 ControlLoopEventManager manager = new ControlLoopEventManager($clName, $event.getRequestID());
167 // Determine if EventManager can actively process the event (i.e. syntax, is_closed_loop_disabled checks etc.)
169 VirtualControlLoopNotification notification = manager.activate($params.getControlLoopYaml(), $event);
170 notification.setFrom("pdp-0001-controller=controlloop"); // Engine.getInstanceName()
171 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
172 notification.setPolicyScope($params.getPolicyScope());
173 notification.setPolicyVersion($params.getPolicyVersion());
175 // Are we actively pursuing this event?
177 if (notification.getNotification() == ControlLoopNotificationType.ACTIVE) {
179 // Insert Event Manager into memory, this will now kick off processing.
183 // Let interested parties know
185 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
187 // Setup the Overall Control Loop timer
189 ControlLoopTimer clTimer = new ControlLoopTimer();
190 clTimer.setClosedLoopControlName($event.getClosedLoopControlName());
191 clTimer.setRequestID($event.getRequestID().toString());
192 clTimer.setDelay(manager.getControlLoopTimeout(1500) + "s");
199 // Let interested parties know
201 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
203 // Retract it from memory
209 // Now that the manager is inserted into Drools working memory, we'll wait for
210 // another rule to fire in order to continue processing. This way we can also
211 // then screen for additional ONSET and ABATED events for this RequestID.
214 } catch (Exception e) {
215 logger.warn("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName(), e);
217 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
218 notification.setNotification(ControlLoopNotificationType.REJECTED);
219 notification.setMessage("Exception occurred: " + e.getMessage());
220 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
221 notification.setPolicyScope($params.getPolicyScope());
222 notification.setPolicyVersion($params.getPolicyVersion());
226 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
236 * This rule happens when we got a valid ONSET, closed loop is enabled and an Event Manager
237 * is now created. We can start processing the yaml specification via the Event Manager.
242 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
243 $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
244 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID() )
245 $clTimer : ControlLoopTimer ( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID().toString() )
248 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
249 logger.info("{}: {}: event={} manager={} clTimer={}",
250 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
251 $event, $manager, $clTimer);
255 // Check which event this is.
257 ControlLoopEventManager.NEW_EVENT_STATUS eventStatus = $manager.onNewEvent($event);
259 // Check what kind of event this is
261 if (eventStatus == NEW_EVENT_STATUS.SUBSEQUENT_ONSET) {
263 // We don't care about subsequent onsets
265 logger.info("{}: {}: subsequent onset",
266 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
270 if (eventStatus == NEW_EVENT_STATUS.SYNTAX_ERROR) {
272 // Ignore any bad syntax events
274 logger.warn("{}: {}: syntax error",
275 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
280 // We only want the initial ONSET event in memory,
281 // all the other events need to be retracted to support
282 // cleanup and avoid the other rules being fired for this event.
284 if (eventStatus != NEW_EVENT_STATUS.FIRST_ONSET) {
285 logger.warn("{}: {}: no first onset",
286 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
290 logger.debug("{}: {}: target={}", $clName,
291 $params.getPolicyName() + "." + drools.getRule().getName(), $event.getTarget());
293 // Now start seeing if we need to process this event
297 // Check if this is a Final Event
299 VirtualControlLoopNotification notification = $manager.isControlLoopFinal();
302 if (notification != null) {
304 // Its final, but are we waiting for abatement?
306 if ($manager.getNumAbatements() > 0) {
307 logger.info("{}: {}: abatement received for {}. Closing the control loop",
308 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
309 $event.getRequestID());
310 notification.setFrom("policy");
311 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
312 notification.setPolicyScope($params.getPolicyScope());
313 notification.setPolicyVersion($params.getPolicyVersion());
315 // In this case, we are done
317 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
321 TargetLock lock = $manager.unlockCurrentOperation();
323 logger.debug("{}: {}: retracting lock=", $clName,
324 $params.getPolicyName() + "." + drools.getRule().getName(), lock);
328 // Retract everything from memory
330 logger.info("{}: {}: retracting onset, manager, and timer",
331 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
333 retract($manager.getOnsetEvent());
337 // TODO - what if we get subsequent Events for this RequestID?
338 // By default, it will all start over again. May be confusing for Ruby.
339 // Or, we could track this and then subsequently ignore the events
343 // Check whether we need to wait for abatement
345 if ($manager.getProcessor().getControlLoop().getAbatement() == true && notification.getNotification() == ControlLoopNotificationType.FINAL_SUCCESS) {
346 logger.info("{}: {}: waiting for abatement ..",
347 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
349 logger.info("{}: {}: no abatement expect for {}. Closing the control loop",
350 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
351 $event.getRequestID());
353 notification.setFrom("policy");
354 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
355 notification.setPolicyScope($params.getPolicyScope());
356 notification.setPolicyVersion($params.getPolicyVersion());
359 // In this case, we are done
361 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
365 TargetLock lock = $manager.unlockCurrentOperation();
367 logger.debug("{}: {}: retracting lock=", $clName,
368 $params.getPolicyName() + "." + drools.getRule().getName(), lock);
372 // Retract everything from memory
374 logger.info("{}: {}: retracting onset, manager, and timer",
375 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
377 retract($manager.getOnsetEvent());
384 // NOT final, so let's ask for the next operation
386 ControlLoopOperationManager operation = $manager.processControlLoop();
387 if (operation != null) {
389 // Let's ask for a lock right away
391 LockResult<GuardResult, TargetLock> result = $manager.lockCurrentOperation();
392 logger.info("{}: {}: guard lock acquired={}",
393 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
395 if (result.getA().equals(GuardResult.LOCK_ACQUIRED)) {
397 // insert the operation into memory
402 // insert operation timeout object
404 OperationTimer opTimer = new OperationTimer();
405 opTimer.setClosedLoopControlName($event.getClosedLoopControlName());
406 opTimer.setRequestID($event.getRequestID().toString());
407 opTimer.setDelay(operation.getOperationTimeout().toString() + "s");
411 // Insert lock into memory
413 insert(result.getB());
416 logger.debug("The target resource {} is already processing",
417 $event.getAAI().get($event.getTarget()));
418 notification = new VirtualControlLoopNotification($event);
419 notification.setNotification(ControlLoopNotificationType.REJECTED);
420 notification.setMessage("The target " + $event.getAAI().get($event.getTarget()) + " is already locked");
421 notification.setFrom("policy");
422 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
423 notification.setPolicyScope($params.getPolicyScope());
424 notification.setPolicyVersion($params.getPolicyVersion());
426 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
432 logger.info("{}: {}: starting operation={}",
433 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
437 // Probably waiting for abatement
439 logger.info("{}: {}: no operation, probably waiting for abatement",
440 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
443 } catch (Exception e) {
444 logger.warn("{}: {}: unexpected",
446 $params.getPolicyName() + "." + drools.getRule().getName(), e);
448 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
449 notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
450 notification.setMessage(e.getMessage());
451 notification.setFrom("policy");
452 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
453 notification.setPolicyScope($params.getPolicyScope());
454 notification.setPolicyVersion($params.getPolicyVersion());
456 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
467 * Guard Permitted, let's send request to the actor.
470 rule "EVENT.MANAGER.OPERATION.LOCKED.GUARD_PERMITTED"
472 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
473 $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
474 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID() )
475 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestID() == $event.getRequestID(), "Permit".equalsIgnoreCase(getGuardApprovalStatus()) )
476 $lock : TargetLock (requestID == $event.getRequestID())
477 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID().toString() )
480 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
481 logger.info("{}: {}: event={} manager={} operation={} lock={}",
482 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
483 $event, $manager, $operation, $lock);
485 Object request = null;
486 boolean caughtException = false;
488 request = $operation.startOperation($event);
490 catch (ControlLoopException e) {
491 String msg = e.getMessage();
492 logger.warn("{}: {}: operation={}: AAI failure: {}",
493 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
495 $operation.setOperationHasException(msg);
498 caughtException = true;
501 // Having the modify statement in the catch clause doesn't work for whatever reason
502 if (caughtException) {
503 modify($manager) {finishOperation($operation)};
505 else if (request != null) {
506 logger.debug("{}: {}: starting operation ..",
507 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
509 // Tell interested parties we are performing this Operation
511 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
512 notification.setNotification(ControlLoopNotificationType.OPERATION);
513 notification.setMessage($operation.getOperationMessage());
514 notification.setHistory($operation.getHistory());
515 notification.setFrom("policy");
516 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
517 notification.setPolicyScope($params.getPolicyScope());
518 notification.setPolicyVersion($params.getPolicyVersion());
520 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
522 switch ($operation.policy.getActor()){
526 if (request instanceof Request) {
527 PolicyEngine.manager.deliver("APPC-CL", request);
529 else if (request instanceof LCMRequestWrapper) {
530 PolicyEngine.manager.deliver("APPC-LCM-READ", request);
534 // at this point the AAI named query request should have already been made, the response recieved and used
535 // in the construction of the SO Request which is stored in operationRequest
537 if(request instanceof SORequest) {
538 // Call SO. The response will be inserted into memory once it's received
539 SOActorServiceProvider.sendRequest($event.getRequestID().toString(), drools.getWorkingMemory(), request);
543 if (request instanceof VFCRequest) {
545 Thread t = new Thread(new VFCManager(drools.getWorkingMemory(), (VFCRequest)request));
552 // What happens if its null?
554 logger.warn("{}: {}: unexpected null operation request",
556 $params.getPolicyName() + "." + drools.getRule().getName());
557 if ("SO".equals($operation.policy.getActor())) {
560 modify($manager) {finishOperation($operation)};
562 else if ("vfc".equalsIgnoreCase($operation.policy.getActor())) {
565 modify($manager) {finishOperation($operation)};
574 * We were able to acquire a lock so now let's ask Xacml Guard whether
575 * we are allowed to proceed with the request to the actor.
578 rule "EVENT.MANAGER.OPERATION.LOCKED.GUARD_NOT_YET_QUERIED"
580 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
581 $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
582 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID() )
583 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestID() == $event.getRequestID(), getGuardApprovalStatus() == "NONE" )
584 $lock : TargetLock (requestID == $event.getRequestID())
587 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
588 logger.info("{}: {}: event={} manager={} operation={} lock={}",
589 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
590 $event, $manager, $operation, $lock);
593 // Sending notification that we are about to query Guard ("DB write - start operation")
595 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
596 notification.setNotification(ControlLoopNotificationType.OPERATION);
597 notification.setMessage("Sending guard query for " + $operation.policy.getActor() + " " + $operation.policy.getRecipe());
598 notification.setHistory($operation.getHistory());
599 notification.setFrom("policy");
600 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
601 notification.setPolicyScope($params.getPolicyScope());
602 notification.setPolicyVersion($params.getPolicyVersion());
604 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
607 // Now send Guard Request to XACML Guard. In order to bypass the call to Guard,
608 // just change guardEnabled to false.
610 // In order to use REST XACML, provide a URL instead of "" as a second argument
611 // to the CallGuardTask() and set the first argument to null
612 // (instead of XacmlPdpEngine).
615 // NOTE: The environment properties uses "guard.disabled" but the boolean is guardEnabled
616 boolean guardEnabled = "false".equalsIgnoreCase(PolicyEngine.manager.getEnvironmentProperty("guard.disabled"));
620 Thread t = new Thread(new org.onap.policy.guard.CallGuardTask(
621 drools.getWorkingMemory(),
622 $event.getClosedLoopControlName(),
623 $operation.policy.getActor().toString(),
624 $operation.policy.getRecipe(),
625 $operation.getTargetEntity(),
626 $event.getRequestID().toString()
631 insert(new PolicyGuardResponse("Permit", $event.getRequestID(), $operation.policy.getRecipe()));
637 // This rule will be triggered when a thread talking to the XACML Guard inserts a
638 // guardResponse object into the working memory
640 rule "GUARD.RESPONSE"
642 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
643 $event : VirtualControlLoopEvent( closedLoopControlName == $clName, closedLoopEventStatus == ControlLoopEventStatus.ONSET )
644 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID() )
645 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestID() == $event.getRequestID() )
646 $lock : TargetLock (requestID == $event.getRequestID())
647 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID().toString() )
648 $guardResponse : PolicyGuardResponse(requestID == $event.getRequestID(), $operation.policy.recipe == operation)
651 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
652 logger.info("{}: {}: event={} manager={} operation={} lock={} opTimer={} guardResponse={}",
653 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
654 $event, $manager, $operation, $lock, $opTimer, $guardResponse);
657 //we will permit the operation if there was no Guard for it
658 if("Indeterminate".equalsIgnoreCase($guardResponse.getResult())){
659 $guardResponse.setResult("Permit");
663 // This notification has Guard result in "message". ("DB write - end operation in case of Guard Deny")
665 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
666 notification.setNotification(ControlLoopNotificationType.OPERATION);
667 notification.setMessage("Guard result for " + $operation.policy.getActor() + " " + $operation.policy.getRecipe() + " is " + $guardResponse.getResult());
668 notification.setHistory($operation.getHistory());
669 notification.setFrom("policy");
670 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
671 notification.setPolicyScope($params.getPolicyScope());
672 notification.setPolicyVersion($params.getPolicyVersion());
674 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
676 if("Permit".equalsIgnoreCase($guardResponse.getResult())){
678 modify($operation){setGuardApprovalStatus($guardResponse.getResult())};
681 //This is the Deny case
682 $operation.startOperation($event);
683 $operation.setOperationHasGuardDeny();
686 modify($manager) {finishOperation($operation)};
689 retract($guardResponse);
695 * This rule responds to APPC Response Events
697 * I would have like to be consistent and write the Response like this:
698 * $response : Response( CommonHeader.RequestID == $onset.getRequestID() )
700 * However, no compile error was given. But a runtime error was given. I think
701 * because drools is confused between the classname CommonHeader vs the property CommonHeader.
706 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
707 $event : VirtualControlLoopEvent( closedLoopControlName == $clName, closedLoopEventStatus == ControlLoopEventStatus.ONSET )
708 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID() )
709 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestID() == $event.getRequestID() )
710 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID().toString() )
711 $lock : TargetLock (requestID == $event.getRequestID())
712 $response : Response( getCommonHeader().RequestID == $event.getRequestID() )
715 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
716 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
717 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
718 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
719 $event, $manager, $operation, $lock, $opTimer, $response);
721 // Get the result of the operation
723 PolicyResult policyResult = $operation.onResponse($response);
724 if (policyResult != null) {
725 logger.debug("{}: {}: operation finished - result={}",
726 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
729 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
731 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
732 notification.setFrom("policy");
733 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
734 notification.setPolicyScope($params.getPolicyScope());
735 notification.setPolicyVersion($params.getPolicyVersion());
736 notification.setMessage($operation.getOperationHistory());
737 notification.setHistory($operation.getHistory());
738 if (policyResult.equals(PolicyResult.SUCCESS)) {
739 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
741 // Let interested parties know
743 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
745 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
747 // Let interested parties know
749 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
752 // Ensure the operation is complete
754 if ($operation.isOperationComplete() == true) {
756 // It is complete, remove it from memory
760 // We must also retract the timer object
761 // NOTE: We could write a Rule to do this
765 // Complete the operation
767 modify($manager) {finishOperation($operation)};
770 // Just doing this will kick off the LOCKED rule again
772 modify($operation) {};
776 // Its not finished yet (i.e. expecting more Response objects)
778 // Or possibly it is a leftover response that we timed the request out previously
782 // We are going to retract these objects from memory
789 * The problem with Responses is that they don't have a controlLoopControlName
790 * field in them, so the only way to attach them is via RequestID. If we have multiple
791 * control loop .drl's loaded in the same container, we need to be sure the cleanup
792 * rules don't remove Responses for other control loops.
795 rule "APPC.RESPONSE.CLEANUP"
797 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
798 $response : Response($id : getCommonHeader().RequestID )
799 not ( VirtualControlLoopEvent( requestID == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
802 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
803 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
804 logger.debug("{}: {}: orphan appc response={}",
805 $clName, $params.getPolicyName() + "." + drools.getRule().getName(), $id);
815 * This rule responds to APPC Response Events using the new LCM interface provided by appc
818 rule "APPC.LCM.RESPONSE"
820 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
821 $event : VirtualControlLoopEvent( closedLoopControlName == $clName, closedLoopEventStatus == ControlLoopEventStatus.ONSET )
822 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID() )
823 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestID() == $event.getRequestID() )
824 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID().toString() )
825 $lock : TargetLock (requestID == $event.getRequestID())
826 $response : LCMResponseWrapper( getBody().getCommonHeader().getRequestId() == $event.getRequestID() )
829 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
830 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
831 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
832 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
833 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
836 // Get the result of the operation
838 PolicyResult policyResult = $operation.onResponse($response);
839 if (policyResult != null) {
840 logger.debug("{}: {}: operation finished - result={}",
841 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
845 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
847 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
848 notification.setFrom("policy");
849 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
850 notification.setPolicyScope($params.getPolicyScope());
851 notification.setPolicyVersion($params.getPolicyVersion());
852 notification.setMessage($operation.getOperationHistory());
853 notification.setHistory($operation.getHistory());
854 if (policyResult.equals(PolicyResult.SUCCESS)) {
855 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
857 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
859 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
861 // Ensure the operation is complete
863 if ($operation.isOperationComplete() == true) {
865 // It is complete, remove it from memory
869 // We must also retract the timer object
870 // NOTE: We could write a Rule to do this
874 // Complete the operation
876 modify($manager) {finishOperation($operation)};
879 // Just doing this will kick off the LOCKED rule again
881 modify($operation) {};
885 // Its not finished yet (i.e. expecting more Response objects)
887 // Or possibly it is a leftover response that we timed the request out previously
891 // We are going to retract these objects from memory
898 * Clean Up any lingering LCM reponses
901 rule "APPC.LCM.RESPONSE.CLEANUP"
903 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
904 $response : LCMResponseWrapper($id : getBody().getCommonHeader().getRequestId )
905 not ( VirtualControlLoopEvent( requestID == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
908 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
909 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
910 logger.debug("{}: {}: orphan appc response={}",
911 $clName, $params.getPolicyName() + "." + drools.getRule().getName(), $id);
920 * This rule responds to SO Response Events
925 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
926 $event : VirtualControlLoopEvent( closedLoopControlName == $clName, closedLoopEventStatus == ControlLoopEventStatus.ONSET )
927 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName() )
928 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestID() == $event.getRequestID() )
929 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID().toString() )
930 $lock : TargetLock (requestID == $event.getRequestID())
931 $response : SOResponseWrapper(requestID.toString() == $event.getRequestID().toString() )
934 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
935 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
936 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
937 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
938 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
940 // Get the result of the operation
942 PolicyResult policyResult = $operation.onResponse($response);
943 if (policyResult != null) {
944 logger.debug("{}: {}: operation finished - result={}",
945 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
949 // This Operation has completed, construct a notification showing our results
951 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
952 notification.setFrom("policy");
953 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
954 notification.setPolicyScope($params.getPolicyScope());
955 notification.setPolicyVersion($params.getPolicyVersion());
956 notification.setMessage($operation.getOperationHistory());
957 notification.setHistory($operation.getHistory());
958 if (policyResult.equals(PolicyResult.SUCCESS)) {
959 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
961 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
964 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
966 // Ensure the operation is complete
968 if ($operation.isOperationComplete() == true) {
970 // It is complete, remove it from memory
974 // We must also retract the timer object
975 // NOTE: We could write a Rule to do this
979 // Complete the operation
981 modify($manager) {finishOperation($operation)};
984 // Just doing this will kick off the LOCKED rule again
986 modify($operation) {};
990 // Its not finished yet (i.e. expecting more Response objects)
992 // Or possibly it is a leftover response that we timed the request out previously
996 // We are going to retract these objects from memory
1004 * This rule responds to VFC Response Events
1009 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
1010 $event : VirtualControlLoopEvent( closedLoopControlName == $clName, closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1011 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName() )
1012 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestID() == $event.getRequestID() )
1013 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID().toString() )
1014 $lock : TargetLock (requestID == $event.getRequestID())
1015 $response : VFCResponse( requestId.toString() == $event.getRequestID().toString() )
1017 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1018 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
1019 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
1020 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
1021 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
1023 // Get the result of the operation
1025 PolicyResult policyResult = $operation.onResponse($response);
1026 if (policyResult != null) {
1028 // This Operation has completed, construct a notification showing our results
1030 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1031 notification.setFrom("policy");
1032 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
1033 notification.setPolicyScope($params.getPolicyScope());
1034 notification.setPolicyVersion($params.getPolicyVersion());
1035 notification.setMessage($operation.getOperationHistory());
1036 notification.setHistory($operation.getHistory());
1038 // Ensure the operation is complete
1040 if ($operation.isOperationComplete() == true) {
1042 // It is complete, remove it from memory
1044 retract($operation);
1046 // We must also retract the timer object
1047 // NOTE: We could write a Rule to do this
1051 // Complete the operation
1053 modify($manager) {finishOperation($operation)};
1056 // Just doing this will kick off the LOCKED rule again
1058 modify($operation) {};
1062 // Its not finished yet (i.e. expecting more Response objects)
1064 // Or possibly it is a leftover response that we timed the request out previously
1068 // We are going to retract these objects from memory
1076 * This is the timer that manages the timeout for an individual operation.
1079 rule "EVENT.MANAGER.OPERATION.TIMEOUT"
1082 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
1083 $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
1084 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID() )
1085 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestID() == $event.getRequestID() )
1086 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID().toString(), $to : getDelay() )
1087 $lock : TargetLock (requestID == $event.getRequestID())
1090 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1091 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
1092 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={}",
1093 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
1094 $event, $manager, $operation, $lock, $operation, $opTimer);
1097 // Tell it its timed out
1099 $operation.setOperationHasTimedOut();
1101 // Create a notification for it ("DB Write - end operation")
1103 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1104 notification.setFrom("policy");
1105 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
1106 notification.setPolicyScope($params.getPolicyScope());
1107 notification.setPolicyVersion($params.getPolicyVersion());
1108 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
1109 notification.setMessage($operation.getOperationHistory());
1110 notification.setHistory($operation.getHistory());
1112 // Let interested parties know
1114 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1116 // Get rid of the timer
1120 // Ensure the operation is complete
1122 if ($operation.isOperationComplete() == true) {
1124 // It is complete, remove it from memory
1126 retract($operation);
1128 // Complete the operation
1130 modify($manager) {finishOperation($operation)};
1133 // Just doing this will kick off the LOCKED rule again
1135 modify($operation) {};
1141 * This is the timer that manages the overall control loop timeout.
1144 rule "EVENT.MANAGER.TIMEOUT"
1147 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
1148 $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
1149 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID() )
1150 $clTimer : ControlLoopTimer ( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID().toString(), $to : getDelay() )
1151 $operations : LinkedList()
1152 from collect( ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestID() == $event.getRequestID() ) )
1153 $opTimers : LinkedList()
1154 from collect( OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestID().toString() ) )
1155 $locks : LinkedList()
1156 from collect( TargetLock (requestID == $event.getRequestID()) )
1159 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1160 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
1162 if ($operations == null) {
1163 logger.debug("{}: {}: event={} manager={} clTimer={} operations=0",
1164 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
1165 $event, $manager, $clTimer);
1167 logger.debug("{}: {}: event={} manager={} clTimer={} operations={}",
1168 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
1169 $event, $manager, $clTimer, $operations.size());
1172 // Tell the Event Manager it has timed out
1174 VirtualControlLoopNotification notification = $manager.setControlLoopTimedOut();
1175 if (notification != null) {
1176 notification.setFrom("policy");
1177 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
1178 notification.setPolicyScope($params.getPolicyScope());
1179 notification.setPolicyVersion($params.getPolicyVersion());
1181 // Let interested parties know
1183 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1186 // Retract EVERYTHING
1191 if ($operations != null && $operations.size() > 0) {
1192 Iterator<ControlLoopOperationManager> iter = $operations.iterator();
1193 while (iter.hasNext()) {
1194 ControlLoopOperationManager manager = iter.next();
1198 if ($opTimers != null && $opTimers.size() > 0) {
1199 Iterator<OperationTimer> iter = $opTimers.iterator();
1200 while (iter.hasNext()) {
1201 OperationTimer opTimer = iter.next();
1205 if ($locks != null && $locks.size() > 0) {
1206 Iterator<TargetLock> iter = $locks.iterator();
1207 while (iter.hasNext()) {
1208 TargetLock lock = iter.next();
1210 // Ensure we release the lock
1212 PolicyGuard.unlockTarget(lock);
1223 * This rule will clean up any rogue onsets where there is no
1224 * ControlLoopParams object corresponding to the onset event.
1227 rule "EVENT.CLEANUP"
1229 $event : VirtualControlLoopEvent( $clName: closedLoopControlName )
1230 not ( ControlLoopParams( getClosedLoopControlName() == $clName) )
1233 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1234 logger.info("{}: {}", $clName, drools.getRule().getName());
1235 logger.debug("{}: {}: orphan onset event={}",
1236 $clName, drools.getRule().getName(), $event);