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