blob: 2f1969691ce547c76bcf36346a6fb1c508a740dd [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
Carmelo Casconed4e7a772016-05-03 11:21:29 -070019import com.eclipsesource.json.Json;
20import com.eclipsesource.json.JsonObject;
Carmelo Casconeb7388bd2016-04-14 10:20:13 -070021import com.google.common.testing.EqualsTester;
Carmelo Casconed4e7a772016-05-03 11:21:29 -070022import org.junit.Before;
Carmelo Casconeb7388bd2016-04-14 10:20:13 -070023import org.junit.Test;
24import org.onlab.packet.MacAddress;
25import org.onosproject.bmv2.api.model.Bmv2Model;
26import org.onosproject.bmv2.api.runtime.Bmv2TableEntry;
27import org.onosproject.bmv2.api.runtime.Bmv2TernaryMatchParam;
28import org.onosproject.core.ApplicationId;
29import org.onosproject.core.DefaultApplicationId;
30import org.onosproject.drivers.bmv2.translators.Bmv2DefaultFlowRuleTranslator;
31import org.onosproject.drivers.bmv2.translators.Bmv2FlowRuleTranslator;
Carmelo Casconed4e7a772016-05-03 11:21:29 -070032import org.onosproject.drivers.bmv2.translators.Bmv2SimpleTranslatorConfig;
Carmelo Casconeb7388bd2016-04-14 10:20:13 -070033import org.onosproject.net.DeviceId;
34import org.onosproject.net.PortNumber;
35import org.onosproject.net.flow.DefaultFlowRule;
36import org.onosproject.net.flow.DefaultTrafficSelector;
37import org.onosproject.net.flow.DefaultTrafficTreatment;
38import org.onosproject.net.flow.FlowRule;
39import org.onosproject.net.flow.TrafficSelector;
40import org.onosproject.net.flow.TrafficTreatment;
41
Carmelo Casconed4e7a772016-05-03 11:21:29 -070042import java.io.BufferedReader;
43import java.io.IOException;
44import java.io.InputStream;
45import java.io.InputStreamReader;
Carmelo Casconeb7388bd2016-04-14 10:20:13 -070046import java.util.Random;
47
48import static org.hamcrest.CoreMatchers.equalTo;
49import static org.hamcrest.CoreMatchers.is;
50import static org.hamcrest.MatcherAssert.assertThat;
51
52/**
53 * Tests for {@link Bmv2DefaultFlowRuleTranslator}.
54 */
55public class Bmv2DefaultFlowRuleTranslatorTest {
56
Carmelo Casconed4e7a772016-05-03 11:21:29 -070057 private static final String JSON_CONFIG_PATH = "/simple.json";
Carmelo Casconeb7388bd2016-04-14 10:20:13 -070058 private Random random = new Random();
Carmelo Casconed4e7a772016-05-03 11:21:29 -070059 private Bmv2Model model;
60 private Bmv2FlowRuleTranslator.TranslatorConfig config;
61 private Bmv2FlowRuleTranslator translator;
62
63 @Before
64 public void setUp() throws Exception {
65 InputStream inputStream = Bmv2SimpleTranslatorConfig.class
66 .getResourceAsStream(JSON_CONFIG_PATH);
67 InputStreamReader reader = new InputStreamReader(inputStream);
68 BufferedReader bufReader = new BufferedReader(reader);
69 JsonObject json = null;
70 try {
71 json = Json.parse(bufReader).asObject();
72 } catch (IOException e) {
73 throw new RuntimeException("Unable to parse JSON file: " + e.getMessage());
74 }
75
76 this.model = Bmv2Model.parse(json);
77 this.config = new Bmv2SimpleTranslatorConfig(model);
78 this.translator = new Bmv2DefaultFlowRuleTranslator(config);
79
80 }
Carmelo Casconeb7388bd2016-04-14 10:20:13 -070081
82
83 @Test
84 public void testCompiler() throws Exception {
85
86 DeviceId deviceId = DeviceId.NONE;
87 ApplicationId appId = new DefaultApplicationId(1, "test");
88 int tableId = 0;
89 MacAddress ethDstMac = MacAddress.valueOf(random.nextLong());
90 MacAddress ethSrcMac = MacAddress.valueOf(random.nextLong());
91 short ethType = (short) (0x0000FFFF & random.nextInt());
92 short outPort = (short) random.nextInt(65);
93 short inPort = (short) random.nextInt(65);
94 int timeout = random.nextInt(100);
95 int priority = random.nextInt(100);
96
97 TrafficSelector matchInPort1 = DefaultTrafficSelector
98 .builder()
99 .matchInPort(PortNumber.portNumber(inPort))
100 .matchEthDst(ethDstMac)
101 .matchEthSrc(ethSrcMac)
102 .matchEthType(ethType)
103 .build();
104
105 TrafficTreatment outPort2 = DefaultTrafficTreatment
106 .builder()
107 .setOutput(PortNumber.portNumber(outPort))
108 .build();
109
110 FlowRule rule1 = DefaultFlowRule.builder()
111 .forDevice(deviceId)
112 .forTable(tableId)
113 .fromApp(appId)
114 .withSelector(matchInPort1)
115 .withTreatment(outPort2)
116 .makeTemporary(timeout)
117 .withPriority(priority)
118 .build();
119
120 FlowRule rule2 = DefaultFlowRule.builder()
121 .forDevice(deviceId)
122 .forTable(tableId)
123 .fromApp(appId)
124 .withSelector(matchInPort1)
125 .withTreatment(outPort2)
126 .makeTemporary(timeout)
127 .withPriority(priority)
128 .build();
129
130 Bmv2TableEntry entry1 = translator.translate(rule1);
131 Bmv2TableEntry entry2 = translator.translate(rule1);
132
133 // check equality, i.e. same rules must produce same entries
134 new EqualsTester()
135 .addEqualityGroup(rule1, rule2)
136 .addEqualityGroup(entry1, entry2)
137 .testEquals();
138
139 int numMatchParams = model.table(0).keys().size();
140 // parse values stored in entry1
141 Bmv2TernaryMatchParam inPortParam = (Bmv2TernaryMatchParam) entry1.matchKey().matchParams().get(0);
142 Bmv2TernaryMatchParam ethDstParam = (Bmv2TernaryMatchParam) entry1.matchKey().matchParams().get(1);
143 Bmv2TernaryMatchParam ethSrcParam = (Bmv2TernaryMatchParam) entry1.matchKey().matchParams().get(2);
144 Bmv2TernaryMatchParam ethTypeParam = (Bmv2TernaryMatchParam) entry1.matchKey().matchParams().get(3);
145 double expectedTimeout = (double) (model.table(0).hasTimeouts() ? rule1.timeout() : -1);
146
147 // check that the number of parameters in the entry is the same as the number of table keys
148 assertThat("Incorrect number of match parameters",
149 entry1.matchKey().matchParams().size(), is(equalTo(numMatchParams)));
150
151 // check that values stored in entry are the same used for the flow rule
152 assertThat("Incorrect inPort match param value",
153 inPortParam.value().asReadOnlyBuffer().getShort(), is(equalTo(inPort)));
154 assertThat("Incorrect ethDestMac match param value",
155 ethDstParam.value().asArray(), is(equalTo(ethDstMac.toBytes())));
156 assertThat("Incorrect ethSrcMac match param value",
157 ethSrcParam.value().asArray(), is(equalTo(ethSrcMac.toBytes())));
158 assertThat("Incorrect ethType match param value",
159 ethTypeParam.value().asReadOnlyBuffer().getShort(), is(equalTo(ethType)));
160 assertThat("Incorrect priority value",
161 entry1.priority(), is(equalTo(rule1.priority())));
162 assertThat("Incorrect timeout value",
163 entry1.timeout(), is(equalTo(expectedTimeout)));
164
165 }
166}