[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / lib / openecomp-sdc-vendor-software-product-lib / openecomp-sdc-vendor-software-product-api / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / services / schemagenerator / 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.schemagenerator;
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.composition.CompositionEntityType;
34 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateContext;
35
36 import java.io.File;
37 import java.io.IOException;
38 import java.util.HashMap;
39 import java.util.Map;
40
41 public class SchemaGeneratorConfig {
42   public static final String SCHEMA_GENERATOR_INITIALIZATION_ERROR =
43       "SCHEMA_GENERATOR_INITIALIZATION_ERROR";
44   public static final String SCHEMA_GENERATOR_INITIALIZATION_ERROR_MSG =
45       "Error occurred while loading questionnaire schema schemaTemplates";
46   private static final String CONFIGURATION_NAMESPACE = "vsp.schemaTemplates";
47   private static Map<SchemaTemplateId, SchemaTemplate> schemaTemplates = new HashMap<>();
48   private static ApplicationConfig applicationConfig =
49       ApplicationConfigFactory.getInstance().createInterface();
50
51   private static Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
52   private static StringTemplateLoader stringLoader = new StringTemplateLoader();
53
54   static {
55     configuration.setClassLoaderForTemplateLoading(SchemaGenerator.class.getClassLoader(),
56         File.pathSeparator);
57     configuration.setDefaultEncoding("UTF-8");
58     configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
59     configuration.setLogTemplateExceptions(true);
60     configuration.setTemplateLoader(stringLoader);
61   }
62
63   public static void insertSchemaTemplate(SchemaTemplateContext schemaTemplateContext,
64                                           CompositionEntityType entityType,
65                                           String schemaTemplateString) {
66     applicationConfig.insertValue(CONFIGURATION_NAMESPACE,
67         new SchemaTemplateId(schemaTemplateContext, entityType).toString(), schemaTemplateString);
68   }
69
70   /**
71    * Gets schema template.
72    *
73    * @param schemaTemplateContext the schema template context
74    * @param entityType            the entity type
75    * @return the schema template
76    */
77   public static Template getSchemaTemplate(SchemaTemplateContext schemaTemplateContext,
78                                            CompositionEntityType entityType) {
79     SchemaTemplateId id = new SchemaTemplateId(schemaTemplateContext, entityType);
80     ConfigurationData configurationData =
81         applicationConfig.getConfigurationData(CONFIGURATION_NAMESPACE, id.toString());
82
83     SchemaTemplate schemaTemplate = schemaTemplates.get(id);
84     if (schemaTemplate == null || schemaTemplate.timestamp != configurationData.getTimeStamp()) {
85       stringLoader.putTemplate(id.toString(), configurationData.getValue());
86       Template template;
87       try {
88         template = configuration.getTemplate(id.toString());
89       } catch (IOException exception) {
90         throw new CoreException(
91             new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
92                 .withId(SCHEMA_GENERATOR_INITIALIZATION_ERROR)
93                 .withMessage(SCHEMA_GENERATOR_INITIALIZATION_ERROR_MSG).build(), exception);
94       }
95       schemaTemplate = new SchemaTemplate(template, configurationData.getTimeStamp());
96       schemaTemplates.put(id, schemaTemplate);
97     }
98     return schemaTemplate.template;
99   }
100
101   private static class SchemaTemplateId {
102     private SchemaTemplateContext context;
103     private CompositionEntityType entityType;
104
105     public SchemaTemplateId(SchemaTemplateContext context, CompositionEntityType entityType) {
106       this.context = context;
107       this.entityType = entityType;
108     }
109
110     @Override
111     public String toString() {
112       return context + "." + entityType;
113     }
114
115     @Override
116     public boolean equals(Object obj) {
117       if (this == obj) {
118         return true;
119       }
120       if (obj == null || getClass() != obj.getClass()) {
121         return false;
122       }
123
124       SchemaTemplateId that = (SchemaTemplateId) obj;
125
126       if (entityType != that.entityType) {
127         return false;
128       }
129       if (context != that.context) {
130         return false;
131       }
132
133       return true;
134     }
135
136     @Override
137     public int hashCode() {
138       int result = entityType != null ? entityType.hashCode() : 0;
139       result = 31 * result + (context != null ? context.hashCode() : 0);
140       return result;
141     }
142   }
143
144   private static class SchemaTemplate {
145     private Template template;
146     private long timestamp;
147
148     public SchemaTemplate(Template template, long timestamp) {
149       this.template = template;
150       this.timestamp = timestamp;
151     }
152   }
153
154 }