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