Fix fileEncoding violations for checkstyle
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / sf / EtherTypeClassifierTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertSame;
12
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17
18 import com.google.common.collect.ImmutableMap;
19 import org.junit.Rule;
20 import org.junit.Test;
21 import org.junit.rules.ExpectedException;
22 import org.opendaylight.groupbasedpolicy.api.sf.EtherTypeClassifierDefinition;
23 import org.opendaylight.groupbasedpolicy.api.sf.IpProtoClassifierDefinition;
24 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValue;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValueBuilder;
28
29 public class EtherTypeClassifierTest {
30
31     @Rule
32     public ExpectedException thrown = ExpectedException.none();
33
34     @Test
35     public void testUpdate() {
36         List<MatchBuilder> matches = new ArrayList<>();
37         Map<String, ParameterValue> params = new HashMap<>();
38         matches.add(
39                 new MatchBuilder().setIpMatch(ClassifierTestUtils.createIpMatch(ClassifierTestUtils.TCP.shortValue())));
40         params.putAll(
41                 ClassifierTestUtils.createIntValueParam(EtherTypeClassifierDefinition.ETHERTYPE_PARAM, FlowUtils.IPv4));
42
43         List<MatchBuilder> updated = Classifier.ETHER_TYPE_CL.update(matches, params);
44
45         assertEquals(1, updated.size());
46         MatchBuilder first = updated.get(0);
47         assertEquals(ClassifierTestUtils.IPV4_ETH_TYPE, first.getEthernetMatch().getEthernetType());
48         assertSame(ClassifierTestUtils.TCP, first.getIpMatch().getIpProtocol().longValue());
49     }
50
51     @Test
52     public void testUpdate_overrideBySameValue() {
53         List<MatchBuilder> matches = new ArrayList<>();
54         Map<String, ParameterValue> params = new HashMap<>();
55         matches.add(new MatchBuilder()
56             .setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV6_ETH_TYPE))
57             .setIpMatch(ClassifierTestUtils.createIpMatch(ClassifierTestUtils.UDP.shortValue())));
58         params.putAll(
59                 ClassifierTestUtils.createIntValueParam(EtherTypeClassifierDefinition.ETHERTYPE_PARAM, FlowUtils.IPv6));
60
61         List<MatchBuilder> updated = Classifier.ETHER_TYPE_CL.update(matches, params);
62
63         assertEquals(1, updated.size());
64         MatchBuilder first = updated.get(0);
65         assertEquals(ClassifierTestUtils.IPV6_ETH_TYPE, first.getEthernetMatch().getEthernetType());
66         assertSame(ClassifierTestUtils.UDP, first.getIpMatch().getIpProtocol().longValue());
67     }
68
69     @Test
70     public void testUpdate_overrideByDifferentValue() {
71         List<MatchBuilder> matches = new ArrayList<>();
72         Map<String, ParameterValue> params = new HashMap<>();
73         matches.add(new MatchBuilder()
74             .setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE))
75             .setIpMatch(ClassifierTestUtils.createIpMatch(ClassifierTestUtils.SCTP.shortValue())));
76         params.putAll(
77                 ClassifierTestUtils.createIntValueParam(EtherTypeClassifierDefinition.ETHERTYPE_PARAM, FlowUtils.IPv6));
78
79         thrown.expect(IllegalArgumentException.class);
80         thrown.expectMessage(Classifier.MSG_CLASSIFICATION_CONFLICT_DETECTED);
81         Classifier.ETHER_TYPE_CL.update(matches, params);
82     }
83
84     @Test
85     public void testCheckPresenceOfRequiredParameters_EtherTypeMissing() {
86         Map<String, ParameterValue> params = new HashMap<>();
87         params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
88                 ClassifierTestUtils.TCP));
89
90         thrown.expect(IllegalArgumentException.class);
91         thrown.expectMessage(Classifier.MSG_NOT_SPECIFIED);
92         Classifier.ETHER_TYPE_CL.checkPresenceOfRequiredParams(params);
93     }
94
95     @Test
96     public void testCheckPresenceOfRequiredParameters_EtherTypeNull() {
97         Map<String, ParameterValue> params = new HashMap<>();
98         params.putAll(
99                 ImmutableMap.of(EtherTypeClassifierDefinition.ETHERTYPE_PARAM, new ParameterValueBuilder().build()));
100
101         thrown.expect(IllegalArgumentException.class);
102         thrown.expectMessage(Classifier.MSG_PARAMETER_IS_NOT_PRESENT);
103         Classifier.ETHER_TYPE_CL.checkPresenceOfRequiredParams(params);
104     }
105 }