a47ccaa48da61f27a96fa03744ccbbbae4df03c4
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.core.engine.executor.context;
22
23 import java.util.Properties;
24 import lombok.Getter;
25 import lombok.Setter;
26 import org.onap.policy.apex.context.SchemaHelper;
27 import org.onap.policy.common.utils.coder.CoderException;
28 import org.onap.policy.common.utils.coder.StandardCoder;
29
30 /**
31  * Abstract class for the execution context for logic executions in logic being executed in an Apex engine. The
32  * logic must have easy access to its subject definition, the incoming and outgoing field contexts, as well as the
33  * policy, global, and external context.
34  */
35 @Getter
36 public class AbstractExecutionContext {
37     /** A constant <code>boolean true</code> value available for reuse e.g., for the return value */
38     public final Boolean isTrue = true;
39
40     /**
41      * A constant <code>boolean false</code> value available for reuse e.g., for the return value
42      */
43     public final Boolean isFalse = false;
44
45     /** the execution ID for the current APEX policy execution instance. */
46     public final Long executionId;
47
48     // Standard coder for JSON converts
49     private static final StandardCoder STANDARD_CODER = new StandardCoder();
50
51     // A message specified in the logic
52     @Setter
53     private String message;
54
55     // Execution properties for a policy execution
56     private final Properties executionProperties;
57
58     /**
59      * Instantiates a new task execution context.
60      *
61      * @param executionId the execution ID for the current APEX policy execution instance
62      * @param executionProperties the execution properties for task execution
63      */
64     public AbstractExecutionContext(final long executionId, final Properties executionProperties) {
65
66         // Execution ID is the current policy execution instance
67         this.executionId = executionId;
68         this.executionProperties = executionProperties;
69     }
70
71     /**
72      * Get a JSON representation of an object.
73      *
74      * @param theObject the object to get a JSON representation of
75      * @return the JSON version of the object
76      * @throws CoderException on JSON coding errors
77      */
78     public String stringify2Json(final Object theObject) throws CoderException {
79         return stringify2Json(theObject, null);
80     }
81
82     /**
83      * Get a JSON representation of an object.
84      *
85      * @param theObject the object to get a JSON representation of
86      * @param schemaHelper a schema helper to use for the JSON conversion, if null, a standard conversion is done
87      * @return the JSON version of the object
88      * @throws CoderException on JSON coding errors
89      */
90     public String stringify2Json(final Object theObject, final SchemaHelper schemaHelper) throws CoderException {
91         if (schemaHelper == null) {
92             return STANDARD_CODER.encode(theObject);
93         } else {
94             return schemaHelper.marshal2String(theObject);
95         }
96     }
97 }