2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.groupbasedpolicy.neutron.mapper.mapping;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
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;
34 public class NeutronSecurityGroupAwareDataStoreTest extends NeutronMapperDataBrokerTest {
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";
40 private DataBroker dataBroker;
41 private NeutronSecurityGroupAware groupAware;
42 private SecurityGroup secGroup1;
43 private SecurityGroup secGroup2;
44 private EndpointRegistrator epRegistrator;
48 dataBroker = getDataBroker();
50 secGroup1 = NeutronEntityFactory.securityGroup(secGroupId1, tenantId);
51 secGroup2 = NeutronEntityFactory.securityGroup(secGroupId2, tenantId);
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)))
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)))
63 groupAware = new NeutronSecurityGroupAware(dataBroker, new NeutronSecurityRuleAware(dataBroker, epRegistrator));
67 public void testAddAndDeleteNeutronSecurityGroup_noSecurityRules() throws Exception {
68 groupAware.onCreated(secGroup1, null);
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);
78 groupAware.onDeleted(secGroup1, null, null);
80 PolicyAssert.assertTenantExists(dataBroker, tenantId);
81 PolicyAssert.assertContractCount(dataBroker, tenantId, 0);
82 PolicyAssert.assertEndpointGroupCount(dataBroker, tenantId, 0);
84 groupAware.onCreated(secGroup1, null);
85 groupAware.onCreated(secGroup2, null);
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);
99 groupAware.onDeleted(secGroup2, null, null);
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);
108 groupAware.onDeleted(secGroup1, null, null);
110 PolicyAssert.assertTenantExists(dataBroker, tenantId);
111 PolicyAssert.assertContractCount(dataBroker, tenantId, 0);
112 PolicyAssert.assertEndpointGroupCount(dataBroker, tenantId, 0);
116 public ExpectedException thrown = ExpectedException.none();
119 public void testConstructor_invalidArgument() throws NullPointerException {
120 thrown.expect(NullPointerException.class);
121 new NeutronSecurityGroupAware(null, null);
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")
131 groupAware.onCreated(secGroupA, null);
133 PolicyAssert.assertEndpointGroupExists(dataBroker, tenantId, uuid);
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))
144 groupAware.onCreated(secGroupA, null);
146 PolicyAssert.assertEndpointGroupExists(dataBroker, tenantId, uuid);
150 public void testOnDelete_NonExistingSecGroup() throws Exception {
151 PolicyAssert.assertEndpointGroupNotExists(dataBroker, tenantId, secGroupId2);
153 groupAware.onDeleted(secGroup2, null, null);
155 PolicyAssert.assertEndpointGroupNotExists(dataBroker, tenantId, secGroupId2);