2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.tosca.services;
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;
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;
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.HashMap;
40 import java.util.List;
44 * The type Tosca native types service template.
46 public class ToscaNativeTypesServiceTemplate {
48 * Create service template service template.
50 * @return the service template
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;
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());
68 .put(ToscaNodeType.NETWORK_PORT.getDisplayName(), createToscaNodesPortsNodeType());
70 .put(ToscaNodeType.BLOCK_STORAGE.getDisplayName(), createToscaNodesBlockStorageNodeType());
71 nativeNodeTypes.put(ToscaNodeType.NETWORK.getDisplayName(), createToscaNodesNetworkNodeType());
72 return nativeNodeTypes;
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;
84 private static NodeType createToscaNodesRootNodeType() {
85 NodeType computeNodeType = new NodeType();
86 computeNodeType.setAttributes(createRootAttributes());
87 computeNodeType.setInterfaces(createRootInterfaces());
88 return computeNodeType;
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;
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;
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;
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,
120 propertyDefinitionMap.put("order", DataModelUtil
121 .createPropertyDefinition(PropertyType.INTEGER.getDisplayName(), null, true,
122 DataModelUtil.getConstrainList(DataModelUtil.createGreaterOrEqualConstrain(0)), null,
124 propertyDefinitionMap.put("is_default", DataModelUtil
125 .createPropertyDefinition(PropertyType.BOOLEAN.getDisplayName(), null, false, null, null,
127 propertyDefinitionMap.put("ip_range_start", DataModelUtil
128 .createPropertyDefinition(PropertyType.STRING.getDisplayName(), null, false, null, null,
130 propertyDefinitionMap.put("ip_range_end", DataModelUtil
131 .createPropertyDefinition(PropertyType.STRING.getDisplayName(), null, false, null, null,
133 return propertyDefinitionMap;
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")),
142 propertyDefinitionMap.put("volume_id", DataModelUtil
143 .createPropertyDefinition(PropertyType.STRING.getDisplayName(), null, false, null, null,
145 propertyDefinitionMap.put("snapshot_id", DataModelUtil
146 .createPropertyDefinition(PropertyType.STRING.getDisplayName(), null, false, null, null,
148 return propertyDefinitionMap;
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,
157 propertyDefinitionMap.put("cidr", DataModelUtil
158 .createPropertyDefinition(PropertyType.STRING.getDisplayName(), null, false, null, null,
160 propertyDefinitionMap.put("start_ip", DataModelUtil
161 .createPropertyDefinition(PropertyType.STRING.getDisplayName(), null, false, null, null,
163 propertyDefinitionMap.put("end_ip", DataModelUtil
164 .createPropertyDefinition(PropertyType.STRING.getDisplayName(), null, false, null, null,
166 propertyDefinitionMap.put("gateway_ip", DataModelUtil
167 .createPropertyDefinition(PropertyType.STRING.getDisplayName(), null, false, null, null,
169 propertyDefinitionMap.put("network_name", DataModelUtil
170 .createPropertyDefinition(PropertyType.STRING.getDisplayName(), null, false, null, null,
172 propertyDefinitionMap.put("network_id", DataModelUtil
173 .createPropertyDefinition(PropertyType.STRING.getDisplayName(), null, false, null, null,
175 propertyDefinitionMap.put("segmentation_id", DataModelUtil
176 .createPropertyDefinition(PropertyType.STRING.getDisplayName(), null, false, null, null,
178 propertyDefinitionMap.put("network_type", DataModelUtil
179 .createPropertyDefinition(PropertyType.STRING.getDisplayName(), null, false, null, null,
181 propertyDefinitionMap.put("physical_network", DataModelUtil
182 .createPropertyDefinition(PropertyType.STRING.getDisplayName(), null, false, null, null,
184 propertyDefinitionMap.put("dhcp_enabled", DataModelUtil
185 .createPropertyDefinition(PropertyType.BOOLEAN.getDisplayName(), null, false, null, null,
188 return propertyDefinitionMap;
191 private static Map<String, InterfaceDefinition> createRootInterfaces() {
192 return new HashMap<>();
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;
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;
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;
225 private static List<String> createValidSourceTypes(String... validSourceTypes) {
226 return Arrays.asList(validSourceTypes);
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;
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;
249 private static List<Map<String, RequirementDefinition>> createToscaNodesPortRequirements() {
250 List<Map<String, RequirementDefinition>> requirements = new ArrayList<>();
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);
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);
270 private static Object[] createOccurrences(Object min, Object max) {
271 return new Object[]{min, max};
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,
280 computeAttributesDefMap.put("public_address", DataModelUtil
281 .createAttributeDefinition(PropertyType.STRING.getDisplayName(), "public_address", null,
283 computeAttributesDefMap.put("networks", DataModelUtil
284 .createAttributeDefinition(PropertyType.MAP.getDisplayName(), "networks", null,
286 .createEntrySchema(ToscaDataType.NETWORK_NETWORK_INFO.getDisplayName(), null, null),
288 computeAttributesDefMap.put("ports", DataModelUtil
289 .createAttributeDefinition(PropertyType.MAP.getDisplayName(), "ports", null, DataModelUtil
290 .createEntrySchema(ToscaDataType.NETWORK_PORT_INFO.getDisplayName(), null, null),
292 return computeAttributesDefMap;
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,
300 attributesDefMap.put("tosca_name", DataModelUtil
301 .createAttributeDefinition(PropertyType.STRING.getDisplayName(), "tosca name", null, null,
303 attributesDefMap.put("state", DataModelUtil
304 .createAttributeDefinition(PropertyType.STRING.getDisplayName(), "state", null, null,
306 return attributesDefMap;