08289e642a19f104e61e29b71ecdd42e6f0feb43
[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  *  Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.context.impl.schema;
24
25 import org.onap.policy.apex.context.ContextRuntimeException;
26 import org.onap.policy.apex.context.SchemaHelper;
27 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
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 var 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 var schemaParameters =
74                         (SchemaParameters) ParameterService.get(ContextParameterConstants.SCHEMA_GROUP_NAME);
75
76         // Get the class for the schema helper from the schema parameters
77         final var schemaHelperParameters =
78                 schemaParameters.getSchemaHelperParameters(schema.getSchemaFlavour());
79         if (schemaHelperParameters == null) {
80             final var resultString = "context schema helper parameters not found for context schema  \""
81                     + schema.getSchemaFlavour() + "\"";
82             LOGGER.warn(resultString);
83             throw new ContextRuntimeException(resultString);
84         }
85
86         // Get the class for the schema helper using reflection
87         Object schemaHelperObject = null;
88         final String pluginClass = schemaHelperParameters.getSchemaHelperPluginClass();
89         try {
90             schemaHelperObject = Class.forName(pluginClass).getDeclaredConstructor().newInstance();
91         } catch (final Exception e) {
92             final var resultString = "Apex context schema helper class not found for context schema helper plugin \""
93                     + pluginClass + "\"";
94             LOGGER.warn(resultString, e);
95             throw new ContextRuntimeException(resultString, e);
96         }
97
98         // Check the class is a schema helper
99         if (!(schemaHelperObject instanceof SchemaHelper)) {
100             final var resultString = "Specified Apex context schema helper plugin class \"" + pluginClass
101                     + "\" does not implement the SchemaHelper interface";
102             LOGGER.warn(resultString);
103             throw new ContextRuntimeException(resultString);
104         }
105
106         // The context schema helper to return
107         final var schemaHelper = (SchemaHelper) schemaHelperObject;
108
109         // Lock and load the schema helper
110         schemaHelper.init(owningEntityKey.getKey(), schema);
111
112         LOGGER.exit("Schema Helper factory, owningEntityKey=" + owningEntityKey + ", selected schema helper of class "
113                 + schemaHelper.getClass());
114         return schemaHelper;
115     }
116 }