1 package org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.rule;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertNotNull;
6 import static org.junit.Assert.assertTrue;
9 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
10 import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.StatusCode;
11 import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.group.SecGroupDao;
12 import org.opendaylight.groupbasedpolicy.neutron.mapper.test.ConfigDataStoreReader;
13 import org.opendaylight.groupbasedpolicy.neutron.mapper.test.GbpDataBrokerTest;
14 import org.opendaylight.groupbasedpolicy.neutron.mapper.test.PolicyAssert;
15 import org.opendaylight.neutron.spi.NeutronSecurityRule;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.HasDirection.Direction;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.Contract;
19 public class NeutronSecurityRuleAwareTest extends GbpDataBrokerTest {
21 private static final String RULE_ID = "00000000-0000-0000-0000-000000000001";
22 private static final String RULE_TENANT_ID = "00000000-0000-0000-0000-000000000002";
23 private static final String RULE_GROUP_ID = "00000000-0000-0000-0000-000000000003";
26 public final void testIsDirectionOpposite_InIn() {
27 assertFalse(NeutronSecurityRuleAware.isDirectionOpposite(Direction.In, Direction.In));
31 public final void testIsDirectionOpposite_OutOut() {
32 assertFalse(NeutronSecurityRuleAware.isDirectionOpposite(Direction.Out, Direction.Out));
36 public final void testIsDirectionOpposite_InOut() {
37 assertTrue(NeutronSecurityRuleAware.isDirectionOpposite(Direction.In, Direction.Out));
41 public final void testIsDirectionOpposite_OutIn() {
42 assertTrue(NeutronSecurityRuleAware.isDirectionOpposite(Direction.Out, Direction.In));
46 public void testNeutronSecurityRuleCreatedAndDeleted() throws Exception {
47 DataBroker dataProvider = getDataBroker();
48 SecGroupDao secGroupDao = new SecGroupDao();
49 SecRuleDao secRuleDao = new SecRuleDao();
50 NeutronSecurityRuleAware neutronSecurityRuleAware =
51 new NeutronSecurityRuleAware(dataProvider, secRuleDao, secGroupDao);
53 //create security rule and put to DS
54 NeutronSecurityRule neutronRule = buildNeutronSecurityRule();
55 assertEquals(neutronSecurityRuleAware.canCreateNeutronSecurityRule(neutronRule),
57 neutronSecurityRuleAware.neutronSecurityRuleCreated(neutronRule);
60 PolicyAssert.assertContractExists(dataProvider, RULE_TENANT_ID, RULE_ID);
63 Contract readContract = ConfigDataStoreReader.readContract(dataProvider, RULE_TENANT_ID, RULE_ID).get();
64 assertNotNull(readContract);
65 assertEquals(readContract.getId().getValue(), RULE_ID);
67 assertEquals(neutronSecurityRuleAware.canUpdateNeutronSecurityRule(neutronRule, neutronRule),
68 StatusCode.BAD_REQUEST);
71 assertEquals(neutronSecurityRuleAware.canDeleteNeutronSecurityRule(neutronRule),
73 neutronSecurityRuleAware.neutronSecurityRuleDeleted(neutronRule);
74 PolicyAssert.assertContractNotExists(dataProvider, RULE_TENANT_ID, RULE_ID);
77 //create neutron security rule
78 private NeutronSecurityRule buildNeutronSecurityRule() {
79 NeutronSecurityRule neutronSecurityRule = new NeutronSecurityRule();
80 neutronSecurityRule.setSecurityRuleUUID(RULE_ID);
81 neutronSecurityRule.setSecurityRuleTenantID(RULE_TENANT_ID);
82 neutronSecurityRule.setSecurityRuleGroupID(RULE_GROUP_ID);
83 neutronSecurityRule.setSecurityRuleRemoteIpPrefix("192.0.0.1/24");
84 neutronSecurityRule.setSecurityRulePortMin(1000);
85 neutronSecurityRule.setSecurityRulePortMax(5000);
86 neutronSecurityRule.setSecurityRuleProtocol("tcp");
87 neutronSecurityRule.setSecurityRuleEthertype("IPv4");
88 neutronSecurityRule.setSecurityRuleDirection("ingress");
90 return neutronSecurityRule;