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