blob: 96f872f5b3e21cc8ae8892bf61f881f61e677f9e [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.packet;
tom613d8142014-09-11 15:09:37 -070017
Brian O'Connorabafb502014-12-02 22:26:20 -080018import org.onosproject.net.ConnectPoint;
tom613d8142014-09-11 15:09:37 -070019import org.onlab.packet.Ethernet;
20
21import java.nio.ByteBuffer;
22import java.util.Objects;
23
24import static com.google.common.base.MoreObjects.toStringHelper;
25
26/**
27 * Default implementation of an immutable inbound packet.
28 */
Ray Milkeyafa00d22014-11-13 19:25:15 -080029public final class DefaultInboundPacket implements InboundPacket {
tom613d8142014-09-11 15:09:37 -070030
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 */
alshabib0ed6a202014-10-19 12:42:57 -070042 public DefaultInboundPacket(ConnectPoint receivedFrom, Ethernet parsed,
tom613d8142014-09-11 15:09:37 -070043 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) {
tomfc9a4ff2014-09-22 18:22:47 -070072 if (this == obj) {
73 return true;
74 }
tom613d8142014-09-11 15:09:37 -070075 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}