Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 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 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 18 | import org.onosproject.net.ConnectPoint; |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 19 | import org.onlab.packet.Ethernet; |
| 20 | |
| 21 | import java.nio.ByteBuffer; |
| 22 | import java.util.Objects; |
| 23 | |
| 24 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 25 | |
| 26 | /** |
| 27 | * Default implementation of an immutable inbound packet. |
| 28 | */ |
Ray Milkey | afa00d2 | 2014-11-13 19:25:15 -0800 | [diff] [blame] | 29 | public final class DefaultInboundPacket implements InboundPacket { |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 30 | |
| 31 | private final ConnectPoint receivedFrom; |
| 32 | private final Ethernet parsed; |
| 33 | private final ByteBuffer unparsed; |
| 34 | |
| 35 | /** |
| 36 | * Creates an immutable inbound packet. |
| 37 | * |
| 38 | * @param receivedFrom connection point where received |
| 39 | * @param parsed parsed ethernet frame |
| 40 | * @param unparsed unparsed raw bytes |
| 41 | */ |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 42 | public DefaultInboundPacket(ConnectPoint receivedFrom, Ethernet parsed, |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 43 | ByteBuffer unparsed) { |
| 44 | this.receivedFrom = receivedFrom; |
| 45 | this.parsed = parsed; |
| 46 | this.unparsed = unparsed; |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public ConnectPoint receivedFrom() { |
| 51 | return receivedFrom; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public Ethernet parsed() { |
| 56 | return parsed; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public ByteBuffer unparsed() { |
| 61 | // FIXME: figure out immutability here |
| 62 | return unparsed; |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public int hashCode() { |
| 67 | return Objects.hash(receivedFrom, parsed, unparsed); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public boolean equals(Object obj) { |
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame] | 72 | if (this == obj) { |
| 73 | return true; |
| 74 | } |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 75 | if (obj instanceof InboundPacket) { |
| 76 | final DefaultInboundPacket other = (DefaultInboundPacket) obj; |
| 77 | return Objects.equals(this.receivedFrom, other.receivedFrom) && |
| 78 | Objects.equals(this.parsed, other.parsed) && |
| 79 | Objects.equals(this.unparsed, other.unparsed); |
| 80 | } |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public String toString() { |
| 86 | return toStringHelper(this) |
| 87 | .add("receivedFrom", receivedFrom) |
| 88 | .add("parsed", parsed) |
| 89 | .toString(); |
| 90 | } |
| 91 | } |