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