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.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;
23 public class SecRuleNameDecoderTest {
25 private SecurityRuleBuilder secRule;
28 public void setUp() throws Exception {
29 secRule = new SecurityRuleBuilder().setUuid(new Uuid("01234567-abcd-ef01-0123-0123456789ab"));
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())
41 ClassifierName expectedClsfRefName = new ClassifierName(crName);
42 assertEquals(expectedClsfRefName, SecRuleNameDecoder.getClassifierRefName(secRule.build()));
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)
60 .append(SecRuleNameDecoder.MAX_PORT)
61 .append(MappingUtils.NAME_VALUE_DELIMETER)
63 .append(MappingUtils.NAME_DOUBLE_DELIMETER)
64 .append(IpProtoClassifierDefinition.DEFINITION.getName().getValue())
65 .append(MappingUtils.NAME_VALUE_DELIMETER)
67 .append(MappingUtils.NAME_DOUBLE_DELIMETER)
68 .append(EtherTypeClassifierDefinition.DEFINITION.getName().getValue())
69 .append(MappingUtils.NAME_VALUE_DELIMETER)
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()));
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()));
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()));