Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 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.runtime; |
| 18 | |
| 19 | import com.google.common.collect.Maps; |
| 20 | import com.google.common.testing.EqualsTester; |
| 21 | import org.apache.commons.collections.CollectionUtils; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.util.ImmutableByteSequence; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 24 | import org.onosproject.net.pi.model.PiActionId; |
| 25 | import org.onosproject.net.pi.model.PiMatchFieldId; |
| 26 | import org.onosproject.net.pi.model.PiTableId; |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 27 | |
| 28 | import java.util.Map; |
| 29 | |
| 30 | import static org.hamcrest.MatcherAssert.assertThat; |
| 31 | import static org.hamcrest.Matchers.is; |
| 32 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 33 | import static org.onosproject.net.pi.runtime.PiConstantsTest.DOT; |
| 34 | import static org.onosproject.net.pi.runtime.PiConstantsTest.DROP; |
| 35 | import static org.onosproject.net.pi.runtime.PiConstantsTest.DST_ADDR; |
| 36 | import static org.onosproject.net.pi.runtime.PiConstantsTest.IPV4_HEADER_NAME; |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Unit tests for PiTableEntry class. |
| 40 | */ |
| 41 | public class PiTableEntryTest { |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 42 | private final PiTableEntry piTableEntry1 = PiTableEntry.builder() |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 43 | .forTable(PiTableId.of("Table10")) |
| 44 | .withCookie(0xac) |
| 45 | .withPriority(10) |
| 46 | .withAction(PiAction.builder().withId(PiActionId.of(DROP)).build()) |
| 47 | .withTimeout(100) |
| 48 | .build(); |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 49 | private final PiTableEntry sameAsPiTableEntry1 = PiTableEntry.builder() |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 50 | .forTable(PiTableId.of("Table10")) |
| 51 | .withCookie(0xac) |
| 52 | .withPriority(10) |
| 53 | .withAction(PiAction.builder().withId(PiActionId.of(DROP)).build()) |
| 54 | .withTimeout(100) |
| 55 | .build(); |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 56 | private final PiTableEntry piTableEntry2 = PiTableEntry.builder() |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 57 | .forTable(PiTableId.of("Table20")) |
| 58 | .withCookie(0xac) |
| 59 | .withPriority(10) |
| 60 | .withAction(PiAction.builder().withId(PiActionId.of(DROP)).build()) |
| 61 | .withTimeout(1000) |
| 62 | .build(); |
| 63 | |
| 64 | /** |
| 65 | * Checks that the PiTableEntry class is immutable. |
| 66 | */ |
| 67 | @Test |
| 68 | public void testImmutability() { |
| 69 | assertThatClassIsImmutable(PiTableEntry.class); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Tests the equals, hashCode and toString methods using Guava EqualsTester. |
| 74 | */ |
| 75 | @Test |
| 76 | public void testEquals() { |
| 77 | new EqualsTester() |
| 78 | .addEqualityGroup(piTableEntry1, sameAsPiTableEntry1) |
| 79 | .addEqualityGroup(piTableEntry2) |
| 80 | .testEquals(); |
| 81 | } |
| 82 | |
| 83 | /** |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 84 | * Tests equality of the empty table entry. |
| 85 | */ |
| 86 | @Test |
| 87 | public void testEmptyEquals() { |
| 88 | new EqualsTester() |
| 89 | .addEqualityGroup(PiTableEntry.EMTPY, PiTableEntry.EMTPY) |
| 90 | .testEquals(); |
| 91 | } |
| 92 | |
| 93 | /** |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 94 | * Tests creation of a DefaultFlowRule using a FlowRule constructor. |
| 95 | */ |
| 96 | @Test |
| 97 | public void testBuilder() { |
| 98 | |
| 99 | PiTableId piTableId = PiTableId.of("table10"); |
| 100 | long cookie = 0xfff0323; |
| 101 | int priority = 100; |
| 102 | double timeout = 1000; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 103 | PiMatchFieldId piMatchFieldId = PiMatchFieldId.of(IPV4_HEADER_NAME + DOT + DST_ADDR); |
| 104 | PiFieldMatch piFieldMatch = new PiExactFieldMatch(piMatchFieldId, ImmutableByteSequence.copyFrom(0x0a010101)); |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 105 | PiAction piAction = PiAction.builder().withId(PiActionId.of(DROP)).build(); |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 106 | final Map<PiMatchFieldId, PiFieldMatch> fieldMatches = Maps.newHashMap(); |
| 107 | fieldMatches.put(piMatchFieldId, piFieldMatch); |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 108 | final PiTableEntry piTableEntry = PiTableEntry.builder() |
| 109 | .forTable(piTableId) |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 110 | .withMatchKey(PiMatchKey.builder() |
| 111 | .addFieldMatches(fieldMatches.values()) |
| 112 | .build()) |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 113 | .withAction(piAction) |
| 114 | .withCookie(cookie) |
| 115 | .withPriority(priority) |
| 116 | .withTimeout(timeout) |
| 117 | .build(); |
| 118 | |
| 119 | assertThat(piTableEntry.table(), is(piTableId)); |
| 120 | assertThat(piTableEntry.cookie(), is(cookie)); |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 121 | assertThat("Priority must be set", piTableEntry.priority().isPresent()); |
| 122 | assertThat("Timeout must be set", piTableEntry.timeout().isPresent()); |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 123 | assertThat(piTableEntry.priority().get(), is(priority)); |
| 124 | assertThat(piTableEntry.timeout().get(), is(timeout)); |
| 125 | assertThat("Incorrect match param value", |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 126 | CollectionUtils.isEqualCollection(piTableEntry.matchKey().fieldMatches(), fieldMatches.values())); |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 127 | assertThat(piTableEntry.action(), is(piAction)); |
| 128 | } |
| 129 | } |