d94a59d38169ebbe1955a6aeb5da6b37ac9db9a5
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
5  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.context.impl.schema;
24
25 import java.lang.reflect.Constructor;
26 import lombok.Getter;
27 import org.apache.commons.lang3.NotImplementedException;
28 import org.onap.policy.apex.context.ContextRuntimeException;
29 import org.onap.policy.apex.context.SchemaHelper;
30 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
32 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
33 import org.onap.policy.common.utils.validation.Assertions;
34 import org.slf4j.ext.XLogger;
35 import org.slf4j.ext.XLoggerFactory;
36
37 /**
38  * This class implements the {@link SchemaHelper} functionality that is common across all implementations. Schema
39  * helpers for specific schema mechanisms specialize this class.
40  *
41  * @author Liam Fallon (liam.fallon@ericsson.com)
42  */
43 @Getter
44 public abstract class AbstractSchemaHelper implements SchemaHelper {
45     // Get a reference to the logger
46     private static final XLogger LOGGER = XLoggerFactory.getXLogger(AbstractSchemaHelper.class);
47
48     // The key of the user of this schema helper
49     private AxKey userKey = AxArtifactKey.getNullKey();
50
51     // The schema of this schema helper
52     private AxContextSchema schema = null;
53
54     // The class of objects for this schema
55     private Class<?> schemaClass;
56
57     /**
58      * Sets the schema class for the schema, designed jots to be called by sub classes.
59      *
60      * @param schemaClass the Java class that is used to hold items of this schema
61      */
62     protected void setSchemaClass(final Class<?> schemaClass) {
63         this.schemaClass = schemaClass;
64     }
65
66     /**
67      * {@inheritDoc}.
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      * {@inheritDoc}.
82      */
83     @Override
84     public Object getSchemaObject() {
85         return null;
86     }
87
88     /**
89      * {@inheritDoc}.
90      */
91     @Override
92     public Object createNewInstance() {
93         if (schemaClass == null) {
94             final String returnString =
95                     userKey.getId() + ": could not create an instance, schema class for the schema is null";
96             LOGGER.warn(returnString);
97             throw new ContextRuntimeException(returnString);
98         }
99
100         try {
101             return schemaClass.getDeclaredConstructor().newInstance();
102         } catch (final Exception e) {
103             final String returnString =
104                     userKey.getId() + ": could not create an instance of class \"" + schemaClass.getName()
105                             + "\" using the default constructor \"" + schemaClass.getSimpleName() + "()\"";
106             LOGGER.warn(returnString, e);
107             throw new ContextRuntimeException(returnString, e);
108         }
109     }
110
111     /**
112      * {@inheritDoc}.
113      */
114     @Override
115     public Object createNewInstance(final String stringValue) {
116         if (schemaClass == null) {
117             final String returnString =
118                     userKey.getId() + ": could not create an instance, schema class for the schema is null";
119             LOGGER.warn(returnString);
120             throw new ContextRuntimeException(returnString);
121         }
122
123         try {
124             // Find a string constructor
125             final Constructor<?> stringConstructor = schemaClass.getConstructor(String.class);
126
127             // Invoke the constructor
128             return stringConstructor.newInstance(stringValue);
129         } catch (final Exception e) {
130             final String returnString =
131                     userKey.getId() + ": could not create an instance of class \"" + schemaClass.getName()
132                             + "\" using the string constructor \"" + schemaClass.getSimpleName() + "(String)\"";
133             LOGGER.warn(returnString, e);
134             throw new ContextRuntimeException(returnString);
135         }
136     }
137
138     /**
139      * {@inheritDoc}.
140      */
141     @Override
142     public Object createNewSubInstance(String subType) {
143         throw new NotImplementedException("sub types are not supported on this schema helper");
144     }
145 }