260482c61d4440e554d21338929400e65e9f6b3d
[policy/apex-pdp.git] / context / context-management / src / main / java / org / onap / policy / apex / context / impl / schema / AbstractSchemaHelper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-2020 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      * {@inheritDoc}.
66      */
67     @Override
68     public void init(final AxKey incomingUserKey, final AxContextSchema incomingSchema) {
69         Assertions.argumentOfClassNotNull(incomingUserKey, ContextRuntimeException.class,
70                 "incomingUserKey may not be null");
71         Assertions.argumentOfClassNotNull(incomingSchema, ContextRuntimeException.class,
72                 "incomingSchema may not be null");
73
74         this.userKey = incomingUserKey;
75         this.schema = incomingSchema;
76     }
77
78     /**
79      * {@inheritDoc}.
80      */
81     @Override
82     public AxKey getUserKey() {
83         return userKey;
84     }
85
86     /**
87      * {@inheritDoc}.
88      */
89     @Override
90     public AxContextSchema getSchema() {
91         return schema;
92     }
93
94     /**
95      * {@inheritDoc}.
96      */
97     @Override
98     public Class<?> getSchemaClass() {
99         return schemaClass;
100     }
101
102     /**
103      * {@inheritDoc}.
104      */
105     @Override
106     public Object getSchemaObject() {
107         return null;
108     }
109
110     /**
111      * {@inheritDoc}.
112      */
113     @Override
114     public Object createNewInstance() {
115         if (schemaClass == null) {
116             final String returnString =
117                     userKey.getId() + ": could not create an instance, schema class for the schema is null";
118             LOGGER.warn(returnString);
119             throw new ContextRuntimeException(returnString);
120         }
121
122         try {
123             return schemaClass.getDeclaredConstructor().newInstance();
124         } catch (final Exception e) {
125             final String returnString =
126                     userKey.getId() + ": could not create an instance of class \"" + schemaClass.getName()
127                             + "\" using the default constructor \"" + schemaClass.getSimpleName() + "()\"";
128             LOGGER.warn(returnString, e);
129             throw new ContextRuntimeException(returnString, e);
130         }
131     }
132
133     /**
134      * {@inheritDoc}.
135      */
136     @Override
137     public Object createNewInstance(final String stringValue) {
138         if (schemaClass == null) {
139             final String returnString =
140                     userKey.getId() + ": could not create an instance, schema class for the schema is null";
141             LOGGER.warn(returnString);
142             throw new ContextRuntimeException(returnString);
143         }
144
145         try {
146             // Find a string constructor
147             final Constructor<?> stringConstructor = schemaClass.getConstructor(String.class);
148
149             // Invoke the constructor
150             return stringConstructor.newInstance(stringValue);
151         } catch (final Exception e) {
152             final String returnString =
153                     userKey.getId() + ": could not create an instance of class \"" + schemaClass.getName()
154                             + "\" using the string constructor \"" + schemaClass.getSimpleName() + "(String)\"";
155             LOGGER.warn(returnString, e);
156             throw new ContextRuntimeException(returnString);
157         }
158     }
159
160     /**
161      * {@inheritDoc}.
162      */
163     @Override
164     public Object createNewSubInstance(String subType) {
165         throw new NotImplementedException("sub types are not supported on this schema helper");
166     }
167 }