3e6d130233a22ee1fb3f19590b3048d9aaee1f88
[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.RequiredArgsConstructor;
27 import lombok.Setter;
28 import org.onap.policy.apex.context.SchemaHelper;
29 import org.onap.policy.common.utils.coder.CoderException;
30 import org.onap.policy.common.utils.coder.StandardCoder;
31
32 /**
33  * Abstract class for the execution context for logic executions in logic being executed in an Apex engine. The
34  * logic must have easy access to its subject definition, the incoming and outgoing field contexts, as well as the
35  * policy, global, and external context.
36  */
37 @Getter
38 @RequiredArgsConstructor
39 public class AbstractExecutionContext {
40     /** A constant <code>boolean true</code> value available for reuse e.g., for the return value */
41     public static final Boolean IS_TRUE = true;
42
43     /**
44      * A constant <code>boolean false</code> value available for reuse e.g., for the return value
45      */
46     public static final Boolean IS_FALSE = false;
47
48     // Standard coder for JSON converts
49     private static final StandardCoder STANDARD_CODER = new StandardCoder();
50
51     /** the execution ID for the current APEX policy execution instance. */
52     public final Long executionId;
53
54     // A message specified in the logic
55     @Setter
56     private String message;
57
58     // Execution properties for a policy execution
59     private final Properties executionProperties;
60
61     /**
62      * Get a JSON representation of an object.
63      *
64      * @param theObject the object to get a JSON representation of
65      * @return the JSON version of the object
66      * @throws CoderException on JSON coding errors
67      */
68     public String stringify2Json(final Object theObject) throws CoderException {
69         return stringify2Json(theObject, null);
70     }
71
72     /**
73      * Get a JSON representation of an object.
74      *
75      * @param theObject the object to get a JSON representation of
76      * @param schemaHelper a schema helper to use for the JSON conversion, if null, a standard conversion is done
77      * @return the JSON version of the object
78      * @throws CoderException on JSON coding errors
79      */
80     public String stringify2Json(final Object theObject, final SchemaHelper schemaHelper) throws CoderException {
81         if (schemaHelper == null) {
82             return STANDARD_CODER.encode(theObject);
83         } else {
84             return schemaHelper.marshal2String(theObject);
85         }
86     }
87 }