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