2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
20 * ===================================================================
24 package org.onap.clamp.clds.tosca.update;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.google.gson.JsonObject;
29 import java.io.IOException;
30 import org.onap.clamp.clds.config.ClampProperties;
31 import org.onap.clamp.clds.tosca.update.parser.metadata.ToscaMetadataParser;
32 import org.onap.clamp.clds.tosca.update.parser.metadata.ToscaMetadataParserWithDictionarySupport;
33 import org.onap.clamp.clds.tosca.update.templates.JsonTemplateManager;
34 import org.onap.clamp.clds.util.JsonUtils;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Component;
39 public class ToscaConverterWithDictionarySupport {
41 private static final EELFLogger logger =
42 EELFManager.getInstance().getLogger(ToscaConverterWithDictionarySupport.class);
44 private ClampProperties clampProperties;
45 private ToscaMetadataParser metadataParser;
48 * Constructor with Spring support.
50 * @param clampProperties Clamp Spring properties
51 * @param metadataParser Metadata parser
54 public ToscaConverterWithDictionarySupport(ClampProperties clampProperties,
55 ToscaMetadataParserWithDictionarySupport metadataParser) {
56 this.clampProperties = clampProperties;
57 this.metadataParser = metadataParser;
61 * This method converts a tosca file to a json schema.
62 * It uses some parameters specified in the application.properties.
64 * @param toscaFile The tosca file as String
65 * @param policyTypeToDecode The policy type to decode
66 * @return A json object being a json schema
68 public JsonObject convertToscaToJsonSchemaObject(String toscaFile, String policyTypeToDecode) {
70 return new JsonTemplateManager(toscaFile,
71 clampProperties.getFileContent("tosca.converter.default.datatypes"),
72 clampProperties.getFileContent("tosca.converter.json.schema.templates"))
73 .getJsonSchemaForPolicyType(policyTypeToDecode, Boolean.parseBoolean(clampProperties.getStringValue(
74 "tosca.converter.dictionary.support.enabled")) ? metadataParser : null);
75 } catch (IOException | UnknownComponentException e) {
76 logger.error("Unable to convert the tosca properly, exception caught during the decoding",
78 return new JsonObject();
83 * This method converts a tosca file to a json schema.
84 * It uses some parameters specified in the application.properties.
86 * @param toscaFile The tosca file as String
87 * @param policyTypeToDecode The policy type to decode
88 * @return A String containing the json schema
90 public String convertToscaToJsonSchemaString(String toscaFile, String policyTypeToDecode) {
91 return JsonUtils.GSON.toJson(this.convertToscaToJsonSchemaObject(toscaFile, policyTypeToDecode));