Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present 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.net.pi.impl; |
| 18 | |
| 19 | import com.google.common.testing.EqualsTester; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.onlab.packet.MacAddress; |
| 23 | import org.onosproject.core.ApplicationId; |
| 24 | import org.onosproject.core.DefaultApplicationId; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.PortNumber; |
| 27 | import org.onosproject.net.flow.DefaultFlowRule; |
| 28 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 29 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 30 | import org.onosproject.net.flow.FlowRule; |
| 31 | import org.onosproject.net.flow.TrafficSelector; |
| 32 | import org.onosproject.net.flow.TrafficTreatment; |
| 33 | import org.onosproject.net.pi.model.PiPipeconf; |
| 34 | import org.onosproject.net.pi.runtime.PiTableEntry; |
| 35 | import org.onosproject.net.pi.runtime.PiTernaryFieldMatch; |
| 36 | |
| 37 | import java.util.Optional; |
| 38 | import java.util.Random; |
| 39 | |
| 40 | import static org.hamcrest.CoreMatchers.equalTo; |
| 41 | import static org.hamcrest.CoreMatchers.is; |
| 42 | import static org.hamcrest.MatcherAssert.assertThat; |
| 43 | import static org.onosproject.net.pi.impl.MockInterpreter.*; |
| 44 | |
| 45 | /** |
| 46 | * Tests for {@link PiFlowRuleTranslator}. |
| 47 | */ |
| 48 | @SuppressWarnings("ConstantConditions") |
| 49 | public class PiFlowRuleTranslatorTest { |
| 50 | |
| 51 | private Random random = new Random(); |
| 52 | private PiPipeconf pipeconf; |
| 53 | |
| 54 | @Before |
| 55 | public void setUp() throws Exception { |
| 56 | pipeconf = new MockPipeconf(); |
| 57 | } |
| 58 | |
| 59 | @Test |
| 60 | public void testTranslate() throws Exception { |
| 61 | |
| 62 | DeviceId deviceId = DeviceId.NONE; |
| 63 | ApplicationId appId = new DefaultApplicationId(1, "test"); |
| 64 | int tableId = 0; |
| 65 | MacAddress ethDstMac = MacAddress.valueOf(random.nextLong()); |
| 66 | MacAddress ethSrcMac = MacAddress.valueOf(random.nextLong()); |
| 67 | short ethType = (short) (0x0000FFFF & random.nextInt()); |
| 68 | short outPort = (short) random.nextInt(65); |
| 69 | short inPort = (short) random.nextInt(65); |
| 70 | int timeout = random.nextInt(100); |
| 71 | int priority = random.nextInt(100); |
| 72 | |
| 73 | TrafficSelector matchInPort1 = DefaultTrafficSelector |
| 74 | .builder() |
| 75 | .matchInPort(PortNumber.portNumber(inPort)) |
| 76 | .matchEthDst(ethDstMac) |
| 77 | .matchEthSrc(ethSrcMac) |
| 78 | .matchEthType(ethType) |
| 79 | .build(); |
| 80 | |
| 81 | TrafficTreatment outPort2 = DefaultTrafficTreatment |
| 82 | .builder() |
| 83 | .setOutput(PortNumber.portNumber(outPort)) |
| 84 | .build(); |
| 85 | |
| 86 | FlowRule rule1 = DefaultFlowRule.builder() |
| 87 | .forDevice(deviceId) |
| 88 | .forTable(tableId) |
| 89 | .fromApp(appId) |
| 90 | .withSelector(matchInPort1) |
| 91 | .withTreatment(outPort2) |
| 92 | .makeTemporary(timeout) |
| 93 | .withPriority(priority) |
| 94 | .build(); |
| 95 | |
| 96 | FlowRule rule2 = DefaultFlowRule.builder() |
| 97 | .forDevice(deviceId) |
| 98 | .forTable(tableId) |
| 99 | .fromApp(appId) |
| 100 | .withSelector(matchInPort1) |
| 101 | .withTreatment(outPort2) |
| 102 | .makeTemporary(timeout) |
| 103 | .withPriority(priority) |
| 104 | .build(); |
| 105 | |
| 106 | PiTableEntry entry1 = PiFlowRuleTranslator.translateFlowRule(rule1, pipeconf, null); |
| 107 | PiTableEntry entry2 = PiFlowRuleTranslator.translateFlowRule(rule1, pipeconf, null); |
| 108 | |
| 109 | // check equality, i.e. same rules must produce same entries |
| 110 | new EqualsTester() |
| 111 | .addEqualityGroup(rule1, rule2) |
| 112 | .addEqualityGroup(entry1, entry2) |
| 113 | .testEquals(); |
| 114 | |
| 115 | int numMatchParams = pipeconf.pipelineModel().table(TABLE0).get().matchFields().size(); |
| 116 | // parse values stored in entry1 |
| 117 | PiTernaryFieldMatch inPortParam = (PiTernaryFieldMatch) entry1.fieldMatch(IN_PORT_ID).get(); |
| 118 | PiTernaryFieldMatch ethDstParam = (PiTernaryFieldMatch) entry1.fieldMatch(ETH_DST_ID).get(); |
| 119 | PiTernaryFieldMatch ethSrcParam = (PiTernaryFieldMatch) entry1.fieldMatch(ETH_SRC_ID).get(); |
| 120 | PiTernaryFieldMatch ethTypeParam = (PiTernaryFieldMatch) entry1.fieldMatch(ETH_TYPE_ID).get(); |
| 121 | Optional<Double> expectedTimeout = pipeconf.pipelineModel().table(TABLE0).get().supportsAging() |
| 122 | ? Optional.of((double) rule1.timeout()) : Optional.empty(); |
| 123 | |
| 124 | // check that the number of parameters in the entry is the same as the number of table keys |
| 125 | assertThat("Incorrect number of match parameters", |
| 126 | entry1.fieldMatches().size(), is(equalTo(numMatchParams))); |
| 127 | |
| 128 | // check that values stored in entry are the same used for the flow rule |
| 129 | assertThat("Incorrect inPort match param value", |
| 130 | inPortParam.value().asReadOnlyBuffer().getShort(), is(equalTo(inPort))); |
| 131 | assertThat("Incorrect ethDestMac match param value", |
| 132 | ethDstParam.value().asArray(), is(equalTo(ethDstMac.toBytes()))); |
| 133 | assertThat("Incorrect ethSrcMac match param value", |
| 134 | ethSrcParam.value().asArray(), is(equalTo(ethSrcMac.toBytes()))); |
| 135 | assertThat("Incorrect ethType match param value", |
| 136 | ethTypeParam.value().asReadOnlyBuffer().getShort(), is(equalTo(ethType))); |
| 137 | assertThat("Incorrect priority value", |
| 138 | entry1.priority().get(), is(equalTo(rule1.priority()))); |
| 139 | assertThat("Incorrect timeout value", |
| 140 | entry1.timeout(), is(equalTo(expectedTimeout))); |
| 141 | |
| 142 | } |
| 143 | } |