[sdc] docker file fix for cassandra
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / services / SchemaGeneratorConfig.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
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  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.vendorsoftwareproduct.services;
22
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;
34
35 import java.io.File;
36 import java.io.IOException;
37 import java.util.HashMap;
38 import java.util.Map;
39
40 /**
41  * The type Schema generator config.
42  */
43 public class SchemaGeneratorConfig {
44   /**
45    * The constant SCHEMA_GENERATOR_INITIALIZATION_ERROR.
46    */
47   public static final String SCHEMA_GENERATOR_INITIALIZATION_ERROR =
48       "SCHEMA_GENERATOR_INITIALIZATION_ERROR";
49   /**
50    * The constant SCHEMA_GENERATOR_INITIALIZATION_ERROR_MSG.
51    */
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();
58
59   private static Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
60   private static StringTemplateLoader stringLoader = new StringTemplateLoader();
61
62   static {
63     configuration.setClassLoaderForTemplateLoading(SchemaGenerator.class.getClassLoader(),
64         File.pathSeparator);
65     configuration.setDefaultEncoding("UTF-8");
66     configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
67     configuration.setLogTemplateExceptions(true);
68     configuration.setTemplateLoader(stringLoader);
69   }
70
71   /**
72    * Insert schema template.
73    *
74    * @param schemaTemplateContext the schema template context
75    * @param entityType            the entity type
76    * @param schemaTemplateString  the schema template string
77    */
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);
83   }
84
85   /**
86    * Gets schema template.
87    *
88    * @param schemaTemplateContext the schema template context
89    * @param entityType            the entity type
90    * @return the schema template
91    */
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());
97
98     SchemaTemplate schemaTemplate = schemaTemplates.get(id);
99     if (schemaTemplate == null || schemaTemplate.timestamp != configurationData.getTimeStamp()) {
100       stringLoader.putTemplate(id.toString(), configurationData.getValue());
101       Template template;
102       try {
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);
109       }
110       schemaTemplate = new SchemaTemplate(template, configurationData.getTimeStamp());
111       schemaTemplates.put(id, schemaTemplate);
112     }
113     return schemaTemplate.template;
114   }
115
116   private static class SchemaTemplateId {
117     private SchemaTemplateContext context;
118     private org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType
119         entityType;
120
121     /**
122      * Instantiates a new Schema template id.
123      *
124      * @param context    the context
125      * @param entityType the entity type
126      */
127     public SchemaTemplateId(SchemaTemplateContext context,
128         org.openecomp.sdc.vendorsoftwareproduct.types.composition
129             .CompositionEntityType entityType) {
130       this.context = context;
131       this.entityType = entityType;
132     }
133
134     @Override
135     public String toString() {
136       return context + "." + entityType;
137     }
138
139     @Override
140     public boolean equals(Object obj) {
141       if (this == obj) {
142         return true;
143       }
144       if (obj == null || getClass() != obj.getClass()) {
145         return false;
146       }
147
148       SchemaTemplateId that = (SchemaTemplateId) obj;
149
150       if (entityType != that.entityType) {
151         return false;
152       }
153       if (context != that.context) {
154         return false;
155       }
156
157       return true;
158     }
159
160     @Override
161     public int hashCode() {
162       int result = entityType != null ? entityType.hashCode() : 0;
163       result = 31 * result + (context != null ? context.hashCode() : 0);
164       return result;
165     }
166   }
167
168   private static class SchemaTemplate {
169     private Template template;
170     private long timestamp;
171
172     /**
173      * Instantiates a new Schema template.
174      *
175      * @param template  the template
176      * @param timestamp the timestamp
177      */
178     public SchemaTemplate(Template template, long timestamp) {
179       this.template = template;
180       this.timestamp = timestamp;
181     }
182   }
183
184 }