a29941607609f16f2a58705550017f063040d156
[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         String msg = e.getMessage();
526         logger.warn("{}: {}: operation={}:  AAI failure: {}", 
527                     $params.getClosedLoopControlName(), drools.getRule().getName(),
528                     $operation, msg, e);
529         $operation.setOperationHasException(msg);
530         retract($opTimer);
531         retract($operation);
532         caughtException = true;
533     }
534     
535     // Having the modify statement in the catch clause doesn't work for whatever reason
536     if (caughtException) {
537         modify($manager) {finishOperation($operation)};
538     }
539     else if (request != null) {
540       logger.debug("{}: {}: starting operation ..", 
541                    $params.getClosedLoopControlName(), drools.getRule().getName());
542       //
543       // Tell interested parties we are performing this Operation
544       //
545       VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
546       notification.notification = ControlLoopNotificationType.OPERATION;
547       notification.message = $operation.getOperationMessage();
548       notification.history = $operation.getHistory();
549       notification.from = "policy";
550       notification.policyName = drools.getRule().getName();
551       notification.policyScope = "${policyScope}";
552       notification.policyVersion = "${policyVersion}";
553       
554       PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
555       
556       switch ($operation.policy.getActor()){
557           
558           case "APPC":
559       
560               if (request instanceof Request) {
561                   PolicyEngine.manager.deliver("APPC-CL", request);
562               }
563               else if (request instanceof LCMRequestWrapper) {
564                   PolicyEngine.manager.deliver("APPC-LCM-READ", request);
565               }
566               break;
567           case "SO":
568               // at this point the AAI named query request should have already been made, the response recieved and used
569               // in the construction of the SO Request which is stored in operationRequest
570               
571               if(request instanceof SORequest) {
572                   // Call SO. The response will be inserted into memory once it's received 
573                   SOActorServiceProvider.sendRequest(drools.getWorkingMemory(), request);                        
574               }
575               break;
576           case "VFC":
577               if (request instanceof VFCRequest) {
578                   // Start VFC thread
579                   Thread t = new Thread(new VFCManager(drools.getWorkingMemory(), (VFCRequest)request));
580                   t.start();
581               }          
582               break;
583       }
584     } else {
585       //
586       // What happens if its null?
587       //
588                 logger.warn("{}: {}: unexpected null operation request", 
589                   $params.getClosedLoopControlName(), 
590                   drools.getRule().getName());
591                 if ("SO".equals($operation.policy.getActor())) {
592                     retract($opTimer);
593                         retract($operation);
594                         modify($manager) {finishOperation($operation)};
595                 }
596
597     }
598 end
599
600
601 /*
602 *
603 * We were able to acquire a lock so now let's ask Xacml Guard whether 
604 * we are allowed to proceed with the request to the actor.
605 *
606 */
607 rule "${policyName}.EVENT.MANAGER.OPERATION.LOCKED.GUARD_NOT_YET_QUERIED"
608     when
609         $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
610         $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
611         $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
612         $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID, getGuardApprovalStatus() == "NONE" )
613         $lock : TargetLock (requestID == $event.requestID)
614     then
615
616     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
617     logger.info("{}: {}: event={} manager={} operation={} lock={}", 
618                 $params.getClosedLoopControlName(), drools.getRule().getName(),
619                 $event, $manager, $operation, $lock);
620     
621     //
622     // Sending notification that we are about to query Guard ("DB write - start operation")
623     //
624     VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
625     notification.notification = ControlLoopNotificationType.OPERATION;
626     notification.message = "Sending guard query for " + $operation.policy.getActor() + " " + $operation.policy.getRecipe();
627     notification.history = $operation.getHistory();
628     notification.from = "policy";
629     notification.policyName = drools.getRule().getName();
630     notification.policyScope = "${policyScope}";
631     notification.policyVersion = "${policyVersion}";
632     
633     PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
634         
635     //
636     // Now send Guard Request to XACML Guard. In order to bypass the call to Guard, 
637     // just change guardEnabled to false.
638     // 
639     // In order to use REST XACML, provide a URL instead of "" as a second argument 
640     // to the CallGuardTask() and set the first argument to null 
641     // (instead of XacmlPdpEngine).
642     //
643     
644     // NOTE: The environment properties uses "guard.disabled" but the boolean is guardEnabled
645     boolean guardEnabled = "false".equalsIgnoreCase(PolicyEngine.manager.getEnvironmentProperty("guard.disabled"));
646     
647     if(guardEnabled){
648     
649         Thread t = new Thread(new org.onap.policy.guard.CallGuardTask(
650                                                         drools.getWorkingMemory(),
651                                                         $event.closedLoopControlName,
652                                                         $operation.policy.getActor().toString(),
653                                                         $operation.policy.getRecipe(),
654                                                         $manager.getTargetInstance($operation.policy),
655                                                         //$event.target,
656                                                         $event.requestID.toString()
657                                                         ));
658         t.start();
659     }
660     else{
661         insert(new PolicyGuardResponse("Permit", $event.requestID, $operation.policy.getRecipe()));
662     }
663
664 end
665
666 //
667 // This rule will be triggered when a thread talking to the XACML Guard inserts a 
668 // guardResponse object into the working memory
669 //
670 rule "${policyName}.GUARD.RESPONSE"
671     when
672         $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
673         $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
674         $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID ) 
675         $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
676         $lock : TargetLock (requestID == $event.requestID)
677         $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
678         $guardResponse : PolicyGuardResponse(requestID == $event.requestID, $operation.policy.recipe == operation)
679     then
680
681     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
682     logger.info("{}: {}: event={} manager={} operation={} lock={} opTimer={} guardResponse={}", 
683                  $params.getClosedLoopControlName(), drools.getRule().getName(),
684                  $event, $manager, $operation, $lock, $opTimer, $guardResponse);
685         
686         
687     //we will permit the operation if there was no Guard for it
688     if("Indeterminate".equalsIgnoreCase($guardResponse.result)){
689         $guardResponse.result = "Permit";
690     }
691     
692     //
693     // This notification has Guard result in "message". ("DB write - end operation in case of Guard Deny")
694     //
695     VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
696     notification.notification = ControlLoopNotificationType.OPERATION;
697     notification.message = "Guard result for " + $operation.policy.getActor() + " " + $operation.policy.getRecipe() + " is " + $guardResponse.result;
698     notification.history = $operation.getHistory();
699     notification.from = "policy";
700     notification.policyName = drools.getRule().getName();
701     notification.policyScope = "${policyScope}";
702     notification.policyVersion = "${policyVersion}";
703     
704     PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
705     
706     if("Permit".equalsIgnoreCase($guardResponse.result)){
707     
708         modify($operation){setGuardApprovalStatus($guardResponse.result)};
709     }
710     else {
711         //This is the Deny case
712         $operation.setOperationHasGuardDeny();
713         retract($opTimer);
714         retract($operation);
715         modify($manager) {finishOperation($operation)};
716     }
717     
718     retract($guardResponse);
719             
720 end
721
722 /*
723 *
724 * This rule responds to APPC Response Events
725 *
726 * I would have like to be consistent and write the Response like this:
727 * $response : Response( CommonHeader.RequestID == $onset.requestID )
728 *
729 * However, no compile error was given. But a runtime error was given. I think
730 * because drools is confused between the classname CommonHeader vs the property CommonHeader.
731 *
732 */
733 rule "${policyName}.APPC.RESPONSE"
734     when
735         $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
736         $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET ) 
737         $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
738         $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
739         $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
740         $lock : TargetLock (requestID == $event.requestID)
741         $response : Response( getCommonHeader().RequestID == $event.requestID )
742     then
743
744     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
745     logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
746     logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}", 
747                  $params.getClosedLoopControlName(), drools.getRule().getName(),
748                  $event, $manager, $operation, $lock, $opTimer, $response);
749     //
750     // Get the result of the operation
751     //
752     PolicyResult policyResult = $operation.onResponse($response);
753     if (policyResult != null) {
754         logger.debug("{}: {}: operation finished - result={}", 
755                     $params.getClosedLoopControlName(), drools.getRule().getName(),
756                     policyResult);
757         //
758         // This Operation has completed, construct a notification showing our results. (DB write - end operation)
759         //
760         VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
761         notification.from = "policy";
762         notification.policyName = drools.getRule().getName();
763         notification.policyScope = "${policyScope}";
764         notification.policyVersion = "${policyVersion}";
765         notification.message = $operation.getOperationHistory();
766         notification.history = $operation.getHistory();
767         if (policyResult.equals(PolicyResult.SUCCESS)) {
768             notification.notification = ControlLoopNotificationType.OPERATION_SUCCESS;
769             //
770             // Let interested parties know
771             //
772             PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
773         } else {
774             notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
775             //
776             // Let interested parties know
777             //
778             PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
779         }
780         //
781         // Ensure the operation is complete
782         //
783         if ($operation.isOperationComplete() == true) {
784             //
785             // It is complete, remove it from memory
786             //
787             retract($operation);
788             //
789             // We must also retract the timer object
790             // NOTE: We could write a Rule to do this
791             //
792             retract($opTimer);
793             //
794             // Complete the operation
795             //
796             modify($manager) {finishOperation($operation)};
797         } else {
798             //
799             // Just doing this will kick off the LOCKED rule again
800             //
801             modify($operation) {};
802         }
803     } else {
804         //
805         // Its not finished yet (i.e. expecting more Response objects)
806         //
807         // Or possibly it is a leftover response that we timed the request out previously
808         //
809     }
810     //
811     // We are going to retract these objects from memory
812     //
813     retract($response);
814 end
815
816 /*
817 *
818 * The problem with Responses is that they don't have a controlLoopControlName
819 * field in them, so the only way to attach them is via RequestID. If we have multiple
820 * control loop .drl's loaded in the same container, we need to be sure the cleanup
821 * rules don't remove Responses for other control loops.
822 *
823 */
824 rule "${policyName}.APPC.RESPONSE.CLEANUP"
825     when
826         $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
827         $response : Response($id : getCommonHeader().RequestID )
828         not ( VirtualControlLoopEvent( requestID == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) ) 
829     then
830
831     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
832     logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
833     logger.debug("{}: {}: orphan appc response={}", 
834                 $params.getClosedLoopControlName(), drools.getRule().getName(), $id);
835         
836     //
837     // Retract it
838     //
839     retract($response);
840 end
841
842 /*
843 *
844 * This rule responds to APPC Response Events using the new LCM interface provided by appc
845 *
846 */
847 rule "${policyName}.APPC.LCM.RESPONSE"
848     when
849         $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
850         $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET ) 
851         $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
852         $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
853         $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
854         $lock : TargetLock (requestID == $event.requestID)
855         $response : LCMResponseWrapper( getBody().getCommonHeader().getRequestId() == $event.requestID )
856     then
857
858     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
859     logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
860     logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}", 
861                 $params.getClosedLoopControlName(), drools.getRule().getName(),
862                 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
863     
864     //
865     // Get the result of the operation
866     //
867     PolicyResult policyResult = $operation.onResponse($response);
868     if (policyResult != null) {
869       logger.debug("{}: {}: operation finished - result={}", 
870                   $params.getClosedLoopControlName(), drools.getRule().getName(),
871                   policyResult);
872       
873       //
874       // This Operation has completed, construct a notification showing our results. (DB write - end operation)
875       //
876       VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
877       notification.from = "policy";
878       notification.policyName = drools.getRule().getName();
879       notification.policyScope = "${policyScope}";
880       notification.policyVersion = "${policyVersion}";
881       notification.message = $operation.getOperationHistory();
882       notification.history = $operation.getHistory();
883       if (policyResult.equals(PolicyResult.SUCCESS)) {
884           notification.notification = ControlLoopNotificationType.OPERATION_SUCCESS;
885       } else {
886           notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
887       }
888       PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
889       //
890       // Ensure the operation is complete
891       //
892       if ($operation.isOperationComplete() == true) {
893           //
894           // It is complete, remove it from memory
895           //
896           retract($operation);
897           //
898           // We must also retract the timer object
899           // NOTE: We could write a Rule to do this
900           //
901           retract($opTimer);
902           //
903           // Complete the operation
904           //
905           modify($manager) {finishOperation($operation)};
906       } else {
907           //
908           // Just doing this will kick off the LOCKED rule again
909           //
910           modify($operation) {};
911       }
912     } else {
913         //
914         // Its not finished yet (i.e. expecting more Response objects)
915         //
916         // Or possibly it is a leftover response that we timed the request out previously
917         //
918     }
919     //
920     // We are going to retract these objects from memory
921     //
922     retract($response);
923 end
924
925 /*
926 *
927 * Clean Up any lingering LCM reponses
928 *
929 */
930 rule "${policyName}.APPC.LCM.RESPONSE.CLEANUP"
931     when
932         $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
933         $response : LCMResponseWrapper($id : getBody().getCommonHeader().getRequestId )
934         not ( VirtualControlLoopEvent( requestID == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) ) 
935     then
936     
937     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
938     logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
939     logger.debug("{}: {}: orphan appc response={}", 
940                 $params.getClosedLoopControlName(), drools.getRule().getName(), $id);
941     //
942     // Retract it
943     //
944     retract($response);
945 end
946
947 /*
948 *
949 * This rule responds to SO Response Events
950 *
951 */
952 rule "${policyName}.SO.RESPONSE"
953     when
954         $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
955         $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
956         $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName )
957         $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
958         $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
959         $lock : TargetLock (requestID == $event.requestID)
960         $response : SOResponse( requestReferences.requestId.toString() == $event.requestID.toString() )  
961     then
962         
963     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
964     logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
965     logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}", 
966                 $params.getClosedLoopControlName(), drools.getRule().getName(),
967                 $event, $manager, $operation, $lock, $operation, $opTimer, $response);
968         
969     // Get the result of the operation
970     //
971     PolicyResult policyResult = $operation.onResponse($response);
972     if (policyResult != null) {
973         logger.debug("{}: {}: operation finished - result={}", 
974                     $params.getClosedLoopControlName(), drools.getRule().getName(),
975                     policyResult);
976       
977         //
978         // This Operation has completed, construct a notification showing our results
979         //
980         VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
981         notification.from = "policy";
982         notification.policyName = drools.getRule().getName();
983         notification.policyScope = "${policyScope}";
984         notification.policyVersion = "${policyVersion}";
985         notification.message = $operation.getOperationHistory();
986         notification.history = $operation.getHistory();
987         if (policyResult.equals(PolicyResult.SUCCESS)) {
988             notification.notification = ControlLoopNotificationType.OPERATION_SUCCESS;
989         } else {
990             notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
991
992         }
993         PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
994         //
995         // Ensure the operation is complete
996         //
997         if ($operation.isOperationComplete() == true) {
998             //
999             // It is complete, remove it from memory
1000             //
1001             retract($operation);
1002             //
1003             // We must also retract the timer object
1004             // NOTE: We could write a Rule to do this
1005             //
1006             retract($opTimer);
1007             //
1008             // Complete the operation
1009             //
1010             modify($manager) {finishOperation($operation)};
1011         } else {
1012             //
1013             // Just doing this will kick off the LOCKED rule again
1014             //
1015             modify($operation) {};
1016         }
1017     } else {
1018         //
1019         // Its not finished yet (i.e. expecting more Response objects)
1020         //
1021         // Or possibly it is a leftover response that we timed the request out previously
1022         //
1023     }
1024     //
1025     // We are going to retract these objects from memory
1026     //
1027     retract($response);
1028
1029 end
1030
1031 /*
1032 *
1033 * This rule responds to VFC Response Events
1034 *
1035 */
1036 rule "${policyName}.VFC.RESPONSE"
1037         when
1038                 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1039                 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1040                 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName )
1041                 $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
1042                 $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() )
1043         $lock : TargetLock (requestID == $event.requestID)
1044                 $response : VFCResponse( requestId.toString() == $event.requestID.toString() )  
1045         then
1046                 Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1047         logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1048         logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={} response={}", 
1049                         $params.getClosedLoopControlName(), drools.getRule().getName(),
1050                         $event, $manager, $operation, $lock, $operation, $opTimer, $response);
1051                 
1052                 // Get the result of the operation
1053                 //
1054                 PolicyResult policyResult = $operation.onResponse($response);
1055                 if (policyResult != null) {
1056                         //
1057                         // This Operation has completed, construct a notification showing our results
1058                         //
1059                         VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1060                         notification.from = "policy";
1061                         notification.policyName = drools.getRule().getName();
1062                         notification.policyScope = "${policyScope}";
1063                         notification.policyVersion = "${policyVersion}";
1064                         notification.message = $operation.getOperationHistory();
1065                         notification.history = $operation.getHistory();
1066                         //
1067                         // Ensure the operation is complete
1068                         //
1069                         if ($operation.isOperationComplete() == true) {
1070                                 //
1071                                 // It is complete, remove it from memory
1072                                 //
1073                                 retract($operation);
1074                                 //
1075                                 // We must also retract the timer object
1076                                 // NOTE: We could write a Rule to do this
1077                                 //
1078                                 retract($opTimer);
1079                                 //
1080                                 // Complete the operation
1081                                 //
1082                                 modify($manager) {finishOperation($operation)};
1083                         } else {
1084                                 //
1085                                 // Just doing this will kick off the LOCKED rule again
1086                                 //
1087                                 modify($operation) {};
1088                         }
1089                 } else {
1090                         //
1091                         // Its not finished yet (i.e. expecting more Response objects)
1092                         //
1093                         // Or possibly it is a leftover response that we timed the request out previously
1094                         //
1095                 }
1096                 //
1097                 // We are going to retract these objects from memory
1098                 //
1099                 retract($response);
1100
1101 end
1102
1103 /*
1104 *
1105 * This is the timer that manages the timeout for an individual operation.
1106 *
1107 */
1108 rule "${policyName}.EVENT.MANAGER.OPERATION.TIMEOUT"
1109     timer (expr: $to )
1110     when
1111         $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1112         $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
1113         $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
1114         $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID )
1115         $opTimer : OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString(), $to : getDelay() )
1116         $lock : TargetLock (requestID == $event.requestID)
1117     then
1118     
1119     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1120     logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1121     logger.debug("{}: {}: event={} manager={} operation={} lock={} opTimer={}", 
1122                 $params.getClosedLoopControlName(), drools.getRule().getName(),
1123                 $event, $manager, $operation, $lock, $operation, $opTimer);
1124     
1125     //
1126     // Tell it its timed out
1127     //
1128     $operation.setOperationHasTimedOut();
1129     //
1130     // Create a notification for it ("DB Write - end operation")
1131     //
1132     VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1133     notification.from = "policy";
1134     notification.policyName = drools.getRule().getName();
1135     notification.policyScope = "${policyScope}";
1136     notification.policyVersion = "${policyVersion}";
1137     notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
1138     notification.message = $operation.getOperationHistory();
1139     notification.history = $operation.getHistory();
1140     //
1141     // Let interested parties know
1142     //
1143     PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1144     //
1145     // Get rid of the timer
1146     //
1147     retract($opTimer);
1148     //
1149     // Ensure the operation is complete
1150     //
1151     if ($operation.isOperationComplete() == true) {
1152         //
1153         // It is complete, remove it from memory
1154         //
1155         retract($operation);
1156         //
1157         // Complete the operation
1158         //
1159         modify($manager) {finishOperation($operation)};
1160     } else {
1161         //
1162         // Just doing this will kick off the LOCKED rule again
1163         //
1164         modify($operation) {};
1165     }
1166 end
1167
1168 /*
1169 *
1170 * This is the timer that manages the overall control loop timeout.
1171 *
1172 */
1173 rule "${policyName}.EVENT.MANAGER.TIMEOUT"
1174     timer (expr: $to )
1175     when
1176         $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1177         $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
1178         $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
1179         $clTimer : ControlLoopTimer ( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString(), $to : getDelay() )
1180         $operations : LinkedList()
1181                         from collect( ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID ) )
1182         $opTimers : LinkedList()
1183                         from collect( OperationTimer( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID.toString() ) )
1184         $locks : LinkedList()
1185                         from collect( TargetLock (requestID == $event.requestID) )
1186     then
1187     
1188     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1189     logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1190
1191     if ($operations == null) {
1192       logger.debug("{}: {}: event={} manager={} clTimer={} operations=0", 
1193                   $params.getClosedLoopControlName(), drools.getRule().getName(),
1194                   $event, $manager, $clTimer);
1195     } else {
1196       logger.debug("{}: {}: event={} manager={} clTimer={} operations={}", 
1197                   $params.getClosedLoopControlName(), drools.getRule().getName(),
1198                   $event, $manager, $clTimer, $operations.size());
1199     }
1200     //
1201     // Tell the Event Manager it has timed out
1202     //
1203     VirtualControlLoopNotification notification = $manager.setControlLoopTimedOut();
1204     if (notification != null) {
1205         notification.from = "policy";
1206         notification.policyName = drools.getRule().getName();
1207         notification.policyScope = "${policyScope}";
1208         notification.policyVersion = "${policyVersion}";
1209         //
1210         // Let interested parties know
1211         //
1212         PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
1213     }
1214     //
1215     // Retract EVERYTHING
1216     //
1217     retract($event);
1218     retract($manager);
1219     retract($clTimer);
1220     if ($operations != null && $operations.size() > 0) {
1221         Iterator<ControlLoopOperationManager> iter = $operations.iterator();
1222         while (iter.hasNext()) {
1223             ControlLoopOperationManager manager = iter.next();
1224             retract(manager);
1225         }
1226     }
1227     if ($opTimers != null && $opTimers.size() > 0) {
1228         Iterator<OperationTimer> iter = $opTimers.iterator();
1229         while (iter.hasNext()) {
1230             OperationTimer opTimer = iter.next();
1231             retract(opTimer);
1232         }
1233     }
1234     if ($locks != null && $locks.size() > 0) {
1235         Iterator<TargetLock> iter = $locks.iterator();
1236         while (iter.hasNext()) {
1237             TargetLock lock = iter.next();
1238             //
1239             // Ensure we release the lock
1240             //
1241             PolicyGuard.unlockTarget(lock);
1242             //
1243             //
1244             //
1245             retract(lock);
1246         }
1247     }
1248 end