2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 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.policy.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.policy.clamp.clds.config.ClampProperties;
31 import org.onap.policy.clamp.clds.tosca.update.parser.metadata.ToscaMetadataParser;
32 import org.onap.policy.clamp.clds.tosca.update.parser.metadata.ToscaMetadataParserWithDictionarySupport;
33 import org.onap.policy.clamp.clds.tosca.update.templates.JsonTemplateManager;
34 import org.onap.policy.clamp.clds.util.JsonUtils;
35 import org.onap.policy.clamp.loop.service.Service;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.stereotype.Component;
40 * This is the main class that must be used to convert a tosca to a json schema.
41 * This class adds feature to support the dictionary mechanism that enables json possible values extracted
42 * from the dictionary DB table.
44 * @see org.onap.policy.clamp.clds.tosca.update.ToscaConverterWithDictionarySupport#convertToscaToJsonSchemaObject
47 public class ToscaConverterWithDictionarySupport {
49 private static final EELFLogger logger =
50 EELFManager.getInstance().getLogger(ToscaConverterWithDictionarySupport.class);
52 private ClampProperties clampProperties;
53 private ToscaMetadataParser metadataParser;
56 * Constructor with Spring support.
58 * @param clampProperties Clamp Spring properties
59 * @param metadataParser Metadata parser
62 public ToscaConverterWithDictionarySupport(ClampProperties clampProperties,
63 ToscaMetadataParserWithDictionarySupport metadataParser) {
64 this.clampProperties = clampProperties;
65 this.metadataParser = metadataParser;
69 * This method converts a tosca file to a json schema.
70 * It uses some parameters specified in the application.properties.
72 * @param toscaFile The tosca file as String
73 * @param policyTypeToDecode The policy type to decode
74 * @param serviceModel The service model associated so that the clamp enrichment could be done if required by
76 * @return A json object being a json schema
78 public JsonObject convertToscaToJsonSchemaObject(String toscaFile, String policyTypeToDecode,
79 Service serviceModel) {
81 return new JsonTemplateManager(toscaFile,
82 clampProperties.getFileContent("tosca.converter.default.datatypes"),
83 clampProperties.getFileContent("tosca.converter.json.schema.templates"))
84 .getJsonSchemaForPolicyType(policyTypeToDecode, Boolean.parseBoolean(clampProperties.getStringValue(
85 "tosca.converter.dictionary.support.enabled")) ? metadataParser : null, serviceModel);
86 } catch (IOException | UnknownComponentException e) {
87 logger.error("Unable to convert the tosca properly, exception caught during the decoding",
89 return new JsonObject();
94 * This method converts a tosca file to a json schema.
95 * It uses some parameters specified in the application.properties.
97 * @param toscaFile The tosca file as String
98 * @param policyTypeToDecode The policy type to decode
99 * @param serviceModel The service Model so that clamp enrichment could be done if required by tosca model
100 * @return A String containing the json schema
102 public String convertToscaToJsonSchemaString(String toscaFile, String policyTypeToDecode, Service serviceModel) {
103 return JsonUtils.GSON.toJson(this.convertToscaToJsonSchemaObject(toscaFile, policyTypeToDecode, serviceModel));