e7d4643fd0a959d90f024572b204f9a36e0e05bb
[policy/apex-pdp.git] / core / core-engine / src / main / java / org / onap / policy / apex / core / engine / event / EnField.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
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.event;
22
23 import java.io.Serializable;
24
25 import org.onap.policy.apex.context.ContextRuntimeException;
26 import org.onap.policy.apex.context.SchemaHelper;
27 import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
28 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
29 import org.onap.policy.apex.model.eventmodel.concepts.AxField;
30 import org.slf4j.ext.XLogger;
31 import org.slf4j.ext.XLoggerFactory;
32
33 /**
34  * Instances of the Class EnField are event fields being passed through the Apex system.
35  *
36  * @author Liam Fallon (liam.fallon@ericsson.com)
37  */
38 public class EnField implements Serializable {
39     private static final long serialVersionUID = -5713525780081840333L;
40
41     // Logger for this class
42     private static final XLogger LOGGER = XLoggerFactory.getXLogger(EnField.class);
43
44     // The definition of this field in the Apex model
45     private final AxField axField;
46
47     // The schema helper for this field
48     private transient SchemaHelper schemaHelper;
49
50     // The value of this field
51     private final transient Object value;
52
53     /**
54      * Instantiates a new EnField, an Engine Field.
55      *
56      * @param axField the field definition from the Apex model
57      * @param value the value
58      */
59     public EnField(final AxField axField, final Object value) {
60         // Save the field definition from the Apex model
61         this.axField = axField;
62         this.value = value;
63
64         // Get a schema helper to handle translations of fields to and from the schema
65         try {
66             schemaHelper = new SchemaHelperFactory().createSchemaHelper(axField.getKey(), axField.getSchema());
67         } catch (final ContextRuntimeException e) {
68             final String message = "schema helper cannot be created for parameter with key \"" + axField.getId()
69                     + "\" with schema \"" + axField.getSchema() + "\"";
70             LOGGER.warn(message, e);
71             throw new EnException(message, e);
72         }
73     }
74
75     /**
76      * Gets the field definition of this field.
77      *
78      * @return the field definition
79      */
80     public AxField getAxField() {
81         return axField;
82     }
83
84     /**
85      * Gets the schema helper of this field.
86      *
87      * @return the schema helper for this field
88      */
89     public SchemaHelper getSchemaHelper() {
90         return schemaHelper;
91     }
92
93     /**
94      * Get the name of the field.
95      *
96      * @return the field name
97      */
98     public String getName() {
99         return axField.getKey().getLocalName();
100     }
101
102     /**
103      * Get the key of the field.
104      *
105      * @return the field key
106      */
107     public AxReferenceKey getKey() {
108         return axField.getKey();
109     }
110
111     /**
112      * Get the value of the field.
113      *
114      * @return the value
115      */
116     public Object getValue() {
117         return value;
118     }
119
120     /**
121      * {@inheritDoc}.
122      */
123     @Override
124     public String toString() {
125         return "EnField [axField=" + axField + ", value=" + value + "]";
126     }
127
128     /**
129      * Get an assignable object that will work with the field.
130      *
131      * @return the assignable value
132      */
133     public Object getAssignableValue() {
134         // Use the schema helper to get the translated value of the object
135         return schemaHelper.unmarshal(value);
136     }
137
138     /**
139      * Is the value object assignable to this field.
140      *
141      * @return true if the value is assignable
142      */
143     public boolean isAssignableValue() {
144         try {
145             schemaHelper.unmarshal(value);
146             return true;
147         } catch (final Exception e) {
148             if (LOGGER.isTraceEnabled()) {
149                 LOGGER.trace("value {} is not assignable to this field", value, e);
150             }
151             return false;
152         }
153     }
154 }