2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 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.params.ControlLoopParams;
24 import org.onap.policy.controlloop.VirtualControlLoopEvent;
25 import org.onap.policy.controlloop.VirtualControlLoopNotification;
26 import org.onap.policy.controlloop.ControlLoopEventStatus;
27 import org.onap.policy.controlloop.ControlLoopNotificationType;
28 import org.onap.policy.controlloop.ControlLoopLogger;
29 import org.onap.policy.controlloop.policy.PolicyResult;
30 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
31 import org.onap.policy.controlloop.policy.Policy;
32 import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager;
33 import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager.NEW_EVENT_STATUS;
34 import org.onap.policy.controlloop.eventmanager.ControlLoopOperationManager;
35 import org.onap.policy.controlloop.actor.so.SOActorServiceProvider;
36 import org.onap.policy.aai.AaiNqResponseWrapper;
37 import org.onap.policy.appc.Request;
38 import org.onap.policy.appc.Response;
39 import org.onap.policy.appc.CommonHeader;
40 import org.onap.policy.appclcm.LcmRequestWrapper;
41 import org.onap.policy.appclcm.LcmResponseWrapper;
42 import org.onap.policy.appclcm.LcmRequest;
43 import org.onap.policy.appclcm.LcmResponse;
44 import org.onap.policy.appclcm.LcmCommonHeader;
45 import org.onap.policy.sdnr.PciRequestWrapper;
46 import org.onap.policy.sdnr.PciResponseWrapper;
47 import org.onap.policy.sdnr.PciRequest;
48 import org.onap.policy.sdnr.PciResponse;
49 import org.onap.policy.vfc.VFCRequest;
50 import org.onap.policy.vfc.VFCResponse;
51 import org.onap.policy.vfc.VFCManager;
52 import org.onap.policy.so.SOManager;
53 import org.onap.policy.so.SORequest;
54 import org.onap.policy.so.SORequestStatus;
55 import org.onap.policy.so.SORequestDetails;
56 import org.onap.policy.so.SOModelInfo;
57 import org.onap.policy.so.SOCloudConfiguration;
58 import org.onap.policy.so.SORequestInfo;
59 import org.onap.policy.so.SORequestParameters;
60 import org.onap.policy.so.SORelatedInstanceListElement;
61 import org.onap.policy.so.SORelatedInstance;
62 import org.onap.policy.so.SOResponse;
63 import org.onap.policy.so.SOResponseWrapper;
64 import org.onap.policy.guard.PolicyGuard;
65 import org.onap.policy.guard.PolicyGuard.LockResult;
66 import org.onap.policy.guard.TargetLock;
67 import org.onap.policy.guard.GuardResult;
68 import org.onap.policy.guard.PolicyGuardRequest;
69 import org.onap.policy.guard.PolicyGuardResponse;
70 import org.onap.policy.guard.PolicyGuardXacmlRequestAttributes;
71 import org.onap.policy.guard.PolicyGuardXacmlHelper;
73 import org.yaml.snakeyaml.Yaml;
74 import org.yaml.snakeyaml.constructor.Constructor;
76 import org.slf4j.LoggerFactory;
77 import org.slf4j.Logger;
79 import java.time.Instant;
80 import java.util.LinkedList;
81 import java.util.Iterator;
83 import org.onap.policy.drools.system.PolicyEngine;
88 declare OperationTimer
89 closedLoopControlName : String
97 declare ControlLoopTimer
98 closedLoopControlName : String
105 * Called when the ControlLoopParams object has been inserted into working memory from the BRMSGW.
110 $params : ControlLoopParams()
113 // Note: globals have bad behavior when persistence is used,
114 // hence explicitly getting the logger vs using a global
116 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
117 logger.info("{}: {} : YAML=[{}]", $params.getClosedLoopControlName(), $params.getPolicyName() + "." + drools.getRule().getName(), $params.getControlLoopYaml());
122 * This rule responds to DCAE Events where there is no manager yet. Either it is
123 * the first ONSET, or a subsequent badly formed Event (i.e. Syntax error, or is-closed-loop-disabled)
128 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
129 $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
130 not ( ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() ) )
133 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
134 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
139 // Check the event, because we need it to not be null when
140 // we create the ControlLoopEventManager. The ControlLoopEventManager
141 // will do extra syntax checking as well check if the closed loop is disabled.
143 if ($event.getRequestId() == null) {
144 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
145 notification.setNotification(ControlLoopNotificationType.REJECTED);
146 notification.setFrom("policy");
147 notification.setMessage("Missing requestID");
148 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
149 notification.setPolicyScope($params.getPolicyScope());
150 notification.setPolicyVersion($params.getPolicyVersion());
153 // Let interested parties know
155 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
158 // Retract it from memory
161 } else if ($event.getClosedLoopEventStatus() != ControlLoopEventStatus.ONSET) {
162 throw new ControlLoopException($event.getClosedLoopEventStatus() + " received with no prior onset");
165 // Create an EventManager
167 ControlLoopEventManager manager = new ControlLoopEventManager($clName, $event.getRequestId());
169 // Determine if EventManager can actively process the event (i.e. syntax, is_closed_loop_disabled checks etc.)
171 VirtualControlLoopNotification notification = manager.activate($params.getControlLoopYaml(), $event);
172 notification.setFrom("pdp-0001-controller=controlloop"); // Engine.getInstanceName()
173 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
174 notification.setPolicyScope($params.getPolicyScope());
175 notification.setPolicyVersion($params.getPolicyVersion());
177 // Are we actively pursuing this event?
179 if (notification.getNotification() == ControlLoopNotificationType.ACTIVE) {
181 // Insert Event Manager into memory, this will now kick off processing.
185 // Let interested parties know
187 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
189 // Setup the Overall Control Loop timer
191 ControlLoopTimer clTimer = new ControlLoopTimer();
192 clTimer.setClosedLoopControlName($event.getClosedLoopControlName());
193 clTimer.setRequestID($event.getRequestId().toString());
194 clTimer.setDelay(manager.getControlLoopTimeout(1500) + "s");
201 // Let interested parties know
203 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
205 // Retract it from memory
211 // Now that the manager is inserted into Drools working memory, we'll wait for
212 // another rule to fire in order to continue processing. This way we can also
213 // then screen for additional ONSET and ABATED events for this RequestID.
216 } catch (Exception e) {
217 logger.warn("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName(), e);
219 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
220 notification.setNotification(ControlLoopNotificationType.REJECTED);
221 notification.setMessage("Exception occurred: " + e.getMessage());
222 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
223 notification.setPolicyScope($params.getPolicyScope());
224 notification.setPolicyVersion($params.getPolicyVersion());
228 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
238 * This rule happens when we got a valid ONSET, closed loop is enabled and an Event Manager
239 * is now created. We can start processing the yaml specification via the Event Manager.
244 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
245 $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
246 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
247 $clTimer : ControlLoopTimer ( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
250 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
251 logger.info("{}: {}: event={} manager={} clTimer={}",
252 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
253 $event, $manager, $clTimer);
257 // Check which event this is.
259 ControlLoopEventManager.NEW_EVENT_STATUS eventStatus = $manager.onNewEvent($event);
261 // Check what kind of event this is
263 if (eventStatus == NEW_EVENT_STATUS.SUBSEQUENT_ONSET) {
265 // We don't care about subsequent onsets
267 logger.info("{}: {}: subsequent onset",
268 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
272 if (eventStatus == NEW_EVENT_STATUS.SYNTAX_ERROR) {
274 // Ignore any bad syntax events
276 logger.warn("{}: {}: syntax error",
277 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
282 // We only want the initial ONSET event in memory,
283 // all the other events need to be retracted to support
284 // cleanup and avoid the other rules being fired for this event.
286 if (eventStatus != NEW_EVENT_STATUS.FIRST_ONSET) {
287 logger.warn("{}: {}: no first onset",
288 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
292 logger.debug("{}: {}: target={}", $clName,
293 $params.getPolicyName() + "." + drools.getRule().getName(), $event.getTarget());
295 // Now start seeing if we need to process this event
299 // Check if this is a Final Event
301 VirtualControlLoopNotification notification = $manager.isControlLoopFinal();
304 if (notification != null) {
306 // Its final, but are we waiting for abatement?
308 if ($manager.getNumAbatements() > 0) {
309 logger.info("{}: {}: abatement received for {}. Closing the control loop",
310 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
311 $event.getRequestId());
312 notification.setFrom("policy");
313 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
314 notification.setPolicyScope($params.getPolicyScope());
315 notification.setPolicyVersion($params.getPolicyVersion());
317 // In this case, we are done
319 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
323 TargetLock lock = $manager.unlockCurrentOperation();
325 logger.debug("{}: {}: retracting lock=", $clName,
326 $params.getPolicyName() + "." + drools.getRule().getName(), lock);
330 // Retract everything from memory
332 logger.info("{}: {}: retracting onset, manager, and timer",
333 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
335 retract($manager.getOnsetEvent());
339 // TODO - what if we get subsequent Events for this RequestID?
340 // By default, it will all start over again. May be confusing for Ruby.
341 // Or, we could track this and then subsequently ignore the events
345 // Check whether we need to wait for abatement
347 if ($manager.getProcessor().getControlLoop().getAbatement() == true && notification.getNotification() == ControlLoopNotificationType.FINAL_SUCCESS) {
348 logger.info("{}: {}: waiting for abatement ..",
349 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
351 logger.info("{}: {}: no abatement expect for {}. Closing the control loop",
352 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
353 $event.getRequestId());
355 notification.setFrom("policy");
356 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
357 notification.setPolicyScope($params.getPolicyScope());
358 notification.setPolicyVersion($params.getPolicyVersion());
361 // In this case, we are done
363 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
367 TargetLock lock = $manager.unlockCurrentOperation();
369 logger.debug("{}: {}: retracting lock=", $clName,
370 $params.getPolicyName() + "." + drools.getRule().getName(), lock);
374 // Retract everything from memory
376 logger.info("{}: {}: retracting onset, manager, and timer",
377 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
379 retract($manager.getOnsetEvent());
386 // NOT final, so let's ask for the next operation
388 ControlLoopOperationManager operation = $manager.processControlLoop();
389 if (operation != null) {
391 // Let's ask for a lock right away
393 LockResult<GuardResult, TargetLock> result = $manager.lockCurrentOperation();
394 logger.info("{}: {}: guard lock acquired={}",
395 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
397 if (result.getA().equals(GuardResult.LOCK_ACQUIRED)) {
399 // insert the operation into memory
404 // insert operation timeout object
406 OperationTimer opTimer = new OperationTimer();
407 opTimer.setClosedLoopControlName($event.getClosedLoopControlName());
408 opTimer.setRequestID($event.getRequestId().toString());
409 opTimer.setDelay(operation.getOperationTimeout().toString() + "s");
413 // Insert lock into memory
415 insert(result.getB());
418 logger.debug("The target resource {} is already processing",
419 $event.getAai().get($event.getTarget()));
420 notification = new VirtualControlLoopNotification($event);
421 notification.setNotification(ControlLoopNotificationType.REJECTED);
422 notification.setMessage("The target " + $event.getAai().get($event.getTarget()) + " is already locked");
423 notification.setFrom("policy");
424 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
425 notification.setPolicyScope($params.getPolicyScope());
426 notification.setPolicyVersion($params.getPolicyVersion());
428 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
434 if(result.getB() != null) {
435 retract(result.getB());
438 logger.info("{}: {}: starting operation={}",
439 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
443 // Probably waiting for abatement
445 logger.info("{}: {}: no operation, probably waiting for abatement",
446 $clName, $params.getPolicyName() + "." + drools.getRule().getName());
449 } catch (Exception e) {
450 logger.warn("{}: {}: unexpected",
452 $params.getPolicyName() + "." + drools.getRule().getName(), e);
454 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
455 notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
456 notification.setMessage(e.getMessage());
457 notification.setFrom("policy");
458 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
459 notification.setPolicyScope($params.getPolicyScope());
460 notification.setPolicyVersion($params.getPolicyVersion());
462 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
473 * Guard Permitted, let's send request to the actor.
476 rule "EVENT.MANAGER.OPERATION.LOCKED.GUARD_PERMITTED"
478 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
479 $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
480 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
481 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId(), "Permit".equalsIgnoreCase(getGuardApprovalStatus()) )
482 $lock : TargetLock (requestID == $event.getRequestId())
483 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
486 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
487 logger.info("{}: {}: event={} manager={} operation={} lock={}",
488 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
489 $event, $manager, $operation, $lock);
491 Object request = null;
492 boolean caughtException = false;
495 request = $operation.startOperation($event);
497 if (request != null) {
498 logger.debug("{}: {}: starting operation ..",
500 $params.getPolicyName() + "." + drools.getRule().getName());
502 // Tell interested parties we are performing this Operation
504 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
505 notification.setNotification(ControlLoopNotificationType.OPERATION);
506 notification.setMessage($operation.getOperationMessage());
507 notification.setHistory($operation.getHistory());
508 notification.setFrom("policy");
509 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
510 notification.setPolicyScope($params.getPolicyScope());
511 notification.setPolicyVersion($params.getPolicyVersion());
513 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
515 switch ($operation.policy.getActor()){
519 if (request instanceof Request) {
520 PolicyEngine.manager.deliver("APPC-CL", request);
522 else if (request instanceof LcmRequestWrapper) {
523 PolicyEngine.manager.deliver("APPC-LCM-READ", request);
527 // at this point the AAI named query request should have already been made, the response recieved and used
528 // in the construction of the SO Request which is stored in operationRequest
530 if(request instanceof SORequest) {
531 // Call SO. The response will be inserted into memory once it's received
532 SOActorServiceProvider.sendRequest($event.getRequestId().toString(), drools.getWorkingMemory(), request);
536 if (request instanceof VFCRequest) {
538 Thread t = new Thread(new VFCManager(drools.getWorkingMemory(), (VFCRequest)request));
543 if (request instanceof PciRequestWrapper) {
544 PolicyEngine.manager.deliver("SDNR-CL", request);
550 // What happens if its null?
552 logger.warn("{}: {}: unexpected null operation request",
554 $params.getPolicyName() + "." + drools.getRule().getName());
555 if ("SO".equals($operation.policy.getActor())) {
558 modify($manager) {finishOperation($operation)};
560 else if ("vfc".equalsIgnoreCase($operation.policy.getActor())) {
563 modify($manager) {finishOperation($operation)};
567 } catch (Exception e) {
568 String msg = e.getMessage();
569 logger.warn("{}: {}: operation={}: AAI failure: {}",
571 $params.getPolicyName() + "." + drools.getRule().getName(),
573 $operation.setOperationHasException(msg);
575 if(request != null) {
577 // Create a notification for it ("DB Write - end operation")
579 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
580 notification.setFrom("policy");
581 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
582 notification.setPolicyScope($params.getPolicyScope());
583 notification.setPolicyVersion($params.getPolicyVersion());
584 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
585 notification.setMessage($operation.getOperationHistory());
586 notification.setHistory($operation.getHistory());
588 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
593 caughtException = true;
596 // Having the modify statement in the catch clause doesn't work for whatever reason
597 if (caughtException) {
598 modify($manager) {finishOperation($operation)};
605 * We were able to acquire a lock so now let's ask Xacml Guard whether
606 * we are allowed to proceed with the request to the actor.
609 rule "EVENT.MANAGER.OPERATION.LOCKED.GUARD_NOT_YET_QUERIED"
611 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
612 $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
613 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
614 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId(), getGuardApprovalStatus() == "NONE" )
615 $lock : TargetLock (requestID == $event.getRequestId())
618 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
619 logger.info("{}: {}: event={} manager={} operation={} lock={}",
620 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
621 $event, $manager, $operation, $lock);
624 // Sending notification that we are about to query Guard ("DB write - start operation")
626 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
627 notification.setNotification(ControlLoopNotificationType.OPERATION);
628 notification.setMessage("Sending guard query for " + $operation.policy.getActor() + " " + $operation.policy.getRecipe());
629 notification.setHistory($operation.getHistory());
630 notification.setFrom("policy");
631 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
632 notification.setPolicyScope($params.getPolicyScope());
633 notification.setPolicyVersion($params.getPolicyVersion());
635 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
638 // Now send Guard Request to XACML Guard. In order to bypass the call to Guard,
639 // just change guardEnabled to false.
641 // In order to use REST XACML, provide a URL instead of "" as a second argument
642 // to the CallGuardTask() and set the first argument to null
643 // (instead of XacmlPdpEngine).
646 // NOTE: The environment properties uses "guard.disabled" but the boolean is guardEnabled
647 boolean guardEnabled = "false".equalsIgnoreCase(PolicyEngine.manager.getEnvironmentProperty("guard.disabled"));
651 Thread t = new Thread(new org.onap.policy.guard.CallGuardTask(
652 drools.getWorkingMemory(),
653 $event.getClosedLoopControlName(),
654 $operation.policy.getActor().toString(),
655 $operation.policy.getRecipe(),
656 $operation.getTargetEntity(),
657 $event.getRequestId().toString(),
659 AaiNqResponseWrapper resp = $manager.getNqVserverFromAai();
660 return(resp == null ? null : resp.countVfModules());
665 insert(new PolicyGuardResponse("Permit", $event.getRequestId(), $operation.policy.getRecipe()));
671 // This rule will be triggered when a thread talking to the XACML Guard inserts a
672 // guardResponse object into the working memory
674 rule "GUARD.RESPONSE"
676 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
677 $event : VirtualControlLoopEvent( closedLoopControlName == $clName, closedLoopEventStatus == ControlLoopEventStatus.ONSET )
678 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
679 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() )
680 $lock : TargetLock (requestID == $event.getRequestId())
681 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
682 $guardResponse : PolicyGuardResponse(requestID == $event.getRequestId(), $operation.policy.recipe == operation)
685 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
686 logger.info("{}: {}: event={} manager={} operation={} lock={} opTimer={} guardResponse={}",
687 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
688 $event, $manager, $operation, $lock, $opTimer, $guardResponse);
691 //we will permit the operation if there was no Guard for it
692 if("Indeterminate".equalsIgnoreCase($guardResponse.getResult())){
693 $guardResponse.setResult("Permit");
697 // This notification has Guard result in "message". ("DB write - end operation in case of Guard Deny")
699 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
700 notification.setNotification(ControlLoopNotificationType.OPERATION);
701 notification.setMessage("Guard result for " + $operation.policy.getActor() + " " + $operation.policy.getRecipe() + " is " + $guardResponse.getResult());
702 notification.setHistory($operation.getHistory());
703 notification.setFrom("policy");
704 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
705 notification.setPolicyScope($params.getPolicyScope());
706 notification.setPolicyVersion($params.getPolicyVersion());
708 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
710 if("Permit".equalsIgnoreCase($guardResponse.getResult())){
712 modify($operation){setGuardApprovalStatus($guardResponse.getResult())};
715 //This is the Deny case
716 $operation.startOperation($event);
717 $operation.setOperationHasGuardDeny();
720 modify($manager) {finishOperation($operation)};
723 retract($guardResponse);
729 * This rule responds to APPC Response Events
731 * I would have like to be consistent and write the Response like this:
732 * $response : Response( CommonHeader.RequestId == $onset.getRequestId() )
734 * However, no compile error was given. But a runtime error was given. I think
735 * because drools is confused between the classname CommonHeader vs the property CommonHeader.
740 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
741 $event : VirtualControlLoopEvent( closedLoopControlName == $clName, closedLoopEventStatus == ControlLoopEventStatus.ONSET )
742 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
743 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() )
744 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
745 $lock : TargetLock (requestID == $event.getRequestId())
746 $response : Response( getCommonHeader().RequestId == $event.getRequestId() )
749 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
750 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
751 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
752 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
753 $event, $manager, $operation, $lock, $opTimer, $response);
755 // Get the result of the operation
757 PolicyResult policyResult = $operation.onResponse($response);
758 if (policyResult != null) {
759 logger.debug("{}: {}: operation finished - result={}",
760 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
763 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
765 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
766 notification.setFrom("policy");
767 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
768 notification.setPolicyScope($params.getPolicyScope());
769 notification.setPolicyVersion($params.getPolicyVersion());
770 notification.setMessage($operation.getOperationHistory());
771 notification.setHistory($operation.getHistory());
772 if (policyResult.equals(PolicyResult.SUCCESS)) {
773 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
775 // Let interested parties know
777 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
779 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
781 // Let interested parties know
783 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
786 // Ensure the operation is complete
788 if ($operation.isOperationComplete() == true) {
790 // It is complete, remove it from memory
794 // We must also retract the timer object
795 // NOTE: We could write a Rule to do this
799 // Complete the operation
801 modify($manager) {finishOperation($operation)};
804 // Just doing this will kick off the LOCKED rule again
806 modify($operation) {};
810 // Its not finished yet (i.e. expecting more Response objects)
812 // Or possibly it is a leftover response that we timed the request out previously
816 // We are going to retract these objects from memory
823 * The problem with Responses is that they don't have a controlLoopControlName
824 * field in them, so the only way to attach them is via RequestID. If we have multiple
825 * control loop .drl's loaded in the same container, we need to be sure the cleanup
826 * rules don't remove Responses for other control loops.
829 rule "APPC.RESPONSE.CLEANUP"
831 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
832 $response : Response($id : getCommonHeader().RequestId )
833 not ( VirtualControlLoopEvent( requestId == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
836 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
837 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
838 logger.debug("{}: {}: orphan appc response={}",
839 $clName, $params.getPolicyName() + "." + drools.getRule().getName(), $id);
849 * This rule responds to APPC Response Events using the new LCM interface provided by appc
852 rule "APPC.LCM.RESPONSE"
854 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
855 $event : VirtualControlLoopEvent( closedLoopControlName == $clName, closedLoopEventStatus == ControlLoopEventStatus.ONSET )
856 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
857 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() )
858 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
859 $lock : TargetLock (requestID == $event.getRequestId())
860 $response : LcmResponseWrapper( getBody().getCommonHeader().getRequestId() == $event.getRequestId() )
863 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
864 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
865 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
866 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
867 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
870 // Get the result of the operation
872 PolicyResult policyResult = $operation.onResponse($response);
873 if (policyResult != null) {
874 logger.debug("{}: {}: operation finished - result={}",
875 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
879 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
881 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
882 notification.setFrom("policy");
883 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
884 notification.setPolicyScope($params.getPolicyScope());
885 notification.setPolicyVersion($params.getPolicyVersion());
886 notification.setMessage($operation.getOperationHistory());
887 notification.setHistory($operation.getHistory());
888 if (policyResult.equals(PolicyResult.SUCCESS)) {
889 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
891 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
893 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
895 // Ensure the operation is complete
897 if ($operation.isOperationComplete() == true) {
899 // It is complete, remove it from memory
903 // We must also retract the timer object
904 // NOTE: We could write a Rule to do this
908 // Complete the operation
910 modify($manager) {finishOperation($operation)};
913 // Just doing this will kick off the LOCKED rule again
915 modify($operation) {};
919 // Its not finished yet (i.e. expecting more Response objects)
921 // Or possibly it is a leftover response that we timed the request out previously
925 // We are going to retract these objects from memory
932 * Clean Up any lingering LCM reponses
935 rule "APPC.LCM.RESPONSE.CLEANUP"
937 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
938 $response : LcmResponseWrapper($id : getBody().getCommonHeader().getRequestId )
939 not ( VirtualControlLoopEvent( requestId == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
942 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
943 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
944 logger.debug("{}: {}: orphan appc response={}",
945 $clName, $params.getPolicyName() + "." + drools.getRule().getName(), $id);
954 * This rule responds to SO Response Events
959 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
960 $event : VirtualControlLoopEvent( closedLoopControlName == $clName, closedLoopEventStatus == ControlLoopEventStatus.ONSET )
961 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
962 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() )
963 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
964 $lock : TargetLock (requestID == $event.getRequestId())
965 $response : SOResponseWrapper(requestID.toString() == $event.getRequestId().toString() )
968 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
969 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
970 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
971 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
972 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
974 // Get the result of the operation
976 PolicyResult policyResult = $operation.onResponse($response);
977 if (policyResult != null) {
978 logger.debug("{}: {}: operation finished - result={}",
979 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
983 // This Operation has completed, construct a notification showing our results
985 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
986 notification.setFrom("policy");
987 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
988 notification.setPolicyScope($params.getPolicyScope());
989 notification.setPolicyVersion($params.getPolicyVersion());
990 notification.setMessage($operation.getOperationHistory());
991 notification.setHistory($operation.getHistory());
992 if (policyResult.equals(PolicyResult.SUCCESS)) {
993 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
995 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
998 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1000 // Ensure the operation is complete
1002 if ($operation.isOperationComplete() == true) {
1004 // It is complete, remove it from memory
1006 retract($operation);
1008 // We must also retract the timer object
1009 // NOTE: We could write a Rule to do this
1013 // Complete the operation
1015 modify($manager) {finishOperation($operation)};
1018 // Just doing this will kick off the LOCKED rule again
1020 modify($operation) {};
1024 // Its not finished yet (i.e. expecting more Response objects)
1026 // Or possibly it is a leftover response that we timed the request out previously
1030 // We are going to retract these objects from memory
1038 * This rule responds to VFC Response Events
1043 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
1044 $event : VirtualControlLoopEvent( closedLoopControlName == $clName, closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1045 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
1046 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() )
1047 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
1048 $lock : TargetLock (requestID == $event.getRequestId())
1049 $response : VFCResponse( requestId.toString() == $event.getRequestId().toString() )
1051 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1052 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
1053 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
1054 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
1055 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
1057 // Get the result of the operation
1059 PolicyResult policyResult = $operation.onResponse($response);
1060 if (policyResult != null) {
1062 // This Operation has completed, construct a notification showing our results
1064 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1065 notification.setFrom("policy");
1066 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
1067 notification.setPolicyScope($params.getPolicyScope());
1068 notification.setPolicyVersion($params.getPolicyVersion());
1069 notification.setMessage($operation.getOperationHistory());
1070 notification.setHistory($operation.getHistory());
1072 // Ensure the operation is complete
1074 if ($operation.isOperationComplete() == true) {
1076 // It is complete, remove it from memory
1078 retract($operation);
1080 // We must also retract the timer object
1081 // NOTE: We could write a Rule to do this
1085 // Complete the operation
1087 modify($manager) {finishOperation($operation)};
1090 // Just doing this will kick off the LOCKED rule again
1092 modify($operation) {};
1096 // Its not finished yet (i.e. expecting more Response objects)
1098 // Or possibly it is a leftover response that we timed the request out previously
1102 // We are going to retract these objects from memory
1110 * This is the timer that manages the timeout for an individual operation.
1113 rule "EVENT.MANAGER.OPERATION.TIMEOUT"
1116 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
1117 $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
1118 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
1119 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() )
1120 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString(), $to : getDelay() )
1121 $lock : TargetLock (requestID == $event.getRequestId())
1124 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1125 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
1126 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={}",
1127 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
1128 $event, $manager, $operation, $lock, $operation, $opTimer);
1131 // Tell it its timed out
1133 $operation.setOperationHasTimedOut();
1135 // Create a notification for it ("DB Write - end operation")
1137 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1138 notification.setFrom("policy");
1139 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
1140 notification.setPolicyScope($params.getPolicyScope());
1141 notification.setPolicyVersion($params.getPolicyVersion());
1142 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
1143 notification.setMessage($operation.getOperationHistory());
1144 notification.setHistory($operation.getHistory());
1146 // Let interested parties know
1148 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1150 // Get rid of the timer
1154 // Ensure the operation is complete
1156 if ($operation.isOperationComplete() == true) {
1158 // It is complete, remove it from memory
1160 retract($operation);
1162 // Complete the operation
1164 modify($manager) {finishOperation($operation)};
1167 // Just doing this will kick off the LOCKED rule again
1169 modify($operation) {};
1175 * This is the timer that manages the overall control loop timeout.
1178 rule "EVENT.MANAGER.TIMEOUT"
1181 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
1182 $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
1183 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
1184 $clTimer : ControlLoopTimer ( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString(), $to : getDelay() )
1187 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1188 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
1190 logger.debug("{}: {}: event={}",
1191 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
1194 // Tell the Event Manager it has timed out
1196 VirtualControlLoopNotification notification = $manager.setControlLoopTimedOut();
1197 if (notification != null) {
1198 notification.setFrom("policy");
1199 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
1200 notification.setPolicyScope($params.getPolicyScope());
1201 notification.setPolicyVersion($params.getPolicyVersion());
1203 // Let interested parties know
1205 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1208 // Retract the event
1215 * This rule cleans up the manager and other objects after an event has
1219 rule "EVENT.MANAGER.CLEANUP"
1221 $manager : ControlLoopEventManager( $clName : getClosedLoopControlName(), $requestId : getRequestID() )
1222 $clTimer : ControlLoopTimer ( closedLoopControlName == $clName, requestID == $requestId.toString() )
1223 $operations : LinkedList()
1224 from collect( ControlLoopOperationManager( onset.closedLoopControlName == $clName, onset.getRequestId() == $requestId ) )
1225 $opTimers : LinkedList()
1226 from collect( OperationTimer( closedLoopControlName == $clName, requestID == $requestId.toString() ) )
1227 $locks : LinkedList()
1228 from collect( TargetLock (requestID == $requestId) )
1229 not( VirtualControlLoopEvent( closedLoopControlName == $clName, requestId == $requestId ) )
1232 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1233 logger.info("{}: {}", $clName, drools.getRule().getName());
1235 logger.debug("{}: {}: manager={} clTimer={} operations={}",
1236 $clName, drools.getRule().getName(),
1237 $manager, $clTimer, $operations.size());
1240 // Retract EVERYTHING
1245 for(Object manager: $operations) {
1246 retract((ControlLoopOperationManager) manager);
1248 for(Object opTimer: $opTimers) {
1249 retract((OperationTimer) opTimer);
1251 for(Object lock: $locks) {
1252 TargetLock tgt = (TargetLock) lock;
1254 // Ensure we release the lock
1256 PolicyGuard.unlockTarget(tgt);
1263 * This rule will clean up any rogue onsets where there is no
1264 * ControlLoopParams object corresponding to the onset event.
1267 rule "EVENT.CLEANUP"
1269 $event : VirtualControlLoopEvent( $clName: closedLoopControlName )
1270 not ( ControlLoopParams( getClosedLoopControlName() == $clName) )
1273 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1274 logger.info("{}: {}", $clName, drools.getRule().getName());
1275 logger.debug("{}: {}: orphan onset event={}",
1276 $clName, drools.getRule().getName(), $event);
1283 * This rule responds to SDNR Response Events.
1286 rule "SDNR.RESPONSE"
1288 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
1289 $event : VirtualControlLoopEvent( closedLoopControlName == $clName, closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1290 $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
1291 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() )
1292 $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
1293 $lock : TargetLock (requestID == $event.getRequestId())
1294 $response : PciResponseWrapper( getBody().getCommonHeader().getRequestId() == $event.getRequestId() )
1297 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1298 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
1299 logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
1300 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
1301 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
1304 // Get the result of the operation
1306 PolicyResult policyResult = $operation.onResponse($response);
1307 if (policyResult != null) {
1308 logger.debug("{}: {}: operation finished - result={}",
1309 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
1313 // This Operation has completed, construct a notification showing our results. (DB write - end operation)
1315 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1316 notification.setFrom("policy");
1317 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
1318 notification.setPolicyScope($params.getPolicyScope());
1319 notification.setPolicyVersion($params.getPolicyVersion());
1320 notification.setMessage($operation.getOperationHistory());
1321 notification.setHistory($operation.getHistory());
1322 if (policyResult.equals(PolicyResult.SUCCESS)) {
1323 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
1325 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
1327 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1329 // Ensure the operation is complete
1331 if ($operation.isOperationComplete() == true) {
1333 // It is complete, remove it from memory
1335 retract($operation);
1337 // We must also retract the timer object
1338 // NOTE: We could write a Rule to do this
1342 // Complete the operation
1344 modify($manager) {finishOperation($operation)};
1347 // Just doing this will kick off the LOCKED rule again
1349 modify($operation) {};
1353 // Its not finished yet (i.e. expecting more Response objects)
1355 // Or possibly it is a leftover response that we timed the request out previously
1359 // We are going to retract these objects from memory
1366 * Clean Up any lingering SDNR reponses.
1369 rule "SDNR.RESPONSE.CLEANUP"
1371 $params : ControlLoopParams( $clName : getClosedLoopControlName() )
1372 $response : PciResponseWrapper($id : getBody().getCommonHeader().getRequestId )
1373 not ( VirtualControlLoopEvent( requestId == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
1376 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1377 logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
1378 logger.debug("{}: {}: orphan sdnr response={}",
1379 $clName, $params.getPolicyName() + "." + drools.getRule().getName(), $id);