blob: a8c1a7b81b52b3b3ad00175a727ae13c6fc6c683 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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;
Charles Chancb13d122016-02-10 13:03:40 -080023import java.util.Optional;
tom613d8142014-09-11 15:09:37 -070024
25import static com.google.common.base.MoreObjects.toStringHelper;
26
27/**
28 * Default implementation of an immutable inbound packet.
29 */
Ray Milkeyafa00d22014-11-13 19:25:15 -080030public final class DefaultInboundPacket implements InboundPacket {
tom613d8142014-09-11 15:09:37 -070031
32 private final ConnectPoint receivedFrom;
33 private final Ethernet parsed;
34 private final ByteBuffer unparsed;
Charles Chancb13d122016-02-10 13:03:40 -080035 private final Optional<Long> cookie;
tom613d8142014-09-11 15:09:37 -070036
37 /**
38 * Creates an immutable inbound packet.
39 *
40 * @param receivedFrom connection point where received
41 * @param parsed parsed ethernet frame
42 * @param unparsed unparsed raw bytes
43 */
Charles Chancb13d122016-02-10 13:03:40 -080044 public DefaultInboundPacket(ConnectPoint receivedFrom, Ethernet parsed,
tom613d8142014-09-11 15:09:37 -070045 ByteBuffer unparsed) {
Charles Chancb13d122016-02-10 13:03:40 -080046 this(receivedFrom, parsed, unparsed, Optional.empty());
47 }
48
49 /**
50 * Creates an immutable inbound packet with cookie.
51 *
52 * @param receivedFrom connection point where received
53 * @param parsed parsed ethernet frame
54 * @param unparsed unparsed raw bytes
55 * @param cookie cookie
56 */
57 public DefaultInboundPacket(ConnectPoint receivedFrom, Ethernet parsed,
58 ByteBuffer unparsed, Optional<Long> cookie) {
tom613d8142014-09-11 15:09:37 -070059 this.receivedFrom = receivedFrom;
60 this.parsed = parsed;
61 this.unparsed = unparsed;
Charles Chancb13d122016-02-10 13:03:40 -080062 this.cookie = cookie;
tom613d8142014-09-11 15:09:37 -070063 }
64
65 @Override
66 public ConnectPoint receivedFrom() {
67 return receivedFrom;
68 }
69
70 @Override
71 public Ethernet parsed() {
72 return parsed;
73 }
74
75 @Override
76 public ByteBuffer unparsed() {
77 // FIXME: figure out immutability here
78 return unparsed;
79 }
80
81 @Override
Charles Chancb13d122016-02-10 13:03:40 -080082 public Optional<Long> cookie() {
83 return cookie;
84 }
85
86 @Override
tom613d8142014-09-11 15:09:37 -070087 public int hashCode() {
88 return Objects.hash(receivedFrom, parsed, unparsed);
89 }
90
91 @Override
92 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -070093 if (this == obj) {
94 return true;
95 }
tom613d8142014-09-11 15:09:37 -070096 if (obj instanceof InboundPacket) {
97 final DefaultInboundPacket other = (DefaultInboundPacket) obj;
98 return Objects.equals(this.receivedFrom, other.receivedFrom) &&
99 Objects.equals(this.parsed, other.parsed) &&
100 Objects.equals(this.unparsed, other.unparsed);
101 }
102 return false;
103 }
104
105 @Override
106 public String toString() {
107 return toStringHelper(this)
108 .add("receivedFrom", receivedFrom)
109 .add("parsed", parsed)
110 .toString();
111 }
112}