Fix the CDS calls
[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.onap.clamp.loop.service.Service;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.stereotype.Component;
38
39 @Component
40 public class ToscaConverterWithDictionarySupport {
41
42     private static final EELFLogger logger =
43             EELFManager.getInstance().getLogger(ToscaConverterWithDictionarySupport.class);
44
45     private ClampProperties clampProperties;
46     private ToscaMetadataParser metadataParser;
47
48     /**
49      * Constructor with Spring support.
50      *
51      * @param clampProperties Clamp Spring properties
52      * @param metadataParser  Metadata parser
53      */
54     @Autowired
55     public ToscaConverterWithDictionarySupport(ClampProperties clampProperties,
56                                                ToscaMetadataParserWithDictionarySupport metadataParser) {
57         this.clampProperties = clampProperties;
58         this.metadataParser = metadataParser;
59     }
60
61     /**
62      * This method converts a tosca file to a json schema.
63      * It uses some parameters specified in the application.properties.
64      *
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
68      *                           the tosca model
69      * @return A json object being a json schema
70      */
71     public JsonObject convertToscaToJsonSchemaObject(String toscaFile, String policyTypeToDecode,
72                                                      Service serviceModel) {
73         try {
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",
81                     e);
82             return new JsonObject();
83         }
84     }
85
86     /**
87      * This method converts a tosca file to a json schema.
88      * It uses some parameters specified in the application.properties.
89      *
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
94      */
95     public String convertToscaToJsonSchemaString(String toscaFile, String policyTypeToDecode, Service serviceModel) {
96         return JsonUtils.GSON.toJson(this.convertToscaToJsonSchemaObject(toscaFile, policyTypeToDecode, serviceModel));
97     }
98 }