2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Bell Canada.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.controlloop;
24 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput;
26 import org.onap.policy.controlloop.VirtualControlLoopEvent;
27 import org.onap.policy.controlloop.VirtualControlLoopNotification;
28 import org.onap.policy.controlloop.ControlLoopEventStatus;
29 import org.onap.policy.controlloop.ControlLoopNotificationType;
30 import org.onap.policy.controlloop.ControlLoopLogger;
31 import org.onap.policy.controlloop.policy.PolicyResult;
32 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
33 import org.onap.policy.controlloop.policy.Policy;
34 import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager;
35 import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager.NewEventStatus;
36 import org.onap.policy.controlloop.eventmanager.ControlLoopOperationManager;
37 import org.onap.policy.controlloop.eventmanager.LockCallbackWorkingMemory;
38 import org.onap.policy.controlloop.actor.so.SoActorServiceProvider;
39 import org.onap.policy.controlloop.actor.cds.CdsActorServiceProvider;
40 import org.onap.policy.controlloop.actor.cds.CdsActorServiceProvider.CdsActorServiceManager;
41 import org.onap.policy.controlloop.actor.cds.constants.CdsActorConstants;
42 import org.onap.policy.aai.AaiNqResponseWrapper;
43 import org.onap.policy.aai.AaiCqResponse;
44 import org.onap.policy.appc.Request;
45 import org.onap.policy.appc.Response;
46 import org.onap.policy.appc.CommonHeader;
47 import org.onap.policy.appclcm.LcmRequestWrapper;
48 import org.onap.policy.appclcm.LcmResponseWrapper;
49 import org.onap.policy.appclcm.LcmRequest;
50 import org.onap.policy.appclcm.LcmResponse;
51 import org.onap.policy.appclcm.LcmCommonHeader;
52 import org.onap.policy.cds.CdsResponse;
53 import org.onap.policy.cds.client.CdsProcessorGrpcClient;
54 import org.onap.policy.cds.properties.CdsServerProperties;
55 import org.onap.policy.drools.utils.Pair;
56 import org.onap.policy.sdnr.PciRequestWrapper;
57 import org.onap.policy.sdnr.PciResponseWrapper;
58 import org.onap.policy.sdnr.PciRequest;
59 import org.onap.policy.sdnr.PciResponse;
60 import org.onap.policy.vfc.VfcRequest;
61 import org.onap.policy.vfc.VfcResponse;
62 import org.onap.policy.vfc.VfcManager;
63 import org.onap.policy.so.SoManager;
64 import org.onap.policy.so.SoRequest;
65 import org.onap.policy.so.SoResponseWrapper;
66 import org.onap.policy.sdnc.SdncRequest;
67 import org.onap.policy.sdnc.SdncManager;
68 import org.onap.policy.sdnc.SdncResponse;
69 import org.onap.policy.drools.core.lock.Lock;
70 import org.onap.policy.guard.PolicyGuardRequest;
71 import org.onap.policy.guard.PolicyGuardResponse;
72 import org.onap.policy.guard.PolicyGuardXacmlRequestAttributes;
73 import org.onap.policy.guard.PolicyGuardXacmlHelper;
75 import org.yaml.snakeyaml.Yaml;
76 import org.yaml.snakeyaml.constructor.Constructor;
78 import org.slf4j.LoggerFactory;
79 import org.slf4j.Logger;
81 import java.time.Instant;
82 import java.util.LinkedList;
83 import java.util.Iterator;
85 import org.onap.policy.drools.system.PolicyEngineConstants;
88 * This structure mimics the Params structure.
89 * Its only purpose is to allow management of
90 * rules by the PAP component..
91 * It has no use at runtime since the rules go by
92 * Params for matching purposes.
95 closedLoopControlName : String
96 controlLoopYaml : String
100 * Control Loop Identity
103 closedLoopControlName : String
104 controlLoopYaml : String
108 * Used to trigger clean up Params that no longer have associated rules.
110 declare ParamsInitCleaner
111 closedLoopControlName : String // only used when logging
115 * Used to clean up Params that no longer have associated rules.
117 declare ParamsCleaner
118 closedLoopControlName : String
119 controlLoopYaml : String
123 * This object is to provide support for timeouts
124 * due to a bug in drools' built-in timers
126 declare ControlLoopTimer
127 closedLoopControlName : String
131 //timerType is the type of timer: either "ClosedLoop" or "Operation"
137 * Called to insert the parameters into working memory for this Closed Loop policy. This is called
138 * once each time a closed loop is added or its YAML is updated.
139 * This has a higher salience so we can ensure that the Params is created before we have a chance to
140 * discard any events.
143 rule "${policyName}.SETUP"
146 not( Params( getClosedLoopControlName() == "${closedLoopControlName}",
147 getControlLoopYaml() == "${controlLoopYaml}" ) )
150 Params params = new Params();
151 params.setClosedLoopControlName("${closedLoopControlName}");
152 params.setControlLoopYaml("${controlLoopYaml}");
155 ParamsInitCleaner initCleaner = new ParamsInitCleaner();
156 initCleaner.setClosedLoopControlName("${closedLoopControlName}");
159 // Note: globals have bad behavior when persistence is used,
160 // hence explicitly getting the logger vs using a global
162 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
163 logger.info("{}: {} : YAML=[{}]", params.getClosedLoopControlName(), drools.getRule().getName(),
164 params.getControlLoopYaml());
169 * This rule responds to DCAE Events where there is no manager yet. Either it is
170 * the first ONSET, or a subsequent badly formed Event (i.e. Syntax error, or is-closed-loop-disabled)
173 rule "${policyName}.EVENT"
175 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
176 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
177 not ( ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
178 requestId == $event.getRequestId() ) )
181 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
182 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
187 // Check the event, because we need it to not be null when
188 // we create the ControlLoopEventManager. The ControlLoopEventManager
189 // will do extra syntax checking as well check if the closed loop is disabled.
191 if ($event.getRequestId() == null) {
192 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
193 notification.setNotification(ControlLoopNotificationType.REJECTED);
194 notification.setFrom("policy");
195 notification.setMessage("Missing requestId");
196 notification.setPolicyName(drools.getRule().getName());
197 notification.setPolicyScope("${policyScope}");
198 notification.setPolicyVersion("${policyVersion}");
201 // Let interested parties know
203 PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
206 // Retract it from memory
209 } else if ($event.getClosedLoopEventStatus() != ControlLoopEventStatus.ONSET) {
210 throw new ControlLoopException($event.getClosedLoopEventStatus() + " received with no prior onset");
213 // Create an EventManager
215 ControlLoopEventManager manager = new ControlLoopEventManager($params.getClosedLoopControlName(),
216 $event.getRequestId());
218 // Determine if EventManager can actively process the event
219 // (i.e. syntax, is_closed_loop_disabled checks etc.)
221 VirtualControlLoopNotification notification = manager.activate($params.getControlLoopYaml(), $event);
222 notification.setFrom("pdp-0001-controller=controlloop"); // Engine.getInstanceName()
223 notification.setPolicyName(drools.getRule().getName());
224 notification.setPolicyScope("${policyScope}");
225 notification.setPolicyVersion("${policyVersion}");
227 // Are we actively pursuing this event?
229 if (notification.getNotification() == ControlLoopNotificationType.ACTIVE) {
231 // Insert Event Manager into memory, this will now kick off processing.
235 // Let interested parties know
237 PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
239 // Setup the Overall Control Loop timer
241 ControlLoopTimer clTimer = new ControlLoopTimer();
242 clTimer.setTimerType("ClosedLoop");
243 clTimer.setClosedLoopControlName($event.getClosedLoopControlName());
244 clTimer.setRequestId($event.getRequestId().toString());
245 clTimer.setDelay(manager.getControlLoopTimeout(1500) + "s");
252 // Let interested parties know
254 PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
256 // Retract it from memory
262 // Now that the manager is inserted into Drools working memory, we'll wait for
263 // another rule to fire in order to continue processing. This way we can also
264 // then screen for additional ONSET and ABATED events for this RequestId.
267 } catch (Exception e) {
268 logger.warn("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName(), e);
270 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
271 notification.setNotification(ControlLoopNotificationType.REJECTED);
272 notification.setMessage("Exception occurred: " + e.getMessage());
273 notification.setPolicyName(drools.getRule().getName());
274 notification.setPolicyScope("${policyScope}");
275 notification.setPolicyVersion("${policyVersion}");
279 PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
289 * This rule happens when we got a valid ONSET, closed loop is enabled and an Event Manager
290 * is now created. We can start processing the yaml specification via the Event Manager.
293 rule "${policyName}.EVENT.MANAGER"
295 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
296 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
297 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
298 requestId == $event.getRequestId() )
299 $clTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
300 requestId == $event.getRequestId().toString(), timerType == "ClosedLoop", !expired )
303 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
304 logger.info("{}: {}: event={} manager={} clTimer={}",
305 $params.getClosedLoopControlName(), drools.getRule().getName(),
306 $event, $manager, $clTimer);
310 // Check which event this is.
312 ControlLoopEventManager.NewEventStatus eventStatus = $manager.onNewEvent($event);
314 // Check what kind of event this is
316 if (eventStatus == NewEventStatus.SUBSEQUENT_ONSET) {
318 // We don't care about subsequent onsets
320 logger.info("{}: {}: subsequent onset",
321 $params.getClosedLoopControlName(), drools.getRule().getName());
325 if (eventStatus == NewEventStatus.SYNTAX_ERROR) {
327 // Ignore any bad syntax events
329 logger.warn("{}: {}: syntax error",
330 $params.getClosedLoopControlName(), drools.getRule().getName());
335 // We only want the initial ONSET event in memory,
336 // all the other events need to be retracted to support
337 // cleanup and avoid the other rules being fired for this event.
339 if (eventStatus != NewEventStatus.FIRST_ONSET) {
340 logger.warn("{}: {}: not first onset",
341 $params.getClosedLoopControlName(), drools.getRule().getName());
345 logger.debug("{}: {}: target={}", $params.getClosedLoopControlName(),
346 drools.getRule().getName(), $event.getTarget());
348 // Now start seeing if we need to process this event
352 // Check if this is a Final Event
354 VirtualControlLoopNotification notification = $manager.isControlLoopFinal();
357 if (notification != null) {
359 // Its final, but are we waiting for abatement?
361 if ($manager.getNumAbatements() > 0) {
362 logger.info("{}: {}: abatement received for {}. Closing the control loop",
363 $params.getClosedLoopControlName(), drools.getRule().getName(),
364 $event.getRequestId());
366 /// DB Write---end event processing for this RequestId()
367 $manager.commitAbatement("Event Abated","Closed");
369 notification.setFrom("policy");
370 notification.setPolicyName(drools.getRule().getName());
371 notification.setPolicyScope("${policyScope}");
372 notification.setPolicyVersion("${policyVersion}");
374 // In this case, we are done
376 PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
380 Lock lock = $manager.unlockCurrentOperation();
382 logger.debug("{}: {}: retracting lock={}", $params.getClosedLoopControlName(),
383 drools.getRule().getName(), lock);
387 // Retract everything from memory
389 logger.info("{}: {}: retracting onset, manager, and timer",
390 $params.getClosedLoopControlName(), drools.getRule().getName());
392 retract($manager.getOnsetEvent());
394 // don't retract manager, etc. - a clean-up rule will do that
397 // TODO - what if we get subsequent Events for this RequestId?
398 // By default, it will all start over again. May be confusing for Ruby.
399 // Or, we could track this and then subsequently ignore the events
403 // Check whether we need to wait for abatement
405 if ($manager.getProcessor().getControlLoop().getAbatement() == true && notification.getNotification() == ControlLoopNotificationType.FINAL_SUCCESS) {
406 logger.info("{}: {}: waiting for abatement ..",
407 $params.getClosedLoopControlName(), drools.getRule().getName());
409 logger.info("{}: {}: no abatement expect for {}. Closing the control loop",
410 $params.getClosedLoopControlName(), drools.getRule().getName(),
411 $event.getRequestId());
413 notification.setFrom("policy");
414 notification.setPolicyName(drools.getRule().getName());
415 notification.setPolicyScope("${policyScope}");
416 notification.setPolicyVersion("${policyVersion}");
419 // In this case, we are done
421 PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
425 Lock lock = $manager.unlockCurrentOperation();
427 logger.debug("{}: {}: retracting lock={}", $params.getClosedLoopControlName(),
428 drools.getRule().getName(), lock);
432 // Retract everything from memory
434 logger.info("{}: {}: retracting onset, manager, and timer",
435 $params.getClosedLoopControlName(), drools.getRule().getName());
437 retract($manager.getOnsetEvent());
439 // don't retract manager, etc. - a clean-up rule will do that
444 // NOT final, so let's ask for the next operation
446 ControlLoopOperationManager operation = $manager.processControlLoop();
447 if (operation != null) {
449 // Let's ask for a lock right away
451 logger.info("{}: {}: requesting lock for operation={}",
452 $params.getClosedLoopControlName(), drools.getRule().getName(),
455 Pair<Lock,Lock> oldNew = $manager.lockCurrentOperation(
456 new LockCallbackWorkingMemory($params.getClosedLoopControlName(), drools.getWorkingMemory()));
457 if(oldNew.first() != null) {
458 logger.debug("{}: {}: retracting lock={}", $params.getClosedLoopControlName(),
459 drools.getRule().getName(), oldNew.first());
460 retract(oldNew.first());
462 if(oldNew.second() != null) {
463 logger.debug("{}: {}: inserting lock={}", $params.getClosedLoopControlName(),
464 drools.getRule().getName(), oldNew.second());
465 insert(oldNew.second());
469 // insert the operation into memory
473 // insert operation timeout object
475 ControlLoopTimer opTimer = new ControlLoopTimer();
476 opTimer.setTimerType("Operation");
477 opTimer.setClosedLoopControlName($event.getClosedLoopControlName());
478 opTimer.setRequestId($event.getRequestId().toString());
479 Integer timeout = operation.getOperationTimeout();
480 opTimer.setDelay(timeout > 0 ? timeout.toString() + "s" : $clTimer.getDelay());
485 // Probably waiting for abatement
487 logger.info("{}: {}: no operation, probably waiting for abatement",
488 $params.getClosedLoopControlName(), drools.getRule().getName());
491 } catch (Exception e) {
492 logger.warn("{}: {}: unexpected",
493 $params.getClosedLoopControlName(),
494 drools.getRule().getName(), e);
496 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
497 notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
498 notification.setMessage(e.getMessage());
499 notification.setFrom("policy");
500 notification.setPolicyName(drools.getRule().getName());
501 notification.setPolicyScope("${policyScope}");
502 notification.setPolicyVersion("${policyVersion}");
504 PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
508 // don't retract manager, etc. - a clean-up rule will do that
518 rule "${policyName}.EVENT.MANAGER.OPERATION.LOCK.DENIED"
520 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
521 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
522 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
523 requestId == $event.getRequestId() )
524 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
525 onset.getRequestId() == $event.getRequestId(), "None".equalsIgnoreCase(getGuardApprovalStatus()) )
526 $lock : Lock (ownerKey == $event.getRequestId().toString(), isUnavailable())
529 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
530 logger.info("{}: {}: event={} manager={} operation={} lock={}",
531 $params.getClosedLoopControlName(), drools.getRule().getName(),
532 $event, $manager, $operation, $lock);
534 logger.debug("The target resource {} is already processing",
535 $event.getAai().get($event.getTarget()));
536 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
537 notification.setNotification(ControlLoopNotificationType.REJECTED);
538 notification.setMessage("The target " + $event.getAai().get($event.getTarget())
539 + " is already locked");
540 notification.setFrom("policy");
541 notification.setPolicyName(drools.getRule().getName());
542 notification.setPolicyScope("${policyScope}");
543 notification.setPolicyVersion("${policyVersion}");
545 PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
549 // don't retract manager, etc. - a clean-up rule will do that
554 * Guard Permitted, let's send request to the actor.
557 rule "${policyName}.EVENT.MANAGER.OPERATION.LOCKED.GUARD_PERMITTED"
559 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
560 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
561 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
562 requestId == $event.getRequestId() )
563 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
564 onset.getRequestId() == $event.getRequestId(), "Permit".equalsIgnoreCase(getGuardApprovalStatus()) )
565 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
566 requestId == $event.getRequestId().toString(), timerType == "Operation", !expired )
569 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
570 logger.info("{}: {}: event={} manager={} operation={}",
571 $params.getClosedLoopControlName(), drools.getRule().getName(),
572 $event, $manager, $operation);
574 Object request = null;
575 boolean caughtException = false;
578 request = $operation.startOperation($event);
580 if (request != null) {
581 logger.debug("{}: {}: starting operation ..",
582 $params.getClosedLoopControlName(), drools.getRule().getName());
584 // Tell interested parties we are performing this Operation
586 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
587 notification.setNotification(ControlLoopNotificationType.OPERATION);
588 notification.setMessage($operation.getOperationMessage());
589 notification.setHistory($operation.getHistory());
590 notification.setFrom("policy");
591 notification.setPolicyName(drools.getRule().getName());
592 notification.setPolicyScope("${policyScope}");
593 notification.setPolicyVersion("${policyVersion}");
595 PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
597 switch ($operation.policy.getActor()){
600 if (request instanceof Request) {
601 PolicyEngineConstants.getManager().deliver("APPC-CL", request);
603 else if (request instanceof LcmRequestWrapper) {
604 PolicyEngineConstants.getManager().deliver("APPC-LCM-READ", request);
608 // at this point the AAI named query request should have already been made,
609 // the response recieved and used
610 // in the construction of the SO Request which is stored in operationRequest
612 if(request instanceof SoRequest) {
613 // Call SO. The response will be inserted into memory once it's received
614 class mySoCallback implements SoManager.SoCallback {
615 public void onSoResponseWrapper(SoResponseWrapper wrapper) {
616 drools.getWorkingMemory().insert(wrapper);
619 SoActorServiceProvider.sendRequest($event.getRequestId().toString(),
622 PolicyEngineConstants.getManager().getEnvironmentProperty("so.url"),
623 PolicyEngineConstants.getManager().getEnvironmentProperty("so.username"),
624 PolicyEngineConstants.getManager().getEnvironmentProperty("so.password"));
628 if (request instanceof VfcRequest) {
630 class myVfcCallback implements VfcManager.VfcCallback {
632 public void onResponse(VfcResponse responseError) {
633 drools.getWorkingMemory().insert(responseError);
637 Thread t = new Thread(new VfcManager(new myVfcCallback(),
639 PolicyEngineConstants.getManager().getEnvironmentProperty("vfc.url"),
640 PolicyEngineConstants.getManager().getEnvironmentProperty("vfc.username"),
641 PolicyEngineConstants.getManager().getEnvironmentProperty("vfc.password")));
647 if (request instanceof SdncRequest) {
649 class mySdncCallback implements SdncManager.SdncCallback {
650 public void onCallback(SdncResponse response) {
651 drools.getWorkingMemory().insert(response);
655 Thread t = new Thread(new SdncManager(new mySdncCallback(),
656 (SdncRequest)request,
657 PolicyEngineConstants.getManager().getEnvironmentProperty("sdnc.url"),
658 PolicyEngineConstants.getManager().getEnvironmentProperty("sdnc.username"),
659 PolicyEngineConstants.getManager().getEnvironmentProperty("sdnc.password")));
664 if (request instanceof PciRequestWrapper) {
665 PolicyEngineConstants.getManager().deliver("SDNR-CL", request);
671 if(request instanceof ExecutionServiceInput) {
673 // Instantiate cds actor, service manager and grpc properties
675 CdsActorServiceProvider cdsActor = new CdsActorServiceProvider();
676 CdsActorServiceProvider.CdsActorServiceManager cdsActorServiceManager = cdsActor.new CdsActorServiceManager();
678 CdsServerProperties props = new CdsServerProperties();
679 props.setHost(PolicyEngineConstants.getManager().getEnvironmentProperty("cds.grpcHost"));
680 props.setPort(Integer.parseInt(PolicyEngineConstants.getManager().getEnvironmentProperty("cds.grpcPort")));
681 props.setUsername(PolicyEngineConstants.getManager().getEnvironmentProperty("cds.grpcUsername"));
682 props.setPassword(PolicyEngineConstants.getManager().getEnvironmentProperty("cds.grpcPassword"));
683 props.setTimeout(Integer.parseInt(PolicyEngineConstants.getManager().getEnvironmentProperty("cds.grpcTimeout")));
685 // Send cds grpc request
686 try (CdsProcessorGrpcClient client = new CdsProcessorGrpcClient(cdsActorServiceManager, props)) {
687 CdsResponse response =
688 cdsActorServiceManager.sendRequestToCds(client, props, (ExecutionServiceInput) request);
689 logger.info("CDS response: {}", response);
699 // What happens if its null?
701 logger.warn("{}: {}: unexpected null operation request",
702 $params.getClosedLoopControlName(),
703 drools.getRule().getName());
704 if ("SO".equals($operation.policy.getActor())) {
707 modify($manager) {finishOperation($operation)};
709 else if ("vfc".equalsIgnoreCase($operation.policy.getActor())) {
712 modify($manager) {finishOperation($operation)};
714 else if ("sdnc".equalsIgnoreCase($operation.policy.getActor())) {
717 modify($manager) {finishOperation($operation)};
721 } catch (Exception e) {
722 String msg = e.getMessage();
723 logger.warn("{}: {}: operation={}: AAI failure: {}",
724 $params.getClosedLoopControlName(), drools.getRule().getName(),
726 $operation.setOperationHasException(msg);
728 if(request != null) {
730 // Create a notification for it ("DB Write - end operation")
732 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
733 notification.setFrom("policy");
734 notification.setPolicyName(drools.getRule().getName());
735 notification.setPolicyScope("${policyScope}");
736 notification.setPolicyVersion("${policyVersion}");
737 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
738 notification.setMessage($operation.getOperationHistory());
739 notification.setHistory($operation.getHistory());
741 PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
746 caughtException = true;
749 // Having the modify statement in the catch clause doesn't work for whatever reason
750 if (caughtException) {
751 modify($manager) {finishOperation($operation)};
758 * We were able to acquire a lock so now let's ask Xacml Guard whether
759 * we are allowed to proceed with the request to the actor.
762 rule "${policyName}.EVENT.MANAGER.OPERATION.LOCKED.GUARD_NOT_YET_QUERIED"
764 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
765 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
766 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
767 requestId == $event.getRequestId() )
768 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
769 onset.getRequestId() == $event.getRequestId(), "None".equalsIgnoreCase(getGuardApprovalStatus()) )
770 $lock : Lock (ownerKey == $event.getRequestId().toString(), isActive())
773 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
774 logger.info("{}: {}: event={} manager={} operation={} lock={}",
775 $params.getClosedLoopControlName(), drools.getRule().getName(),
776 $event, $manager, $operation, $lock);
779 // Sending notification that we are about to query Guard ("DB write - start operation")
781 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
782 notification.setNotification(ControlLoopNotificationType.OPERATION);
783 notification.setMessage("Sending guard query for " + $operation.policy.getActor() + " "
784 + $operation.policy.getRecipe());
785 notification.setHistory($operation.getHistory());
786 notification.setFrom("policy");
787 notification.setPolicyName(drools.getRule().getName());
788 notification.setPolicyScope("${policyScope}");
789 notification.setPolicyVersion("${policyVersion}");
791 PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
794 // Now send Guard Request to XACML Guard. In order to bypass the call to Guard,
795 // just change guardEnabled to false.
797 // In order to use REST XACML, provide a URL instead of "" as a second argument
798 // to the CallGuardTask() and set the first argument to null
799 // (instead of XacmlPdpEngine).
802 // NOTE: The environment properties uses "guard.disabled" but the boolean is guardEnabled
803 boolean guardEnabled = "false".equalsIgnoreCase(PolicyEngineConstants.getManager().getEnvironmentProperty("guard.disabled"));
804 boolean cqEnabled = "true".equalsIgnoreCase(PolicyEngineConstants.getManager().getEnvironmentProperty("aai.customQuery"));
808 Thread t = new Thread(new org.onap.policy.guard.CallGuardTask(
809 drools.getWorkingMemory(),
810 $event.getClosedLoopControlName(),
811 $operation.policy.getActor().toString(),
812 $operation.policy.getRecipe(),
813 $operation.getTargetEntity(),
814 $event.getRequestId().toString(),
817 AaiNqResponseWrapper resp = $manager.getNqVserverFromAai();
818 return(resp == null ? null : resp.countVfModules());
821 AaiCqResponse resp_cq = $manager.getCqResponse($event);
822 if (resp_cq == null){
825 String custId = $operation.policy.getTarget().getModelCustomizationId();
826 String invId = $operation.policy.getTarget().getModelInvariantId();
827 String verId = $operation.policy.getTarget().getModelVersionId();
828 return resp_cq.getVfModuleCount(custId, invId, verId);
830 } catch (Exception e){
831 logger.warn("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName(), e);
839 insert(new PolicyGuardResponse("Permit", $event.getRequestId(), $operation.policy.getRecipe()));
845 // This rule will be triggered when a thread talking to the XACML Guard inserts a
846 // guardResponse object into the working memory
848 rule "${policyName}.GUARD.RESPONSE"
850 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
851 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
852 closedLoopEventStatus == ControlLoopEventStatus.ONSET )
853 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
854 requestId == $event.getRequestId() )
855 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
856 onset.getRequestId() == $event.getRequestId() )
857 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
858 requestId == $event.getRequestId().toString(), timerType == "Operation", !expired )
859 $guardResponse : PolicyGuardResponse(requestId == $event.getRequestId(), $operation.policy.recipe == operation)
862 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
863 logger.info("{}: {}: event={} manager={} operation={} opTimer={} guardResponse={}",
864 $params.getClosedLoopControlName(), drools.getRule().getName(),
865 $event, $manager, $operation, $opTimer, $guardResponse);
868 //we will permit the operation if there was no Guard for it
869 if("Indeterminate".equalsIgnoreCase($guardResponse.getResult())){
870 $guardResponse.setResult("Permit");
874 // This notification has Guard result in "message". ("DB write - end operation in case of Guard Deny")
876 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
877 notification.setNotification(ControlLoopNotificationType.OPERATION);
878 notification.setMessage("Guard result for " + $operation.policy.getActor() + " " + $operation.policy.getRecipe()
879 + " is " + $guardResponse.getResult());
880 notification.setHistory($operation.getHistory());
881 notification.setFrom("policy");
882 notification.setPolicyName(drools.getRule().getName());
883 notification.setPolicyScope("${policyScope}");
884 notification.setPolicyVersion("${policyVersion}");
886 PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
888 if("Permit".equalsIgnoreCase($guardResponse.getResult())){
890 modify($operation){setGuardApprovalStatus($guardResponse.getResult())};
893 //This is the Deny case
894 $operation.startOperation($event);
895 $operation.setOperationHasGuardDeny();
898 modify($manager) {finishOperation($operation)};
901 retract($guardResponse);
907 * This rule responds to APPC Response Events
909 * I would have like to be consistent and write the Response like this:
910 * $response : Response( CommonHeader.RequestId == $onset.getRequestId() )
912 * However, no compile error was given. But a runtime error was given. I think
913 * because drools is confused between the classname CommonHeader vs the property CommonHeader.
916 rule "${policyName}.APPC.RESPONSE"
918 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
919 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
920 closedLoopEventStatus == ControlLoopEventStatus.ONSET )
921 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
922 requestId == $event.getRequestId() )
923 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
924 onset.getRequestId() == $event.getRequestId() )
925 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
926 requestId == $event.getRequestId().toString(), timerType == "Operation", !expired )
927 $response : Response( getCommonHeader().RequestId == $event.getRequestId() )
930 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
931 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
932 logger.debug("{}: {}: event={} manager={} operation={} opTimer={} response={}",
933 $params.getClosedLoopControlName(), drools.getRule().getName(),
934 $event, $manager, $operation, $opTimer, $response);
936 // Get the result of the operation
938 PolicyResult policyResult = $operation.onResponse($response);
939 if (policyResult != null) {
940 logger.debug("{}: {}: operation finished - result={}",
941 $params.getClosedLoopControlName(), drools.getRule().getName(),
944 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
946 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
947 notification.setFrom("policy");
948 notification.setPolicyName(drools.getRule().getName());
949 notification.setPolicyScope("${policyScope}");
950 notification.setPolicyVersion("${policyVersion}");
951 notification.setMessage($operation.getOperationHistory());
952 notification.setHistory($operation.getHistory());
953 if (policyResult.equals(PolicyResult.SUCCESS)) {
954 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
956 // Let interested parties know
958 PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
960 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
962 // Let interested parties know
964 PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
967 // Ensure the operation is complete
969 if ($operation.isOperationComplete() == true) {
971 // It is complete, remove it from memory
975 // We must also retract the timer object
976 // NOTE: We could write a Rule to do this
980 // Complete the operation
982 modify($manager) {finishOperation($operation)};
985 // Just doing this will kick off the LOCKED rule again
987 modify($operation) {};
991 // Its not finished yet (i.e. expecting more Response objects)
993 // Or possibly it is a leftover response that we timed the request out previously
997 // We are going to retract these objects from memory
1004 * The problem with Responses is that they don't have a controlLoopControlName
1005 * field in them, so the only way to attach them is via RequestId. If we have multiple
1006 * control loop .drl's loaded in the same container, we need to be sure the cleanup
1007 * rules don't remove Responses for other control loops.
1010 rule "${policyName}.APPC.RESPONSE.CLEANUP"
1012 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1013 $response : Response($id : getCommonHeader().RequestId )
1014 not ( VirtualControlLoopEvent( requestId == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
1017 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1018 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1019 logger.debug("{}: {}: orphan appc response={}",
1020 $params.getClosedLoopControlName(), drools.getRule().getName(), $id);
1030 * This rule responds to APPC Response Events using the new LCM interface provided by appc
1033 rule "${policyName}.APPC.LCM.RESPONSE"
1035 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1036 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
1037 closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1038 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1039 requestId == $event.getRequestId() )
1040 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
1041 onset.getRequestId() == $event.getRequestId() )
1042 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1043 requestId == $event.getRequestId().toString(), timerType == "Operation", !expired )
1044 $response : LcmResponseWrapper( getBody().getCommonHeader().getRequestId() == $event.getRequestId() )
1047 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1048 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1049 logger.debug("{}: {}: event={} manager={} operation={} opTimer={} response={}",
1050 $params.getClosedLoopControlName(), drools.getRule().getName(),
1051 $event, $manager, $operation, $operation, $opTimer, $response);
1054 // Get the result of the operation
1056 PolicyResult policyResult = $operation.onResponse($response);
1057 if (policyResult != null) {
1058 logger.debug("{}: {}: operation finished - result={}",
1059 $params.getClosedLoopControlName(), drools.getRule().getName(),
1063 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
1065 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1066 notification.setFrom("policy");
1067 notification.setPolicyName(drools.getRule().getName());
1068 notification.setPolicyScope("${policyScope}");
1069 notification.setPolicyVersion("${policyVersion}");
1070 notification.setMessage($operation.getOperationHistory());
1071 notification.setHistory($operation.getHistory());
1072 if (policyResult.equals(PolicyResult.SUCCESS)) {
1073 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
1075 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
1077 PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
1079 // Ensure the operation is complete
1081 if ($operation.isOperationComplete() == true) {
1083 // It is complete, remove it from memory
1085 retract($operation);
1087 // We must also retract the timer object
1088 // NOTE: We could write a Rule to do this
1092 // Complete the operation
1094 modify($manager) {finishOperation($operation)};
1097 // Just doing this will kick off the LOCKED rule again
1099 modify($operation) {};
1103 // Its not finished yet (i.e. expecting more Response objects)
1105 // Or possibly it is a leftover response that we timed the request out previously
1109 // We are going to retract these objects from memory
1116 * Clean Up any lingering LCM reponses
1119 rule "${policyName}.APPC.LCM.RESPONSE.CLEANUP"
1121 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1122 $response : LcmResponseWrapper($id : getBody().getCommonHeader().getRequestId )
1123 not ( VirtualControlLoopEvent( requestId == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
1126 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1127 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1128 logger.debug("{}: {}: orphan appc response={}",
1129 $params.getClosedLoopControlName(), drools.getRule().getName(), $id);
1138 * This rule responds to SDNR Response Events using the new interface provided by SDNR
1141 rule "${policyName}.SDNR.RESPONSE"
1143 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1144 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
1145 closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1146 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1147 requestId == $event.getRequestId() )
1148 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
1149 onset.getRequestId() == $event.getRequestId() )
1150 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1151 requestId == $event.getRequestId().toString(), timerType == "Operation", !expired )
1152 $response : PciResponseWrapper( getBody().getCommonHeader().getRequestId() == $event.getRequestId() )
1155 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1156 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1157 logger.debug("{}: {}: event={} manager={} operation={} opTimer={} response={}",
1158 $params.getClosedLoopControlName(), drools.getRule().getName(),
1159 $event, $manager, $operation, $operation, $opTimer, $response);
1162 // Get the result of the operation
1164 PolicyResult policyResult = $operation.onResponse($response);
1165 if (policyResult != null) {
1166 logger.debug("{}: {}: operation finished - result={}",
1167 $params.getClosedLoopControlName(), drools.getRule().getName(),
1171 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
1173 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1174 notification.setFrom("policy");
1175 notification.setPolicyName(drools.getRule().getName());
1176 notification.setPolicyScope("${policyScope}");
1177 notification.setPolicyVersion("${policyVersion}");
1178 notification.setMessage($operation.getOperationHistory());
1179 notification.setHistory($operation.getHistory());
1180 if (policyResult.equals(PolicyResult.SUCCESS)) {
1181 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
1183 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
1185 PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
1187 // Ensure the operation is complete
1189 if ($operation.isOperationComplete()) {
1191 // It is complete, remove it from memory
1193 retract($operation);
1195 // We must also retract the timer object
1196 // NOTE: We could write a Rule to do this
1200 // Complete the operation
1202 modify($manager) {finishOperation($operation)};
1205 // Just doing this will kick off the LOCKED rule again
1207 modify($operation) {};
1211 // Its not finished yet (i.e. expecting more Response objects)
1213 // Or possibly it is a leftover response that we timed the request out previously
1217 // We are going to retract these objects from memory
1224 * Clean Up any lingering SDNR reponses
1227 rule "${policyName}.SDNR.RESPONSE.CLEANUP"
1229 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1230 $response : PciResponseWrapper($id : getBody().getCommonHeader().getRequestId )
1231 not ( VirtualControlLoopEvent( requestId == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
1234 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1235 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1236 logger.debug("{}: {}: orphan SDNR response={}",
1237 $params.getClosedLoopControlName(), drools.getRule().getName(), $id);
1246 * This rule responds to SO Response Events
1249 rule "${policyName}.SO.RESPONSE"
1251 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1252 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
1253 closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1254 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1255 requestId == $event.getRequestId() )
1256 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
1257 onset.getRequestId() == $event.getRequestId() )
1258 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1259 requestId == $event.getRequestId().toString(), timerType == "Operation", !expired )
1260 $response : SoResponseWrapper(requestId.toString() == $event.getRequestId().toString() )
1263 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1264 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1265 logger.debug("{}: {}: event={} manager={} operation={} opTimer={} response={}",
1266 $params.getClosedLoopControlName(), drools.getRule().getName(),
1267 $event, $manager, $operation, $operation, $opTimer, $response);
1269 // Get the result of the operation
1271 PolicyResult policyResult = $operation.onResponse($response);
1272 if (policyResult != null) {
1273 logger.debug("{}: {}: operation finished - result={}",
1274 $params.getClosedLoopControlName(), drools.getRule().getName(),
1278 // This Operation has completed, construct a notification showing our results
1280 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1281 notification.setFrom("policy");
1282 notification.setPolicyName(drools.getRule().getName());
1283 notification.setPolicyScope("${policyScope}");
1284 notification.setPolicyVersion("${policyVersion}");
1285 notification.setMessage($operation.getOperationHistory());
1286 notification.setHistory($operation.getHistory());
1287 if (policyResult.equals(PolicyResult.SUCCESS)) {
1288 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
1290 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
1293 PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
1295 // Ensure the operation is complete
1297 if ($operation.isOperationComplete() == true) {
1299 // It is complete, remove it from memory
1301 retract($operation);
1303 // We must also retract the timer object
1304 // NOTE: We could write a Rule to do this
1308 // Complete the operation
1310 modify($manager) {finishOperation($operation)};
1313 // Just doing this will kick off the LOCKED rule again
1315 modify($operation) {};
1319 // Its not finished yet (i.e. expecting more Response objects)
1321 // Or possibly it is a leftover response that we timed the request out previously
1325 // We are going to retract these objects from memory
1333 * This rule responds to VFC Response Events
1336 rule "${policyName}.VFC.RESPONSE"
1338 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1339 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
1340 closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1341 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1342 requestId == $event.getRequestId() )
1343 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
1344 onset.getRequestId() == $event.getRequestId() )
1345 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1346 requestId == $event.getRequestId().toString(), timerType == "Operation", !expired )
1347 $response : VfcResponse( requestId.toString() == $event.getRequestId().toString() )
1349 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1350 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1351 logger.debug("{}: {}: event={} manager={} operation={} opTimer={} response={}",
1352 $params.getClosedLoopControlName(), drools.getRule().getName(),
1353 $event, $manager, $operation, $operation, $opTimer, $response);
1355 // Get the result of the operation
1357 PolicyResult policyResult = $operation.onResponse($response);
1358 if (policyResult != null) {
1360 // This Operation has completed, construct a notification showing our results
1362 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1363 notification.setFrom("policy");
1364 notification.setPolicyName(drools.getRule().getName());
1365 notification.setPolicyScope("${policyScope}");
1366 notification.setPolicyVersion("${policyVersion}");
1367 notification.setMessage($operation.getOperationHistory());
1368 notification.setHistory($operation.getHistory());
1370 // Ensure the operation is complete
1372 if ($operation.isOperationComplete() == true) {
1374 // It is complete, remove it from memory
1376 retract($operation);
1378 // We must also retract the timer object
1379 // NOTE: We could write a Rule to do this
1383 // Complete the operation
1385 modify($manager) {finishOperation($operation)};
1388 // Just doing this will kick off the LOCKED rule again
1390 modify($operation) {};
1394 // Its not finished yet (i.e. expecting more Response objects)
1396 // Or possibly it is a leftover response that we timed the request out previously
1400 // We are going to retract these objects from memory
1408 * This rule responds to SDNC Response Events
1412 rule "${policyName}.SDNC.RESPONSE"
1414 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1415 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
1416 closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1417 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1418 requestId == $event.getRequestId() )
1419 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
1420 onset.getRequestId() == $event.getRequestId() )
1421 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1422 requestId == $event.getRequestId().toString(), timerType == "Operation", !expired )
1423 $response : SdncResponse( requestId.toString() == $event.getRequestId().toString() )
1425 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1426 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1427 logger.debug("{}: {}: event={} manager={} operation={} opTimer={} response={}",
1428 $params.getClosedLoopControlName(), drools.getRule().getName(),
1429 $event, $manager, $operation, $operation, $opTimer, $response);
1431 // Get the result of the operation
1433 PolicyResult policyResult = $operation.onResponse($response);
1434 if (policyResult != null) {
1436 // This Operation has completed, construct a notification showing our results
1438 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1439 notification.setFrom("policy");
1440 notification.setPolicyName(drools.getRule().getName());
1441 notification.setPolicyScope("${policyScope}");
1442 notification.setPolicyVersion("${policyVersion}");
1443 notification.setMessage($operation.getOperationHistory());
1444 notification.setHistory($operation.getHistory());
1446 // Ensure the operation is complete
1448 if ($operation.isOperationComplete()) {
1450 // It is complete, remove it from memory
1452 retract($operation);
1454 // We must also retract the timer object
1455 // NOTE: We could write a Rule to do this
1459 // Complete the operation
1461 modify($manager) {finishOperation($operation)};
1464 // Just doing this will kick off the LOCKED rule again
1466 modify($operation) {};
1470 // Its not finished yet (i.e. expecting more Response objects)
1472 // Or possibly it is a leftover response that we timed the request out previously
1476 // We are going to retract these objects from memory
1484 * This rule responds to CDS Response Events
1486 rule "${policyName}.CDS.RESPONSE"
1488 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1489 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
1490 closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1491 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1492 requestId == $event.getRequestId() )
1493 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
1494 onset.getRequestId() == $event.getRequestId() )
1495 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1496 requestId == $event.getRequestId().toString(), timerType == "Operation", !expired )
1497 $response : CdsResponse( requestId == $event.getRequestId().toString() )
1501 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1502 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1503 logger.debug("{}: {}: event={} manager={} operation={} opTimer={} response={}",
1504 $params.getClosedLoopControlName(), drools.getRule().getName(),
1505 $event, $manager, $operation, $operation, $opTimer, $response);
1507 // Get the result of the operation
1508 PolicyResult policyResult = $operation.onResponse($response);
1510 if (policyResult != null) {
1511 logger.debug("{}: {}: operation finished - result={}",
1512 $params.getClosedLoopControlName(), drools.getRule().getName(),
1515 // The operation has completed, construct a notification showing our results
1516 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1517 notification.setFrom("policy");
1518 notification.setPolicyName(drools.getRule().getName());
1519 notification.setPolicyScope("${policyScope}");
1520 notification.setPolicyVersion("${policyVersion}");
1521 notification.setMessage($operation.getOperationHistory());
1522 notification.setHistory($operation.getHistory());
1523 notification.setNotification(
1524 ($response != null && CdsActorConstants.SUCCESS.equals($response.getStatus()))
1525 ? ControlLoopNotificationType.OPERATION_SUCCESS : ControlLoopNotificationType.OPERATION_FAILURE);
1527 // Send the notification
1528 PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
1530 // Ensure the operation is complete
1531 if ($operation.isOperationComplete()) {
1533 // It is complete, remove it from memory
1534 retract($operation);
1536 // We must also retract the timer object
1537 // NOTE: We could write a Rule to do this
1540 // Complete the operation
1541 modify($manager) {finishOperation($operation)};
1544 // Just doing this will kick off the LOCKED rule again
1545 modify($operation) {};
1548 // Its not finished yet (i.e. expecting more Response objects)
1549 // Or possibly it is a leftover response that we timed the request out previously
1551 "policyResult is null"
1552 + "\nIt's not finished yet (i.e. expecting more Response objects)"
1553 + "\nOr possibly it is a leftover response that we timed the request out previously");
1556 // We are going to retract these objects from memory
1563 * This manages a single timer.
1564 * Due to a bug in the drools code, the drools timer needed to be split from most of the objects in the when clause
1567 rule "${policyName}.TIMER.FIRED"
1568 timer (expr: $timeout)
1570 $timer : ControlLoopTimer($timeout : delay, !expired)
1572 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1573 logger.info("This is ${policyName}.TIMER.FIRED");
1574 modify($timer){setExpired(true)};
1579 * This is the timer that manages the timeout for an individual operation.
1582 rule "${policyName}.EVENT.MANAGER.OPERATION.TIMEOUT"
1584 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1585 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
1586 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1587 requestId == $event.getRequestId() )
1588 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
1589 onset.getRequestId() == $event.getRequestId() )
1590 $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1591 requestId == $event.getRequestId().toString(), expired, timerType == "Operation" )
1594 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1595 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1596 logger.debug("{}: {}: event={} manager={} operation={} opTimer={}",
1597 $params.getClosedLoopControlName(), drools.getRule().getName(),
1598 $event, $manager, $operation, $operation, $opTimer);
1601 // Tell it it has timed out
1603 $operation.setOperationHasTimedOut();
1605 // Create a notification for it ("DB Write - end operation")
1607 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1608 notification.setFrom("policy");
1609 notification.setPolicyName(drools.getRule().getName());
1610 notification.setPolicyScope("${policyScope}");
1611 notification.setPolicyVersion("${policyVersion}");
1612 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
1613 notification.setMessage($operation.getOperationHistory());
1614 notification.setHistory($operation.getHistory());
1616 // Let interested parties know
1618 PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
1620 // Get rid of the timer
1624 // Ensure the operation is complete
1626 if ($operation.isOperationComplete() == true) {
1628 // It is complete, remove it from memory
1630 retract($operation);
1632 // Complete the operation
1634 modify($manager) {finishOperation($operation)};
1637 // Just doing this will kick off the LOCKED rule again
1639 modify($operation) {};
1645 * This is the timer that manages the overall control loop timeout.
1648 rule "${policyName}.EVENT.MANAGER.TIMEOUT"
1650 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1651 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
1652 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1653 requestId == $event.getRequestId() )
1654 $clTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1655 requestId == $event.getRequestId().toString(), expired, timerType == "ClosedLoop" )
1658 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1659 logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1661 logger.debug("{}: {}: event={}",
1662 $params.getClosedLoopControlName(), drools.getRule().getName(),
1665 // Tell the Event Manager it has timed out
1667 VirtualControlLoopNotification notification = $manager.setControlLoopTimedOut();
1668 if (notification != null) {
1669 notification.setFrom("policy");
1670 notification.setPolicyName(drools.getRule().getName());
1671 notification.setPolicyScope("${policyScope}");
1672 notification.setPolicyVersion("${policyVersion}");
1674 // Let interested parties know
1676 PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
1679 // Retract the event
1686 * This rule cleans up the manager and other objects after an event has
1690 rule "${policyName}.EVENT.MANAGER.CLEANUP"
1692 $manager : ControlLoopEventManager( $clName : getClosedLoopControlName(), $requestId : getRequestId() )
1693 $operations : LinkedList()
1694 from collect( ControlLoopOperationManager( onset.closedLoopControlName == $clName,
1695 onset.getRequestId() == $requestId ) )
1696 $timers : LinkedList()
1697 from collect( ControlLoopTimer( closedLoopControlName == $clName,
1698 requestId == $requestId.toString() ) )
1699 not( VirtualControlLoopEvent( closedLoopControlName == $clName, requestId == $requestId ) )
1702 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1703 logger.info("{}: {}", $clName, drools.getRule().getName());
1705 logger.debug("{}: {}: manager={} timers={} operations={}",
1706 $clName, drools.getRule().getName(),
1707 $manager, $timers.size(), $operations.size());
1710 // Retract lock by invoking unlock()
1712 Lock lock = $manager.unlockCurrentOperation();
1718 // Retract EVERYTHING
1723 for(Object manager: $operations) {
1724 retract((ControlLoopOperationManager) manager);
1726 for(Object timer: $timers) {
1727 retract((ControlLoopTimer) timer);
1733 * This rule will clean up any rogue onsets where there is no
1734 * ControlLoopParams object corresponding to the onset event.
1737 rule "${policyName}.EVENT.CLEANUP"
1739 $event : VirtualControlLoopEvent( $clName: closedLoopControlName )
1740 not ( Params( getClosedLoopControlName() == $clName) )
1743 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1744 logger.info("{}: {}", $clName, drools.getRule().getName());
1745 logger.debug("{}: {}: orphan onset event={}",
1746 $clName, drools.getRule().getName(), $event);
1752 * Creates a cleaner for every Params object.
1753 * This has a higher salience so that it is fired before PARAMS.FINISHED in ANY policy.
1755 rule "${policyName}.PARAMS.CLEANING"
1759 ParamsInitCleaner( )
1762 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1763 logger.info("{}: {} : YAML=[{}]", $params.getClosedLoopControlName(), drools.getRule().getName(),
1764 $params.getControlLoopYaml());
1766 ParamsCleaner cleaner = new ParamsCleaner();
1767 cleaner.setClosedLoopControlName($params.getClosedLoopControlName());
1768 cleaner.setControlLoopYaml($params.getControlLoopYaml());
1773 * Finished creating cleaner objects, so remove the trigger.
1774 * This has a higher salience so that it is fired before processing any events.
1776 rule "${policyName}.PARAMS.FINISHED"
1779 $initCleaner: ParamsInitCleaner( )
1782 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1783 logger.info("{}: {}", $initCleaner.getClosedLoopControlName(), drools.getRule().getName());
1785 retract($initCleaner);
1789 * Identifies Params objects that are still active, removing their associated cleaners.
1790 * This should only leave one active Params object for each policy.
1791 * This has a higher salience so that it is fired before PARAMS.DELETE in ANY policy.
1793 rule "${policyName}.PARAMS.ACTIVE"
1796 $params: Params( getClosedLoopControlName() == "${closedLoopControlName}",
1797 getControlLoopYaml() == "${controlLoopYaml}" )
1798 $cleaner: ParamsCleaner( getClosedLoopControlName() == "${closedLoopControlName}",
1799 getControlLoopYaml() == "${controlLoopYaml}" )
1802 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1803 logger.info("{}: {} : YAML=[{}]", $params.getClosedLoopControlName(), drools.getRule().getName(),
1804 $params.getControlLoopYaml());
1810 * Delete Params objects that are not active (i.e., those that still have an associated
1812 * This has a higher salience so that it is fired before PARAMS.CLEANED in ANY policy.
1814 rule "${policyName}.PARAMS.DELETE"
1818 $cleaner: ParamsCleaner( getClosedLoopControlName() == $params.getClosedLoopControlName(),
1819 getControlLoopYaml() == $params.getControlLoopYaml() )
1822 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1823 logger.info("{}: {} : YAML=[{}]", $params.getClosedLoopControlName(), drools.getRule().getName(),
1824 $params.getControlLoopYaml());
1830 * Finished clean-up, so delete the cleaner objects.
1831 * This has a higher salience so that it is fired before processing any events.
1833 rule "${policyName}.PARAMS.CLEANED"
1836 $cleaner: ParamsCleaner( )
1839 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1840 logger.info("{}: {} : YAML=[{}]", $cleaner.getClosedLoopControlName(), drools.getRule().getName(),
1841 $cleaner.getControlLoopYaml());