- Created by Wayne Dunican on Sept 13, 2023
You are viewing an old version of this page. View the current version.
Compare with Current View Page History
Version 1 Next »
POLICY-4765 - Getting issue details... STATUS
Description
apex-pdp has a concept of global context but doesn't have a mechanism for declaring and sharing context across policy sets in the same JVM.
If policies are deployed independently they should be able to share context between them.
Technically this should not be an issue in the Apex,but currently there is no way of declaring and/or passing a reference to the context hashmaps between apex engines.
In this story we investigate and implement this improvement.
Investigation
Procedure
- Spin up docker-compose environment for testing
- Create two policies that share a context album
- Deploy both policies
- Determine is Policy A can access context modified by Policy B
Steps
- Modify DecisionMaker Policy
- Split into two seperate policies:
- One which contains the answer logic
- One which contains the decision logic
- Deploy both policies in single Apex engine
- Policy A and Policy B will both share the AnswerAlbum Context
- Set the answers using Policy A which contains the logic to set the answers. Policy B does not contain this logic.
- Make a decision using Policy B. This will choose an answer from the AnswerAlbum.
- Split into two seperate policies:
Step | Description | Result |
---|---|---|
Modify DecisionMaker Policy |
|
DecisionMakerAnswerPolicy Expand source
#------------------------------------------------------------------------------- # ============LICENSE_START======================================================= # Copyright (C) 2016-2018 Ericsson. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # SPDX-License-Identifier: Apache-2.0 # ============LICENSE_END========================================================= #------------------------------------------------------------------------------- # POLICY A model create name=DecisionMakerPolicyModel schema create name=SimpleStringType flavour=Java schema=java.lang.String schema create name=SimpleIntegerType flavour=Java schema=java.lang.Integer album create name=AnswerAlbum scope=policy writable=true schemaName=SimpleStringType album create name=LastAnswerAlbum scope=policy writable=true schemaName=SimpleIntegerType event create name=AnswerEvent nameSpace=org.onap.policy.apex.domains.decisionmaker source=DCAE target=apex event parameter create name=AnswerEvent parName=a0 schemaName=SimpleStringType event parameter create name=AnswerEvent parName=a1 schemaName=SimpleStringType event parameter create name=AnswerEvent parName=a2 schemaName=SimpleStringType event parameter create name=AnswerEvent parName=a3 schemaName=SimpleStringType event parameter create name=AnswerEvent parName=a4 schemaName=SimpleStringType event parameter create name=AnswerEvent parName=a5 schemaName=SimpleStringType event parameter create name=AnswerEvent parName=a6 schemaName=SimpleStringType event create name=MakeDecisionEvent nameSpace=org.onap.policy.apex.domains.decisionmaker source=DCAE target=apex event parameter create name=MakeDecisionEvent parName=mode schemaName=SimpleStringType event create name=DecisionEvent nameSpace=org.onap.policy.apex.domains.decisionmaker source=DCAE target=apex event parameter create name=DecisionEvent parName=decision schemaName=SimpleStringType task create name=AnswerInitTask task inputfield create name=AnswerInitTask fieldName=a0 schemaName=SimpleStringType task inputfield create name=AnswerInitTask fieldName=a1 schemaName=SimpleStringType task inputfield create name=AnswerInitTask fieldName=a2 schemaName=SimpleStringType task inputfield create name=AnswerInitTask fieldName=a3 schemaName=SimpleStringType task inputfield create name=AnswerInitTask fieldName=a4 schemaName=SimpleStringType task inputfield create name=AnswerInitTask fieldName=a5 schemaName=SimpleStringType task inputfield create name=AnswerInitTask fieldName=a6 schemaName=SimpleStringType task outputfield create name=AnswerInitTask fieldName=a0 schemaName=SimpleStringType task outputfield create name=AnswerInitTask fieldName=a1 schemaName=SimpleStringType task outputfield create name=AnswerInitTask fieldName=a2 schemaName=SimpleStringType task outputfield create name=AnswerInitTask fieldName=a3 schemaName=SimpleStringType task outputfield create name=AnswerInitTask fieldName=a4 schemaName=SimpleStringType task outputfield create name=AnswerInitTask fieldName=a5 schemaName=SimpleStringType task outputfield create name=AnswerInitTask fieldName=a6 schemaName=SimpleStringType task contextref create name=AnswerInitTask albumName=AnswerAlbum task contextref create name=AnswerInitTask albumName=LastAnswerAlbum task logic create name=AnswerInitTask logicFlavour=JAVASCRIPT logic=LS #MACROFILE:"src/main/resources/logic/AnswerInitTask.js" LE task create name=RandomAnswerTask task inputfield create name=RandomAnswerTask fieldName=mode schemaName=SimpleStringType task outputfield create name=RandomAnswerTask fieldName=decision schemaName=SimpleStringType task contextref create name=RandomAnswerTask albumName=AnswerAlbum task logic create name=RandomAnswerTask logicFlavour=JAVASCRIPT logic=LS #MACROFILE:"src/main/resources/logic/RandomAnswerTask.js" LE task create name=PessimisticAnswerTask task inputfield create name=PessimisticAnswerTask fieldName=mode schemaName=SimpleStringType task outputfield create name=PessimisticAnswerTask fieldName=decision schemaName=SimpleStringType task contextref create name=PessimisticAnswerTask albumName=AnswerAlbum task logic create name=PessimisticAnswerTask logicFlavour=JAVASCRIPT logic=LS #MACROFILE:"src/main/resources/logic/PessimisticAnswerTask.js" LE task create name=OptimisticAnswerTask task inputfield create name=OptimisticAnswerTask fieldName=mode schemaName=SimpleStringType task outputfield create name=OptimisticAnswerTask fieldName=decision schemaName=SimpleStringType task contextref create name=OptimisticAnswerTask albumName=AnswerAlbum task logic create name=OptimisticAnswerTask logicFlavour=JAVASCRIPT logic=LS #MACROFILE:"src/main/resources/logic/OptimisticAnswerTask.js" LE task create name=DitheringAnswerTask task inputfield create name=DitheringAnswerTask fieldName=mode schemaName=SimpleStringType task outputfield create name=DitheringAnswerTask fieldName=decision schemaName=SimpleStringType task contextref create name=DitheringAnswerTask albumName=AnswerAlbum task logic create name=DitheringAnswerTask logicFlavour=JAVASCRIPT logic=LS #MACROFILE:"src/main/resources/logic/DitheringAnswerTask.js" LE task create name=RoundRobinAnswerTask task inputfield create name=RoundRobinAnswerTask fieldName=mode schemaName=SimpleStringType task outputfield create name=RoundRobinAnswerTask fieldName=decision schemaName=SimpleStringType task contextref create name=RoundRobinAnswerTask albumName=AnswerAlbum task contextref create name=RoundRobinAnswerTask albumName=LastAnswerAlbum task logic create name=RoundRobinAnswerTask logicFlavour=JAVASCRIPT logic=LS #MACROFILE:"src/main/resources/logic/RoundRobinAnswerTask.js" LE policy create name=AnswerInitPolicy template=freestyle firstState=AnswerInitState policy state create name=AnswerInitPolicy stateName=AnswerInitState triggerName=AnswerEvent defaultTaskName=AnswerInitTask policy state output create name=AnswerInitPolicy stateName=AnswerInitState outputName=AnswerInitOutput eventName=AnswerEvent policy state taskref create name=AnswerInitPolicy stateName=AnswerInitState taskName=AnswerInitTask outputType=DIRECT outputName=AnswerInitOutput validate
Policy B Expand source
#------------------------------------------------------------------------------- # ============LICENSE_START======================================================= # Copyright (C) 2016-2018 Ericsson. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # SPDX-License-Identifier: Apache-2.0 # ============LICENSE_END========================================================= #------------------------------------------------------------------------------- # POLICY B model create name=DecisionMakerPolicyModelTwo schema create name=SimpleStringType flavour=Java schema=java.lang.String schema create name=SimpleIntegerType flavour=Java schema=java.lang.Integer album create name=AnswerAlbum scope=policy writable=true schemaName=SimpleStringType album create name=LastAnswerAlbum scope=policy writable=true schemaName=SimpleIntegerType event create name=AnswerEvent nameSpace=org.onap.policy.apex.domains.decisionmaker source=DCAE target=apex event parameter create name=AnswerEvent parName=a0 schemaName=SimpleStringType event parameter create name=AnswerEvent parName=a1 schemaName=SimpleStringType event parameter create name=AnswerEvent parName=a2 schemaName=SimpleStringType event parameter create name=AnswerEvent parName=a3 schemaName=SimpleStringType event parameter create name=AnswerEvent parName=a4 schemaName=SimpleStringType event parameter create name=AnswerEvent parName=a5 schemaName=SimpleStringType event parameter create name=AnswerEvent parName=a6 schemaName=SimpleStringType event create name=MakeDecisionEvent nameSpace=org.onap.policy.apex.domains.decisionmaker source=DCAE target=apex event parameter create name=MakeDecisionEvent parName=mode schemaName=SimpleStringType event create name=DecisionEvent nameSpace=org.onap.policy.apex.domains.decisionmaker source=DCAE target=apex event parameter create name=DecisionEvent parName=decision schemaName=SimpleStringType task create name=AnswerInitTask task inputfield create name=AnswerInitTask fieldName=a0 schemaName=SimpleStringType task inputfield create name=AnswerInitTask fieldName=a1 schemaName=SimpleStringType task inputfield create name=AnswerInitTask fieldName=a2 schemaName=SimpleStringType task inputfield create name=AnswerInitTask fieldName=a3 schemaName=SimpleStringType task inputfield create name=AnswerInitTask fieldName=a4 schemaName=SimpleStringType task inputfield create name=AnswerInitTask fieldName=a5 schemaName=SimpleStringType task inputfield create name=AnswerInitTask fieldName=a6 schemaName=SimpleStringType task outputfield create name=AnswerInitTask fieldName=a0 schemaName=SimpleStringType task outputfield create name=AnswerInitTask fieldName=a1 schemaName=SimpleStringType task outputfield create name=AnswerInitTask fieldName=a2 schemaName=SimpleStringType task outputfield create name=AnswerInitTask fieldName=a3 schemaName=SimpleStringType task outputfield create name=AnswerInitTask fieldName=a4 schemaName=SimpleStringType task outputfield create name=AnswerInitTask fieldName=a5 schemaName=SimpleStringType task outputfield create name=AnswerInitTask fieldName=a6 schemaName=SimpleStringType task contextref create name=AnswerInitTask albumName=AnswerAlbum task contextref create name=AnswerInitTask albumName=LastAnswerAlbum task logic create name=AnswerInitTask logicFlavour=JAVASCRIPT logic=LS #MACROFILE:"src/main/resources/logic/AnswerInitTask.js" LE task create name=RandomAnswerTask task inputfield create name=RandomAnswerTask fieldName=mode schemaName=SimpleStringType task outputfield create name=RandomAnswerTask fieldName=decision schemaName=SimpleStringType task contextref create name=RandomAnswerTask albumName=AnswerAlbum task logic create name=RandomAnswerTask logicFlavour=JAVASCRIPT logic=LS #MACROFILE:"src/main/resources/logic/RandomAnswerTask.js" LE task create name=PessimisticAnswerTask task inputfield create name=PessimisticAnswerTask fieldName=mode schemaName=SimpleStringType task outputfield create name=PessimisticAnswerTask fieldName=decision schemaName=SimpleStringType task contextref create name=PessimisticAnswerTask albumName=AnswerAlbum task logic create name=PessimisticAnswerTask logicFlavour=JAVASCRIPT logic=LS #MACROFILE:"src/main/resources/logic/PessimisticAnswerTask.js" LE task create name=OptimisticAnswerTask task inputfield create name=OptimisticAnswerTask fieldName=mode schemaName=SimpleStringType task outputfield create name=OptimisticAnswerTask fieldName=decision schemaName=SimpleStringType task contextref create name=OptimisticAnswerTask albumName=AnswerAlbum task logic create name=OptimisticAnswerTask logicFlavour=JAVASCRIPT logic=LS #MACROFILE:"src/main/resources/logic/OptimisticAnswerTask.js" LE task create name=DitheringAnswerTask task inputfield create name=DitheringAnswerTask fieldName=mode schemaName=SimpleStringType task outputfield create name=DitheringAnswerTask fieldName=decision schemaName=SimpleStringType task contextref create name=DitheringAnswerTask albumName=AnswerAlbum task logic create name=DitheringAnswerTask logicFlavour=JAVASCRIPT logic=LS #MACROFILE:"src/main/resources/logic/DitheringAnswerTask.js" LE task create name=RoundRobinAnswerTask task inputfield create name=RoundRobinAnswerTask fieldName=mode schemaName=SimpleStringType task outputfield create name=RoundRobinAnswerTask fieldName=decision schemaName=SimpleStringType task contextref create name=RoundRobinAnswerTask albumName=AnswerAlbum task contextref create name=RoundRobinAnswerTask albumName=LastAnswerAlbum task logic create name=RoundRobinAnswerTask logicFlavour=JAVASCRIPT logic=LS #MACROFILE:"src/main/resources/logic/RoundRobinAnswerTask.js" LE policy create name=DecisionMakerPolicy template=freestyle firstState=MakeDecisionState policy state create name=DecisionMakerPolicy stateName=MakeDecisionState triggerName=MakeDecisionEvent defaultTaskName=RandomAnswerTask policy state output create name=DecisionMakerPolicy stateName=MakeDecisionState outputName=DecisionFinalOutput eventName=DecisionEvent policy state taskref create name=DecisionMakerPolicy stateName=MakeDecisionState taskName=RandomAnswerTask outputType=DIRECT outputName=DecisionFinalOutput policy state taskref create name=DecisionMakerPolicy stateName=MakeDecisionState taskName=PessimisticAnswerTask outputType=DIRECT outputName=DecisionFinalOutput policy state taskref create name=DecisionMakerPolicy stateName=MakeDecisionState taskName=OptimisticAnswerTask outputType=DIRECT outputName=DecisionFinalOutput policy state taskref create name=DecisionMakerPolicy stateName=MakeDecisionState taskName=DitheringAnswerTask outputType=DIRECT outputName=DecisionFinalOutput policy state taskref create name=DecisionMakerPolicy stateName=MakeDecisionState taskName=RoundRobinAnswerTask outputType=DIRECT outputName=DecisionFinalOutput policy state selecttasklogic create name=DecisionMakerPolicy stateName=MakeDecisionState logicFlavour=JAVASCRIPT logic=LS #MACROFILE:"src/main/resources/logic/MakeDecisionStateTSL.js" LE validate |
Other Changes |
|
ApexConfig.json Expand source
{ "engineServiceParameters": { "name": "MyApexEngine", "version": "0.0.1", "id": 45, "instanceCount": 4, "deploymentPort": 12345, "engineParameters": { "executorParameters": { "JAVASCRIPT": { "parameterClassName": "org.onap.policy.apex.plugins.executor.javascript.JavascriptExecutorParameters" } } } }, "eventInputParameters": { "RESTConsumerTwo": { "carrierTechnologyParameters": { "carrierTechnology": "RESTSERVER", "parameterClassName": "org.onap.policy.apex.plugins.event.carrier.restserver.RestServerCarrierTechnologyParameters", "parameters": { "standalone": true, "host": "0.0.0.0", "port": 23325 } }, "eventProtocolParameters": { "eventProtocol": "JSON" }, "synchronousMode": true, "synchronousPeer": "RESTProducerTwo", "synchronousTimeout": 500 } }, "eventOutputParameters": { "logProducerTwo": { "carrierTechnologyParameters": { "carrierTechnology": "FILE", "parameters": { "fileName": "/tmp/EventsOutTwo.json" } }, "eventProtocolParameters": { "eventProtocol": "JSON" } }, "RESTProducerTwo": { "carrierTechnologyParameters":{ "carrierTechnology" : "RESTSERVER", "parameterClassName" : "org.onap.policy.apex.plugins.event.carrier.restserver.RestServerCarrierTechnologyParameters" }, "eventProtocolParameters":{ "eventProtocol" : "JSON" }, "synchronousMode" : true, "synchronousPeer" : "RESTConsumerTwo", "synchronousTimeout" : 500 } } }
ToscaTemplate.json Expand source
{ "tosca_definitions_version": "tosca_simple_yaml_1_1_0", "topology_template": { "policies": [ { "onap.policies.native.apex.DecisionMakerTwo": { "type": "onap.policies.native.Apex", "type_version": "1.0.0", "name": "onap.policies.native.apex.DecisionMakerTwo", "version": "1.0.0", "properties": { } } } ] } }
MakeDecision_server.html Expand source
<!-- ============LICENSE_START======================================================= Copyright (C) 2016-2018 Ericsson. All rights reserved. ================================================================================ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. SPDX-License-Identifier: Apache-2.0 ============LICENSE_END========================================================= --> <!-- http://localhost:23325/apex/eventInput/EventIn --> <html> <head> <script src="http://code.jquery.com/jquery-latest.js"></script> <script> $(document).ready(function() { $("#answerspost").click(function(e) { var elements = document.getElementById("answerform").elements; var formValues = new Object; formValues["name"] = "AnswerEvent"; for (var i = 0, element; element = elements[i++];) { if (element.type === "text" && element.value != "") { formValues[element.name] = element.value; } } console.log(formValues); var stringifiedForm = JSON.stringify(formValues); console.log(stringifiedForm); $.ajax({ type : "POST", url : "http://localhost:23325/apex/RESTConsumerTwo/EventIn", data : stringifiedForm, crossDomain : true, contentType : "application/json; charset=utf-8", success : function(data) { alert("Answers Set Successfully !!!"); }, failure : function(errMsg) { alert(errMsg); } }); e.preventDefault(); //STOP default action }); }); </script> <script> $(document).ready(function() { $("#modepost").click(function(e) { var elements = document.getElementById("modeform").elements; var formValues = new Object; formValues["name"] = "MakeDecisionEvent"; for (var i = 0, element; element = elements[i++];) { if (element.type === "radio" && element.checked) { formValues[element.name] = element.value; } } console.log(formValues); var stringifiedForm = JSON.stringify(formValues); console.log(stringifiedForm); $.ajax({ type : "POST", url : "http://localhost:23325/apex/RESTConsumerTwo/EventIn", data : stringifiedForm, crossDomain : true, contentType : "application/json; charset=utf-8", success : function(data) { alert("Decision Taken: " + data.decision); }, failure : function(errMsg) { alert(errMsg); } }); e.preventDefault(); //STOP default action }); }); </script> </head> <body> <h3>Decision Maker Answers</h3> <form name="answerform" id="answerform" method="POST"> <table> <tr> <td>First Answer:</td> <td><input type="text" name="a0" value="Never Ever" /></td> </tr> <tr> <td>Second Answer:</td> <td><input type="text" name="a1" value="No" /></td> </tr> <tr> <td>Third Answer:</td> <td><input type="text" name="a2" value="Maybe not" /></td> </tr> <tr> <td>Fourth Answer</td> <td><input type="text" name="a3" value="Wait" /></td> </tr> <tr> <td>Fifth Answer:</td> <td><input type="text" name="a4" value="Maybe" /></td> </tr> <tr> <td>Sixth Answer:</td> <td><input type="text" name="a5" value="Yes" /></td> </tr> <tr> <td>Seventh Answer:</td> <td><input type="text" name="a6" value="Absolutely" /></td> </tr> <tr> <td /> <td><input type="button" class="btn btn-info" id="answerspost" value="Set Answers"></td> </tr> </table> </form> <h3>Decision Maker Mode</h3> <form name="modeform" id="modeform" method="POST"> <table> <tr> <td><input name="mode" type="radio" value="random" checked="checked">random</td> <td><input name="mode" type="radio" value="pessimistic">pessimistic</td> <td><input name="mode" type="radio" value="optimistic"> optimistic</td> <td><input name="mode" type="radio" value="dithering">dithering</td> </tr> <tr> <td /> <td /> <td /> <td><input type="button" class="btn btn-info" id="modepost" value="Make Decision"></td> </tr> </table> </form> </body> </html>
pom.xml Expand source
<execution> <id>generate-decisionmaker-policy</id> <phase>compile</phase> <goals> <goal>java</goal> </goals> <configuration> <mainClass>org.onap.policy.apex.auth.clieditor.tosca.ApexCliToscaEditorMain</mainClass> <classpathScope>compile</classpathScope> <arguments> <argument>--command-file=${project.basedir}/src/main/resources/policy/${policymodel.decisionmaker.name}.apex</argument> <argument>--output-tosca-file=${project.build.directory}/classes/${policymodel.decisionmaker.name}.json</argument> <argument>--log-file=${project.build.directory}/${policymodel.decisionmaker.name}_policygeneration.log</argument> <argument>--apex-config-file=${project.basedir}/src/main/resources/examples/config/DecisionMaker/ApexConfigRESTServerNoModel.json</argument> <argument>--tosca-template-file=${project.basedir}/src/main/resources/tosca/ToscaTemplate.json</argument> </arguments> </configuration> </execution> <execution> <id>generate-decisionmakertwo-policy</id> <phase>compile</phase> <goals> <goal>java</goal> </goals> <configuration> <mainClass>org.onap.policy.apex.auth.clieditor.tosca.ApexCliToscaEditorMain</mainClass> <classpathScope>compile</classpathScope> <arguments> <argument>--command-file=${project.basedir}/src/main/resources/policy/${policymodel.decisionmakertwo.name}.apex</argument> <argument>--output-tosca-file=${project.build.directory}/classes/${policymodel.decisionmakertwo.name}.json</argument> <argument>--log-file=${project.build.directory}/${policymodel.decisionmakertwo.name}_policygeneration.log</argument> <argument>--apex-config-file=${project.basedir}/src/main/resources/examples/config/DecisionMaker/ApexConfigRESTServerNoModelTwo.json</argument> <argument>--tosca-template-file=${project.basedir}/src/main/resources/tosca/ToscaTemplateTwo.json</argument> </arguments> </configuration> </execution> |
Deploy both policies | Deploy both policies on single Apex instance | |
Set Answers (Policy A) | Use Policy A to set the answers | |
Make Decision (Policy B) | Use Policy B to select an answer | |
- No labels