Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.packet; |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 17 | |
Thiago Santos | a4a17cb | 2019-01-30 14:23:58 -0800 | [diff] [blame] | 18 | import com.google.common.base.MoreObjects; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 19 | import org.onosproject.net.DeviceId; |
Thiago Santos | a4a17cb | 2019-01-30 14:23:58 -0800 | [diff] [blame] | 20 | import org.onosproject.net.PortNumber; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 21 | import org.onosproject.net.flow.TrafficTreatment; |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 22 | |
Thiago Santos | a4a17cb | 2019-01-30 14:23:58 -0800 | [diff] [blame] | 23 | import java.nio.ByteBuffer; |
| 24 | import java.util.Objects; |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * Default implementation of an immutable outbound packet. |
| 28 | */ |
Ray Milkey | afa00d2 | 2014-11-13 19:25:15 -0800 | [diff] [blame] | 29 | public final class DefaultOutboundPacket implements OutboundPacket { |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 30 | private final DeviceId sendThrough; |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 31 | private final TrafficTreatment treatment; |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 32 | private final ByteBuffer data; |
Thiago Santos | a4a17cb | 2019-01-30 14:23:58 -0800 | [diff] [blame] | 33 | private final PortNumber inPort; |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Creates an immutable outbound packet. |
| 37 | * |
| 38 | * @param sendThrough identifier through which to send the packet |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 39 | * @param treatment list of packet treatments |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 40 | * @param data raw packet data |
| 41 | */ |
| 42 | public DefaultOutboundPacket(DeviceId sendThrough, |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 43 | TrafficTreatment treatment, ByteBuffer data) { |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 44 | this.sendThrough = sendThrough; |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 45 | this.treatment = treatment; |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 46 | this.data = data; |
Thiago Santos | a4a17cb | 2019-01-30 14:23:58 -0800 | [diff] [blame] | 47 | this.inPort = null; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Creates an immutable outbound packet. |
| 52 | * |
| 53 | * @param sendThrough identifier through which to send the packet |
| 54 | * @param treatment list of packet treatments |
| 55 | * @param data raw packet data |
| 56 | * @param inPort input port to be used for the packet |
| 57 | */ |
| 58 | public DefaultOutboundPacket(DeviceId sendThrough, |
| 59 | TrafficTreatment treatment, ByteBuffer data, |
| 60 | PortNumber inPort) { |
| 61 | this.sendThrough = sendThrough; |
| 62 | this.treatment = treatment; |
| 63 | this.data = data; |
| 64 | this.inPort = inPort; |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public DeviceId sendThrough() { |
| 69 | return sendThrough; |
| 70 | } |
| 71 | |
| 72 | @Override |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 73 | public TrafficTreatment treatment() { |
| 74 | return treatment; |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public ByteBuffer data() { |
| 79 | // FIXME: figure out immutability here |
| 80 | return data; |
| 81 | } |
| 82 | |
| 83 | @Override |
Thiago Santos | a4a17cb | 2019-01-30 14:23:58 -0800 | [diff] [blame] | 84 | public PortNumber inPort() { |
| 85 | return inPort; |
| 86 | } |
| 87 | |
| 88 | @Override |
Ray Milkey | afa00d2 | 2014-11-13 19:25:15 -0800 | [diff] [blame] | 89 | public int hashCode() { |
Thiago Santos | a4a17cb | 2019-01-30 14:23:58 -0800 | [diff] [blame] | 90 | return Objects.hash(sendThrough, treatment, data, inPort); |
Ray Milkey | afa00d2 | 2014-11-13 19:25:15 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public boolean equals(Object obj) { |
| 95 | if (this == obj) { |
| 96 | return true; |
| 97 | } |
| 98 | if (obj instanceof OutboundPacket) { |
| 99 | final DefaultOutboundPacket other = (DefaultOutboundPacket) obj; |
| 100 | return Objects.equals(this.sendThrough, other.sendThrough) && |
| 101 | Objects.equals(this.treatment, other.treatment) && |
Thiago Santos | a4a17cb | 2019-01-30 14:23:58 -0800 | [diff] [blame] | 102 | Objects.equals(this.data, other.data) && |
| 103 | Objects.equals(this.inPort, other.inPort); |
Ray Milkey | afa00d2 | 2014-11-13 19:25:15 -0800 | [diff] [blame] | 104 | } |
| 105 | return false; |
| 106 | } |
| 107 | @Override |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 108 | public String toString() { |
| 109 | return MoreObjects.toStringHelper(this) |
| 110 | .add("sendThrough", sendThrough) |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 111 | .add("treatment", treatment) |
Thiago Santos | a4a17cb | 2019-01-30 14:23:58 -0800 | [diff] [blame] | 112 | .add("inPort", inPort) |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 113 | .toString(); |
| 114 | } |
| 115 | } |