diff options
author | Jim Hahn <jrh3@att.com> | 2018-11-04 14:33:36 -0500 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2018-11-05 15:49:18 -0500 |
commit | 6f4b15df2c0711f042a930eed614c826704954e9 (patch) | |
tree | 82525d67db337dc90f5eec5ab1a5bf251d6b0d64 /controlloop/templates/template.demo.clc/src/main | |
parent | 470b4061be484787787b83644fde8429c1688318 (diff) |
Cleanup all Param objects when any rules change
Fix license date for new junit test.
Updated some comments.
Ensured different control loop names in different rule sets of new junit
test.
Used different mechanism for examining facts in new junit test.
Fixed typo in comment.
Changed the way new junit checks for existence of facts.
Fixed another typo in comment.
Updated clean-up rules in CLC template and added junit test to verify
that Params are cleaned up appropriately.
Removed code that retracts $manager objects so that the manager clean-up
rule will be used, instead, thus ensuring that all associated objects are
deleted when the manager is deleted. This was done for amsterdam, CLC,
and casablanca templates.
Added an extra fireAllRules() after calls to fireUntilHalt() to enable
clean-up rules to fire in junit tests.
Address Josh's review comments.
Enhance junit test to compare actual object references to ensure that
Param objects are as expected.
Change-Id: If1fefd3f57e417180c62a3849defbaa6ae83afc1
Issue-ID: POLICY-1248
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'controlloop/templates/template.demo.clc/src/main')
-rw-r--r-- | controlloop/templates/template.demo.clc/src/main/resources/__closedLoopControlName__.drl | 118 |
1 files changed, 56 insertions, 62 deletions
diff --git a/controlloop/templates/template.demo.clc/src/main/resources/__closedLoopControlName__.drl b/controlloop/templates/template.demo.clc/src/main/resources/__closedLoopControlName__.drl index 15de89850..f4fcba96e 100644 --- a/controlloop/templates/template.demo.clc/src/main/resources/__closedLoopControlName__.drl +++ b/controlloop/templates/template.demo.clc/src/main/resources/__closedLoopControlName__.drl @@ -75,6 +75,8 @@ import org.slf4j.Logger; import java.time.Instant; import java.util.LinkedList; import java.util.Iterator; +import java.util.HashSet; +import java.util.Set; import org.onap.policy.drools.system.PolicyEngine; @@ -103,7 +105,8 @@ end */ declare ParamsCleaner closedLoopControlName : String - controlLoopYaml : String + identified : boolean // true if all active Params have been identified + active : Set // Params that are still active end @@ -127,7 +130,8 @@ end /* * -* Called once and only once to insert the parameters into working memory for this Closed Loop policy. +* Called to insert the parameters into working memory for this Closed Loop policy. This is called +* once each time a closed loop is added or its YAML is updated. * This has a higher salience so we can ensure that the Params is created before we have a chance to * discard any events. * @@ -142,6 +146,12 @@ rule "${policyName}.SETUP" params.setClosedLoopControlName("${closedLoopControlName}"); params.setControlLoopYaml("${controlLoopYaml}"); insert(params); + + ParamsCleaner cleaner = new ParamsCleaner(); + cleaner.setClosedLoopControlName("${closedLoopControlName}"); + cleaner.setIdentified(false); + cleaner.setActive(new HashSet()); + insert(cleaner); // Note: globals have bad behavior when persistence is used, // hence explicitly getting the logger vs using a global @@ -379,8 +389,8 @@ rule "${policyName}.EVENT.MANAGER" $params.getClosedLoopControlName(), drools.getRule().getName()); retract($manager.getOnsetEvent()); - retract($manager); - retract($clTimer); + + // don't retract manager, etc. - a clean-up rule will do that // // TODO - what if we get subsequent Events for this RequestId? // By default, it will all start over again. May be confusing for Ruby. @@ -427,8 +437,8 @@ rule "${policyName}.EVENT.MANAGER" $params.getClosedLoopControlName(), drools.getRule().getName()); retract($manager.getOnsetEvent()); - retract($manager); - retract($clTimer); + + // don't retract manager, etc. - a clean-up rule will do that } } // @@ -482,8 +492,8 @@ rule "${policyName}.EVENT.MANAGER" PolicyEngine.manager.deliver("POLICY-CL-MGT", notification); retract($event); - retract($manager); - retract($clTimer); + + // don't retract manager, etc. - a clean-up rule will do that if(result.getB() != null) { retract(result.getB()); @@ -516,8 +526,8 @@ rule "${policyName}.EVENT.MANAGER" PolicyEngine.manager.deliver("POLICY-CL-MGT", notification); retract($event); - retract($manager); - retract($clTimer); + + // don't retract manager, etc. - a clean-up rule will do that } end @@ -1324,91 +1334,75 @@ rule "${policyName}.EVENT.CLEANUP" end /* -* -* When rules are deleted, the associated Params (and its subordinate objects) -* remain in working memory, because there are no longer any rules to clean -* them up. However, ANY time new rules are loaded, this rule will trigger -* a clean-up of ALL Params, regardless of their name & yaml, thus removing -* any that no longer have associated rules. -* This has a higher salience so that we immediately check Params when the -* rules change, before processing any events. -* +* Indicates to the cleaner that this Params object is still active. +* This has a higher salience so that it is fired before processing any events. */ -rule "${policyName}.PARAMS.CHECKUP" - salience 2 +rule "${policyName}.PARAMS.ACTIVE" + salience 4 when - Params( $clName: closedLoopControlName, $yaml: controlLoopYaml ) + $params: Params( getClosedLoopControlName() == "${closedLoopControlName}", + getControlLoopYaml() == "${controlLoopYaml}" ) + ParamsCleaner( !identified, $active: active ) then Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage()); - logger.info("{}: {} : YAML=[{}]", $clName, drools.getRule().getName(), $yaml); - - ParamsCleaner cleaner = new ParamsCleaner(); - cleaner.setClosedLoopControlName($clName); - cleaner.setControlLoopYaml($yaml); + logger.info("{}: {} : YAML=[{}]", $params.getClosedLoopControlName(), drools.getRule().getName(), + $params.getControlLoopYaml()); + + $active.add($params); - insert(cleaner); + // do NOT update anything at this point end /* -* -* This rule removes "cleaner" objects for rules that are still active, thus -* preventing the associated Params objects from being removed. Any cleaners -* that are left after this rule has fired will cause their corresponding Params -* to be removed. -* This has a higher salience so that we discard the cleaner before it has -* a chance to force the removal of the associated Params. -* +* Finished identifying active Params objects. Begin deleting inactive Params. +* This has a higher salience so that it is fired before processing any events. */ -rule "${policyName}.CLEANER.ACTIVE" - salience 2 +rule "${policyName}.PARAMS.IDENTIFIED" + salience 3 when - $cleaner: ParamsCleaner( getClosedLoopControlName() == "${closedLoopControlName}", getControlLoopYaml() == "${controlLoopYaml}" ) + $cleaner: ParamsCleaner( !identified ) then Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage()); - logger.info("{}: {} : YAML=[{}]", $cleaner.getClosedLoopControlName(), drools.getRule().getName(), $cleaner.getControlLoopYaml()); + logger.info("{}: {}", $cleaner.getClosedLoopControlName(), drools.getRule().getName()); - retract($cleaner); + $cleaner.setIdentified(true); + update($cleaner); end /* -* -* This rule removes Params objects that no longer have associated rules; if a -* Params still had associated rules, then the cleaner would have been removed -* by those rules and thus this rule would not fire. -* This has a higher salience so that we remove old Params before it causes any -* events to be processed. -* +* Delete Params objects that have not been identified as being active. +* This has a higher salience so that it is fired before processing any events. */ -rule "${policyName}.PARAMS.CLEANUP" - salience 1 +rule "${policyName}.PARAMS.DELETE" + salience 2 when - $params: Params( $clName: closedLoopControlName, $yaml: controlLoopYaml ) - ParamsCleaner( getClosedLoopControlName() == $clName, getControlLoopYaml() == $yaml ) + $params: Params( ) + ParamsCleaner( identified, !active.contains($params) ) then Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage()); - logger.info("{}: {} : YAML=[{}]", $params.getClosedLoopControlName(), drools.getRule().getName(), $params.getControlLoopYaml()); - + logger.info("{}: {} : YAML=[{}]", $params.getClosedLoopControlName(), drools.getRule().getName(), + $params.getControlLoopYaml()); + retract($params); - // Note: the cleaner may be needed for cleaning additional params, thus - // we do not retract it here - we'll leave that to another rule + // do NOT update anything at this point end /* -* -* This rule removes "cleaner" objects when they're no longer needed. -* +* Finished deleting inactive Params objects, so remove the cleaner. +* This has a higher salience so that it is fired before processing any events. */ -rule "${policyName}.CLEANER.CLEANUP" +rule "${policyName}.PARAMS.CLEANED" + salience 1 when - $cleaner: ParamsCleaner( ) + $cleaner: ParamsCleaner( identified ) then Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage()); - logger.info("{}: {} : YAML=[{}]", $cleaner.getClosedLoopControlName(), drools.getRule().getName(), $cleaner.getControlLoopYaml()); + logger.info("{}: {}", $cleaner.getClosedLoopControlName(), drools.getRule().getName()); retract($cleaner); end |