blob: b73f4b5a00272e6dd12c605e4ab09a6c01a5b456 [file] [log] [blame]
Ekber Aziz123ad5d2017-11-27 12:36:35 -08001/*
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 */
16package org.onosproject.p4runtime.ctl;
17
18import com.google.common.testing.EqualsTester;
19import org.junit.After;
20import org.junit.Before;
21import org.junit.Test;
22import org.onlab.util.ImmutableByteSequence;
23import org.onosproject.net.DeviceId;
Carmelo Cascone4c289b72019-01-22 15:30:45 -080024import org.onosproject.net.pi.model.PiPacketMetadataId;
25import org.onosproject.net.pi.runtime.PiPacketMetadata;
Ekber Aziz123ad5d2017-11-27 12:36:35 -080026import org.onosproject.net.pi.runtime.PiPacketOperation;
Carmelo Cascone4c289b72019-01-22 15:30:45 -080027import org.onosproject.p4runtime.ctl.controller.PacketInEvent;
Ekber Aziz123ad5d2017-11-27 12:36:35 -080028
29import static org.onlab.util.ImmutableByteSequence.copyFrom;
Ekber Aziz123ad5d2017-11-27 12:36:35 -080030import static org.onosproject.net.pi.model.PiPacketOperationType.PACKET_IN;
Carmelo Cascone8a571af2018-04-06 23:17:04 -070031import static org.onosproject.net.pi.model.PiPacketOperationType.PACKET_OUT;
Ekber Aziz123ad5d2017-11-27 12:36:35 -080032
33/**
34 * Test for DefaultPacketIn class.
35 */
Carmelo Casconee5b28722018-06-22 17:28:28 +020036public class PacketInEventTest {
Ekber Aziz123ad5d2017-11-27 12:36:35 -080037
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 Cascone4c289b72019-01-22 15:30:45 -080050 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 Casconee5b28722018-06-22 17:28:28 +020053 private PacketInEvent packetIn3;
Ekber Aziz123ad5d2017-11-27 12:36:35 -080054
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 Aziz123ad5d2017-11-27 12:36:35 -080063 .withData(ImmutableByteSequence.ofOnes(512))
64 .withType(PACKET_OUT)
Carmelo Cascone4c289b72019-01-22 15:30:45 -080065 .withMetadata(PiPacketMetadata.builder()
66 .withId(PiPacketMetadataId.of("egress_port"))
Carmelo Cascone8a571af2018-04-06 23:17:04 -070067 .withValue(copyFrom(DEFAULT_ORIGINAL_VALUE).fit(DEFAULT_BIT_WIDTH))
Ekber Aziz123ad5d2017-11-27 12:36:35 -080068 .build())
69 .build();
70
71 packetOperation2 = PiPacketOperation.builder()
Ekber Aziz123ad5d2017-11-27 12:36:35 -080072 .withData(ImmutableByteSequence.ofOnes(512))
73 .withType(PACKET_IN)
Carmelo Cascone4c289b72019-01-22 15:30:45 -080074 .withMetadata(PiPacketMetadata.builder()
75 .withId(PiPacketMetadataId.of("ingress_port"))
Carmelo Cascone8a571af2018-04-06 23:17:04 -070076 .withValue(copyFrom(DEFAULT_ORIGINAL_VALUE).fit(DEFAULT_BIT_WIDTH))
Ekber Aziz123ad5d2017-11-27 12:36:35 -080077 .build())
78 .build();
79
Carmelo Cascone4c289b72019-01-22 15:30:45 -080080 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 Aziz123ad5d2017-11-27 12:36:35 -080084 }
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 Cascone4c289b72019-01-22 15:30:45 -0800107 new org.onosproject.p4runtime.ctl.controller.PacketInEvent(nullDeviceId, packetOperation);
Ekber Aziz123ad5d2017-11-27 12:36:35 -0800108 }
109
110 /**
111 * Tests constructor with null object as PacketOperation parameter.
112 */
113 @Test(expected = NullPointerException.class)
114 public void testConstructorWithNullPacketOperation() {
115
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800116 new org.onosproject.p4runtime.ctl.controller.PacketInEvent(deviceId, nullPacketOperation);
Ekber Aziz123ad5d2017-11-27 12:36:35 -0800117 }
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 Cascone8a571af2018-04-06 23:17:04 -0700152}