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