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.translator.services.heattotosca.globaltypes;
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 import org.openecomp.sdc.tosca.datatypes.model.AttributeDefinition;
28 import org.openecomp.sdc.tosca.datatypes.model.Constraint;
29 import org.openecomp.sdc.tosca.datatypes.model.DataType;
30 import org.openecomp.sdc.tosca.datatypes.model.NodeType;
31 import org.openecomp.sdc.tosca.datatypes.model.PropertyDefinition;
32 import org.openecomp.sdc.tosca.datatypes.model.PropertyType;
33 import org.openecomp.sdc.tosca.datatypes.model.RequirementDefinition;
34 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
35 import org.openecomp.sdc.tosca.services.DataModelUtil;
36 import org.openecomp.sdc.tosca.services.ToscaConstants;
37 import org.openecomp.sdc.translator.services.heattotosca.Constants;
39 import java.util.ArrayList;
40 import java.util.HashMap;
41 import java.util.List;
44 public class NeutronSecurityRulesGlobalType {
46 * Create service template service template.
48 * @return the service template
50 public static ServiceTemplate createServiceTemplate() {
51 ServiceTemplate neutronSecurityRulesServiceTemplate = new ServiceTemplate();
52 neutronSecurityRulesServiceTemplate
53 .setTosca_definitions_version(ToscaConstants.TOSCA_DEFINITIONS_VERSION);
54 neutronSecurityRulesServiceTemplate.setMetadata(DataModelUtil
55 .createMetadata(Constants.NEUTRON_SECURITY_RULES_TEMPLATE_NAME, "1.0.0", null));
56 neutronSecurityRulesServiceTemplate.setImports(GlobalTypesUtil.createCommonImportList());
57 neutronSecurityRulesServiceTemplate.setDescription("Neutron Security Rules TOSCA Global Types");
58 neutronSecurityRulesServiceTemplate.setData_types(createGlobalDataTypes());
59 neutronSecurityRulesServiceTemplate.setNode_types(createGlobalNodeTypes());
60 return neutronSecurityRulesServiceTemplate;
64 private static Map<String, DataType> createGlobalDataTypes() {
65 Map<String, DataType> globalDataTypes = new HashMap<>();
66 globalDataTypes.put(ToscaDataType.NEUTRON_SECURITY_RULES_RULE.getDisplayName(),
67 createSecurityRulesDataType());
68 return globalDataTypes;
71 private static DataType createSecurityRulesDataType() {
72 DataType addressPairDataType = new DataType();
73 addressPairDataType.setDerived_from(ToscaDataType.ROOT.getDisplayName());
74 addressPairDataType.setDescription("Rules Pairs");
75 Map<String, PropertyDefinition> addressPairProp = new HashMap<>();
77 addressPairProp.put("direction", DataModelUtil
78 .createPropertyDefinition(PropertyType.STRING.getDisplayName(),
79 "The direction in which the security group rule is applied", false,
80 DataModelUtil.createValidValuesConstraintsList("egress", "ingress"), null, null,
82 addressPairProp.put("ethertype", DataModelUtil
83 .createPropertyDefinition(PropertyType.STRING.getDisplayName(), "Ethertype of the traffic",
84 false, DataModelUtil.createValidValuesConstraintsList("IPv4", "IPv6"), null, null,
86 Constraint portRangeMaxConstraint = new Constraint();
87 portRangeMaxConstraint.setIn_range(new Integer[]{0, 65535});
88 addressPairProp.put("port_range_max", DataModelUtil
89 .createPropertyDefinition(PropertyType.INTEGER.getDisplayName(),
90 "The maximum port number in the range that is matched by the security group rule. ",
91 false, DataModelUtil.getConstrainList(portRangeMaxConstraint), null, null, null));
92 Constraint portRangeMinConstraint = new Constraint();
93 portRangeMinConstraint.setIn_range(new Integer[]{0, 65535});
94 addressPairProp.put("port_range_min", DataModelUtil
95 .createPropertyDefinition(PropertyType.INTEGER.getDisplayName(),
96 "The minimum port number in the range that is matched by the security group rule.",
97 false, DataModelUtil.getConstrainList(portRangeMinConstraint), null, null, null));
98 addressPairProp.put("protocol", DataModelUtil
99 .createPropertyDefinition(PropertyType.STRING.getDisplayName(),
100 "The protocol that is matched by the security group rule", false,
101 DataModelUtil.createValidValuesConstraintsList("tcp", "udp", "icmp"), null, null,
103 addressPairProp.put("remote_group_id", DataModelUtil
104 .createPropertyDefinition(PropertyType.STRING.getDisplayName(),
105 "The remote group ID to be associated with this security group rule", false, null, null,
107 addressPairProp.put("remote_ip_prefix", DataModelUtil
108 .createPropertyDefinition(PropertyType.STRING.getDisplayName(),
109 "The remote IP prefix (CIDR) to be associated with this security group rule", false,
110 null, null, null, null));
111 addressPairProp.put("remote_mode", DataModelUtil
112 .createPropertyDefinition(PropertyType.STRING.getDisplayName(),
113 "Whether to specify a remote group or a remote IP prefix", false,
114 DataModelUtil.createValidValuesConstraintsList("remote_ip_prefix", "remote_group_id"),
115 null, null, "remote_ip_prefix"));
116 addressPairDataType.setProperties(addressPairProp);
118 return addressPairDataType;
121 private static Map<String, NodeType> createGlobalNodeTypes() {
122 Map<String, NodeType> globalNodeTypes = new HashMap<>();
123 globalNodeTypes.put(ToscaNodeType.NEUTRON_SECURITY_RULES.getDisplayName(),
124 createNeutronSecurityRulesNodeType());
125 return globalNodeTypes;
128 private static NodeType createNeutronSecurityRulesNodeType() {
129 NodeType novaServerNodeType = new NodeType();
130 novaServerNodeType.setDerived_from(ToscaNodeType.ROOT.getDisplayName());
131 novaServerNodeType.setProperties(createNeutronSecurityRulesProperties());
132 novaServerNodeType.setAttributes(createNeutronSecurityRulesAttributes());
133 novaServerNodeType.setRequirements(createNeutronSecurityRequirements());
134 return novaServerNodeType;
137 private static List<Map<String, RequirementDefinition>> createNeutronSecurityRequirements() {
138 final List<Map<String, RequirementDefinition>> requirements = new ArrayList<>();
139 final Map<String, RequirementDefinition> portRequirement = new HashMap<>();
140 RequirementDefinition requirementDefinition = new RequirementDefinition();
141 requirementDefinition.setCapability(ToscaCapabilityType.ATTACHMENT.getDisplayName());
142 requirementDefinition.setNode(ToscaNodeType.NEUTRON_PORT.getDisplayName());
143 requirementDefinition.setRelationship(ToscaRelationshipType.ATTACHES_TO.getDisplayName());
144 requirementDefinition.setOccurrences(new Object[]{0, ToscaConstants.UNBOUNDED});
145 portRequirement.put(ToscaConstants.PORT_REQUIREMENT_ID, requirementDefinition);
146 requirements.add(portRequirement);
151 private static Map<String, PropertyDefinition> createNeutronSecurityRulesProperties() {
152 Map<String, PropertyDefinition> neutronSecurityRulesPropertyDefMap = new HashMap<>();
153 neutronSecurityRulesPropertyDefMap.put(Constants.NAME_PROPERTY_NAME, DataModelUtil
154 .createPropertyDefinition(PropertyType.STRING.getDisplayName(),
155 "A symbolic name for this security group, which is not required to be unique.", false,
156 null, null, null, null));
157 neutronSecurityRulesPropertyDefMap.put(Constants.DESCRIPTION_PROPERTY_NAME, DataModelUtil
158 .createPropertyDefinition(PropertyType.STRING.getDisplayName(),
159 "Description of the security group", false, null, null, null, null));
160 neutronSecurityRulesPropertyDefMap.put(Constants.RULES_PROPERTY_NAME, DataModelUtil
161 .createPropertyDefinition(PropertyType.LIST.getDisplayName(),
162 "List of security group rules", false, null, null, DataModelUtil
163 .createEntrySchema(ToscaDataType.NEUTRON_SECURITY_RULES_RULE.getDisplayName(), null,
165 return neutronSecurityRulesPropertyDefMap;
168 private static Map<String, AttributeDefinition> createNeutronSecurityRulesAttributes() {
169 Map<String, AttributeDefinition> neutronSecurityRulesAttributesDefMap = new HashMap<>();
170 neutronSecurityRulesAttributesDefMap.put("show", DataModelUtil
171 .createAttributeDefinition(PropertyType.STRING.getDisplayName(),
172 "Detailed information about resource", null, null, null));
173 return neutronSecurityRulesAttributesDefMap;