APEX offers a lot of flexibility for defining, deploying,
and executing policies. Based on a theoretic model, it
- supports virtually any policy model and allows to
- translate legacy policies into the APEX execution format.
+ supports virtually any policy model and supports
+ translation of legacy policies into the APEX execution format.
However, the most important aspect for using APEX is to
decide what policy is needed, what underlying policy
concepts should be used, and how the decision logic
values in the configuration information passed to APEX
engines.
+ .. container:: paragraph
+
+ The *taskParameters* field is specified under *engineParameters*
+ in the ApexConfig. It can contain one or more task parameters, where each
+ item can contain the parameter key, value as well as the taskId to which it is associated.
+ If the taskId is not specified, then the parameters are added to all tasks.
+
Concept: Logic
##############
attempt to teach you about the scripting languages
themselves … that is up to you!
- .. tip::
- JVM-based scripting languages
- For more more information on scripting for the Java platform see: https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/prog_guide/index.html
+ .. tip::
+ JVM-based scripting languages
+ For more more information on scripting for the Java platform see: https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/prog_guide/index.html
- .. note::
- What do Tasks do?
- The function of an Apex Task is to provide the logic that can be executed for an Apex State as one of the steps in
- an Apex Policy. Each task receives some *incoming fields*, executes some logic (e.g: make a decision based on
- *shared state* or *context*, *incoming fields*, *external context*, etc.), perhaps set some *shared state* or
- *context* and then emits *outgoing fields*. The state that uses the task is responsible for extracting the
- *incoming fields* from the state input event. The state also has an *output mapper* associated with the task, and
- this *output mapper* is responsible for mapping the *outgoing fields* from the task into an appropriate
- output event for the state.
+ .. note::
+ What do Tasks do?
+ The function of an Apex Task is to provide the logic that can be executed for an Apex State as one of the steps in
+ an Apex Policy. Each task receives some *incoming fields*, executes some logic (e.g: make a decision based on
+ *shared state* or *context*, *incoming fields*, *external context*, etc.), perhaps set some *shared state* or
+ *context* and then emits *outgoing fields*. The state that uses the task is responsible for extracting the
+ *incoming fields* from the state input event. The state also has an *output mapper* associated with the task, and
+ this *output mapper* is responsible for mapping the *outgoing fields* from the task into an appropriate
+ output event for the state.
.. container:: paragraph
.. container:: content
- .. code:: javascript
+ .. code:: javascript
:number-lines:
/*
and exit.
.. note::
- How to return a value from task logic
- Some languages explicitly support returning values from the script (e.g. MVEL and JRuby) using an explicit
- return statement (e.g. ``return true``), other languages do not (e.g. JavaScript and Jython). For
- languages that do not support the ``return`` statement, a special field called ``returnValue`` must be
- created to hold the result of the task logic operation (i.e. assign a ``java.lang.Boolean``
- value to the ``returnValue`` field before completing the task).
- Also, in MVEL if there is no explicit return statement then the return value of the last executed statement will return
- (e.g. the statement a=(1+2) will return the value 3).
+ How to return a value from task logic
+ Some languages explicitly support returning values from the script (e.g. MVEL and JRuby) using an explicit
+ return statement (e.g. ``return true``), other languages do not (e.g. JavaScript and Jython). For
+ languages that do not support the ``return`` statement, a special field called ``returnValue`` must be
+ created to hold the result of the task logic operation (i.e. assign a ``java.lang.Boolean``
+ value to the ``returnValue`` field before completing the task).
+ Also, in MVEL if there is no explicit return statement then the return value of the last executed statement will return
+ (e.g. the statement a=(1+2) will return the value 3).
.. container:: paragraph
``executor`` keyword in MVEL):
Table 1. The ``executor`` Fields / Methods
-
+
+------------+-------------+--------------------------------+-------------------------------------------------------------------------------------+
| Name | Type | Java type | Description |
+============+=============+================================+=====================================================================================+
the scripting languages themselves … that is up to you!
.. tip::
- JVM-based scripting languages
- For more more information on Scripting for the Java platform see:
- https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/prog_guide/index.html
-
- .. note::
- What does Task Selection Logic do?
- When an Apex state references multiple tasks, there must be a way to dynamically decide
- which task should be chosen and executed. This can depend on the many factors, e.g. the
- *incoming event for the state*, *shared state* or *context*, *external context*,
- etc.. This is the function of a state’s Task Selection Logic. Obviously, if there is
- only one task then Task only one task then Task Selection Logic is not needed.
- Each state must also select one of the tasks a the *default state*. If the Task
- Selection Logic is unable to select an appropriate task, then it should select the
- *default task*. Once the task has been selected the Apex Engine will then execute that
- task.
+ JVM-based scripting languages
+ For more more information on Scripting for the Java platform see:
+ https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/prog_guide/index.html
+
+ .. note::
+ What does Task Selection Logic do?
+ When an Apex state references multiple tasks, there must be a way to dynamically decide
+ which task should be chosen and executed. This can depend on the many factors, e.g. the
+ *incoming event for the state*, *shared state* or *context*, *external context*,
+ etc.. This is the function of a state’s Task Selection Logic. Obviously, if there is
+ only one task then Task only one task then Task Selection Logic is not needed.
+ Each state must also select one of the tasks a the *default state*. If the Task
+ Selection Logic is unable to select an appropriate task, then it should select the
+ *default task*. Once the task has been selected the Apex Engine will then execute that
+ task.
.. container:: paragraph
be returned, but this will cause the policy invoking this task
will fail and exit.
- .. note::
+ .. note::
How to return a value from Task Selection Logic
- Some languages explicitly support returning values from the script (e.g. MVEL and
- JRuby) using an explicit return statement (e.g. ``return true``), other languages do not (e.g.
- JavaScript and Jython). For languages that do not support the ``return`` statement, a special field called
- ``returnValue`` must be created to hold the result of the task logic operation (i.e. assign a ``java.lang.Boolean``
- value to the ``returnValue`` field before completing the task).
- Also, in MVEL if there is not explicit return statement then the return value of the last executed statement will
- return (e.g. the statement a=(1+2) will return the value 3).
+ Some languages explicitly support returning values from the script (e.g. MVEL and
+ JRuby) using an explicit return statement (e.g. ``return true``), other languages do not (e.g.
+ JavaScript and Jython). For languages that do not support the ``return`` statement, a special field called
+ ``returnValue`` must be created to hold the result of the task logic operation (i.e. assign a ``java.lang.Boolean``
+ value to the ``returnValue`` field before completing the task).
+ Also, in MVEL if there is not explicit return statement then the return value of the last executed statement will
+ return (e.g. the statement a=(1+2) will return the value 3).
.. container:: paragraph
| :number-lines: | :number-lines: |
| | |
| >c: | # cd /usr/local/src/apex-pdp |
- | >cd \dev\apex | # mvn clean install -DskipTest |
+ | >cd \dev\apex | # mvn clean install -DskipTests |
| >mvn clean install -DskipTests | |
+-------------------------------------------------------+--------------------------------------------------------+
| | | | .getOutFieldSchemaHelper("authorised") |
| | | | .createNewInstance("false"); |
+------------+-------------+--------------------------------+-------------------------------------------------------------------------------------+
+| parameters | Fields | java.util.Map <String,String> | .. container:: paragraph |
+| | | | |
+| | | | All parameters in the current task. This is implemented as a standard Java Map. |
+| | | | |
+| | | | .. container:: |
+| | | | |
+| | | | .. container:: content |
+| | | | |
+| | | | .. container:: paragraph |
+| | | | |
+| | | | **Example:** |
+| | | | |
+| | | | .. code:: javascript |
+| | | | |
+| | | | executor.parameters.get("ParameterKey1")) |
++------------+-------------+--------------------------------+-------------------------------------------------------------------------------------+
| ContextAlbum getContextAlbum(String ctxtAlbumName ) | .. container:: paragraph |
| | |
| | A utility method to retrieve a ``ContextAlbum`` for use in the task. |
Examples given here use Javascript (if not stated otherwise),
other execution environments will be similar.
-Add Nashorn
-###########
-
- .. container:: paragraph
-
- First line in the logic use this import.
-
- .. container:: listingblock
-
- .. container:: title
-
- JS Nashorn
-
- .. container:: content
-
- .. code:: javascript
-
- load("nashorn:mozilla_compat.js");
-
Finish Logic with Success or Error
##################################
.. container:: paragraph
To finish logic, i.e. return to APEX, with success use the
- following lines close to the end of the logic.
+ following line close to the end of the logic.
.. container:: listingblock
.. code:: javascript
- var returnValueType = Java.type("java.lang.Boolean");
- var returnValue = new returnValueType(true);
+ true;
.. container:: paragraph
.. code:: javascript
- var returnValueType = Java.type("java.lang.Boolean");
- var returnValue = new returnValueType(false);
+ false;
Logic Logging
#############
rootLogger.error("Serious error in logic detected: " + executor.subject.id);
+Accessing TaskParameters
+########################
+
+ .. container:: paragraph
+
+ TaskParameters available in a Task can be accessed in the logic.
+ The parameters in each task are made available at the executor level.
+ This example assumes a parameter with key ``ParameterKey1``.
+
+ .. container:: listingblock
+
+ .. container:: title
+
+ JS TaskParameter value
+
+ .. container:: content
+
+ .. code:: javascript
+
+ executor.parameters.get("ParameterKey1"))
+
+ .. container:: paragraph
+
+ Alternatively, the task parameters can also be accessed from the task object.
+
+ .. container:: listingblock
+
+ .. container:: title
+
+ JS TaskParameter value using task object
+
+ .. container:: content
+
+ .. code:: javascript
+
+ executor.subject.task.getTaskParameters.get("ParameterKey1").getTaskParameterValue()
+
Local Variable for Infields
###########################
.. container:: content
- .. code:: javascript
+ .. code:: javascript
var albumProblemMap = executor.getContextAlbum("albumProblemMap");
var linkProblem = albumProblemMap.getSchemaHelper().createNewInstance();
.. container:: content
- .. code:: javascript
+ .. code:: javascript
var linkProblem = executor.getContextAlbum("albumProblemMap").getSchemaHelper().createNewInstance();
Policy Examples
^^^^^^^^^^^^^^^
+Sample APEX Policy in TOSCA format
+----------------------------------
+
+ .. container:: paragraph
+
+ An example APEX policy in TOSCA format for the vCPE
+ use case can be found here:
+
+ .. container:: ulist
+
+ - `APEX TOSCA Policy
+ vCPE <https://github.com/onap/policy-models/blob/master/models-examples/src/main/resources/policies/vCPE.apex.policy.operational.input.tosca.json>`__
+
My First Policy
---------------
.. container::
:name: footer-text
- 2.0.0-SNAPSHOT
- Last updated 2018-09-04 16:04:24 IST
+ 2.3.0-SNAPSHOT
+ Last updated 2020-03-16 16:04:24 GMT
.. |APEX Policy Matrix| image:: images/apex-intro/ApexPolicyMatrix.png
.. |APEX Policy Model for Execution| image:: images/apex-policy-model/UmlPolicyModels.png