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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.context.impl.schema;
23 import org.onap.policy.apex.context.ContextRuntimeException;
24 import org.onap.policy.apex.context.SchemaHelper;
25 import org.onap.policy.apex.context.parameters.SchemaHelperParameters;
26 import org.onap.policy.apex.context.parameters.SchemaParameters;
27 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
28 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
29 import org.onap.policy.apex.model.basicmodel.service.ModelService;
30 import org.onap.policy.apex.model.basicmodel.service.ParameterService;
31 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
32 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
33 import org.onap.policy.apex.model.utilities.Assertions;
34 import org.slf4j.ext.XLogger;
35 import org.slf4j.ext.XLoggerFactory;
38 * This class returns a {@link SchemaHelper} for the particular type of schema mechanism configured for use.
40 * @author Liam Fallon (liam.fallon@ericsson.com)
42 public class SchemaHelperFactory {
43 // Get a reference to the logger
44 private static final XLogger LOGGER = XLoggerFactory.getXLogger(SchemaHelperFactory.class);
47 * Return a {@link SchemaHelper} for the particular type of schema mechanism configured for use.
49 * @param owningEntityKey The key of the entity that owns the schema helper
50 * @param schemaKey The key of the schema the schema helper is operating on
51 * @return a lock schema that can handle translation of objects in a particular schema format
52 * @throws ContextRuntimeException the context runtime exception
54 public SchemaHelper createSchemaHelper(final AxKey owningEntityKey, final AxArtifactKey schemaKey)
55 throws ContextRuntimeException {
56 LOGGER.entry("schema helper factory, owningEntityKey=" + owningEntityKey);
57 Assertions.argumentNotNull(owningEntityKey, ContextRuntimeException.class,
58 "Parameter \"owningEntityKey\" may not be null");
59 Assertions.argumentNotNull(schemaKey, ContextRuntimeException.class, "Parameter \"schemaKey\" may not be null");
61 // Get the schema for items in the album
62 final AxContextSchema schema = ModelService.getModel(AxContextSchemas.class).get(schemaKey);
64 final String resultString =
65 "schema \"" + schemaKey.getID() + "\" for entity " + owningEntityKey.getID() + " does not exist";
66 LOGGER.warn(resultString);
67 throw new ContextRuntimeException(resultString);
70 // Get the schema class using the parameter service
71 final SchemaParameters schemaParameters = ParameterService.getParameters(SchemaParameters.class);
72 if (schemaParameters == null) {
73 final String resultString = "context schema parameters \"" + SchemaParameters.class.getCanonicalName()
74 + "\" not found in parameter service";
75 LOGGER.warn(resultString);
76 throw new ContextRuntimeException(resultString);
79 // Get the class for the schema helper from the schema parameters
80 final SchemaHelperParameters schemaHelperParameters =
81 schemaParameters.getSchemaHelperParameters(schema.getSchemaFlavour());
82 if (schemaHelperParameters == null) {
83 final String resultString = "context schema helper parameters not found for context schema \""
84 + schema.getSchemaFlavour() + "\"";
85 LOGGER.warn(resultString);
86 throw new ContextRuntimeException(resultString);
89 // Get the class for the schema helper using reflection
90 Object schemaHelperObject = null;
91 final String pluginClass = schemaHelperParameters.getSchemaHelperPluginClass();
93 schemaHelperObject = Class.forName(pluginClass).newInstance();
94 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
95 final String resultString = "Apex context schema helper class not found for context schema helper plugin \""
97 LOGGER.warn(resultString, e);
98 throw new ContextRuntimeException(resultString, e);
101 // Check the class is a schema helper
102 if (!(schemaHelperObject instanceof SchemaHelper)) {
103 final String resultString = "Specified Apex context schema helper plugin class \"" + pluginClass
104 + "\" does not implement the SchemaHelper interface";
105 LOGGER.warn(resultString);
106 throw new ContextRuntimeException(resultString);
109 // The context schema helper to return
110 final SchemaHelper schemaHelper = (SchemaHelper) schemaHelperObject;
112 // Lock and load the schema helper
113 schemaHelper.init(owningEntityKey.getKey(), schema);
115 LOGGER.exit("Schema Helper factory, owningEntityKey=" + owningEntityKey + ", selected schema helper of class "
116 + schemaHelper.getClass());