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