Carmelo Cascone | 0ec92f1 | 2016-06-17 14:41:40 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-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.bmv2.api.runtime; |
| 18 | |
| 19 | import com.eclipsesource.json.Json; |
| 20 | import com.eclipsesource.json.JsonObject; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.packet.MacAddress; |
| 24 | import org.onosproject.bmv2.api.context.Bmv2Configuration; |
| 25 | import org.onosproject.bmv2.api.context.Bmv2DefaultConfiguration; |
| 26 | |
| 27 | import java.io.BufferedReader; |
| 28 | import java.io.InputStreamReader; |
| 29 | |
| 30 | import static org.hamcrest.MatcherAssert.assertThat; |
| 31 | import static org.hamcrest.Matchers.is; |
| 32 | |
| 33 | public class Bmv2ExtensionBuilderTest { |
| 34 | |
| 35 | private Bmv2Configuration config; |
| 36 | |
| 37 | @Before |
| 38 | public void setUp() throws Exception { |
| 39 | JsonObject json = Json.parse(new BufferedReader(new InputStreamReader( |
| 40 | this.getClass().getResourceAsStream("/simple.json")))).asObject(); |
| 41 | config = Bmv2DefaultConfiguration.parse(json); |
| 42 | } |
| 43 | |
| 44 | @Test |
| 45 | public void testExtensionSelector() throws Exception { |
| 46 | |
| 47 | Bmv2ExtensionSelector extSelectorExact = Bmv2ExtensionSelector.builder() |
| 48 | .forConfiguration(config) |
| 49 | .matchExact("standard_metadata", "ingress_port", (short) 255) |
| 50 | .matchExact("ethernet", "etherType", 512) |
| 51 | .matchExact("ethernet", "dstAddr", 1024L) |
| 52 | .matchExact("ethernet", "srcAddr", MacAddress.BROADCAST.toBytes()) |
| 53 | .build(); |
| 54 | |
| 55 | Bmv2ExtensionSelector extSelectorTernary = Bmv2ExtensionSelector.builder() |
| 56 | .forConfiguration(config) |
| 57 | .matchTernary("standard_metadata", "ingress_port", (short) 255, (short) 255) |
| 58 | .matchTernary("ethernet", "etherType", 512, 512) |
| 59 | .matchTernary("ethernet", "dstAddr", 1024L, 1024L) |
| 60 | .matchTernary("ethernet", "srcAddr", MacAddress.BROADCAST.toBytes(), MacAddress.NONE.toBytes()) |
| 61 | .build(); |
| 62 | |
| 63 | Bmv2ExtensionSelector extSelectorLpm = Bmv2ExtensionSelector.builder() |
| 64 | .forConfiguration(config) |
| 65 | .matchLpm("standard_metadata", "ingress_port", (short) 255, 1) |
| 66 | .matchLpm("ethernet", "etherType", 512, 2) |
| 67 | .matchLpm("ethernet", "dstAddr", 1024L, 3) |
| 68 | .matchLpm("ethernet", "srcAddr", MacAddress.BROADCAST.toBytes(), 4) |
| 69 | .build(); |
| 70 | |
| 71 | Bmv2ExtensionSelector extSelectorValid = Bmv2ExtensionSelector.builder() |
| 72 | .forConfiguration(config) |
| 73 | .matchValid("standard_metadata", "ingress_port", true) |
| 74 | .matchValid("ethernet", "etherType", true) |
| 75 | .matchValid("ethernet", "dstAddr", false) |
| 76 | .matchValid("ethernet", "srcAddr", false) |
| 77 | .build(); |
| 78 | |
| 79 | assertThat(extSelectorExact.parameterMap().size(), is(4)); |
| 80 | assertThat(extSelectorTernary.parameterMap().size(), is(4)); |
| 81 | assertThat(extSelectorLpm.parameterMap().size(), is(4)); |
| 82 | assertThat(extSelectorValid.parameterMap().size(), is(4)); |
| 83 | |
| 84 | // TODO add more tests, e.g. check for byte sequences content and size. |
| 85 | } |
| 86 | |
| 87 | @Test |
| 88 | public void testExtensionTreatment() throws Exception { |
| 89 | |
| 90 | Bmv2ExtensionTreatment treatment = Bmv2ExtensionTreatment.builder() |
| 91 | .forConfiguration(config) |
| 92 | .setActionName("set_egress_port") |
| 93 | .addParameter("port", 1) |
| 94 | .build(); |
| 95 | |
| 96 | assertThat(treatment.action().parameters().size(), is(1)); |
| 97 | |
| 98 | // TODO add more tests, e.g. check for byte sequences content and size. |
| 99 | } |
| 100 | } |