Ekber Aziz | 123ad5d | 2017-11-27 12:36:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | package org.onosproject.p4runtime.ctl; |
| 17 | |
| 18 | import com.google.common.testing.EqualsTester; |
| 19 | import org.junit.After; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.onlab.util.ImmutableByteSequence; |
| 23 | import org.onosproject.net.DeviceId; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame^] | 24 | import org.onosproject.net.pi.model.PiPacketMetadataId; |
| 25 | import org.onosproject.net.pi.runtime.PiPacketMetadata; |
Ekber Aziz | 123ad5d | 2017-11-27 12:36:35 -0800 | [diff] [blame] | 26 | import org.onosproject.net.pi.runtime.PiPacketOperation; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame^] | 27 | import org.onosproject.p4runtime.ctl.controller.PacketInEvent; |
Ekber Aziz | 123ad5d | 2017-11-27 12:36:35 -0800 | [diff] [blame] | 28 | |
| 29 | import static org.onlab.util.ImmutableByteSequence.copyFrom; |
Ekber Aziz | 123ad5d | 2017-11-27 12:36:35 -0800 | [diff] [blame] | 30 | import static org.onosproject.net.pi.model.PiPacketOperationType.PACKET_IN; |
Carmelo Cascone | 8a571af | 2018-04-06 23:17:04 -0700 | [diff] [blame] | 31 | import static org.onosproject.net.pi.model.PiPacketOperationType.PACKET_OUT; |
Ekber Aziz | 123ad5d | 2017-11-27 12:36:35 -0800 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Test for DefaultPacketIn class. |
| 35 | */ |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 36 | public class PacketInEventTest { |
Ekber Aziz | 123ad5d | 2017-11-27 12:36:35 -0800 | [diff] [blame] | 37 | |
| 38 | private static final int DEFAULT_ORIGINAL_VALUE = 255; |
| 39 | private static final int DEFAULT_BIT_WIDTH = 9; |
| 40 | |
| 41 | private final DeviceId deviceId = DeviceId.deviceId("dummy:1"); |
| 42 | private final DeviceId sameDeviceId = DeviceId.deviceId("dummy:1"); |
| 43 | private final DeviceId deviceId2 = DeviceId.deviceId("dummy:2"); |
| 44 | private final DeviceId nullDeviceId = null; |
| 45 | |
| 46 | private PiPacketOperation packetOperation; |
| 47 | private PiPacketOperation packetOperation2; |
| 48 | private PiPacketOperation nullPacketOperation = null; |
| 49 | |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame^] | 50 | private org.onosproject.p4runtime.ctl.controller.PacketInEvent packetIn; |
| 51 | private org.onosproject.p4runtime.ctl.controller.PacketInEvent sameAsPacketIn; |
| 52 | private org.onosproject.p4runtime.ctl.controller.PacketInEvent packetIn2; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 53 | private PacketInEvent packetIn3; |
Ekber Aziz | 123ad5d | 2017-11-27 12:36:35 -0800 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * Setup method for packetOperation and packetOperation2. |
| 57 | * @throws ImmutableByteSequence.ByteSequenceTrimException if byte sequence cannot be trimmed |
| 58 | */ |
| 59 | @Before |
| 60 | public void setup() throws ImmutableByteSequence.ByteSequenceTrimException { |
| 61 | |
| 62 | packetOperation = PiPacketOperation.builder() |
Ekber Aziz | 123ad5d | 2017-11-27 12:36:35 -0800 | [diff] [blame] | 63 | .withData(ImmutableByteSequence.ofOnes(512)) |
| 64 | .withType(PACKET_OUT) |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame^] | 65 | .withMetadata(PiPacketMetadata.builder() |
| 66 | .withId(PiPacketMetadataId.of("egress_port")) |
Carmelo Cascone | 8a571af | 2018-04-06 23:17:04 -0700 | [diff] [blame] | 67 | .withValue(copyFrom(DEFAULT_ORIGINAL_VALUE).fit(DEFAULT_BIT_WIDTH)) |
Ekber Aziz | 123ad5d | 2017-11-27 12:36:35 -0800 | [diff] [blame] | 68 | .build()) |
| 69 | .build(); |
| 70 | |
| 71 | packetOperation2 = PiPacketOperation.builder() |
Ekber Aziz | 123ad5d | 2017-11-27 12:36:35 -0800 | [diff] [blame] | 72 | .withData(ImmutableByteSequence.ofOnes(512)) |
| 73 | .withType(PACKET_IN) |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame^] | 74 | .withMetadata(PiPacketMetadata.builder() |
| 75 | .withId(PiPacketMetadataId.of("ingress_port")) |
Carmelo Cascone | 8a571af | 2018-04-06 23:17:04 -0700 | [diff] [blame] | 76 | .withValue(copyFrom(DEFAULT_ORIGINAL_VALUE).fit(DEFAULT_BIT_WIDTH)) |
Ekber Aziz | 123ad5d | 2017-11-27 12:36:35 -0800 | [diff] [blame] | 77 | .build()) |
| 78 | .build(); |
| 79 | |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame^] | 80 | packetIn = new org.onosproject.p4runtime.ctl.controller.PacketInEvent(deviceId, packetOperation); |
| 81 | sameAsPacketIn = new org.onosproject.p4runtime.ctl.controller.PacketInEvent(sameDeviceId, packetOperation); |
| 82 | packetIn2 = new org.onosproject.p4runtime.ctl.controller.PacketInEvent(deviceId2, packetOperation); |
| 83 | packetIn3 = new org.onosproject.p4runtime.ctl.controller.PacketInEvent(deviceId, packetOperation2); |
Ekber Aziz | 123ad5d | 2017-11-27 12:36:35 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /** |
| 87 | * tearDown method for packetOperation and packetOperation2. |
| 88 | */ |
| 89 | @After |
| 90 | public void tearDown() { |
| 91 | packetOperation = null; |
| 92 | packetOperation2 = null; |
| 93 | |
| 94 | packetIn = null; |
| 95 | sameAsPacketIn = null; |
| 96 | packetIn2 = null; |
| 97 | packetIn3 = null; |
| 98 | } |
| 99 | |
| 100 | |
| 101 | /** |
| 102 | * Tests constructor with null object as a DeviceId parameter. |
| 103 | */ |
| 104 | @Test(expected = NullPointerException.class) |
| 105 | public void testConstructorWithNullDeviceId() { |
| 106 | |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame^] | 107 | new org.onosproject.p4runtime.ctl.controller.PacketInEvent(nullDeviceId, packetOperation); |
Ekber Aziz | 123ad5d | 2017-11-27 12:36:35 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Tests constructor with null object as PacketOperation parameter. |
| 112 | */ |
| 113 | @Test(expected = NullPointerException.class) |
| 114 | public void testConstructorWithNullPacketOperation() { |
| 115 | |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame^] | 116 | new org.onosproject.p4runtime.ctl.controller.PacketInEvent(deviceId, nullPacketOperation); |
Ekber Aziz | 123ad5d | 2017-11-27 12:36:35 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Test for deviceId method. |
| 121 | */ |
| 122 | @Test |
| 123 | public void deviceId() { |
| 124 | new EqualsTester() |
| 125 | .addEqualityGroup(deviceId, packetIn.deviceId(), sameAsPacketIn.deviceId()) |
| 126 | .addEqualityGroup(packetIn2) |
| 127 | .testEquals(); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Test for packetOperation method. |
| 132 | */ |
| 133 | @Test |
| 134 | public void packetOperation() { |
| 135 | new EqualsTester() |
| 136 | .addEqualityGroup(packetOperation, packetIn.packetOperation()) |
| 137 | .addEqualityGroup(packetIn3.packetOperation()) |
| 138 | .testEquals(); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Checks the operation of equals(), hashCode() and toString() methods. |
| 143 | */ |
| 144 | @Test |
| 145 | public void testEquals() { |
| 146 | new EqualsTester() |
| 147 | .addEqualityGroup(packetIn, sameAsPacketIn) |
| 148 | .addEqualityGroup(packetIn2) |
| 149 | .addEqualityGroup(packetIn3) |
| 150 | .testEquals(); |
| 151 | } |
Carmelo Cascone | 8a571af | 2018-04-06 23:17:04 -0700 | [diff] [blame] | 152 | } |