e63f3b3c1036a6e03dcc4738d79011cc16f09758
[groupbasedpolicy.git] / neutron-mapper / src / test / java / org / opendaylight / groupbasedpolicy / neutron / mapper / mapping / rule / SecRuleNameDecoderTest.java
1 package org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.rule;
2
3 import static org.junit.Assert.assertEquals;
4
5 import org.junit.Before;
6 import org.junit.Test;
7 import org.opendaylight.groupbasedpolicy.api.sf.EtherTypeClassifierDefinition;
8 import org.opendaylight.groupbasedpolicy.api.sf.IpProtoClassifierDefinition;
9 import org.opendaylight.groupbasedpolicy.api.sf.L4ClassifierDefinition;
10 import org.opendaylight.groupbasedpolicy.neutron.mapper.util.MappingUtils;
11 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
12 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClassifierName;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClauseName;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.HasDirection.Direction;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionIngress;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeV4;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolTcp;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.SecurityRuleAttributes;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRuleBuilder;
22
23 public class SecRuleNameDecoderTest {
24
25     private SecurityRuleBuilder secRule;
26
27     @Before
28     public void setUp() throws Exception {
29         secRule = new SecurityRuleBuilder().setUuid(new Uuid("01234567-abcd-ef01-0123-0123456789ab"));
30     }
31
32     @Test
33     public final void testGetClassifierRefName() {
34         secRule.setDirection(DirectionIngress.class);
35         secRule.setEthertype(EthertypeV4.class);
36         ClassifierName clsfInstanceName = SecRuleNameDecoder.getClassifierInstanceName(secRule.build());
37         String crName = new StringBuilder().append(Direction.In.name())
38             .append(MappingUtils.NAME_DOUBLE_DELIMETER)
39             .append(clsfInstanceName.getValue())
40             .toString();
41         ClassifierName expectedClsfRefName = new ClassifierName(crName);
42         assertEquals(expectedClsfRefName, SecRuleNameDecoder.getClassifierRefName(secRule.build()));
43     }
44
45     @Test
46     public final void testGetClassifierInstanceName() {
47         secRule.setDirection(DirectionIngress.class);
48         secRule.setEthertype(EthertypeV4.class);
49         SecurityRuleAttributes.Protocol protocolTcp = new SecurityRuleAttributes.Protocol(ProtocolTcp.class);
50         secRule.setProtocol(protocolTcp);
51         secRule.setPortRangeMin(8010);
52         secRule.setPortRangeMax(8020);
53         StringBuilder frmtBuilder = new StringBuilder();
54         frmtBuilder.append(L4ClassifierDefinition.DEFINITION.getName().getValue())
55             .append(MappingUtils.NAME_DELIMETER)
56             .append(L4ClassifierDefinition.DST_PORT_RANGE_PARAM)
57             .append(SecRuleNameDecoder.MIN_PORT)
58             .append(MappingUtils.NAME_VALUE_DELIMETER)
59             .append("%d")
60             .append(SecRuleNameDecoder.MAX_PORT)
61             .append(MappingUtils.NAME_VALUE_DELIMETER)
62             .append("%d")
63             .append(MappingUtils.NAME_DOUBLE_DELIMETER)
64             .append(IpProtoClassifierDefinition.DEFINITION.getName().getValue())
65             .append(MappingUtils.NAME_VALUE_DELIMETER)
66             .append("%s")
67             .append(MappingUtils.NAME_DOUBLE_DELIMETER)
68             .append(EtherTypeClassifierDefinition.DEFINITION.getName().getValue())
69             .append(MappingUtils.NAME_VALUE_DELIMETER)
70             .append("%s");
71         String frmtClsfName = String.format(frmtBuilder.toString(), 8010, 8020, secRule.getProtocol().getIdentityref().getSimpleName(),
72                 secRule.getEthertype().getSimpleName());
73         ClassifierName expectedClsfInstanceName = new ClassifierName(frmtClsfName);
74         assertEquals(expectedClsfInstanceName, SecRuleNameDecoder.getClassifierInstanceName(secRule.build()));
75     }
76
77     @Test
78     public final void testGetClauseName_noRemoteIpPrefix() {
79         secRule.setDirection(DirectionIngress.class);
80         secRule.setEthertype(EthertypeV4.class);
81         ClauseName expectedClauseName = new ClauseName(SecRuleNameDecoder.getSubjectName(secRule.build()));
82         assertEquals(expectedClauseName, SecRuleNameDecoder.getClauseName(secRule.build()));
83     }
84
85     @Test
86     public final void testGetClauseName_remoteIpPrefix() {
87         secRule.setDirection(DirectionIngress.class);
88         secRule.setEthertype(EthertypeV4.class);
89         secRule.setRemoteIpPrefix(new IpPrefix(new Ipv4Prefix("10.0.0.0/8")));
90         ClauseName expectedClauseName = new ClauseName(SecRuleNameDecoder.getSubjectName(secRule.build()).getValue()
91                 + MappingUtils.NAME_DOUBLE_DELIMETER + "10.0.0.0_8");
92         assertEquals(expectedClauseName, SecRuleNameDecoder.getClauseName(secRule.build()));
93     }
94 }