2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-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.VirtualControlLoopEvent;
24 import org.onap.policy.controlloop.VirtualControlLoopNotification;
25 import org.onap.policy.controlloop.ControlLoopEventStatus;
26 import org.onap.policy.controlloop.ControlLoopNotificationType;
27 import org.onap.policy.controlloop.ControlLoopLogger;
28 import org.onap.policy.controlloop.policy.PolicyResult;
29 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
30 import org.onap.policy.controlloop.policy.Policy;
31 import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager;
32 import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager.NEW_EVENT_STATUS;
33 import org.onap.policy.controlloop.eventmanager.ControlLoopOperationManager;
34 import org.onap.policy.controlloop.actor.so.SOActorServiceProvider;
35 import org.onap.policy.appc.Request;
36 import org.onap.policy.appc.Response;
37 import org.onap.policy.appc.CommonHeader;
38 import org.onap.policy.appclcm.LcmRequestWrapper;
39 import org.onap.policy.appclcm.LcmResponseWrapper;
40 import org.onap.policy.appclcm.LcmRequest;
41 import org.onap.policy.appclcm.LcmResponse;
42 import org.onap.policy.appclcm.LcmCommonHeader;
43 import org.onap.policy.vfc.VFCRequest;
44 import org.onap.policy.vfc.VFCResponse;
45 import org.onap.policy.vfc.VFCManager;
46 import org.onap.policy.so.SOManager;
47 import org.onap.policy.so.SORequest;
48 import org.onap.policy.so.SORequestStatus;
49 import org.onap.policy.so.SORequestDetails;
50 import org.onap.policy.so.SOModelInfo;
51 import org.onap.policy.so.SOCloudConfiguration;
52 import org.onap.policy.so.SORequestInfo;
53 import org.onap.policy.so.SORequestParameters;
54 import org.onap.policy.so.SORelatedInstanceListElement;
55 import org.onap.policy.so.SORelatedInstance;
56 import org.onap.policy.so.SOResponse;
57 import org.onap.policy.so.SOResponseWrapper;
58 import org.onap.policy.guard.PolicyGuard;
59 import org.onap.policy.guard.PolicyGuard.LockResult;
60 import org.onap.policy.guard.TargetLock;
61 import org.onap.policy.guard.GuardResult;
62 import org.onap.policy.guard.PolicyGuardRequest;
63 import org.onap.policy.guard.PolicyGuardResponse;
64 import org.onap.policy.guard.PolicyGuardXacmlRequestAttributes;
65 import org.onap.policy.guard.PolicyGuardXacmlHelper;
67 import org.yaml.snakeyaml.Yaml;
68 import org.yaml.snakeyaml.constructor.Constructor;
70 import org.slf4j.LoggerFactory;
71 import org.slf4j.Logger;
73 import java.time.Instant;
74 import java.util.LinkedList;
75 import java.util.Iterator;
77 import org.onap.policy.drools.system.PolicyEngine;
80 * This structure mimics the Params structure.
81 * Its only purpose is to allow management of
82 * rules by the PAP component..
83 * It has no use at runtime since the rules go by
84 * Params for matching purposes.
87 closedLoopControlName : String
88 controlLoopYaml : String
92 * Control Loop Identity
95 closedLoopControlName : String
96 controlLoopYaml : String
103 declare OperationTimer
104 closedLoopControlName : String
112 declare ControlLoopTimer
113 closedLoopControlName : String
120 * Called once and only once to insert the parameters into working memory for this Closed Loop policy.
123 rule "${policyName}.SETUP"
127 Params params = new Params();
128 params.setClosedLoopControlName("${closedLoopControlName}");
129 params.setControlLoopYaml("${controlLoopYaml}");
132 // Note: globals have bad behavior when persistence is used,
133 // hence explicitly getting the logger vs using a global
135 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
136 logger.info("{}: {} : YAML=[{}]", params.getClosedLoopControlName(), drools.getRule().getName(), params.getControlLoopYaml());
141 * This rule responds to DCAE Events where there is no manager yet. Either it is
142 * the first ONSET, or a subsequent badly formed Event (i.e. Syntax error, or is-closed-loop-disabled)
145 rule "${policyName}.EVENT"
147 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
148 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
149 not ( ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() ) )
152 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
153 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
158 // Check the event, because we need it to not be null when
159 // we create the ControlLoopEventManager. The ControlLoopEventManager
160 // will do extra syntax checking as well check if the closed loop is disabled.
162 if ($event.getRequestId() == null) {
163 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
164 notification.setNotification(ControlLoopNotificationType.REJECTED);
165 notification.setFrom("policy");
166 notification.setMessage("Missing requestId");
167 notification.setPolicyName(drools.getRule().getName());
168 notification.setPolicyScope("${policyScope}");
169 notification.setPolicyVersion("${policyVersion}");
172 // Let interested parties know
174 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
177 // Retract it from memory
180 } else if ($event.getClosedLoopEventStatus() != ControlLoopEventStatus.ONSET) {
181 throw new ControlLoopException($event.getClosedLoopEventStatus() + " received with no prior onset");
184 // Create an EventManager
186 ControlLoopEventManager manager = new ControlLoopEventManager($params.getClosedLoopControlName(), $event.getRequestId());
188 // Determine if EventManager can actively process the event (i.e. syntax, is_closed_loop_disabled checks etc.)
190 VirtualControlLoopNotification notification = manager.activate($params.getControlLoopYaml(), $event);
191 notification.setFrom("pdp-0001-controller=controlloop"); // Engine.getInstanceName()
192 notification.setPolicyName(drools.getRule().getName());
193 notification.setPolicyScope("${policyScope}");
194 notification.setPolicyVersion("${policyVersion}");
196 // Are we actively pursuing this event?
198 if (notification.getNotification() == ControlLoopNotificationType.ACTIVE) {
200 // Insert Event Manager into memory, this will now kick off processing.
204 // Let interested parties know
206 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
208 // Setup the Overall Control Loop timer
210 ControlLoopTimer clTimer = new ControlLoopTimer();
211 clTimer.setClosedLoopControlName($event.getClosedLoopControlName());
212 clTimer.setRequestID($event.getRequestId().toString());
213 clTimer.setDelay(manager.getControlLoopTimeout(1500) + "s");
220 // Let interested parties know
222 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
224 // Retract it from memory
230 // Now that the manager is inserted into Drools working memory, we'll wait for
231 // another rule to fire in order to continue processing. This way we can also
232 // then screen for additional ONSET and ABATED events for this RequestId.
235 } catch (Exception e) {
236 logger.warn("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName(), e);
238 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
239 notification.setNotification(ControlLoopNotificationType.REJECTED);
240 notification.setMessage("Exception occurred: " + e.getMessage());
241 notification.setPolicyName(drools.getRule().getName());
242 notification.setPolicyScope("${policyScope}");
243 notification.setPolicyVersion("${policyVersion}");
247 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
257 * This rule happens when we got a valid ONSET, closed loop is enabled and an Event Manager
258 * is now created. We can start processing the yaml specification via the Event Manager.
261 rule "${policyName}.EVENT.MANAGER"
263 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
264 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
265 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
266 $clTimer : ControlLoopTimer ( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
269 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
270 logger.info("{}: {}: event={} manager={} clTimer={}",
271 $params.getClosedLoopControlName(), drools.getRule().getName(),
272 $event, $manager, $clTimer);
276 // Check which event this is.
278 ControlLoopEventManager.NEW_EVENT_STATUS eventStatus = $manager.onNewEvent($event);
280 // Check what kind of event this is
282 if (eventStatus == NEW_EVENT_STATUS.SUBSEQUENT_ONSET) {
284 // We don't care about subsequent onsets
286 logger.info("{}: {}: subsequent onset",
287 $params.getClosedLoopControlName(), drools.getRule().getName());
291 if (eventStatus == NEW_EVENT_STATUS.SYNTAX_ERROR) {
293 // Ignore any bad syntax events
295 logger.warn("{}: {}: syntax error",
296 $params.getClosedLoopControlName(), drools.getRule().getName());
301 // We only want the initial ONSET event in memory,
302 // all the other events need to be retracted to support
303 // cleanup and avoid the other rules being fired for this event.
305 if (eventStatus != NEW_EVENT_STATUS.FIRST_ONSET) {
306 logger.warn("{}: {}: no first onset",
307 $params.getClosedLoopControlName(), drools.getRule().getName());
311 logger.debug("{}: {}: target={}", $params.getClosedLoopControlName(),
312 drools.getRule().getName(), $event.getTarget());
314 // Now start seeing if we need to process this event
318 // Check if this is a Final Event
320 VirtualControlLoopNotification notification = $manager.isControlLoopFinal();
323 if (notification != null) {
325 // Its final, but are we waiting for abatement?
327 if ($manager.getNumAbatements() > 0) {
328 logger.info("{}: {}: abatement received for {}. Closing the control loop",
329 $params.getClosedLoopControlName(), drools.getRule().getName(),
330 $event.getRequestId());
331 notification.setFrom("policy");
332 notification.setPolicyName(drools.getRule().getName());
333 notification.setPolicyScope("${policyScope}");
334 notification.setPolicyVersion("${policyVersion}");
336 // In this case, we are done
338 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
342 TargetLock lock = $manager.unlockCurrentOperation();
344 logger.debug("{}: {}: retracting lock=", $params.getClosedLoopControlName(),
345 drools.getRule().getName(), lock);
349 // Retract everything from memory
351 logger.info("{}: {}: retracting onset, manager, and timer",
352 $params.getClosedLoopControlName(), drools.getRule().getName());
354 retract($manager.getOnsetEvent());
358 // TODO - what if we get subsequent Events for this RequestId?
359 // By default, it will all start over again. May be confusing for Ruby.
360 // Or, we could track this and then subsequently ignore the events
364 // Check whether we need to wait for abatement
366 if ($manager.getProcessor().getControlLoop().getAbatement() == true && notification.getNotification() == ControlLoopNotificationType.FINAL_SUCCESS) {
367 logger.info("{}: {}: waiting for abatement ..",
368 $params.getClosedLoopControlName(), drools.getRule().getName());
370 logger.info("{}: {}: no abatement expect for {}. Closing the control loop",
371 $params.getClosedLoopControlName(), drools.getRule().getName(),
372 $event.getRequestId());
374 notification.setFrom("policy");
375 notification.setPolicyName(drools.getRule().getName());
376 notification.setPolicyScope("${policyScope}");
377 notification.setPolicyVersion("${policyVersion}");
380 // In this case, we are done
382 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
386 TargetLock lock = $manager.unlockCurrentOperation();
388 logger.debug("{}: {}: retracting lock=", $params.getClosedLoopControlName(),
389 drools.getRule().getName(), lock);
393 // Retract everything from memory
395 logger.info("{}: {}: retracting onset, manager, and timer",
396 $params.getClosedLoopControlName(), drools.getRule().getName());
398 retract($manager.getOnsetEvent());
405 // NOT final, so let's ask for the next operation
407 ControlLoopOperationManager operation = $manager.processControlLoop();
408 if (operation != null) {
410 // Let's ask for a lock right away
412 LockResult<GuardResult, TargetLock> result = $manager.lockCurrentOperation();
413 logger.info("{}: {}: guard lock acquired={}",
414 $params.getClosedLoopControlName(), drools.getRule().getName(),
416 if (result.getA().equals(GuardResult.LOCK_ACQUIRED)) {
418 // insert the operation into memory
423 // insert operation timeout object
425 OperationTimer opTimer = new OperationTimer();
426 opTimer.setClosedLoopControlName($event.getClosedLoopControlName());
427 opTimer.setRequestID($event.getRequestId().toString());
428 opTimer.setDelay(operation.getOperationTimeout().toString() + "s");
432 // Insert lock into memory
434 insert(result.getB());
437 logger.debug("The target resource {} is already processing",
438 $event.getAai().get($event.getTarget()));
439 notification = new VirtualControlLoopNotification($event);
440 notification.setNotification(ControlLoopNotificationType.REJECTED);
441 notification.setMessage("The target " + $event.getAai().get($event.getTarget()) + " is already locked");
442 notification.setFrom("policy");
443 notification.setPolicyName(drools.getRule().getName());
444 notification.setPolicyScope("${policyScope}");
445 notification.setPolicyVersion("${policyVersion}");
447 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
453 logger.info("{}: {}: starting operation={}",
454 $params.getClosedLoopControlName(), drools.getRule().getName(),
458 // Probably waiting for abatement
460 logger.info("{}: {}: no operation, probably waiting for abatement",
461 $params.getClosedLoopControlName(), drools.getRule().getName());
464 } catch (Exception e) {
465 logger.warn("{}: {}: unexpected",
466 $params.getClosedLoopControlName(),
467 drools.getRule().getName(), e);
469 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
470 notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
471 notification.setMessage(e.getMessage());
472 notification.setFrom("policy");
473 notification.setPolicyName(drools.getRule().getName());
474 notification.setPolicyScope("${policyScope}");
475 notification.setPolicyVersion("${policyVersion}");
477 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
488 * Guard Permitted, let's send request to the actor.
491 rule "${policyName}.EVENT.MANAGER.OPERATION.LOCKED.GUARD_PERMITTED"
493 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
494 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
495 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
496 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId(), "Permit".equalsIgnoreCase(getGuardApprovalStatus()) )
497 $lock : TargetLock (requestID == $event.getRequestId())
498 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
501 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
502 logger.info("{}: {}: event={} manager={} operation={} lock={}",
503 $params.getClosedLoopControlName(), drools.getRule().getName(),
504 $event, $manager, $operation, $lock);
506 Object request = null;
507 boolean caughtException = false;
509 request = $operation.startOperation($event);
511 catch (ControlLoopException e) {
512 String msg = e.getMessage();
513 logger.warn("{}: {}: operation={}: AAI failure: {}",
514 $params.getClosedLoopControlName(), drools.getRule().getName(),
516 $operation.setOperationHasException(msg);
519 caughtException = true;
522 // Having the modify statement in the catch clause doesn't work for whatever reason
523 if (caughtException) {
524 modify($manager) {finishOperation($operation)};
526 else if (request != null) {
527 logger.debug("{}: {}: starting operation ..",
528 $params.getClosedLoopControlName(), drools.getRule().getName());
530 // Tell interested parties we are performing this Operation
532 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
533 notification.setNotification(ControlLoopNotificationType.OPERATION);
534 notification.setMessage($operation.getOperationMessage());
535 notification.setHistory($operation.getHistory());
536 notification.setFrom("policy");
537 notification.setPolicyName(drools.getRule().getName());
538 notification.setPolicyScope("${policyScope}");
539 notification.setPolicyVersion("${policyVersion}");
541 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
543 switch ($operation.policy.getActor()){
547 if (request instanceof Request) {
548 PolicyEngine.manager.deliver("APPC-CL", request);
550 else if (request instanceof LcmRequestWrapper) {
551 PolicyEngine.manager.deliver("APPC-LCM-READ", request);
555 // at this point the AAI named query request should have already been made, the response recieved and used
556 // in the construction of the SO Request which is stored in operationRequest
558 if(request instanceof SORequest) {
559 // Call SO. The response will be inserted into memory once it's received
560 SOActorServiceProvider.sendRequest($event.getRequestId().toString(), drools.getWorkingMemory(), request);
564 if (request instanceof VFCRequest) {
566 Thread t = new Thread(new VFCManager(drools.getWorkingMemory(), (VFCRequest)request));
573 // What happens if its null?
575 logger.warn("{}: {}: unexpected null operation request",
576 $params.getClosedLoopControlName(),
577 drools.getRule().getName());
578 if ("SO".equals($operation.policy.getActor())) {
581 modify($manager) {finishOperation($operation)};
583 else if ("vfc".equalsIgnoreCase($operation.policy.getActor())) {
586 modify($manager) {finishOperation($operation)};
595 * We were able to acquire a lock so now let's ask Xacml Guard whether
596 * we are allowed to proceed with the request to the actor.
599 rule "${policyName}.EVENT.MANAGER.OPERATION.LOCKED.GUARD_NOT_YET_QUERIED"
601 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
602 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
603 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
604 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId(), getGuardApprovalStatus() == "NONE" )
605 $lock : TargetLock (requestID == $event.getRequestId())
608 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
609 logger.info("{}: {}: event={} manager={} operation={} lock={}",
610 $params.getClosedLoopControlName(), drools.getRule().getName(),
611 $event, $manager, $operation, $lock);
614 // Sending notification that we are about to query Guard ("DB write - start operation")
616 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
617 notification.setNotification(ControlLoopNotificationType.OPERATION);
618 notification.setMessage("Sending guard query for " + $operation.policy.getActor() + " " + $operation.policy.getRecipe());
619 notification.setHistory($operation.getHistory());
620 notification.setFrom("policy");
621 notification.setPolicyName(drools.getRule().getName());
622 notification.setPolicyScope("${policyScope}");
623 notification.setPolicyVersion("${policyVersion}");
625 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
628 // Now send Guard Request to XACML Guard. In order to bypass the call to Guard,
629 // just change guardEnabled to false.
631 // In order to use REST XACML, provide a URL instead of "" as a second argument
632 // to the CallGuardTask() and set the first argument to null
633 // (instead of XacmlPdpEngine).
636 // NOTE: The environment properties uses "guard.disabled" but the boolean is guardEnabled
637 boolean guardEnabled = "false".equalsIgnoreCase(PolicyEngine.manager.getEnvironmentProperty("guard.disabled"));
641 Thread t = new Thread(new org.onap.policy.guard.CallGuardTask(
642 drools.getWorkingMemory(),
643 $event.getClosedLoopControlName(),
644 $operation.policy.getActor().toString(),
645 $operation.policy.getRecipe(),
646 $operation.getTargetEntity(),
647 $event.getRequestId().toString()
652 insert(new PolicyGuardResponse("Permit", $event.getRequestId(), $operation.policy.getRecipe()));
658 // This rule will be triggered when a thread talking to the XACML Guard inserts a
659 // guardResponse object into the working memory
661 rule "${policyName}.GUARD.RESPONSE"
663 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
664 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
665 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
666 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() )
667 $lock : TargetLock (requestID == $event.getRequestId())
668 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
669 $guardResponse : PolicyGuardResponse(requestID == $event.getRequestId(), $operation.policy.recipe == operation)
672 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
673 logger.info("{}: {}: event={} manager={} operation={} lock={} opTimer={} guardResponse={}",
674 $params.getClosedLoopControlName(), drools.getRule().getName(),
675 $event, $manager, $operation, $lock, $opTimer, $guardResponse);
678 //we will permit the operation if there was no Guard for it
679 if("Indeterminate".equalsIgnoreCase($guardResponse.getResult())){
680 $guardResponse.setResult("Permit");
684 // This notification has Guard result in "message". ("DB write - end operation in case of Guard Deny")
686 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
687 notification.setNotification(ControlLoopNotificationType.OPERATION);
688 notification.setMessage("Guard result for " + $operation.policy.getActor() + " " + $operation.policy.getRecipe() + " is " + $guardResponse.getResult());
689 notification.setHistory($operation.getHistory());
690 notification.setFrom("policy");
691 notification.setPolicyName(drools.getRule().getName());
692 notification.setPolicyScope("${policyScope}");
693 notification.setPolicyVersion("${policyVersion}");
695 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
697 if("Permit".equalsIgnoreCase($guardResponse.getResult())){
699 modify($operation){setGuardApprovalStatus($guardResponse.getResult())};
702 //This is the Deny case
703 $operation.startOperation($event);
704 $operation.setOperationHasGuardDeny();
707 modify($manager) {finishOperation($operation)};
710 retract($guardResponse);
716 * This rule responds to APPC Response Events
718 * I would have like to be consistent and write the Response like this:
719 * $response : Response( CommonHeader.RequestId == $onset.getRequestId() )
721 * However, no compile error was given. But a runtime error was given. I think
722 * because drools is confused between the classname CommonHeader vs the property CommonHeader.
725 rule "${policyName}.APPC.RESPONSE"
727 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
728 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
729 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
730 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() )
731 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
732 $lock : TargetLock (requestID == $event.getRequestId())
733 $response : Response( getCommonHeader().RequestId == $event.getRequestId() )
736 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
737 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
738 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
739 $params.getClosedLoopControlName(), drools.getRule().getName(),
740 $event, $manager, $operation, $lock, $opTimer, $response);
742 // Get the result of the operation
744 PolicyResult policyResult = $operation.onResponse($response);
745 if (policyResult != null) {
746 logger.debug("{}: {}: operation finished - result={}",
747 $params.getClosedLoopControlName(), drools.getRule().getName(),
750 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
752 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
753 notification.setFrom("policy");
754 notification.setPolicyName(drools.getRule().getName());
755 notification.setPolicyScope("${policyScope}");
756 notification.setPolicyVersion("${policyVersion}");
757 notification.setMessage($operation.getOperationHistory());
758 notification.setHistory($operation.getHistory());
759 if (policyResult.equals(PolicyResult.SUCCESS)) {
760 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
762 // Let interested parties know
764 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
766 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
768 // Let interested parties know
770 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
773 // Ensure the operation is complete
775 if ($operation.isOperationComplete() == true) {
777 // It is complete, remove it from memory
781 // We must also retract the timer object
782 // NOTE: We could write a Rule to do this
786 // Complete the operation
788 modify($manager) {finishOperation($operation)};
791 // Just doing this will kick off the LOCKED rule again
793 modify($operation) {};
797 // Its not finished yet (i.e. expecting more Response objects)
799 // Or possibly it is a leftover response that we timed the request out previously
803 // We are going to retract these objects from memory
810 * The problem with Responses is that they don't have a controlLoopControlName
811 * field in them, so the only way to attach them is via RequestId. If we have multiple
812 * control loop .drl's loaded in the same container, we need to be sure the cleanup
813 * rules don't remove Responses for other control loops.
816 rule "${policyName}.APPC.RESPONSE.CLEANUP"
818 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
819 $response : Response($id : getCommonHeader().RequestId )
820 not ( VirtualControlLoopEvent( requestId == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
823 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
824 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
825 logger.debug("{}: {}: orphan appc response={}",
826 $params.getClosedLoopControlName(), drools.getRule().getName(), $id);
836 * This rule responds to APPC Response Events using the new LCM interface provided by appc
839 rule "${policyName}.APPC.LCM.RESPONSE"
841 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
842 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
843 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
844 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() )
845 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
846 $lock : TargetLock (requestID == $event.getRequestId())
847 $response : LcmResponseWrapper( getBody().getCommonHeader().getRequestId() == $event.getRequestId() )
850 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
851 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
852 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
853 $params.getClosedLoopControlName(), drools.getRule().getName(),
854 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
857 // Get the result of the operation
859 PolicyResult policyResult = $operation.onResponse($response);
860 if (policyResult != null) {
861 logger.debug("{}: {}: operation finished - result={}",
862 $params.getClosedLoopControlName(), drools.getRule().getName(),
866 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
868 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
869 notification.setFrom("policy");
870 notification.setPolicyName(drools.getRule().getName());
871 notification.setPolicyScope("${policyScope}");
872 notification.setPolicyVersion("${policyVersion}");
873 notification.setMessage($operation.getOperationHistory());
874 notification.setHistory($operation.getHistory());
875 if (policyResult.equals(PolicyResult.SUCCESS)) {
876 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
878 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
880 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
882 // Ensure the operation is complete
884 if ($operation.isOperationComplete() == true) {
886 // It is complete, remove it from memory
890 // We must also retract the timer object
891 // NOTE: We could write a Rule to do this
895 // Complete the operation
897 modify($manager) {finishOperation($operation)};
900 // Just doing this will kick off the LOCKED rule again
902 modify($operation) {};
906 // Its not finished yet (i.e. expecting more Response objects)
908 // Or possibly it is a leftover response that we timed the request out previously
912 // We are going to retract these objects from memory
919 * Clean Up any lingering LCM reponses
922 rule "${policyName}.APPC.LCM.RESPONSE.CLEANUP"
924 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
925 $response : LcmResponseWrapper($id : getBody().getCommonHeader().getRequestId )
926 not ( VirtualControlLoopEvent( requestId == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
929 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
930 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
931 logger.debug("{}: {}: orphan appc response={}",
932 $params.getClosedLoopControlName(), drools.getRule().getName(), $id);
941 * This rule responds to SO Response Events
944 rule "${policyName}.SO.RESPONSE"
946 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
947 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
948 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
949 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() )
950 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
951 $lock : TargetLock (requestID == $event.getRequestId())
952 $response : SOResponseWrapper(requestID.toString() == $event.getRequestId().toString() )
955 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
956 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
957 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
958 $params.getClosedLoopControlName(), drools.getRule().getName(),
959 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
961 // Get the result of the operation
963 PolicyResult policyResult = $operation.onResponse($response);
964 if (policyResult != null) {
965 logger.debug("{}: {}: operation finished - result={}",
966 $params.getClosedLoopControlName(), drools.getRule().getName(),
970 // This Operation has completed, construct a notification showing our results
972 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
973 notification.setFrom("policy");
974 notification.setPolicyName(drools.getRule().getName());
975 notification.setPolicyScope("${policyScope}");
976 notification.setPolicyVersion("${policyVersion}");
977 notification.setMessage($operation.getOperationHistory());
978 notification.setHistory($operation.getHistory());
979 if (policyResult.equals(PolicyResult.SUCCESS)) {
980 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
982 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
985 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
987 // Ensure the operation is complete
989 if ($operation.isOperationComplete() == true) {
991 // It is complete, remove it from memory
995 // We must also retract the timer object
996 // NOTE: We could write a Rule to do this
1000 // Complete the operation
1002 modify($manager) {finishOperation($operation)};
1005 // Just doing this will kick off the LOCKED rule again
1007 modify($operation) {};
1011 // Its not finished yet (i.e. expecting more Response objects)
1013 // Or possibly it is a leftover response that we timed the request out previously
1017 // We are going to retract these objects from memory
1025 * This rule responds to VFC Response Events
1028 rule "${policyName}.VFC.RESPONSE"
1030 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1031 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1032 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
1033 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() )
1034 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
1035 $lock : TargetLock (requestID == $event.getRequestId())
1036 $response : VFCResponse( requestId.toString() == $event.getRequestId().toString() )
1038 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1039 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1040 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
1041 $params.getClosedLoopControlName(), drools.getRule().getName(),
1042 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
1044 // Get the result of the operation
1046 PolicyResult policyResult = $operation.onResponse($response);
1047 if (policyResult != null) {
1049 // This Operation has completed, construct a notification showing our results
1051 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1052 notification.setFrom("policy");
1053 notification.setPolicyName(drools.getRule().getName());
1054 notification.setPolicyScope("${policyScope}");
1055 notification.setPolicyVersion("${policyVersion}");
1056 notification.setMessage($operation.getOperationHistory());
1057 notification.setHistory($operation.getHistory());
1059 // Ensure the operation is complete
1061 if ($operation.isOperationComplete() == true) {
1063 // It is complete, remove it from memory
1065 retract($operation);
1067 // We must also retract the timer object
1068 // NOTE: We could write a Rule to do this
1072 // Complete the operation
1074 modify($manager) {finishOperation($operation)};
1077 // Just doing this will kick off the LOCKED rule again
1079 modify($operation) {};
1083 // Its not finished yet (i.e. expecting more Response objects)
1085 // Or possibly it is a leftover response that we timed the request out previously
1089 // We are going to retract these objects from memory
1097 * This is the timer that manages the timeout for an individual operation.
1100 rule "${policyName}.EVENT.MANAGER.OPERATION.TIMEOUT"
1103 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1104 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
1105 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
1106 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() )
1107 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString(), $to : getDelay() )
1108 $lock : TargetLock (requestID == $event.getRequestId())
1111 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1112 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1113 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={}",
1114 $params.getClosedLoopControlName(), drools.getRule().getName(),
1115 $event, $manager, $operation, $lock, $operation, $opTimer);
1118 // Tell it its timed out
1120 $operation.setOperationHasTimedOut();
1122 // Create a notification for it ("DB Write - end operation")
1124 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1125 notification.setFrom("policy");
1126 notification.setPolicyName(drools.getRule().getName());
1127 notification.setPolicyScope("${policyScope}");
1128 notification.setPolicyVersion("${policyVersion}");
1129 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
1130 notification.setMessage($operation.getOperationHistory());
1131 notification.setHistory($operation.getHistory());
1133 // Let interested parties know
1135 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1137 // Get rid of the timer
1141 // Ensure the operation is complete
1143 if ($operation.isOperationComplete() == true) {
1145 // It is complete, remove it from memory
1147 retract($operation);
1149 // Complete the operation
1151 modify($manager) {finishOperation($operation)};
1154 // Just doing this will kick off the LOCKED rule again
1156 modify($operation) {};
1162 * This is the timer that manages the overall control loop timeout.
1165 rule "${policyName}.EVENT.MANAGER.TIMEOUT"
1168 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1169 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
1170 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
1171 $clTimer : ControlLoopTimer ( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString(), $to : getDelay() )
1172 $operations : LinkedList()
1173 from collect( ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() ) )
1174 $opTimers : LinkedList()
1175 from collect( OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() ) )
1176 $locks : LinkedList()
1177 from collect( TargetLock (requestID == $event.getRequestId()) )
1180 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1181 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1183 if ($operations == null) {
1184 logger.debug("{}: {}: event={} manager={} clTimer={} operations=0",
1185 $params.getClosedLoopControlName(), drools.getRule().getName(),
1186 $event, $manager, $clTimer);
1188 logger.debug("{}: {}: event={} manager={} clTimer={} operations={}",
1189 $params.getClosedLoopControlName(), drools.getRule().getName(),
1190 $event, $manager, $clTimer, $operations.size());
1193 // Tell the Event Manager it has timed out
1195 VirtualControlLoopNotification notification = $manager.setControlLoopTimedOut();
1196 if (notification != null) {
1197 notification.setFrom("policy");
1198 notification.setPolicyName(drools.getRule().getName());
1199 notification.setPolicyScope("${policyScope}");
1200 notification.setPolicyVersion("${policyVersion}");
1202 // Let interested parties know
1204 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1207 // Retract EVERYTHING
1212 if ($operations != null && $operations.size() > 0) {
1213 Iterator<ControlLoopOperationManager> iter = $operations.iterator();
1214 while (iter.hasNext()) {
1215 ControlLoopOperationManager manager = iter.next();
1219 if ($opTimers != null && $opTimers.size() > 0) {
1220 Iterator<OperationTimer> iter = $opTimers.iterator();
1221 while (iter.hasNext()) {
1222 OperationTimer opTimer = iter.next();
1226 if ($locks != null && $locks.size() > 0) {
1227 Iterator<TargetLock> iter = $locks.iterator();
1228 while (iter.hasNext()) {
1229 TargetLock lock = iter.next();
1231 // Ensure we release the lock
1233 PolicyGuard.unlockTarget(lock);