d1cd3b11b2bac6fabd434bf3ff45264d0d6ca7cc
[policy/apex-pdp.git] /
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.context.impl.schema;
23
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;
38
39 /**
40  * This class returns a {@link SchemaHelper} for the particular type of schema mechanism configured for use.
41  *
42  * @author Liam Fallon (liam.fallon@ericsson.com)
43  */
44 public class SchemaHelperFactory {
45     // Get a reference to the logger
46     private static final XLogger LOGGER = XLoggerFactory.getXLogger(SchemaHelperFactory.class);
47
48     /**
49      * Return a {@link SchemaHelper} for the particular type of schema mechanism configured for use.
50      *
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
55      */
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");
62
63         // Get the schema for items in the album
64         final AxContextSchema schema = ModelService.getModel(AxContextSchemas.class).get(schemaKey);
65         if (schema == null) {
66             final String resultString =
67                     "schema \"" + schemaKey.getId() + "\" for entity " + owningEntityKey.getId() + " does not exist";
68             LOGGER.warn(resultString);
69             throw new ContextRuntimeException(resultString);
70         }
71
72         // Get the schema class using the parameter service
73         final SchemaParameters schemaParameters = ParameterService.get(ContextParameterConstants.SCHEMA_GROUP_NAME);
74
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);
83         }
84
85         // Get the class for the schema helper using reflection
86         Object schemaHelperObject = null;
87         final String pluginClass = schemaHelperParameters.getSchemaHelperPluginClass();
88         try {
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 \""
92                     + pluginClass + "\"";
93             LOGGER.warn(resultString, e);
94             throw new ContextRuntimeException(resultString, e);
95         }
96
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);
103         }
104
105         // The context schema helper to return
106         final SchemaHelper schemaHelper = (SchemaHelper) schemaHelperObject;
107
108         // Lock and load the schema helper
109         schemaHelper.init(owningEntityKey.getKey(), schema);
110
111         LOGGER.exit("Schema Helper factory, owningEntityKey=" + owningEntityKey + ", selected schema helper of class "
112                 + schemaHelper.getClass());
113         return schemaHelper;
114     }
115 }