diff options
Diffstat (limited to 'examples')
16 files changed, 1044 insertions, 0 deletions
diff --git a/examples/examples-decisionmaker-sharedcontext/pom.xml b/examples/examples-decisionmaker-sharedcontext/pom.xml new file mode 100644 index 000000000..acbfeff3d --- /dev/null +++ b/examples/examples-decisionmaker-sharedcontext/pom.xml @@ -0,0 +1,121 @@ +<!-- + ============LICENSE_START======================================================= + Copyright (C) 2023 Nordix Foundation. + ================================================================================ + 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========================================================= +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.onap.policy.apex-pdp.examples</groupId> + <artifactId>examples</artifactId> + <version>3.0.1-SNAPSHOT</version> + </parent> + + <artifactId>examples-decisionmaker-sharedcontext</artifactId> + <name>${project.artifactId}</name> + <description>Specific code for the APEX Decision Maker Example</description> + + <properties> + <policymodel.decisionmaker.name>DecisionMakerPolicyModel</policymodel.decisionmaker.name> + <policymodel.decisionmakertwo.name>DecisionMakerPolicyModel_Decision</policymodel.decisionmakertwo.name> + <apex-domains-decisionmaker-dir>${project.basedir}/src</apex-domains-decisionmaker-dir> + </properties> + + <dependencies> + <dependency> + <groupId>org.onap.policy.apex-pdp.auth</groupId> + <artifactId>cli-editor</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.onap.policy.apex-pdp.services</groupId> + <artifactId>services-engine</artifactId> + <version>${project.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.onap.policy.apex-pdp.plugins.plugins-context.plugins-context-schema</groupId> + <artifactId>plugins-context-schema-avro</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.onap.policy.apex-pdp.plugins.plugins-executor</groupId> + <artifactId>plugins-executor-javascript</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.onap.policy.apex-pdp.plugins.plugins-event.plugins-event-carrier</groupId> + <artifactId>plugins-event-carrier-restserver</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <argLine>-Xss1m</argLine> + </configuration> + </plugin> + <!-- Generate the APEX Policy JSON from the APEX CLI command --> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>exec-maven-plugin</artifactId> + <executions> + <execution> + <id>generate-decisionmaker-sharedcontext-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-sharedcontext-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/ApexConfigRESTServerNoModel_Decision.json</argument> + <argument>--tosca-template-file=${project.basedir}/src/main/resources/tosca/ToscaTemplateTwo.json</argument> + </arguments> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> +</project> diff --git a/examples/examples-decisionmaker-sharedcontext/src/main/resources/examples/config/DecisionMaker/ApexConfigRESTServerNoModel.json b/examples/examples-decisionmaker-sharedcontext/src/main/resources/examples/config/DecisionMaker/ApexConfigRESTServerNoModel.json new file mode 100644 index 000000000..8d72f66ee --- /dev/null +++ b/examples/examples-decisionmaker-sharedcontext/src/main/resources/examples/config/DecisionMaker/ApexConfigRESTServerNoModel.json @@ -0,0 +1,60 @@ +{ + "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": { + "RESTConsumer": { + "carrierTechnologyParameters": { + "carrierTechnology": "RESTSERVER", + "parameterClassName": "org.onap.policy.apex.plugins.event.carrier.restserver.RestServerCarrierTechnologyParameters", + "parameters": { + "standalone": true, + "host": "0.0.0.0", + "port": 23324 + } + }, + "eventProtocolParameters": { + "eventProtocol": "JSON" + }, + "synchronousMode": true, + "synchronousPeer": "RESTProducer", + "synchronousTimeout": 500 + } + }, + "eventOutputParameters": { + "logProducer": { + "carrierTechnologyParameters": { + "carrierTechnology": "FILE", + "parameters": { + "fileName": "/tmp/EventsOut.json" + } + }, + "eventProtocolParameters": { + "eventProtocol": "JSON" + } + }, + "RESTProducer": { + "carrierTechnologyParameters":{ + "carrierTechnology" : "RESTSERVER", + "parameterClassName" : "org.onap.policy.apex.plugins.event.carrier.restserver.RestServerCarrierTechnologyParameters" + }, + "eventProtocolParameters":{ + "eventProtocol" : "JSON" + }, + "synchronousMode" : true, + "synchronousPeer" : "RESTConsumer", + "synchronousTimeout" : 500 + } + } +} diff --git a/examples/examples-decisionmaker-sharedcontext/src/main/resources/examples/config/DecisionMaker/ApexConfigRESTServerNoModel_Decision.json b/examples/examples-decisionmaker-sharedcontext/src/main/resources/examples/config/DecisionMaker/ApexConfigRESTServerNoModel_Decision.json new file mode 100644 index 000000000..5feee4788 --- /dev/null +++ b/examples/examples-decisionmaker-sharedcontext/src/main/resources/examples/config/DecisionMaker/ApexConfigRESTServerNoModel_Decision.json @@ -0,0 +1,60 @@ +{ + "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 + } + } +} diff --git a/examples/examples-decisionmaker-sharedcontext/src/main/resources/examples/html/DecisionMaker/MakeDecision_Server.html b/examples/examples-decisionmaker-sharedcontext/src/main/resources/examples/html/DecisionMaker/MakeDecision_Server.html new file mode 100644 index 000000000..8809f05ad --- /dev/null +++ b/examples/examples-decisionmaker-sharedcontext/src/main/resources/examples/html/DecisionMaker/MakeDecision_Server.html @@ -0,0 +1,153 @@ +<!-- + ============LICENSE_START======================================================= + Copyright (C) 2023 Nordix Foundation. 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:23324/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:23324/apex/RESTConsumer/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:23324/apex/RESTConsumer/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> diff --git a/examples/examples-decisionmaker-sharedcontext/src/main/resources/examples/html/DecisionMaker/MakeDecision_Server_Decsion.html b/examples/examples-decisionmaker-sharedcontext/src/main/resources/examples/html/DecisionMaker/MakeDecision_Server_Decsion.html new file mode 100644 index 000000000..cf79ed688 --- /dev/null +++ b/examples/examples-decisionmaker-sharedcontext/src/main/resources/examples/html/DecisionMaker/MakeDecision_Server_Decsion.html @@ -0,0 +1,153 @@ +<!-- + ============LICENSE_START======================================================= + Copyright (C) 2023 Nordix Foundation. 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> diff --git a/examples/examples-decisionmaker-sharedcontext/src/main/resources/logic/AnswerInitTask.js b/examples/examples-decisionmaker-sharedcontext/src/main/resources/logic/AnswerInitTask.js new file mode 100644 index 000000000..17861b8fe --- /dev/null +++ b/examples/examples-decisionmaker-sharedcontext/src/main/resources/logic/AnswerInitTask.js @@ -0,0 +1,47 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2023 Nordix Foundation. 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========================================================= + */ + +executor.logger.info(executor.subject.id); +executor.logger.info(executor.inFields); + +var answerAlbum = executor.getContextAlbum("AnswerAlbum"); + +answerAlbum.put("a0", executor.inFields.get("a0")); +answerAlbum.put("a1", executor.inFields.get("a1")); +answerAlbum.put("a2", executor.inFields.get("a2")); +answerAlbum.put("a3", executor.inFields.get("a3")); +answerAlbum.put("a4", executor.inFields.get("a4")); +answerAlbum.put("a5", executor.inFields.get("a5")); +answerAlbum.put("a6", executor.inFields.get("a6")); + +var lastAnswerAlbum = executor.getContextAlbum("LastAnswerAlbum"); +lastAnswerAlbum.put("lastAnswer", answerAlbum.size() - 1); + +executor.outFields.put("a0", answerAlbum.get("a0")); +executor.outFields.put("a1", answerAlbum.get("a1")); +executor.outFields.put("a2", answerAlbum.get("a2")); +executor.outFields.put("a3", answerAlbum.get("a3")); +executor.outFields.put("a4", answerAlbum.get("a4")); +executor.outFields.put("a5", answerAlbum.get("a5")); +executor.outFields.put("a6", answerAlbum.get("a6")); + +executor.logger.info(executor.outFields); + +true; diff --git a/examples/examples-decisionmaker-sharedcontext/src/main/resources/logic/DitheringAnswerTask.js b/examples/examples-decisionmaker-sharedcontext/src/main/resources/logic/DitheringAnswerTask.js new file mode 100644 index 000000000..cecbd0dd1 --- /dev/null +++ b/examples/examples-decisionmaker-sharedcontext/src/main/resources/logic/DitheringAnswerTask.js @@ -0,0 +1,38 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2023 Nordix Foundation. 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========================================================= + */ + +executor.logger.info(executor.subject.id); +executor.logger.info(executor.inFields); + +var size = executor.getContextAlbum("AnswerAlbum").size(); + +var selection = 2 + Math.floor(Math.random() * 3); + +var selectionA = "a" + selection; + +executor.logger.info(size); +executor.logger.info(selectionA); + +executor.outFields.put("decision", executor.getContextAlbum("AnswerAlbum").get(selectionA)); + +executor.logger.info(executor.outFields); + +true; + diff --git a/examples/examples-decisionmaker-sharedcontext/src/main/resources/logic/MakeDecisionStateTSL.js b/examples/examples-decisionmaker-sharedcontext/src/main/resources/logic/MakeDecisionStateTSL.js new file mode 100644 index 000000000..d1bdfd644 --- /dev/null +++ b/examples/examples-decisionmaker-sharedcontext/src/main/resources/logic/MakeDecisionStateTSL.js @@ -0,0 +1,42 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2023 Nordix Foundation. 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========================================================= + */ + +executor.logger.info(executor.subject.id); +executor.logger.info(executor.inFields); + +if (executor.inFields.get("mode").equals("random")) { + executor.subject.getTaskKey("RandomAnswerTask").copyTo(executor.selectedTask); +} +else if (executor.inFields.get("mode").equals("pessimistic")) { + executor.subject.getTaskKey("PessimisticAnswerTask").copyTo(executor.selectedTask); +} +else if (executor.inFields.get("mode").equals("optimistic")) { + executor.subject.getTaskKey("OptimisticAnswerTask").copyTo(executor.selectedTask); +} +else if (executor.inFields.get("mode").equals("dithering")) { + executor.subject.getTaskKey("DitheringAnswerTask").copyTo(executor.selectedTask); +} +else if (executor.inFields.get("mode").equals("roundrobin")) { + executor.subject.getTaskKey("RoundRobinAnswerTask").copyTo(executor.selectedTask); +} + +executor.logger.info("Answer Selected Task:" + executor.selectedTask); + +true; diff --git a/examples/examples-decisionmaker-sharedcontext/src/main/resources/logic/OptimisticAnswerTask.js b/examples/examples-decisionmaker-sharedcontext/src/main/resources/logic/OptimisticAnswerTask.js new file mode 100644 index 000000000..cd0b18bc8 --- /dev/null +++ b/examples/examples-decisionmaker-sharedcontext/src/main/resources/logic/OptimisticAnswerTask.js @@ -0,0 +1,37 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2023 Nordix Foundation. 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========================================================= + */ + +executor.logger.info(executor.subject.id); +executor.logger.info(executor.inFields); + +var size = executor.getContextAlbum("AnswerAlbum").size(); + +var selection = size - Math.floor(Math.random() * size / 2) - 1; + +var selectionA = "a" + selection; + +executor.logger.info(size); +executor.logger.info(selectionA); + +executor.outFields.put("decision", executor.getContextAlbum("AnswerAlbum").get(selectionA)); + +executor.logger.info(executor.outFields); + +true; diff --git a/examples/examples-decisionmaker-sharedcontext/src/main/resources/logic/PessimisticAnswerTask.js b/examples/examples-decisionmaker-sharedcontext/src/main/resources/logic/PessimisticAnswerTask.js new file mode 100644 index 000000000..19889f8d7 --- /dev/null +++ b/examples/examples-decisionmaker-sharedcontext/src/main/resources/logic/PessimisticAnswerTask.js @@ -0,0 +1,37 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2023 Nordix Foundation. 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========================================================= + */ + +executor.logger.info(executor.subject.id); +executor.logger.info(executor.inFields); + +var size = executor.getContextAlbum("AnswerAlbum").size(); + +var selection = Math.floor(Math.random() * size / 2); + +var selectionA = "a" + selection; + +executor.logger.info(size); +executor.logger.info(selectionA); + +executor.outFields.put("decision", executor.getContextAlbum("AnswerAlbum").get(selectionA)); + +executor.logger.info(executor.outFields); + +true; diff --git a/examples/examples-decisionmaker-sharedcontext/src/main/resources/logic/RandomAnswerTask.js b/examples/examples-decisionmaker-sharedcontext/src/main/resources/logic/RandomAnswerTask.js new file mode 100644 index 000000000..f5ed9000c --- /dev/null +++ b/examples/examples-decisionmaker-sharedcontext/src/main/resources/logic/RandomAnswerTask.js @@ -0,0 +1,37 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2023 Nordix Foundation. 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========================================================= + */ + +executor.logger.info(executor.subject.id); +executor.logger.info(executor.inFields); + +var size = executor.getContextAlbum("AnswerAlbum").size(); + +var selection = Math.floor(Math.random() * size); + +var selectionA = "a" + selection; + +executor.logger.info(size); +executor.logger.info(selectionA); + +executor.outFields.put("decision", executor.getContextAlbum("AnswerAlbum").get(selectionA)); + +executor.logger.info(executor.outFields); + +true; diff --git a/examples/examples-decisionmaker-sharedcontext/src/main/resources/logic/RoundRobinAnswerTask.js b/examples/examples-decisionmaker-sharedcontext/src/main/resources/logic/RoundRobinAnswerTask.js new file mode 100644 index 000000000..ead3decc2 --- /dev/null +++ b/examples/examples-decisionmaker-sharedcontext/src/main/resources/logic/RoundRobinAnswerTask.js @@ -0,0 +1,45 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2023 Nordix Foundation. 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========================================================= + */ + +executor.logger.info(executor.subject.id); +executor.logger.info(executor.inFields); + +var size = executor.getContextAlbum("AnswerAlbum").size(); +var lastAnswer = executor.getContextAlbum("LastAnswerAlbum").get("lastAnswer"); + +executor.logger.info(size); +executor.logger.info(lastAnswer); + +var answer = ++lastAnswer; +if (answer >= size) { + answer = 0; +} + +executor.getContextAlbum("LastAnswerAlbum").put("lastAnswer", answer) + +var selectionA = "a" + answer; + +executor.logger.info(selectionA); + +executor.outFields.put("decision", executor.getContextAlbum("AnswerAlbum").get(selectionA)); + +executor.logger.info(executor.outFields); + +true; diff --git a/examples/examples-decisionmaker-sharedcontext/src/main/resources/policy/DecisionMakerPolicyModel.apex b/examples/examples-decisionmaker-sharedcontext/src/main/resources/policy/DecisionMakerPolicyModel.apex new file mode 100644 index 000000000..3313b637e --- /dev/null +++ b/examples/examples-decisionmaker-sharedcontext/src/main/resources/policy/DecisionMakerPolicyModel.apex @@ -0,0 +1,69 @@ +#------------------------------------------------------------------------------- +# ============LICENSE_START======================================================= +# Copyright (C) 2023 Nordix Foundation. 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========================================================= +#------------------------------------------------------------------------------- + +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 + +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 + +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 + diff --git a/examples/examples-decisionmaker-sharedcontext/src/main/resources/policy/DecisionMakerPolicyModel_Decision.apex b/examples/examples-decisionmaker-sharedcontext/src/main/resources/policy/DecisionMakerPolicyModel_Decision.apex new file mode 100644 index 000000000..465d1bd28 --- /dev/null +++ b/examples/examples-decisionmaker-sharedcontext/src/main/resources/policy/DecisionMakerPolicyModel_Decision.apex @@ -0,0 +1,111 @@ +#------------------------------------------------------------------------------- +# ============LICENSE_START======================================================= +# Copyright (C) 2023 Nordix Foundation. 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========================================================= +#------------------------------------------------------------------------------- + +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=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=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 + diff --git a/examples/examples-decisionmaker-sharedcontext/src/main/resources/tosca/ToscaTemplate.json b/examples/examples-decisionmaker-sharedcontext/src/main/resources/tosca/ToscaTemplate.json new file mode 100644 index 000000000..c9e11d49b --- /dev/null +++ b/examples/examples-decisionmaker-sharedcontext/src/main/resources/tosca/ToscaTemplate.json @@ -0,0 +1,17 @@ +{ + "tosca_definitions_version": "tosca_simple_yaml_1_1_0", + "topology_template": { + "policies": [ + { + "onap.policies.native.apex.DecisionMaker": { + "type": "onap.policies.native.Apex", + "type_version": "1.0.0", + "name": "onap.policies.native.apex.DecisionMaker", + "version": "1.0.0", + "properties": { + } + } + } + ] + } +} diff --git a/examples/examples-decisionmaker-sharedcontext/src/main/resources/tosca/ToscaTemplateTwo.json b/examples/examples-decisionmaker-sharedcontext/src/main/resources/tosca/ToscaTemplateTwo.json new file mode 100644 index 000000000..0354d4eb7 --- /dev/null +++ b/examples/examples-decisionmaker-sharedcontext/src/main/resources/tosca/ToscaTemplateTwo.json @@ -0,0 +1,17 @@ +{ + "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": { + } + } + } + ] + } +} |