blob: b657aa12d4d1eb75080c7de8e4ab468f3217a4c3 [file] [log] [blame]
Jian Li43e066b2018-07-16 17:43:56 +09001/*
2 * Copyright 2018-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.openstacknetworking.impl;
17
18import com.google.common.collect.ImmutableList;
19import com.google.common.collect.Lists;
20import com.google.common.util.concurrent.MoreExecutors;
21import org.junit.After;
22import org.junit.Before;
23import org.junit.Test;
24import org.onlab.junit.TestUtils;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.core.CoreServiceAdapter;
27import org.onosproject.core.DefaultApplicationId;
28import org.onosproject.event.Event;
29import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupEvent;
30import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupListener;
31import org.onosproject.store.service.TestStorageService;
32import org.openstack4j.model.network.SecurityGroup;
33import org.openstack4j.model.network.SecurityGroupRule;
34import org.openstack4j.openstack.networking.domain.NeutronSecurityGroup;
35import org.openstack4j.openstack.networking.domain.NeutronSecurityGroupRule;
36
37import java.util.List;
38
39import static org.junit.Assert.assertEquals;
40import static org.junit.Assert.assertNotNull;
41import static org.junit.Assert.assertNull;
42import static org.onosproject.openstacknetworking.api.OpenstackSecurityGroupEvent.Type.OPENSTACK_SECURITY_GROUP_CREATED;
43import static org.onosproject.openstacknetworking.api.OpenstackSecurityGroupEvent.Type.OPENSTACK_SECURITY_GROUP_REMOVED;
44
45/**
46 * Unit tests for openstack security group manager.
47 */
48public class OpenstackSecurityGroupManagerTest {
49
50 private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test");
51
52 private static final String SECURITY_GROUP_ID_1 = "sg-id-1";
53 private static final String SECURITY_GROUP_ID_2 = "sg-id-2";
54 private static final String UNKNOWN_ID = "sg-id-x";
55
56
57 private static final String SECURITY_GROUP_NAME_1 = "sg-name-1";
58 private static final String SECURITY_GROUP_NAME_2 = "sg-name-2";
59
60 private static final String SECURITY_GROUP_TENANT_ID_1 = "tenant-id-1";
61 private static final String SECURITY_GROUP_TENANT_ID_2 = "tenant-id-2";
62
63 private static final String SECURITY_GROUP_DESCRIPTION_1 = "description-1";
64 private static final String SECURITY_GROUP_DESCRIPTION_2 = "description-2";
65
66 private static final String SECURITY_GROUP_RULE_ID_1_1 = "sgr-id-1-1";
67 private static final String SECURITY_GROUP_RULE_ID_1_2 = "sgr-id-1-2";
68 private static final String SECURITY_GROUP_RULE_ID_2_1 = "sgr-id-2-1";
69 private static final String SECURITY_GROUP_RULE_ID_2_2 = "sgr-id-2-2";
70
71 private static final String SECURITY_GROUP_ETH_TYPE = "IP";
72 private static final String SECURITY_GROUP_DIRECTION = "EGRESS";
73 private static final String SECURITY_GROUP_PROTOCOL_1 = "TCP";
74 private static final String SECURITY_GROUP_PROTOCOL_2 = "UDP";
75
76 private static final int SECURITY_GROUP_PORT_RANGE_MIN_1 = 1;
77 private static final int SECURITY_GROUP_PORT_RANGE_MIN_2 = 101;
78 private static final int SECURITY_GROUP_PORT_RANGE_MAX_1 = 100;
79 private static final int SECURITY_GROUP_PORT_RANGE_MAX_2 = 200;
80
81 private static final String SECURITY_GROUP_REMOTE_IP_PREFIX_1 = "1.1.1.0/24";
82 private static final String SECURITY_GROUP_REMOTE_IP_PREFIX_2 = "2.2.2.0/24";
83
84 private SecurityGroup securityGroup1;
85 private SecurityGroup securityGroup2;
86
87 private SecurityGroupRule securityGroupRule11;
88 private SecurityGroupRule securityGroupRule12;
89 private SecurityGroupRule securityGroupRule21;
90 private SecurityGroupRule securityGroupRule22;
91
92 private List<SecurityGroupRule> securityGroupRules1;
93 private List<SecurityGroupRule> securityGroupRules2;
94
95 private OpenstackSecurityGroupManager target;
96 private DistributedSecurityGroupStore store;
97
98 private final TestOpenstackSecurityGroupListener
99 testOpenstackSecurityGroupListener = new TestOpenstackSecurityGroupListener();
100
101 /**
102 * Initial setup for this unit test.
103 */
104 @Before
105 public void setUp() throws Exception {
106
107 store = new DistributedSecurityGroupStore();
108 TestUtils.setField(store, "coreService", new TestCoreService());
109 TestUtils.setField(store, "storageService", new TestStorageService());
110 TestUtils.setField(store, "eventExecutor", MoreExecutors.newDirectExecutorService());
111 store.activate();
112
113 target = new OpenstackSecurityGroupManager();
114 TestUtils.setField(target, "coreService", new TestCoreService());
115 target.osSecurityGroupStore = store;
116 target.addListener(testOpenstackSecurityGroupListener);
117 target.activate();
118
119 securityGroupRule11 = NeutronSecurityGroupRule.builder()
120 .id(SECURITY_GROUP_RULE_ID_1_1)
121 .securityGroupId(SECURITY_GROUP_ID_1)
122 .tenantId(SECURITY_GROUP_TENANT_ID_1)
123 .remoteGroupId(SECURITY_GROUP_ID_1)
124 .ethertype(SECURITY_GROUP_ETH_TYPE)
125 .direction(SECURITY_GROUP_DIRECTION)
126 .portRangeMin(SECURITY_GROUP_PORT_RANGE_MIN_1)
127 .portRangeMax(SECURITY_GROUP_PORT_RANGE_MAX_1)
128 .protocol(SECURITY_GROUP_PROTOCOL_1)
129 .remoteIpPrefix(SECURITY_GROUP_REMOTE_IP_PREFIX_1)
130 .build();
131
132 securityGroupRule12 = NeutronSecurityGroupRule.builder()
133 .id(SECURITY_GROUP_RULE_ID_1_2)
134 .securityGroupId(SECURITY_GROUP_ID_1)
135 .tenantId(SECURITY_GROUP_TENANT_ID_1)
136 .remoteGroupId(SECURITY_GROUP_ID_1)
137 .ethertype(SECURITY_GROUP_ETH_TYPE)
138 .direction(SECURITY_GROUP_DIRECTION)
139 .portRangeMin(SECURITY_GROUP_PORT_RANGE_MIN_1)
140 .portRangeMax(SECURITY_GROUP_PORT_RANGE_MAX_1)
141 .protocol(SECURITY_GROUP_PROTOCOL_1)
142 .remoteIpPrefix(SECURITY_GROUP_REMOTE_IP_PREFIX_1)
143 .build();
144
145 securityGroupRule21 = NeutronSecurityGroupRule.builder()
146 .id(SECURITY_GROUP_RULE_ID_2_1)
147 .securityGroupId(SECURITY_GROUP_ID_2)
148 .tenantId(SECURITY_GROUP_TENANT_ID_2)
149 .remoteGroupId(SECURITY_GROUP_ID_2)
150 .ethertype(SECURITY_GROUP_ETH_TYPE)
151 .direction(SECURITY_GROUP_DIRECTION)
152 .portRangeMin(SECURITY_GROUP_PORT_RANGE_MIN_2)
153 .portRangeMax(SECURITY_GROUP_PORT_RANGE_MAX_2)
154 .protocol(SECURITY_GROUP_PROTOCOL_2)
155 .remoteIpPrefix(SECURITY_GROUP_REMOTE_IP_PREFIX_2)
156 .build();
157
158 securityGroupRule22 = NeutronSecurityGroupRule.builder()
159 .id(SECURITY_GROUP_RULE_ID_2_2)
160 .securityGroupId(SECURITY_GROUP_ID_2)
161 .tenantId(SECURITY_GROUP_TENANT_ID_2)
162 .remoteGroupId(SECURITY_GROUP_ID_2)
163 .ethertype(SECURITY_GROUP_ETH_TYPE)
164 .direction(SECURITY_GROUP_DIRECTION)
165 .portRangeMin(SECURITY_GROUP_PORT_RANGE_MIN_2)
166 .portRangeMax(SECURITY_GROUP_PORT_RANGE_MAX_2)
167 .protocol(SECURITY_GROUP_PROTOCOL_2)
168 .remoteIpPrefix(SECURITY_GROUP_REMOTE_IP_PREFIX_2)
169 .build();
170
171 securityGroupRules1 = ImmutableList.of(securityGroupRule11, securityGroupRule12);
172 securityGroupRules2 = ImmutableList.of(securityGroupRule21, securityGroupRule22);
173
174 securityGroup1 = NeutronSecurityGroup.builder()
175 .id(SECURITY_GROUP_ID_1)
176 .name(SECURITY_GROUP_NAME_1)
177 .tenantId(SECURITY_GROUP_TENANT_ID_1)
178 .description(SECURITY_GROUP_DESCRIPTION_1)
179 .build();
180
181 securityGroup2 = NeutronSecurityGroup.builder()
182 .id(SECURITY_GROUP_ID_2)
183 .name(SECURITY_GROUP_NAME_2)
184 .tenantId(SECURITY_GROUP_TENANT_ID_2)
185 .description(SECURITY_GROUP_DESCRIPTION_2)
186 .build();
187
188 }
189
190 /**
191 * Tears down all of this unit test.
192 */
193 @After
194 public void tearDown() {
195 target.removeListener(testOpenstackSecurityGroupListener);
196 store.deactivate();
197 target.deactivate();
198 store = null;
199 target = null;
200 }
201
202 /**
203 * Tests if getting all security groups returns the correct set of groups.
204 */
205 @Test
206 public void testGetSecurityGroups() {
207 createBasicSecurityGroups();
208 assertEquals("Number of security group did not match",
209 2, target.securityGroups().size());
210 }
211
212 /**
213 * Tests if getting a security group with group ID returns the correct group.
214 */
215 @Test
216 public void testGetSecurityGroupById() {
217 createBasicSecurityGroups();
218 assertNotNull("Instance port did not match", target.securityGroup(SECURITY_GROUP_ID_1));
219 assertNotNull("Instance port did not match", target.securityGroup(SECURITY_GROUP_ID_2));
220 assertNull("Instance port did not match", target.securityGroup(UNKNOWN_ID));
221 }
222
223 /**
224 * Tests creating and removing a security group, and checks if it triggers proper events.
225 */
226 @Test
227 public void testCreateAndRemoveSecurityGroup() {
228 target.createSecurityGroup(securityGroup1);
229 assertEquals("Number of security group did not match",
230 1, target.securityGroups().size());
231 assertNotNull("Security group did not match",
232 target.securityGroup(SECURITY_GROUP_ID_1));
233
234 target.removeSecurityGroup(SECURITY_GROUP_ID_1);
235 assertEquals("Number of security group did not match",
236 0, target.securityGroups().size());
237 assertNull("Security group did not match",
238 target.securityGroup(SECURITY_GROUP_ID_1));
239
240 validateEvents(OPENSTACK_SECURITY_GROUP_CREATED, OPENSTACK_SECURITY_GROUP_REMOVED);
241 }
242
243 /**
244 * Tests if creating a null security group fails with an exception.
245 */
246 @Test(expected = NullPointerException.class)
247 public void testCreateNullSecurityGroup() {
248 target.createSecurityGroup(null);
249 }
250
251 /**
252 * Tests if creating a duplicated security group fails with an exception.
253 */
254 @Test(expected = IllegalArgumentException.class)
255 public void testCreateDuplicateSecurityGroup() {
256 target.createSecurityGroup(securityGroup1);
257 target.createSecurityGroup(securityGroup1);
258 }
259
260 /**
261 * Tests if removing security group with null ID fails with an exception.
262 */
263 @Test(expected = IllegalArgumentException.class)
264 public void testRemoveSecurityGroupWithNull() {
265 target.removeSecurityGroup(null);
266 }
267
268 /**
269 * Tests if updating an unregistered security group fails with an exception.
270 */
271 @Test(expected = IllegalArgumentException.class)
272 public void testUpdateUnregisteredSecurityGroup() {
273 target.updateSecurityGroup(securityGroup1);
274 }
275
276 private void createBasicSecurityGroups() {
277 target.createSecurityGroup(securityGroup1);
278 target.createSecurityGroup(securityGroup2);
279 }
280
281 private static class TestCoreService extends CoreServiceAdapter {
282
283 @Override
284 public ApplicationId registerApplication(String name) {
285 return TEST_APP_ID;
286 }
287 }
288
289 private static class TestOpenstackSecurityGroupListener
290 implements OpenstackSecurityGroupListener {
291 private List<OpenstackSecurityGroupEvent> events = Lists.newArrayList();
292
293 @Override
294 public void event(OpenstackSecurityGroupEvent event) {
295 events.add(event);
296 }
297 }
298
299 private void validateEvents(Enum... types) {
300 int i = 0;
301 assertEquals("Number of events did not match", types.length,
302 testOpenstackSecurityGroupListener.events.size());
303 for (Event event : testOpenstackSecurityGroupListener.events) {
304 assertEquals("Incorrect event received", types[i], event.type());
305 i++;
306 }
307 testOpenstackSecurityGroupListener.events.clear();
308 }
309}