54689e3fbb5196201b5a94458717f9458bab96fb
[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.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;
36
37 /**
38  * This class returns a {@link SchemaHelper} for the particular type of schema mechanism configured for use.
39  *
40  * @author Liam Fallon (liam.fallon@ericsson.com)
41  */
42 public class SchemaHelperFactory {
43     // Get a reference to the logger
44     private static final XLogger LOGGER = XLoggerFactory.getXLogger(SchemaHelperFactory.class);
45
46     /**
47      * Return a {@link SchemaHelper} for the particular type of schema mechanism configured for use.
48      *
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
53      */
54     public SchemaHelper createSchemaHelper(final AxKey owningEntityKey, final AxArtifactKey schemaKey) {
55         LOGGER.entry("schema helper factory, owningEntityKey=" + owningEntityKey);
56         Assertions.argumentNotNull(owningEntityKey, ContextRuntimeException.class,
57                 "Parameter \"owningEntityKey\" may not be null");
58         Assertions.argumentNotNull(schemaKey, ContextRuntimeException.class, "Parameter \"schemaKey\" may not be null");
59
60         // Get the schema for items in the album
61         final AxContextSchema schema = ModelService.getModel(AxContextSchemas.class).get(schemaKey);
62         if (schema == null) {
63             final String resultString =
64                     "schema \"" + schemaKey.getID() + "\" for entity " + owningEntityKey.getID() + " does not exist";
65             LOGGER.warn(resultString);
66             throw new ContextRuntimeException(resultString);
67         }
68
69         // Get the schema class using the parameter service
70         final SchemaParameters schemaParameters = ParameterService.getParameters(SchemaParameters.class);
71
72         // Get the class for the schema helper from the schema parameters
73         final SchemaHelperParameters schemaHelperParameters =
74                 schemaParameters.getSchemaHelperParameters(schema.getSchemaFlavour());
75         if (schemaHelperParameters == null) {
76             final String resultString = "context schema helper parameters not found for context schema  \""
77                     + schema.getSchemaFlavour() + "\"";
78             LOGGER.warn(resultString);
79             throw new ContextRuntimeException(resultString);
80         }
81
82         // Get the class for the schema helper using reflection
83         Object schemaHelperObject = null;
84         final String pluginClass = schemaHelperParameters.getSchemaHelperPluginClass();
85         try {
86             schemaHelperObject = Class.forName(pluginClass).newInstance();
87         } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
88             final String resultString = "Apex context schema helper class not found for context schema helper plugin \""
89                     + pluginClass + "\"";
90             LOGGER.warn(resultString, e);
91             throw new ContextRuntimeException(resultString, e);
92         }
93
94         // Check the class is a schema helper
95         if (!(schemaHelperObject instanceof SchemaHelper)) {
96             final String resultString = "Specified Apex context schema helper plugin class \"" + pluginClass
97                     + "\" does not implement the SchemaHelper interface";
98             LOGGER.warn(resultString);
99             throw new ContextRuntimeException(resultString);
100         }
101
102         // The context schema helper to return
103         final SchemaHelper schemaHelper = (SchemaHelper) schemaHelperObject;
104
105         // Lock and load the schema helper
106         schemaHelper.init(owningEntityKey.getKey(), schema);
107
108         LOGGER.exit("Schema Helper factory, owningEntityKey=" + owningEntityKey + ", selected schema helper of class "
109                 + schemaHelper.getClass());
110         return schemaHelper;
111     }
112 }