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 / ContrailV2NetworkRuleGlobalType.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.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.DataType;
29 import org.openecomp.sdc.tosca.datatypes.model.NodeType;
30 import org.openecomp.sdc.tosca.datatypes.model.PropertyDefinition;
31 import org.openecomp.sdc.tosca.datatypes.model.PropertyType;
32 import org.openecomp.sdc.tosca.datatypes.model.RequirementDefinition;
33 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
34 import org.openecomp.sdc.tosca.services.DataModelUtil;
35 import org.openecomp.sdc.tosca.services.ToscaConstants;
36 import org.openecomp.sdc.translator.services.heattotosca.Constants;
37
38 import java.util.ArrayList;
39 import java.util.HashMap;
40 import java.util.List;
41 import java.util.Map;
42
43 public class ContrailV2NetworkRuleGlobalType {
44   /**
45    * Create service template service template.
46    *
47    * @return the service template
48    */
49   public static ServiceTemplate createServiceTemplate() {
50     ServiceTemplate contrailNetworkRuleServiceTemplate = new ServiceTemplate();
51     contrailNetworkRuleServiceTemplate
52         .setTosca_definitions_version(ToscaConstants.TOSCA_DEFINITIONS_VERSION);
53     contrailNetworkRuleServiceTemplate.setMetadata(
54         DataModelUtil.createMetadata(Constants
55                 .CONTRAILV2_NETWORK_RULE_TEMPLATE_NAME, "1.0.0", null));
56     contrailNetworkRuleServiceTemplate.setImports(GlobalTypesUtil.createCommonImportList());
57     contrailNetworkRuleServiceTemplate.setDescription("Contrail V2 Network Rule Global Types");
58     contrailNetworkRuleServiceTemplate.setData_types(createGlobalDataTypes());
59     contrailNetworkRuleServiceTemplate.setNode_types(createGlobalNodeTypes());
60     return contrailNetworkRuleServiceTemplate;
61   }
62
63
64   private static Map<String, DataType> createGlobalDataTypes() {
65     Map<String, DataType> globalDataTypes = new HashMap<>();
66     globalDataTypes.put(ToscaDataType.CONTRAILV2_NETWORK_RULE_SRC_PORT_PAIRS.getDisplayName(),
67         createRuleSrcPortPairsDataType());
68     globalDataTypes.put(ToscaDataType.CONTRAILV2_NETWORK_RULE_DST_PORT_PAIRS.getDisplayName(),
69         createRuleDstPortPairsDataType());
70     globalDataTypes
71         .put(ToscaDataType.CONTRAILV2_NETWORK_RULE.getDisplayName(), createRuleDataType());
72     globalDataTypes.put(ToscaDataType.CONTRAILV2_NETWORK_RULE_DST_VIRTUAL_NETWORK.getDisplayName(),
73         createRuleDstVirtualNetworkDataType());
74     globalDataTypes.put(ToscaDataType.CONTRAILV2_NETWORK_RULE_SRC_VIRTUAL_NETWORK.getDisplayName(),
75         createRuleSrcVirtualNetworkDataType());
76     globalDataTypes.put(ToscaDataType.CONTRAILV2_NETWORK_RULE_LIST.getDisplayName(),
77         createPolicyRulesListDataType());
78     globalDataTypes.put(ToscaDataType.CONTRAILV2_NETWORK_RULE_ACTION_LIST.getDisplayName(),
79         createRuleActionListDataType());
80     return globalDataTypes;
81   }
82
83   private static DataType createRuleDataType() {
84     DataType dataType = new DataType();
85     dataType.setDerived_from(ToscaDataType.ROOT.getDisplayName());
86     dataType.setDescription("policy rule");
87     Map<String, PropertyDefinition> properties = new HashMap<>();
88     properties.put("network_policy_entries_policy_rule_direction", DataModelUtil
89         .createPropertyDefinition(PropertyType.STRING.getDisplayName(), "Direction", false, null,
90             null, null, null));
91     properties.put("network_policy_entries_policy_rule_protocol", DataModelUtil
92         .createPropertyDefinition(PropertyType.STRING.getDisplayName(), "Protocol", false, null,
93             null, null, null));
94     properties.put("network_policy_entries_policy_rule_src_ports", DataModelUtil
95         .createPropertyDefinition(PropertyType.LIST.getDisplayName(), "Source ports", false, null,
96             null, DataModelUtil.createEntrySchema(
97                 ToscaDataType.CONTRAILV2_NETWORK_RULE_SRC_PORT_PAIRS.getDisplayName(), null, null),
98             null));
99     properties.put("network_policy_entries_policy_rule_dst_ports", DataModelUtil
100         .createPropertyDefinition(PropertyType.LIST.getDisplayName(), "Destination ports", false,
101             null, null, DataModelUtil.createEntrySchema(
102                 ToscaDataType.CONTRAILV2_NETWORK_RULE_DST_PORT_PAIRS.getDisplayName(), null, null),
103             null));
104     properties.put("network_policy_entries_policy_rule_dst_addresses", DataModelUtil
105         .createPropertyDefinition(PropertyType.LIST.getDisplayName(), "Destination addresses",
106             false, null, null, DataModelUtil.createEntrySchema(
107                 ToscaDataType.CONTRAILV2_NETWORK_RULE_DST_VIRTUAL_NETWORK.getDisplayName(), null,
108                 null), null));
109     properties.put("network_policy_entries_policy_rule_src_addresses", DataModelUtil
110         .createPropertyDefinition(PropertyType.LIST.getDisplayName(), "Source addresses", false,
111             null, null, DataModelUtil.createEntrySchema(
112                 ToscaDataType.CONTRAILV2_NETWORK_RULE_SRC_VIRTUAL_NETWORK.getDisplayName(), null,
113                 null), null));
114     properties.put("network_policy_entries_policy_rule_action_list", DataModelUtil
115         .createPropertyDefinition(
116             ToscaDataType.CONTRAILV2_NETWORK_RULE_ACTION_LIST.getDisplayName(), "Action list",
117             false, null, null, null, null));
118
119     dataType.setProperties(properties);
120     return dataType;
121
122   }
123
124
125   private static DataType createRuleDstVirtualNetworkDataType() {
126     DataType dataType = new DataType();
127     dataType.setDerived_from(ToscaDataType.ROOT.getDisplayName());
128     dataType.setDescription("destination addresses");
129     Map<String, PropertyDefinition> properties = new HashMap<>();
130
131     properties.put("network_policy_entries_policy_rule_dst_addresses_virtual_network", DataModelUtil
132         .createPropertyDefinition(PropertyType.STRING.getDisplayName(),
133             "Destination addresses Virtual network", false, null, null, null, null));
134
135     dataType.setProperties(properties);
136     return dataType;
137   }
138
139   private static DataType createRuleSrcVirtualNetworkDataType() {
140     DataType dataType = new DataType();
141     dataType.setDerived_from(ToscaDataType.ROOT.getDisplayName());
142     dataType.setDescription("source addresses");
143     Map<String, PropertyDefinition> properties = new HashMap<>();
144
145     properties.put("network_policy_entries_policy_rule_src_addresses_virtual_network", DataModelUtil
146         .createPropertyDefinition(PropertyType.STRING.getDisplayName(),
147             "Source addresses Virtual network", false, null, null, null, null));
148
149     dataType.setProperties(properties);
150     return dataType;
151   }
152
153   private static DataType createPolicyRulesListDataType() {
154     DataType dataType = new DataType();
155     dataType.setDerived_from(ToscaDataType.ROOT.getDisplayName());
156     dataType.setDescription("list of policy rules");
157     Map<String, PropertyDefinition> properties = new HashMap<>();
158
159     properties.put("network_policy_entries_policy_rule", DataModelUtil
160         .createPropertyDefinition(PropertyType.LIST.getDisplayName(), "Contrail network rule",
161             false, null, null, DataModelUtil
162                 .createEntrySchema(ToscaDataType.CONTRAILV2_NETWORK_RULE.getDisplayName(), null,
163                     null), null));
164
165     dataType.setProperties(properties);
166     return dataType;
167   }
168
169   private static DataType createRuleActionListDataType() {
170     DataType dataType = new DataType();
171     dataType.setDerived_from(ToscaDataType.ROOT.getDisplayName());
172     dataType.setDescription("Action List");
173     Map<String, PropertyDefinition> properties = new HashMap<>();
174
175     properties.put("network_policy_entries_policy_rule_action_list_simple_action", DataModelUtil
176         .createPropertyDefinition(PropertyType.STRING.getDisplayName(), "Simple Action", false,
177             null, null, null, null));
178     properties.put("network_policy_entries_policy_rule_action_list_apply_service", DataModelUtil
179         .createPropertyDefinition(PropertyType.LIST.getDisplayName(), "Apply Service", false, null,
180             null, DataModelUtil.createEntrySchema(PropertyType.STRING.getDisplayName(), null, null),
181             null));
182
183     dataType.setProperties(properties);
184     return dataType;
185   }
186
187   private static DataType createRuleDstPortPairsDataType() {
188     DataType dataType = new DataType();
189     dataType.setDerived_from(ToscaDataType.ROOT.getDisplayName());
190     dataType.setDescription("destination port pairs");
191     Map<String, PropertyDefinition> properties = new HashMap<>();
192
193     properties.put("network_policy_entries_policy_rule_dst_ports_start_port", DataModelUtil
194         .createPropertyDefinition(PropertyType.STRING.getDisplayName(), "Start port", false, null,
195             null, null, null));
196     properties.put("network_policy_entries_policy_rule_dst_ports_end_port", DataModelUtil
197         .createPropertyDefinition(PropertyType.STRING.getDisplayName(), "End port", false, null,
198             null, null, null));
199
200     dataType.setProperties(properties);
201     return dataType;
202   }
203
204   private static DataType createRuleSrcPortPairsDataType() {
205     DataType dataType = new DataType();
206     dataType.setDerived_from(ToscaDataType.ROOT.getDisplayName());
207     dataType.setDescription("source port pairs");
208     Map<String, PropertyDefinition> properties = new HashMap<>();
209
210     properties.put("network_policy_entries_policy_rule_src_ports_start_port", DataModelUtil
211         .createPropertyDefinition(PropertyType.STRING.getDisplayName(), "Start port", false, null,
212             null, null, null));
213     properties.put("network_policy_entries_policy_rule_src_ports_end_port", DataModelUtil
214         .createPropertyDefinition(PropertyType.STRING.getDisplayName(), "End port", false, null,
215             null, null, null));
216
217     dataType.setProperties(properties);
218     return dataType;
219   }
220
221   private static Map<String, NodeType> createGlobalNodeTypes() {
222     Map<String, NodeType> globalNodeTypes = new HashMap<>();
223     globalNodeTypes.put(ToscaNodeType.CONTRAILV2_NETWORK_RULE.getDisplayName(),
224         createContrailV2NetworkRuleNodeType());
225     return globalNodeTypes;
226   }
227
228   private static NodeType createContrailV2NetworkRuleNodeType() {
229     NodeType contrailNetworkRuleNodeType = new NodeType();
230     contrailNetworkRuleNodeType.setDerived_from(ToscaNodeType.ROOT.getDisplayName());
231     contrailNetworkRuleNodeType.setProperties(createContrailNetworkRuleProperties());
232     contrailNetworkRuleNodeType.setAttributes(createContrailNetworkRuleAttributes());
233     contrailNetworkRuleNodeType.setRequirements(createContrailNetworkRuleRequirements());
234     return contrailNetworkRuleNodeType;
235   }
236
237
238   private static Map<String, PropertyDefinition> createContrailNetworkRuleProperties() {
239     Map<String, PropertyDefinition> contrailNetworkRulePropertyDefMap = new HashMap<>();
240     contrailNetworkRulePropertyDefMap.put(Constants.NAME_PROPERTY_NAME, DataModelUtil
241         .createPropertyDefinition(PropertyType.STRING.getDisplayName(),
242             "A symbolic name for this contrail v2 network rule", false, null, null, null, null));
243     contrailNetworkRulePropertyDefMap.put("network_policy_entries", DataModelUtil
244         .createPropertyDefinition(ToscaDataType.CONTRAILV2_NETWORK_RULE_LIST.getDisplayName(),
245             "A symbolic name for this contrail v2 network rule", false, null, null, null, null));
246
247     return contrailNetworkRulePropertyDefMap;
248   }
249
250
251   private static Map<String, AttributeDefinition> createContrailNetworkRuleAttributes() {
252     Map<String, AttributeDefinition> contrailNetworkRuleAttributesDefMap = new HashMap<>();
253     contrailNetworkRuleAttributesDefMap.put("fq_name", DataModelUtil
254         .createAttributeDefinition(PropertyType.STRING.getDisplayName(), "fq_name", null, null,
255             null));
256
257     return contrailNetworkRuleAttributesDefMap;
258   }
259
260   private static List<Map<String, RequirementDefinition>> createContrailNetworkRuleRequirements() {
261     final List<Map<String, RequirementDefinition>> requirements = new ArrayList<>();
262     final Map<String, RequirementDefinition>
263             contrailNetworkRuleRequirementsDefMap = new HashMap<>();
264     RequirementDefinition req = new RequirementDefinition();
265     req.setCapability(ToscaCapabilityType.ATTACHMENT.getDisplayName());
266     req.setOccurrences(new Object[]{0, ToscaConstants.UNBOUNDED});
267     req.setNode(ToscaNodeType.NETWORK.getDisplayName());
268     req.setRelationship(ToscaRelationshipType.ATTACHES_TO.getDisplayName());
269     contrailNetworkRuleRequirementsDefMap.put("network", req);
270     requirements.add(contrailNetworkRuleRequirementsDefMap);
271
272     return requirements;
273   }
274 }