[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 / SchemaGenerator.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.template.Template;
24 import freemarker.template.TemplateException;
25 import org.openecomp.sdc.common.errors.CoreException;
26 import org.openecomp.sdc.common.errors.ErrorCategory;
27 import org.openecomp.sdc.common.errors.ErrorCode;
28 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateContext;
29 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateInput;
30
31 import java.io.IOException;
32 import java.io.StringWriter;
33 import java.io.Writer;
34
35 /**
36  * The type Schema generator.
37  */
38 public class SchemaGenerator {
39   /**
40    * The constant SCHEMA_GENERATION_ERROR.
41    */
42   public static final String SCHEMA_GENERATION_ERROR = "SCHEMA_GENERATION_ERROR";
43
44   /**
45    * Generate string.
46    *
47    * @param schemaTemplateContext the schema template context
48    * @param entityType            the entity type
49    * @param input                 the input
50    * @return the string
51    */
52   public static String generate(SchemaTemplateContext schemaTemplateContext,
53        org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType entityType,
54                                 SchemaTemplateInput input) {
55     Template schemaTemplate =
56         SchemaGeneratorConfig.getSchemaTemplate(schemaTemplateContext, entityType);
57     return processTemplate(input, schemaTemplate);
58   }
59
60   private static String processTemplate(SchemaTemplateInput input, Template schemaTemplate) {
61     try (Writer writer = new StringWriter(1024)) {
62       schemaTemplate.process(input, writer);
63       return writer.toString();
64     } catch (IOException | TemplateException exception) {
65       throw new CoreException(
66           new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
67               .withId(SCHEMA_GENERATION_ERROR).withMessage(exception.getMessage()).build());
68     }
69   }
70 }