Fix the new tosca converter
[clamp.git] / src / main / java / org / onap / clamp / clds / tosca / update / ToscaConverterWithDictionarySupport.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.clds.tosca.update;
25
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;
37
38 @Component
39 public class ToscaConverterWithDictionarySupport {
40
41     private static final EELFLogger logger =
42             EELFManager.getInstance().getLogger(ToscaConverterWithDictionarySupport.class);
43
44     private ClampProperties clampProperties;
45     private ToscaMetadataParser metadataParser;
46
47     /**
48      * Constructor with Spring support.
49      *
50      * @param clampProperties Clamp Spring properties
51      * @param metadataParser  Metadata parser
52      */
53     @Autowired
54     public ToscaConverterWithDictionarySupport(ClampProperties clampProperties,
55                                                ToscaMetadataParserWithDictionarySupport metadataParser) {
56         this.clampProperties = clampProperties;
57         this.metadataParser = metadataParser;
58     }
59
60     /**
61      * This method converts a tosca file to a json schema.
62      * It uses some parameters specified in the application.properties.
63      *
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
67      */
68     public JsonObject convertToscaToJsonSchemaObject(String toscaFile, String policyTypeToDecode) {
69         try {
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",
77                     e);
78             return new JsonObject();
79         }
80     }
81
82     /**
83      * This method converts a tosca file to a json schema.
84      * It uses some parameters specified in the application.properties.
85      *
86      * @param toscaFile          The tosca file as String
87      * @param policyTypeToDecode The policy type to decode
88      * @return A String containing the json schema
89      */
90     public String convertToscaToJsonSchemaString(String toscaFile, String policyTypeToDecode) {
91         return JsonUtils.GSON.toJson(this.convertToscaToJsonSchemaObject(toscaFile, policyTypeToDecode));
92     }
93 }