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.startOperation($event);
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 // Sending notification that we are about to query Guard ("DB write - start operation")
599 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
600 notification.notification = ControlLoopNotificationType.OPERATION;
601 notification.message = "Sending guard query for " + $operation.policy.getActor() + " " + $operation.policy.getRecipe();
602 notification.history = $operation.getHistory();
603 notification.from = "policy";
604 notification.policyName = drools.getRule().getName();
605 notification.policyScope = "${policyScope}";
606 notification.policyVersion = "${policyVersion}";
608 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
611 // Now send Guard Request to XACML Guard. In order to bypass the call to Guard,
612 // just change guardEnabled to false.
614 // In order to use REST XACML, provide a URL instead of "" as a second argument
615 // to the CallGuardTask() and set the first argument to null
616 // (instead of XacmlPdpEngine).
618 boolean guardEnabled = false;
622 Thread t = new Thread(new org.onap.policy.guard.CallGuardTask(
623 drools.getWorkingMemory(),
624 $event.closedLoopControlName,
625 $operation.policy.getActor().toString(),
626 $operation.policy.getRecipe(),
627 $manager.getTargetInstance($operation.policy),
629 $event.requestID.toString()
634 insert(new PolicyGuardResponse("Permit", $event.requestID, $operation.policy.getRecipe()));
640 // This rule will be triggered when a thread talking to the XACML Guard inserts a
641 // guardResponse object into the working memory
643 rule "${policyName}.GUARD.RESPONSE"
645 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
646 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
647 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
648 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
649 $lock : TargetLock (requestID == $event.requestID)
650 $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
651 $guardResponse : PolicyGuardResponse(requestID == $event.requestID, $operation.policy.recipe == operation)
654 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
655 logger.info("{}: {}: event={} manager={} operation={} lock={} opTimer={} guardResponse={}",
656 $params.getClosedLoopControlName(), drools.getRule().getName(),
657 $event, $manager, $operation, $lock, $opTimer, $guardResponse);
660 //we will permit the operation if there was no Guard for it
661 if("Indeterminate".equalsIgnoreCase($guardResponse.result)){
662 $guardResponse.result = "Permit";
666 // This notification has Guard result in "message". ("DB write - end operation in case of Guard Deny")
668 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
669 notification.notification = ControlLoopNotificationType.OPERATION;
670 notification.message = "Guard result for " + $operation.policy.getActor() + " " + $operation.policy.getRecipe() + " is " + $guardResponse.result;
671 notification.history = $operation.getHistory();
672 notification.from = "policy";
673 notification.policyName = drools.getRule().getName();
674 notification.policyScope = "${policyScope}";
675 notification.policyVersion = "${policyVersion}";
677 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
679 if("Permit".equalsIgnoreCase($guardResponse.result)){
681 modify($operation){setGuardApprovalStatus($guardResponse.result)};
684 //This is the Deny case
685 $operation.setOperationHasGuardDeny();
688 modify($manager) {finishOperation($operation)};
691 retract($guardResponse);
697 * This rule responds to APPC Response Events
699 * I would have like to be consistent and write the Response like this:
700 * $response : Response( CommonHeader.RequestID == $onset.requestID )
702 * However, no compile error was given. But a runtime error was given. I think
703 * because drools is confused between the classname CommonHeader vs the property CommonHeader.
706 rule "${policyName}.APPC.RESPONSE"
708 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
709 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
710 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
711 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
712 $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
713 $lock : TargetLock (requestID == $event.requestID)
714 $response : Response( getCommonHeader().RequestID == $event.requestID )
717 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
718 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
719 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
720 $params.getClosedLoopControlName(), drools.getRule().getName(),
721 $event, $manager, $operation, $lock, $opTimer, $response);
723 // Get the result of the operation
725 PolicyResult policyResult = $operation.onResponse($response);
726 if (policyResult != null) {
727 logger.debug("{}: {}: operation finished - result={}",
728 $params.getClosedLoopControlName(), drools.getRule().getName(),
731 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
733 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
734 notification.from = "policy";
735 notification.policyName = drools.getRule().getName();
736 notification.policyScope = "${policyScope}";
737 notification.policyVersion = "${policyVersion}";
738 notification.message = $operation.getOperationHistory();
739 notification.history = $operation.getHistory();
740 if (policyResult.equals(PolicyResult.SUCCESS)) {
741 notification.notification = ControlLoopNotificationType.OPERATION_SUCCESS;
743 // Let interested parties know
745 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
747 notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
749 // Let interested parties know
751 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
754 // Ensure the operation is complete
756 if ($operation.isOperationComplete() == true) {
758 // It is complete, remove it from memory
762 // We must also retract the timer object
763 // NOTE: We could write a Rule to do this
767 // Complete the operation
769 modify($manager) {finishOperation($operation)};
772 // Just doing this will kick off the LOCKED rule again
774 modify($operation) {};
778 // Its not finished yet (i.e. expecting more Response objects)
780 // Or possibly it is a leftover response that we timed the request out previously
784 // We are going to retract these objects from memory
791 * The problem with Responses is that they don't have a controlLoopControlName
792 * field in them, so the only way to attach them is via RequestID. If we have multiple
793 * control loop .drl's loaded in the same container, we need to be sure the cleanup
794 * rules don't remove Responses for other control loops.
797 rule "${policyName}.APPC.RESPONSE.CLEANUP"
799 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
800 $response : Response($id : getCommonHeader().RequestID )
801 not ( VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), requestID == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
804 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
805 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
806 logger.debug("{}: {}: orphan appc response={}",
807 $params.getClosedLoopControlName(), drools.getRule().getName(), $id);
817 * This rule responds to APPC Response Events using the new LCM interface provided by appc
820 rule "${policyName}.APPC.LCM.RESPONSE"
822 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
823 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
824 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
825 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
826 $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
827 $lock : TargetLock (requestID == $event.requestID)
828 $response : LCMResponseWrapper( getBody().getCommonHeader().getRequestId() == $event.requestID )
831 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
832 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
833 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
834 $params.getClosedLoopControlName(), drools.getRule().getName(),
835 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
838 // Get the result of the operation
840 PolicyResult policyResult = $operation.onResponse($response);
841 if (policyResult != null) {
842 logger.debug("{}: {}: operation finished - result={}",
843 $params.getClosedLoopControlName(), drools.getRule().getName(),
847 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
849 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
850 notification.from = "policy";
851 notification.policyName = drools.getRule().getName();
852 notification.policyScope = "${policyScope}";
853 notification.policyVersion = "${policyVersion}";
854 notification.message = $operation.getOperationHistory();
855 notification.history = $operation.getHistory();
856 if (policyResult.equals(PolicyResult.SUCCESS)) {
857 notification.notification = ControlLoopNotificationType.OPERATION_SUCCESS;
859 notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
861 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
863 // Ensure the operation is complete
865 if ($operation.isOperationComplete() == true) {
867 // It is complete, remove it from memory
871 // We must also retract the timer object
872 // NOTE: We could write a Rule to do this
876 // Complete the operation
878 modify($manager) {finishOperation($operation)};
881 // Just doing this will kick off the LOCKED rule again
883 modify($operation) {};
887 // Its not finished yet (i.e. expecting more Response objects)
889 // Or possibly it is a leftover response that we timed the request out previously
893 // We are going to retract these objects from memory
900 * Clean Up any lingering LCM reponses
903 rule "${policyName}.APPC.LCM.RESPONSE.CLEANUP"
905 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
906 $response : LCMResponseWrapper($id : getBody().getCommonHeader().getRequestId )
907 not ( VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), requestID == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
910 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
911 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
912 logger.debug("{}: {}: orphan appc response={}",
913 $params.getClosedLoopControlName(), drools.getRule().getName(), $id);
922 * This rule responds to SO Response Events
925 rule "${policyName}.SO.RESPONSE"
927 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
928 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
929 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName )
930 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
931 $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
932 $lock : TargetLock (requestID == $event.requestID)
933 $response : SOResponse( requestReferences.requestId.toString() == $event.requestID.toString() )
936 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
937 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
938 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
939 $params.getClosedLoopControlName(), drools.getRule().getName(),
940 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
942 // Get the result of the operation
944 PolicyResult policyResult = $operation.onResponse($response);
945 if (policyResult != null) {
946 logger.debug("{}: {}: operation finished - result={}",
947 $params.getClosedLoopControlName(), drools.getRule().getName(),
951 // This Operation has completed, construct a notification showing our results
953 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
954 notification.from = "policy";
955 notification.policyName = drools.getRule().getName();
956 notification.policyScope = "${policyScope}";
957 notification.policyVersion = "${policyVersion}";
958 notification.message = $operation.getOperationHistory();
959 notification.history = $operation.getHistory();
960 if (policyResult.equals(PolicyResult.SUCCESS)) {
961 notification.notification = ControlLoopNotificationType.OPERATION_SUCCESS;
963 notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
966 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
968 // Ensure the operation is complete
970 if ($operation.isOperationComplete() == true) {
972 // It is complete, remove it from memory
976 // We must also retract the timer object
977 // NOTE: We could write a Rule to do this
981 // Complete the operation
983 modify($manager) {finishOperation($operation)};
986 // Just doing this will kick off the LOCKED rule again
988 modify($operation) {};
992 // Its not finished yet (i.e. expecting more Response objects)
994 // Or possibly it is a leftover response that we timed the request out previously
998 // We are going to retract these objects from memory
1006 * This rule responds to VFC Response Events
1009 rule "${policyName}.VFC.RESPONSE"
1011 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1012 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1013 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName )
1014 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
1015 $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
1016 $lock : TargetLock (requestID == $event.requestID)
1017 $response : VFCResponse( requestId.toString() == $event.requestID.toString() )
1019 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1020 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1021 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
1022 $params.getClosedLoopControlName(), drools.getRule().getName(),
1023 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
1025 // Get the result of the operation
1027 PolicyResult policyResult = $operation.onResponse($response);
1028 if (policyResult != null) {
1030 // This Operation has completed, construct a notification showing our results
1032 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1033 notification.from = "policy";
1034 notification.policyName = drools.getRule().getName();
1035 notification.policyScope = "${policyScope}";
1036 notification.policyVersion = "${policyVersion}";
1037 notification.message = $operation.getOperationHistory();
1038 notification.history = $operation.getHistory();
1040 // Ensure the operation is complete
1042 if ($operation.isOperationComplete() == true) {
1044 // It is complete, remove it from memory
1046 retract($operation);
1048 // We must also retract the timer object
1049 // NOTE: We could write a Rule to do this
1053 // Complete the operation
1055 modify($manager) {finishOperation($operation)};
1058 // Just doing this will kick off the LOCKED rule again
1060 modify($operation) {};
1064 // Its not finished yet (i.e. expecting more Response objects)
1066 // Or possibly it is a leftover response that we timed the request out previously
1070 // We are going to retract these objects from memory
1078 * This is the timer that manages the timeout for an individual operation.
1081 rule "${policyName}.EVENT.MANAGER.OPERATION.TIMEOUT"
1084 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1085 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
1086 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
1087 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
1088 $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString(), $to : getDelay() )
1089 $lock : TargetLock (requestID == $event.requestID)
1092 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1093 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1094 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={}",
1095 $params.getClosedLoopControlName(), drools.getRule().getName(),
1096 $event, $manager, $operation, $lock, $operation, $opTimer);
1099 // Tell it its timed out
1101 $operation.setOperationHasTimedOut();
1103 // Create a notification for it ("DB Write - end operation")
1105 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1106 notification.from = "policy";
1107 notification.policyName = drools.getRule().getName();
1108 notification.policyScope = "${policyScope}";
1109 notification.policyVersion = "${policyVersion}";
1110 notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
1111 notification.message = $operation.getOperationHistory();
1112 notification.history = $operation.getHistory();
1114 // Let interested parties know
1116 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1118 // Get rid of the timer
1122 // Ensure the operation is complete
1124 if ($operation.isOperationComplete() == true) {
1126 // It is complete, remove it from memory
1128 retract($operation);
1130 // Complete the operation
1132 modify($manager) {finishOperation($operation)};
1135 // Just doing this will kick off the LOCKED rule again
1137 modify($operation) {};
1143 * This is the timer that manages the overall control loop timeout.
1146 rule "${policyName}.EVENT.MANAGER.TIMEOUT"
1149 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1150 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
1151 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
1152 $clTimer : ControlLoopTimer ( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString(), $to : getDelay() )
1153 $operations : LinkedList()
1154 from collect( ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID ) )
1155 $opTimers : LinkedList()
1156 from collect( OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() ) )
1157 $locks : LinkedList()
1158 from collect( TargetLock (requestID == $event.requestID) )
1161 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1162 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1164 if ($operations == null) {
1165 logger.debug("{}: {}: event={} manager={} clTimer={} operations=0",
1166 $params.getClosedLoopControlName(), drools.getRule().getName(),
1167 $event, $manager, $clTimer);
1169 logger.debug("{}: {}: event={} manager={} clTimer={} operations={}",
1170 $params.getClosedLoopControlName(), drools.getRule().getName(),
1171 $event, $manager, $clTimer, $operations.size());
1174 // Tell the Event Manager it has timed out
1176 VirtualControlLoopNotification notification = $manager.setControlLoopTimedOut();
1177 if (notification != null) {
1178 notification.from = "policy";
1179 notification.policyName = drools.getRule().getName();
1180 notification.policyScope = "${policyScope}";
1181 notification.policyVersion = "${policyVersion}";
1183 // Let interested parties know
1185 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1188 // Retract EVERYTHING
1193 if ($operations != null && $operations.size() > 0) {
1194 Iterator<ControlLoopOperationManager> iter = $operations.iterator();
1195 while (iter.hasNext()) {
1196 ControlLoopOperationManager manager = iter.next();
1200 if ($opTimers != null && $opTimers.size() > 0) {
1201 Iterator<OperationTimer> iter = $opTimers.iterator();
1202 while (iter.hasNext()) {
1203 OperationTimer opTimer = iter.next();
1207 if ($locks != null && $locks.size() > 0) {
1208 Iterator<TargetLock> iter = $locks.iterator();
1209 while (iter.hasNext()) {
1210 TargetLock lock = iter.next();
1212 // Ensure we release the lock
1214 PolicyGuard.unlockTarget(lock);