blob: 7b1b215e742dc14a7068a29c4104ce961cb92521 [file] [log] [blame]
Carmelo Casconeb7388bd2016-04-14 10:20:13 -07001/*
2 * Copyright 2014-2016 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.drivers.bmv2;
18
19import com.google.common.testing.EqualsTester;
20import org.junit.Test;
21import org.onlab.packet.MacAddress;
22import org.onosproject.bmv2.api.model.Bmv2Model;
23import org.onosproject.bmv2.api.runtime.Bmv2TableEntry;
24import org.onosproject.bmv2.api.runtime.Bmv2TernaryMatchParam;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.core.DefaultApplicationId;
27import org.onosproject.drivers.bmv2.translators.Bmv2DefaultFlowRuleTranslator;
28import org.onosproject.drivers.bmv2.translators.Bmv2FlowRuleTranslator;
29import org.onosproject.net.DeviceId;
30import org.onosproject.net.PortNumber;
31import org.onosproject.net.flow.DefaultFlowRule;
32import org.onosproject.net.flow.DefaultTrafficSelector;
33import org.onosproject.net.flow.DefaultTrafficTreatment;
34import org.onosproject.net.flow.FlowRule;
35import org.onosproject.net.flow.TrafficSelector;
36import org.onosproject.net.flow.TrafficTreatment;
37
38import java.util.Random;
39
40import static org.hamcrest.CoreMatchers.equalTo;
41import static org.hamcrest.CoreMatchers.is;
42import static org.hamcrest.MatcherAssert.assertThat;
43
44/**
45 * Tests for {@link Bmv2DefaultFlowRuleTranslator}.
46 */
47public class Bmv2DefaultFlowRuleTranslatorTest {
48
49 private Random random = new Random();
50 private Bmv2FlowRuleTranslator translator = new Bmv2DefaultFlowRuleTranslator();
51 private Bmv2Model model = translator.config().model();
52
53
54 @Test
55 public void testCompiler() throws Exception {
56
57 DeviceId deviceId = DeviceId.NONE;
58 ApplicationId appId = new DefaultApplicationId(1, "test");
59 int tableId = 0;
60 MacAddress ethDstMac = MacAddress.valueOf(random.nextLong());
61 MacAddress ethSrcMac = MacAddress.valueOf(random.nextLong());
62 short ethType = (short) (0x0000FFFF & random.nextInt());
63 short outPort = (short) random.nextInt(65);
64 short inPort = (short) random.nextInt(65);
65 int timeout = random.nextInt(100);
66 int priority = random.nextInt(100);
67
68 TrafficSelector matchInPort1 = DefaultTrafficSelector
69 .builder()
70 .matchInPort(PortNumber.portNumber(inPort))
71 .matchEthDst(ethDstMac)
72 .matchEthSrc(ethSrcMac)
73 .matchEthType(ethType)
74 .build();
75
76 TrafficTreatment outPort2 = DefaultTrafficTreatment
77 .builder()
78 .setOutput(PortNumber.portNumber(outPort))
79 .build();
80
81 FlowRule rule1 = DefaultFlowRule.builder()
82 .forDevice(deviceId)
83 .forTable(tableId)
84 .fromApp(appId)
85 .withSelector(matchInPort1)
86 .withTreatment(outPort2)
87 .makeTemporary(timeout)
88 .withPriority(priority)
89 .build();
90
91 FlowRule rule2 = DefaultFlowRule.builder()
92 .forDevice(deviceId)
93 .forTable(tableId)
94 .fromApp(appId)
95 .withSelector(matchInPort1)
96 .withTreatment(outPort2)
97 .makeTemporary(timeout)
98 .withPriority(priority)
99 .build();
100
101 Bmv2TableEntry entry1 = translator.translate(rule1);
102 Bmv2TableEntry entry2 = translator.translate(rule1);
103
104 // check equality, i.e. same rules must produce same entries
105 new EqualsTester()
106 .addEqualityGroup(rule1, rule2)
107 .addEqualityGroup(entry1, entry2)
108 .testEquals();
109
110 int numMatchParams = model.table(0).keys().size();
111 // parse values stored in entry1
112 Bmv2TernaryMatchParam inPortParam = (Bmv2TernaryMatchParam) entry1.matchKey().matchParams().get(0);
113 Bmv2TernaryMatchParam ethDstParam = (Bmv2TernaryMatchParam) entry1.matchKey().matchParams().get(1);
114 Bmv2TernaryMatchParam ethSrcParam = (Bmv2TernaryMatchParam) entry1.matchKey().matchParams().get(2);
115 Bmv2TernaryMatchParam ethTypeParam = (Bmv2TernaryMatchParam) entry1.matchKey().matchParams().get(3);
116 double expectedTimeout = (double) (model.table(0).hasTimeouts() ? rule1.timeout() : -1);
117
118 // check that the number of parameters in the entry is the same as the number of table keys
119 assertThat("Incorrect number of match parameters",
120 entry1.matchKey().matchParams().size(), is(equalTo(numMatchParams)));
121
122 // check that values stored in entry are the same used for the flow rule
123 assertThat("Incorrect inPort match param value",
124 inPortParam.value().asReadOnlyBuffer().getShort(), is(equalTo(inPort)));
125 assertThat("Incorrect ethDestMac match param value",
126 ethDstParam.value().asArray(), is(equalTo(ethDstMac.toBytes())));
127 assertThat("Incorrect ethSrcMac match param value",
128 ethSrcParam.value().asArray(), is(equalTo(ethSrcMac.toBytes())));
129 assertThat("Incorrect ethType match param value",
130 ethTypeParam.value().asReadOnlyBuffer().getShort(), is(equalTo(ethType)));
131 assertThat("Incorrect priority value",
132 entry1.priority(), is(equalTo(rule1.priority())));
133 assertThat("Incorrect timeout value",
134 entry1.timeout(), is(equalTo(expectedTimeout)));
135
136 }
137}