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 closedLoopControlName : String
80 controlLoopYaml : String
84 declare OperationTimer
85 closedLoopControlName : String
90 declare ControlLoopTimer
91 closedLoopControlName : String
98 * Called once and only once to insert the parameters into working memory for this Closed Loop policy.
101 rule "${policyName}.SETUP"
105 Params params = new Params();
106 params.setClosedLoopControlName("${closedLoopControlName}");
107 params.setControlLoopYaml("${controlLoopYaml}");
110 // Note: globals have bad behavior when persistence is used,
111 // hence explicitly getting the logger vs using a global
113 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
114 logger.info("{}: {} : YAML=[{}]", params.getClosedLoopControlName(), drools.getRule().getName(), params.getControlLoopYaml());
116 String sqlDbUsername = PolicyEngine.manager.getEnvironmentProperty("sql.db.username");
117 String sqlDbPassword = PolicyEngine.manager.getEnvironmentProperty("sql.db.password");
119 String aaiUrl = PolicyEngine.manager.getEnvironmentProperty("aai.url");
120 String aaiUsername = PolicyEngine.manager.getEnvironmentProperty("aai.username");
121 String aaiPassword = PolicyEngine.manager.getEnvironmentProperty("aai.password");
123 String soUrl =PolicyEngine.manager.getEnvironmentProperty("so.url");
124 String soUsername = PolicyEngine.manager.getEnvironmentProperty("so.username");
125 String soPassword = PolicyEngine.manager.getEnvironmentProperty("so.password");
127 String vfcUrl =PolicyEngine.manager.getEnvironmentProperty("vfc.url");
128 String vfcUsername = PolicyEngine.manager.getEnvironmentProperty("vfc.username");
129 String vfcPassword = PolicyEngine.manager.getEnvironmentProperty("vfc.password");
131 String guardUrl = PolicyEngine.manager.getEnvironmentProperty("guard.url");
132 String guardUsername = PolicyEngine.manager.getEnvironmentProperty("guard.username");
133 String guardPassword = PolicyEngine.manager.getEnvironmentProperty("guard.password");
134 String guardJdbcUrl = PolicyEngine.manager.getEnvironmentProperty("guard.jdbc.url");
136 logger.info("{}: {} : AAI=[{}:{}]", params.getClosedLoopControlName(), drools.getRule().getName(), aaiUrl, aaiUsername);
137 logger.info("{}: {} : SO=[{}:{}]", params.getClosedLoopControlName(), drools.getRule().getName(), soUrl, soUsername);
138 logger.info("{}: {} : VFC=[{}:{}]", params.getClosedLoopControlName(), drools.getRule().getName(), vfcUrl, vfcUsername);
139 logger.info("{}: {} : GUARD=[{}:{}:{}]", params.getClosedLoopControlName(), drools.getRule().getName(), guardUrl, guardUsername, guardJdbcUrl);
140 logger.info("{}: {} : DB=[{}:{}]", params.getClosedLoopControlName(), drools.getRule().getName(), sqlDbUsername, sqlDbPassword);
145 * This rule responds to DCAE Events where there is no manager yet. Either it is
146 * the first ONSET, or a subsequent badly formed Event (i.e. Syntax error, or is-closed-loop-disabled)
149 rule "${policyName}.EVENT"
151 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
152 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
153 not ( ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID ) )
156 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
157 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
162 // Check the event, because we need it to not be null when
163 // we create the ControlLoopEventManager. The ControlLoopEventManager
164 // will do extra syntax checking as well check if the closed loop is disabled.
166 if ($event.requestID == null) {
167 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
168 notification.notification = ControlLoopNotificationType.REJECTED;
169 notification.from = "policy";
170 notification.message = "Missing requestID";
171 notification.policyName = drools.getRule().getName();
172 notification.policyScope = "${policyScope}";
173 notification.policyVersion = "${policyVersion}";
176 // Let interested parties know
178 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
181 // Retract it from memory
186 // Create an EventManager
188 ControlLoopEventManager manager = new ControlLoopEventManager($params.getClosedLoopControlName(), $event.requestID);
190 // Determine if EventManager can actively process the event (i.e. syntax, is_closed_loop_disabled checks etc.)
192 VirtualControlLoopNotification notification = manager.activate($params.getControlLoopYaml(), $event);
193 notification.from = "pdp-0001-controller=controlloop"; // Engine.getInstanceName()
194 notification.policyName = drools.getRule().getName();
195 notification.policyScope = "${policyScope}";
196 notification.policyVersion = "${policyVersion}";
198 // Are we actively pursuing this event?
200 if (notification.notification == ControlLoopNotificationType.ACTIVE) {
202 // Insert Event Manager into memory, this will now kick off processing.
206 // Let interested parties know
208 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
210 // Setup the Overall Control Loop timer
212 ControlLoopTimer clTimer = new ControlLoopTimer();
213 clTimer.setClosedLoopControlName($event.closedLoopControlName);
214 clTimer.setRequestID($event.requestID.toString());
215 clTimer.setDelay(manager.getControlLoopTimeout(1500) + "s");
222 // Let interested parties know
224 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
226 // Retract it from memory
232 // Now that the manager is inserted into Drools working memory, we'll wait for
233 // another rule to fire in order to continue processing. This way we can also
234 // then screen for additional ONSET and ABATED events for this RequestID.
237 } catch (Exception e) {
238 logger.warn("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName(), e);
240 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
241 notification.notification = ControlLoopNotificationType.REJECTED;
242 notification.message = "Exception occurred " + e.getMessage();
243 notification.policyName = drools.getRule().getName();
244 notification.policyScope = "${policyScope}";
245 notification.policyVersion = "${policyVersion}";
249 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
259 * This rule happens when we got a valid ONSET, closed loop is enabled and an Event Manager
260 * is now created. We can start processing the yaml specification via the Event Manager.
263 rule "${policyName}.EVENT.MANAGER"
265 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
266 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
267 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
268 $clTimer : ControlLoopTimer ( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
271 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
272 logger.info("{}: {}: event={} manager={} clTimer={}",
273 $params.getClosedLoopControlName(), drools.getRule().getName(),
274 $event, $manager, $clTimer);
276 // Check which event this is.
278 ControlLoopEventManager.NEW_EVENT_STATUS eventStatus = $manager.onNewEvent($event);
281 // Check what kind of event this is
283 if (eventStatus == NEW_EVENT_STATUS.SUBSEQUENT_ONSET) {
285 // We don't care about subsequent onsets
287 logger.info("{}: {}: subsequent onset",
288 $params.getClosedLoopControlName(), drools.getRule().getName());
292 if (eventStatus == NEW_EVENT_STATUS.SYNTAX_ERROR) {
294 // Ignore any bad syntax events
296 logger.warn("{}: {}: syntax error",
297 $params.getClosedLoopControlName(), drools.getRule().getName());
302 // We only want the initial ONSET event in memory,
303 // all the other events need to be retracted to support
304 // cleanup and avoid the other rules being fired for this event.
306 if (eventStatus != NEW_EVENT_STATUS.FIRST_ONSET) {
307 logger.warn("{}: {}: no first onset",
308 $params.getClosedLoopControlName(), drools.getRule().getName());
312 logger.debug("{}: {}: target={}", $params.getClosedLoopControlName(),
313 drools.getRule().getName(), $event.target);
315 // Now start seeing if we need to process this event
319 // Check if this is a Final Event
321 VirtualControlLoopNotification notification = $manager.isControlLoopFinal();
324 if (notification != null) {
326 // Its final, but are we waiting for abatement?
328 if ($manager.getNumAbatements() > 0) {
329 logger.info("{}: {}: abatement received for {}. Closing the control loop",
330 $params.getClosedLoopControlName(), drools.getRule().getName(),
332 notification.from = "policy";
333 notification.policyName = drools.getRule().getName();
334 notification.policyScope = "${policyScope}";
335 notification.policyVersion = "${policyVersion}";
337 // In this case, we are done
339 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
343 TargetLock lock = $manager.unlockCurrentOperation();
345 logger.debug("{}: {}: retracting lock=", $params.getClosedLoopControlName(),
346 drools.getRule().getName(), lock);
350 // Retract everything from memory
352 logger.info("{}: {}: retracting onset, manager, and timer",
353 $params.getClosedLoopControlName(), drools.getRule().getName());
355 retract($manager.getOnsetEvent());
359 // TODO - what if we get subsequent Events for this RequestID?
360 // By default, it will all start over again. May be confusing for Ruby.
361 // Or, we could track this and then subsequently ignore the events
365 // Check whether we need to wait for abatement
367 if ($manager.getProcessor().getControlLoop().getAbatement() == true && notification.notification == ControlLoopNotificationType.FINAL_SUCCESS) {
368 logger.info("{}: {}: waiting for abatement ..",
369 $params.getClosedLoopControlName(), drools.getRule().getName());
371 logger.info("{}: {}: no abatement expect for {}. Closing the control loop",
372 $params.getClosedLoopControlName(), drools.getRule().getName(),
375 notification.from = "policy";
376 notification.policyName = drools.getRule().getName();
377 notification.policyScope = "${policyScope}";
378 notification.policyVersion = "${policyVersion}";
381 // In this case, we are done
383 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
387 TargetLock lock = $manager.unlockCurrentOperation();
389 logger.debug("{}: {}: retracting lock=", $params.getClosedLoopControlName(),
390 drools.getRule().getName(), lock);
394 // Retract everything from memory
396 logger.info("{}: {}: retracting onset, manager, and timer",
397 $params.getClosedLoopControlName(), drools.getRule().getName());
399 retract($manager.getOnsetEvent());
406 // NOT final, so let's ask for the next operation
408 ControlLoopOperationManager operation = $manager.processControlLoop();
409 if (operation != null) {
410 logger.info("{}: {}: starting operation={}",
411 $params.getClosedLoopControlName(), drools.getRule().getName(),
414 // insert into memory
418 // insert operation timeout object
420 OperationTimer opTimer = new OperationTimer();
421 opTimer.setClosedLoopControlName($event.closedLoopControlName);
422 opTimer.setRequestID($event.requestID.toString());
423 opTimer.setDelay(operation.getOperationTimeout().toString() + "s");
427 // Let's ask for a lock right away
429 LockResult<GuardResult, TargetLock> result = $manager.lockCurrentOperation();
430 if (result.getA().equals(GuardResult.LOCK_ACQUIRED)) {
431 logger.info("{}: {}: guard lock acquired={}",
432 $params.getClosedLoopControlName(), drools.getRule().getName(),
436 // Insert into memory
438 insert(result.getB());
442 // Probably waiting for abatement
444 logger.info("{}: {}: no operation, probably waiting for abatement",
445 $params.getClosedLoopControlName(), drools.getRule().getName());
448 } catch (Exception e) {
449 logger.warn("{}: {}: unexpected",
450 $params.getClosedLoopControlName(),
451 drools.getRule().getName(), e);
454 // TODO should we abort if we get an exception?
467 rule "${policyName}.EVENT.MANAGER.OPERATION.NOT_LOCKED.TIMEOUT"
470 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
471 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
472 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
473 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
474 not ( TargetLock (requestID == $event.requestID) )
477 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
478 logger.info("{}: {}: event={} manager={} operation={}",
479 $params.getClosedLoopControlName(), drools.getRule().getName(),
480 $event, $manager, $operation);
483 // Need to ask for a Lock
485 LockResult<GuardResult, TargetLock> result = $manager.lockCurrentOperation();
486 if (result.getA().equals(GuardResult.LOCK_ACQUIRED)) {
487 logger.info("{}: {}: guard lock acquired={}",
488 $params.getClosedLoopControlName(), drools.getRule().getName(),
492 // Insert into memory
494 insert(result.getB());
501 * Guard Permitted, let's send request to the actor.
504 rule "${policyName}.EVENT.MANAGER.OPERATION.LOCKED.GUARD_PERMITTED"
506 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
507 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
508 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
509 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID, "Permit".equalsIgnoreCase(getGuardApprovalStatus()) )
510 $lock : TargetLock (requestID == $event.requestID)
513 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
514 logger.info("{}: {}: event={} manager={} operation={} lock={}",
515 $params.getClosedLoopControlName(), drools.getRule().getName(),
516 $event, $manager, $operation, $lock);
518 Object request = $operation.getOperationRequest();
520 if (request != null) {
521 logger.debug("{}: {}: starting operation ..",
522 $params.getClosedLoopControlName(), drools.getRule().getName());
524 // Tell interested parties we are performing this Operation
526 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
527 notification.notification = ControlLoopNotificationType.OPERATION;
528 notification.message = $operation.getOperationMessage();
529 notification.history = $operation.getHistory();
530 notification.from = "policy";
531 notification.policyName = drools.getRule().getName();
532 notification.policyScope = "${policyScope}";
533 notification.policyVersion = "${policyVersion}";
535 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
537 switch ($operation.policy.getActor()){
541 if (request instanceof Request) {
542 PolicyEngine.manager.deliver("APPC-CL", request);
544 else if (request instanceof LCMRequestWrapper) {
545 PolicyEngine.manager.deliver("APPC-LCM-READ", request);
549 // at this point the AAI named query request should have already been made, the response recieved and used
550 // in the construction of the SO Request which is stored in operationRequest
552 if(request instanceof SORequest) {
553 // Call SO. The response will be inserted into memory once it's received
554 SOActorServiceProvider.sendRequest(drools.getWorkingMemory(), request);
558 if (request instanceof VFCRequest) {
560 Thread t = new Thread(new VFCManager(drools.getWorkingMemory(), (VFCRequest)request));
567 // What happens if its null?
569 logger.warn("{}: {}: unexpected null operation request",
570 $params.getClosedLoopControlName(),
571 drools.getRule().getName());
578 * We were able to acquire a lock so now let's ask Xacml Guard whether
579 * we are allowed to proceed with the request to the actor.
582 rule "${policyName}.EVENT.MANAGER.OPERATION.LOCKED.GUARD_NOT_YET_QUERIED"
584 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
585 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
586 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
587 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID, getGuardApprovalStatus() == "NONE" )
588 $lock : TargetLock (requestID == $event.requestID)
591 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
592 logger.info("{}: {}: event={} manager={} operation={} lock={}",
593 $params.getClosedLoopControlName(), drools.getRule().getName(),
594 $event, $manager, $operation, $lock);
597 // We are starting the operation but the actor won't be contacted until Guard is queried and permitted.
599 $operation.startOperation($event);
602 // Sending notification that we are about to query Guard ("DB write - start operation")
604 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
605 notification.notification = ControlLoopNotificationType.OPERATION;
606 notification.message = $operation.getOperationMessage();
607 notification.history = $operation.getHistory();
608 notification.from = "policy";
609 notification.policyName = drools.getRule().getName();
610 notification.policyScope = "${policyScope}";
611 notification.policyVersion = "${policyVersion}";
613 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
616 // Now send Guard Request to XACML Guard. In order to bypass the call to Guard,
617 // just change guardEnabled to false.
619 // In order to use REST XACML, provide a URL instead of "" as a second argument
620 // to the CallGuardTask() and set the first argument to null
621 // (instead of XacmlPdpEngine).
623 boolean guardEnabled = true;
627 Thread t = new Thread(new org.onap.policy.guard.CallGuardTask(
628 PolicyEngine.manager.getEnvironmentProperty("guard.url"),
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".equals($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 = $operation.getOperationMessage($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( closedLoopControlName == $params.getClosedLoopControlName(), 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( closedLoopControlName == $params.getClosedLoopControlName(), 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( request.requestId == $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);