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 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;
27 public class NeutronSecurityGroupAwareDataStoreTest extends NeutronMapperDataBrokerTest {
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";
33 private DataBroker dataBroker;
34 private NeutronSecurityGroupAware groupAware;
35 private SecurityGroup secGroup1;
36 private SecurityGroup secGroup2;
40 dataBroker = getDataBroker();
41 groupAware = new NeutronSecurityGroupAware(dataBroker, new NeutronSecurityRuleAware(dataBroker));
43 secGroup1 = NeutronEntityFactory.securityGroup(secGroupId1, tenantId);
44 secGroup2 = NeutronEntityFactory.securityGroup(secGroupId2, tenantId);
48 public void testAddAndDeleteNeutronSecurityGroup_noSecurityRules() throws Exception {
49 groupAware.onCreated(secGroup1, null);
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);
59 groupAware.onDeleted(secGroup1, null, null);
61 PolicyAssert.assertTenantExists(dataBroker, tenantId);
62 PolicyAssert.assertContractCount(dataBroker, tenantId, 0);
63 PolicyAssert.assertEndpointGroupCount(dataBroker, tenantId, 0);
65 groupAware.onCreated(secGroup1, null);
66 groupAware.onCreated(secGroup2, null);
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);
80 groupAware.onDeleted(secGroup2, null, null);
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);
89 groupAware.onDeleted(secGroup1, null, null);
91 PolicyAssert.assertTenantExists(dataBroker, tenantId);
92 PolicyAssert.assertContractCount(dataBroker, tenantId, 0);
93 PolicyAssert.assertEndpointGroupCount(dataBroker, tenantId, 0);
97 public ExpectedException thrown = ExpectedException.none();
100 public void testConstructor_invalidArgument() throws NullPointerException {
101 thrown.expect(NullPointerException.class);
102 new NeutronSecurityGroupAware(null, null);
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")
112 groupAware.onCreated(secGroupA, null);
114 PolicyAssert.assertEndpointGroupExists(dataBroker, tenantId, uuid);
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))
125 groupAware.onCreated(secGroupA, null);
127 PolicyAssert.assertEndpointGroupExists(dataBroker, tenantId, uuid);
131 public void testOnDelete_NonExistingSecGroup() throws Exception {
132 PolicyAssert.assertEndpointGroupNotExists(dataBroker, tenantId, secGroupId2);
134 groupAware.onDeleted(secGroup2, null, null);
136 PolicyAssert.assertEndpointGroupNotExists(dataBroker, tenantId, secGroupId2);