blob: c64d1a569eaeee0e40f96e9c379d94518a799005 [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
Yuta HIGUCHIaa132f52014-06-26 10:18:39 -070028// CHECKSTYLE IGNORE WriteTag FOR NEXT 2 LINES
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080029/**
30 * @author Shudong Zhou (shudong.zhou@bigswitch.com)
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080031 */
Ray Milkey269ffb92014-04-03 14:43:30 -070032public class BSNPROBE extends BasePacket {
33 protected long controllerId;
34 protected int sequenceId;
35 protected byte[] srcMac;
36 protected byte[] dstMac;
37 protected long srcSwDpid;
38 protected int srcPortNo;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080039
40 public BSNPROBE() {
41 srcMac = new byte[6];
42 dstMac = new byte[6];
43 }
44
45
Ray Milkey269ffb92014-04-03 14:43:30 -070046 public long getControllerId() {
47 return this.controllerId;
48 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080049
Ray Milkey269ffb92014-04-03 14:43:30 -070050 public BSNPROBE setControllerId(long controllerId) {
51 this.controllerId = controllerId;
52 return this;
53 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080054
Ray Milkey269ffb92014-04-03 14:43:30 -070055 public int getSequenceId() {
56 return sequenceId;
57 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080058
Ray Milkey269ffb92014-04-03 14:43:30 -070059 public BSNPROBE setSequenceId(int sequenceId) {
60 this.sequenceId = sequenceId;
61 return this;
62 }
63
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080064 public byte[] getSrcMac() {
Pavlin Radoslavov1d595c02014-04-16 14:11:54 -070065 return ArrayUtils.clone(this.srcMac);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080066 }
67
68 public BSNPROBE setSrcMac(byte[] srcMac) {
Pavlin Radoslavov1d595c02014-04-16 14:11:54 -070069 this.srcMac = ArrayUtils.clone(srcMac);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080070 return this;
71 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080072
Ray Milkey269ffb92014-04-03 14:43:30 -070073 public byte[] getDstMac() {
Pavlin Radoslavov1d595c02014-04-16 14:11:54 -070074 return ArrayUtils.clone(this.dstMac);
Ray Milkey269ffb92014-04-03 14:43:30 -070075 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080076
Ray Milkey269ffb92014-04-03 14:43:30 -070077 public BSNPROBE setDstMac(byte[] dstMac) {
Pavlin Radoslavov1d595c02014-04-16 14:11:54 -070078 this.dstMac = ArrayUtils.clone(dstMac);
Ray Milkey269ffb92014-04-03 14:43:30 -070079 return this;
80 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080081
Ray Milkey269ffb92014-04-03 14:43:30 -070082 public long getSrcSwDpid() {
83 return srcSwDpid;
84 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080085
Ray Milkey269ffb92014-04-03 14:43:30 -070086 public BSNPROBE setSrcSwDpid(long srcSwDpid) {
87 this.srcSwDpid = srcSwDpid;
88 return this;
89 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080090
Ray Milkey269ffb92014-04-03 14:43:30 -070091 public int getSrcPortNo() {
92 return srcPortNo;
93 }
94
95 public BSNPROBE setSrcPortNo(int srcPortNo) {
96 this.srcPortNo = srcPortNo;
97 return this;
98 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080099
100 @Override
101 public byte[] serialize() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700102 short length = 8 /* controllerId */ + 4 /* seqId */
103 + 12 /* srcMac dstMac */ + 8 /* srcSwDpid */ + 4 /* srcPortNo */;
104
105 byte[] payloadData = null;
106 if (this.payload != null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800107 payload.setParent(this);
108 payloadData = payload.serialize();
109 length += payloadData.length;
110 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700111
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800112 byte[] data = new byte[length];
113 ByteBuffer bb = ByteBuffer.wrap(data);
114 bb.putLong(this.controllerId);
115 bb.putInt(this.sequenceId);
116 bb.put(this.srcMac);
117 bb.put(this.dstMac);
118 bb.putLong(this.srcSwDpid);
119 bb.putInt(this.srcPortNo);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700120 if (payloadData != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700121 bb.put(payloadData);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700122 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800123
Ray Milkeyb29e6262014-04-09 16:02:14 -0700124 if (this.parent != null && this.parent instanceof BSN) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700125 ((BSN) this.parent).setType(BSN.BSN_TYPE_PROBE);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700126 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800127
128 return data;
129 }
130
131 @Override
132 public IPacket deserialize(byte[] data, int offset, int length) {
133 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
Ray Milkey269ffb92014-04-03 14:43:30 -0700134
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800135 controllerId = bb.getLong();
136 sequenceId = bb.getInt();
137 bb.get(this.srcMac, 0, 6);
138 bb.get(this.dstMac, 0, 6);
139 this.srcSwDpid = bb.getLong();
140 this.srcPortNo = bb.getInt();
Ray Milkey269ffb92014-04-03 14:43:30 -0700141
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800142 if (bb.hasRemaining()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700143 this.payload = new Data();
144 this.payload = payload.deserialize(data, bb.position(), bb.limit() - bb.position());
145 this.payload.setParent(this);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800146 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700147
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800148 return this;
149 }
150
151 /* (non-Javadoc)
152 * @see java.lang.Object#hashCode()
153 */
154 @Override
155 public int hashCode() {
156 final int prime = 883;
157 int result = super.hashCode();
Pavlin Radoslavov45233db2014-04-09 17:33:01 -0700158 result = prime * result + Arrays.hashCode(srcMac);
159 result = prime * result + Arrays.hashCode(dstMac);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800160 result = prime * result + (int) (srcSwDpid >> 32) + (int) srcSwDpid;
161 result = prime * result + srcPortNo;
162 return result;
163 }
164
165 /* (non-Javadoc)
166 * @see java.lang.Object#equals(java.lang.Object)
167 */
168 @Override
169 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700170 if (this == obj) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800171 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700172 }
173 if (!super.equals(obj)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800174 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700175 }
176 if (!(obj instanceof BSNPROBE)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800177 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700178 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800179 BSNPROBE other = (BSNPROBE) obj;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700180 if (!Arrays.equals(srcMac, other.srcMac)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800181 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700182 }
183 if (!Arrays.equals(dstMac, other.dstMac)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700184 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700185 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800186 return (sequenceId == other.sequenceId &&
Ray Milkey269ffb92014-04-03 14:43:30 -0700187 srcSwDpid == other.srcSwDpid &&
188 srcPortNo == other.srcPortNo
189 );
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800190 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700191
Yuta HIGUCHIaa132f52014-06-26 10:18:39 -0700192 @Override
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800193 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700194 StringBuffer sb = new StringBuffer("\n");
195 sb.append("BSN Probe packet");
196 sb.append("\nSource Mac: ");
197 sb.append(HexString.toHexString(srcMac));
198 sb.append("\nDestination Mac: ");
199 sb.append(HexString.toHexString(dstMac));
200 sb.append("\nSource Switch: ");
201 sb.append(HexString.toHexString(srcSwDpid));
202 sb.append(" port: " + srcPortNo);
203 sb.append("\nSequence No.:" + sequenceId);
204
205 return sb.toString();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800206 }
207}