Added oparent to sdc main
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / utils / ToscaTypesDefinitionUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.ci.tests.utils;
22
23 import org.openecomp.sdc.be.model.Component;
24 import org.openecomp.sdc.be.model.User;
25 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaAnnotationsTypesDefinition;
26 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaParameterConstants;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.yaml.snakeyaml.Yaml;
30 import org.yaml.snakeyaml.constructor.Constructor;
31
32 import static org.junit.Assert.assertNotNull;
33 import static org.openecomp.sdc.ci.tests.utils.CsarParserUtils.downloadComponentCsar;
34 import static org.openecomp.sdc.ci.tests.utils.ToscaParserUtils.getDataFromZipFileByBytes;
35 import static org.testng.AssertJUnit.fail;
36
37 public class ToscaTypesDefinitionUtils {
38
39     private ToscaTypesDefinitionUtils() {
40     }
41
42     private static final String ANNOTATIONS_TYPE_PATH = ToscaParameterConstants.TOSCA_DEFINITION_PATH + "/annotations.yml";
43     private static Logger log = LoggerFactory.getLogger(ToscaTypesDefinitionUtils.class);
44
45     public static ToscaAnnotationsTypesDefinition getToscaAnnotationsFromCsar(Component csarOwner, User user) throws Exception {
46         byte[] decodeBase64 = downloadComponentCsar(csarOwner, user);
47         String dataFromZipFileByBytes = getDataFromZipFileByBytes(ANNOTATIONS_TYPE_PATH, decodeBase64);
48         assertNotNull(dataFromZipFileByBytes);
49         return parseToscaAnnotationsYml(dataFromZipFileByBytes);
50     }
51
52     private static ToscaAnnotationsTypesDefinition parseToscaAnnotationsYml(String payload){
53         Constructor constructor = initToscaAnnotationDefObject();
54         return (ToscaAnnotationsTypesDefinition) parseToscaYamlPayload(payload, constructor);
55     }
56
57     private static Object parseToscaYamlPayload(String payload, Constructor constructor) {
58         Yaml yaml = new Yaml(constructor);
59         try {
60             return yaml.load(payload);
61         } catch (Exception e) {
62             log.debug("Failed to parse tosca yaml file", e);
63             e.printStackTrace();
64             fail("Failed to parse tosca yaml file");
65         }
66         return null;
67     }
68
69     private static Constructor initToscaAnnotationDefObject() {
70         Constructor toscaStructure = new Constructor(ToscaAnnotationsTypesDefinition.class);
71         toscaStructure.addTypeDescription(ToscaAnnotationsTypesDefinition.getTypeDescription());
72         return toscaStructure;
73     }
74 }