84025fc25db481a556638228e521ec42fea551ae
[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.argumentNotNull(owningEntityKey, ContextRuntimeException.class,
58                 "Parameter \"owningEntityKey\" may not be null");
59         Assertions.argumentNotNull(schemaKey, ContextRuntimeException.class, "Parameter \"schemaKey\" may not be null");
60
61         // Get the schema for items in the album
62         final AxContextSchema schema = ModelService.getModel(AxContextSchemas.class).get(schemaKey);
63         if (schema == null) {
64             final String resultString =
65                     "schema \"" + schemaKey.getID() + "\" for entity " + owningEntityKey.getID() + " does not exist";
66             LOGGER.warn(resultString);
67             throw new ContextRuntimeException(resultString);
68         }
69
70         // Get the schema class using the parameter service
71         final SchemaParameters schemaParameters = ParameterService.get(ContextParameterConstants.SCHEMA_GROUP_NAME);
72
73         // Get the class for the schema helper from the schema parameters
74         final SchemaHelperParameters schemaHelperParameters =
75                 schemaParameters.getSchemaHelperParameters(schema.getSchemaFlavour());
76         if (schemaHelperParameters == null) {
77             final String resultString = "context schema helper parameters not found for context schema  \""
78                     + schema.getSchemaFlavour() + "\"";
79             LOGGER.warn(resultString);
80             throw new ContextRuntimeException(resultString);
81         }
82
83         // Get the class for the schema helper using reflection
84         Object schemaHelperObject = null;
85         final String pluginClass = schemaHelperParameters.getSchemaHelperPluginClass();
86         try {
87             schemaHelperObject = Class.forName(pluginClass).newInstance();
88         } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
89             final String resultString = "Apex context schema helper class not found for context schema helper plugin \""
90                     + pluginClass + "\"";
91             LOGGER.warn(resultString, e);
92             throw new ContextRuntimeException(resultString, e);
93         }
94
95         // Check the class is a schema helper
96         if (!(schemaHelperObject instanceof SchemaHelper)) {
97             final String resultString = "Specified Apex context schema helper plugin class \"" + pluginClass
98                     + "\" does not implement the SchemaHelper interface";
99             LOGGER.warn(resultString);
100             throw new ContextRuntimeException(resultString);
101         }
102
103         // The context schema helper to return
104         final SchemaHelper schemaHelper = (SchemaHelper) schemaHelperObject;
105
106         // Lock and load the schema helper
107         schemaHelper.init(owningEntityKey.getKey(), schema);
108
109         LOGGER.exit("Schema Helper factory, owningEntityKey=" + owningEntityKey + ", selected schema helper of class "
110                 + schemaHelper.getClass());
111         return schemaHelper;
112     }
113 }