1 package org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.rule;
3 import static org.junit.Assert.assertEquals;
5 import org.junit.Before;
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.rev100924.IpPrefix;
12 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.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.security.rules.attributes.security.rules.SecurityRuleBuilder;
22 public class SecRuleNameDecoderTest {
24 private SecurityRuleBuilder secRule;
27 public void setUp() throws Exception {
28 secRule = new SecurityRuleBuilder().setId(new Uuid("01234567-abcd-ef01-0123-0123456789ab"));
32 public final void testGetClassifierRefName() {
33 secRule.setDirection(DirectionIngress.class);
34 secRule.setEthertype(EthertypeV4.class);
35 ClassifierName clsfInstanceName = SecRuleNameDecoder.getClassifierInstanceName(secRule.build());
36 String crName = new StringBuilder().append(Direction.In.name())
37 .append(MappingUtils.NAME_DOUBLE_DELIMETER)
38 .append(clsfInstanceName.getValue())
40 ClassifierName expectedClsfRefName = new ClassifierName(crName);
41 assertEquals(expectedClsfRefName, SecRuleNameDecoder.getClassifierRefName(secRule.build()));
45 public final void testGetClassifierInstanceName() {
46 secRule.setDirection(DirectionIngress.class);
47 secRule.setEthertype(EthertypeV4.class);
48 secRule.setProtocol(ProtocolTcp.class);
49 secRule.setPortRangeMin(8010);
50 secRule.setPortRangeMax(8020);
51 StringBuilder frmtBuilder = new StringBuilder();
52 frmtBuilder.append(L4ClassifierDefinition.DEFINITION.getName().getValue())
53 .append(MappingUtils.NAME_DELIMETER)
54 .append(L4ClassifierDefinition.DST_PORT_RANGE_PARAM)
55 .append(SecRuleNameDecoder.MIN_PORT)
56 .append(MappingUtils.NAME_VALUE_DELIMETER)
58 .append(SecRuleNameDecoder.MAX_PORT)
59 .append(MappingUtils.NAME_VALUE_DELIMETER)
61 .append(MappingUtils.NAME_DOUBLE_DELIMETER)
62 .append(IpProtoClassifierDefinition.DEFINITION.getName().getValue())
63 .append(MappingUtils.NAME_VALUE_DELIMETER)
65 .append(MappingUtils.NAME_DOUBLE_DELIMETER)
66 .append(EtherTypeClassifierDefinition.DEFINITION.getName().getValue())
67 .append(MappingUtils.NAME_VALUE_DELIMETER)
69 String frmtClsfName = String.format(frmtBuilder.toString(), 8010, 8020, secRule.getProtocol().getSimpleName(),
70 secRule.getEthertype().getSimpleName());
71 ClassifierName expectedClsfInstanceName = new ClassifierName(frmtClsfName);
72 assertEquals(expectedClsfInstanceName, SecRuleNameDecoder.getClassifierInstanceName(secRule.build()));
76 public final void testGetClauseName_noRemoteIpPrefix() {
77 secRule.setDirection(DirectionIngress.class);
78 secRule.setEthertype(EthertypeV4.class);
79 ClauseName expectedClauseName = new ClauseName(SecRuleNameDecoder.getSubjectName(secRule.build()));
80 assertEquals(expectedClauseName, SecRuleNameDecoder.getClauseName(secRule.build()));
84 public final void testGetClauseName_remoteIpPrefix() {
85 secRule.setDirection(DirectionIngress.class);
86 secRule.setEthertype(EthertypeV4.class);
87 secRule.setRemoteIpPrefix(new IpPrefix(new Ipv4Prefix("10.0.0.0/8")));
88 ClauseName expectedClauseName = new ClauseName(SecRuleNameDecoder.getSubjectName(secRule.build()).getValue()
89 + MappingUtils.NAME_DOUBLE_DELIMETER + "10.0.0.0_8");
90 assertEquals(expectedClauseName, SecRuleNameDecoder.getClauseName(secRule.build()));