blob: 2f19d49d6969edba5549be38611d6bc2f9172892 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
Ray Milkey269ffb92014-04-03 14:43:30 -07002 * Copyright 2012, Big Switch Networks, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 * not use this file except in compliance with the License. You may obtain
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
14 * under the License.
15 **/
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080016
17/**
Ray Milkey269ffb92014-04-03 14:43:30 -070018 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080019 */
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070020package net.onrc.onos.core.packet;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080021
22import java.nio.ByteBuffer;
23import java.util.Arrays;
Jonathan Harta99ec672014-04-03 11:30:34 -070024
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080025import org.openflow.util.HexString;
26
27/**
28 * @author Shudong Zhou (shudong.zhou@bigswitch.com)
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080029 */
Ray Milkey269ffb92014-04-03 14:43:30 -070030public class BSNPROBE extends BasePacket {
31 protected long controllerId;
32 protected int sequenceId;
33 protected byte[] srcMac;
34 protected byte[] dstMac;
35 protected long srcSwDpid;
36 protected int srcPortNo;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080037
38 public BSNPROBE() {
39 srcMac = new byte[6];
40 dstMac = new byte[6];
41 }
42
43
Ray Milkey269ffb92014-04-03 14:43:30 -070044 public long getControllerId() {
45 return this.controllerId;
46 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080047
Ray Milkey269ffb92014-04-03 14:43:30 -070048 public BSNPROBE setControllerId(long controllerId) {
49 this.controllerId = controllerId;
50 return this;
51 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080052
Ray Milkey269ffb92014-04-03 14:43:30 -070053 public int getSequenceId() {
54 return sequenceId;
55 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080056
Ray Milkey269ffb92014-04-03 14:43:30 -070057 public BSNPROBE setSequenceId(int sequenceId) {
58 this.sequenceId = sequenceId;
59 return this;
60 }
61
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080062 public byte[] getSrcMac() {
63 return this.srcMac;
64 }
65
66 public BSNPROBE setSrcMac(byte[] srcMac) {
67 this.srcMac = srcMac;
68 return this;
69 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080070
Ray Milkey269ffb92014-04-03 14:43:30 -070071 public byte[] getDstMac() {
72 return dstMac;
73 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080074
Ray Milkey269ffb92014-04-03 14:43:30 -070075 public BSNPROBE setDstMac(byte[] dstMac) {
76 this.dstMac = dstMac;
77 return this;
78 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080079
Ray Milkey269ffb92014-04-03 14:43:30 -070080 public long getSrcSwDpid() {
81 return srcSwDpid;
82 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080083
Ray Milkey269ffb92014-04-03 14:43:30 -070084 public BSNPROBE setSrcSwDpid(long srcSwDpid) {
85 this.srcSwDpid = srcSwDpid;
86 return this;
87 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080088
Ray Milkey269ffb92014-04-03 14:43:30 -070089 public int getSrcPortNo() {
90 return srcPortNo;
91 }
92
93 public BSNPROBE setSrcPortNo(int srcPortNo) {
94 this.srcPortNo = srcPortNo;
95 return this;
96 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080097
98 @Override
99 public byte[] serialize() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700100 short length = 8 /* controllerId */ + 4 /* seqId */
101 + 12 /* srcMac dstMac */ + 8 /* srcSwDpid */ + 4 /* srcPortNo */;
102
103 byte[] payloadData = null;
104 if (this.payload != null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800105 payload.setParent(this);
106 payloadData = payload.serialize();
107 length += payloadData.length;
108 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700109
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800110 byte[] data = new byte[length];
111 ByteBuffer bb = ByteBuffer.wrap(data);
112 bb.putLong(this.controllerId);
113 bb.putInt(this.sequenceId);
114 bb.put(this.srcMac);
115 bb.put(this.dstMac);
116 bb.putLong(this.srcSwDpid);
117 bb.putInt(this.srcPortNo);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700118 if (payloadData != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700119 bb.put(payloadData);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700120 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800121
Ray Milkeyb29e6262014-04-09 16:02:14 -0700122 if (this.parent != null && this.parent instanceof BSN) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700123 ((BSN) this.parent).setType(BSN.BSN_TYPE_PROBE);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700124 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800125
126 return data;
127 }
128
129 @Override
130 public IPacket deserialize(byte[] data, int offset, int length) {
131 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
Ray Milkey269ffb92014-04-03 14:43:30 -0700132
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800133 controllerId = bb.getLong();
134 sequenceId = bb.getInt();
135 bb.get(this.srcMac, 0, 6);
136 bb.get(this.dstMac, 0, 6);
137 this.srcSwDpid = bb.getLong();
138 this.srcPortNo = bb.getInt();
Ray Milkey269ffb92014-04-03 14:43:30 -0700139
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800140 if (bb.hasRemaining()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700141 this.payload = new Data();
142 this.payload = payload.deserialize(data, bb.position(), bb.limit() - bb.position());
143 this.payload.setParent(this);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800144 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700145
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800146 return this;
147 }
148
149 /* (non-Javadoc)
150 * @see java.lang.Object#hashCode()
151 */
152 @Override
153 public int hashCode() {
154 final int prime = 883;
155 int result = super.hashCode();
Pavlin Radoslavov45233db2014-04-09 17:33:01 -0700156 result = prime * result + Arrays.hashCode(srcMac);
157 result = prime * result + Arrays.hashCode(dstMac);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800158 result = prime * result + (int) (srcSwDpid >> 32) + (int) srcSwDpid;
159 result = prime * result + srcPortNo;
160 return result;
161 }
162
163 /* (non-Javadoc)
164 * @see java.lang.Object#equals(java.lang.Object)
165 */
166 @Override
167 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700168 if (this == obj) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800169 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700170 }
171 if (!super.equals(obj)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800172 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700173 }
174 if (!(obj instanceof BSNPROBE)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800175 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700176 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800177 BSNPROBE other = (BSNPROBE) obj;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700178 if (!Arrays.equals(srcMac, other.srcMac)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800179 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700180 }
181 if (!Arrays.equals(dstMac, other.dstMac)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700182 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700183 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800184 return (sequenceId == other.sequenceId &&
Ray Milkey269ffb92014-04-03 14:43:30 -0700185 srcSwDpid == other.srcSwDpid &&
186 srcPortNo == other.srcPortNo
187 );
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800188 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700189
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800190 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700191 StringBuffer sb = new StringBuffer("\n");
192 sb.append("BSN Probe packet");
193 sb.append("\nSource Mac: ");
194 sb.append(HexString.toHexString(srcMac));
195 sb.append("\nDestination Mac: ");
196 sb.append(HexString.toHexString(dstMac));
197 sb.append("\nSource Switch: ");
198 sb.append(HexString.toHexString(srcSwDpid));
199 sb.append(" port: " + srcPortNo);
200 sb.append("\nSequence No.:" + sequenceId);
201
202 return sb.toString();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800203 }
204}