blob: e66d15ffdc5d6427b2251f4aeae15e7aa91fc0d2 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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
Thiago Santosa4a17cb2019-01-30 14:23:58 -080018import com.google.common.base.MoreObjects;
Brian O'Connorabafb502014-12-02 22:26:20 -080019import org.onosproject.net.DeviceId;
Thiago Santosa4a17cb2019-01-30 14:23:58 -080020import org.onosproject.net.PortNumber;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.net.flow.TrafficTreatment;
tom613d8142014-09-11 15:09:37 -070022
Thiago Santosa4a17cb2019-01-30 14:23:58 -080023import java.nio.ByteBuffer;
24import java.util.Objects;
tom613d8142014-09-11 15:09:37 -070025
26/**
27 * Default implementation of an immutable outbound packet.
28 */
Ray Milkeyafa00d22014-11-13 19:25:15 -080029public final class DefaultOutboundPacket implements OutboundPacket {
tom613d8142014-09-11 15:09:37 -070030 private final DeviceId sendThrough;
tom8bb16062014-09-12 14:47:46 -070031 private final TrafficTreatment treatment;
tom613d8142014-09-11 15:09:37 -070032 private final ByteBuffer data;
Thiago Santosa4a17cb2019-01-30 14:23:58 -080033 private final PortNumber inPort;
tom613d8142014-09-11 15:09:37 -070034
35 /**
36 * Creates an immutable outbound packet.
37 *
38 * @param sendThrough identifier through which to send the packet
tom8bb16062014-09-12 14:47:46 -070039 * @param treatment list of packet treatments
tom613d8142014-09-11 15:09:37 -070040 * @param data raw packet data
41 */
42 public DefaultOutboundPacket(DeviceId sendThrough,
alshabib1d4cace2014-09-13 19:16:26 -070043 TrafficTreatment treatment, ByteBuffer data) {
tom613d8142014-09-11 15:09:37 -070044 this.sendThrough = sendThrough;
tom8bb16062014-09-12 14:47:46 -070045 this.treatment = treatment;
tom613d8142014-09-11 15:09:37 -070046 this.data = data;
Thiago Santosa4a17cb2019-01-30 14:23:58 -080047 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;
tom613d8142014-09-11 15:09:37 -070065 }
66
67 @Override
68 public DeviceId sendThrough() {
69 return sendThrough;
70 }
71
72 @Override
tom8bb16062014-09-12 14:47:46 -070073 public TrafficTreatment treatment() {
74 return treatment;
tom613d8142014-09-11 15:09:37 -070075 }
76
77 @Override
78 public ByteBuffer data() {
79 // FIXME: figure out immutability here
80 return data;
81 }
82
83 @Override
Thiago Santosa4a17cb2019-01-30 14:23:58 -080084 public PortNumber inPort() {
85 return inPort;
86 }
87
88 @Override
Ray Milkeyafa00d22014-11-13 19:25:15 -080089 public int hashCode() {
Thiago Santosa4a17cb2019-01-30 14:23:58 -080090 return Objects.hash(sendThrough, treatment, data, inPort);
Ray Milkeyafa00d22014-11-13 19:25:15 -080091 }
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 Santosa4a17cb2019-01-30 14:23:58 -0800102 Objects.equals(this.data, other.data) &&
103 Objects.equals(this.inPort, other.inPort);
Ray Milkeyafa00d22014-11-13 19:25:15 -0800104 }
105 return false;
106 }
107 @Override
tom613d8142014-09-11 15:09:37 -0700108 public String toString() {
109 return MoreObjects.toStringHelper(this)
110 .add("sendThrough", sendThrough)
tom8bb16062014-09-12 14:47:46 -0700111 .add("treatment", treatment)
Thiago Santosa4a17cb2019-01-30 14:23:58 -0800112 .add("inPort", inPort)
tom613d8142014-09-11 15:09:37 -0700113 .toString();
114 }
115}