Exception handling for image software versions
[aai/babel.git] / src / test / java / org / onap / aai / babel / csar / vnfcatalog / SdcToscaHelper.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2019 European Software Marketing Ltd.
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 package org.onap.aai.babel.csar.vnfcatalog;
23
24 import java.util.ArrayList;
25 import java.util.LinkedHashMap;
26 import org.onap.sdc.toscaparser.api.NodeTemplate;
27 import org.onap.sdc.toscaparser.api.SubstitutionMappings;
28
29 public class SdcToscaHelper {
30
31     private ArrayList<NodeTemplate> smnodetemplates = new ArrayList<>();
32
33     /**
34      * Create the test SubstitutionMappings.
35      *
36      * @return the new Substitution Mappings
37      */
38     public SubstitutionMappings buildMappings() {
39         LinkedHashMap<String, Object> defProps = getImagesDefProps();
40
41         LinkedHashMap<String, Object> defs = buildNodeTemplateTypeInfo(defProps);
42         LinkedHashMap<String, Object> caps = new LinkedHashMap<>();
43         LinkedHashMap<String, Object> reqs = new LinkedHashMap<>();
44
45         String type = "tosca.nodes.custom";
46
47         LinkedHashMap<String, Object> smsubMappingDef = new LinkedHashMap<>();
48         smsubMappingDef.put("node_type", type);
49         smsubMappingDef.put("capabilities", caps);
50         smsubMappingDef.put("requirements", reqs);
51
52         LinkedHashMap<String, Object> smcustomDefs = buildCustomTypeDefinitions(type, defs);
53
54         return new SubstitutionMappings(smsubMappingDef, smnodetemplates, null, null, null, null, smcustomDefs);
55     }
56
57     private LinkedHashMap<String, Object> getImagesDefProps() {
58         LinkedHashMap<String, Object> imagesDef = new LinkedHashMap<>();
59         imagesDef.put("type", "map");
60         imagesDef.put("required", false);
61         imagesDef.put("entry_schema", "{type=org.openecomp.datatypes.ImageInfo}");
62
63         LinkedHashMap<String, Object> defProps = new LinkedHashMap<>();
64         defProps.put("images", imagesDef);
65         return defProps;
66     }
67
68     private LinkedHashMap<String, Object> buildCustomTypeDefinitions(String type,
69             LinkedHashMap<String, Object> typeInfo) {
70         LinkedHashMap<String, Object> customDefs = new LinkedHashMap<>();
71         customDefs.put(type, typeInfo);
72         return customDefs;
73     }
74
75     private LinkedHashMap<String, Object> buildNodeTemplateTypeInfo(LinkedHashMap<String, Object> props) {
76         LinkedHashMap<String, Object> typeInfo = new LinkedHashMap<>();
77         typeInfo.put("derived_from", "tosca.nodes.Root");
78         typeInfo.put("properties", props);
79         return typeInfo;
80     }
81
82     /**
83      * Create a new NodeTemplate and add it to the list (for populating the Substitution Mappings).
84      *
85      * @return the new NodeTemplate
86      */
87     public NodeTemplate addNodeTemplate() {
88         String name = "node name";
89         String type = "tosca.nodes.custom";
90
91         LinkedHashMap<String, Object> ntMap = new LinkedHashMap<>();
92         ntMap.put("type", type);
93         ntMap.put("properties", null);
94
95         LinkedHashMap<String, Object> ntnodeTemplates = buildCustomTypeDefinitions(name, ntMap);
96         ntnodeTemplates.put("derived_from", null);
97         ntnodeTemplates.put("properties", getImagesDefProps());
98
99         LinkedHashMap<String, Object> typeInfo = buildNodeTemplateTypeInfo(getImagesDefProps());
100         LinkedHashMap<String, Object> customDefs = buildCustomTypeDefinitions(type, typeInfo);
101         NodeTemplate nodeTemplate = new NodeTemplate(name, ntnodeTemplates, customDefs, null, null);
102         smnodetemplates.add(nodeTemplate);
103         return nodeTemplate;
104     }
105
106     /**
107      * Simulate the creation of a NodeTemplate by the SDC TOSCA parser. Populate the properties of the NodeTemplate with
108      * the supplied images.
109      *
110      * @param images
111      *            the value of the images property
112      */
113     public void addNodeTemplate(Object images) {
114         LinkedHashMap<String, Object> properties = new LinkedHashMap<>();
115         properties.put("images", images);
116
117         String type = "tosca.nodes.custom";
118         LinkedHashMap<String, Object> nodeTemplate = new LinkedHashMap<>();
119         nodeTemplate.put("type", type);
120         nodeTemplate.put("properties", properties);
121
122         String name = "node name";
123         LinkedHashMap<String, Object> ntnodeTemplates = buildCustomTypeDefinitions(name, nodeTemplate);
124         ntnodeTemplates.put("derived_from", null);
125         ntnodeTemplates.put("properties", getImagesDefProps());
126
127         LinkedHashMap<String, Object> typeInfo = buildNodeTemplateTypeInfo(getImagesDefProps());
128         LinkedHashMap<String, Object> customDefs = buildCustomTypeDefinitions(type, typeInfo);
129
130         smnodetemplates.add(new NodeTemplate(name, ntnodeTemplates, customDefs, null, null));
131     }
132 }
133