blob: 59931420510ace88db79a190b1821a3997f5d9df [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;
Ekber Aziz123ad5d2017-11-27 12:36:35 -080024import org.onosproject.net.pi.model.PiControlMetadataId;
Carmelo Cascone8a571af2018-04-06 23:17:04 -070025import org.onosproject.net.pi.runtime.PiControlMetadata;
Ekber Aziz123ad5d2017-11-27 12:36:35 -080026import org.onosproject.net.pi.runtime.PiPacketOperation;
27
28import static org.onlab.util.ImmutableByteSequence.copyFrom;
Ekber Aziz123ad5d2017-11-27 12:36:35 -080029import static org.onosproject.net.pi.model.PiPacketOperationType.PACKET_IN;
Carmelo Cascone8a571af2018-04-06 23:17:04 -070030import static org.onosproject.net.pi.model.PiPacketOperationType.PACKET_OUT;
Ekber Aziz123ad5d2017-11-27 12:36:35 -080031
32/**
33 * Test for DefaultPacketIn class.
34 */
35public class DefaultPacketInTest {
36
37 private static final int DEFAULT_ORIGINAL_VALUE = 255;
38 private static final int DEFAULT_BIT_WIDTH = 9;
39
40 private final DeviceId deviceId = DeviceId.deviceId("dummy:1");
41 private final DeviceId sameDeviceId = DeviceId.deviceId("dummy:1");
42 private final DeviceId deviceId2 = DeviceId.deviceId("dummy:2");
43 private final DeviceId nullDeviceId = null;
44
45 private PiPacketOperation packetOperation;
46 private PiPacketOperation packetOperation2;
47 private PiPacketOperation nullPacketOperation = null;
48
49 private DefaultPacketIn packetIn;
50 private DefaultPacketIn sameAsPacketIn;
51 private DefaultPacketIn packetIn2;
52 private DefaultPacketIn packetIn3;
53
54 /**
55 * Setup method for packetOperation and packetOperation2.
56 * @throws ImmutableByteSequence.ByteSequenceTrimException if byte sequence cannot be trimmed
57 */
58 @Before
59 public void setup() throws ImmutableByteSequence.ByteSequenceTrimException {
60
61 packetOperation = PiPacketOperation.builder()
62 .forDevice(deviceId)
63 .withData(ImmutableByteSequence.ofOnes(512))
64 .withType(PACKET_OUT)
65 .withMetadata(PiControlMetadata.builder()
66 .withId(PiControlMetadataId.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()
72 .forDevice(deviceId2)
73 .withData(ImmutableByteSequence.ofOnes(512))
74 .withType(PACKET_IN)
75 .withMetadata(PiControlMetadata.builder()
76 .withId(PiControlMetadataId.of("ingress_port"))
Carmelo Cascone8a571af2018-04-06 23:17:04 -070077 .withValue(copyFrom(DEFAULT_ORIGINAL_VALUE).fit(DEFAULT_BIT_WIDTH))
Ekber Aziz123ad5d2017-11-27 12:36:35 -080078 .build())
79 .build();
80
81 packetIn = new DefaultPacketIn(deviceId, packetOperation);
82 sameAsPacketIn = new DefaultPacketIn(sameDeviceId, packetOperation);
83 packetIn2 = new DefaultPacketIn(deviceId2, packetOperation);
84 packetIn3 = new DefaultPacketIn(deviceId, packetOperation2);
85 }
86
87 /**
88 * tearDown method for packetOperation and packetOperation2.
89 */
90 @After
91 public void tearDown() {
92 packetOperation = null;
93 packetOperation2 = null;
94
95 packetIn = null;
96 sameAsPacketIn = null;
97 packetIn2 = null;
98 packetIn3 = null;
99 }
100
101
102 /**
103 * Tests constructor with null object as a DeviceId parameter.
104 */
105 @Test(expected = NullPointerException.class)
106 public void testConstructorWithNullDeviceId() {
107
108 new DefaultPacketIn(nullDeviceId, packetOperation);
109 }
110
111 /**
112 * Tests constructor with null object as PacketOperation parameter.
113 */
114 @Test(expected = NullPointerException.class)
115 public void testConstructorWithNullPacketOperation() {
116
117 new DefaultPacketIn(deviceId, nullPacketOperation);
118 }
119
120 /**
121 * Test for deviceId method.
122 */
123 @Test
124 public void deviceId() {
125 new EqualsTester()
126 .addEqualityGroup(deviceId, packetIn.deviceId(), sameAsPacketIn.deviceId())
127 .addEqualityGroup(packetIn2)
128 .testEquals();
129 }
130
131 /**
132 * Test for packetOperation method.
133 */
134 @Test
135 public void packetOperation() {
136 new EqualsTester()
137 .addEqualityGroup(packetOperation, packetIn.packetOperation())
138 .addEqualityGroup(packetIn3.packetOperation())
139 .testEquals();
140 }
141
142 /**
143 * Checks the operation of equals(), hashCode() and toString() methods.
144 */
145 @Test
146 public void testEquals() {
147 new EqualsTester()
148 .addEqualityGroup(packetIn, sameAsPacketIn)
149 .addEqualityGroup(packetIn2)
150 .addEqualityGroup(packetIn3)
151 .testEquals();
152 }
Carmelo Cascone8a571af2018-04-06 23:17:04 -0700153}