bb2f9695c7b9bad10cab0a54f774c8bd5007a8dc
[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.mapping.rule.NeutronSecurityRuleAware;
19 import org.opendaylight.groupbasedpolicy.neutron.mapper.test.NeutronEntityFactory;
20 import org.opendaylight.groupbasedpolicy.neutron.mapper.test.PolicyAssert;
21 import org.opendaylight.groupbasedpolicy.neutron.mapper.util.MappingUtils;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroup.IntraGroupPolicy;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.groups.attributes.security.groups.SecurityGroup;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.groups.attributes.security.groups.SecurityGroupBuilder;
26
27 public class NeutronSecurityGroupAwareDataStoreTest extends NeutronMapperDataBrokerTest {
28
29     private final String tenantId = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
30     private final String secGroupId1 = "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb";
31     private final String secGroupId2 = "cccccccc-cccc-cccc-cccc-cccccccccccc";
32
33     private DataBroker dataBroker;
34     private NeutronSecurityGroupAware groupAware;
35     private SecurityGroup secGroup1;
36     private SecurityGroup secGroup2;
37
38     @Before
39     public void init() {
40         dataBroker = getDataBroker();
41         groupAware = new NeutronSecurityGroupAware(dataBroker, new NeutronSecurityRuleAware(dataBroker));
42
43         secGroup1 = NeutronEntityFactory.securityGroup(secGroupId1, tenantId);
44         secGroup2 = NeutronEntityFactory.securityGroup(secGroupId2, tenantId);
45     }
46
47     @Test
48     public void testAddAndDeleteNeutronSecurityGroup_noSecurityRules() throws Exception {
49         groupAware.onCreated(secGroup1, null);
50
51         PolicyAssert.assertTenantExists(dataBroker, tenantId);
52         PolicyAssert.assertContractCount(dataBroker, tenantId, 0);
53         PolicyAssert.assertEndpointGroupCount(dataBroker, tenantId, 1);
54         PolicyAssert.assertEndpointGroupExists(dataBroker, tenantId, secGroupId1);
55         PolicyAssert.assertNoProviderNamedSelectors(dataBroker, tenantId, secGroupId1);
56         PolicyAssert.assertNoConsumerNamedSelectors(dataBroker, tenantId, secGroupId1);
57         PolicyAssert.assertIntraGroupPolicy(dataBroker, tenantId, secGroupId1, IntraGroupPolicy.RequireContract);
58
59         groupAware.onDeleted(secGroup1, null, null);
60
61         PolicyAssert.assertTenantExists(dataBroker, tenantId);
62         PolicyAssert.assertContractCount(dataBroker, tenantId, 0);
63         PolicyAssert.assertEndpointGroupCount(dataBroker, tenantId, 0);
64
65         groupAware.onCreated(secGroup1, null);
66         groupAware.onCreated(secGroup2, null);
67
68         PolicyAssert.assertTenantExists(dataBroker, tenantId);
69         PolicyAssert.assertContractCount(dataBroker, tenantId, 0);
70         PolicyAssert.assertEndpointGroupCount(dataBroker, tenantId, 2);
71         PolicyAssert.assertEndpointGroupExists(dataBroker, tenantId, secGroupId1);
72         PolicyAssert.assertNoProviderNamedSelectors(dataBroker, tenantId, secGroupId1);
73         PolicyAssert.assertNoConsumerNamedSelectors(dataBroker, tenantId, secGroupId1);
74         PolicyAssert.assertIntraGroupPolicy(dataBroker, tenantId, secGroupId1, IntraGroupPolicy.RequireContract);
75         PolicyAssert.assertEndpointGroupExists(dataBroker, tenantId, secGroupId2);
76         PolicyAssert.assertNoProviderNamedSelectors(dataBroker, tenantId, secGroupId2);
77         PolicyAssert.assertNoConsumerNamedSelectors(dataBroker, tenantId, secGroupId2);
78         PolicyAssert.assertIntraGroupPolicy(dataBroker, tenantId, secGroupId2, IntraGroupPolicy.RequireContract);
79
80         groupAware.onDeleted(secGroup2, null, null);
81
82         PolicyAssert.assertTenantExists(dataBroker, tenantId);
83         PolicyAssert.assertContractCount(dataBroker, tenantId, 0);
84         PolicyAssert.assertEndpointGroupCount(dataBroker, tenantId, 1);
85         PolicyAssert.assertEndpointGroupExists(dataBroker, tenantId, secGroupId1);
86         PolicyAssert.assertNoProviderNamedSelectors(dataBroker, tenantId, secGroupId1);
87         PolicyAssert.assertNoConsumerNamedSelectors(dataBroker, tenantId, secGroupId1);
88
89         groupAware.onDeleted(secGroup1, null, null);
90
91         PolicyAssert.assertTenantExists(dataBroker, tenantId);
92         PolicyAssert.assertContractCount(dataBroker, tenantId, 0);
93         PolicyAssert.assertEndpointGroupCount(dataBroker, tenantId, 0);
94     }
95
96     @Rule
97     public ExpectedException thrown = ExpectedException.none();
98
99     @Test
100     public void testConstructor_invalidArgument() throws NullPointerException {
101         thrown.expect(NullPointerException.class);
102         new NeutronSecurityGroupAware(null, null);
103     }
104
105     @Test
106     public void testAddNeutronSecurityGroup_ExternalImplicitGroup() throws Exception {
107         String uuid = MappingUtils.EIG_UUID.getValue();
108         SecurityGroup secGroupA = new SecurityGroupBuilder().setUuid(new Uuid(uuid))
109             .setTenantId(new Uuid(tenantId))
110             .setName("correctName")
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             .build();
125         groupAware.onCreated(secGroupA, null);
126
127         PolicyAssert.assertEndpointGroupExists(dataBroker, tenantId, uuid);
128     }
129
130     @Test
131     public void testOnDelete_NonExistingSecGroup() throws Exception {
132         PolicyAssert.assertEndpointGroupNotExists(dataBroker, tenantId, secGroupId2);
133
134         groupAware.onDeleted(secGroup2, null, null);
135
136         PolicyAssert.assertEndpointGroupNotExists(dataBroker, tenantId, secGroupId2);
137     }
138
139 }