2 * Copyright (c) 2015 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.rule;
10 import static org.junit.Assert.assertEquals;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.opendaylight.groupbasedpolicy.api.sf.EtherTypeClassifierDefinition;
15 import org.opendaylight.groupbasedpolicy.api.sf.IpProtoClassifierDefinition;
16 import org.opendaylight.groupbasedpolicy.api.sf.L4ClassifierDefinition;
17 import org.opendaylight.groupbasedpolicy.neutron.mapper.util.MappingUtils;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClassifierName;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClauseName;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.HasDirection.Direction;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionIngress;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeV4;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolTcp;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.SecurityRuleAttributes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRuleBuilder;
30 public class SecRuleNameDecoderTest {
32 private SecurityRuleBuilder secRule;
35 public void setUp() throws Exception {
36 secRule = new SecurityRuleBuilder().setUuid(new Uuid("01234567-abcd-ef01-0123-0123456789ab"));
40 public final void testGetClassifierRefName() {
41 secRule.setDirection(DirectionIngress.class);
42 secRule.setEthertype(EthertypeV4.class);
43 ClassifierName clsfInstanceName = SecRuleNameDecoder.getClassifierInstanceName(secRule.build());
44 String crName = new StringBuilder().append(Direction.In.name())
45 .append(MappingUtils.NAME_DOUBLE_DELIMETER)
46 .append(clsfInstanceName.getValue())
48 ClassifierName expectedClsfRefName = new ClassifierName(crName);
49 assertEquals(expectedClsfRefName, SecRuleNameDecoder.getClassifierRefName(secRule.build()));
53 public final void testGetClassifierInstanceName() {
54 secRule.setDirection(DirectionIngress.class);
55 secRule.setEthertype(EthertypeV4.class);
56 SecurityRuleAttributes.Protocol protocolTcp = new SecurityRuleAttributes.Protocol(ProtocolTcp.class);
57 secRule.setProtocol(protocolTcp);
58 secRule.setPortRangeMin(8010);
59 secRule.setPortRangeMax(8020);
60 StringBuilder frmtBuilder = new StringBuilder();
61 frmtBuilder.append(L4ClassifierDefinition.DEFINITION.getName().getValue())
62 .append(MappingUtils.NAME_DELIMETER)
63 .append(L4ClassifierDefinition.DST_PORT_RANGE_PARAM)
64 .append(SecRuleNameDecoder.MIN_PORT)
65 .append(MappingUtils.NAME_VALUE_DELIMETER)
67 .append(SecRuleNameDecoder.MAX_PORT)
68 .append(MappingUtils.NAME_VALUE_DELIMETER)
70 .append(MappingUtils.NAME_DOUBLE_DELIMETER)
71 .append(IpProtoClassifierDefinition.DEFINITION.getName().getValue())
72 .append(MappingUtils.NAME_VALUE_DELIMETER)
74 .append(MappingUtils.NAME_DOUBLE_DELIMETER)
75 .append(EtherTypeClassifierDefinition.DEFINITION.getName().getValue())
76 .append(MappingUtils.NAME_VALUE_DELIMETER)
78 String frmtClsfName = String.format(frmtBuilder.toString(), 8010, 8020,
79 secRule.getProtocol().getIdentityref().getSimpleName(), secRule.getEthertype().getSimpleName());
80 ClassifierName expectedClsfInstanceName = new ClassifierName(frmtClsfName);
81 assertEquals(expectedClsfInstanceName, SecRuleNameDecoder.getClassifierInstanceName(secRule.build()));
85 public final void testGetClauseName_noRemoteIpPrefix() {
86 secRule.setDirection(DirectionIngress.class);
87 secRule.setEthertype(EthertypeV4.class);
88 ClauseName expectedClauseName = new ClauseName(SecRuleNameDecoder.getSubjectName(secRule.build()));
89 assertEquals(expectedClauseName, SecRuleNameDecoder.getClauseName(secRule.build()));
93 public final void testGetClauseName_remoteIpPrefix() {
94 secRule.setDirection(DirectionIngress.class);
95 secRule.setEthertype(EthertypeV4.class);
96 secRule.setRemoteIpPrefix(new IpPrefix(new Ipv4Prefix("10.0.0.0/8")));
97 ClauseName expectedClauseName = new ClauseName(SecRuleNameDecoder.getSubjectName(secRule.build()).getValue()
98 + MappingUtils.NAME_DOUBLE_DELIMETER + "10.0.0.0_8");
99 assertEquals(expectedClauseName, SecRuleNameDecoder.getClauseName(secRule.build()));