2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.vendorsoftwareproduct.services;
23 import freemarker.cache.StringTemplateLoader;
24 import freemarker.template.Configuration;
25 import freemarker.template.Template;
26 import freemarker.template.TemplateExceptionHandler;
27 import org.openecomp.core.utilities.applicationconfig.ApplicationConfig;
28 import org.openecomp.core.utilities.applicationconfig.ApplicationConfigFactory;
29 import org.openecomp.core.utilities.applicationconfig.type.ConfigurationData;
30 import org.openecomp.sdc.common.errors.CoreException;
31 import org.openecomp.sdc.common.errors.ErrorCategory;
32 import org.openecomp.sdc.common.errors.ErrorCode;
33 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateContext;
36 import java.io.IOException;
37 import java.util.HashMap;
41 * The type Schema generator config.
43 public class SchemaGeneratorConfig {
45 * The constant SCHEMA_GENERATOR_INITIALIZATION_ERROR.
47 public static final String SCHEMA_GENERATOR_INITIALIZATION_ERROR =
48 "SCHEMA_GENERATOR_INITIALIZATION_ERROR";
50 * The constant SCHEMA_GENERATOR_INITIALIZATION_ERROR_MSG.
52 public static final String SCHEMA_GENERATOR_INITIALIZATION_ERROR_MSG =
53 "Error occurred while loading questionnaire schema schemaTemplates";
54 private static final String CONFIGURATION_NAMESPACE = "vsp.schemaTemplates";
55 private static Map<SchemaTemplateId, SchemaTemplate> schemaTemplates = new HashMap<>();
56 private static ApplicationConfig applicationConfig =
57 ApplicationConfigFactory.getInstance().createInterface();
59 private static Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
60 private static StringTemplateLoader stringLoader = new StringTemplateLoader();
63 configuration.setClassLoaderForTemplateLoading(SchemaGenerator.class.getClassLoader(),
65 configuration.setDefaultEncoding("UTF-8");
66 configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
67 configuration.setLogTemplateExceptions(true);
68 configuration.setTemplateLoader(stringLoader);
72 * Insert schema template.
74 * @param schemaTemplateContext the schema template context
75 * @param entityType the entity type
76 * @param schemaTemplateString the schema template string
78 public static void insertSchemaTemplate(SchemaTemplateContext schemaTemplateContext,
79 org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType entityType,
80 String schemaTemplateString) {
81 applicationConfig.insertValue(CONFIGURATION_NAMESPACE,
82 new SchemaTemplateId(schemaTemplateContext, entityType).toString(), schemaTemplateString);
86 * Gets schema template.
88 * @param schemaTemplateContext the schema template context
89 * @param entityType the entity type
90 * @return the schema template
92 public static Template getSchemaTemplate(SchemaTemplateContext schemaTemplateContext,
93 org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType entityType) {
94 SchemaTemplateId id = new SchemaTemplateId(schemaTemplateContext, entityType);
95 ConfigurationData configurationData =
96 applicationConfig.getConfigurationData(CONFIGURATION_NAMESPACE, id.toString());
98 SchemaTemplate schemaTemplate = schemaTemplates.get(id);
99 if (schemaTemplate == null || schemaTemplate.timestamp != configurationData.getTimeStamp()) {
100 stringLoader.putTemplate(id.toString(), configurationData.getValue());
103 template = configuration.getTemplate(id.toString());
104 } catch (IOException exception) {
105 throw new CoreException(
106 new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
107 .withId(SCHEMA_GENERATOR_INITIALIZATION_ERROR)
108 .withMessage(SCHEMA_GENERATOR_INITIALIZATION_ERROR_MSG).build(), exception);
110 schemaTemplate = new SchemaTemplate(template, configurationData.getTimeStamp());
111 schemaTemplates.put(id, schemaTemplate);
113 return schemaTemplate.template;
116 private static class SchemaTemplateId {
117 private SchemaTemplateContext context;
118 private org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType
122 * Instantiates a new Schema template id.
124 * @param context the context
125 * @param entityType the entity type
127 public SchemaTemplateId(SchemaTemplateContext context,
128 org.openecomp.sdc.vendorsoftwareproduct.types.composition
129 .CompositionEntityType entityType) {
130 this.context = context;
131 this.entityType = entityType;
135 public String toString() {
136 return context + "." + entityType;
140 public boolean equals(Object obj) {
144 if (obj == null || getClass() != obj.getClass()) {
148 SchemaTemplateId that = (SchemaTemplateId) obj;
150 if (entityType != that.entityType) {
153 if (context != that.context) {
161 public int hashCode() {
162 int result = entityType != null ? entityType.hashCode() : 0;
163 result = 31 * result + (context != null ? context.hashCode() : 0);
168 private static class SchemaTemplate {
169 private Template template;
170 private long timestamp;
173 * Instantiates a new Schema template.
175 * @param template the template
176 * @param timestamp the timestamp
178 public SchemaTemplate(Template template, long timestamp) {
179 this.template = template;
180 this.timestamp = timestamp;