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