Bug 4550 - always rebuild security group rule in security group
[neutron.git] / integration / test / src / test / java / org / opendaylight / neutron / e2etest / NeutronSecurityRuleTests.java
1 /*
2  * Copyright (C) 2015 IBM, Inc.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.neutron.e2etest;
10
11 import java.io.OutputStreamWriter;
12
13 import java.lang.Thread;
14
15 import java.net.HttpURLConnection;
16 import java.net.URL;
17
18 import org.junit.Assert;
19
20 import com.google.gson.Gson;
21 import com.google.gson.JsonArray;
22 import com.google.gson.JsonObject;
23
24 public class NeutronSecurityRuleTests {
25     String base;
26
27     public NeutronSecurityRuleTests(String base) {
28         this.base = base;
29     }
30
31     public void securityRule_collection_get_test() {
32         String url = base + "/security-group-rules";
33         ITNeutronE2E.test_fetch(url, "Security Rule Collection GET failed");
34     }
35
36     public String singleton_sr_create_test() {
37         String url = base + "/security-group-rules";
38         String content = " {\"security_group_rule\": " +
39             "{\"remote_group_id\": null, \"direction\": \"ingress\", " +
40             "\"remote_ip_prefix\": null, \"protocol\": \"tcp\", " +
41             "\"ethertype\": \"IPv6\", \"tenant_id\": " +
42             "\"00f340c7c3b34ab7be1fc690c05a0275\", \"port_range_max\": 77, " +
43             "\"port_range_min\": 77, " +
44             "\"id\": \"9b4be7fa-e56e-40fb-9516-1f0fa9185669\", " +
45             "\"security_group_id\": " +
46             "\"b60490fe-60a5-40be-af63-1d641381b784\"}}";
47         ITNeutronE2E.test_create(url, content, "Security Rule Singleton Post Failed");
48         return content;
49     }
50
51     public void singleton_sr_get_with_one_query_item_test(String createJsonString) {
52         String url = base + "/security-group-rules";
53         ITNeutronE2E.test_fetch_with_one_query_item(url, createJsonString, "security_group_rules");
54     }
55
56     public void multiple_sr_create_test() {
57         String url = base + "/security-group-rules";
58         String content = " {\"security_group_rules\": [" +
59                 "{" +
60                 "  \"id\": \"35fb0f34-c8d3-416d-a205-a2c75f7b8e22\"," +
61                 "  \"direction\": \"egress\"," +
62                 "  \"ethertype\": \"IPv6\"," +
63                 "  \"security_group_id\": \"70f1b157-e79b-44dc-85a8-7de0fc9f2aab\"," +
64                 "  \"tenant_id\": \"2640ee2ac2474bf3906e482047204fcb\"" +
65                 "}," +
66                 "{" +
67                 "  \"id\": \"63814eed-bc12-4fe4-8b17-2af178224c71\"," +
68                 "  \"direction\": \"egress\"," +
69                 "  \"ethertype\": \"IPv4\"," +
70                 "  \"security_group_id\": \"70f1b157-e79b-44dc-85a8-7de0fc9f2aab\"," +
71                 "  \"tenant_id\": \"2640ee2ac2474bf3906e482047204fcb\"" +
72                 "}," +
73                 "{" +
74                 "  \"id\": \"ccb9823e-559b-4d84-b656-2739f8e56d89\"," +
75                 "  \"direction\": \"ingress\"," +
76                 "  \"ethertype\": \"IPv6\"," +
77                 "  \"remote_group_id\": \"70f1b157-e79b-44dc-85a8-7de0fc9f2aab\"," +
78                 "  \"security_group_id\": \"70f1b157-e79b-44dc-85a8-7de0fc9f2aab\"," +
79                 "  \"tenant_id\": \"2640ee2ac2474bf3906e482047204fcb\"" +
80                 "}," +
81                 "{" +
82                 "  \"id\": \"fbc3f809-7378-40a4-822f-7a70f6ccba98\"," +
83                 "  \"direction\": \"ingress\"," +
84                 "  \"ethertype\": \"IPv4\"," +
85                 "  \"remote_group_id\": \"70f1b157-e79b-44dc-85a8-7de0fc9f2aab\"," +
86                 "  \"security_group_id\": \"70f1b157-e79b-44dc-85a8-7de0fc9f2aab\"," +
87                 "  \"tenant_id\": \"2640ee2ac2474bf3906e482047204fcb\"" +
88                 "}" +
89                 "]}";
90         ITNeutronE2E.test_create(url, content, "Security Rule Multiple Post Failed");
91     }
92
93     public void singleton_sr_modify_test() {
94         String url = base + "/security-group-rules/9b4be7fa-e56e-40fb-9516-1f0fa9185669";
95         String content = " {\"security_group_rule\": " +
96             "{\"remote_group_id\": null, \"direction\": \"egress\", " +
97             "\"remote_ip_prefix\": null, \"protocol\": \"tcp\", " +
98             "\"ethertype\": \"IPv6\", \"tenant_id\": " +
99             "\"00f340c7c3b34ab7be1fc690c05a0275\", \"port_range_max\": 77, " +
100             "\"port_range_min\": 77, " +
101             "\"id\": \"9b4be7fa-e56e-40fb-9516-1f0fa9185669\", " +
102             "\"security_group_id\": " +
103             "\"b60490fe-60a5-40be-af63-1d641381b784\"}}";
104         ITNeutronE2E.test_modify(url, content, "Security Rule Singleton Put Failed");
105     }
106
107     public void sr_element_get_test() {
108         String url = base + "/security-group-rules/9b4be7fa-e56e-40fb-9516-1f0fa9185669";
109         ITNeutronE2E.test_fetch(url, true, "Security Rule Element Get Failed");
110     }
111
112     public void sr_element_get_with_query_test() {
113         String url = base + "/security-group-rules/9b4be7fa-e56e-40fb-9516-1f0fa9185669" +
114             "?fields=id&fields=direction&fields=protocol&fields=port_range_min" +
115             "&fields=port_range_max&fields=ethertype&fields=remote_ip_prefix" +
116             "&fields=remote_group_id&fields=security_group_id&fields=tenant_id" +
117             "&fields=limit&fields=marker&fields=page_reverse";
118         ITNeutronE2E.test_fetch(url, true, "Security Rule Element Get With Query Failed");
119     }
120
121     public void sr_delete_test() {
122         String url = base + "/security-group-rules/9b4be7fa-e56e-40fb-9516-1f0fa9185669";
123         ITNeutronE2E.test_delete(url, "Security Rule Delete Failed");
124     }
125
126     public void sr_element_negative_get_test() {
127         String url = base + "/security-group-rules/9b4be7fa-e56e-40fb-9516-1f0fa9185669";
128         ITNeutronE2E.test_fetch(url, false, "Security Rule Element Negative Get Failed");
129     }
130
131     public void bug4043_ipv4_test() {
132         String url = base + "/security-group-rules";
133         String content = " {\"security_group_rule\": " +
134             "{\"remote_group_id\": null, \"direction\": \"ingress\", " +
135             "\"remote_ip_prefix\": \"10.10.10.10/16\", \"protocol\": \"tcp\", " +
136             "\"ethertype\": \"IPv4\", \"tenant_id\": " +
137             "\"00f340c7c3b34ab7be1fc690c05a0275\", \"port_range_max\": 77, " +
138             "\"port_range_min\": 77, " +
139             "\"id\": \"01234567-0123-0123-0123-01234567890a\", " +
140             "\"security_group_id\": " +
141             "\"b60490fe-60a5-40be-af63-1d641381b784\"}}";
142         ITNeutronE2E.test_create(url, content, "Security Rule bug4043 IPv4 Failed");
143
144         url = url + "/01234567-0123-0123-0123-01234567890a";
145         ITNeutronE2E.test_delete(url, "Security Rule Delete Failed");
146     }
147
148     public void bug4043_ipv6_test() {
149         String url = base + "/security-group-rules";
150         String content = " {\"security_group_rule\": " +
151             "{\"remote_group_id\": null, \"direction\": \"ingress\", " +
152             "\"remote_ip_prefix\": \"fe80::1/10\", \"protocol\": \"tcp\", " +
153             "\"ethertype\": \"IPv6\", \"tenant_id\": " +
154             "\"00f340c7c3b34ab7be1fc690c05a0275\", \"port_range_max\": 77, " +
155             "\"port_range_min\": 77, " +
156             "\"id\": \"01234567-0123-0123-0123-01234567890a\", " +
157             "\"security_group_id\": " +
158             "\"b60490fe-60a5-40be-af63-1d641381b784\"}}";
159         ITNeutronE2E.test_create(url, content, "Security Rule Bug4043 IPv6 Failed");
160
161         url = url + "/01234567-0123-0123-0123-01234567890a";
162         ITNeutronE2E.test_delete(url, "Security Rule Delete Failed");
163     }
164
165     public void bug4550_sg_sr_test() {
166         String sg_uuid = "d55c7ec4-6189-4cbf-b136-3059dc2afd37";
167         String sg_url = base + "/security-groups";
168         String sg_content = "{\"security_group\": {\"tenant_id\": " +
169             "\"4693014ff2dd4485b32d778c4942b18f\", \"description\": \"\", " +
170             "\"id\": \"" + sg_uuid + "\", " +
171             "\"security_group_rules\": [], " +
172             "\"name\": \"tempest-secgroup-bug4550-sg-sr-test\"}}";
173         ITNeutronE2E.test_create(sg_url, sg_content, "Security Group Singleton Post Failed");
174
175         String rule_uuid = "19a72e11-783c-4baf-bf49-ed8053956d5e";
176         String rule_url = base + "/security-group-rules";
177         String rule_content = " {\"security_group_rules\": [" +
178                 "{" +
179                 "  \"id\": \"" + rule_uuid + "\"," +
180                 "  \"direction\": \"egress\"," +
181                 "  \"ethertype\": \"IPv4\"," +
182                 "  \"security_group_id\": \"" + sg_uuid + "\"," +
183                 "  \"tenant_id\": \"4693014ff2dd4485b32d778c4942b18f\"" +
184                 "}" +
185                 "]}";
186         ITNeutronE2E.test_create(rule_url, rule_content, "Security Rule bug4550 Failed");
187
188         String sg_one_url = sg_url + "/" + sg_uuid;
189         JsonObject jsonObject = ITNeutronE2E.test_fetch_gson(sg_one_url, "Security group bug4550 fetch");
190         JsonObject sg = jsonObject.getAsJsonObject("security_group");
191         JsonArray sgRules = sg.getAsJsonArray("security_group_rules");
192         Assert.assertEquals("security rule bug4550 array size", 1, sgRules.size());
193         Assert.assertEquals("security rule bug4550 uuid",
194                             rule_uuid, sgRules.get(0).getAsJsonObject().get("id").getAsString());
195     }
196
197     public static void runTests(String base) {
198         NeutronSecurityRuleTests securityRule_tester = new NeutronSecurityRuleTests(base);
199         String createJsonString = securityRule_tester.singleton_sr_create_test();
200         securityRule_tester.singleton_sr_get_with_one_query_item_test(createJsonString);
201         securityRule_tester.multiple_sr_create_test();
202         securityRule_tester.singleton_sr_modify_test();
203         securityRule_tester.sr_element_get_test();
204         securityRule_tester.sr_element_get_with_query_test();
205         securityRule_tester.securityRule_collection_get_test();
206         securityRule_tester.sr_delete_test();
207         securityRule_tester.sr_element_negative_get_test();
208         securityRule_tester.bug4043_ipv4_test();
209         securityRule_tester.bug4043_ipv6_test();
210         securityRule_tester.bug4550_sg_sr_test();
211     }
212 }