ad9ca3fb4c9cc73f45af1c2075c9ca13fecbebdc
[groupbasedpolicy.git] / neutron-mapper / src / test / java / org / opendaylight / groupbasedpolicy / neutron / mapper / mapping / rule / NeutronSecurityRuleAwareTest.java
1 package org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.rule;
2
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;
7
8 import org.junit.Test;
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;
18
19 public class NeutronSecurityRuleAwareTest extends GbpDataBrokerTest {
20
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";
24
25     @Test
26     public final void testIsDirectionOpposite_InIn() {
27         assertFalse(NeutronSecurityRuleAware.isDirectionOpposite(Direction.In, Direction.In));
28     }
29
30     @Test
31     public final void testIsDirectionOpposite_OutOut() {
32         assertFalse(NeutronSecurityRuleAware.isDirectionOpposite(Direction.Out, Direction.Out));
33     }
34
35     @Test
36     public final void testIsDirectionOpposite_InOut() {
37         assertTrue(NeutronSecurityRuleAware.isDirectionOpposite(Direction.In, Direction.Out));
38     }
39
40     @Test
41     public final void testIsDirectionOpposite_OutIn() {
42         assertTrue(NeutronSecurityRuleAware.isDirectionOpposite(Direction.Out, Direction.In));
43     }
44
45     @Test
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);
52
53         //create security rule and put to DS
54         NeutronSecurityRule neutronRule = buildNeutronSecurityRule();
55         assertEquals(neutronSecurityRuleAware.canCreateNeutronSecurityRule(neutronRule),
56                 StatusCode.OK);
57         neutronSecurityRuleAware.neutronSecurityRuleCreated(neutronRule);
58
59         //read security rule
60         PolicyAssert.assertContractExists(dataProvider, RULE_TENANT_ID, RULE_ID);
61
62         //compare
63         Contract readContract = ConfigDataStoreReader.readContract(dataProvider, RULE_TENANT_ID, RULE_ID).get();
64         assertNotNull(readContract);
65         assertEquals(readContract.getId().getValue(), RULE_ID);
66
67         assertEquals(neutronSecurityRuleAware.canUpdateNeutronSecurityRule(neutronRule, neutronRule),
68                 StatusCode.BAD_REQUEST);
69
70         //delete rule
71         assertEquals(neutronSecurityRuleAware.canDeleteNeutronSecurityRule(neutronRule),
72                 StatusCode.OK);
73         neutronSecurityRuleAware.neutronSecurityRuleDeleted(neutronRule);
74         PolicyAssert.assertContractNotExists(dataProvider, RULE_TENANT_ID, RULE_ID);
75     }
76
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");
89
90         return neutronSecurityRule;
91     }
92
93 }