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.schemagenerator;
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.logging.api.Logger;
29 import org.openecomp.sdc.logging.api.LoggerFactory;
30 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
31 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateContext;
32 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateInput;
34 import java.io.IOException;
35 import java.io.StringWriter;
36 import java.io.Writer;
38 public class SchemaGenerator {
39 public static final String SCHEMA_GENERATION_ERROR = "SCHEMA_GENERATION_ERROR";
40 private final static Logger log = (Logger) LoggerFactory.getLogger(SchemaGenerator.class.getName());
45 * @param schemaTemplateContext the schema template context
46 * @param entityType the entity type
47 * @param input the input
50 public static String generate(SchemaTemplateContext schemaTemplateContext,
51 CompositionEntityType entityType, SchemaTemplateInput input) {
52 Template schemaTemplate =
53 SchemaGeneratorConfig.getSchemaTemplate(schemaTemplateContext, entityType);
54 return processTemplate(input, schemaTemplate);
57 private static String processTemplate(SchemaTemplateInput input, Template schemaTemplate) {
58 try (Writer writer = new StringWriter(1024)) {
59 schemaTemplate.process(input, writer);
60 return writer.toString();
61 } catch (IOException | TemplateException exception) {
62 log.debug("",exception);
63 throw new CoreException(
64 new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
65 .withId(SCHEMA_GENERATION_ERROR).withMessage(exception.getMessage()).build());