e2c50aebbde0bf15a3c402e31eb894b3a6d20b39
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / sf / ClassifierTest.java
1 /*
2  * Copyright (c) 2014 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
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf;
10
11 import static org.junit.Assert.assertEquals;
12
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Set;
20
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.groupbasedpolicy.api.sf.EtherTypeClassifierDefinition;
24 import org.opendaylight.groupbasedpolicy.api.sf.IpProtoClassifierDefinition;
25 import org.opendaylight.groupbasedpolicy.api.sf.L4ClassifierDefinition;
26 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValue;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatch;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
31
32 public class ClassifierTest {
33
34     List<MatchBuilder> matches;
35     Map<String, ParameterValue> params;
36
37     @Before
38     public void setUp() {
39         matches = new ArrayList<>(Collections.singletonList(new MatchBuilder()));
40         params = new HashMap<>();
41     }
42
43     @Test
44     public void updateMatchTest() {
45         Long dstRangeStart = Long.valueOf(8079);
46         Long dstRangeEnd = Long.valueOf(8081);
47         params.putAll(ClassifierTestUtils.createIntValueParam(EtherTypeClassifierDefinition.ETHERTYPE_PARAM,
48                 FlowUtils.IPv4));
49         params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
50                 ClassifierTestUtils.TCP));
51         params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.SRC_PORT_PARAM, 80));
52         params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM,
53                 dstRangeStart, dstRangeEnd));
54         ClassificationResult result = Classifier.L4_CL.updateMatch(matches, params);
55         assertEquals(true, result.isSuccessfull());
56         assertEquals(3, result.getMatchBuilders().size());
57         Set<Long> dstPorts = new HashSet<>();
58         for (MatchBuilder match : result.getMatchBuilders()) {
59             assertEquals(true, ClassifierTestUtils.IPV4_ETH_TYPE.equals(match.getEthernetMatch().getEthernetType()));
60             assertEquals(true, new TcpMatchBuilder((TcpMatch) match.getLayer4Match()).getTcpSourcePort()
61                 .getValue()
62                 .intValue() == 80);
63             dstPorts.add(Long.valueOf(new TcpMatchBuilder((TcpMatch) match.getLayer4Match()).getTcpDestinationPort()
64                 .getValue()
65                 .longValue()));
66         }
67         for (Long i = dstRangeStart; i <= dstRangeEnd; i++) {
68             assertEquals(true, dstPorts.contains((i)));
69         }
70     }
71 }