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