push addional code
[sdc.git] / openecomp-be / lib / openecomp-tosca-lib / src / main / java / org / openecomp / sdc / tosca / services / ToscaNativeTypesServiceTemplate.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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.tosca.services;
22
23 import org.openecomp.sdc.tosca.datatypes.ToscaCapabilityType;
24 import org.openecomp.sdc.tosca.datatypes.ToscaDataType;
25 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
26 import org.openecomp.sdc.tosca.datatypes.ToscaRelationshipType;
27
28 import org.openecomp.sdc.tosca.datatypes.model.AttributeDefinition;
29 import org.openecomp.sdc.tosca.datatypes.model.CapabilityDefinition;
30 import org.openecomp.sdc.tosca.datatypes.model.InterfaceDefinition;
31 import org.openecomp.sdc.tosca.datatypes.model.NodeType;
32 import org.openecomp.sdc.tosca.datatypes.model.PropertyDefinition;
33 import org.openecomp.sdc.tosca.datatypes.model.PropertyType;
34 import org.openecomp.sdc.tosca.datatypes.model.RequirementDefinition;
35 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
36
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.HashMap;
40 import java.util.List;
41 import java.util.Map;
42
43 /**
44  * The type Tosca native types service template.
45  */
46 public class ToscaNativeTypesServiceTemplate {
47   /**
48    * Create service template service template.
49    *
50    * @return the service template
51    */
52   public static ServiceTemplate createServiceTemplate() {
53     ServiceTemplate nativeNodeTypesServiceTemplate = new ServiceTemplate();
54     nativeNodeTypesServiceTemplate
55         .setTosca_definitions_version(ToscaConstants.TOSCA_DEFINITIONS_VERSION);
56     nativeNodeTypesServiceTemplate.setMetadata(DataModelUtil
57         .createMetadata(ToscaConstants.NATIVE_TYPES_SERVICE_TEMPLATE_NAME, "1.0.0", null));
58     nativeNodeTypesServiceTemplate.setDescription("TOSCA Native Node Types");
59     nativeNodeTypesServiceTemplate.setNode_types(createNativeNodeTypes());
60     return nativeNodeTypesServiceTemplate;
61   }
62
63   private static Map<String, NodeType> createNativeNodeTypes() {
64     Map<String, NodeType> nativeNodeTypes = new HashMap<>();
65     nativeNodeTypes.put(ToscaNodeType.COMPUTE.getDisplayName(), createToscaNodesComputeNodeType());
66     nativeNodeTypes.put(ToscaNodeType.ROOT.getDisplayName(), createToscaNodesRootNodeType());
67     nativeNodeTypes
68         .put(ToscaNodeType.NETWORK_PORT.getDisplayName(), createToscaNodesPortsNodeType());
69     nativeNodeTypes
70         .put(ToscaNodeType.BLOCK_STORAGE.getDisplayName(), createToscaNodesBlockStorageNodeType());
71     nativeNodeTypes.put(ToscaNodeType.NETWORK.getDisplayName(), createToscaNodesNetworkNodeType());
72     return nativeNodeTypes;
73   }
74
75   private static NodeType createToscaNodesComputeNodeType() {
76     NodeType computeNodeType = new NodeType();
77     computeNodeType.setDerived_from(ToscaNodeType.ROOT.getDisplayName());
78     computeNodeType.setAttributes(createComputeAttributes());
79     computeNodeType.setRequirements(createComputeRequirements());
80     computeNodeType.setCapabilities(createComputeCapabilities());
81     return computeNodeType;
82   }
83
84   private static NodeType createToscaNodesRootNodeType() {
85     NodeType computeNodeType = new NodeType();
86     computeNodeType.setAttributes(createRootAttributes());
87     computeNodeType.setInterfaces(createRootInterfaces());
88     return computeNodeType;
89   }
90
91   private static NodeType createToscaNodesPortsNodeType() {
92     NodeType computeNodeType = new NodeType();
93     computeNodeType.setDerived_from(ToscaNodeType.ROOT.getDisplayName());
94     computeNodeType.setProperties(createToscaNodesPortProperties());
95     computeNodeType.setRequirements(createToscaNodesPortRequirements());
96     return computeNodeType;
97   }
98
99   private static NodeType createToscaNodesBlockStorageNodeType() {
100     NodeType computeNodeType = new NodeType();
101     computeNodeType.setDerived_from(ToscaNodeType.ROOT.getDisplayName());
102     computeNodeType.setProperties(createToscaNodesBlockStorageProperties());
103     computeNodeType.setCapabilities(createToscaNodesBlockStorageCapabilities());
104     return computeNodeType;
105   }
106
107   private static NodeType createToscaNodesNetworkNodeType() {
108     NodeType computeNodeType = new NodeType();
109     computeNodeType.setDerived_from(ToscaNodeType.ROOT.getDisplayName());
110     computeNodeType.setProperties(createToscaNodesNetworkProperties());
111     computeNodeType.setCapabilities(createToscaNodesNetworkCapabilities());
112     return computeNodeType;
113   }
114
115   private static Map<String, PropertyDefinition> createToscaNodesPortProperties() {
116     Map<String, PropertyDefinition> propertyDefinitionMap = new HashMap<>();
117     propertyDefinitionMap.put("ip_address", DataModelUtil
118         .createPropertyDefinition(PropertyType.STRING.getDisplayName(), null, false, null, null,
119             null, null));
120     propertyDefinitionMap.put("order", DataModelUtil
121         .createPropertyDefinition(PropertyType.INTEGER.getDisplayName(), null, true,
122             DataModelUtil.getConstrainList(DataModelUtil.createGreaterOrEqualConstrain(0)), null,
123             null, 0));
124     propertyDefinitionMap.put("is_default", DataModelUtil
125         .createPropertyDefinition(PropertyType.BOOLEAN.getDisplayName(), null, false, null, null,
126             null, false));
127     propertyDefinitionMap.put("ip_range_start", DataModelUtil
128         .createPropertyDefinition(PropertyType.STRING.getDisplayName(), null, false, null, null,
129             null, null));
130     propertyDefinitionMap.put("ip_range_end", DataModelUtil
131         .createPropertyDefinition(PropertyType.STRING.getDisplayName(), null, false, null, null,
132             null, null));
133     return propertyDefinitionMap;
134   }
135
136   private static Map<String, PropertyDefinition> createToscaNodesBlockStorageProperties() {
137     Map<String, PropertyDefinition> propertyDefinitionMap = new HashMap<>();
138     propertyDefinitionMap.put("size", DataModelUtil
139         .createPropertyDefinition(PropertyType.SCALAR_UNIT_SIZE.getDisplayName(), null, false,
140             DataModelUtil.getConstrainList(DataModelUtil.createGreaterOrEqualConstrain("1 MB")),
141             null, null, null));
142     propertyDefinitionMap.put("volume_id", DataModelUtil
143         .createPropertyDefinition(PropertyType.STRING.getDisplayName(), null, false, null, null,
144             null, null));
145     propertyDefinitionMap.put("snapshot_id", DataModelUtil
146         .createPropertyDefinition(PropertyType.STRING.getDisplayName(), null, false, null, null,
147             null, null));
148     return propertyDefinitionMap;
149   }
150
151   private static Map<String, PropertyDefinition> createToscaNodesNetworkProperties() {
152     Map<String, PropertyDefinition> propertyDefinitionMap = new HashMap<>();
153     propertyDefinitionMap.put("ip_version", DataModelUtil
154         .createPropertyDefinition(PropertyType.INTEGER.getDisplayName(), null, false,
155             DataModelUtil.getConstrainList(DataModelUtil.createValidValuesConstraint(4, 6)), null,
156             null, 4));
157     propertyDefinitionMap.put("cidr", DataModelUtil
158         .createPropertyDefinition(PropertyType.STRING.getDisplayName(), null, false, null, null,
159             null, null));
160     propertyDefinitionMap.put("start_ip", DataModelUtil
161         .createPropertyDefinition(PropertyType.STRING.getDisplayName(), null, false, null, null,
162             null, null));
163     propertyDefinitionMap.put("end_ip", DataModelUtil
164         .createPropertyDefinition(PropertyType.STRING.getDisplayName(), null, false, null, null,
165             null, null));
166     propertyDefinitionMap.put("gateway_ip", DataModelUtil
167         .createPropertyDefinition(PropertyType.STRING.getDisplayName(), null, false, null, null,
168             null, null));
169     propertyDefinitionMap.put("network_name", DataModelUtil
170         .createPropertyDefinition(PropertyType.STRING.getDisplayName(), null, false, null, null,
171             null, null));
172     propertyDefinitionMap.put("network_id", DataModelUtil
173         .createPropertyDefinition(PropertyType.STRING.getDisplayName(), null, false, null, null,
174             null, null));
175     propertyDefinitionMap.put("segmentation_id", DataModelUtil
176         .createPropertyDefinition(PropertyType.STRING.getDisplayName(), null, false, null, null,
177             null, null));
178     propertyDefinitionMap.put("network_type", DataModelUtil
179         .createPropertyDefinition(PropertyType.STRING.getDisplayName(), null, false, null, null,
180             null, null));
181     propertyDefinitionMap.put("physical_network", DataModelUtil
182         .createPropertyDefinition(PropertyType.STRING.getDisplayName(), null, false, null, null,
183             null, null));
184     propertyDefinitionMap.put("dhcp_enabled", DataModelUtil
185         .createPropertyDefinition(PropertyType.BOOLEAN.getDisplayName(), null, false, null, null,
186             null, true));
187
188     return propertyDefinitionMap;
189   }
190
191   private static Map<String, InterfaceDefinition> createRootInterfaces() {
192     return new HashMap<>();
193   }
194
195   private static Map<String, CapabilityDefinition> createComputeCapabilities() {
196     Map<String, CapabilityDefinition> computeCapabilities = new HashMap<>();
197     computeCapabilities.put(ToscaConstants.HOST_CAPABILITY_ID,
198         createCapabilityDefinition(ToscaCapabilityType.CONTAINER.getDisplayName(),
199             createValidSourceTypes("tosca.nodes.SoftwareComponent")));
200     computeCapabilities.put(ToscaConstants.ENDPOINT_CAPABILITY_ID,
201         createCapabilityDefinition(ToscaCapabilityType.ENDPOINT_ADMIN.getDisplayName(), null));
202     computeCapabilities.put(ToscaConstants.OS_CAPABILITY_ID,
203         createCapabilityDefinition(ToscaCapabilityType.OPERATING_SYSTEM.getDisplayName(), null));
204     computeCapabilities.put(ToscaConstants.SCALABLE_CAPABILITY_ID,
205         createCapabilityDefinition(ToscaCapabilityType.SCALABLE.getDisplayName(), null));
206     computeCapabilities.put(ToscaConstants.BINDING_CAPABILITY_ID,
207         createCapabilityDefinition(ToscaCapabilityType.NETWORK_BINDABLE.getDisplayName(), null));
208     return computeCapabilities;
209   }
210
211   private static Map<String, CapabilityDefinition> createToscaNodesBlockStorageCapabilities() {
212     Map<String, CapabilityDefinition> computeCapabilities = new HashMap<>();
213     computeCapabilities.put("attachment",
214         createCapabilityDefinition(ToscaCapabilityType.ATTACHMENT.getDisplayName(), null));
215     return computeCapabilities;
216   }
217
218   private static Map<String, CapabilityDefinition> createToscaNodesNetworkCapabilities() {
219     Map<String, CapabilityDefinition> computeCapabilities = new HashMap<>();
220     computeCapabilities.put(ToscaConstants.LINK_CAPABILITY_ID,
221         createCapabilityDefinition(ToscaCapabilityType.NETWORK_LINKABLE.getDisplayName(), null));
222     return computeCapabilities;
223   }
224
225   private static List<String> createValidSourceTypes(String... validSourceTypes) {
226     return Arrays.asList(validSourceTypes);
227   }
228
229   private static CapabilityDefinition createCapabilityDefinition(String type,
230                                                                  List<String> validSourceTypes) {
231     CapabilityDefinition capabilityDefinition = new CapabilityDefinition();
232     capabilityDefinition.setType(type);
233     capabilityDefinition.setValid_source_types(validSourceTypes);
234     return capabilityDefinition;
235   }
236
237   private static List<Map<String, RequirementDefinition>> createComputeRequirements() {
238     List<Map<String, RequirementDefinition>> computeRequirementList = new ArrayList<>();
239     Map<String, RequirementDefinition> localStorageRequirement = new HashMap<>();
240     localStorageRequirement.put("local_storage", DataModelUtil
241         .createRequirement(ToscaCapabilityType.ATTACHMENT.getDisplayName(),
242             ToscaNodeType.BLOCK_STORAGE.getDisplayName(),
243             ToscaRelationshipType.NATIVE_ATTACHES_TO.getDisplayName(),
244             createOccurrences(0, ToscaConstants.UNBOUNDED)));
245     computeRequirementList.add(localStorageRequirement);
246     return computeRequirementList;
247   }
248
249   private static List<Map<String, RequirementDefinition>> createToscaNodesPortRequirements() {
250     List<Map<String, RequirementDefinition>> requirements = new ArrayList<>();
251
252     Map<String, RequirementDefinition> linkRequirement = new HashMap<>();
253     linkRequirement.put(ToscaConstants.LINK_REQUIREMENT_ID, DataModelUtil
254         .createRequirement(ToscaCapabilityType.NETWORK_LINKABLE.getDisplayName(),
255             ToscaNodeType.ROOT.getDisplayName(),
256             ToscaRelationshipType.NETWORK_LINK_TO.getDisplayName(), null));
257     requirements.add(linkRequirement);
258
259     Map<String, RequirementDefinition> bindingRequirement = new HashMap<>();
260     bindingRequirement.put(ToscaConstants.BINDING_REQUIREMENT_ID, DataModelUtil
261         .createRequirement(ToscaCapabilityType.NETWORK_BINDABLE.getDisplayName(),
262             ToscaNodeType.ROOT.getDisplayName(),
263             ToscaRelationshipType.NETWORK_BINDS_TO.getDisplayName(), null));
264     requirements.add(bindingRequirement);
265
266     return requirements;
267   }
268
269
270   private static Object[] createOccurrences(Object min, Object max) {
271     return new Object[]{min, max};
272
273   }
274
275   private static Map<String, AttributeDefinition> createComputeAttributes() {
276     Map<String, AttributeDefinition> computeAttributesDefMap = new HashMap<>();
277     computeAttributesDefMap.put("private_address", DataModelUtil
278         .createAttributeDefinition(PropertyType.STRING.getDisplayName(), "private address", null,
279             null, null));
280     computeAttributesDefMap.put("public_address", DataModelUtil
281         .createAttributeDefinition(PropertyType.STRING.getDisplayName(), "public_address", null,
282             null, null));
283     computeAttributesDefMap.put("networks", DataModelUtil
284         .createAttributeDefinition(PropertyType.MAP.getDisplayName(), "networks", null,
285             DataModelUtil
286                 .createEntrySchema(ToscaDataType.NETWORK_NETWORK_INFO.getDisplayName(), null, null),
287             null));
288     computeAttributesDefMap.put("ports", DataModelUtil
289         .createAttributeDefinition(PropertyType.MAP.getDisplayName(), "ports", null, DataModelUtil
290                 .createEntrySchema(ToscaDataType.NETWORK_PORT_INFO.getDisplayName(), null, null),
291             null));
292     return computeAttributesDefMap;
293   }
294
295   private static Map<String, AttributeDefinition> createRootAttributes() {
296     Map<String, AttributeDefinition> attributesDefMap = new HashMap<>();
297     attributesDefMap.put("tosca_id", DataModelUtil
298         .createAttributeDefinition(PropertyType.STRING.getDisplayName(), "tosca id", null, null,
299             null));
300     attributesDefMap.put("tosca_name", DataModelUtil
301         .createAttributeDefinition(PropertyType.STRING.getDisplayName(), "tosca name", null, null,
302             null));
303     attributesDefMap.put("state", DataModelUtil
304         .createAttributeDefinition(PropertyType.STRING.getDisplayName(), "state", null, null,
305             null));
306     return attributesDefMap;
307   }
308
309 }