83d1b6ba51c2700dc988d1494667fc32b494c2e5
[policy/apex-pdp.git] /
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.context.impl.schema;
22
23 import java.lang.reflect.Constructor;
24
25 import org.onap.policy.apex.context.ContextRuntimeException;
26 import org.onap.policy.apex.context.SchemaHelper;
27 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
28 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
29 import org.onap.policy.apex.model.utilities.Assertions;
30 import org.slf4j.ext.XLogger;
31 import org.slf4j.ext.XLoggerFactory;
32
33 /**
34  * This class implements the {@link SchemaHelper} functionality that is common across all implementations. Schema
35  * helpers for specific schema mechanisms specialize this class.
36  *
37  * @author Liam Fallon (liam.fallon@ericsson.com)
38  */
39 public abstract class AbstractSchemaHelper implements SchemaHelper {
40     // Get a reference to the logger
41     private static final XLogger LOGGER = XLoggerFactory.getXLogger(AbstractSchemaHelper.class);
42
43     // The key of the user of this schema helper
44     private AxKey userKey = null;
45
46     // The schema of this schema helper
47     private AxContextSchema schema = null;
48
49     // The class of objects for this schema
50     private Class<?> schemaClass;
51
52     /**
53      * Sets the schema class for the schema, designed jots to be called by sub classes.
54      *
55      * @param schemaClass the Java class that is used to hold items of this schema
56      */
57     protected void setSchemaClass(final Class<?> schemaClass) {
58         this.schemaClass = schemaClass;
59     }
60
61     /*
62      * (non-Javadoc)
63      *
64      * @see org.onap.policy.apex.context.SchemaHelper#init(org.onap.policy.apex.model.basicmodel.concepts.AxKey,
65      * org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema)
66      */
67     @Override
68     public void init(final AxKey incomingUserKey, final AxContextSchema incomingSchema) throws ContextRuntimeException {
69         Assertions.argumentNotNull(incomingUserKey, ContextRuntimeException.class, "incomingUserKey may not be null");
70         Assertions.argumentNotNull(incomingSchema, ContextRuntimeException.class, "incomingSchema may not be null");
71
72         this.userKey = incomingUserKey;
73         this.schema = incomingSchema;
74     }
75
76     /*
77      * (non-Javadoc)
78      *
79      * @see org.onap.policy.apex.context.SchemaHelper#getKey()
80      */
81     @Override
82     public AxKey getUserKey() {
83         return userKey;
84     }
85
86     /*
87      * (non-Javadoc)
88      *
89      * @see org.onap.policy.apex.context.SchemaHelper#getSchema()
90      */
91     @Override
92     public AxContextSchema getSchema() {
93         return schema;
94     }
95
96     /*
97      * (non-Javadoc)
98      *
99      * @see org.onap.policy.apex.context.SchemaHelper#getSchemaClass()
100      */
101     @Override
102     public Class<?> getSchemaClass() {
103         return schemaClass;
104     }
105
106     /*
107      * (non-Javadoc)
108      *
109      * @see org.onap.policy.apex.context.SchemaHelper#getSchemaObject()
110      */
111     @Override
112     public Object getSchemaObject() {
113         return null;
114     }
115
116     /*
117      * (non-Javadoc)
118      *
119      * @see org.onap.policy.apex.context.SchemaHelper#createNewInstance()
120      */
121     @Override
122     public Object createNewInstance() {
123         if (schemaClass == null) {
124             final String returnString =
125                     userKey.getID() + ": could not create an instance, schema class for the schema is null";
126             LOGGER.warn(returnString);
127             throw new ContextRuntimeException(returnString);
128         }
129
130         try {
131             return schemaClass.newInstance();
132         } catch (final Exception e) {
133             final String returnString =
134                     userKey.getID() + ": could not create an instance of class \"" + schemaClass.getCanonicalName()
135                             + "\" using the default constructor \"" + schemaClass.getSimpleName() + "()\"";
136             LOGGER.warn(returnString, e);
137             throw new ContextRuntimeException(returnString, e);
138         }
139     }
140
141     /*
142      * (non-Javadoc)
143      *
144      * @see org.onap.policy.apex.context.SchemaHelper#createNewInstance(java.lang.String)
145      */
146     @Override
147     public Object createNewInstance(final String stringValue) {
148         if (schemaClass == null) {
149             final String returnString =
150                     userKey.getID() + ": could not create an instance, schema class for the schema is null";
151             LOGGER.warn(returnString);
152             throw new ContextRuntimeException(returnString);
153         }
154
155         try {
156             // Find a string constructor
157             final Constructor<?> stringConstructor = schemaClass.getConstructor(String.class);
158
159             // Invoke the constructor
160             return stringConstructor.newInstance(stringValue);
161         } catch (final Exception e) {
162             final String returnString =
163                     userKey.getID() + ": could not create an instance of class \"" + schemaClass.getCanonicalName()
164                             + "\" using the string constructor \"" + schemaClass.getSimpleName() + "(String)\"";
165             LOGGER.warn(returnString, e);
166             throw new ContextRuntimeException(returnString);
167         }
168     }
169 }