40c2f189e2250efcbc0a357cfb3d46741d5fc37f
[policy/drools-applications.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Bell Canada.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.controlloop;
23
24 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput;
25
26 import org.onap.policy.controlloop.VirtualControlLoopEvent;
27 import org.onap.policy.controlloop.VirtualControlLoopNotification;
28 import org.onap.policy.controlloop.ControlLoopEventStatus;
29 import org.onap.policy.controlloop.ControlLoopException;
30 import org.onap.policy.controlloop.ControlLoopNotificationType;
31 import org.onap.policy.controlloop.ControlLoopLogger;
32 import org.onap.policy.controlloop.policy.PolicyResult;
33 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
34 import org.onap.policy.controlloop.policy.Policy;
35 import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager;
36 import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager.NewEventStatus;
37 import org.onap.policy.controlloop.eventmanager.ControlLoopOperationManager;
38 import org.onap.policy.controlloop.eventmanager.LockCallbackWorkingMemory;
39 import org.onap.policy.controlloop.actor.so.SoActorServiceProvider;
40 import org.onap.policy.controlloop.actor.cds.CdsActorServiceProvider;
41 import org.onap.policy.controlloop.actor.cds.CdsActorServiceProvider.CdsActorServiceManager;
42 import org.onap.policy.controlloop.actor.cds.constants.CdsActorConstants;
43 import org.onap.policy.aai.AaiNqResponseWrapper;
44 import org.onap.policy.aai.AaiCqResponse;
45 import org.onap.policy.appc.Request;
46 import org.onap.policy.appc.Response;
47 import org.onap.policy.appc.CommonHeader;
48 import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
49 import org.onap.policy.cds.CdsResponse;
50 import org.onap.policy.cds.client.CdsProcessorGrpcClient;
51 import org.onap.policy.cds.properties.CdsServerProperties;
52 import org.onap.policy.drools.utils.Pair;
53 import org.onap.policy.sdnr.PciRequestWrapper;
54 import org.onap.policy.sdnr.PciResponseWrapper;
55 import org.onap.policy.sdnr.PciRequest;
56 import org.onap.policy.sdnr.PciResponse;
57 import org.onap.policy.vfc.VfcRequest;
58 import org.onap.policy.vfc.VfcResponse;
59 import org.onap.policy.vfc.VfcManager;
60 import org.onap.policy.so.SoManager;
61 import org.onap.policy.so.SoRequest;
62 import org.onap.policy.so.SoResponseWrapper;
63 import org.onap.policy.sdnc.SdncRequest;
64 import org.onap.policy.sdnc.SdncManager;
65 import org.onap.policy.sdnc.SdncResponse;
66 import org.onap.policy.drools.core.lock.Lock;
67 import org.onap.policy.guard.PolicyGuardRequest;
68 import org.onap.policy.guard.PolicyGuardResponse;
69 import org.onap.policy.guard.PolicyGuardXacmlRequestAttributes;
70 import org.onap.policy.guard.PolicyGuardXacmlHelper;
71
72 import org.yaml.snakeyaml.Yaml;
73 import org.yaml.snakeyaml.constructor.Constructor;
74
75 import org.slf4j.LoggerFactory;
76 import org.slf4j.Logger;
77
78 import java.time.Instant;
79 import java.util.LinkedList;
80 import java.util.Iterator;
81
82 import org.onap.policy.drools.system.PolicyEngineConstants;
83
84 /*
85  * This structure mimics the Params structure.
86  * Its only purpose is to allow management of
87  * rules by the PAP component..
88  * It has no use at runtime since the rules go by
89  * Params for matching purposes.
90  */
91 declare PapParams
92   closedLoopControlName : String
93   controlLoopYaml : String
94 end
95
96 /*
97  * Control Loop Identity
98  */
99 declare Params
100   closedLoopControlName : String
101   controlLoopYaml : String
102 end
103
104 /*
105  * Used to trigger clean up Params that no longer have associated rules.
106  */
107 declare ParamsInitCleaner
108   closedLoopControlName : String    // only used when logging
109 end
110
111 /*
112  * Used to clean up Params that no longer have associated rules.
113  */
114 declare ParamsCleaner
115   closedLoopControlName : String
116   controlLoopYaml : String
117 end
118
119 /*
120  * This object is to provide support for timeouts
121  * due to a bug in drools' built-in timers
122  */
123 declare ControlLoopTimer
124     closedLoopControlName : String
125     requestId : String
126     delay : String
127     expired : boolean
128     //timerType is the type of timer: either "ClosedLoop" or "Operation"
129     timerType : String
130 end
131
132 /*
133 *
134 * Called to insert the parameters into working memory for this Closed Loop policy.  This is called
135 * once each time a closed loop is added or its YAML is updated.
136 * This has a higher salience so we can ensure that the Params is created before we have a chance to
137 * discard any events.
138 *
139 */
140 rule "${policyName}.SETUP"
141     salience 1
142     when
143         not( Params( getClosedLoopControlName() == "${closedLoopControlName}",
144             getControlLoopYaml() == "${controlLoopYaml}" ) )
145     then
146
147     Params params = new Params();
148     params.setClosedLoopControlName("${closedLoopControlName}");
149     params.setControlLoopYaml("${controlLoopYaml}");
150     insert(params);
151
152     ParamsInitCleaner initCleaner = new ParamsInitCleaner();
153     initCleaner.setClosedLoopControlName("${closedLoopControlName}");
154     insert(initCleaner);
155
156     // Note: globals have bad behavior when persistence is used,
157     //       hence explicitly getting the logger vs using a global
158
159     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
160     logger.info("{}: {} : YAML=[{}]", params.getClosedLoopControlName(), drools.getRule().getName(),
161         params.getControlLoopYaml());
162 end
163
164 /*
165 *
166 * This rule responds to DCAE Events where there is no manager yet. Either it is
167 * the first ONSET, or a subsequent badly formed Event (i.e. Syntax error, or is-closed-loop-disabled)
168 *
169 */
170 rule "${policyName}.EVENT"
171     when
172         $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
173         $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
174         not ( ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
175             requestId == $event.getRequestId() ) )
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.getRequestId() == null) {
189             VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
190             notification.setNotification(ControlLoopNotificationType.REJECTED);
191             notification.setFrom("policy");
192             notification.setMessage("Missing requestId");
193             notification.setPolicyName(drools.getRule().getName());
194             notification.setPolicyScope("${policyScope}");
195             notification.setPolicyVersion("${policyVersion}");
196
197             //
198             // Let interested parties know
199             //
200             PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
201
202             //
203             // Retract it from memory
204             //
205             retract($event);
206         } else if ($event.getClosedLoopEventStatus() != ControlLoopEventStatus.ONSET) {
207             throw new ControlLoopException($event.getClosedLoopEventStatus() + " received with no prior onset");
208         } else {
209             //
210             // Create an EventManager
211             //
212             ControlLoopEventManager manager = new ControlLoopEventManager($params.getClosedLoopControlName(),
213                 $event.getRequestId());
214             //
215             // Determine if EventManager can actively process the event
216             // (i.e. syntax, is_closed_loop_disabled checks etc.)
217             //
218             VirtualControlLoopNotification notification = manager.activate($params.getControlLoopYaml(), $event);
219             notification.setFrom("pdp-0001-controller=controlloop"); // Engine.getInstanceName()
220             notification.setPolicyName(drools.getRule().getName());
221             notification.setPolicyScope("${policyScope}");
222             notification.setPolicyVersion("${policyVersion}");
223             //
224             // Are we actively pursuing this event?
225             //
226             if (notification.getNotification() == ControlLoopNotificationType.ACTIVE) {
227                 //
228                 // Insert Event Manager into memory, this will now kick off processing.
229                 //
230                 insert(manager);
231                 //
232                 // Let interested parties know
233                 //
234                 PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
235                 //
236                 // Setup the Overall Control Loop timer
237                 //
238                 ControlLoopTimer clTimer = new ControlLoopTimer();
239                 clTimer.setTimerType("ClosedLoop");
240                 clTimer.setClosedLoopControlName($event.getClosedLoopControlName());
241                 clTimer.setRequestId($event.getRequestId().toString());
242                 clTimer.setDelay(manager.getControlLoopTimeout(1500) + "s");
243                 //
244                 // Insert it
245                 //
246                 insert(clTimer);
247             } else {
248                 //
249                 // Let interested parties know
250                 //
251                 PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
252                 //
253                 // Retract it from memory
254                 //
255                 retract($event);
256             }
257
258             //
259             // Now that the manager is inserted into Drools working memory, we'll wait for
260             // another rule to fire in order to continue processing. This way we can also
261             // then screen for additional ONSET and ABATED events for this RequestId.
262             //
263         }
264     } catch (Exception e) {
265         logger.warn("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName(), e);
266
267         VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
268         notification.setNotification(ControlLoopNotificationType.REJECTED);
269         notification.setMessage("Exception occurred: " + e.getMessage());
270         notification.setPolicyName(drools.getRule().getName());
271         notification.setPolicyScope("${policyScope}");
272         notification.setPolicyVersion("${policyVersion}");
273         //
274         //
275         //
276         PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
277         //
278         // Retract the event
279         //
280         retract($event);
281     }
282 end
283
284 /*
285 *
286 * This rule happens when we got a valid ONSET, closed loop is enabled and an Event Manager
287 * is now created. We can start processing the yaml specification via the Event Manager.
288 *
289 */
290 rule "${policyName}.EVENT.MANAGER"
291     when
292         $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
293         $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
294         $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
295             requestId == $event.getRequestId() )
296         $clTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
297             requestId == $event.getRequestId().toString(), timerType == "ClosedLoop", !expired )
298     then
299
300     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
301     logger.info("{}: {}: event={} manager={} clTimer={}",
302                 $params.getClosedLoopControlName(), drools.getRule().getName(),
303                 $event, $manager, $clTimer);
304
305     try {
306         //
307         // Check which event this is.
308         //
309         ControlLoopEventManager.NewEventStatus eventStatus = $manager.onNewEvent($event);
310         //
311         // Check what kind of event this is
312         //
313         if (eventStatus == NewEventStatus.SUBSEQUENT_ONSET) {
314             //
315             // We don't care about subsequent onsets
316             //
317             logger.info("{}: {}: subsequent onset",
318                         $params.getClosedLoopControlName(), drools.getRule().getName());
319             retract($event);
320             return;
321         }
322         if (eventStatus == NewEventStatus.SYNTAX_ERROR) {
323             //
324             // Ignore any bad syntax events
325             //
326             logger.warn("{}: {}: syntax error",
327                         $params.getClosedLoopControlName(), drools.getRule().getName());
328             retract($event);
329             return;
330         }
331         //
332         // We only want the initial ONSET event in memory,
333         // all the other events need to be retracted to support
334         // cleanup and avoid the other rules being fired for this event.
335         //
336         if (eventStatus != NewEventStatus.FIRST_ONSET) {
337             logger.warn("{}: {}: not first onset",
338                         $params.getClosedLoopControlName(), drools.getRule().getName());
339             retract($event);
340         }
341
342         logger.debug("{}: {}: target={}", $params.getClosedLoopControlName(),
343                      drools.getRule().getName(), $event.getTarget());
344         //
345         // Now start seeing if we need to process this event
346         //
347
348         //
349         // Check if this is a Final Event
350         //
351         VirtualControlLoopNotification notification = $manager.isControlLoopFinal();
352
353
354         if (notification != null) {
355             //
356             // Its final, but are we waiting for abatement?
357             //
358             if ($manager.getNumAbatements() > 0) {
359                 logger.info("{}: {}: abatement received for {}.  Closing the control loop",
360                             $params.getClosedLoopControlName(), drools.getRule().getName(),
361                             $event.getRequestId());
362
363                 /// DB Write---end event processing for this RequestId()
364                 $manager.commitAbatement("Event Abated","Closed");
365
366                 notification.setFrom("policy");
367                 notification.setPolicyName(drools.getRule().getName());
368                 notification.setPolicyScope("${policyScope}");
369                 notification.setPolicyVersion("${policyVersion}");
370                 //
371                 // In this case, we are done
372                 //
373                 PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
374                 //
375                 // Unlock the target
376                 //
377                 Lock lock = $manager.unlockCurrentOperation();
378                 if(lock != null) {
379                     logger.debug("{}: {}: retracting lock={}", $params.getClosedLoopControlName(),
380                                 drools.getRule().getName(), lock);
381                     retract(lock);
382                 }
383                 //
384                 // Retract everything from memory
385                 //
386                 logger.info("{}: {}: retracting onset, manager, and timer",
387                             $params.getClosedLoopControlName(), drools.getRule().getName());
388
389                 retract($manager.getOnsetEvent());
390
391                 // don't retract manager, etc. - a clean-up rule will do that
392
393                 //
394                 // TODO - what if we get subsequent Events for this RequestId?
395                 // By default, it will all start over again. May be confusing for Ruby.
396                 // Or, we could track this and then subsequently ignore the events
397                 //
398             } else {
399                 //
400                 // Check whether we need to wait for abatement
401                 //
402                 if ($manager.getProcessor().getControlLoop().getAbatement() == true && notification.getNotification() == ControlLoopNotificationType.FINAL_SUCCESS) {
403                   logger.info("{}: {}: waiting for abatement ..",
404                               $params.getClosedLoopControlName(), drools.getRule().getName());
405                 } else {
406                   logger.info("{}: {}: no abatement expect for {}.  Closing the control loop",
407                               $params.getClosedLoopControlName(), drools.getRule().getName(),
408                               $event.getRequestId());
409
410                   notification.setFrom("policy");
411                   notification.setPolicyName(drools.getRule().getName());
412                   notification.setPolicyScope("${policyScope}");
413                   notification.setPolicyVersion("${policyVersion}");
414
415                   //
416                   // In this case, we are done
417                   //
418                   PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
419                   //
420                   // Unlock the target
421                   //
422                   Lock lock = $manager.unlockCurrentOperation();
423                   if(lock != null) {
424                       logger.debug("{}: {}: retracting lock={}", $params.getClosedLoopControlName(),
425                                   drools.getRule().getName(), lock);
426                       retract(lock);
427                   }
428                   //
429                   // Retract everything from memory
430                   //
431                   logger.info("{}: {}: retracting onset, manager, and timer",
432                               $params.getClosedLoopControlName(), drools.getRule().getName());
433
434                   retract($manager.getOnsetEvent());
435
436                 // don't retract manager, etc. - a clean-up rule will do that
437                 }
438             }
439         } else {
440             //
441             // NOT final, so let's ask for the next operation
442             //
443             ControlLoopOperationManager operation = $manager.processControlLoop();
444             if (operation != null) {
445               //
446               // Let's ask for a lock right away
447               //
448               logger.info("{}: {}: requesting lock for operation={}",
449                             $params.getClosedLoopControlName(), drools.getRule().getName(),
450                             operation);
451
452               Pair<Lock,Lock> oldNew = $manager.lockCurrentOperation(
453                         new LockCallbackWorkingMemory($params.getClosedLoopControlName(), drools.getWorkingMemory()));
454               if(oldNew.first() != null) {
455                   logger.debug("{}: {}: retracting lock={}", $params.getClosedLoopControlName(),
456                               drools.getRule().getName(), oldNew.first());
457                   retract(oldNew.first());
458               }
459               if(oldNew.second() != null) {
460                   logger.debug("{}: {}: inserting lock={}", $params.getClosedLoopControlName(),
461                               drools.getRule().getName(), oldNew.second());
462                   insert(oldNew.second());
463               }
464
465               //
466               // insert the operation into memory
467               //
468               insert(operation);
469               //
470               // insert operation timeout object
471               //
472               ControlLoopTimer opTimer = new ControlLoopTimer();
473               opTimer.setTimerType("Operation");
474               opTimer.setClosedLoopControlName($event.getClosedLoopControlName());
475               opTimer.setRequestId($event.getRequestId().toString());
476               Integer timeout = operation.getOperationTimeout();
477               opTimer.setDelay(timeout > 0 ? timeout.toString() + "s" : $clTimer.getDelay());
478               insert(opTimer);
479
480             } else {
481                 //
482                 // Probably waiting for abatement
483                 //
484               logger.info("{}: {}: no operation, probably waiting for abatement",
485                           $params.getClosedLoopControlName(), drools.getRule().getName());
486             }
487         }
488     } catch (Exception e) {
489          logger.warn("{}: {}: unexpected",
490                   $params.getClosedLoopControlName(),
491                   drools.getRule().getName(), e);
492
493          VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
494          notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
495          notification.setMessage(e.getMessage());
496          notification.setFrom("policy");
497          notification.setPolicyName(drools.getRule().getName());
498          notification.setPolicyScope("${policyScope}");
499          notification.setPolicyVersion("${policyVersion}");
500
501          PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
502
503          retract($event);
504
505          // don't retract manager, etc. - a clean-up rule will do that
506     }
507
508 end
509
510 /*
511 *
512 * Lock denied
513 *
514 */
515 rule "${policyName}.EVENT.MANAGER.OPERATION.LOCK.DENIED"
516     when
517         $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
518         $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
519         $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
520             requestId == $event.getRequestId() )
521         $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
522             onset.getRequestId() == $event.getRequestId(), "None".equalsIgnoreCase(getGuardApprovalStatus()) )
523         $lock : Lock (ownerKey == $event.getRequestId().toString(), isUnavailable())
524     then
525
526     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
527     logger.info("{}: {}: event={} manager={} operation={} lock={}",
528                 $params.getClosedLoopControlName(), drools.getRule().getName(),
529                 $event, $manager, $operation, $lock);
530
531     logger.debug("The target resource {} is already processing",
532                         $event.getAai().get($event.getTarget()));
533     VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
534     notification.setNotification(ControlLoopNotificationType.REJECTED);
535     notification.setMessage("The target " + $event.getAai().get($event.getTarget())
536                         + " is already locked");
537     notification.setFrom("policy");
538     notification.setPolicyName(drools.getRule().getName());
539     notification.setPolicyScope("${policyScope}");
540     notification.setPolicyVersion("${policyVersion}");
541
542     PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
543
544     retract($event);
545
546     // don't retract manager, etc. - a clean-up rule will do that
547 end
548
549 /*
550 *
551 * Guard Permitted, let's send request to the actor.
552 *
553 */
554 rule "${policyName}.EVENT.MANAGER.OPERATION.LOCKED.GUARD_PERMITTED"
555     when
556         $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
557         $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
558         $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
559             requestId == $event.getRequestId() )
560         $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
561             onset.getRequestId() == $event.getRequestId(), "Permit".equalsIgnoreCase(getGuardApprovalStatus()) )
562         $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
563             requestId == $event.getRequestId().toString(), timerType == "Operation", !expired )
564     then
565
566     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
567     logger.info("{}: {}: event={} manager={} operation={}",
568                 $params.getClosedLoopControlName(), drools.getRule().getName(),
569                 $event, $manager, $operation);
570
571     Object request = null;
572     boolean caughtException = false;
573
574     try {
575         request = $operation.startOperation($event);
576
577         if (request != null) {
578           logger.debug("{}: {}: starting operation ..",
579                        $params.getClosedLoopControlName(), drools.getRule().getName());
580           //
581           // Tell interested parties we are performing this Operation
582           //
583           VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
584           notification.setNotification(ControlLoopNotificationType.OPERATION);
585           notification.setMessage($operation.getOperationMessage());
586           notification.setHistory($operation.getHistory());
587           notification.setFrom("policy");
588           notification.setPolicyName(drools.getRule().getName());
589           notification.setPolicyScope("${policyScope}");
590           notification.setPolicyVersion("${policyVersion}");
591
592           PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
593
594           switch ($operation.policy.getActor()){
595
596               case "APPC":
597                   if (request instanceof Request) {
598                       PolicyEngineConstants.getManager().deliver("APPC-CL", request);
599                   }
600                   else if (request instanceof AppcLcmDmaapWrapper) {
601                       PolicyEngineConstants.getManager().deliver("APPC-LCM-READ", request);
602                   }
603                   break;
604               case "SO":
605                   // at this point the AAI named query request should have already been made,
606                   // the response recieved and used
607                   // in the construction of the SO Request which is stored in operationRequest
608
609                   if(request instanceof SoRequest) {
610                       // Call SO. The response will be inserted into memory once it's received
611                       class mySoCallback implements SoManager.SoCallback {
612                           public void onSoResponseWrapper(SoResponseWrapper wrapper) {
613                               drools.getWorkingMemory().insert(wrapper);
614                           }
615                       }
616                       SoActorServiceProvider.sendRequest($event.getRequestId().toString(),
617                           new mySoCallback(),
618                           request,
619                           PolicyEngineConstants.getManager().getEnvironmentProperty("so.url"),
620                           PolicyEngineConstants.getManager().getEnvironmentProperty("so.username"),
621                           PolicyEngineConstants.getManager().getEnvironmentProperty("so.password"));
622                   }
623                   break;
624               case "VFC":
625                   if (request instanceof VfcRequest) {
626                       // Start VFC thread
627                       class myVfcCallback implements VfcManager.VfcCallback {
628
629                           public void onResponse(VfcResponse responseError) {
630                               drools.getWorkingMemory().insert(responseError);
631                           }
632                       };
633                       // Start VFC thread
634                       Thread t = new Thread(new VfcManager(new myVfcCallback(),
635                           (VfcRequest)request,
636                           PolicyEngineConstants.getManager().getEnvironmentProperty("vfc.url"),
637                           PolicyEngineConstants.getManager().getEnvironmentProperty("vfc.username"),
638                           PolicyEngineConstants.getManager().getEnvironmentProperty("vfc.password")));
639                       t.start();
640                   }
641                   break;
642
643               case "SDNC":
644                   if (request instanceof SdncRequest) {
645                      // Start SDNC thread
646                       class mySdncCallback implements SdncManager.SdncCallback {
647                           public void onCallback(SdncResponse response) {
648                               drools.getWorkingMemory().insert(response);
649                           }
650                       }
651                       // Start SDNC thread
652                       Thread t = new Thread(new SdncManager(new mySdncCallback(),
653                           (SdncRequest)request,
654                           PolicyEngineConstants.getManager().getEnvironmentProperty("sdnc.url"),
655                           PolicyEngineConstants.getManager().getEnvironmentProperty("sdnc.username"),
656                           PolicyEngineConstants.getManager().getEnvironmentProperty("sdnc.password")));
657                       t.start();
658                   }
659                   break;
660               case "SDNR":
661                   if (request instanceof PciRequestWrapper) {
662                       PolicyEngineConstants.getManager().deliver("SDNR-CL", request);
663                   }
664                   break;
665
666              case "CDS":
667
668                   if(request instanceof ExecutionServiceInput) {
669
670                       // Instantiate cds actor, service manager and grpc properties
671
672                       CdsActorServiceProvider cdsActor = new CdsActorServiceProvider();
673                       CdsActorServiceProvider.CdsActorServiceManager cdsActorServiceManager = cdsActor.new CdsActorServiceManager();
674
675                       CdsServerProperties props = new CdsServerProperties();
676                       props.setHost(PolicyEngineConstants.getManager().getEnvironmentProperty("cds.grpcHost"));
677                       props.setPort(Integer.parseInt(PolicyEngineConstants.getManager().getEnvironmentProperty("cds.grpcPort")));
678                       props.setUsername(PolicyEngineConstants.getManager().getEnvironmentProperty("cds.grpcUsername"));
679                       props.setPassword(PolicyEngineConstants.getManager().getEnvironmentProperty("cds.grpcPassword"));
680                       props.setTimeout(Integer.parseInt(PolicyEngineConstants.getManager().getEnvironmentProperty("cds.grpcTimeout")));
681
682                       // Send cds grpc request
683                       try (CdsProcessorGrpcClient client = new CdsProcessorGrpcClient(cdsActorServiceManager, props)) {
684                           CdsResponse response =
685                                 cdsActorServiceManager.sendRequestToCds(client, props, (ExecutionServiceInput) request);
686                           logger.info("CDS response: {}", response);
687                           insert(response);
688                       }
689
690                   }
691                   break;
692
693           }
694         } else {
695           //
696           // What happens if its null?
697           //
698             logger.warn("{}: {}: unexpected null operation request",
699                       $params.getClosedLoopControlName(),
700                       drools.getRule().getName());
701             if ("SO".equals($operation.policy.getActor())) {
702                 retract($opTimer);
703                 retract($operation);
704                 modify($manager) {finishOperation($operation)};
705             }
706             else if ("vfc".equalsIgnoreCase($operation.policy.getActor())) {
707                 retract($opTimer);
708                 retract($operation);
709                 modify($manager) {finishOperation($operation)};
710             }
711             else if ("sdnc".equalsIgnoreCase($operation.policy.getActor())) {
712                 retract($opTimer);
713                 retract($operation);
714                 modify($manager) {finishOperation($operation)};
715             }
716         }
717
718     } catch (Exception e) {
719         String msg = e.getMessage();
720         logger.warn("{}: {}: operation={}:  AAI failure: {}",
721                     $params.getClosedLoopControlName(), drools.getRule().getName(),
722                     $operation, msg, e);
723         $operation.setOperationHasException(msg);
724
725         if(request != null) {
726             //
727             // Create a notification for it ("DB Write - end operation")
728             //
729             VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
730             notification.setFrom("policy");
731             notification.setPolicyName(drools.getRule().getName());
732             notification.setPolicyScope("${policyScope}");
733             notification.setPolicyVersion("${policyVersion}");
734             notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
735             notification.setMessage($operation.getOperationHistory());
736             notification.setHistory($operation.getHistory());
737
738             PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
739         }
740
741         retract($opTimer);
742         retract($operation);
743         caughtException = true;
744     }
745
746     // Having the modify statement in the catch clause doesn't work for whatever reason
747     if (caughtException) {
748         modify($manager) {finishOperation($operation)};
749     }
750 end
751
752
753 /*
754 *
755 * We were able to acquire a lock so now let's ask Xacml Guard whether
756 * we are allowed to proceed with the request to the actor.
757 *
758 */
759 rule "${policyName}.EVENT.MANAGER.OPERATION.LOCKED.GUARD_NOT_YET_QUERIED"
760     when
761         $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
762         $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
763         $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
764             requestId == $event.getRequestId() )
765         $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
766             onset.getRequestId() == $event.getRequestId(), "None".equalsIgnoreCase(getGuardApprovalStatus()) )
767         $lock : Lock (ownerKey == $event.getRequestId().toString(), isActive())
768     then
769
770     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
771     logger.info("{}: {}: event={} manager={} operation={} lock={}",
772                 $params.getClosedLoopControlName(), drools.getRule().getName(),
773                 $event, $manager, $operation, $lock);
774
775     //
776     // Sending notification that we are about to query Guard ("DB write - start operation")
777     //
778     VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
779     notification.setNotification(ControlLoopNotificationType.OPERATION);
780     notification.setMessage("Sending guard query for " + $operation.policy.getActor() + " "
781         + $operation.policy.getRecipe());
782     notification.setHistory($operation.getHistory());
783     notification.setFrom("policy");
784     notification.setPolicyName(drools.getRule().getName());
785     notification.setPolicyScope("${policyScope}");
786     notification.setPolicyVersion("${policyVersion}");
787
788     PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
789
790     //
791     // Now send Guard Request to XACML Guard. In order to bypass the call to Guard,
792     // just change guardEnabled to false.
793     //
794     // In order to use REST XACML, provide a URL instead of "" as a second argument
795     // to the CallGuardTask() and set the first argument to null
796     // (instead of XacmlPdpEngine).
797     //
798
799     // NOTE: The environment properties uses "guard.disabled" but the boolean is guardEnabled
800     boolean guardEnabled = "false".equalsIgnoreCase(PolicyEngineConstants.getManager().getEnvironmentProperty("guard.disabled"));
801     boolean cqEnabled = "true".equalsIgnoreCase(PolicyEngineConstants.getManager().getEnvironmentProperty("aai.customQuery"));
802
803     if(guardEnabled){
804
805         Thread t = new Thread(new org.onap.policy.guard.CallGuardTask(
806                                                         drools.getWorkingMemory(),
807                                                         $event.getClosedLoopControlName(),
808                                                         $operation.policy.getActor().toString(),
809                                                         $operation.policy.getRecipe(),
810                                                         $operation.getTargetEntity(),
811                                                         $event.getRequestId().toString(),
812                                                         () -> {
813                                                              if (!cqEnabled) {
814                                                                  AaiNqResponseWrapper resp = $manager.getNqVserverFromAai();
815                                                                  return(resp == null ? null : resp.countVfModules());
816                                                              } else {
817                                                                  try {
818                                                                      AaiCqResponse resp_cq  = $manager.getCqResponse($event);
819                                                                      if (resp_cq == null){
820                                                                          return null;
821                                                                      } else {
822                                                                          String custId = $operation.policy.getTarget().getModelCustomizationId();
823                                                                          String invId = $operation.policy.getTarget().getModelInvariantId();
824                                                                          String verId = $operation.policy.getTarget().getModelVersionId();
825                                                                          return resp_cq.getVfModuleCount(custId, invId, verId);
826                                                                      }
827                                                                  } catch (Exception e){
828                                                                      logger.warn("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName(), e);
829                                                                  }
830                                                                  return null;
831                                                              }
832                                                         }));
833         t.start();
834     }
835     else{
836         insert(new PolicyGuardResponse("Permit", $event.getRequestId(), $operation.policy.getRecipe()));
837     }
838
839 end
840
841 //
842 // This rule will be triggered when a thread talking to the XACML Guard inserts a
843 // guardResponse object into the working memory
844 //
845 rule "${policyName}.GUARD.RESPONSE"
846     when
847         $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
848         $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
849             closedLoopEventStatus == ControlLoopEventStatus.ONSET )
850         $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
851             requestId == $event.getRequestId() )
852         $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
853             onset.getRequestId() == $event.getRequestId() )
854         $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
855             requestId == $event.getRequestId().toString(), timerType == "Operation", !expired )
856         $guardResponse : PolicyGuardResponse(requestId == $event.getRequestId(), $operation.policy.recipe == operation)
857     then
858
859     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
860     logger.info("{}: {}: event={} manager={} operation={} opTimer={} guardResponse={}",
861                  $params.getClosedLoopControlName(), drools.getRule().getName(),
862                  $event, $manager, $operation, $opTimer, $guardResponse);
863
864
865     //we will permit the operation if there was no Guard for it
866     if("Indeterminate".equalsIgnoreCase($guardResponse.getResult())){
867         $guardResponse.setResult("Permit");
868     }
869
870     //
871     // This notification has Guard result in "message". ("DB write - end operation in case of Guard Deny")
872     //
873     VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
874     notification.setNotification(ControlLoopNotificationType.OPERATION);
875     notification.setMessage("Guard result for " + $operation.policy.getActor() + " " + $operation.policy.getRecipe()
876         + " is " + $guardResponse.getResult());
877     notification.setHistory($operation.getHistory());
878     notification.setFrom("policy");
879     notification.setPolicyName(drools.getRule().getName());
880     notification.setPolicyScope("${policyScope}");
881     notification.setPolicyVersion("${policyVersion}");
882
883     PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
884
885     if("Permit".equalsIgnoreCase($guardResponse.getResult())){
886
887         modify($operation){setGuardApprovalStatus($guardResponse.getResult())};
888     }
889     else {
890         //This is the Deny case
891         $operation.startOperation($event);
892         $operation.setOperationHasGuardDeny();
893         retract($opTimer);
894         retract($operation);
895         modify($manager) {finishOperation($operation)};
896     }
897
898     retract($guardResponse);
899
900 end
901
902 /*
903 *
904 * This rule responds to APPC Response Events
905 *
906 * I would have like to be consistent and write the Response like this:
907 * $response : Response( CommonHeader.RequestId == $onset.getRequestId() )
908 *
909 * However, no compile error was given. But a runtime error was given. I think
910 * because drools is confused between the classname CommonHeader vs the property CommonHeader.
911 *
912 */
913 rule "${policyName}.APPC.RESPONSE"
914     when
915         $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
916         $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
917             closedLoopEventStatus == ControlLoopEventStatus.ONSET )
918         $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
919             requestId == $event.getRequestId() )
920         $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
921             onset.getRequestId() == $event.getRequestId() )
922         $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
923             requestId == $event.getRequestId().toString(), timerType == "Operation", !expired )
924         $response : Response( getCommonHeader().RequestId == $event.getRequestId() )
925     then
926
927     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
928     logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
929     logger.debug("{}: {}: event={} manager={} operation={} opTimer={} response={}",
930                  $params.getClosedLoopControlName(), drools.getRule().getName(),
931                  $event, $manager, $operation, $opTimer, $response);
932     //
933     // Get the result of the operation
934     //
935     PolicyResult policyResult = $operation.onResponse($response);
936     if (policyResult != null) {
937         logger.debug("{}: {}: operation finished - result={}",
938                     $params.getClosedLoopControlName(), drools.getRule().getName(),
939                     policyResult);
940         //
941         // This Operation has completed, construct a notification showing our results. (DB write - end operation)
942         //
943         VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
944         notification.setFrom("policy");
945         notification.setPolicyName(drools.getRule().getName());
946         notification.setPolicyScope("${policyScope}");
947         notification.setPolicyVersion("${policyVersion}");
948         notification.setMessage($operation.getOperationHistory());
949         notification.setHistory($operation.getHistory());
950         if (policyResult.equals(PolicyResult.SUCCESS)) {
951             notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
952             //
953             // Let interested parties know
954             //
955             PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
956         } else {
957             notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
958             //
959             // Let interested parties know
960             //
961             PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
962         }
963         //
964         // Ensure the operation is complete
965         //
966         if ($operation.isOperationComplete() == true) {
967             //
968             // It is complete, remove it from memory
969             //
970             retract($operation);
971             //
972             // We must also retract the timer object
973             // NOTE: We could write a Rule to do this
974             //
975             retract($opTimer);
976             //
977             // Complete the operation
978             //
979             modify($manager) {finishOperation($operation)};
980         } else {
981             //
982             // Just doing this will kick off the LOCKED rule again
983             //
984             modify($operation) {};
985         }
986     } else {
987         //
988         // Its not finished yet (i.e. expecting more Response objects)
989         //
990         // Or possibly it is a leftover response that we timed the request out previously
991         //
992     }
993     //
994     // We are going to retract these objects from memory
995     //
996     retract($response);
997 end
998
999 /*
1000 *
1001 * The problem with Responses is that they don't have a controlLoopControlName
1002 * field in them, so the only way to attach them is via RequestId. If we have multiple
1003 * control loop .drl's loaded in the same container, we need to be sure the cleanup
1004 * rules don't remove Responses for other control loops.
1005 *
1006 */
1007 rule "${policyName}.APPC.RESPONSE.CLEANUP"
1008     when
1009         $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1010         $response : Response($id : getCommonHeader().RequestId )
1011         not ( VirtualControlLoopEvent( requestId == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
1012     then
1013
1014     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1015     logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1016     logger.debug("{}: {}: orphan appc response={}",
1017                 $params.getClosedLoopControlName(), drools.getRule().getName(), $id);
1018
1019     //
1020     // Retract it
1021     //
1022     retract($response);
1023 end
1024
1025 /*
1026 *
1027 * This rule responds to APPC Response Events using the new LCM interface provided by appc
1028 *
1029 */
1030 rule "${policyName}.APPC.LCM.RESPONSE"
1031     when
1032         $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1033         $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
1034             closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1035         $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1036             requestId == $event.getRequestId() )
1037         $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
1038             onset.getRequestId() == $event.getRequestId() )
1039         $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1040             requestId == $event.getRequestId().toString(), timerType == "Operation", !expired )
1041         $response : AppcLcmDmaapWrapper( getBody().getOutput().getCommonHeader().getRequestId() == $event.getRequestId() )
1042     then
1043
1044     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1045     logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1046     logger.debug("{}: {}: event={} manager={} operation={} opTimer={} response={}",
1047                 $params.getClosedLoopControlName(), drools.getRule().getName(),
1048                 $event, $manager, $operation, $operation, $opTimer, $response);
1049
1050     //
1051     // Get the result of the operation
1052     //
1053     PolicyResult policyResult = $operation.onResponse($response);
1054     if (policyResult != null) {
1055       logger.debug("{}: {}: operation finished - result={}",
1056                   $params.getClosedLoopControlName(), drools.getRule().getName(),
1057                   policyResult);
1058
1059       //
1060       // This Operation has completed, construct a notification showing our results. (DB write - end operation)
1061       //
1062       VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1063       notification.setFrom("policy");
1064       notification.setPolicyName(drools.getRule().getName());
1065       notification.setPolicyScope("${policyScope}");
1066       notification.setPolicyVersion("${policyVersion}");
1067       notification.setMessage($operation.getOperationHistory());
1068       notification.setHistory($operation.getHistory());
1069       if (policyResult.equals(PolicyResult.SUCCESS)) {
1070           notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
1071       } else {
1072           notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
1073       }
1074       PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
1075       //
1076       // Ensure the operation is complete
1077       //
1078       if ($operation.isOperationComplete() == true) {
1079           //
1080           // It is complete, remove it from memory
1081           //
1082           retract($operation);
1083           //
1084           // We must also retract the timer object
1085           // NOTE: We could write a Rule to do this
1086           //
1087           retract($opTimer);
1088           //
1089           // Complete the operation
1090           //
1091           modify($manager) {finishOperation($operation)};
1092       } else {
1093           //
1094           // Just doing this will kick off the LOCKED rule again
1095           //
1096           modify($operation) {};
1097       }
1098     } else {
1099         //
1100         // Its not finished yet (i.e. expecting more Response objects)
1101         //
1102         // Or possibly it is a leftover response that we timed the request out previously
1103         //
1104     }
1105     //
1106     // We are going to retract these objects from memory
1107     //
1108     retract($response);
1109 end
1110
1111 /*
1112 *
1113 * Clean Up any lingering LCM reponses
1114 *
1115 */
1116 rule "${policyName}.APPC.LCM.RESPONSE.CLEANUP"
1117     when
1118         $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1119         $response : AppcLcmDmaapWrapper($id : getBody().getOutput().getCommonHeader().getRequestId() )
1120         not ( VirtualControlLoopEvent( requestId == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) ) 
1121     then
1122
1123     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1124     logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1125     logger.debug("{}: {}: orphan appc response={}",
1126                 $params.getClosedLoopControlName(), drools.getRule().getName(), $id);
1127     //
1128     // Retract it
1129     //
1130     retract($response);
1131 end
1132
1133 /*
1134 *
1135 * This rule responds to SDNR Response Events using the new interface provided by SDNR
1136 *
1137 */
1138 rule "${policyName}.SDNR.RESPONSE"
1139     when
1140         $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1141         $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
1142             closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1143         $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1144             requestId == $event.getRequestId() )
1145         $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
1146             onset.getRequestId() == $event.getRequestId() )
1147         $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1148             requestId == $event.getRequestId().toString(), timerType == "Operation", !expired )
1149         $response : PciResponseWrapper( getBody().getCommonHeader().getRequestId() == $event.getRequestId() )
1150     then
1151
1152     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1153     logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1154     logger.debug("{}: {}: event={} manager={} operation={} opTimer={} response={}",
1155                 $params.getClosedLoopControlName(), drools.getRule().getName(),
1156                 $event, $manager, $operation, $operation, $opTimer, $response);
1157
1158     //
1159     // Get the result of the operation
1160     //
1161     PolicyResult policyResult = $operation.onResponse($response);
1162     if (policyResult != null) {
1163       logger.debug("{}: {}: operation finished - result={}",
1164                   $params.getClosedLoopControlName(), drools.getRule().getName(),
1165                   policyResult);
1166
1167       //
1168       // This Operation has completed, construct a notification showing our results. (DB write - end operation)
1169       //
1170       VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1171       notification.setFrom("policy");
1172       notification.setPolicyName(drools.getRule().getName());
1173       notification.setPolicyScope("${policyScope}");
1174       notification.setPolicyVersion("${policyVersion}");
1175       notification.setMessage($operation.getOperationHistory());
1176       notification.setHistory($operation.getHistory());
1177       if (policyResult.equals(PolicyResult.SUCCESS)) {
1178           notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
1179       } else {
1180           notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
1181       }
1182       PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
1183       //
1184       // Ensure the operation is complete
1185       //
1186       if ($operation.isOperationComplete()) {
1187           //
1188           // It is complete, remove it from memory
1189           //
1190           retract($operation);
1191           //
1192           // We must also retract the timer object
1193           // NOTE: We could write a Rule to do this
1194           //
1195           retract($opTimer);
1196           //
1197           // Complete the operation
1198           //
1199           modify($manager) {finishOperation($operation)};
1200       } else {
1201           //
1202           // Just doing this will kick off the LOCKED rule again
1203           //
1204           modify($operation) {};
1205       }
1206     } else {
1207         //
1208         // Its not finished yet (i.e. expecting more Response objects)
1209         //
1210         // Or possibly it is a leftover response that we timed the request out previously
1211         //
1212     }
1213     //
1214     // We are going to retract these objects from memory
1215     //
1216     retract($response);
1217 end
1218
1219 /*
1220 *
1221 * Clean Up any lingering SDNR reponses
1222 *
1223 */
1224 rule "${policyName}.SDNR.RESPONSE.CLEANUP"
1225     when
1226         $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1227         $response : PciResponseWrapper($id : getBody().getCommonHeader().getRequestId )
1228         not ( VirtualControlLoopEvent( requestId == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
1229     then
1230
1231     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1232     logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1233     logger.debug("{}: {}: orphan SDNR response={}",
1234                 $params.getClosedLoopControlName(), drools.getRule().getName(), $id);
1235     //
1236     // Retract it
1237     //
1238     retract($response);
1239 end
1240
1241 /*
1242 *
1243 * This rule responds to SO Response Events
1244 *
1245 */
1246 rule "${policyName}.SO.RESPONSE"
1247     when
1248         $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1249         $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
1250             closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1251         $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1252             requestId == $event.getRequestId() )
1253         $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
1254             onset.getRequestId() == $event.getRequestId() )
1255         $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1256             requestId == $event.getRequestId().toString(), timerType == "Operation", !expired )
1257         $response : SoResponseWrapper(requestId.toString() == $event.getRequestId().toString() )
1258     then
1259
1260     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1261     logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1262     logger.debug("{}: {}: event={} manager={} operation={} opTimer={} response={}",
1263                 $params.getClosedLoopControlName(), drools.getRule().getName(),
1264                 $event, $manager, $operation, $operation, $opTimer, $response);
1265
1266     // Get the result of the operation
1267     //
1268     PolicyResult policyResult = $operation.onResponse($response);
1269     if (policyResult != null) {
1270         logger.debug("{}: {}: operation finished - result={}",
1271                     $params.getClosedLoopControlName(), drools.getRule().getName(),
1272                     policyResult);
1273
1274         //
1275         // This Operation has completed, construct a notification showing our results
1276         //
1277         VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1278         notification.setFrom("policy");
1279         notification.setPolicyName(drools.getRule().getName());
1280         notification.setPolicyScope("${policyScope}");
1281         notification.setPolicyVersion("${policyVersion}");
1282         notification.setMessage($operation.getOperationHistory());
1283         notification.setHistory($operation.getHistory());
1284         if (policyResult.equals(PolicyResult.SUCCESS)) {
1285             notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
1286         } else {
1287             notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
1288
1289         }
1290         PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
1291         //
1292         // Ensure the operation is complete
1293         //
1294         if ($operation.isOperationComplete() == true) {
1295             //
1296             // It is complete, remove it from memory
1297             //
1298             retract($operation);
1299             //
1300             // We must also retract the timer object
1301             // NOTE: We could write a Rule to do this
1302             //
1303             retract($opTimer);
1304             //
1305             // Complete the operation
1306             //
1307             modify($manager) {finishOperation($operation)};
1308         } else {
1309             //
1310             // Just doing this will kick off the LOCKED rule again
1311             //
1312             modify($operation) {};
1313         }
1314     } else {
1315         //
1316         // Its not finished yet (i.e. expecting more Response objects)
1317         //
1318         // Or possibly it is a leftover response that we timed the request out previously
1319         //
1320     }
1321     //
1322     // We are going to retract these objects from memory
1323     //
1324     retract($response);
1325
1326 end
1327
1328 /*
1329 *
1330 * This rule responds to VFC Response Events
1331 *
1332 */
1333 rule "${policyName}.VFC.RESPONSE"
1334     when
1335         $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1336         $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
1337             closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1338         $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1339             requestId == $event.getRequestId() )
1340         $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
1341             onset.getRequestId() == $event.getRequestId() )
1342         $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1343             requestId == $event.getRequestId().toString(), timerType == "Operation", !expired )
1344         $response : VfcResponse( requestId.toString() == $event.getRequestId().toString() )
1345     then
1346         Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1347         logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1348         logger.debug("{}: {}: event={} manager={} operation={} opTimer={} response={}",
1349                     $params.getClosedLoopControlName(), drools.getRule().getName(),
1350                     $event, $manager, $operation, $operation, $opTimer, $response);
1351
1352         // Get the result of the operation
1353         //
1354         PolicyResult policyResult = $operation.onResponse($response);
1355         if (policyResult != null) {
1356             //
1357             // This Operation has completed, construct a notification showing our results
1358             //
1359             VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1360             notification.setFrom("policy");
1361             notification.setPolicyName(drools.getRule().getName());
1362             notification.setPolicyScope("${policyScope}");
1363             notification.setPolicyVersion("${policyVersion}");
1364             notification.setMessage($operation.getOperationHistory());
1365             notification.setHistory($operation.getHistory());
1366             //
1367             // Ensure the operation is complete
1368             //
1369             if ($operation.isOperationComplete() == true) {
1370                 //
1371                 // It is complete, remove it from memory
1372                 //
1373                 retract($operation);
1374                 //
1375                 // We must also retract the timer object
1376                 // NOTE: We could write a Rule to do this
1377                 //
1378                 retract($opTimer);
1379                 //
1380                 // Complete the operation
1381                 //
1382                 modify($manager) {finishOperation($operation)};
1383             } else {
1384                 //
1385                 // Just doing this will kick off the LOCKED rule again
1386                 //
1387                 modify($operation) {};
1388             }
1389         } else {
1390             //
1391             // Its not finished yet (i.e. expecting more Response objects)
1392             //
1393             // Or possibly it is a leftover response that we timed the request out previously
1394             //
1395         }
1396         //
1397         // We are going to retract these objects from memory
1398         //
1399         retract($response);
1400
1401 end
1402
1403 /*
1404 *
1405 * This rule responds to SDNC Response Events
1406 *
1407 */
1408
1409 rule "${policyName}.SDNC.RESPONSE"
1410     when
1411         $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1412         $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
1413             closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1414         $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1415             requestId == $event.getRequestId() )
1416         $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
1417             onset.getRequestId() == $event.getRequestId() )
1418         $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1419             requestId == $event.getRequestId().toString(), timerType == "Operation", !expired )
1420         $response : SdncResponse( requestId.toString() == $event.getRequestId().toString() )
1421     then
1422         Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1423         logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1424         logger.debug("{}: {}: event={} manager={} operation={} opTimer={} response={}",
1425                     $params.getClosedLoopControlName(), drools.getRule().getName(),
1426                     $event, $manager, $operation, $operation, $opTimer, $response);
1427
1428         // Get the result of the operation
1429         //
1430         PolicyResult policyResult = $operation.onResponse($response);
1431         if (policyResult != null) {
1432             //
1433             // This Operation has completed, construct a notification showing our results
1434             //
1435             VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1436             notification.setFrom("policy");
1437             notification.setPolicyName(drools.getRule().getName());
1438             notification.setPolicyScope("${policyScope}");
1439             notification.setPolicyVersion("${policyVersion}");
1440             notification.setMessage($operation.getOperationHistory());
1441             notification.setHistory($operation.getHistory());
1442             //
1443             // Ensure the operation is complete
1444             //
1445             if ($operation.isOperationComplete()) {
1446                 //
1447                 // It is complete, remove it from memory
1448                 //
1449                 retract($operation);
1450                 //
1451                 // We must also retract the timer object
1452                 // NOTE: We could write a Rule to do this
1453                 //
1454                 retract($opTimer);
1455                 //
1456                 // Complete the operation
1457                 //
1458                 modify($manager) {finishOperation($operation)};
1459             } else {
1460                 //
1461                 // Just doing this will kick off the LOCKED rule again
1462                 //
1463                 modify($operation) {};
1464             }
1465         } else {
1466             //
1467             // Its not finished yet (i.e. expecting more Response objects)
1468             //
1469             // Or possibly it is a leftover response that we timed the request out previously
1470             //
1471         }
1472         //
1473         // We are going to retract these objects from memory
1474         //
1475         retract($response);
1476
1477 end
1478
1479
1480 /**
1481  * This rule responds to CDS Response Events
1482  */
1483 rule "${policyName}.CDS.RESPONSE"
1484     when
1485         $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1486         $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(),
1487             closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1488         $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1489             requestId == $event.getRequestId() )
1490         $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
1491             onset.getRequestId() == $event.getRequestId() )
1492         $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1493             requestId == $event.getRequestId().toString(), timerType == "Operation", !expired )
1494         $response : CdsResponse( requestId == $event.getRequestId().toString() )
1495
1496     then
1497
1498         Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1499         logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1500         logger.debug("{}: {}: event={} manager={} operation={} opTimer={} response={}",
1501                     $params.getClosedLoopControlName(), drools.getRule().getName(),
1502                     $event, $manager, $operation, $operation, $opTimer, $response);
1503
1504         // Get the result of the operation
1505         PolicyResult policyResult = $operation.onResponse($response);
1506
1507         if (policyResult != null) {
1508             logger.debug("{}: {}: operation finished - result={}",
1509                     $params.getClosedLoopControlName(), drools.getRule().getName(),
1510                     policyResult);
1511
1512             // The operation has completed, construct a notification showing our results
1513             VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1514             notification.setFrom("policy");
1515             notification.setPolicyName(drools.getRule().getName());
1516             notification.setPolicyScope("${policyScope}");
1517             notification.setPolicyVersion("${policyVersion}");
1518             notification.setMessage($operation.getOperationHistory());
1519             notification.setHistory($operation.getHistory());
1520             notification.setNotification(
1521                 ($response != null && CdsActorConstants.SUCCESS.equals($response.getStatus()))
1522                     ? ControlLoopNotificationType.OPERATION_SUCCESS : ControlLoopNotificationType.OPERATION_FAILURE);
1523
1524             // Send the notification
1525             PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
1526
1527             // Ensure the operation is complete
1528             if ($operation.isOperationComplete()) {
1529
1530                 // It is complete, remove it from memory
1531                 retract($operation);
1532
1533                 // We must also retract the timer object
1534                 // NOTE: We could write a Rule to do this
1535                 retract($opTimer);
1536
1537                 // Complete the operation
1538                 modify($manager) {finishOperation($operation)};
1539
1540             } else {
1541                 // Just doing this will kick off the LOCKED rule again
1542                 modify($operation) {};
1543             }
1544         } else {
1545             // Its not finished yet (i.e. expecting more Response objects)
1546             // Or possibly it is a leftover response that we timed the request out previously
1547             logger.info(
1548                 "policyResult is null"
1549                     + "\nIt's not finished yet (i.e. expecting more Response objects)"
1550                     + "\nOr possibly it is a leftover response that we timed the request out previously");
1551         }
1552
1553         // We are going to retract these objects from memory
1554         retract($response);
1555
1556 end
1557
1558 /*
1559 *
1560 * This manages a single timer.
1561 * Due to a bug in the drools code, the drools timer needed to be split from most of the objects in the when clause
1562 *
1563 */
1564 rule "${policyName}.TIMER.FIRED"
1565     timer (expr: $timeout)
1566     when
1567         $timer : ControlLoopTimer($timeout : delay, !expired)
1568     then
1569         Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1570         logger.info("This is ${policyName}.TIMER.FIRED");
1571         modify($timer){setExpired(true)};
1572     end
1573
1574 /*
1575 *
1576 * This is the timer that manages the timeout for an individual operation.
1577 *
1578 */
1579 rule "${policyName}.EVENT.MANAGER.OPERATION.TIMEOUT"
1580     when
1581         $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1582         $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
1583         $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1584             requestId == $event.getRequestId() )
1585         $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(),
1586             onset.getRequestId() == $event.getRequestId() )
1587         $opTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1588             requestId == $event.getRequestId().toString(), expired, timerType == "Operation" )
1589     then
1590
1591     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1592     logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1593     logger.debug("{}: {}: event={} manager={} operation={} opTimer={}",
1594                 $params.getClosedLoopControlName(), drools.getRule().getName(),
1595                 $event, $manager, $operation, $operation, $opTimer);
1596
1597     //
1598     // Tell it it has timed out
1599     //
1600     $operation.setOperationHasTimedOut();
1601     //
1602     // Create a notification for it ("DB Write - end operation")
1603     //
1604     VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1605     notification.setFrom("policy");
1606     notification.setPolicyName(drools.getRule().getName());
1607     notification.setPolicyScope("${policyScope}");
1608     notification.setPolicyVersion("${policyVersion}");
1609     notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
1610     notification.setMessage($operation.getOperationHistory());
1611     notification.setHistory($operation.getHistory());
1612     //
1613     // Let interested parties know
1614     //
1615     PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
1616     //
1617     // Get rid of the timer
1618     //
1619     retract($opTimer);
1620     //
1621     // Ensure the operation is complete
1622     //
1623     if ($operation.isOperationComplete() == true) {
1624         //
1625         // It is complete, remove it from memory
1626         //
1627         retract($operation);
1628         //
1629         // Complete the operation
1630         //
1631         modify($manager) {finishOperation($operation)};
1632     } else {
1633         //
1634         // Just doing this will kick off the LOCKED rule again
1635         //
1636         modify($operation) {};
1637     }
1638 end
1639
1640 /*
1641 *
1642 * This is the timer that manages the overall control loop timeout.
1643 *
1644 */
1645 rule "${policyName}.EVENT.MANAGER.TIMEOUT"
1646     when
1647         $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1648         $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
1649         $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
1650             requestId == $event.getRequestId() )
1651         $clTimer : ControlLoopTimer( closedLoopControlName == $event.getClosedLoopControlName(),
1652             requestId == $event.getRequestId().toString(), expired, timerType == "ClosedLoop" )
1653     then
1654
1655     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1656     logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName());
1657
1658     logger.debug("{}: {}: event={}",
1659               $params.getClosedLoopControlName(), drools.getRule().getName(),
1660               $event);
1661     //
1662     // Tell the Event Manager it has timed out
1663     //
1664     VirtualControlLoopNotification notification = $manager.setControlLoopTimedOut();
1665     if (notification != null) {
1666         notification.setFrom("policy");
1667         notification.setPolicyName(drools.getRule().getName());
1668         notification.setPolicyScope("${policyScope}");
1669         notification.setPolicyVersion("${policyVersion}");
1670         //
1671         // Let interested parties know
1672         //
1673         PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);
1674     }
1675     //
1676     // Retract the event
1677     //
1678     retract($event);
1679 end
1680
1681 /*
1682 *
1683 * This rule cleans up the manager and other objects after an event has
1684 * been retracted.
1685 *
1686 */
1687 rule "${policyName}.EVENT.MANAGER.CLEANUP"
1688     when
1689         $manager : ControlLoopEventManager( $clName : getClosedLoopControlName(), $requestId : getRequestId() )
1690         $operations : LinkedList()
1691                         from collect( ControlLoopOperationManager( onset.closedLoopControlName == $clName,
1692                             onset.getRequestId() == $requestId ) )
1693         $timers : LinkedList()
1694                         from collect( ControlLoopTimer( closedLoopControlName == $clName,
1695                             requestId == $requestId.toString() ) )
1696         not( VirtualControlLoopEvent( closedLoopControlName == $clName, requestId == $requestId ) )
1697     then
1698
1699     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1700     logger.info("{}: {}", $clName, drools.getRule().getName());
1701
1702     logger.debug("{}: {}: manager={} timers={} operations={}",
1703               $clName, drools.getRule().getName(),
1704               $manager, $timers.size(), $operations.size());
1705
1706     //
1707     // Retract lock by invoking unlock()
1708     //
1709     Lock lock = $manager.unlockCurrentOperation();
1710     if(lock != null) {
1711         retract(lock);
1712     }
1713
1714     //
1715     // Retract EVERYTHING
1716     //
1717
1718     retract($manager);
1719
1720     for(Object manager: $operations) {
1721         retract((ControlLoopOperationManager) manager);
1722     }
1723     for(Object timer: $timers) {
1724         retract((ControlLoopTimer) timer);
1725     }
1726 end
1727
1728 /*
1729 *
1730 * This rule will clean up any rogue onsets where there is no
1731 * ControlLoopParams object corresponding to the onset event.
1732 *
1733 */
1734 rule "${policyName}.EVENT.CLEANUP"
1735     when
1736         $event : VirtualControlLoopEvent( $clName: closedLoopControlName )
1737         not ( Params( getClosedLoopControlName() == $clName) )
1738     then
1739
1740     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1741     logger.info("{}: {}", $clName, drools.getRule().getName());
1742     logger.debug("{}: {}: orphan onset event={}",
1743                 $clName, drools.getRule().getName(), $event);
1744
1745     retract($event);
1746 end
1747
1748 /*
1749 * Creates a cleaner for every Params object.
1750 * This has a higher salience so that it is fired before PARAMS.FINISHED in ANY policy.
1751 */
1752 rule "${policyName}.PARAMS.CLEANING"
1753     salience 2
1754     when
1755         $params: Params( )
1756         ParamsInitCleaner( )
1757     then
1758
1759     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1760     logger.info("{}: {} : YAML=[{}]", $params.getClosedLoopControlName(), drools.getRule().getName(),
1761         $params.getControlLoopYaml());
1762
1763     ParamsCleaner cleaner = new ParamsCleaner();
1764     cleaner.setClosedLoopControlName($params.getClosedLoopControlName());
1765     cleaner.setControlLoopYaml($params.getControlLoopYaml());
1766     insert(cleaner);
1767 end
1768
1769 /*
1770 * Finished creating cleaner objects, so remove the trigger.
1771 * This has a higher salience so that it is fired before processing any events.
1772 */
1773 rule "${policyName}.PARAMS.FINISHED"
1774     salience 1
1775     when
1776         $initCleaner: ParamsInitCleaner( )
1777     then
1778
1779     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1780     logger.info("{}: {}", $initCleaner.getClosedLoopControlName(), drools.getRule().getName());
1781
1782     retract($initCleaner);
1783 end
1784
1785 /*
1786 * Identifies Params objects that are still active, removing their associated cleaners.
1787 * This should only leave one active Params object for each policy.
1788 * This has a higher salience so that it is fired before PARAMS.DELETE in ANY policy.
1789 */
1790 rule "${policyName}.PARAMS.ACTIVE"
1791     salience 3
1792     when
1793         $params: Params( getClosedLoopControlName() == "${closedLoopControlName}",
1794                             getControlLoopYaml() == "${controlLoopYaml}" )
1795         $cleaner: ParamsCleaner( getClosedLoopControlName() == "${closedLoopControlName}",
1796                             getControlLoopYaml() == "${controlLoopYaml}"  )
1797     then
1798
1799     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1800     logger.info("{}: {} : YAML=[{}]", $params.getClosedLoopControlName(), drools.getRule().getName(),
1801         $params.getControlLoopYaml());
1802
1803     retract($cleaner);
1804 end
1805
1806 /*
1807 * Delete Params objects that are not active (i.e., those that still have an associated
1808 * cleaner object).
1809 * This has a higher salience so that it is fired before PARAMS.CLEANED in ANY policy.
1810 */
1811 rule "${policyName}.PARAMS.DELETE"
1812     salience 2
1813     when
1814         $params: Params( )
1815         $cleaner: ParamsCleaner( getClosedLoopControlName() == $params.getClosedLoopControlName(),
1816                             getControlLoopYaml() == $params.getControlLoopYaml()  )
1817     then
1818
1819     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1820     logger.info("{}: {} : YAML=[{}]", $params.getClosedLoopControlName(), drools.getRule().getName(),
1821         $params.getControlLoopYaml());
1822
1823     retract($params);
1824 end
1825
1826 /*
1827 * Finished clean-up, so delete the cleaner objects.
1828 * This has a higher salience so that it is fired before processing any events.
1829 */
1830 rule "${policyName}.PARAMS.CLEANED"
1831     salience 1
1832     when
1833         $cleaner: ParamsCleaner( )
1834     then
1835
1836     Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
1837     logger.info("{}: {} : YAML=[{}]", $cleaner.getClosedLoopControlName(), drools.getRule().getName(),
1838         $cleaner.getControlLoopYaml());
1839
1840     retract($cleaner);
1841 end