Carmelo Cascone | b7388bd | 2016-04-14 10:20:13 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.drivers.bmv2; |
| 18 | |
Carmelo Cascone | d4e7a77 | 2016-05-03 11:21:29 -0700 | [diff] [blame^] | 19 | import com.eclipsesource.json.Json; |
| 20 | import com.eclipsesource.json.JsonObject; |
Carmelo Cascone | b7388bd | 2016-04-14 10:20:13 -0700 | [diff] [blame] | 21 | import com.google.common.testing.EqualsTester; |
Carmelo Cascone | d4e7a77 | 2016-05-03 11:21:29 -0700 | [diff] [blame^] | 22 | import org.junit.Before; |
Carmelo Cascone | b7388bd | 2016-04-14 10:20:13 -0700 | [diff] [blame] | 23 | import org.junit.Test; |
| 24 | import org.onlab.packet.MacAddress; |
| 25 | import org.onosproject.bmv2.api.model.Bmv2Model; |
| 26 | import org.onosproject.bmv2.api.runtime.Bmv2TableEntry; |
| 27 | import org.onosproject.bmv2.api.runtime.Bmv2TernaryMatchParam; |
| 28 | import org.onosproject.core.ApplicationId; |
| 29 | import org.onosproject.core.DefaultApplicationId; |
| 30 | import org.onosproject.drivers.bmv2.translators.Bmv2DefaultFlowRuleTranslator; |
| 31 | import org.onosproject.drivers.bmv2.translators.Bmv2FlowRuleTranslator; |
Carmelo Cascone | d4e7a77 | 2016-05-03 11:21:29 -0700 | [diff] [blame^] | 32 | import org.onosproject.drivers.bmv2.translators.Bmv2SimpleTranslatorConfig; |
Carmelo Cascone | b7388bd | 2016-04-14 10:20:13 -0700 | [diff] [blame] | 33 | import org.onosproject.net.DeviceId; |
| 34 | import org.onosproject.net.PortNumber; |
| 35 | import org.onosproject.net.flow.DefaultFlowRule; |
| 36 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 37 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 38 | import org.onosproject.net.flow.FlowRule; |
| 39 | import org.onosproject.net.flow.TrafficSelector; |
| 40 | import org.onosproject.net.flow.TrafficTreatment; |
| 41 | |
Carmelo Cascone | d4e7a77 | 2016-05-03 11:21:29 -0700 | [diff] [blame^] | 42 | import java.io.BufferedReader; |
| 43 | import java.io.IOException; |
| 44 | import java.io.InputStream; |
| 45 | import java.io.InputStreamReader; |
Carmelo Cascone | b7388bd | 2016-04-14 10:20:13 -0700 | [diff] [blame] | 46 | import java.util.Random; |
| 47 | |
| 48 | import static org.hamcrest.CoreMatchers.equalTo; |
| 49 | import static org.hamcrest.CoreMatchers.is; |
| 50 | import static org.hamcrest.MatcherAssert.assertThat; |
| 51 | |
| 52 | /** |
| 53 | * Tests for {@link Bmv2DefaultFlowRuleTranslator}. |
| 54 | */ |
| 55 | public class Bmv2DefaultFlowRuleTranslatorTest { |
| 56 | |
Carmelo Cascone | d4e7a77 | 2016-05-03 11:21:29 -0700 | [diff] [blame^] | 57 | private static final String JSON_CONFIG_PATH = "/simple.json"; |
Carmelo Cascone | b7388bd | 2016-04-14 10:20:13 -0700 | [diff] [blame] | 58 | private Random random = new Random(); |
Carmelo Cascone | d4e7a77 | 2016-05-03 11:21:29 -0700 | [diff] [blame^] | 59 | 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 Cascone | b7388bd | 2016-04-14 10:20:13 -0700 | [diff] [blame] | 81 | |
| 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 | } |