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