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