apply checkstyle check during build for neutron-mapper
[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.junit.Before;
16 import org.junit.Rule;
17 import org.junit.Test;
18 import org.junit.rules.ExpectedException;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.groupbasedpolicy.neutron.mapper.EndpointRegistrator;
21 import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.rule.NeutronSecurityRuleAware;
22 import org.opendaylight.groupbasedpolicy.neutron.mapper.test.NeutronEntityFactory;
23 import org.opendaylight.groupbasedpolicy.neutron.mapper.test.NeutronMapperDataBrokerTest;
24 import org.opendaylight.groupbasedpolicy.neutron.mapper.test.PolicyAssert;
25 import org.opendaylight.groupbasedpolicy.neutron.mapper.util.MappingUtils;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.RegisterEndpointInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.UnregisterEndpointInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroup.IntraGroupPolicy;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.groups.attributes.security.groups.SecurityGroup;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.groups.attributes.security.groups.SecurityGroupBuilder;
32
33 public class NeutronSecurityGroupAwareDataStoreTest extends NeutronMapperDataBrokerTest {
34
35     private final String tenantId = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
36     private final String secGroupId1 = "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb";
37     private final String secGroupId2 = "cccccccc-cccc-cccc-cccc-cccccccccccc";
38
39     private DataBroker dataBroker;
40     private NeutronSecurityGroupAware groupAware;
41     private SecurityGroup secGroup1;
42     private SecurityGroup secGroup2;
43     private EndpointRegistrator epRegistrator;
44
45     @Before
46     @SuppressWarnings("checkstyle:LineLength") // Longer lines in this method are caused by long package names,
47                                                // this will be removed when deprecated classes will be cleared.
48     public void init() {
49         dataBroker = getDataBroker();
50
51         secGroup1 = NeutronEntityFactory.securityGroup(secGroupId1, tenantId);
52         secGroup2 = NeutronEntityFactory.securityGroup(secGroupId2, tenantId);
53
54
55         epRegistrator = mock(EndpointRegistrator.class);
56         when(epRegistrator.registerEndpoint(any(RegisterEndpointInput.class))).thenReturn(true);
57         when(epRegistrator.registerEndpoint(
58                 any(org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterEndpointInput.class)))
59                     .thenReturn(true);
60         when(epRegistrator.unregisterEndpoint(any(UnregisterEndpointInput.class))).thenReturn(true);
61         when(epRegistrator.unregisterEndpoint(
62                 any(org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.UnregisterEndpointInput.class)))
63                     .thenReturn(true);
64         groupAware = new NeutronSecurityGroupAware(dataBroker, new NeutronSecurityRuleAware(dataBroker, epRegistrator));
65     }
66
67     @Test
68     public void testAddAndDeleteNeutronSecurityGroup_noSecurityRules() throws Exception {
69         groupAware.onCreated(secGroup1, null);
70
71         PolicyAssert.assertTenantExists(dataBroker, tenantId);
72         PolicyAssert.assertContractCount(dataBroker, tenantId, 0);
73         PolicyAssert.assertEndpointGroupCount(dataBroker, tenantId, 1);
74         PolicyAssert.assertEndpointGroupExists(dataBroker, tenantId, secGroupId1);
75         PolicyAssert.assertNoProviderNamedSelectors(dataBroker, tenantId, secGroupId1);
76         PolicyAssert.assertNoConsumerNamedSelectors(dataBroker, tenantId, secGroupId1);
77         PolicyAssert.assertIntraGroupPolicy(dataBroker, tenantId, secGroupId1, IntraGroupPolicy.RequireContract);
78
79         groupAware.onDeleted(secGroup1, null, null);
80
81         PolicyAssert.assertTenantExists(dataBroker, tenantId);
82         PolicyAssert.assertContractCount(dataBroker, tenantId, 0);
83         PolicyAssert.assertEndpointGroupCount(dataBroker, tenantId, 0);
84
85         groupAware.onCreated(secGroup1, null);
86         groupAware.onCreated(secGroup2, null);
87
88         PolicyAssert.assertTenantExists(dataBroker, tenantId);
89         PolicyAssert.assertContractCount(dataBroker, tenantId, 0);
90         PolicyAssert.assertEndpointGroupCount(dataBroker, tenantId, 2);
91         PolicyAssert.assertEndpointGroupExists(dataBroker, tenantId, secGroupId1);
92         PolicyAssert.assertNoProviderNamedSelectors(dataBroker, tenantId, secGroupId1);
93         PolicyAssert.assertNoConsumerNamedSelectors(dataBroker, tenantId, secGroupId1);
94         PolicyAssert.assertIntraGroupPolicy(dataBroker, tenantId, secGroupId1, IntraGroupPolicy.RequireContract);
95         PolicyAssert.assertEndpointGroupExists(dataBroker, tenantId, secGroupId2);
96         PolicyAssert.assertNoProviderNamedSelectors(dataBroker, tenantId, secGroupId2);
97         PolicyAssert.assertNoConsumerNamedSelectors(dataBroker, tenantId, secGroupId2);
98         PolicyAssert.assertIntraGroupPolicy(dataBroker, tenantId, secGroupId2, IntraGroupPolicy.RequireContract);
99
100         groupAware.onDeleted(secGroup2, null, null);
101
102         PolicyAssert.assertTenantExists(dataBroker, tenantId);
103         PolicyAssert.assertContractCount(dataBroker, tenantId, 0);
104         PolicyAssert.assertEndpointGroupCount(dataBroker, tenantId, 1);
105         PolicyAssert.assertEndpointGroupExists(dataBroker, tenantId, secGroupId1);
106         PolicyAssert.assertNoProviderNamedSelectors(dataBroker, tenantId, secGroupId1);
107         PolicyAssert.assertNoConsumerNamedSelectors(dataBroker, tenantId, secGroupId1);
108
109         groupAware.onDeleted(secGroup1, null, null);
110
111         PolicyAssert.assertTenantExists(dataBroker, tenantId);
112         PolicyAssert.assertContractCount(dataBroker, tenantId, 0);
113         PolicyAssert.assertEndpointGroupCount(dataBroker, tenantId, 0);
114     }
115
116     @Rule
117     public ExpectedException thrown = ExpectedException.none();
118
119     @Test
120     public void testConstructor_invalidArgument() throws NullPointerException {
121         thrown.expect(NullPointerException.class);
122         new NeutronSecurityGroupAware(null, null);
123     }
124
125     @Test
126     public void testAddNeutronSecurityGroup_ExternalImplicitGroup() throws Exception {
127         String uuid = MappingUtils.EIG_UUID.getValue();
128         SecurityGroup secGroupA = new SecurityGroupBuilder().setUuid(new Uuid(uuid))
129             .setTenantId(new Uuid(tenantId))
130             .setName("correctName")
131             .build();
132         groupAware.onCreated(secGroupA, null);
133
134         PolicyAssert.assertEndpointGroupExists(dataBroker, tenantId, uuid);
135     }
136
137     @Test
138     public void testAddNeutronSecurityGroup_incorrectNameDescription() throws Exception {
139         String uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd";
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 }