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