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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.context.impl.schema;
24 import org.onap.policy.apex.context.ContextRuntimeException;
25 import org.onap.policy.apex.context.SchemaHelper;
26 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
27 import org.onap.policy.apex.context.parameters.SchemaHelperParameters;
28 import org.onap.policy.apex.context.parameters.SchemaParameters;
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.basicmodel.service.ModelService;
32 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
33 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
34 import org.onap.policy.common.parameters.ParameterService;
35 import org.onap.policy.common.utils.validation.Assertions;
36 import org.slf4j.ext.XLogger;
37 import org.slf4j.ext.XLoggerFactory;
40 * This class returns a {@link SchemaHelper} for the particular type of schema mechanism configured for use.
42 * @author Liam Fallon (liam.fallon@ericsson.com)
44 public class SchemaHelperFactory {
45 // Get a reference to the logger
46 private static final XLogger LOGGER = XLoggerFactory.getXLogger(SchemaHelperFactory.class);
49 * Return a {@link SchemaHelper} for the particular type of schema mechanism configured for use.
51 * @param owningEntityKey The key of the entity that owns the schema helper
52 * @param schemaKey The key of the schema the schema helper is operating on
53 * @return a lock schema that can handle translation of objects in a particular schema format
54 * @throws ContextRuntimeException the context runtime exception
56 public SchemaHelper createSchemaHelper(final AxKey owningEntityKey, final AxArtifactKey schemaKey) {
57 LOGGER.entry("schema helper factory, owningEntityKey=" + owningEntityKey);
58 Assertions.argumentOfClassNotNull(owningEntityKey, ContextRuntimeException.class,
59 "Parameter \"owningEntityKey\" may not be null");
60 Assertions.argumentOfClassNotNull(schemaKey, ContextRuntimeException.class,
61 "Parameter \"schemaKey\" may not be null");
63 // Get the schema for items in the album
64 final AxContextSchema schema = ModelService.getModel(AxContextSchemas.class).get(schemaKey);
66 final String resultString =
67 "schema \"" + schemaKey.getId() + "\" for entity " + owningEntityKey.getId() + " does not exist";
68 LOGGER.warn(resultString);
69 throw new ContextRuntimeException(resultString);
72 // Get the schema class using the parameter service
73 final SchemaParameters schemaParameters = ParameterService.get(ContextParameterConstants.SCHEMA_GROUP_NAME);
75 // Get the class for the schema helper from the schema parameters
76 final SchemaHelperParameters schemaHelperParameters =
77 schemaParameters.getSchemaHelperParameters(schema.getSchemaFlavour());
78 if (schemaHelperParameters == null) {
79 final String resultString = "context schema helper parameters not found for context schema \""
80 + schema.getSchemaFlavour() + "\"";
81 LOGGER.warn(resultString);
82 throw new ContextRuntimeException(resultString);
85 // Get the class for the schema helper using reflection
86 Object schemaHelperObject = null;
87 final String pluginClass = schemaHelperParameters.getSchemaHelperPluginClass();
89 schemaHelperObject = Class.forName(pluginClass).getDeclaredConstructor().newInstance();
90 } catch (final Exception e) {
91 final String resultString = "Apex context schema helper class not found for context schema helper plugin \""
93 LOGGER.warn(resultString, e);
94 throw new ContextRuntimeException(resultString, e);
97 // Check the class is a schema helper
98 if (!(schemaHelperObject instanceof SchemaHelper)) {
99 final String resultString = "Specified Apex context schema helper plugin class \"" + pluginClass
100 + "\" does not implement the SchemaHelper interface";
101 LOGGER.warn(resultString);
102 throw new ContextRuntimeException(resultString);
105 // The context schema helper to return
106 final SchemaHelper schemaHelper = (SchemaHelper) schemaHelperObject;
108 // Lock and load the schema helper
109 schemaHelper.init(owningEntityKey.getKey(), schema);
111 LOGGER.exit("Schema Helper factory, owningEntityKey=" + owningEntityKey + ", selected schema helper of class "
112 + schemaHelper.getClass());