push addional code
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / main / java / org / openecomp / sdc / translator / services / heattotosca / globaltypes / NovaServerGlobalType.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.translator.services.heattotosca.globaltypes;
22
23 import org.openecomp.sdc.tosca.datatypes.ToscaDataType;
24 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
25 import org.openecomp.sdc.tosca.datatypes.model.AttributeDefinition;
26 import org.openecomp.sdc.tosca.datatypes.model.Constraint;
27 import org.openecomp.sdc.tosca.datatypes.model.DataType;
28 import org.openecomp.sdc.tosca.datatypes.model.NodeType;
29 import org.openecomp.sdc.tosca.datatypes.model.PropertyDefinition;
30 import org.openecomp.sdc.tosca.datatypes.model.PropertyType;
31 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
32 import org.openecomp.sdc.tosca.datatypes.model.heatextend.PropertyTypeExt;
33 import org.openecomp.sdc.tosca.services.DataModelUtil;
34 import org.openecomp.sdc.tosca.services.ToscaConstants;
35 import org.openecomp.sdc.translator.services.heattotosca.Constants;
36
37 import java.util.ArrayList;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Map;
41
42 public class NovaServerGlobalType {
43
44   /**
45    * Create service template service template.
46    *
47    * @return the service template
48    */
49   public static ServiceTemplate createServiceTemplate() {
50     ServiceTemplate novaeServerServiceTemplate = new ServiceTemplate();
51     novaeServerServiceTemplate
52         .setTosca_definitions_version(ToscaConstants.TOSCA_DEFINITIONS_VERSION);
53     novaeServerServiceTemplate.setMetadata(
54         DataModelUtil.createMetadata(Constants.NOVA_SERVER_TEMPLATE_NAME, "1.0.0", null));
55     novaeServerServiceTemplate.setDescription("Nova Server TOSCA Global Types");
56     novaeServerServiceTemplate.setImports(GlobalTypesUtil.createCommonImportList());
57     novaeServerServiceTemplate.setData_types(createGlobalDataTypes());
58     novaeServerServiceTemplate.setNode_types(createGlobalNodeTypes());
59     return novaeServerServiceTemplate;
60   }
61
62
63   private static Map<String, NodeType> createGlobalNodeTypes() {
64     Map<String, NodeType> globalNodeTypes = new HashMap<>();
65     globalNodeTypes.put(ToscaNodeType.NOVA_SERVER.getDisplayName(), createNovaServerNodeType());
66     return globalNodeTypes;
67   }
68
69   private static NodeType createNovaServerNodeType() {
70     NodeType novaServerNodeType = new NodeType();
71     novaServerNodeType.setDerived_from(ToscaNodeType.COMPUTE.getDisplayName());
72     novaServerNodeType.setProperties(createNovaServerProperties());
73     novaServerNodeType.setAttributes(createNovaServerAttributes());
74     return novaServerNodeType;
75   }
76
77
78   private static Map<String, PropertyDefinition> createNovaServerProperties() {
79     Map<String, PropertyDefinition> novaServerPropertyDefMap = new HashMap<>();
80     novaServerPropertyDefMap.put("flavor", DataModelUtil
81         .createPropertyDefinition(PropertyType.STRING.getDisplayName(),
82             "The ID or name of the flavor to boot onto", true, null, null, null, null));
83     novaServerPropertyDefMap.put("admin_pass", DataModelUtil
84         .createPropertyDefinition(PropertyType.STRING.getDisplayName(),
85             "The administrator password for the server", false, null, null, null, null));
86     novaServerPropertyDefMap.put("availability_zone", DataModelUtil
87         .createPropertyDefinition(PropertyType.STRING.getDisplayName(),
88             "Availability zone to create servers in", false, null, null, null, null));
89     novaServerPropertyDefMap.put("config_drive", DataModelUtil
90         .createPropertyDefinition(PropertyType.BOOLEAN.getDisplayName(),
91             "enable config drive on the server", false, null, null, null, null));
92     novaServerPropertyDefMap.put("contrail_service_instance_ind", DataModelUtil
93         .createPropertyDefinition(PropertyType.BOOLEAN.getDisplayName(),
94             "Nova server related to service instance indicator", false, null, null, null, false));
95     novaServerPropertyDefMap.put("diskConfig", DataModelUtil
96         .createPropertyDefinition(PropertyType.STRING.getDisplayName(),
97             "Control how the disk is partitioned when the server is created", false,
98             getDiskConfigConstraints(), null, null, null));
99     novaServerPropertyDefMap.put("flavor_update_policy", DataModelUtil
100         .createPropertyDefinition(PropertyType.STRING.getDisplayName(),
101             "Policy on how to apply a flavor update", false, getFlavorUpdatePolicyConstraints(),
102             null, null, "RESIZE"));
103     novaServerPropertyDefMap.put("image", DataModelUtil
104         .createPropertyDefinition(PropertyType.STRING.getDisplayName(),
105             "The ID or name of the image to boot with", false, null, null, null, null));
106     novaServerPropertyDefMap.put("image_update_policy", DataModelUtil
107         .createPropertyDefinition(PropertyType.STRING.getDisplayName(),
108             "Policy on how to apply an image-id update", false, getImageUpdatePolicyConstraints(),
109             null, null, "REBUILD"));
110     novaServerPropertyDefMap.put("key_name", DataModelUtil
111         .createPropertyDefinition(PropertyType.STRING.getDisplayName(),
112             "Name of keypair to inject into the server", false, null, null, null, null));
113     novaServerPropertyDefMap.put("metadata", DataModelUtil
114         .createPropertyDefinition(PropertyTypeExt.JSON.getDisplayName(),
115             "Arbitrary JSON metadata to store for this server", false, null, null, null, null));
116     novaServerPropertyDefMap.put(Constants.NAME_PROPERTY_NAME, DataModelUtil
117         .createPropertyDefinition(PropertyType.STRING.getDisplayName(), "Server name", false, null,
118             null, null, null));
119     novaServerPropertyDefMap.put("personality", DataModelUtil
120         .createPropertyDefinition(PropertyType.MAP.getDisplayName(),
121             "A map of files to create/overwrite on the server upon boot", false, null, null,
122             DataModelUtil.createEntrySchema(PropertyType.STRING.getDisplayName(), null, null),
123             new HashMap<String, String>()));
124     novaServerPropertyDefMap.put("reservation_id", DataModelUtil
125         .createPropertyDefinition(PropertyType.STRING.getDisplayName(),
126             "A UUID for the set of servers being requested", false, null, null, null, null));
127     novaServerPropertyDefMap.put("scheduler_hints", DataModelUtil
128         .createPropertyDefinition(PropertyType.MAP.getDisplayName(),
129             "Arbitrary key-value pairs specified by the client to help boot a server", false, null,
130             null, DataModelUtil.createEntrySchema(PropertyType.STRING.getDisplayName(), null, null),
131             null));
132     novaServerPropertyDefMap.put(Constants.SECURITY_GROUPS_PROPERTY_NAME, DataModelUtil
133         .createPropertyDefinition(PropertyType.LIST.getDisplayName(),
134             "List of security group names or IDs", false, null, null,
135             DataModelUtil.createEntrySchema(PropertyType.STRING.getDisplayName(), null, null),
136             new ArrayList<>()));
137     novaServerPropertyDefMap.put("software_config_transport", DataModelUtil
138         .createPropertyDefinition(PropertyType.STRING.getDisplayName(),
139             "How the server should receive the metadata required for software configuration", false,
140             getSoftwareConfigTransportConstraints(), null, null, "POLL_SERVER_CFN"));
141     novaServerPropertyDefMap.put("user_data", DataModelUtil
142         .createPropertyDefinition(PropertyType.STRING.getDisplayName(),
143             "User data script to be executed by cloud-init", false, null, null, null, ""));
144     novaServerPropertyDefMap.put("user_data_format", DataModelUtil
145         .createPropertyDefinition(PropertyType.STRING.getDisplayName(),
146             "How the user_data should be formatted for the server", false,
147             getUserDataFormatConstraint(), null, null, "HEAT_CFNTOOLS"));
148     novaServerPropertyDefMap.put("user_data_update_policy", DataModelUtil
149         .createPropertyDefinition(PropertyType.STRING.getDisplayName(),
150             "Policy on how to apply a user_data update", false,
151             getUserDataUpdatePolicyConstraints(), null, null, "REPLACE"));
152     return novaServerPropertyDefMap;
153   }
154
155   private static Map<String, AttributeDefinition> createNovaServerAttributes() {
156     Map<String, AttributeDefinition> novaServerAttributesDefMap = new HashMap<>();
157     novaServerAttributesDefMap.put("accessIPv4", DataModelUtil
158         .createAttributeDefinition(PropertyType.STRING.getDisplayName(),
159             "The manually assigned alternative public IPv4 address of the server", null, null,
160             null));
161     novaServerAttributesDefMap.put("accessIPv6", DataModelUtil
162         .createAttributeDefinition(PropertyType.STRING.getDisplayName(),
163             "The manually assigned alternative public IPv6 address of the server", null, null,
164             null));
165     novaServerAttributesDefMap.put("addresses", DataModelUtil
166         .createAttributeDefinition(PropertyType.MAP.getDisplayName(),
167             "A dict of all network addresses with corresponding port_id", null, DataModelUtil
168                 .createEntrySchema(ToscaDataType.NOVA_SERVER_NETWORK_ADDRESS_INFO.getDisplayName(),
169                     null, null), null));
170     novaServerAttributesDefMap.put("console_urls", DataModelUtil
171         .createAttributeDefinition(PropertyType.STRING.getDisplayName(), "URLs of servers consoles",
172             null, null, null));
173     novaServerAttributesDefMap.put("instance_name", DataModelUtil
174         .createAttributeDefinition(PropertyType.STRING.getDisplayName(),
175             "AWS compatible instance name", null, null, null));
176     novaServerAttributesDefMap.put(Constants.NAME_PROPERTY_NAME, DataModelUtil
177         .createAttributeDefinition(PropertyType.STRING.getDisplayName(), "Name of the server", null,
178             null, null));
179     novaServerAttributesDefMap.put("show", DataModelUtil
180         .createAttributeDefinition(PropertyType.STRING.getDisplayName(),
181             "Detailed information about resource", null, null, null));
182     return novaServerAttributesDefMap;
183   }
184
185   private static List<Constraint> getUserDataFormatConstraint() {
186     List<Constraint> constraints;
187     Constraint validValues;
188     constraints = new ArrayList<>();
189     validValues =
190         DataModelUtil.createValidValuesConstraint("SOFTWARE_CONFIG", "RAW", "HEAT_CFNTOOLS");
191     constraints.add(validValues);
192     return constraints;
193   }
194
195   private static List<Constraint> getUserDataUpdatePolicyConstraints() {
196     List<Constraint> constraints;
197     Constraint validValues;
198     constraints = new ArrayList<>();
199     validValues = DataModelUtil.createValidValuesConstraint("REPLACE", "IGNORE");
200     constraints.add(validValues);
201     return constraints;
202   }
203
204   private static List<Constraint> getSoftwareConfigTransportConstraints() {
205     List<Constraint> constraints;
206     Constraint validValues;
207     constraints = new ArrayList<>();
208     validValues = DataModelUtil
209         .createValidValuesConstraint("POLL_SERVER_CFN", "POLL_SERVER_HEAT", "POLL_TEMP_URL",
210             "ZAQAR_MESSAGE");
211     constraints.add(validValues);
212     return constraints;
213   }
214
215   private static List<Constraint> getImageUpdatePolicyConstraints() {
216     List<Constraint> constraints;
217     Constraint validValues;
218     constraints = new ArrayList<>();
219     validValues = DataModelUtil
220         .createValidValuesConstraint("REBUILD_PRESERVE_EPHEMERAL", "REPLACE", "REBUILD");
221     constraints.add(validValues);
222     return constraints;
223   }
224
225   private static List<Constraint> getFlavorUpdatePolicyConstraints() {
226     Constraint validValues;
227     List<Constraint> constraints = new ArrayList<>();
228     validValues = DataModelUtil.createValidValuesConstraint("RESIZE", "REPLACE");
229     constraints.add(validValues);
230     return constraints;
231   }
232
233   private static List<Constraint> getDiskConfigConstraints() {
234     List<Constraint> constraints = new ArrayList<>();
235     Constraint validValues = DataModelUtil.createValidValuesConstraint("AUTO", "MANUAL");
236     constraints.add(validValues);
237     return constraints;
238   }
239
240   private static Map<String, DataType> createGlobalDataTypes() {
241     Map<String, DataType> globalDataTypes = new HashMap<>();
242     globalDataTypes.put(ToscaDataType.NOVA_SERVER_PORT_EXTRA_PROPERTIES.getDisplayName(),
243         createPortExtraDataDataType());
244     globalDataTypes.put(ToscaDataType.NOVA_SERVER_NETWORK_ADDRESS_INFO.getDisplayName(),
245         createAddressInfoDataType());
246     return globalDataTypes;
247   }
248
249   private static DataType createAddressInfoDataType() {
250     DataType addressInfoDataType = new DataType();
251     addressInfoDataType.setDerived_from(ToscaDataType.NETWORK_NETWORK_INFO.getDisplayName());
252     addressInfoDataType.setDescription("Network addresses with corresponding port id");
253
254     Map<String, PropertyDefinition> addressInfoProp = new HashMap<>();
255     addressInfoProp.put("port_id", DataModelUtil
256         .createPropertyDefinition(PropertyType.STRING.getDisplayName(), "Port id", false, null,
257             null, null, null));
258     addressInfoDataType.setProperties(addressInfoProp);
259
260     return addressInfoDataType;
261   }
262
263
264   private static DataType createPortExtraDataDataType() {
265     DataType portExtraDataType = new DataType();
266     portExtraDataType.setDerived_from(ToscaDataType.ROOT.getDisplayName());
267     portExtraDataType.setDescription("Nova server network expand properties for port");
268     Map<String, PropertyDefinition> portExtraPropMap = new HashMap<>();
269
270     portExtraPropMap.put("admin_state_up", DataModelUtil
271         .createPropertyDefinition(PropertyType.BOOLEAN.getDisplayName(),
272             "The administrative state of this port", false, null, null, null, true));
273     portExtraPropMap.put("allowed_address_pairs", DataModelUtil
274         .createPropertyDefinition(PropertyType.LIST.getDisplayName(),
275             "Additional MAC/IP address pairs allowed to pass through the port", false, null, null,
276             DataModelUtil
277                 .createEntrySchema(ToscaDataType.NETWORK_ADDRESS_PAIR.getDisplayName(), null, null),
278             null));
279
280     List<Constraint> bindingVnicConstraints = new ArrayList<>();
281     Constraint validValues =
282         DataModelUtil.createValidValuesConstraint("macvtap", "direct", "normal");
283     bindingVnicConstraints.add(validValues);
284     portExtraPropMap.put("binding:vnic_type", DataModelUtil
285         .createPropertyDefinition(PropertyType.STRING.getDisplayName(),
286             "The vnic type to be bound on the neutron port", false, bindingVnicConstraints, null,
287             null, null));
288
289     portExtraPropMap.put("mac_address", DataModelUtil
290         .createPropertyDefinition(PropertyType.STRING.getDisplayName(),
291             "MAC address to give to this port", false, null, null, null, null));
292     portExtraPropMap.put("port_security_enabled", DataModelUtil
293         .createPropertyDefinition(PropertyType.BOOLEAN.getDisplayName(),
294             "Flag to enable/disable port security on the port", false, null, null, null, null));
295     portExtraPropMap.put("qos_policy", DataModelUtil
296         .createPropertyDefinition(PropertyType.STRING.getDisplayName(),
297             "The name or ID of QoS policy to attach to this port", false, null, null, null, null));
298     portExtraPropMap.put("value_specs", DataModelUtil
299         .createPropertyDefinition(PropertyType.MAP.getDisplayName(),
300             "Extra parameters to include in the request", false, null, null,
301             DataModelUtil.createEntrySchema(PropertyType.STRING.getDisplayName(), null, null),
302             new HashMap<String, String>()));
303     portExtraDataType.setProperties(portExtraPropMap);
304     return portExtraDataType;
305   }
306
307 }