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.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;
33 public class NeutronSecurityGroupAwareDataStoreTest extends NeutronMapperDataBrokerTest {
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";
39 private DataBroker dataBroker;
40 private NeutronSecurityGroupAware groupAware;
41 private SecurityGroup secGroup1;
42 private SecurityGroup secGroup2;
43 private EndpointRegistrator epRegistrator;
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.
49 dataBroker = getDataBroker();
51 secGroup1 = NeutronEntityFactory.securityGroup(secGroupId1, tenantId);
52 secGroup2 = NeutronEntityFactory.securityGroup(secGroupId2, tenantId);
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)))
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)))
64 groupAware = new NeutronSecurityGroupAware(dataBroker, new NeutronSecurityRuleAware(dataBroker, epRegistrator));
68 public void testAddAndDeleteNeutronSecurityGroup_noSecurityRules() throws Exception {
69 groupAware.onCreated(secGroup1, null);
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);
79 groupAware.onDeleted(secGroup1, null, null);
81 PolicyAssert.assertTenantExists(dataBroker, tenantId);
82 PolicyAssert.assertContractCount(dataBroker, tenantId, 0);
83 PolicyAssert.assertEndpointGroupCount(dataBroker, tenantId, 0);
85 groupAware.onCreated(secGroup1, null);
86 groupAware.onCreated(secGroup2, null);
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);
100 groupAware.onDeleted(secGroup2, null, null);
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);
109 groupAware.onDeleted(secGroup1, null, null);
111 PolicyAssert.assertTenantExists(dataBroker, tenantId);
112 PolicyAssert.assertContractCount(dataBroker, tenantId, 0);
113 PolicyAssert.assertEndpointGroupCount(dataBroker, tenantId, 0);
117 public ExpectedException thrown = ExpectedException.none();
120 public void testConstructor_invalidArgument() throws NullPointerException {
121 thrown.expect(NullPointerException.class);
122 new NeutronSecurityGroupAware(null, null);
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")
132 groupAware.onCreated(secGroupA, null);
134 PolicyAssert.assertEndpointGroupExists(dataBroker, tenantId, uuid);
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))
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);