6702a6200ff3d6bfc9f84ef2b5c85d2468996d30
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2020-2021 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.policy.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.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;
38
39 /**
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.
43  *
44  * @see org.onap.policy.clamp.clds.tosca.update.ToscaConverterWithDictionarySupport#convertToscaToJsonSchemaObject
45  */
46 @Component
47 public class ToscaConverterWithDictionarySupport {
48
49     private static final EELFLogger logger =
50             EELFManager.getInstance().getLogger(ToscaConverterWithDictionarySupport.class);
51
52     private ClampProperties clampProperties;
53     private ToscaMetadataParser metadataParser;
54
55     /**
56      * Constructor with Spring support.
57      *
58      * @param clampProperties Clamp Spring properties
59      * @param metadataParser  Metadata parser
60      */
61     @Autowired
62     public ToscaConverterWithDictionarySupport(ClampProperties clampProperties,
63                                                ToscaMetadataParserWithDictionarySupport metadataParser) {
64         this.clampProperties = clampProperties;
65         this.metadataParser = metadataParser;
66     }
67
68     /**
69      * This method converts a tosca file to a json schema.
70      * It uses some parameters specified in the application.properties.
71      *
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
75      *                           the tosca model
76      * @return A json object being a json schema
77      */
78     public JsonObject convertToscaToJsonSchemaObject(String toscaFile, String policyTypeToDecode,
79                                                      Service serviceModel) {
80         try {
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",
88                     e);
89             return new JsonObject();
90         }
91     }
92
93     /**
94      * This method converts a tosca file to a json schema.
95      * It uses some parameters specified in the application.properties.
96      *
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
101      */
102     public String convertToscaToJsonSchemaString(String toscaFile, String policyTypeToDecode, Service serviceModel) {
103         return JsonUtils.GSON.toJson(this.convertToscaToJsonSchemaObject(toscaFile, policyTypeToDecode, serviceModel));
104     }
105 }