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 | /** |
| 84 | * Tests creation of a DefaultFlowRule using a FlowRule constructor. |
| 85 | */ |
| 86 | @Test |
| 87 | public void testBuilder() { |
| 88 | |
| 89 | PiTableId piTableId = PiTableId.of("table10"); |
| 90 | long cookie = 0xfff0323; |
| 91 | int priority = 100; |
| 92 | double timeout = 1000; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 93 | PiMatchFieldId piMatchFieldId = PiMatchFieldId.of(IPV4_HEADER_NAME + DOT + DST_ADDR); |
| 94 | PiFieldMatch piFieldMatch = new PiExactFieldMatch(piMatchFieldId, ImmutableByteSequence.copyFrom(0x0a010101)); |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 95 | PiAction piAction = PiAction.builder().withId(PiActionId.of(DROP)).build(); |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 96 | final Map<PiMatchFieldId, PiFieldMatch> fieldMatches = Maps.newHashMap(); |
| 97 | fieldMatches.put(piMatchFieldId, piFieldMatch); |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 98 | final PiTableEntry piTableEntry = PiTableEntry.builder() |
| 99 | .forTable(piTableId) |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 100 | .withMatchKey(PiMatchKey.builder() |
| 101 | .addFieldMatches(fieldMatches.values()) |
| 102 | .build()) |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 103 | .withAction(piAction) |
| 104 | .withCookie(cookie) |
| 105 | .withPriority(priority) |
| 106 | .withTimeout(timeout) |
| 107 | .build(); |
| 108 | |
| 109 | assertThat(piTableEntry.table(), is(piTableId)); |
| 110 | assertThat(piTableEntry.cookie(), is(cookie)); |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 111 | assertThat("Priority must be set", piTableEntry.priority().isPresent()); |
| 112 | assertThat("Timeout must be set", piTableEntry.timeout().isPresent()); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 113 | assertThat(piTableEntry.priority().getAsInt(), is(priority)); |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 114 | assertThat(piTableEntry.timeout().get(), is(timeout)); |
| 115 | assertThat("Incorrect match param value", |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 116 | CollectionUtils.isEqualCollection(piTableEntry.matchKey().fieldMatches(), fieldMatches.values())); |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 117 | assertThat(piTableEntry.action(), is(piAction)); |
| 118 | } |
| 119 | } |