blob: 03e166608371ac6ac484ac581aa3fceda7f2ceed [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() {
Pavlin Radoslavov0a9d5c32014-04-15 17:45:23 -070063 if (this.srcMac == null) {
64 return null;
65 }
66 return this.srcMac.clone();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080067 }
68
69 public BSNPROBE setSrcMac(byte[] srcMac) {
Pavlin Radoslavov0a9d5c32014-04-15 17:45:23 -070070 if (srcMac == null) {
71 this.srcMac = null;
72 } else {
73 this.srcMac = srcMac.clone();
74 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080075 return this;
76 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080077
Ray Milkey269ffb92014-04-03 14:43:30 -070078 public byte[] getDstMac() {
Pavlin Radoslavov0a9d5c32014-04-15 17:45:23 -070079 if (this.dstMac == null) {
80 return null;
81 }
82 return this.dstMac.clone();
Ray Milkey269ffb92014-04-03 14:43:30 -070083 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080084
Ray Milkey269ffb92014-04-03 14:43:30 -070085 public BSNPROBE setDstMac(byte[] dstMac) {
Pavlin Radoslavov0a9d5c32014-04-15 17:45:23 -070086 if (dstMac == null) {
87 this.dstMac = null;
88 } else {
89 this.dstMac = dstMac.clone();
90 }
Ray Milkey269ffb92014-04-03 14:43:30 -070091 return this;
92 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080093
Ray Milkey269ffb92014-04-03 14:43:30 -070094 public long getSrcSwDpid() {
95 return srcSwDpid;
96 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080097
Ray Milkey269ffb92014-04-03 14:43:30 -070098 public BSNPROBE setSrcSwDpid(long srcSwDpid) {
99 this.srcSwDpid = srcSwDpid;
100 return this;
101 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800102
Ray Milkey269ffb92014-04-03 14:43:30 -0700103 public int getSrcPortNo() {
104 return srcPortNo;
105 }
106
107 public BSNPROBE setSrcPortNo(int srcPortNo) {
108 this.srcPortNo = srcPortNo;
109 return this;
110 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800111
112 @Override
113 public byte[] serialize() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700114 short length = 8 /* controllerId */ + 4 /* seqId */
115 + 12 /* srcMac dstMac */ + 8 /* srcSwDpid */ + 4 /* srcPortNo */;
116
117 byte[] payloadData = null;
118 if (this.payload != null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800119 payload.setParent(this);
120 payloadData = payload.serialize();
121 length += payloadData.length;
122 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700123
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800124 byte[] data = new byte[length];
125 ByteBuffer bb = ByteBuffer.wrap(data);
126 bb.putLong(this.controllerId);
127 bb.putInt(this.sequenceId);
128 bb.put(this.srcMac);
129 bb.put(this.dstMac);
130 bb.putLong(this.srcSwDpid);
131 bb.putInt(this.srcPortNo);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700132 if (payloadData != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700133 bb.put(payloadData);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700134 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800135
Ray Milkeyb29e6262014-04-09 16:02:14 -0700136 if (this.parent != null && this.parent instanceof BSN) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700137 ((BSN) this.parent).setType(BSN.BSN_TYPE_PROBE);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700138 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800139
140 return data;
141 }
142
143 @Override
144 public IPacket deserialize(byte[] data, int offset, int length) {
145 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
Ray Milkey269ffb92014-04-03 14:43:30 -0700146
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800147 controllerId = bb.getLong();
148 sequenceId = bb.getInt();
149 bb.get(this.srcMac, 0, 6);
150 bb.get(this.dstMac, 0, 6);
151 this.srcSwDpid = bb.getLong();
152 this.srcPortNo = bb.getInt();
Ray Milkey269ffb92014-04-03 14:43:30 -0700153
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800154 if (bb.hasRemaining()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700155 this.payload = new Data();
156 this.payload = payload.deserialize(data, bb.position(), bb.limit() - bb.position());
157 this.payload.setParent(this);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800158 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700159
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800160 return this;
161 }
162
163 /* (non-Javadoc)
164 * @see java.lang.Object#hashCode()
165 */
166 @Override
167 public int hashCode() {
168 final int prime = 883;
169 int result = super.hashCode();
Pavlin Radoslavov45233db2014-04-09 17:33:01 -0700170 result = prime * result + Arrays.hashCode(srcMac);
171 result = prime * result + Arrays.hashCode(dstMac);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800172 result = prime * result + (int) (srcSwDpid >> 32) + (int) srcSwDpid;
173 result = prime * result + srcPortNo;
174 return result;
175 }
176
177 /* (non-Javadoc)
178 * @see java.lang.Object#equals(java.lang.Object)
179 */
180 @Override
181 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700182 if (this == obj) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800183 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700184 }
185 if (!super.equals(obj)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800186 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700187 }
188 if (!(obj instanceof BSNPROBE)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800189 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700190 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800191 BSNPROBE other = (BSNPROBE) obj;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700192 if (!Arrays.equals(srcMac, other.srcMac)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800193 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700194 }
195 if (!Arrays.equals(dstMac, other.dstMac)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700196 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700197 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800198 return (sequenceId == other.sequenceId &&
Ray Milkey269ffb92014-04-03 14:43:30 -0700199 srcSwDpid == other.srcSwDpid &&
200 srcPortNo == other.srcPortNo
201 );
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800202 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700203
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800204 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700205 StringBuffer sb = new StringBuffer("\n");
206 sb.append("BSN Probe packet");
207 sb.append("\nSource Mac: ");
208 sb.append(HexString.toHexString(srcMac));
209 sb.append("\nDestination Mac: ");
210 sb.append(HexString.toHexString(dstMac));
211 sb.append("\nSource Switch: ");
212 sb.append(HexString.toHexString(srcSwDpid));
213 sb.append(" port: " + srcPortNo);
214 sb.append("\nSequence No.:" + sequenceId);
215
216 return sb.toString();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800217 }
218}