Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 2 | * 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 Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 16 | |
| 17 | /** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 18 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 19 | */ |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 20 | package net.onrc.onos.core.packet; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 21 | |
| 22 | import java.nio.ByteBuffer; |
| 23 | import java.util.Arrays; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 24 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 25 | import org.openflow.util.HexString; |
| 26 | |
| 27 | /** |
| 28 | * @author Shudong Zhou (shudong.zhou@bigswitch.com) |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 29 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 30 | public 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 Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 37 | |
| 38 | public BSNPROBE() { |
| 39 | srcMac = new byte[6]; |
| 40 | dstMac = new byte[6]; |
| 41 | } |
| 42 | |
| 43 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 44 | public long getControllerId() { |
| 45 | return this.controllerId; |
| 46 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 47 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 48 | public BSNPROBE setControllerId(long controllerId) { |
| 49 | this.controllerId = controllerId; |
| 50 | return this; |
| 51 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 52 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 53 | public int getSequenceId() { |
| 54 | return sequenceId; |
| 55 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 56 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 57 | public BSNPROBE setSequenceId(int sequenceId) { |
| 58 | this.sequenceId = sequenceId; |
| 59 | return this; |
| 60 | } |
| 61 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 62 | public byte[] getSrcMac() { |
Pavlin Radoslavov | 0a9d5c3 | 2014-04-15 17:45:23 -0700 | [diff] [blame] | 63 | if (this.srcMac == null) { |
| 64 | return null; |
| 65 | } |
| 66 | return this.srcMac.clone(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | public BSNPROBE setSrcMac(byte[] srcMac) { |
Pavlin Radoslavov | 0a9d5c3 | 2014-04-15 17:45:23 -0700 | [diff] [blame] | 70 | if (srcMac == null) { |
| 71 | this.srcMac = null; |
| 72 | } else { |
| 73 | this.srcMac = srcMac.clone(); |
| 74 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 75 | return this; |
| 76 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 77 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 78 | public byte[] getDstMac() { |
Pavlin Radoslavov | 0a9d5c3 | 2014-04-15 17:45:23 -0700 | [diff] [blame] | 79 | if (this.dstMac == null) { |
| 80 | return null; |
| 81 | } |
| 82 | return this.dstMac.clone(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 83 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 84 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 85 | public BSNPROBE setDstMac(byte[] dstMac) { |
Pavlin Radoslavov | 0a9d5c3 | 2014-04-15 17:45:23 -0700 | [diff] [blame] | 86 | if (dstMac == null) { |
| 87 | this.dstMac = null; |
| 88 | } else { |
| 89 | this.dstMac = dstMac.clone(); |
| 90 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 91 | return this; |
| 92 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 93 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 94 | public long getSrcSwDpid() { |
| 95 | return srcSwDpid; |
| 96 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 97 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 98 | public BSNPROBE setSrcSwDpid(long srcSwDpid) { |
| 99 | this.srcSwDpid = srcSwDpid; |
| 100 | return this; |
| 101 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 102 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 103 | public int getSrcPortNo() { |
| 104 | return srcPortNo; |
| 105 | } |
| 106 | |
| 107 | public BSNPROBE setSrcPortNo(int srcPortNo) { |
| 108 | this.srcPortNo = srcPortNo; |
| 109 | return this; |
| 110 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 111 | |
| 112 | @Override |
| 113 | public byte[] serialize() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 114 | 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 Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 119 | payload.setParent(this); |
| 120 | payloadData = payload.serialize(); |
| 121 | length += payloadData.length; |
| 122 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 123 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 124 | 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 Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 132 | if (payloadData != null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 133 | bb.put(payloadData); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 134 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 135 | |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 136 | if (this.parent != null && this.parent instanceof BSN) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 137 | ((BSN) this.parent).setType(BSN.BSN_TYPE_PROBE); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 138 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 139 | |
| 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 146 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 147 | 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 153 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 154 | if (bb.hasRemaining()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 155 | this.payload = new Data(); |
| 156 | this.payload = payload.deserialize(data, bb.position(), bb.limit() - bb.position()); |
| 157 | this.payload.setParent(this); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 158 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 159 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 160 | 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 Radoslavov | 45233db | 2014-04-09 17:33:01 -0700 | [diff] [blame] | 170 | result = prime * result + Arrays.hashCode(srcMac); |
| 171 | result = prime * result + Arrays.hashCode(dstMac); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 172 | 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 Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 182 | if (this == obj) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 183 | return true; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 184 | } |
| 185 | if (!super.equals(obj)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 186 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 187 | } |
| 188 | if (!(obj instanceof BSNPROBE)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 189 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 190 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 191 | BSNPROBE other = (BSNPROBE) obj; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 192 | if (!Arrays.equals(srcMac, other.srcMac)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 193 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 194 | } |
| 195 | if (!Arrays.equals(dstMac, other.dstMac)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 196 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 197 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 198 | return (sequenceId == other.sequenceId && |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 199 | srcSwDpid == other.srcSwDpid && |
| 200 | srcPortNo == other.srcPortNo |
| 201 | ); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 202 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 203 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 204 | public String toString() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 205 | 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 Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 217 | } |
| 218 | } |