2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.guard.PolicyGuard;
58 import org.onap.policy.guard.PolicyGuard.LockResult;
59 import org.onap.policy.guard.TargetLock;
60 import org.onap.policy.guard.GuardResult;
61 import org.onap.policy.guard.PolicyGuardRequest;
62 import org.onap.policy.guard.PolicyGuardResponse;
63 import org.onap.policy.guard.PolicyGuardXacmlRequestAttributes;
64 import org.onap.policy.guard.PolicyGuardXacmlHelper;
66 import org.yaml.snakeyaml.Yaml;
67 import org.yaml.snakeyaml.constructor.Constructor;
69 import org.slf4j.LoggerFactory;
70 import org.slf4j.Logger;
72 import java.time.Instant;
73 import java.util.LinkedList;
74 import java.util.Iterator;
76 import org.onap.policy.drools.system.PolicyEngine;
79 * This structure mimics the Params structure.
80 * Its only purpose is to allow management of
81 * rules by the PAP component..
82 * It has no use at runtime since the rules go by
83 * Params for matching purposes.
86 closedLoopControlName : String
87 controlLoopYaml : String
91 * Control Loop Identity
94 closedLoopControlName : String
95 controlLoopYaml : String
102 declare OperationTimer
103 closedLoopControlName : String
111 declare ControlLoopTimer
112 closedLoopControlName : String
119 * Called once and only once to insert the parameters into working memory for this Closed Loop policy.
122 rule "${policyName}.SETUP"
126 Params params = new Params();
127 params.setClosedLoopControlName("${closedLoopControlName}");
128 params.setControlLoopYaml("${controlLoopYaml}");
131 // Note: globals have bad behavior when persistence is used,
132 // hence explicitly getting the logger vs using a global
134 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
135 logger.info("{}: {} : YAML=[{}]", params.getClosedLoopControlName(), drools.getRule().getName(), params.getControlLoopYaml());
137 String sqlDbUsername = PolicyEngine.manager.getEnvironmentProperty("sql.db.username");
139 String aaiUrl = PolicyEngine.manager.getEnvironmentProperty("aai.url");
140 String aaiUsername = PolicyEngine.manager.getEnvironmentProperty("aai.username");
141 String aaiPassword = PolicyEngine.manager.getEnvironmentProperty("aai.password");
143 String soUrl =PolicyEngine.manager.getEnvironmentProperty("so.url");
144 String soUsername = PolicyEngine.manager.getEnvironmentProperty("so.username");
145 String soPassword = PolicyEngine.manager.getEnvironmentProperty("so.password");
147 String vfcUrl =PolicyEngine.manager.getEnvironmentProperty("vfc.url");
148 String vfcUsername = PolicyEngine.manager.getEnvironmentProperty("vfc.username");
149 String vfcPassword = PolicyEngine.manager.getEnvironmentProperty("vfc.password");
151 String guardUrl = PolicyEngine.manager.getEnvironmentProperty("guard.url");
152 String guardUsername = PolicyEngine.manager.getEnvironmentProperty("pdpx.username");
153 String guardPassword = PolicyEngine.manager.getEnvironmentProperty("pdpx.password");
154 String guardJdbcUrl = PolicyEngine.manager.getEnvironmentProperty("guard.jdbc.url");
155 String guardDisabled = PolicyEngine.manager.getEnvironmentProperty("guard.disabled");
157 logger.info("{}: {} : AAI=[{}:{}]", params.getClosedLoopControlName(), drools.getRule().getName(), aaiUrl, aaiUsername);
158 logger.info("{}: {} : SO=[{}:{}]", params.getClosedLoopControlName(), drools.getRule().getName(), soUrl, soUsername);
159 logger.info("{}: {} : VFC=[{}:{}]", params.getClosedLoopControlName(), drools.getRule().getName(), vfcUrl, vfcUsername);
160 logger.info("{}: {} : GUARD=[{}:{}:{}:{}]", params.getClosedLoopControlName(), drools.getRule().getName(), guardUrl, guardUsername, guardJdbcUrl, guardDisabled);
161 logger.info("{}: {} : DB=[{}]", params.getClosedLoopControlName(), drools.getRule().getName(), sqlDbUsername);
166 * This rule responds to DCAE Events where there is no manager yet. Either it is
167 * the first ONSET, or a subsequent badly formed Event (i.e. Syntax error, or is-closed-loop-disabled)
170 rule "${policyName}.EVENT"
172 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
173 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
174 not ( ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID ) )
177 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
178 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
183 // Check the event, because we need it to not be null when
184 // we create the ControlLoopEventManager. The ControlLoopEventManager
185 // will do extra syntax checking as well check if the closed loop is disabled.
187 if ($event.requestID == null) {
188 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
189 notification.notification = ControlLoopNotificationType.REJECTED;
190 notification.from = "policy";
191 notification.message = "Missing requestID";
192 notification.policyName = drools.getRule().getName();
193 notification.policyScope = "${policyScope}";
194 notification.policyVersion = "${policyVersion}";
197 // Let interested parties know
199 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
202 // Retract it from memory
207 // Create an EventManager
209 ControlLoopEventManager manager = new ControlLoopEventManager($params.getClosedLoopControlName(), $event.requestID);
211 // Determine if EventManager can actively process the event (i.e. syntax, is_closed_loop_disabled checks etc.)
213 VirtualControlLoopNotification notification = manager.activate($params.getControlLoopYaml(), $event);
214 notification.from = "pdp-0001-controller=controlloop"; // Engine.getInstanceName()
215 notification.policyName = drools.getRule().getName();
216 notification.policyScope = "${policyScope}";
217 notification.policyVersion = "${policyVersion}";
219 // Are we actively pursuing this event?
221 if (notification.notification == ControlLoopNotificationType.ACTIVE) {
223 // Insert Event Manager into memory, this will now kick off processing.
227 // Let interested parties know
229 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
231 // Setup the Overall Control Loop timer
233 ControlLoopTimer clTimer = new ControlLoopTimer();
234 clTimer.setClosedLoopControlName($event.closedLoopControlName);
235 clTimer.setRequestID($event.requestID.toString());
236 clTimer.setDelay(manager.getControlLoopTimeout(1500) + "s");
243 // Let interested parties know
245 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
247 // Retract it from memory
253 // Now that the manager is inserted into Drools working memory, we'll wait for
254 // another rule to fire in order to continue processing. This way we can also
255 // then screen for additional ONSET and ABATED events for this RequestID.
258 } catch (Exception e) {
259 logger.warn("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName(), e);
261 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
262 notification.notification = ControlLoopNotificationType.REJECTED;
263 notification.message = "Exception occurred " + e.getMessage();
264 notification.policyName = drools.getRule().getName();
265 notification.policyScope = "${policyScope}";
266 notification.policyVersion = "${policyVersion}";
270 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
280 * This rule happens when we got a valid ONSET, closed loop is enabled and an Event Manager
281 * is now created. We can start processing the yaml specification via the Event Manager.
284 rule "${policyName}.EVENT.MANAGER"
286 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
287 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
288 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
289 $clTimer : ControlLoopTimer ( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
292 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
293 logger.info("{}: {}: event={} manager={} clTimer={}",
294 $params.getClosedLoopControlName(), drools.getRule().getName(),
295 $event, $manager, $clTimer);
297 // Check which event this is.
299 ControlLoopEventManager.NEW_EVENT_STATUS eventStatus = $manager.onNewEvent($event);
302 // Check what kind of event this is
304 if (eventStatus == NEW_EVENT_STATUS.SUBSEQUENT_ONSET) {
306 // We don't care about subsequent onsets
308 logger.info("{}: {}: subsequent onset",
309 $params.getClosedLoopControlName(), drools.getRule().getName());
313 if (eventStatus == NEW_EVENT_STATUS.SYNTAX_ERROR) {
315 // Ignore any bad syntax events
317 logger.warn("{}: {}: syntax error",
318 $params.getClosedLoopControlName(), drools.getRule().getName());
323 // We only want the initial ONSET event in memory,
324 // all the other events need to be retracted to support
325 // cleanup and avoid the other rules being fired for this event.
327 if (eventStatus != NEW_EVENT_STATUS.FIRST_ONSET) {
328 logger.warn("{}: {}: no first onset",
329 $params.getClosedLoopControlName(), drools.getRule().getName());
333 logger.debug("{}: {}: target={}", $params.getClosedLoopControlName(),
334 drools.getRule().getName(), $event.target);
336 // Now start seeing if we need to process this event
340 // Check if this is a Final Event
342 VirtualControlLoopNotification notification = $manager.isControlLoopFinal();
345 if (notification != null) {
347 // Its final, but are we waiting for abatement?
349 if ($manager.getNumAbatements() > 0) {
350 logger.info("{}: {}: abatement received for {}. Closing the control loop",
351 $params.getClosedLoopControlName(), drools.getRule().getName(),
353 notification.from = "policy";
354 notification.policyName = drools.getRule().getName();
355 notification.policyScope = "${policyScope}";
356 notification.policyVersion = "${policyVersion}";
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=", $params.getClosedLoopControlName(),
367 drools.getRule().getName(), lock);
371 // Retract everything from memory
373 logger.info("{}: {}: retracting onset, manager, and timer",
374 $params.getClosedLoopControlName(), drools.getRule().getName());
376 retract($manager.getOnsetEvent());
380 // TODO - what if we get subsequent Events for this RequestID?
381 // By default, it will all start over again. May be confusing for Ruby.
382 // Or, we could track this and then subsequently ignore the events
386 // Check whether we need to wait for abatement
388 if ($manager.getProcessor().getControlLoop().getAbatement() == true && notification.notification == ControlLoopNotificationType.FINAL_SUCCESS) {
389 logger.info("{}: {}: waiting for abatement ..",
390 $params.getClosedLoopControlName(), drools.getRule().getName());
392 logger.info("{}: {}: no abatement expect for {}. Closing the control loop",
393 $params.getClosedLoopControlName(), drools.getRule().getName(),
396 notification.from = "policy";
397 notification.policyName = drools.getRule().getName();
398 notification.policyScope = "${policyScope}";
399 notification.policyVersion = "${policyVersion}";
402 // In this case, we are done
404 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
408 TargetLock lock = $manager.unlockCurrentOperation();
410 logger.debug("{}: {}: retracting lock=", $params.getClosedLoopControlName(),
411 drools.getRule().getName(), lock);
415 // Retract everything from memory
417 logger.info("{}: {}: retracting onset, manager, and timer",
418 $params.getClosedLoopControlName(), drools.getRule().getName());
420 retract($manager.getOnsetEvent());
427 // NOT final, so let's ask for the next operation
429 ControlLoopOperationManager operation = $manager.processControlLoop();
430 if (operation != null) {
432 // Let's ask for a lock right away
434 LockResult<GuardResult, TargetLock> result = $manager.lockCurrentOperation();
435 logger.info("{}: {}: guard lock acquired={}",
436 $params.getClosedLoopControlName(), drools.getRule().getName(),
438 if (result.getA().equals(GuardResult.LOCK_ACQUIRED)) {
440 // insert the operation into memory
445 // insert operation timeout object
447 OperationTimer opTimer = new OperationTimer();
448 opTimer.setClosedLoopControlName($event.closedLoopControlName);
449 opTimer.setRequestID($event.requestID.toString());
450 opTimer.setDelay(operation.getOperationTimeout().toString() + "s");
454 // Insert lock into memory
456 insert(result.getB());
459 logger.debug("The target resource {} is already processing",
460 $event.AAI.get($event.target));
461 notification = new VirtualControlLoopNotification($event);
462 notification.notification = ControlLoopNotificationType.REJECTED;
463 notification.message = "The target " + $event.AAI.get($event.target) + " is already locked";
464 notification.from = "policy";
465 notification.policyName = drools.getRule().getName();
466 notification.policyScope = "${policyScope}";
467 notification.policyVersion = "${policyVersion}";
469 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
475 logger.info("{}: {}: starting operation={}",
476 $params.getClosedLoopControlName(), drools.getRule().getName(),
480 // Probably waiting for abatement
482 logger.info("{}: {}: no operation, probably waiting for abatement",
483 $params.getClosedLoopControlName(), drools.getRule().getName());
486 } catch (Exception e) {
487 logger.warn("{}: {}: unexpected",
488 $params.getClosedLoopControlName(),
489 drools.getRule().getName(), e);
492 // TODO should we abort if we get an exception?
500 * Guard Permitted, let's send request to the actor.
503 rule "${policyName}.EVENT.MANAGER.OPERATION.LOCKED.GUARD_PERMITTED"
505 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
506 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
507 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
508 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID, "Permit".equalsIgnoreCase(getGuardApprovalStatus()) )
509 $lock : TargetLock (requestID == $event.requestID)
512 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
513 logger.info("{}: {}: event={} manager={} operation={} lock={}",
514 $params.getClosedLoopControlName(), drools.getRule().getName(),
515 $event, $manager, $operation, $lock);
517 Object request = $operation.startOperation($event);
519 if (request != null) {
520 logger.debug("{}: {}: starting operation ..",
521 $params.getClosedLoopControlName(), drools.getRule().getName());
523 // Tell interested parties we are performing this Operation
525 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
526 notification.notification = ControlLoopNotificationType.OPERATION;
527 notification.message = $operation.getOperationMessage();
528 notification.history = $operation.getHistory();
529 notification.from = "policy";
530 notification.policyName = drools.getRule().getName();
531 notification.policyScope = "${policyScope}";
532 notification.policyVersion = "${policyVersion}";
534 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
536 switch ($operation.policy.getActor()){
540 if (request instanceof Request) {
541 PolicyEngine.manager.deliver("APPC-CL", request);
543 else if (request instanceof LCMRequestWrapper) {
544 PolicyEngine.manager.deliver("APPC-LCM-READ", request);
548 // at this point the AAI named query request should have already been made, the response recieved and used
549 // in the construction of the SO Request which is stored in operationRequest
551 if(request instanceof SORequest) {
552 // Call SO. The response will be inserted into memory once it's received
553 SOActorServiceProvider.sendRequest(drools.getWorkingMemory(), request);
557 if (request instanceof VFCRequest) {
559 Thread t = new Thread(new VFCManager(drools.getWorkingMemory(), (VFCRequest)request));
566 // What happens if its null?
568 logger.warn("{}: {}: unexpected null operation request",
569 $params.getClosedLoopControlName(),
570 drools.getRule().getName());
571 if ("SO".equals($operation.policy.getActor())) {
573 modify($manager) {finishOperation($operation)};
582 * We were able to acquire a lock so now let's ask Xacml Guard whether
583 * we are allowed to proceed with the request to the actor.
586 rule "${policyName}.EVENT.MANAGER.OPERATION.LOCKED.GUARD_NOT_YET_QUERIED"
588 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
589 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
590 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
591 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID, getGuardApprovalStatus() == "NONE" )
592 $lock : TargetLock (requestID == $event.requestID)
595 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
596 logger.info("{}: {}: event={} manager={} operation={} lock={}",
597 $params.getClosedLoopControlName(), drools.getRule().getName(),
598 $event, $manager, $operation, $lock);
601 // Sending notification that we are about to query Guard ("DB write - start operation")
603 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
604 notification.notification = ControlLoopNotificationType.OPERATION;
605 notification.message = "Sending guard query for " + $operation.policy.getActor() + " " + $operation.policy.getRecipe();
606 notification.history = $operation.getHistory();
607 notification.from = "policy";
608 notification.policyName = drools.getRule().getName();
609 notification.policyScope = "${policyScope}";
610 notification.policyVersion = "${policyVersion}";
612 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
615 // Now send Guard Request to XACML Guard. In order to bypass the call to Guard,
616 // just change guardEnabled to false.
618 // In order to use REST XACML, provide a URL instead of "" as a second argument
619 // to the CallGuardTask() and set the first argument to null
620 // (instead of XacmlPdpEngine).
623 // NOTE: The environment properties uses "guard.disabled" but the boolean is guardEnabled
624 boolean guardEnabled = "false".equalsIgnoreCase(PolicyEngine.manager.getEnvironmentProperty("guard.disabled"));
628 Thread t = new Thread(new org.onap.policy.guard.CallGuardTask(
629 drools.getWorkingMemory(),
630 $event.closedLoopControlName,
631 $operation.policy.getActor().toString(),
632 $operation.policy.getRecipe(),
633 $manager.getTargetInstance($operation.policy),
635 $event.requestID.toString()
640 insert(new PolicyGuardResponse("Permit", $event.requestID, $operation.policy.getRecipe()));
646 // This rule will be triggered when a thread talking to the XACML Guard inserts a
647 // guardResponse object into the working memory
649 rule "${policyName}.GUARD.RESPONSE"
651 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
652 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
653 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
654 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
655 $lock : TargetLock (requestID == $event.requestID)
656 $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
657 $guardResponse : PolicyGuardResponse(requestID == $event.requestID, $operation.policy.recipe == operation)
660 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
661 logger.info("{}: {}: event={} manager={} operation={} lock={} opTimer={} guardResponse={}",
662 $params.getClosedLoopControlName(), drools.getRule().getName(),
663 $event, $manager, $operation, $lock, $opTimer, $guardResponse);
666 //we will permit the operation if there was no Guard for it
667 if("Indeterminate".equalsIgnoreCase($guardResponse.result)){
668 $guardResponse.result = "Permit";
672 // This notification has Guard result in "message". ("DB write - end operation in case of Guard Deny")
674 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
675 notification.notification = ControlLoopNotificationType.OPERATION;
676 notification.message = "Guard result for " + $operation.policy.getActor() + " " + $operation.policy.getRecipe() + " is " + $guardResponse.result;
677 notification.history = $operation.getHistory();
678 notification.from = "policy";
679 notification.policyName = drools.getRule().getName();
680 notification.policyScope = "${policyScope}";
681 notification.policyVersion = "${policyVersion}";
683 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
685 if("Permit".equalsIgnoreCase($guardResponse.result)){
687 modify($operation){setGuardApprovalStatus($guardResponse.result)};
690 //This is the Deny case
691 $operation.setOperationHasGuardDeny();
694 modify($manager) {finishOperation($operation)};
697 retract($guardResponse);
703 * This rule responds to APPC Response Events
705 * I would have like to be consistent and write the Response like this:
706 * $response : Response( CommonHeader.RequestID == $onset.requestID )
708 * However, no compile error was given. But a runtime error was given. I think
709 * because drools is confused between the classname CommonHeader vs the property CommonHeader.
712 rule "${policyName}.APPC.RESPONSE"
714 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
715 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
716 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
717 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
718 $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
719 $lock : TargetLock (requestID == $event.requestID)
720 $response : Response( getCommonHeader().RequestID == $event.requestID )
723 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
724 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
725 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
726 $params.getClosedLoopControlName(), drools.getRule().getName(),
727 $event, $manager, $operation, $lock, $opTimer, $response);
729 // Get the result of the operation
731 PolicyResult policyResult = $operation.onResponse($response);
732 if (policyResult != null) {
733 logger.debug("{}: {}: operation finished - result={}",
734 $params.getClosedLoopControlName(), drools.getRule().getName(),
737 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
739 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
740 notification.from = "policy";
741 notification.policyName = drools.getRule().getName();
742 notification.policyScope = "${policyScope}";
743 notification.policyVersion = "${policyVersion}";
744 notification.message = $operation.getOperationHistory();
745 notification.history = $operation.getHistory();
746 if (policyResult.equals(PolicyResult.SUCCESS)) {
747 notification.notification = ControlLoopNotificationType.OPERATION_SUCCESS;
749 // Let interested parties know
751 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
753 notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
755 // Let interested parties know
757 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
760 // Ensure the operation is complete
762 if ($operation.isOperationComplete() == true) {
764 // It is complete, remove it from memory
768 // We must also retract the timer object
769 // NOTE: We could write a Rule to do this
773 // Complete the operation
775 modify($manager) {finishOperation($operation)};
778 // Just doing this will kick off the LOCKED rule again
780 modify($operation) {};
784 // Its not finished yet (i.e. expecting more Response objects)
786 // Or possibly it is a leftover response that we timed the request out previously
790 // We are going to retract these objects from memory
797 * The problem with Responses is that they don't have a controlLoopControlName
798 * field in them, so the only way to attach them is via RequestID. If we have multiple
799 * control loop .drl's loaded in the same container, we need to be sure the cleanup
800 * rules don't remove Responses for other control loops.
803 rule "${policyName}.APPC.RESPONSE.CLEANUP"
805 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
806 $response : Response($id : getCommonHeader().RequestID )
807 not ( VirtualControlLoopEvent( requestID == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
810 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
811 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
812 logger.debug("{}: {}: orphan appc response={}",
813 $params.getClosedLoopControlName(), drools.getRule().getName(), $id);
823 * This rule responds to APPC Response Events using the new LCM interface provided by appc
826 rule "${policyName}.APPC.LCM.RESPONSE"
828 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
829 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
830 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
831 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
832 $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
833 $lock : TargetLock (requestID == $event.requestID)
834 $response : LCMResponseWrapper( getBody().getCommonHeader().getRequestId() == $event.requestID )
837 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
838 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
839 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
840 $params.getClosedLoopControlName(), drools.getRule().getName(),
841 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
844 // Get the result of the operation
846 PolicyResult policyResult = $operation.onResponse($response);
847 if (policyResult != null) {
848 logger.debug("{}: {}: operation finished - result={}",
849 $params.getClosedLoopControlName(), drools.getRule().getName(),
853 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
855 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
856 notification.from = "policy";
857 notification.policyName = drools.getRule().getName();
858 notification.policyScope = "${policyScope}";
859 notification.policyVersion = "${policyVersion}";
860 notification.message = $operation.getOperationHistory();
861 notification.history = $operation.getHistory();
862 if (policyResult.equals(PolicyResult.SUCCESS)) {
863 notification.notification = ControlLoopNotificationType.OPERATION_SUCCESS;
865 notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
867 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
869 // Ensure the operation is complete
871 if ($operation.isOperationComplete() == true) {
873 // It is complete, remove it from memory
877 // We must also retract the timer object
878 // NOTE: We could write a Rule to do this
882 // Complete the operation
884 modify($manager) {finishOperation($operation)};
887 // Just doing this will kick off the LOCKED rule again
889 modify($operation) {};
893 // Its not finished yet (i.e. expecting more Response objects)
895 // Or possibly it is a leftover response that we timed the request out previously
899 // We are going to retract these objects from memory
906 * Clean Up any lingering LCM reponses
909 rule "${policyName}.APPC.LCM.RESPONSE.CLEANUP"
911 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
912 $response : LCMResponseWrapper($id : getBody().getCommonHeader().getRequestId )
913 not ( VirtualControlLoopEvent( requestID == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
916 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
917 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
918 logger.debug("{}: {}: orphan appc response={}",
919 $params.getClosedLoopControlName(), drools.getRule().getName(), $id);
928 * This rule responds to SO Response Events
931 rule "${policyName}.SO.RESPONSE"
933 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
934 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
935 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName )
936 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
937 $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
938 $lock : TargetLock (requestID == $event.requestID)
939 $response : SOResponse( requestReferences.requestId.toString() == $event.requestID.toString() )
942 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
943 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
944 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
945 $params.getClosedLoopControlName(), drools.getRule().getName(),
946 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
948 // Get the result of the operation
950 PolicyResult policyResult = $operation.onResponse($response);
951 if (policyResult != null) {
952 logger.debug("{}: {}: operation finished - result={}",
953 $params.getClosedLoopControlName(), drools.getRule().getName(),
957 // This Operation has completed, construct a notification showing our results
959 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
960 notification.from = "policy";
961 notification.policyName = drools.getRule().getName();
962 notification.policyScope = "${policyScope}";
963 notification.policyVersion = "${policyVersion}";
964 notification.message = $operation.getOperationHistory();
965 notification.history = $operation.getHistory();
966 if (policyResult.equals(PolicyResult.SUCCESS)) {
967 notification.notification = ControlLoopNotificationType.OPERATION_SUCCESS;
969 notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
972 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
974 // Ensure the operation is complete
976 if ($operation.isOperationComplete() == true) {
978 // It is complete, remove it from memory
982 // We must also retract the timer object
983 // NOTE: We could write a Rule to do this
987 // Complete the operation
989 modify($manager) {finishOperation($operation)};
992 // Just doing this will kick off the LOCKED rule again
994 modify($operation) {};
998 // Its not finished yet (i.e. expecting more Response objects)
1000 // Or possibly it is a leftover response that we timed the request out previously
1004 // We are going to retract these objects from memory
1012 * This rule responds to VFC Response Events
1015 rule "${policyName}.VFC.RESPONSE"
1017 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1018 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1019 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName )
1020 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
1021 $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
1022 $lock : TargetLock (requestID == $event.requestID)
1023 $response : VFCResponse( requestId.toString() == $event.requestID.toString() )
1025 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1026 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1027 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
1028 $params.getClosedLoopControlName(), drools.getRule().getName(),
1029 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
1031 // Get the result of the operation
1033 PolicyResult policyResult = $operation.onResponse($response);
1034 if (policyResult != null) {
1036 // This Operation has completed, construct a notification showing our results
1038 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1039 notification.from = "policy";
1040 notification.policyName = drools.getRule().getName();
1041 notification.policyScope = "${policyScope}";
1042 notification.policyVersion = "${policyVersion}";
1043 notification.message = $operation.getOperationHistory();
1044 notification.history = $operation.getHistory();
1046 // Ensure the operation is complete
1048 if ($operation.isOperationComplete() == true) {
1050 // It is complete, remove it from memory
1052 retract($operation);
1054 // We must also retract the timer object
1055 // NOTE: We could write a Rule to do this
1059 // Complete the operation
1061 modify($manager) {finishOperation($operation)};
1064 // Just doing this will kick off the LOCKED rule again
1066 modify($operation) {};
1070 // Its not finished yet (i.e. expecting more Response objects)
1072 // Or possibly it is a leftover response that we timed the request out previously
1076 // We are going to retract these objects from memory
1084 * This is the timer that manages the timeout for an individual operation.
1087 rule "${policyName}.EVENT.MANAGER.OPERATION.TIMEOUT"
1090 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1091 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
1092 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
1093 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
1094 $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString(), $to : getDelay() )
1095 $lock : TargetLock (requestID == $event.requestID)
1098 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1099 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1100 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={}",
1101 $params.getClosedLoopControlName(), drools.getRule().getName(),
1102 $event, $manager, $operation, $lock, $operation, $opTimer);
1105 // Tell it its timed out
1107 $operation.setOperationHasTimedOut();
1109 // Create a notification for it ("DB Write - end operation")
1111 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1112 notification.from = "policy";
1113 notification.policyName = drools.getRule().getName();
1114 notification.policyScope = "${policyScope}";
1115 notification.policyVersion = "${policyVersion}";
1116 notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
1117 notification.message = $operation.getOperationHistory();
1118 notification.history = $operation.getHistory();
1120 // Let interested parties know
1122 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1124 // Get rid of the timer
1128 // Ensure the operation is complete
1130 if ($operation.isOperationComplete() == true) {
1132 // It is complete, remove it from memory
1134 retract($operation);
1136 // Complete the operation
1138 modify($manager) {finishOperation($operation)};
1141 // Just doing this will kick off the LOCKED rule again
1143 modify($operation) {};
1149 * This is the timer that manages the overall control loop timeout.
1152 rule "${policyName}.EVENT.MANAGER.TIMEOUT"
1155 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1156 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
1157 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
1158 $clTimer : ControlLoopTimer ( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString(), $to : getDelay() )
1159 $operations : LinkedList()
1160 from collect( ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID ) )
1161 $opTimers : LinkedList()
1162 from collect( OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() ) )
1163 $locks : LinkedList()
1164 from collect( TargetLock (requestID == $event.requestID) )
1167 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1168 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1170 if ($operations == null) {
1171 logger.debug("{}: {}: event={} manager={} clTimer={} operations=0",
1172 $params.getClosedLoopControlName(), drools.getRule().getName(),
1173 $event, $manager, $clTimer);
1175 logger.debug("{}: {}: event={} manager={} clTimer={} operations={}",
1176 $params.getClosedLoopControlName(), drools.getRule().getName(),
1177 $event, $manager, $clTimer, $operations.size());
1180 // Tell the Event Manager it has timed out
1182 VirtualControlLoopNotification notification = $manager.setControlLoopTimedOut();
1183 if (notification != null) {
1184 notification.from = "policy";
1185 notification.policyName = drools.getRule().getName();
1186 notification.policyScope = "${policyScope}";
1187 notification.policyVersion = "${policyVersion}";
1189 // Let interested parties know
1191 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1194 // Retract EVERYTHING
1199 if ($operations != null && $operations.size() > 0) {
1200 Iterator<ControlLoopOperationManager> iter = $operations.iterator();
1201 while (iter.hasNext()) {
1202 ControlLoopOperationManager manager = iter.next();
1206 if ($opTimers != null && $opTimers.size() > 0) {
1207 Iterator<OperationTimer> iter = $opTimers.iterator();
1208 while (iter.hasNext()) {
1209 OperationTimer opTimer = iter.next();
1213 if ($locks != null && $locks.size() > 0) {
1214 Iterator<TargetLock> iter = $locks.iterator();
1215 while (iter.hasNext()) {
1216 TargetLock lock = iter.next();
1218 // Ensure we release the lock
1220 PolicyGuard.unlockTarget(lock);