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