]> review.fuel-infra Code Review - openstack-build/neutron-build.git/blob
8a6bd7d6e33e2749b3376b25c524d583ec7a1c02
[openstack-build/neutron-build.git] /
1 import groovy.transform.ToString\r
2 import groovy.transform.EqualsAndHashCode\r
3 \r
4 import com.radware.alteon.beans.adc.*;\r
5 import com.radware.alteon.api.*;\r
6 import com.radware.alteon.sdk.*\r
7 import com.radware.alteon.sdk.rpm.*\r
8 import com.radware.alteon.api.impl.AlteonCliUtils;\r
9 import com.radware.alteon.cli.CliSession;\r
10 \r
11 \r
12 @ToString(includeNames=true)\r
13 @EqualsAndHashCode(excludes=["gateway","mask","ips"])\r
14 class SubnetInfo {\r
15   String id\r
16   String gateway\r
17   String mask\r
18   String ips\r
19 }\r
20 \r
21 @ToString(includeNames=true)\r
22 @EqualsAndHashCode(excludes=["subnets"])\r
23 class PortInfo {\r
24   String name\r
25   def subnets = [:]\r
26 }\r
27 \r
28 \r
29 def tokenize_key(map_key) {\r
30     def ret_arr = map_key.tokenize(".")\r
31     if (ret_arr.size > 0 && ret_arr[0].startsWith("port")) {\r
32         return ret_arr\r
33     }\r
34     else\r
35         return null;\r
36 }\r
37 \r
38 \r
39 def parse(advanced_props) {\r
40     def ports = [:]\r
41     advanced_props.each {\r
42         key, value ->\r
43         def parsed_key = tokenize_key(key)\r
44         if (parsed_key) {\r
45             def port_name = parsed_key[0]\r
46             def subnet_id = parsed_key[1]\r
47             def property = parsed_key[2]\r
48             def port_info = ports.get(port_name)\r
49             if (port_info) {\r
50                 def subnet_info = port_info.subnets.get(subnet_id)\r
51                 if (subnet_info) {\r
52                     subnet_info[property] = value\r
53                 }\r
54                 else {\r
55                     subnet_info = new SubnetInfo(id:subnet_id)\r
56                     subnet_info[property] = value\r
57                     port_info.subnets.put(subnet_id, subnet_info)\r
58                 }\r
59             }\r
60             else {\r
61                 port_info = new PortInfo(name:port_name)\r
62                 subnet_info = new SubnetInfo(id:subnet_id)\r
63                 subnet_info[property] = value\r
64                 port_info.subnets.put(subnet_id, subnet_info)\r
65                 ports.put(port_name, port_info)\r
66             }\r
67         }\r
68     }\r
69     return ports\r
70 }\r
71 \r
72 def get_property_per_port (ports, port_name, property_name) {\r
73     port_info = ports[port_name]\r
74     if (port_info) {\r
75         port_subnet = port_info.subnets\r
76         if (port_subnet && !port_subnet.isEmpty()) {\r
77             port_subnet_item = port_subnet.values().iterator().next()\r
78             port_subnet_property = port_subnet_item[property_name]\r
79             if (port_subnet_property) {\r
80                 val_array = port_subnet_property.tokenize(",")\r
81                 if (!val_array.isEmpty())\r
82                     return val_array[0]\r
83             }\r
84         }\r
85     }\r
86     else {\r
87         return null\r
88     }\r
89 }\r
90 \r
91 def cidr_to_mask(cidr) throws NumberFormatException {\r
92 \r
93     String[] st = cidr.split("\\/");\r
94     if (st.length != 2) {\r
95         throw new NumberFormatException("Invalid CIDR format '"\r
96                 + cidr + "', should be: xx.xx.xx.xx/xx");\r
97     }\r
98     String symbolicIP = st[0];\r
99     String symbolicCIDR = st[1];\r
100 \r
101     Integer numericCIDR = new Integer(symbolicCIDR);\r
102     if (numericCIDR > 32) {\r
103         throw new NumberFormatException("CIDR can not be greater than 32");\r
104     }\r
105     //Get IP\r
106     st = symbolicIP.split("\\.");\r
107     if (st.length != 4) {\r
108         throw new NumberFormatException("Invalid IP address: " + symbolicIP);\r
109     }\r
110     int i = 24;\r
111     baseIPnumeric = 0;\r
112     for (int n = 0; n < st.length; n++) {\r
113         int value = Integer.parseInt(st[n]);\r
114         if (value != (value & 0xff)) {\r
115             throw new NumberFormatException("Invalid IP address: " + symbolicIP);\r
116         }\r
117         baseIPnumeric += value << i;\r
118         i -= 8;\r
119     }\r
120     //Get netmask\r
121     if (numericCIDR < 1)\r
122         throw new NumberFormatException("Netmask CIDR can not be less than 1");\r
123     netmaskNumeric = 0xffffffff;\r
124     netmaskNumeric = netmaskNumeric << (32 - numericCIDR);\r
125     return netmaskNumeric\r
126 }\r
127 \r
128 \r
129 def String convert_numeric_ip_to_symbolic(ip) {\r
130     StringBuffer sb = new StringBuffer(15);\r
131     for (int shift = 24; shift > 0; shift -= 8) {\r
132         // process 3 bytes, from high order byte down.\r
133         def tmp = (ip >>> shift) & 0xff\r
134         sb.append(tmp)\r
135         sb.append('.');\r
136     }\r
137     sb.append(ip & 0xff);\r
138     return sb.toString();\r
139 }\r
140 \r
141 \r
142 primary_adc = sdk.read(service.getPrimaryId())\r
143 primary_config = primary_adc.adcInfo.advancedConfiguration\r
144 primary_ports = parse(primary_config)\r
145 data_ip_address = get_property_per_port(primary_ports, "port1", "ips")\r
146 data_ip_mask = convert_numeric_ip_to_symbolic(cidr_to_mask(get_property_per_port(primary_ports, "port1", "mask")))\r
147 gateway = get_property_per_port(primary_ports, "port1", "gateway")\r
148 \r
149 if (service.request.ha) {\r
150     secondary_adc = sdk.read(service.getSecondaryId())\r
151     secondary_config = secondary_adc.adcInfo.advancedConfiguration\r
152     secondary_ports = parse(secondary_config)\r
153     ha_ip_address_1 = get_property_per_port(primary_ports, "port2", "ips")\r
154     ha_ip_address_2 = get_property_per_port(secondary_ports, "port2", "ips")\r
155     ha_vrrp_ip_address = ha_ip_address_1\r
156     ha_ip_mask = convert_numeric_ip_to_symbolic(cidr_to_mask(get_property_per_port(primary_ports, "port2", "mask")))\r
157 }\r
158 else {\r
159     secondary_adc = null\r
160     secondary_config = null\r
161     secondary_ports = null\r
162     ha_ip_address_1 = "1.1.1.1"\r
163     ha_ip_address_2 = "1.1.1.2"\r
164     ha_vrrp_ip_address = "1.1.1.3"\r
165     ha_ip_mask = "255.255.255.255"\r
166     ha_group_vr_id = 2\r
167 }\r
168 \r