90fc4ad67c3301dd81dfb21ab6a082ca0dfc3e12
[groupbasedpolicy.git] / neutron-mapper / src / test / java / org / opendaylight / groupbasedpolicy / neutron / mapper / mapping / NeutronSecurityGroupAwareDataStoreTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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.groupbasedpolicy.neutron.mapper.mapping;
10
11 import org.apache.commons.lang3.StringUtils;
12 import org.junit.Before;
13 import org.junit.Rule;
14 import org.junit.Test;
15 import org.junit.rules.ExpectedException;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.groupbasedpolicy.neutron.mapper.test.NeutronMapperDataBrokerTest;
18 import org.opendaylight.groupbasedpolicy.neutron.mapper.test.NeutronEntityFactory;
19 import org.opendaylight.groupbasedpolicy.neutron.mapper.test.PolicyAssert;
20 import org.opendaylight.groupbasedpolicy.neutron.mapper.util.MappingUtils;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroup.IntraGroupPolicy;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.groups.attributes.security.groups.SecurityGroup;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.groups.attributes.security.groups.SecurityGroupBuilder;
25
26 public class NeutronSecurityGroupAwareDataStoreTest extends NeutronMapperDataBrokerTest {
27
28     private final String tenantId = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
29     private final String secGroupId1 = "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb";
30     private final String secGroupId2 = "cccccccc-cccc-cccc-cccc-cccccccccccc";
31
32     private DataBroker dataBroker;
33     private NeutronSecurityGroupAware groupAware;
34     private SecurityGroup secGroup1;
35     private SecurityGroup secGroup2;
36
37     @Before
38     public void init() {
39         dataBroker = getDataBroker();
40         groupAware = new NeutronSecurityGroupAware(dataBroker);
41
42         secGroup1 = NeutronEntityFactory.securityGroup(secGroupId1, tenantId);
43         secGroup2 = NeutronEntityFactory.securityGroup(secGroupId2, tenantId);
44     }
45
46     @Test
47     public void testAddAndDeleteNeutronSecurityGroup_noSecurityRules() throws Exception {
48         groupAware.onCreated(secGroup1, null);
49
50         PolicyAssert.assertTenantExists(dataBroker, tenantId);
51         PolicyAssert.assertContractCount(dataBroker, tenantId, 0);
52         PolicyAssert.assertEndpointGroupCount(dataBroker, tenantId, 1);
53         PolicyAssert.assertEndpointGroupExists(dataBroker, tenantId, secGroupId1);
54         PolicyAssert.assertNoProviderNamedSelectors(dataBroker, tenantId, secGroupId1);
55         PolicyAssert.assertNoConsumerNamedSelectors(dataBroker, tenantId, secGroupId1);
56         PolicyAssert.assertIntraGroupPolicy(dataBroker, tenantId, secGroupId1, IntraGroupPolicy.RequireContract);
57
58         groupAware.onDeleted(secGroup1, null, null);
59
60         PolicyAssert.assertTenantExists(dataBroker, tenantId);
61         PolicyAssert.assertContractCount(dataBroker, tenantId, 0);
62         PolicyAssert.assertEndpointGroupCount(dataBroker, tenantId, 0);
63
64         groupAware.onCreated(secGroup1, null);
65         groupAware.onCreated(secGroup2, null);
66
67         PolicyAssert.assertTenantExists(dataBroker, tenantId);
68         PolicyAssert.assertContractCount(dataBroker, tenantId, 0);
69         PolicyAssert.assertEndpointGroupCount(dataBroker, tenantId, 2);
70         PolicyAssert.assertEndpointGroupExists(dataBroker, tenantId, secGroupId1);
71         PolicyAssert.assertNoProviderNamedSelectors(dataBroker, tenantId, secGroupId1);
72         PolicyAssert.assertNoConsumerNamedSelectors(dataBroker, tenantId, secGroupId1);
73         PolicyAssert.assertIntraGroupPolicy(dataBroker, tenantId, secGroupId1, IntraGroupPolicy.RequireContract);
74         PolicyAssert.assertEndpointGroupExists(dataBroker, tenantId, secGroupId2);
75         PolicyAssert.assertNoProviderNamedSelectors(dataBroker, tenantId, secGroupId2);
76         PolicyAssert.assertNoConsumerNamedSelectors(dataBroker, tenantId, secGroupId2);
77         PolicyAssert.assertIntraGroupPolicy(dataBroker, tenantId, secGroupId2, IntraGroupPolicy.RequireContract);
78
79         groupAware.onDeleted(secGroup2, null, null);
80
81         PolicyAssert.assertTenantExists(dataBroker, tenantId);
82         PolicyAssert.assertContractCount(dataBroker, tenantId, 0);
83         PolicyAssert.assertEndpointGroupCount(dataBroker, tenantId, 1);
84         PolicyAssert.assertEndpointGroupExists(dataBroker, tenantId, secGroupId1);
85         PolicyAssert.assertNoProviderNamedSelectors(dataBroker, tenantId, secGroupId1);
86         PolicyAssert.assertNoConsumerNamedSelectors(dataBroker, tenantId, secGroupId1);
87
88         groupAware.onDeleted(secGroup1, null, null);
89
90         PolicyAssert.assertTenantExists(dataBroker, tenantId);
91         PolicyAssert.assertContractCount(dataBroker, tenantId, 0);
92         PolicyAssert.assertEndpointGroupCount(dataBroker, tenantId, 0);
93     }
94
95     @Rule
96     public ExpectedException thrown = ExpectedException.none();
97
98     @Test
99     public void testConstructor_invalidArgument() throws NullPointerException {
100         thrown.expect(NullPointerException.class);
101         new NeutronSecurityGroupAware(null);
102     }
103
104     @Test
105     public void testAddNeutronSecurityGroup_ExternalImplicitGroup() throws Exception {
106         String uuid = MappingUtils.EIG_UUID.getValue();
107         SecurityGroup secGroupA = new SecurityGroupBuilder().setUuid(new Uuid(uuid))
108             .setTenantId(new Uuid(tenantId))
109             .setName("correctName")
110             .setDescription("correct description")
111             .build();
112         groupAware.onCreated(secGroupA, null);
113
114         PolicyAssert.assertEndpointGroupExists(dataBroker, tenantId, uuid);
115     }
116
117     @Test
118     public void testAddNeutronSecurityGroup_incorrectNameDescription() throws Exception {
119         String uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd";
120         String longDescription = StringUtils.repeat("a", 4100);
121         SecurityGroup secGroupA = new SecurityGroupBuilder().setUuid(new Uuid(uuid))
122             .setTenantId(new Uuid(tenantId))
123             .setName("123")
124             .setDescription(longDescription)
125             .build();
126         groupAware.onCreated(secGroupA, null);
127
128         PolicyAssert.assertEndpointGroupExists(dataBroker, tenantId, uuid);
129     }
130
131     @Test
132     public void testOnDelete_NonExistingSecGroup() throws Exception {
133         PolicyAssert.assertEndpointGroupNotExists(dataBroker, tenantId, secGroupId2);
134
135         groupAware.onDeleted(secGroup2, null, null);
136
137         PolicyAssert.assertEndpointGroupNotExists(dataBroker, tenantId, secGroupId2);
138     }
139
140 }