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 | |
Pavlin Radoslavov | 1d595c0 | 2014-04-16 14:11:54 -0700 | [diff] [blame] | 25 | import org.apache.commons.lang.ArrayUtils; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 26 | import org.openflow.util.HexString; |
| 27 | |
| 28 | /** |
| 29 | * @author Shudong Zhou (shudong.zhou@bigswitch.com) |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 30 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 31 | public 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 Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 38 | |
| 39 | public BSNPROBE() { |
| 40 | srcMac = new byte[6]; |
| 41 | dstMac = new byte[6]; |
| 42 | } |
| 43 | |
| 44 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 45 | public long getControllerId() { |
| 46 | return this.controllerId; |
| 47 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 48 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 49 | public BSNPROBE setControllerId(long controllerId) { |
| 50 | this.controllerId = controllerId; |
| 51 | return this; |
| 52 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 53 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 54 | public int getSequenceId() { |
| 55 | return sequenceId; |
| 56 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 57 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 58 | public BSNPROBE setSequenceId(int sequenceId) { |
| 59 | this.sequenceId = sequenceId; |
| 60 | return this; |
| 61 | } |
| 62 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 63 | public byte[] getSrcMac() { |
Pavlin Radoslavov | 1d595c0 | 2014-04-16 14:11:54 -0700 | [diff] [blame] | 64 | return ArrayUtils.clone(this.srcMac); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | public BSNPROBE setSrcMac(byte[] srcMac) { |
Pavlin Radoslavov | 1d595c0 | 2014-04-16 14:11:54 -0700 | [diff] [blame] | 68 | this.srcMac = ArrayUtils.clone(srcMac); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 69 | return this; |
| 70 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 71 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 72 | public byte[] getDstMac() { |
Pavlin Radoslavov | 1d595c0 | 2014-04-16 14:11:54 -0700 | [diff] [blame] | 73 | return ArrayUtils.clone(this.dstMac); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 74 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 75 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 76 | public BSNPROBE setDstMac(byte[] dstMac) { |
Pavlin Radoslavov | 1d595c0 | 2014-04-16 14:11:54 -0700 | [diff] [blame] | 77 | this.dstMac = ArrayUtils.clone(dstMac); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 78 | return this; |
| 79 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 80 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 81 | public long getSrcSwDpid() { |
| 82 | return srcSwDpid; |
| 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 setSrcSwDpid(long srcSwDpid) { |
| 86 | this.srcSwDpid = srcSwDpid; |
| 87 | return this; |
| 88 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 89 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 90 | public int getSrcPortNo() { |
| 91 | return srcPortNo; |
| 92 | } |
| 93 | |
| 94 | public BSNPROBE setSrcPortNo(int srcPortNo) { |
| 95 | this.srcPortNo = srcPortNo; |
| 96 | return this; |
| 97 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 98 | |
| 99 | @Override |
| 100 | public byte[] serialize() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 101 | 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 Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 106 | payload.setParent(this); |
| 107 | payloadData = payload.serialize(); |
| 108 | length += payloadData.length; |
| 109 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 110 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 111 | 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 Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 119 | if (payloadData != null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 120 | bb.put(payloadData); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 121 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 122 | |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 123 | if (this.parent != null && this.parent instanceof BSN) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 124 | ((BSN) this.parent).setType(BSN.BSN_TYPE_PROBE); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 125 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 126 | |
| 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 133 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 134 | 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 140 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 141 | if (bb.hasRemaining()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 142 | this.payload = new Data(); |
| 143 | this.payload = payload.deserialize(data, bb.position(), bb.limit() - bb.position()); |
| 144 | this.payload.setParent(this); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 145 | } |
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 | 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 Radoslavov | 45233db | 2014-04-09 17:33:01 -0700 | [diff] [blame] | 157 | result = prime * result + Arrays.hashCode(srcMac); |
| 158 | result = prime * result + Arrays.hashCode(dstMac); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 159 | 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 Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 169 | if (this == obj) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 170 | return true; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 171 | } |
| 172 | if (!super.equals(obj)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 173 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 174 | } |
| 175 | if (!(obj instanceof BSNPROBE)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 176 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 177 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 178 | BSNPROBE other = (BSNPROBE) obj; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 179 | if (!Arrays.equals(srcMac, other.srcMac)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 180 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 181 | } |
| 182 | if (!Arrays.equals(dstMac, other.dstMac)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 183 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 184 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 185 | return (sequenceId == other.sequenceId && |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 186 | srcSwDpid == other.srcSwDpid && |
| 187 | srcPortNo == other.srcPortNo |
| 188 | ); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 189 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 190 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 191 | public String toString() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 192 | 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 Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 204 | } |
| 205 | } |