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.onap.clamp.loop.service.Service;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.stereotype.Component;
40 public class ToscaConverterWithDictionarySupport {
42 private static final EELFLogger logger =
43 EELFManager.getInstance().getLogger(ToscaConverterWithDictionarySupport.class);
45 private ClampProperties clampProperties;
46 private ToscaMetadataParser metadataParser;
49 * Constructor with Spring support.
51 * @param clampProperties Clamp Spring properties
52 * @param metadataParser Metadata parser
55 public ToscaConverterWithDictionarySupport(ClampProperties clampProperties,
56 ToscaMetadataParserWithDictionarySupport metadataParser) {
57 this.clampProperties = clampProperties;
58 this.metadataParser = metadataParser;
62 * This method converts a tosca file to a json schema.
63 * It uses some parameters specified in the application.properties.
65 * @param toscaFile The tosca file as String
66 * @param policyTypeToDecode The policy type to decode
67 * @param serviceModel The service model associated so that the clamp enrichment could be done if required by
69 * @return A json object being a json schema
71 public JsonObject convertToscaToJsonSchemaObject(String toscaFile, String policyTypeToDecode,
72 Service serviceModel) {
74 return new JsonTemplateManager(toscaFile,
75 clampProperties.getFileContent("tosca.converter.default.datatypes"),
76 clampProperties.getFileContent("tosca.converter.json.schema.templates"))
77 .getJsonSchemaForPolicyType(policyTypeToDecode, Boolean.parseBoolean(clampProperties.getStringValue(
78 "tosca.converter.dictionary.support.enabled")) ? metadataParser : null, serviceModel);
79 } catch (IOException | UnknownComponentException e) {
80 logger.error("Unable to convert the tosca properly, exception caught during the decoding",
82 return new JsonObject();
87 * This method converts a tosca file to a json schema.
88 * It uses some parameters specified in the application.properties.
90 * @param toscaFile The tosca file as String
91 * @param policyTypeToDecode The policy type to decode
92 * @param serviceModel The service Model so that clamp enrichment could be done if required by tosca model
93 * @return A String containing the json schema
95 public String convertToscaToJsonSchemaString(String toscaFile, String policyTypeToDecode, Service serviceModel) {
96 return JsonUtils.GSON.toJson(this.convertToscaToJsonSchemaObject(toscaFile, policyTypeToDecode, serviceModel));