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