24cb7f86ab8f2a826818cfb3fac744ce57399419
[policy/drools-applications.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.controlloop;
22
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.appc.Request;
37 import org.onap.policy.appc.Response;
38 import org.onap.policy.appc.CommonHeader;
39 import org.onap.policy.appclcm.LcmRequestWrapper;
40 import org.onap.policy.appclcm.LcmResponseWrapper;
41 import org.onap.policy.appclcm.LcmRequest;
42 import org.onap.policy.appclcm.LcmResponse;
43 import org.onap.policy.appclcm.LcmCommonHeader;
44 import org.onap.policy.vfc.VFCRequest;
45 import org.onap.policy.vfc.VFCResponse;
46 import org.onap.policy.vfc.VFCManager;
47 import org.onap.policy.so.SOManager;
48 import org.onap.policy.so.SORequest;
49 import org.onap.policy.so.SORequestStatus;
50 import org.onap.policy.so.SORequestDetails;
51 import org.onap.policy.so.SOModelInfo;
52 import org.onap.policy.so.SOCloudConfiguration;
53 import org.onap.policy.so.SORequestInfo;
54 import org.onap.policy.so.SORequestParameters;
55 import org.onap.policy.so.SORelatedInstanceListElement;
56 import org.onap.policy.so.SORelatedInstance;
57 import org.onap.policy.so.SOResponse;
58 import org.onap.policy.so.SOResponseWrapper;
59 import org.onap.policy.guard.PolicyGuard;
60 import org.onap.policy.guard.PolicyGuard.LockResult;
61 import org.onap.policy.guard.TargetLock;
62 import org.onap.policy.guard.GuardResult;
63 import org.onap.policy.guard.PolicyGuardRequest;
64 import org.onap.policy.guard.PolicyGuardResponse;
65 import org.onap.policy.guard.PolicyGuardXacmlRequestAttributes;
66 import org.onap.policy.guard.PolicyGuardXacmlHelper;
67
68 import org.yaml.snakeyaml.Yaml;
69 import org.yaml.snakeyaml.constructor.Constructor;
70
71 import org.slf4j.LoggerFactory;
72 import org.slf4j.Logger;
73
74 import java.time.Instant;
75 import java.util.LinkedList;
76 import java.util.Iterator;
77
78 import org.onap.policy.drools.system.PolicyEngine;
79
80 /*
81  * Operation Timer
82  */
83 declare OperationTimer
84   closedLoopControlName : String
85   requestID : String
86   delay : String
87 end
88
89 /*
90  * Control Loop Timer
91  */
92 declare ControlLoopTimer
93   closedLoopControlName : String
94   requestID : String
95   delay : String
96 end
97
98 /*
99 *
100 * Called when the ControlLoopParams object has been inserted into working memory from the BRMSGW.
101 *
102 */
103 rule "INSERT.PARAMS"
104     when
105         $params : ControlLoopParams()
106     then
107
108     // Note: globals have bad behavior when persistence is used,
109     //       hence explicitly getting the logger vs using a global
110
111     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
112     logger.info("{}: {} : YAML=[{}]", $params.getClosedLoopControlName(), $params.getPolicyName() + "." + drools.getRule().getName(), $params.getControlLoopYaml());
113 end
114
115 /*
116 *
117 * This rule responds to DCAE Events where there is no manager yet. Either it is
118 * the first ONSET, or a subsequent badly formed Event (i.e. Syntax error, or is-closed-loop-disabled)
119 *
120 */
121 rule "EVENT"
122     when
123         $params : ControlLoopParams( $clName : getClosedLoopControlName() )
124         $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
125         not ( ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() ) )
126     then
127
128     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
129     logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
130
131     try {
132
133         //
134         // Check the event, because we need it to not be null when
135         // we create the ControlLoopEventManager. The ControlLoopEventManager
136         // will do extra syntax checking as well check if the closed loop is disabled.
137         //
138         if ($event.getRequestId() == null) {
139             VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
140             notification.setNotification(ControlLoopNotificationType.REJECTED);
141             notification.setFrom("policy");
142             notification.setMessage("Missing requestID");
143             notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
144             notification.setPolicyScope($params.getPolicyScope());
145             notification.setPolicyVersion($params.getPolicyVersion());
146
147             //
148             // Let interested parties know
149             //
150             PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
151
152             //
153             // Retract it from memory
154             //
155             retract($event);
156         } else if ($event.getClosedLoopEventStatus() != ControlLoopEventStatus.ONSET) {
157             throw new ControlLoopException($event.getClosedLoopEventStatus() + " received with no prior onset");
158         } else {
159             //
160             // Create an EventManager
161             //
162             ControlLoopEventManager manager = new ControlLoopEventManager($clName, $event.getRequestId());
163             //
164             // Determine if EventManager can actively process the event (i.e. syntax, is_closed_loop_disabled checks etc.)
165             //
166             VirtualControlLoopNotification notification = manager.activate($params.getControlLoopYaml(), $event);
167             notification.setFrom("pdp-0001-controller=controlloop"); // Engine.getInstanceName()
168             notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
169             notification.setPolicyScope($params.getPolicyScope());
170             notification.setPolicyVersion($params.getPolicyVersion());
171             //
172             // Are we actively pursuing this event?
173             //
174             if (notification.getNotification() == ControlLoopNotificationType.ACTIVE) {
175                 //
176                 // Insert Event Manager into memory, this will now kick off processing.
177                 //
178                 insert(manager);
179                 //
180                 // Let interested parties know
181                 //
182                 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
183                 //
184                 // Setup the Overall Control Loop timer
185                 //
186                 ControlLoopTimer clTimer = new ControlLoopTimer();
187                 clTimer.setClosedLoopControlName($event.getClosedLoopControlName());
188                 clTimer.setRequestID($event.getRequestId().toString());
189                 clTimer.setDelay(manager.getControlLoopTimeout(1500) + "s");
190                 //
191                 // Insert it
192                 //
193                 insert(clTimer);
194             } else {
195                 //
196                 // Let interested parties know
197                 //
198                 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
199                 //
200                 // Retract it from memory
201                 //
202                 retract($event);
203             }
204
205             //
206             // Now that the manager is inserted into Drools working memory, we'll wait for
207             // another rule to fire in order to continue processing. This way we can also
208             // then screen for additional ONSET and ABATED events for this RequestID.
209             //
210         }
211     } catch (Exception e) {
212         logger.warn("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName(), e);
213
214         VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
215         notification.setNotification(ControlLoopNotificationType.REJECTED);
216         notification.setMessage("Exception occurred: " + e.getMessage());
217         notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
218         notification.setPolicyScope($params.getPolicyScope());
219         notification.setPolicyVersion($params.getPolicyVersion());
220         //
221         //
222         //
223         PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
224         //
225         // Retract the event
226         //
227         retract($event);
228     }
229 end
230
231 /*
232 *
233 * This rule happens when we got a valid ONSET, closed loop is enabled and an Event Manager
234 * is now created. We can start processing the yaml specification via the Event Manager.
235 *
236 */
237 rule "EVENT.MANAGER"
238     when
239         $params : ControlLoopParams( $clName : getClosedLoopControlName() )
240         $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
241         $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
242         $clTimer : ControlLoopTimer ( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
243     then
244
245     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
246     logger.info("{}: {}: event={} manager={} clTimer={}",
247                 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
248                 $event, $manager, $clTimer);
249
250     try {
251         //
252         // Check which event this is.
253         //
254         ControlLoopEventManager.NEW_EVENT_STATUS eventStatus = $manager.onNewEvent($event);
255         //
256         // Check what kind of event this is
257         //
258         if (eventStatus == NEW_EVENT_STATUS.SUBSEQUENT_ONSET) {
259             //
260             // We don't care about subsequent onsets
261             //
262             logger.info("{}: {}: subsequent onset",
263                         $clName, $params.getPolicyName() + "." + drools.getRule().getName());
264             retract($event);
265             return;
266         }
267         if (eventStatus == NEW_EVENT_STATUS.SYNTAX_ERROR) {
268             //
269             // Ignore any bad syntax events
270             //
271             logger.warn("{}: {}: syntax error",
272                         $clName, $params.getPolicyName() + "." + drools.getRule().getName());
273             retract($event);
274             return;
275         }
276         //
277         // We only want the initial ONSET event in memory,
278         // all the other events need to be retracted to support
279         // cleanup and avoid the other rules being fired for this event.
280         //
281         if (eventStatus != NEW_EVENT_STATUS.FIRST_ONSET) {
282             logger.warn("{}: {}: no first onset",
283                         $clName, $params.getPolicyName() + "." + drools.getRule().getName());
284             retract($event);
285         }
286
287         logger.debug("{}: {}: target={}", $clName,
288                      $params.getPolicyName() + "." + drools.getRule().getName(), $event.getTarget());
289         //
290         // Now start seeing if we need to process this event
291         //
292
293         //
294         // Check if this is a Final Event
295         //
296         VirtualControlLoopNotification notification = $manager.isControlLoopFinal();
297
298
299         if (notification != null) {
300             //
301             // Its final, but are we waiting for abatement?
302             //
303             if ($manager.getNumAbatements() > 0) {
304                 logger.info("{}: {}: abatement received for {}.  Closing the control loop",
305                             $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
306                             $event.getRequestId());
307                 notification.setFrom("policy");
308                 notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
309                 notification.setPolicyScope($params.getPolicyScope());
310                 notification.setPolicyVersion($params.getPolicyVersion());
311                 //
312                 // In this case, we are done
313                 //
314                 PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
315                 //
316                 // Unlock the target
317                 //
318                 TargetLock lock = $manager.unlockCurrentOperation();
319                 if (lock != null) {
320                     logger.debug("{}: {}: retracting lock=", $clName,
321                                  $params.getPolicyName() + "." + drools.getRule().getName(), lock);
322                     retract(lock);
323                 }
324                 //
325                 // Retract everything from memory
326                 //
327                 logger.info("{}: {}: retracting onset, manager, and timer",
328                             $clName, $params.getPolicyName() + "." + drools.getRule().getName());
329
330                 retract($manager.getOnsetEvent());
331                 retract($manager);
332                 retract($clTimer);
333                 //
334                 // TODO - what if we get subsequent Events for this RequestID?
335                 // By default, it will all start over again. May be confusing for Ruby.
336                 // Or, we could track this and then subsequently ignore the events
337                 //
338             } else {
339                 //
340                 // Check whether we need to wait for abatement
341                 //
342                 if ($manager.getProcessor().getControlLoop().getAbatement() == true && notification.getNotification() == ControlLoopNotificationType.FINAL_SUCCESS) {
343                   logger.info("{}: {}: waiting for abatement ..",
344                               $clName, $params.getPolicyName() + "." + drools.getRule().getName());
345                 } else {
346                   logger.info("{}: {}: no abatement expect for {}.  Closing the control loop",
347                               $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
348                               $event.getRequestId());
349
350                   notification.setFrom("policy");
351                   notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
352                   notification.setPolicyScope($params.getPolicyScope());
353                   notification.setPolicyVersion($params.getPolicyVersion());
354
355                   //
356                   // In this case, we are done
357                   //
358                   PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
359                   //
360                   // Unlock the target
361                   //
362                   TargetLock lock = $manager.unlockCurrentOperation();
363                   if (lock != null) {
364                       logger.debug("{}: {}: retracting lock=", $clName,
365                                   $params.getPolicyName() + "." + drools.getRule().getName(), lock);
366                       retract(lock);
367                   }
368                   //
369                   // Retract everything from memory
370                   //
371                   logger.info("{}: {}: retracting onset, manager, and timer",
372                               $clName, $params.getPolicyName() + "." + drools.getRule().getName());
373
374                   retract($manager.getOnsetEvent());
375                   retract($manager);
376                   retract($clTimer);
377                 }
378             }
379         } else {
380             //
381             // NOT final, so let's ask for the next operation
382             //
383             ControlLoopOperationManager operation = $manager.processControlLoop();
384             if (operation != null) {
385               //
386               // Let's ask for a lock right away
387               //
388               LockResult<GuardResult, TargetLock> result = $manager.lockCurrentOperation();
389               logger.info("{}: {}: guard lock acquired={}",
390                             $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
391                             result.getB());
392               if (result.getA().equals(GuardResult.LOCK_ACQUIRED)) {
393                   //
394                   // insert the operation into memory
395                   //
396                   insert(operation);
397
398                   //
399                   // insert operation timeout object
400                   //
401                   OperationTimer opTimer = new OperationTimer();
402                   opTimer.setClosedLoopControlName($event.getClosedLoopControlName());
403                   opTimer.setRequestID($event.getRequestId().toString());
404                   opTimer.setDelay(operation.getOperationTimeout().toString() + "s");
405                   insert(opTimer);
406
407                   //
408                   // Insert lock into memory
409                   //
410                   insert(result.getB());
411               }
412               else {
413                   logger.debug("The target resource {} is already processing",
414                                 $event.getAai().get($event.getTarget()));
415                   notification = new VirtualControlLoopNotification($event);
416                   notification.setNotification(ControlLoopNotificationType.REJECTED);
417                   notification.setMessage("The target " + $event.getAai().get($event.getTarget()) + " is already locked");
418                   notification.setFrom("policy");
419                   notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
420                   notification.setPolicyScope($params.getPolicyScope());
421                   notification.setPolicyVersion($params.getPolicyVersion());
422
423                   PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
424
425                   retract($event);
426                   retract($manager);
427                   retract($clTimer);
428
429                 if(result.getB() != null) {
430                   retract(result.getB());
431                 }
432               }
433               logger.info("{}: {}: starting operation={}",
434                           $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
435                           operation);
436             } else {
437               //
438               // Probably waiting for abatement
439               //
440               logger.info("{}: {}: no operation, probably waiting for abatement",
441                           $clName, $params.getPolicyName() + "." + drools.getRule().getName());
442             }
443         }
444     } catch (Exception e) {
445         logger.warn("{}: {}: unexpected",
446                         $clName,
447                         $params.getPolicyName() + "." + drools.getRule().getName(), e);
448
449         VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
450         notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
451         notification.setMessage(e.getMessage());
452         notification.setFrom("policy");
453         notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
454         notification.setPolicyScope($params.getPolicyScope());
455         notification.setPolicyVersion($params.getPolicyVersion());
456
457         PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
458
459         retract($event);
460         retract($manager);
461         retract($clTimer);
462     }
463
464 end
465
466 /*
467 *
468 * Guard Permitted, let's send request to the actor.
469 *
470 */
471 rule "EVENT.MANAGER.OPERATION.LOCKED.GUARD_PERMITTED"
472     when
473         $params : ControlLoopParams( $clName : getClosedLoopControlName() )
474         $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
475         $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
476         $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId(), "Permit".equalsIgnoreCase(getGuardApprovalStatus()) )
477         $lock : TargetLock (requestID == $event.getRequestId())
478         $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
479     then
480
481     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
482     logger.info("{}: {}: event={} manager={} operation={} lock={}",
483                 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
484                 $event, $manager, $operation, $lock);
485
486     Object request = null;
487     boolean caughtException = false;
488
489     try {
490         request = $operation.startOperation($event);
491
492         if (request != null) {
493           logger.debug("{}: {}: starting operation ..",
494                        $clName,
495                        $params.getPolicyName() + "." + drools.getRule().getName());
496           //
497           // Tell interested parties we are performing this Operation
498           //
499           VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
500           notification.setNotification(ControlLoopNotificationType.OPERATION);
501           notification.setMessage($operation.getOperationMessage());
502           notification.setHistory($operation.getHistory());
503           notification.setFrom("policy");
504           notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
505           notification.setPolicyScope($params.getPolicyScope());
506           notification.setPolicyVersion($params.getPolicyVersion());
507
508           PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
509
510           switch ($operation.policy.getActor()){
511
512               case "APPC":
513
514                   if (request instanceof Request) {
515                       PolicyEngine.manager.deliver("APPC-CL", request);
516                   }
517                   else if (request instanceof LcmRequestWrapper) {
518                       PolicyEngine.manager.deliver("APPC-LCM-READ", request);
519                   }
520                   break;
521               case "SO":
522                   // at this point the AAI named query request should have already been made, the response recieved and used
523                   // in the construction of the SO Request which is stored in operationRequest
524
525                   if(request instanceof SORequest) {
526                       // Call SO. The response will be inserted into memory once it's received
527                       SOActorServiceProvider.sendRequest($event.getRequestId().toString(), drools.getWorkingMemory(), request);
528                   }
529                   break;
530               case "VFC":
531                   if (request instanceof VFCRequest) {
532                       // Start VFC thread
533                       Thread t = new Thread(new VFCManager(drools.getWorkingMemory(), (VFCRequest)request));
534                       t.start();
535                   }
536                   break;
537           }
538         } else {
539           //
540           // What happens if its null?
541           //
542             logger.warn("{}: {}: unexpected null operation request",
543                       $clName,
544                       $params.getPolicyName() + "." + drools.getRule().getName());
545             if ("SO".equals($operation.policy.getActor())) {
546                 retract($opTimer);
547                 retract($operation);
548                 modify($manager) {finishOperation($operation)};
549             }
550             else if ("vfc".equalsIgnoreCase($operation.policy.getActor())) {
551                 retract($opTimer);
552                 retract($operation);
553                 modify($manager) {finishOperation($operation)};
554             }
555         }
556
557     } catch (Exception e) {
558         String msg = e.getMessage();
559         logger.warn("{}: {}: operation={}:  AAI failure: {}",
560                     $clName,
561                     $params.getPolicyName() + "." + drools.getRule().getName(),
562                     $operation, msg, e);
563         $operation.setOperationHasException(msg);
564
565         if(request != null) {
566             //
567             // Create a notification for it ("DB Write - end operation")
568             //
569             VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
570             notification.setFrom("policy");
571             notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
572             notification.setPolicyScope($params.getPolicyScope());
573             notification.setPolicyVersion($params.getPolicyVersion());
574             notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
575             notification.setMessage($operation.getOperationHistory());
576             notification.setHistory($operation.getHistory());
577
578             PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
579         }
580
581         retract($opTimer);
582         retract($operation);
583         caughtException = true;
584     }
585
586     // Having the modify statement in the catch clause doesn't work for whatever reason
587     if (caughtException) {
588         modify($manager) {finishOperation($operation)};
589     }
590 end
591
592
593 /*
594 *
595 * We were able to acquire a lock so now let's ask Xacml Guard whether
596 * we are allowed to proceed with the request to the actor.
597 *
598 */
599 rule "EVENT.MANAGER.OPERATION.LOCKED.GUARD_NOT_YET_QUERIED"
600     when
601         $params : ControlLoopParams( $clName : getClosedLoopControlName() )
602         $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
603         $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
604         $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId(), getGuardApprovalStatus() == "NONE" )
605         $lock : TargetLock (requestID == $event.getRequestId())
606     then
607
608     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
609     logger.info("{}: {}: event={} manager={} operation={} lock={}",
610                 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
611                 $event, $manager, $operation, $lock);
612
613     //
614     // Sending notification that we are about to query Guard ("DB write - start operation")
615     //
616     VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
617     notification.setNotification(ControlLoopNotificationType.OPERATION);
618     notification.setMessage("Sending guard query for " + $operation.policy.getActor() + " " + $operation.policy.getRecipe());
619     notification.setHistory($operation.getHistory());
620     notification.setFrom("policy");
621     notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
622     notification.setPolicyScope($params.getPolicyScope());
623     notification.setPolicyVersion($params.getPolicyVersion());
624
625     PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
626
627     //
628     // Now send Guard Request to XACML Guard. In order to bypass the call to Guard,
629     // just change guardEnabled to false.
630     //
631     // In order to use REST XACML, provide a URL instead of "" as a second argument
632     // to the CallGuardTask() and set the first argument to null
633     // (instead of XacmlPdpEngine).
634     //
635
636     // NOTE: The environment properties uses "guard.disabled" but the boolean is guardEnabled
637     boolean guardEnabled = "false".equalsIgnoreCase(PolicyEngine.manager.getEnvironmentProperty("guard.disabled"));
638
639     if(guardEnabled){
640
641         Thread t = new Thread(new org.onap.policy.guard.CallGuardTask(
642                                                         drools.getWorkingMemory(),
643                                                         $event.getClosedLoopControlName(),
644                                                         $operation.policy.getActor().toString(),
645                                                         $operation.policy.getRecipe(),
646                                                         $operation.getTargetEntity(),
647                                                         $event.getRequestId().toString()
648                                                         ));
649         t.start();
650     }
651     else{
652         insert(new PolicyGuardResponse("Permit", $event.getRequestId(), $operation.policy.getRecipe()));
653     }
654
655 end
656
657 //
658 // This rule will be triggered when a thread talking to the XACML Guard inserts a
659 // guardResponse object into the working memory
660 //
661 rule "GUARD.RESPONSE"
662     when
663         $params : ControlLoopParams( $clName : getClosedLoopControlName() )
664         $event : VirtualControlLoopEvent( closedLoopControlName == $clName, closedLoopEventStatus == ControlLoopEventStatus.ONSET )
665         $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
666         $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() )
667         $lock : TargetLock (requestID == $event.getRequestId())
668         $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
669         $guardResponse : PolicyGuardResponse(requestID == $event.getRequestId(), $operation.policy.recipe == operation)
670     then
671
672     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
673     logger.info("{}: {}: event={} manager={} operation={} lock={} opTimer={} guardResponse={}",
674                  $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
675                  $event, $manager, $operation, $lock, $opTimer, $guardResponse);
676
677
678     //we will permit the operation if there was no Guard for it
679     if("Indeterminate".equalsIgnoreCase($guardResponse.getResult())){
680         $guardResponse.setResult("Permit");
681     }
682
683     //
684     // This notification has Guard result in "message". ("DB write - end operation in case of Guard Deny")
685     //
686     VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
687     notification.setNotification(ControlLoopNotificationType.OPERATION);
688     notification.setMessage("Guard result for " + $operation.policy.getActor() + " " + $operation.policy.getRecipe() + " is " + $guardResponse.getResult());
689     notification.setHistory($operation.getHistory());
690     notification.setFrom("policy");
691     notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
692     notification.setPolicyScope($params.getPolicyScope());
693     notification.setPolicyVersion($params.getPolicyVersion());
694
695     PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
696
697     if("Permit".equalsIgnoreCase($guardResponse.getResult())){
698
699         modify($operation){setGuardApprovalStatus($guardResponse.getResult())};
700     }
701     else {
702         //This is the Deny case
703         $operation.startOperation($event);
704         $operation.setOperationHasGuardDeny();
705         retract($opTimer);
706         retract($operation);
707         modify($manager) {finishOperation($operation)};
708     }
709
710     retract($guardResponse);
711
712 end
713
714 /*
715 *
716 * This rule responds to APPC Response Events
717 *
718 * I would have like to be consistent and write the Response like this:
719 * $response : Response( CommonHeader.RequestId == $onset.getRequestId() )
720 *
721 * However, no compile error was given. But a runtime error was given. I think
722 * because drools is confused between the classname CommonHeader vs the property CommonHeader.
723 *
724 */
725 rule "APPC.RESPONSE"
726     when
727         $params : ControlLoopParams( $clName : getClosedLoopControlName() )
728         $event : VirtualControlLoopEvent( closedLoopControlName == $clName, closedLoopEventStatus == ControlLoopEventStatus.ONSET )
729         $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
730         $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() )
731         $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
732         $lock : TargetLock (requestID == $event.getRequestId())
733         $response : Response( getCommonHeader().RequestId == $event.getRequestId() )
734     then
735
736     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
737     logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
738     logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
739                  $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
740                  $event, $manager, $operation, $lock, $opTimer, $response);
741     //
742     // Get the result of the operation
743     //
744     PolicyResult policyResult = $operation.onResponse($response);
745     if (policyResult != null) {
746         logger.debug("{}: {}: operation finished - result={}",
747                     $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
748                     policyResult);
749         //
750         // This Operation has completed, construct a notification showing our results. (DB write - end operation)
751         //
752         VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
753         notification.setFrom("policy");
754         notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
755         notification.setPolicyScope($params.getPolicyScope());
756         notification.setPolicyVersion($params.getPolicyVersion());
757         notification.setMessage($operation.getOperationHistory());
758         notification.setHistory($operation.getHistory());
759         if (policyResult.equals(PolicyResult.SUCCESS)) {
760             notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
761             //
762             // Let interested parties know
763             //
764             PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
765         } else {
766             notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
767             //
768             // Let interested parties know
769             //
770             PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
771         }
772         //
773         // Ensure the operation is complete
774         //
775         if ($operation.isOperationComplete() == true) {
776             //
777             // It is complete, remove it from memory
778             //
779             retract($operation);
780             //
781             // We must also retract the timer object
782             // NOTE: We could write a Rule to do this
783             //
784             retract($opTimer);
785             //
786             // Complete the operation
787             //
788             modify($manager) {finishOperation($operation)};
789         } else {
790             //
791             // Just doing this will kick off the LOCKED rule again
792             //
793             modify($operation) {};
794         }
795     } else {
796         //
797         // Its not finished yet (i.e. expecting more Response objects)
798         //
799         // Or possibly it is a leftover response that we timed the request out previously
800         //
801     }
802     //
803     // We are going to retract these objects from memory
804     //
805     retract($response);
806 end
807
808 /*
809 *
810 * The problem with Responses is that they don't have a controlLoopControlName
811 * field in them, so the only way to attach them is via RequestID. If we have multiple
812 * control loop .drl's loaded in the same container, we need to be sure the cleanup
813 * rules don't remove Responses for other control loops.
814 *
815 */
816 rule "APPC.RESPONSE.CLEANUP"
817     when
818         $params : ControlLoopParams( $clName : getClosedLoopControlName() )
819         $response : Response($id : getCommonHeader().RequestId )
820         not ( VirtualControlLoopEvent( requestId == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
821     then
822
823     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
824     logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
825     logger.debug("{}: {}: orphan appc response={}",
826                 $clName, $params.getPolicyName() + "." + drools.getRule().getName(), $id);
827
828     //
829     // Retract it
830     //
831     retract($response);
832 end
833
834 /*
835 *
836 * This rule responds to APPC Response Events using the new LCM interface provided by appc
837 *
838 */
839 rule "APPC.LCM.RESPONSE"
840     when
841         $params : ControlLoopParams( $clName : getClosedLoopControlName() )
842         $event : VirtualControlLoopEvent( closedLoopControlName == $clName, closedLoopEventStatus == ControlLoopEventStatus.ONSET )
843         $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
844         $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() )
845         $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
846         $lock : TargetLock (requestID == $event.getRequestId())
847         $response : LcmResponseWrapper( getBody().getCommonHeader().getRequestId() == $event.getRequestId() )
848     then
849
850     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
851     logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
852     logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
853                 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
854                 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
855
856     //
857     // Get the result of the operation
858     //
859     PolicyResult policyResult = $operation.onResponse($response);
860     if (policyResult != null) {
861       logger.debug("{}: {}: operation finished - result={}",
862                   $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
863                   policyResult);
864
865       //
866       // This Operation has completed, construct a notification showing our results. (DB write - end operation)
867       //
868       VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
869       notification.setFrom("policy");
870       notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
871       notification.setPolicyScope($params.getPolicyScope());
872       notification.setPolicyVersion($params.getPolicyVersion());
873       notification.setMessage($operation.getOperationHistory());
874       notification.setHistory($operation.getHistory());
875       if (policyResult.equals(PolicyResult.SUCCESS)) {
876           notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
877       } else {
878           notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
879       }
880       PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
881       //
882       // Ensure the operation is complete
883       //
884       if ($operation.isOperationComplete() == true) {
885           //
886           // It is complete, remove it from memory
887           //
888           retract($operation);
889           //
890           // We must also retract the timer object
891           // NOTE: We could write a Rule to do this
892           //
893           retract($opTimer);
894           //
895           // Complete the operation
896           //
897           modify($manager) {finishOperation($operation)};
898       } else {
899           //
900           // Just doing this will kick off the LOCKED rule again
901           //
902           modify($operation) {};
903       }
904     } else {
905         //
906         // Its not finished yet (i.e. expecting more Response objects)
907         //
908         // Or possibly it is a leftover response that we timed the request out previously
909         //
910     }
911     //
912     // We are going to retract these objects from memory
913     //
914     retract($response);
915 end
916
917 /*
918 *
919 * Clean Up any lingering LCM reponses
920 *
921 */
922 rule "APPC.LCM.RESPONSE.CLEANUP"
923     when
924         $params : ControlLoopParams( $clName : getClosedLoopControlName() )
925         $response : LcmResponseWrapper($id : getBody().getCommonHeader().getRequestId )
926         not ( VirtualControlLoopEvent( requestId == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
927     then
928
929     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
930     logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
931     logger.debug("{}: {}: orphan appc response={}",
932                 $clName, $params.getPolicyName() + "." + drools.getRule().getName(), $id);
933     //
934     // Retract it
935     //
936     retract($response);
937 end
938
939 /*
940 *
941 * This rule responds to SO Response Events
942 *
943 */
944 rule "SO.RESPONSE"
945     when
946         $params : ControlLoopParams( $clName : getClosedLoopControlName() )
947         $event : VirtualControlLoopEvent( closedLoopControlName == $clName, closedLoopEventStatus == ControlLoopEventStatus.ONSET )
948         $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
949         $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() )
950         $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
951         $lock : TargetLock (requestID == $event.getRequestId())
952         $response : SOResponseWrapper(requestID.toString() == $event.getRequestId().toString() )
953     then
954
955     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
956     logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
957     logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
958                 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
959                 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
960
961     // Get the result of the operation
962     //
963     PolicyResult policyResult = $operation.onResponse($response);
964     if (policyResult != null) {
965         logger.debug("{}: {}: operation finished - result={}",
966                     $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
967                     policyResult);
968
969         //
970         // This Operation has completed, construct a notification showing our results
971         //
972         VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
973         notification.setFrom("policy");
974         notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
975         notification.setPolicyScope($params.getPolicyScope());
976         notification.setPolicyVersion($params.getPolicyVersion());
977         notification.setMessage($operation.getOperationHistory());
978         notification.setHistory($operation.getHistory());
979         if (policyResult.equals(PolicyResult.SUCCESS)) {
980             notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
981         } else {
982             notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
983
984         }
985         PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
986         //
987         // Ensure the operation is complete
988         //
989         if ($operation.isOperationComplete() == true) {
990             //
991             // It is complete, remove it from memory
992             //
993             retract($operation);
994             //
995             // We must also retract the timer object
996             // NOTE: We could write a Rule to do this
997             //
998             retract($opTimer);
999             //
1000             // Complete the operation
1001             //
1002             modify($manager) {finishOperation($operation)};
1003         } else {
1004             //
1005             // Just doing this will kick off the LOCKED rule again
1006             //
1007             modify($operation) {};
1008         }
1009     } else {
1010         //
1011         // Its not finished yet (i.e. expecting more Response objects)
1012         //
1013         // Or possibly it is a leftover response that we timed the request out previously
1014         //
1015     }
1016     //
1017     // We are going to retract these objects from memory
1018     //
1019     retract($response);
1020
1021 end
1022
1023 /*
1024 *
1025 * This rule responds to VFC Response Events
1026 *
1027 */
1028 rule "VFC.RESPONSE"
1029     when
1030         $params : ControlLoopParams( $clName : getClosedLoopControlName() )
1031         $event : VirtualControlLoopEvent( closedLoopControlName == $clName, closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1032         $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
1033         $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() )
1034         $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() )
1035         $lock : TargetLock (requestID == $event.getRequestId())
1036         $response : VFCResponse( requestId.toString() == $event.getRequestId().toString() )
1037     then
1038         Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1039         logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
1040         logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}",
1041                     $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
1042                     $event, $manager, $operation, $lock, $operation, $opTimer, $response);
1043
1044         // Get the result of the operation
1045         //
1046         PolicyResult policyResult = $operation.onResponse($response);
1047         if (policyResult != null) {
1048             //
1049             // This Operation has completed, construct a notification showing our results
1050             //
1051             VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1052             notification.setFrom("policy");
1053             notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
1054             notification.setPolicyScope($params.getPolicyScope());
1055             notification.setPolicyVersion($params.getPolicyVersion());
1056             notification.setMessage($operation.getOperationHistory());
1057             notification.setHistory($operation.getHistory());
1058             //
1059             // Ensure the operation is complete
1060             //
1061             if ($operation.isOperationComplete() == true) {
1062                 //
1063                 // It is complete, remove it from memory
1064                 //
1065                 retract($operation);
1066                 //
1067                 // We must also retract the timer object
1068                 // NOTE: We could write a Rule to do this
1069                 //
1070                 retract($opTimer);
1071                 //
1072                 // Complete the operation
1073                 //
1074                 modify($manager) {finishOperation($operation)};
1075             } else {
1076                 //
1077                 // Just doing this will kick off the LOCKED rule again
1078                 //
1079                 modify($operation) {};
1080             }
1081         } else {
1082             //
1083             // Its not finished yet (i.e. expecting more Response objects)
1084             //
1085             // Or possibly it is a leftover response that we timed the request out previously
1086             //
1087         }
1088         //
1089         // We are going to retract these objects from memory
1090         //
1091         retract($response);
1092
1093 end
1094
1095 /*
1096 *
1097 * This is the timer that manages the timeout for an individual operation.
1098 *
1099 */
1100 rule "EVENT.MANAGER.OPERATION.TIMEOUT"
1101     timer (expr: $to )
1102     when
1103         $params : ControlLoopParams( $clName : getClosedLoopControlName() )
1104         $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
1105         $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
1106         $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() )
1107         $opTimer : OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString(), $to : getDelay() )
1108         $lock : TargetLock (requestID == $event.getRequestId())
1109     then
1110
1111     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1112     logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
1113     logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={}",
1114                 $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
1115                 $event, $manager, $operation, $lock, $operation, $opTimer);
1116
1117     //
1118     // Tell it its timed out
1119     //
1120     $operation.setOperationHasTimedOut();
1121     //
1122     // Create a notification for it ("DB Write - end operation")
1123     //
1124     VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1125     notification.setFrom("policy");
1126     notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
1127     notification.setPolicyScope($params.getPolicyScope());
1128     notification.setPolicyVersion($params.getPolicyVersion());
1129     notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
1130     notification.setMessage($operation.getOperationHistory());
1131     notification.setHistory($operation.getHistory());
1132     //
1133     // Let interested parties know
1134     //
1135     PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1136     //
1137     // Get rid of the timer
1138     //
1139     retract($opTimer);
1140     //
1141     // Ensure the operation is complete
1142     //
1143     if ($operation.isOperationComplete() == true) {
1144         //
1145         // It is complete, remove it from memory
1146         //
1147         retract($operation);
1148         //
1149         // Complete the operation
1150         //
1151         modify($manager) {finishOperation($operation)};
1152     } else {
1153         //
1154         // Just doing this will kick off the LOCKED rule again
1155         //
1156         modify($operation) {};
1157     }
1158 end
1159
1160 /*
1161 *
1162 * This is the timer that manages the overall control loop timeout.
1163 *
1164 */
1165 rule "EVENT.MANAGER.TIMEOUT"
1166     timer (expr: $to )
1167     when
1168         $params : ControlLoopParams( $clName : getClosedLoopControlName() )
1169         $event : VirtualControlLoopEvent( closedLoopControlName == $clName )
1170         $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() )
1171         $clTimer : ControlLoopTimer ( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString(), $to : getDelay() )
1172     then
1173
1174     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1175     logger.info("{}: {}", $clName, $params.getPolicyName() + "." + drools.getRule().getName());
1176
1177     logger.debug("{}: {}: event={}",
1178               $clName, $params.getPolicyName() + "." + drools.getRule().getName(),
1179               $event);
1180     //
1181     // Tell the Event Manager it has timed out
1182     //
1183     VirtualControlLoopNotification notification = $manager.setControlLoopTimedOut();
1184     if (notification != null) {
1185         notification.setFrom("policy");
1186         notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
1187         notification.setPolicyScope($params.getPolicyScope());
1188         notification.setPolicyVersion($params.getPolicyVersion());
1189         //
1190         // Let interested parties know
1191         //
1192         PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1193     }
1194     //
1195     // Retract the event
1196     //
1197     retract($event);
1198 end
1199
1200 /*
1201 *
1202 * This rule cleans up the manager and other objects after an event has
1203 * been retracted.
1204 *
1205 */
1206 rule "EVENT.MANAGER.CLEANUP"
1207     when
1208         $manager : ControlLoopEventManager( $clName : getClosedLoopControlName(), $requestId : getRequestID() )
1209         $clTimer : ControlLoopTimer ( closedLoopControlName == $clName, requestID == $requestId.toString() )
1210         $operations : LinkedList()
1211                         from collect( ControlLoopOperationManager( onset.closedLoopControlName == $clName, onset.getRequestId() == $requestId ) )
1212         $opTimers : LinkedList()
1213                         from collect( OperationTimer( closedLoopControlName == $clName, requestID == $requestId.toString() ) )
1214         $locks : LinkedList()
1215                         from collect( TargetLock (requestID == $requestId) )
1216         not( VirtualControlLoopEvent( closedLoopControlName == $clName, requestId == $requestId ) )
1217     then
1218
1219     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1220     logger.info("{}: {}", $clName, drools.getRule().getName());
1221
1222     logger.debug("{}: {}: manager={} clTimer={} operations={}",
1223               $clName, drools.getRule().getName(),
1224               $manager, $clTimer, $operations.size());
1225
1226     //
1227     // Retract EVERYTHING
1228     //
1229     retract($manager);
1230     retract($clTimer);
1231
1232     for(Object manager: $operations) {
1233         retract((ControlLoopOperationManager) manager);
1234     }
1235     for(Object opTimer: $opTimers) {
1236         retract((OperationTimer) opTimer);
1237     }
1238     for(Object lock: $locks) {
1239         TargetLock tgt = (TargetLock) lock;
1240         //
1241         // Ensure we release the lock
1242         //
1243         PolicyGuard.unlockTarget(tgt);
1244         retract(tgt);
1245     }
1246 end
1247
1248 /*
1249 *
1250 * This rule will clean up any rogue onsets where there is no
1251 * ControlLoopParams object corresponding to the onset event.
1252 *
1253 */
1254 rule "EVENT.CLEANUP"
1255     when
1256         $event : VirtualControlLoopEvent( $clName: closedLoopControlName )
1257         not ( ControlLoopParams( getClosedLoopControlName() == $clName) )
1258     then
1259
1260     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1261     logger.info("{}: {}", $clName, drools.getRule().getName());
1262     logger.debug("{}: {}: orphan onset event={}",
1263                 $clName, drools.getRule().getName(), $event);
1264
1265     retract($event);
1266 end