blob: 1daccdaf23f4124b1a97b60b7ceed4ebdef08dad [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
Pavlin Radoslavov1d595c02014-04-16 14:11:54 -070025import org.apache.commons.lang.ArrayUtils;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080026import org.openflow.util.HexString;
27
28/**
29 * @author Shudong Zhou (shudong.zhou@bigswitch.com)
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080030 */
Ray Milkey269ffb92014-04-03 14:43:30 -070031public class BSNPROBE extends BasePacket {
32 protected long controllerId;
33 protected int sequenceId;
34 protected byte[] srcMac;
35 protected byte[] dstMac;
36 protected long srcSwDpid;
37 protected int srcPortNo;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080038
39 public BSNPROBE() {
40 srcMac = new byte[6];
41 dstMac = new byte[6];
42 }
43
44
Ray Milkey269ffb92014-04-03 14:43:30 -070045 public long getControllerId() {
46 return this.controllerId;
47 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080048
Ray Milkey269ffb92014-04-03 14:43:30 -070049 public BSNPROBE setControllerId(long controllerId) {
50 this.controllerId = controllerId;
51 return this;
52 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080053
Ray Milkey269ffb92014-04-03 14:43:30 -070054 public int getSequenceId() {
55 return sequenceId;
56 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080057
Ray Milkey269ffb92014-04-03 14:43:30 -070058 public BSNPROBE setSequenceId(int sequenceId) {
59 this.sequenceId = sequenceId;
60 return this;
61 }
62
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080063 public byte[] getSrcMac() {
Pavlin Radoslavov1d595c02014-04-16 14:11:54 -070064 return ArrayUtils.clone(this.srcMac);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080065 }
66
67 public BSNPROBE setSrcMac(byte[] srcMac) {
Pavlin Radoslavov1d595c02014-04-16 14:11:54 -070068 this.srcMac = ArrayUtils.clone(srcMac);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080069 return this;
70 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080071
Ray Milkey269ffb92014-04-03 14:43:30 -070072 public byte[] getDstMac() {
Pavlin Radoslavov1d595c02014-04-16 14:11:54 -070073 return ArrayUtils.clone(this.dstMac);
Ray Milkey269ffb92014-04-03 14:43:30 -070074 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080075
Ray Milkey269ffb92014-04-03 14:43:30 -070076 public BSNPROBE setDstMac(byte[] dstMac) {
Pavlin Radoslavov1d595c02014-04-16 14:11:54 -070077 this.dstMac = ArrayUtils.clone(dstMac);
Ray Milkey269ffb92014-04-03 14:43:30 -070078 return this;
79 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080080
Ray Milkey269ffb92014-04-03 14:43:30 -070081 public long getSrcSwDpid() {
82 return srcSwDpid;
83 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080084
Ray Milkey269ffb92014-04-03 14:43:30 -070085 public BSNPROBE setSrcSwDpid(long srcSwDpid) {
86 this.srcSwDpid = srcSwDpid;
87 return this;
88 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080089
Ray Milkey269ffb92014-04-03 14:43:30 -070090 public int getSrcPortNo() {
91 return srcPortNo;
92 }
93
94 public BSNPROBE setSrcPortNo(int srcPortNo) {
95 this.srcPortNo = srcPortNo;
96 return this;
97 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080098
99 @Override
100 public byte[] serialize() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700101 short length = 8 /* controllerId */ + 4 /* seqId */
102 + 12 /* srcMac dstMac */ + 8 /* srcSwDpid */ + 4 /* srcPortNo */;
103
104 byte[] payloadData = null;
105 if (this.payload != null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800106 payload.setParent(this);
107 payloadData = payload.serialize();
108 length += payloadData.length;
109 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700110
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800111 byte[] data = new byte[length];
112 ByteBuffer bb = ByteBuffer.wrap(data);
113 bb.putLong(this.controllerId);
114 bb.putInt(this.sequenceId);
115 bb.put(this.srcMac);
116 bb.put(this.dstMac);
117 bb.putLong(this.srcSwDpid);
118 bb.putInt(this.srcPortNo);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700119 if (payloadData != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700120 bb.put(payloadData);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700121 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800122
Ray Milkeyb29e6262014-04-09 16:02:14 -0700123 if (this.parent != null && this.parent instanceof BSN) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700124 ((BSN) this.parent).setType(BSN.BSN_TYPE_PROBE);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700125 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800126
127 return data;
128 }
129
130 @Override
131 public IPacket deserialize(byte[] data, int offset, int length) {
132 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
Ray Milkey269ffb92014-04-03 14:43:30 -0700133
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800134 controllerId = bb.getLong();
135 sequenceId = bb.getInt();
136 bb.get(this.srcMac, 0, 6);
137 bb.get(this.dstMac, 0, 6);
138 this.srcSwDpid = bb.getLong();
139 this.srcPortNo = bb.getInt();
Ray Milkey269ffb92014-04-03 14:43:30 -0700140
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800141 if (bb.hasRemaining()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700142 this.payload = new Data();
143 this.payload = payload.deserialize(data, bb.position(), bb.limit() - bb.position());
144 this.payload.setParent(this);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800145 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700146
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800147 return this;
148 }
149
150 /* (non-Javadoc)
151 * @see java.lang.Object#hashCode()
152 */
153 @Override
154 public int hashCode() {
155 final int prime = 883;
156 int result = super.hashCode();
Pavlin Radoslavov45233db2014-04-09 17:33:01 -0700157 result = prime * result + Arrays.hashCode(srcMac);
158 result = prime * result + Arrays.hashCode(dstMac);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800159 result = prime * result + (int) (srcSwDpid >> 32) + (int) srcSwDpid;
160 result = prime * result + srcPortNo;
161 return result;
162 }
163
164 /* (non-Javadoc)
165 * @see java.lang.Object#equals(java.lang.Object)
166 */
167 @Override
168 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700169 if (this == obj) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800170 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700171 }
172 if (!super.equals(obj)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800173 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700174 }
175 if (!(obj instanceof BSNPROBE)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800176 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700177 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800178 BSNPROBE other = (BSNPROBE) obj;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700179 if (!Arrays.equals(srcMac, other.srcMac)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800180 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700181 }
182 if (!Arrays.equals(dstMac, other.dstMac)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700183 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700184 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800185 return (sequenceId == other.sequenceId &&
Ray Milkey269ffb92014-04-03 14:43:30 -0700186 srcSwDpid == other.srcSwDpid &&
187 srcPortNo == other.srcPortNo
188 );
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800189 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700190
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800191 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700192 StringBuffer sb = new StringBuffer("\n");
193 sb.append("BSN Probe packet");
194 sb.append("\nSource Mac: ");
195 sb.append(HexString.toHexString(srcMac));
196 sb.append("\nDestination Mac: ");
197 sb.append(HexString.toHexString(dstMac));
198 sb.append("\nSource Switch: ");
199 sb.append(HexString.toHexString(srcSwDpid));
200 sb.append(" port: " + srcPortNo);
201 sb.append("\nSequence No.:" + sequenceId);
202
203 return sb.toString();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800204 }
205}