Move definitions of classifiers and actions to
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / sf / EtherTypeClassifierTest.java
1 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf;
2
3 import static org.junit.Assert.assertEquals;
4
5 import java.util.ArrayList;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9
10 import org.junit.Before;
11 import org.junit.Rule;
12 import org.junit.Test;
13 import org.junit.rules.ExpectedException;
14 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils;
15 import org.opendaylight.groupbasedpolicy.sf.classifiers.EtherTypeClassifierDefinition;
16 import org.opendaylight.groupbasedpolicy.sf.classifiers.IpProtoClassifierDefinition;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValue;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValueBuilder;
20
21 import com.google.common.collect.ImmutableMap;
22
23 public class EtherTypeClassifierTest {
24
25     @Rule
26     public ExpectedException thrown = ExpectedException.none();
27
28     List<MatchBuilder> matches;
29     Map<String, ParameterValue> params;
30
31     @Before
32     public void setUp() {
33         matches = new ArrayList<>();
34         params = new HashMap<>();
35     }
36
37     @Test
38     public void setValueTest() {
39         matches.add(new MatchBuilder().setIpMatch(ClassifierTestUtils.createIpMatch(ClassifierTestUtils.TCP.shortValue())));
40         params.putAll(ClassifierTestUtils.createIntValueParam(EtherTypeClassifierDefinition.ETHERTYPE_PARAM,
41                 FlowUtils.IPv4));
42         matches = Classifier.ETHER_TYPE_CL.update(matches, params);
43         assertEquals(true,
44                 ClassifierTestUtils.IPV4_ETH_TYPE.equals(matches.get(0).getEthernetMatch().getEthernetType()));
45         assertEquals(true, ClassifierTestUtils.TCP.equals(matches.get(0).getIpMatch().getIpProtocol().longValue()));
46     }
47
48     @Test
49     public void overrideByTheSameValueTest() {
50         matches.add(new MatchBuilder().setEthernetMatch(
51                 ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV6_ETH_TYPE)).setIpMatch(
52                 ClassifierTestUtils.createIpMatch(ClassifierTestUtils.UDP.shortValue())));
53         params.putAll(ClassifierTestUtils.createIntValueParam(EtherTypeClassifierDefinition.ETHERTYPE_PARAM,
54                 FlowUtils.IPv6));
55         matches = Classifier.ETHER_TYPE_CL.update(matches, params);
56         assertEquals(true,
57                 ClassifierTestUtils.IPV6_ETH_TYPE.equals(matches.get(0).getEthernetMatch().getEthernetType()));
58         assertEquals(true, ClassifierTestUtils.UDP.equals(matches.get(0).getIpMatch().getIpProtocol().longValue()));
59     }
60
61     @Test
62     public void overrideByDifferentValueTest() {
63         matches.add(new MatchBuilder().setEthernetMatch(
64                 ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE)).setIpMatch(
65                 ClassifierTestUtils.createIpMatch(ClassifierTestUtils.SCTP.shortValue())));
66         params.putAll(ClassifierTestUtils.createIntValueParam(EtherTypeClassifierDefinition.ETHERTYPE_PARAM,
67                 FlowUtils.IPv6));
68         thrown.expect(IllegalArgumentException.class);
69         thrown.expectMessage("Classification conflict detected");
70         matches = Classifier.ETHER_TYPE_CL.update(matches, params);
71     }
72
73     @Test
74     public void checkPresenceOfRequiredParameters1Test() {
75         params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
76                 ClassifierTestUtils.TCP));
77         thrown.expect(IllegalArgumentException.class);
78         thrown.expectMessage("not specified.");
79         Classifier.ETHER_TYPE_CL.checkPresenceOfRequiredParams(params);
80     }
81
82     @Test
83     public void checkPresenceOfRequiredParameters2Test() {
84         params.putAll(ImmutableMap.<String, ParameterValue>of(EtherTypeClassifierDefinition.ETHERTYPE_PARAM,
85                 new ParameterValueBuilder().build()));
86         thrown.expect(IllegalArgumentException.class);
87         thrown.expectMessage("Value of ethertype parameter is not present");
88         Classifier.ETHER_TYPE_CL.checkPresenceOfRequiredParams(params);
89     }
90 }