2c2388953a170ad441068abdac1e5c7fab48ef74
[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.AccessLevel;
27 import lombok.Getter;
28 import lombok.Setter;
29 import org.apache.commons.lang3.NotImplementedException;
30 import org.onap.policy.apex.context.ContextRuntimeException;
31 import org.onap.policy.apex.context.SchemaHelper;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
34 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
35 import org.onap.policy.common.utils.validation.Assertions;
36 import org.slf4j.ext.XLogger;
37 import org.slf4j.ext.XLoggerFactory;
38
39 /**
40  * This class implements the {@link SchemaHelper} functionality that is common across all implementations. Schema
41  * helpers for specific schema mechanisms specialize this class.
42  *
43  * @author Liam Fallon (liam.fallon@ericsson.com)
44  */
45 @Getter
46 public abstract class AbstractSchemaHelper implements SchemaHelper {
47     // Get a reference to the logger
48     private static final XLogger LOGGER = XLoggerFactory.getXLogger(AbstractSchemaHelper.class);
49
50     // The key of the user of this schema helper
51     private AxKey userKey = AxArtifactKey.getNullKey();
52
53     // The schema of this schema helper
54     private AxContextSchema schema = null;
55
56     // The class of objects for this schema
57     @Setter(AccessLevel.PROTECTED)
58     private Class<?> schemaClass;
59
60     /**
61      * {@inheritDoc}.
62      */
63     @Override
64     public void init(final AxKey incomingUserKey, final AxContextSchema incomingSchema) {
65         Assertions.argumentOfClassNotNull(incomingUserKey, ContextRuntimeException.class,
66                 "incomingUserKey may not be null");
67         Assertions.argumentOfClassNotNull(incomingSchema, ContextRuntimeException.class,
68                 "incomingSchema may not be null");
69
70         this.userKey = incomingUserKey;
71         this.schema = incomingSchema;
72     }
73
74     /**
75      * {@inheritDoc}.
76      */
77     @Override
78     public Object getSchemaObject() {
79         return null;
80     }
81
82     /**
83      * {@inheritDoc}.
84      */
85     @Override
86     public Object createNewInstance() {
87         if (schemaClass == null) {
88             final String returnString =
89                     userKey.getId() + ": could not create an instance, schema class for the schema is null";
90             LOGGER.warn(returnString);
91             throw new ContextRuntimeException(returnString);
92         }
93
94         try {
95             return schemaClass.getDeclaredConstructor().newInstance();
96         } catch (final Exception e) {
97             final String returnString =
98                     userKey.getId() + ": could not create an instance of class \"" + schemaClass.getName()
99                             + "\" using the default constructor \"" + schemaClass.getSimpleName() + "()\"";
100             LOGGER.warn(returnString, e);
101             throw new ContextRuntimeException(returnString, e);
102         }
103     }
104
105     /**
106      * {@inheritDoc}.
107      */
108     @Override
109     public Object createNewInstance(final String stringValue) {
110         if (schemaClass == null) {
111             final String returnString =
112                     userKey.getId() + ": could not create an instance, schema class for the schema is null";
113             LOGGER.warn(returnString);
114             throw new ContextRuntimeException(returnString);
115         }
116
117         try {
118             // Find a string constructor
119             final Constructor<?> stringConstructor = schemaClass.getConstructor(String.class);
120
121             // Invoke the constructor
122             return stringConstructor.newInstance(stringValue);
123         } catch (final Exception e) {
124             final String returnString =
125                     userKey.getId() + ": could not create an instance of class \"" + schemaClass.getName()
126                             + "\" using the string constructor \"" + schemaClass.getSimpleName() + "(String)\"";
127             LOGGER.warn(returnString, e);
128             throw new ContextRuntimeException(returnString);
129         }
130     }
131
132     /**
133      * {@inheritDoc}.
134      */
135     @Override
136     public Object createNewSubInstance(String subType) {
137         throw new NotImplementedException("sub types are not supported on this schema helper");
138     }
139 }