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