2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 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.NewEventStatus;
33 import org.onap.policy.controlloop.eventmanager.ControlLoopOperationManager;
34 import org.onap.policy.controlloop.actor.so.SoActorServiceProvider;
35 import org.onap.policy.aai.AaiNqResponseWrapper;
36 import org.onap.policy.appc.Request;
37 import org.onap.policy.appc.Response;
38 import org.onap.policy.appc.CommonHeader;
39 import org.onap.policy.appclcm.LcmRequestWrapper;
40 import org.onap.policy.appclcm.LcmResponseWrapper;
41 import org.onap.policy.appclcm.LcmRequest;
42 import org.onap.policy.appclcm.LcmResponse;
43 import org.onap.policy.appclcm.LcmCommonHeader;
44 import org.onap.policy.sdnr.PciRequestWrapper;
45 import org.onap.policy.sdnr.PciResponseWrapper;
46 import org.onap.policy.sdnr.PciRequest;
47 import org.onap.policy.sdnr.PciResponse;
48 import org.onap.policy.vfc.VfcRequest;
49 import org.onap.policy.vfc.VfcResponse;
50 import org.onap.policy.vfc.VfcManager;
51 import org.onap.policy.so.SoManager;
52 import org.onap.policy.so.SoRequest;
53 import org.onap.policy.so.SoResponseWrapper;
54 import org.onap.policy.sdnc.SdncRequest;
55 import org.onap.policy.sdnc.SdncManager;
56 import org.onap.policy.sdnc.SdncResponse;
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
99 * Used to trigger clean up Params that no longer have associated rules.
101 declare ParamsInitCleaner
102 closedLoopControlName : String // only used when logging
106 * Used to clean up Params that no longer have associated rules.
108 declare ParamsCleaner
109 closedLoopControlName : String
110 controlLoopYaml : String
114 * This object is to provide support for timeouts
115 * due to a bug in drools' built-in timers
117 declare ControlLoopTimer
118 closedLoopControlName : String
122 //timerType is the type of timer: either "ClosedLoop" or "Operation"
128 * Called to insert the parameters into working memory for this Closed Loop policy. This is called
129 * once each time a closed loop is added or its YAML is updated.
130 * This has a higher salience so we can ensure that the Params is created before we have a chance to
131 * discard any events.
134 rule "${policyName}.SETUP"
137 not( Params( getClosedLoopControlName() == "${closedLoopControlName}",
138 getControlLoopYaml() == "${controlLoopYaml}" ) )
141 Params params = new Params();
142 params.setClosedLoopControlName("${closedLoopControlName}");
143 params.setControlLoopYaml("${controlLoopYaml}");
146 ParamsInitCleaner initCleaner = new ParamsInitCleaner();
147 initCleaner.setClosedLoopControlName("${closedLoopControlName}");
150 // Note: globals have bad behavior when persistence is used,
151 // hence explicitly getting the logger vs using a global
153 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
154 logger.info("{}: {} : YAML=[{}]", params.getClosedLoopControlName(), drools.getRule().getName(),
155 params.getControlLoopYaml());
160 * This rule responds to DCAE Events where there is no manager yet. Either it is
161 * the first ONSET, or a subsequent badly formed Event (i.e. Syntax error, or is-closed-loop-disabled)
164 rule "${policyName}.EVENT"
166 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
167 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
168 not ( ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
169 requestId == $event.getRequestId() ) )
172 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
173 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
178 // Check the event, because we need it to not be null when
179 // we create the ControlLoopEventManager. The ControlLoopEventManager
180 // will do extra syntax checking as well check if the closed loop is disabled.
182 if ($event.getRequestId() == null) {
183 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
184 notification.setNotification(ControlLoopNotificationType.REJECTED);
185 notification.setFrom("policy");
186 notification.setMessage("Missing requestId");
187 notification.setPolicyName(drools.getRule().getName());
188 notification.setPolicyScope("${policyScope}");
189 notification.setPolicyVersion("${policyVersion}");
192 // Let interested parties know
194 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
197 // Retract it from memory
200 } else if ($event.getClosedLoopEventStatus() != ControlLoopEventStatus.ONSET) {
201 throw new ControlLoopException($event.getClosedLoopEventStatus() + " received with no prior onset");
204 // Create an EventManager
206 ControlLoopEventManager manager = new ControlLoopEventManager($params.getClosedLoopControlName(),
207 $event.getRequestId());
209 // Determine if EventManager can actively process the event
210 // (i.e. syntax, is_closed_loop_disabled checks etc.)
212 VirtualControlLoopNotification notification = manager.activate($params.getControlLoopYaml(), $event);
213 notification.setFrom("pdp-0001-controller=controlloop"); // Engine.getInstanceName()
214 notification.setPolicyName(drools.getRule().getName());
215 notification.setPolicyScope("${policyScope}");
216 notification.setPolicyVersion("${policyVersion}");
218 // Are we actively pursuing this event?
220 if (notification.getNotification() == ControlLoopNotificationType.ACTIVE) {
222 // Insert Event Manager into memory, this will now kick off processing.
226 // Let interested parties know
228 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
230 // Setup the Overall Control Loop timer
232 ControlLoopTimer clTimer = new ControlLoopTimer();
233 clTimer.setTimerType("ClosedLoop");
234 clTimer.setClosedLoopControlName($event.getClosedLoopControlName());
235 clTimer.setRequestId($event.getRequestId().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.setNotification(ControlLoopNotificationType.REJECTED);
263 notification.setMessage("Exception occurred: " + e.getMessage());
264 notification.setPolicyName(drools.getRule().getName());
265 notification.setPolicyScope("${policyScope}");
266 notification.setPolicyVersion("${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.getClosedLoopControlName(),
289 requestId == $event.getRequestId() )
290 $clTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
291 requestId == $event.getRequestId().toString(), timerType == "ClosedLoop", !expired )
294 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
295 logger.info("{}: {}: event={} manager={} clTimer={}",
296 $params.getClosedLoopControlName(), drools.getRule().getName(),
297 $event, $manager, $clTimer);
301 // Check which event this is.
303 ControlLoopEventManager.NewEventStatus eventStatus = $manager.onNewEvent($event);
305 // Check what kind of event this is
307 if (eventStatus == NewEventStatus.SUBSEQUENT_ONSET) {
309 // We don't care about subsequent onsets
311 logger.info("{}: {}: subsequent onset",
312 $params.getClosedLoopControlName(), drools.getRule().getName());
316 if (eventStatus == NewEventStatus.SYNTAX_ERROR) {
318 // Ignore any bad syntax events
320 logger.warn("{}: {}: syntax error",
321 $params.getClosedLoopControlName(), drools.getRule().getName());
326 // We only want the initial ONSET event in memory,
327 // all the other events need to be retracted to support
328 // cleanup and avoid the other rules being fired for this event.
330 if (eventStatus != NewEventStatus.FIRST_ONSET) {
331 logger.warn("{}: {}: not first onset",
332 $params.getClosedLoopControlName(), drools.getRule().getName());
336 logger.debug("{}: {}: target={}", $params.getClosedLoopControlName(),
337 drools.getRule().getName(), $event.getTarget());
339 // Now start seeing if we need to process this event
343 // Check if this is a Final Event
345 VirtualControlLoopNotification notification = $manager.isControlLoopFinal();
348 if (notification != null) {
350 // Its final, but are we waiting for abatement?
352 if ($manager.getNumAbatements() > 0) {
353 logger.info("{}: {}: abatement received for {}. Closing the control loop",
354 $params.getClosedLoopControlName(), drools.getRule().getName(),
355 $event.getRequestId());
357 /// DB Write---end event processing for this RequestId()
358 $manager.commitAbatement("Event Abated","Closed");
360 notification.setFrom("policy");
361 notification.setPolicyName(drools.getRule().getName());
362 notification.setPolicyScope("${policyScope}");
363 notification.setPolicyVersion("${policyVersion}");
365 // In this case, we are done
367 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
371 TargetLock lock = $manager.unlockCurrentOperation();
373 logger.debug("{}: {}: retracting lock=", $params.getClosedLoopControlName(),
374 drools.getRule().getName(), lock);
378 // Retract everything from memory
380 logger.info("{}: {}: retracting onset, manager, and timer",
381 $params.getClosedLoopControlName(), drools.getRule().getName());
383 retract($manager.getOnsetEvent());
385 // don't retract manager, etc. - a clean-up rule will do that
388 // TODO - what if we get subsequent Events for this RequestId?
389 // By default, it will all start over again. May be confusing for Ruby.
390 // Or, we could track this and then subsequently ignore the events
394 // Check whether we need to wait for abatement
396 if ($manager.getProcessor().getControlLoop().getAbatement() == true && notification.getNotification() == ControlLoopNotificationType.FINAL_SUCCESS) {
397 logger.info("{}: {}: waiting for abatement ..",
398 $params.getClosedLoopControlName(), drools.getRule().getName());
400 logger.info("{}: {}: no abatement expect for {}. Closing the control loop",
401 $params.getClosedLoopControlName(), drools.getRule().getName(),
402 $event.getRequestId());
404 notification.setFrom("policy");
405 notification.setPolicyName(drools.getRule().getName());
406 notification.setPolicyScope("${policyScope}");
407 notification.setPolicyVersion("${policyVersion}");
410 // In this case, we are done
412 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
416 TargetLock lock = $manager.unlockCurrentOperation();
418 logger.debug("{}: {}: retracting lock=", $params.getClosedLoopControlName(),
419 drools.getRule().getName(), lock);
423 // Retract everything from memory
425 logger.info("{}: {}: retracting onset, manager, and timer",
426 $params.getClosedLoopControlName(), drools.getRule().getName());
428 retract($manager.getOnsetEvent());
430 // don't retract manager, etc. - a clean-up rule will do that
435 // NOT final, so let's ask for the next operation
437 ControlLoopOperationManager operation = $manager.processControlLoop();
438 if (operation != null) {
440 // Let's ask for a lock right away
442 LockResult<GuardResult, TargetLock> result = $manager.lockCurrentOperation();
443 logger.info("{}: {}: guard lock acquired={}",
444 $params.getClosedLoopControlName(), drools.getRule().getName(),
446 if (result.getA().equals(GuardResult.LOCK_ACQUIRED)) {
448 // insert the operation into memory
452 // insert operation timeout object
454 ControlLoopTimer opTimer = new ControlLoopTimer();
455 opTimer.setTimerType("Operation");
456 opTimer.setClosedLoopControlName($event.getClosedLoopControlName());
457 opTimer.setRequestId($event.getRequestId().toString());
458 opTimer.setDelay(operation.getOperationTimeout().toString() + "s");
462 // Insert lock into memory
464 insert(result.getB());
466 logger.debug("The target resource {} is already processing",
467 $event.getAai().get($event.getTarget()));
468 notification = new VirtualControlLoopNotification($event);
469 notification.setNotification(ControlLoopNotificationType.REJECTED);
470 notification.setMessage("The target " + $event.getAai().get($event.getTarget())
471 + " is already locked");
472 notification.setFrom("policy");
473 notification.setPolicyName(drools.getRule().getName());
474 notification.setPolicyScope("${policyScope}");
475 notification.setPolicyVersion("${policyVersion}");
477 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
481 // don't retract manager, etc. - a clean-up rule will do that
483 if (result.getB() != null) {
484 retract(result.getB());
487 logger.info("{}: {}: starting operation={}",
488 $params.getClosedLoopControlName(), drools.getRule().getName(),
492 // Probably waiting for abatement
494 logger.info("{}: {}: no operation, probably waiting for abatement",
495 $params.getClosedLoopControlName(), drools.getRule().getName());
498 } catch (Exception e) {
499 logger.warn("{}: {}: unexpected",
500 $params.getClosedLoopControlName(),
501 drools.getRule().getName(), e);
503 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
504 notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
505 notification.setMessage(e.getMessage());
506 notification.setFrom("policy");
507 notification.setPolicyName(drools.getRule().getName());
508 notification.setPolicyScope("${policyScope}");
509 notification.setPolicyVersion("${policyVersion}");
511 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
515 // don't retract manager, etc. - a clean-up rule will do that
522 * Guard Permitted, let's send request to the actor.
525 rule "${policyName}.EVENT.MANAGER.OPERATION.LOCKED.GUARD_PERMITTED"
527 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
528 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
529 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
530 requestId == $event.getRequestId() )
531 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
532 onset.getRequestId() == $event.getRequestId(), "Permit".equalsIgnoreCase(getGuardApprovalStatus()) )
533 $lock : TargetLock (requestId == $event.getRequestId())
534 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
535 requestId == $event.getRequestId().toString(), timerType == "Operation", !expired )
538 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
539 logger.info("{}: {}: event={} manager={} operation={} lock={}",
540 $params.getClosedLoopControlName(), drools.getRule().getName(),
541 $event, $manager, $operation, $lock);
543 Object request = null;
544 boolean caughtException = false;
547 request = $operation.startOperation($event);
549 if (request != null) {
550 logger.debug("{}: {}: starting operation ..",
551 $params.getClosedLoopControlName(), drools.getRule().getName());
553 // Tell interested parties we are performing this Operation
555 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
556 notification.setNotification(ControlLoopNotificationType.OPERATION);
557 notification.setMessage($operation.getOperationMessage());
558 notification.setHistory($operation.getHistory());
559 notification.setFrom("policy");
560 notification.setPolicyName(drools.getRule().getName());
561 notification.setPolicyScope("${policyScope}");
562 notification.setPolicyVersion("${policyVersion}");
564 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
566 switch ($operation.policy.getActor()){
569 if (request instanceof Request) {
570 PolicyEngine.manager.deliver("APPC-CL", request);
572 else if (request instanceof LcmRequestWrapper) {
573 PolicyEngine.manager.deliver("APPC-LCM-READ", request);
577 // at this point the AAI named query request should have already been made,
578 // the response recieved and used
579 // in the construction of the SO Request which is stored in operationRequest
581 if(request instanceof SoRequest) {
582 // Call SO. The response will be inserted into memory once it's received
583 class mySoCallback implements SoManager.SoCallback {
584 public void onSoResponseWrapper(SoResponseWrapper wrapper) {
585 drools.getWorkingMemory().insert(wrapper);
588 SoActorServiceProvider.sendRequest($event.getRequestId().toString(),
591 PolicyEngine.manager.getEnvironmentProperty("so.url"),
592 PolicyEngine.manager.getEnvironmentProperty("so.username"),
593 PolicyEngine.manager.getEnvironmentProperty("so.password"));
597 if (request instanceof VfcRequest) {
599 class myVfcCallback implements VfcManager.VfcCallback {
601 public void onResponse(VfcResponse responseError) {
602 drools.getWorkingMemory().insert(responseError);
606 Thread t = new Thread(new VfcManager(new myVfcCallback(),
608 PolicyEngine.manager.getEnvironmentProperty("vfc.url"),
609 PolicyEngine.manager.getEnvironmentProperty("vfc.username"),
610 PolicyEngine.manager.getEnvironmentProperty("vfc.password")));
616 if (request instanceof SdncRequest) {
618 class mySdncCallback implements SdncManager.SdncCallback {
619 public void onCallback(SdncResponse response) {
620 drools.getWorkingMemory().insert(response);
624 Thread t = new Thread(new SdncManager(new mySdncCallback(),
625 (SdncRequest)request,
626 PolicyEngine.manager.getEnvironmentProperty("sdnc.url"),
627 PolicyEngine.manager.getEnvironmentProperty("sdnc.username"),
628 PolicyEngine.manager.getEnvironmentProperty("sdnc.password")));
633 if (request instanceof PciRequestWrapper) {
634 PolicyEngine.manager.deliver("SDNR-CL", request);
640 // What happens if its null?
642 logger.warn("{}: {}: unexpected null operation request",
643 $params.getClosedLoopControlName(),
644 drools.getRule().getName());
645 if ("SO".equals($operation.policy.getActor())) {
648 modify($manager) {finishOperation($operation)};
650 else if ("vfc".equalsIgnoreCase($operation.policy.getActor())) {
653 modify($manager) {finishOperation($operation)};
655 else if ("sdnc".equalsIgnoreCase($operation.policy.getActor())) {
658 modify($manager) {finishOperation($operation)};
662 } catch (Exception e) {
663 String msg = e.getMessage();
664 logger.warn("{}: {}: operation={}: AAI failure: {}",
665 $params.getClosedLoopControlName(), drools.getRule().getName(),
667 $operation.setOperationHasException(msg);
669 if(request != null) {
671 // Create a notification for it ("DB Write - end operation")
673 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
674 notification.setFrom("policy");
675 notification.setPolicyName(drools.getRule().getName());
676 notification.setPolicyScope("${policyScope}");
677 notification.setPolicyVersion("${policyVersion}");
678 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
679 notification.setMessage($operation.getOperationHistory());
680 notification.setHistory($operation.getHistory());
682 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
687 caughtException = true;
690 // Having the modify statement in the catch clause doesn't work for whatever reason
691 if (caughtException) {
692 modify($manager) {finishOperation($operation)};
699 * We were able to acquire a lock so now let's ask Xacml Guard whether
700 * we are allowed to proceed with the request to the actor.
703 rule "${policyName}.EVENT.MANAGER.OPERATION.LOCKED.GUARD_NOT_YET_QUERIED"
705 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
706 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
707 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
708 requestId == $event.getRequestId() )
709 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
710 onset.getRequestId() == $event.getRequestId(), getGuardApprovalStatus() == "NONE" )
711 $lock : TargetLock (requestId == $event.getRequestId())
714 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
715 logger.info("{}: {}: event={} manager={} operation={} lock={}",
716 $params.getClosedLoopControlName(), drools.getRule().getName(),
717 $event, $manager, $operation, $lock);
720 // Sending notification that we are about to query Guard ("DB write - start operation")
722 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
723 notification.setNotification(ControlLoopNotificationType.OPERATION);
724 notification.setMessage("Sending guard query for " + $operation.policy.getActor() + " "
725 + $operation.policy.getRecipe());
726 notification.setHistory($operation.getHistory());
727 notification.setFrom("policy");
728 notification.setPolicyName(drools.getRule().getName());
729 notification.setPolicyScope("${policyScope}");
730 notification.setPolicyVersion("${policyVersion}");
732 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
735 // Now send Guard Request to XACML Guard. In order to bypass the call to Guard,
736 // just change guardEnabled to false.
738 // In order to use REST XACML, provide a URL instead of "" as a second argument
739 // to the CallGuardTask() and set the first argument to null
740 // (instead of XacmlPdpEngine).
743 // NOTE: The environment properties uses "guard.disabled" but the boolean is guardEnabled
744 boolean guardEnabled = "false".equalsIgnoreCase(PolicyEngine.manager.getEnvironmentProperty("guard.disabled"));
748 Thread t = new Thread(new org.onap.policy.guard.CallGuardTask(
749 drools.getWorkingMemory(),
750 $event.getClosedLoopControlName(),
751 $operation.policy.getActor().toString(),
752 $operation.policy.getRecipe(),
753 $operation.getTargetEntity(),
754 $event.getRequestId().toString(),
756 AaiNqResponseWrapper resp = $manager.getNqVserverFromAai();
757 return(resp == null ? null : resp.countVfModules());
762 insert(new PolicyGuardResponse("Permit", $event.getRequestId(), $operation.policy.getRecipe()));
768 // This rule will be triggered when a thread talking to the XACML Guard inserts a
769 // guardResponse object into the working memory
771 rule "${policyName}.GUARD.RESPONSE"
773 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
774 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
775 closedLoopEventStatus == ControlLoopEventStatus.ONSET )
776 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
777 requestId == $event.getRequestId() )
778 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
779 onset.getRequestId() == $event.getRequestId() )
780 $lock : TargetLock (requestId == $event.getRequestId())
781 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
782 requestId == $event.getRequestId().toString(), timerType == "Operation", !expired )
783 $guardResponse : PolicyGuardResponse(requestId == $event.getRequestId(), $operation.policy.recipe == operation)
786 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
787 logger.info("{}: {}: event={} manager={} operation={} lock={} opTimer={} guardResponse={}",
788 $params.getClosedLoopControlName(), drools.getRule().getName(),
789 $event, $manager, $operation, $lock, $opTimer, $guardResponse);
792 //we will permit the operation if there was no Guard for it
793 if("Indeterminate".equalsIgnoreCase($guardResponse.getResult())){
794 $guardResponse.setResult("Permit");
798 // This notification has Guard result in "message". ("DB write - end operation in case of Guard Deny")
800 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
801 notification.setNotification(ControlLoopNotificationType.OPERATION);
802 notification.setMessage("Guard result for " + $operation.policy.getActor() + " " + $operation.policy.getRecipe()
803 + " is " + $guardResponse.getResult());
804 notification.setHistory($operation.getHistory());
805 notification.setFrom("policy");
806 notification.setPolicyName(drools.getRule().getName());
807 notification.setPolicyScope("${policyScope}");
808 notification.setPolicyVersion("${policyVersion}");
810 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
812 if("Permit".equalsIgnoreCase($guardResponse.getResult())){
814 modify($operation){setGuardApprovalStatus($guardResponse.getResult())};
817 //This is the Deny case
818 $operation.startOperation($event);
819 $operation.setOperationHasGuardDeny();
822 modify($manager) {finishOperation($operation)};
825 retract($guardResponse);
831 * This rule responds to APPC Response Events
833 * I would have like to be consistent and write the Response like this:
834 * $response : Response( CommonHeader.RequestId == $onset.getRequestId() )
836 * However, no compile error was given. But a runtime error was given. I think
837 * because drools is confused between the classname CommonHeader vs the property CommonHeader.
840 rule "${policyName}.APPC.RESPONSE"
842 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
843 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
844 closedLoopEventStatus == ControlLoopEventStatus.ONSET )
845 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
846 requestId == $event.getRequestId() )
847 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
848 onset.getRequestId() == $event.getRequestId() )
849 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
850 requestId == $event.getRequestId().toString(), timerType == "Operation", !expired )
851 $lock : TargetLock (requestId == $event.getRequestId())
852 $response : Response( getCommonHeader().RequestId == $event.getRequestId() )
855 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
856 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
857 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
858 $params.getClosedLoopControlName(), drools.getRule().getName(),
859 $event, $manager, $operation, $lock, $opTimer, $response);
861 // Get the result of the operation
863 PolicyResult policyResult = $operation.onResponse($response);
864 if (policyResult != null) {
865 logger.debug("{}: {}: operation finished - result={}",
866 $params.getClosedLoopControlName(), drools.getRule().getName(),
869 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
871 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
872 notification.setFrom("policy");
873 notification.setPolicyName(drools.getRule().getName());
874 notification.setPolicyScope("${policyScope}");
875 notification.setPolicyVersion("${policyVersion}");
876 notification.setMessage($operation.getOperationHistory());
877 notification.setHistory($operation.getHistory());
878 if (policyResult.equals(PolicyResult.SUCCESS)) {
879 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
881 // Let interested parties know
883 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
885 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
887 // Let interested parties know
889 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
892 // Ensure the operation is complete
894 if ($operation.isOperationComplete() == true) {
896 // It is complete, remove it from memory
900 // We must also retract the timer object
901 // NOTE: We could write a Rule to do this
905 // Complete the operation
907 modify($manager) {finishOperation($operation)};
910 // Just doing this will kick off the LOCKED rule again
912 modify($operation) {};
916 // Its not finished yet (i.e. expecting more Response objects)
918 // Or possibly it is a leftover response that we timed the request out previously
922 // We are going to retract these objects from memory
929 * The problem with Responses is that they don't have a controlLoopControlName
930 * field in them, so the only way to attach them is via RequestId. If we have multiple
931 * control loop .drl's loaded in the same container, we need to be sure the cleanup
932 * rules don't remove Responses for other control loops.
935 rule "${policyName}.APPC.RESPONSE.CLEANUP"
937 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
938 $response : Response($id : getCommonHeader().RequestId )
939 not ( VirtualControlLoopEvent( requestId == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
942 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
943 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
944 logger.debug("{}: {}: orphan appc response={}",
945 $params.getClosedLoopControlName(), drools.getRule().getName(), $id);
955 * This rule responds to APPC Response Events using the new LCM interface provided by appc
958 rule "${policyName}.APPC.LCM.RESPONSE"
960 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
961 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
962 closedLoopEventStatus == ControlLoopEventStatus.ONSET )
963 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
964 requestId == $event.getRequestId() )
965 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
966 onset.getRequestId() == $event.getRequestId() )
967 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
968 requestId == $event.getRequestId().toString(), timerType == "Operation", !expired )
969 $lock : TargetLock (requestId == $event.getRequestId())
970 $response : LcmResponseWrapper( getBody().getCommonHeader().getRequestId() == $event.getRequestId() )
973 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
974 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
975 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
976 $params.getClosedLoopControlName(), drools.getRule().getName(),
977 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
980 // Get the result of the operation
982 PolicyResult policyResult = $operation.onResponse($response);
983 if (policyResult != null) {
984 logger.debug("{}: {}: operation finished - result={}",
985 $params.getClosedLoopControlName(), drools.getRule().getName(),
989 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
991 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
992 notification.setFrom("policy");
993 notification.setPolicyName(drools.getRule().getName());
994 notification.setPolicyScope("${policyScope}");
995 notification.setPolicyVersion("${policyVersion}");
996 notification.setMessage($operation.getOperationHistory());
997 notification.setHistory($operation.getHistory());
998 if (policyResult.equals(PolicyResult.SUCCESS)) {
999 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
1001 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
1003 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1005 // Ensure the operation is complete
1007 if ($operation.isOperationComplete() == true) {
1009 // It is complete, remove it from memory
1011 retract($operation);
1013 // We must also retract the timer object
1014 // NOTE: We could write a Rule to do this
1018 // Complete the operation
1020 modify($manager) {finishOperation($operation)};
1023 // Just doing this will kick off the LOCKED rule again
1025 modify($operation) {};
1029 // Its not finished yet (i.e. expecting more Response objects)
1031 // Or possibly it is a leftover response that we timed the request out previously
1035 // We are going to retract these objects from memory
1042 * Clean Up any lingering LCM reponses
1045 rule "${policyName}.APPC.LCM.RESPONSE.CLEANUP"
1047 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1048 $response : LcmResponseWrapper($id : getBody().getCommonHeader().getRequestId )
1049 not ( VirtualControlLoopEvent( requestId == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
1052 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1053 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1054 logger.debug("{}: {}: orphan appc response={}",
1055 $params.getClosedLoopControlName(), drools.getRule().getName(), $id);
1064 * This rule responds to SDNR Response Events using the new interface provided by SDNR
1067 rule "${policyName}.SDNR.RESPONSE"
1069 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1070 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
1071 closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1072 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1073 requestId == $event.getRequestId() )
1074 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
1075 onset.getRequestId() == $event.getRequestId() )
1076 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1077 requestId == $event.getRequestId().toString(), timerType == "Operation", !expired )
1078 $lock : TargetLock (requestId == $event.getRequestId())
1079 $response : PciResponseWrapper( getBody().getCommonHeader().getRequestId() == $event.getRequestId() )
1082 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1083 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1084 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
1085 $params.getClosedLoopControlName(), drools.getRule().getName(),
1086 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
1089 // Get the result of the operation
1091 PolicyResult policyResult = $operation.onResponse($response);
1092 if (policyResult != null) {
1093 logger.debug("{}: {}: operation finished - result={}",
1094 $params.getClosedLoopControlName(), drools.getRule().getName(),
1098 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
1100 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1101 notification.setFrom("policy");
1102 notification.setPolicyName(drools.getRule().getName());
1103 notification.setPolicyScope("${policyScope}");
1104 notification.setPolicyVersion("${policyVersion}");
1105 notification.setMessage($operation.getOperationHistory());
1106 notification.setHistory($operation.getHistory());
1107 if (policyResult.equals(PolicyResult.SUCCESS)) {
1108 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
1110 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
1112 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1114 // Ensure the operation is complete
1116 if ($operation.isOperationComplete()) {
1118 // It is complete, remove it from memory
1120 retract($operation);
1122 // We must also retract the timer object
1123 // NOTE: We could write a Rule to do this
1127 // Complete the operation
1129 modify($manager) {finishOperation($operation)};
1132 // Just doing this will kick off the LOCKED rule again
1134 modify($operation) {};
1138 // Its not finished yet (i.e. expecting more Response objects)
1140 // Or possibly it is a leftover response that we timed the request out previously
1144 // We are going to retract these objects from memory
1151 * Clean Up any lingering SDNR reponses
1154 rule "${policyName}.SDNR.RESPONSE.CLEANUP"
1156 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1157 $response : PciResponseWrapper($id : getBody().getCommonHeader().getRequestId )
1158 not ( VirtualControlLoopEvent( requestId == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
1161 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1162 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1163 logger.debug("{}: {}: orphan SDNR response={}",
1164 $params.getClosedLoopControlName(), drools.getRule().getName(), $id);
1173 * This rule responds to SO Response Events
1176 rule "${policyName}.SO.RESPONSE"
1178 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1179 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
1180 closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1181 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1182 requestId == $event.getRequestId() )
1183 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
1184 onset.getRequestId() == $event.getRequestId() )
1185 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1186 requestId == $event.getRequestId().toString(), timerType == "Operation", !expired )
1187 $lock : TargetLock (requestId == $event.getRequestId())
1188 $response : SoResponseWrapper(requestId.toString() == $event.getRequestId().toString() )
1191 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1192 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1193 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
1194 $params.getClosedLoopControlName(), drools.getRule().getName(),
1195 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
1197 // Get the result of the operation
1199 PolicyResult policyResult = $operation.onResponse($response);
1200 if (policyResult != null) {
1201 logger.debug("{}: {}: operation finished - result={}",
1202 $params.getClosedLoopControlName(), drools.getRule().getName(),
1206 // This Operation has completed, construct a notification showing our results
1208 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1209 notification.setFrom("policy");
1210 notification.setPolicyName(drools.getRule().getName());
1211 notification.setPolicyScope("${policyScope}");
1212 notification.setPolicyVersion("${policyVersion}");
1213 notification.setMessage($operation.getOperationHistory());
1214 notification.setHistory($operation.getHistory());
1215 if (policyResult.equals(PolicyResult.SUCCESS)) {
1216 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
1218 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
1221 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1223 // Ensure the operation is complete
1225 if ($operation.isOperationComplete() == true) {
1227 // It is complete, remove it from memory
1229 retract($operation);
1231 // We must also retract the timer object
1232 // NOTE: We could write a Rule to do this
1236 // Complete the operation
1238 modify($manager) {finishOperation($operation)};
1241 // Just doing this will kick off the LOCKED rule again
1243 modify($operation) {};
1247 // Its not finished yet (i.e. expecting more Response objects)
1249 // Or possibly it is a leftover response that we timed the request out previously
1253 // We are going to retract these objects from memory
1261 * This rule responds to VFC Response Events
1264 rule "${policyName}.VFC.RESPONSE"
1266 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1267 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
1268 closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1269 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1270 requestId == $event.getRequestId() )
1271 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
1272 onset.getRequestId() == $event.getRequestId() )
1273 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1274 requestId == $event.getRequestId().toString(), timerType == "Operation", !expired )
1275 $lock : TargetLock (requestId == $event.getRequestId())
1276 $response : VfcResponse( requestId.toString() == $event.getRequestId().toString() )
1278 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1279 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1280 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
1281 $params.getClosedLoopControlName(), drools.getRule().getName(),
1282 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
1284 // Get the result of the operation
1286 PolicyResult policyResult = $operation.onResponse($response);
1287 if (policyResult != null) {
1289 // This Operation has completed, construct a notification showing our results
1291 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1292 notification.setFrom("policy");
1293 notification.setPolicyName(drools.getRule().getName());
1294 notification.setPolicyScope("${policyScope}");
1295 notification.setPolicyVersion("${policyVersion}");
1296 notification.setMessage($operation.getOperationHistory());
1297 notification.setHistory($operation.getHistory());
1299 // Ensure the operation is complete
1301 if ($operation.isOperationComplete() == true) {
1303 // It is complete, remove it from memory
1305 retract($operation);
1307 // We must also retract the timer object
1308 // NOTE: We could write a Rule to do this
1312 // Complete the operation
1314 modify($manager) {finishOperation($operation)};
1317 // Just doing this will kick off the LOCKED rule again
1319 modify($operation) {};
1323 // Its not finished yet (i.e. expecting more Response objects)
1325 // Or possibly it is a leftover response that we timed the request out previously
1329 // We are going to retract these objects from memory
1337 * This rule responds to SDNC Response Events
1341 rule "${policyName}.SDNC.RESPONSE"
1343 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1344 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
1345 closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1346 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1347 requestId == $event.getRequestId() )
1348 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
1349 onset.getRequestId() == $event.getRequestId() )
1350 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1351 requestId == $event.getRequestId().toString(), timerType == "Operation", !expired )
1352 $lock : TargetLock (requestId == $event.getRequestId())
1353 $response : SdncResponse( requestId.toString() == $event.getRequestId().toString() )
1355 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1356 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1357 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
1358 $params.getClosedLoopControlName(), drools.getRule().getName(),
1359 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
1361 // Get the result of the operation
1363 PolicyResult policyResult = $operation.onResponse($response);
1364 if (policyResult != null) {
1366 // This Operation has completed, construct a notification showing our results
1368 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1369 notification.setFrom("policy");
1370 notification.setPolicyName(drools.getRule().getName());
1371 notification.setPolicyScope("${policyScope}");
1372 notification.setPolicyVersion("${policyVersion}");
1373 notification.setMessage($operation.getOperationHistory());
1374 notification.setHistory($operation.getHistory());
1376 // Ensure the operation is complete
1378 if ($operation.isOperationComplete()) {
1380 // It is complete, remove it from memory
1382 retract($operation);
1384 // We must also retract the timer object
1385 // NOTE: We could write a Rule to do this
1389 // Complete the operation
1391 modify($manager) {finishOperation($operation)};
1394 // Just doing this will kick off the LOCKED rule again
1396 modify($operation) {};
1400 // Its not finished yet (i.e. expecting more Response objects)
1402 // Or possibly it is a leftover response that we timed the request out previously
1406 // We are going to retract these objects from memory
1414 * This manages a single timer.
1415 * Due to a bug in the drools code, the drools timer needed to be split from most of the objects in the when clause
1418 rule "${policyName}.TIMER.FIRED"
1419 timer (expr: $timeout)
1421 $timer : ControlLoopTimer($timeout : delay, !expired)
1423 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1424 logger.info("This is ${policyName}.TIMER.FIRED");
1425 modify($timer){setExpired(true)};
1430 * This is the timer that manages the timeout for an individual operation.
1433 rule "${policyName}.EVENT.MANAGER.OPERATION.TIMEOUT"
1435 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1436 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
1437 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1438 requestId == $event.getRequestId() )
1439 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
1440 onset.getRequestId() == $event.getRequestId() )
1441 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1442 requestId == $event.getRequestId().toString(), expired, timerType == "Operation" )
1443 $lock : TargetLock (requestId == $event.getRequestId())
1446 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1447 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1448 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={}",
1449 $params.getClosedLoopControlName(), drools.getRule().getName(),
1450 $event, $manager, $operation, $lock, $operation, $opTimer);
1453 // Tell it its timed out
1455 $operation.setOperationHasTimedOut();
1457 // Create a notification for it ("DB Write - end operation")
1459 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1460 notification.setFrom("policy");
1461 notification.setPolicyName(drools.getRule().getName());
1462 notification.setPolicyScope("${policyScope}");
1463 notification.setPolicyVersion("${policyVersion}");
1464 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
1465 notification.setMessage($operation.getOperationHistory());
1466 notification.setHistory($operation.getHistory());
1468 // Let interested parties know
1470 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1472 // Get rid of the timer
1476 // Ensure the operation is complete
1478 if ($operation.isOperationComplete() == true) {
1480 // It is complete, remove it from memory
1482 retract($operation);
1484 // Complete the operation
1486 modify($manager) {finishOperation($operation)};
1489 // Just doing this will kick off the LOCKED rule again
1491 modify($operation) {};
1497 * This is the timer that manages the overall control loop timeout.
1500 rule "${policyName}.EVENT.MANAGER.TIMEOUT"
1502 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1503 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
1504 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1505 requestId == $event.getRequestId() )
1506 $clTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1507 requestId == $event.getRequestId().toString(), expired, timerType == "ClosedLoop" )
1510 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1511 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1513 logger.debug("{}: {}: event={}",
1514 $params.getClosedLoopControlName(), drools.getRule().getName(),
1517 // Tell the Event Manager it has timed out
1519 VirtualControlLoopNotification notification = $manager.setControlLoopTimedOut();
1520 if (notification != null) {
1521 notification.setFrom("policy");
1522 notification.setPolicyName(drools.getRule().getName());
1523 notification.setPolicyScope("${policyScope}");
1524 notification.setPolicyVersion("${policyVersion}");
1526 // Let interested parties know
1528 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1531 // Retract the event
1538 * This rule cleans up the manager and other objects after an event has
1542 rule "${policyName}.EVENT.MANAGER.CLEANUP"
1544 $manager : ControlLoopEventManager( $clName : getClosedLoopControlName(), $requestId : getRequestId() )
1545 $operations : LinkedList()
1546 from collect( ControlLoopOperationManager( onset.closedLoopControlName == $clName,
1547 onset.getRequestId() == $requestId ) )
1548 $timers : LinkedList()
1549 from collect( ControlLoopTimer( closedLoopControlName == $clName,
1550 requestId == $requestId.toString() ) )
1551 $locks : LinkedList()
1552 from collect( TargetLock (requestId == $requestId) )
1553 not( VirtualControlLoopEvent( closedLoopControlName == $clName, requestId == $requestId ) )
1556 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1557 logger.info("{}: {}", $clName, drools.getRule().getName());
1559 logger.debug("{}: {}: manager={} timers={} operations={}",
1560 $clName, drools.getRule().getName(),
1561 $manager, $timers.size(), $operations.size());
1564 // Retract EVERYTHING
1568 for(Object manager: $operations) {
1569 retract((ControlLoopOperationManager) manager);
1571 for(Object timer: $timers) {
1572 retract((ControlLoopTimer) timer);
1574 for(Object lock: $locks) {
1575 TargetLock tgt = (TargetLock) lock;
1577 // Ensure we release the lock
1579 PolicyGuard.unlockTarget(tgt);
1586 * This rule will clean up any rogue onsets where there is no
1587 * ControlLoopParams object corresponding to the onset event.
1590 rule "${policyName}.EVENT.CLEANUP"
1592 $event : VirtualControlLoopEvent( $clName: closedLoopControlName )
1593 not ( Params( getClosedLoopControlName() == $clName) )
1596 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1597 logger.info("{}: {}", $clName, drools.getRule().getName());
1598 logger.debug("{}: {}: orphan onset event={}",
1599 $clName, drools.getRule().getName(), $event);
1605 * Creates a cleaner for every Params object.
1606 * This has a higher salience so that it is fired before PARAMS.FINISHED in ANY policy.
1608 rule "${policyName}.PARAMS.CLEANING"
1612 ParamsInitCleaner( )
1615 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1616 logger.info("{}: {} : YAML=[{}]", $params.getClosedLoopControlName(), drools.getRule().getName(),
1617 $params.getControlLoopYaml());
1619 ParamsCleaner cleaner = new ParamsCleaner();
1620 cleaner.setClosedLoopControlName($params.getClosedLoopControlName());
1621 cleaner.setControlLoopYaml($params.getControlLoopYaml());
1626 * Finished creating cleaner objects, so remove the trigger.
1627 * This has a higher salience so that it is fired before processing any events.
1629 rule "${policyName}.PARAMS.FINISHED"
1632 $initCleaner: ParamsInitCleaner( )
1635 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1636 logger.info("{}: {}", $initCleaner.getClosedLoopControlName(), drools.getRule().getName());
1638 retract($initCleaner);
1642 * Identifies Params objects that are still active, removing their associated cleaners.
1643 * This should only leave one active Params object for each policy.
1644 * This has a higher salience so that it is fired before PARAMS.DELETE in ANY policy.
1646 rule "${policyName}.PARAMS.ACTIVE"
1649 $params: Params( getClosedLoopControlName() == "${closedLoopControlName}",
1650 getControlLoopYaml() == "${controlLoopYaml}" )
1651 $cleaner: ParamsCleaner( getClosedLoopControlName() == "${closedLoopControlName}",
1652 getControlLoopYaml() == "${controlLoopYaml}" )
1655 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1656 logger.info("{}: {} : YAML=[{}]", $params.getClosedLoopControlName(), drools.getRule().getName(),
1657 $params.getControlLoopYaml());
1663 * Delete Params objects that are not active (i.e., those that still have an associated
1665 * This has a higher salience so that it is fired before PARAMS.CLEANED in ANY policy.
1667 rule "${policyName}.PARAMS.DELETE"
1671 $cleaner: ParamsCleaner( getClosedLoopControlName() == $params.getClosedLoopControlName(),
1672 getControlLoopYaml() == $params.getControlLoopYaml() )
1675 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1676 logger.info("{}: {} : YAML=[{}]", $params.getClosedLoopControlName(), drools.getRule().getName(),
1677 $params.getControlLoopYaml());
1683 * Finished clean-up, so delete the cleaner objects.
1684 * This has a higher salience so that it is fired before processing any events.
1686 rule "${policyName}.PARAMS.CLEANED"
1689 $cleaner: ParamsCleaner( )
1692 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1693 logger.info("{}: {} : YAML=[{}]", $cleaner.getClosedLoopControlName(), drools.getRule().getName(),
1694 $cleaner.getControlLoopYaml());