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() { |
| 63 | return this.srcMac; |
| 64 | } |
| 65 | |
| 66 | public BSNPROBE setSrcMac(byte[] srcMac) { |
| 67 | this.srcMac = srcMac; |
| 68 | return this; |
| 69 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 70 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 71 | public byte[] getDstMac() { |
| 72 | return dstMac; |
| 73 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 74 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 75 | public BSNPROBE setDstMac(byte[] dstMac) { |
| 76 | this.dstMac = dstMac; |
| 77 | return this; |
| 78 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 79 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 80 | public long getSrcSwDpid() { |
| 81 | return srcSwDpid; |
| 82 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 83 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 84 | public BSNPROBE setSrcSwDpid(long srcSwDpid) { |
| 85 | this.srcSwDpid = srcSwDpid; |
| 86 | return this; |
| 87 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 88 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 89 | public int getSrcPortNo() { |
| 90 | return srcPortNo; |
| 91 | } |
| 92 | |
| 93 | public BSNPROBE setSrcPortNo(int srcPortNo) { |
| 94 | this.srcPortNo = srcPortNo; |
| 95 | return this; |
| 96 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 97 | |
| 98 | @Override |
| 99 | public byte[] serialize() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 100 | short length = 8 /* controllerId */ + 4 /* seqId */ |
| 101 | + 12 /* srcMac dstMac */ + 8 /* srcSwDpid */ + 4 /* srcPortNo */; |
| 102 | |
| 103 | byte[] payloadData = null; |
| 104 | if (this.payload != null) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 105 | payload.setParent(this); |
| 106 | payloadData = payload.serialize(); |
| 107 | length += payloadData.length; |
| 108 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 109 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 110 | byte[] data = new byte[length]; |
| 111 | ByteBuffer bb = ByteBuffer.wrap(data); |
| 112 | bb.putLong(this.controllerId); |
| 113 | bb.putInt(this.sequenceId); |
| 114 | bb.put(this.srcMac); |
| 115 | bb.put(this.dstMac); |
| 116 | bb.putLong(this.srcSwDpid); |
| 117 | bb.putInt(this.srcPortNo); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame^] | 118 | if (payloadData != null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 119 | bb.put(payloadData); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame^] | 120 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 121 | |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame^] | 122 | if (this.parent != null && this.parent instanceof BSN) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 123 | ((BSN) this.parent).setType(BSN.BSN_TYPE_PROBE); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame^] | 124 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 125 | |
| 126 | return data; |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public IPacket deserialize(byte[] data, int offset, int length) { |
| 131 | ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 132 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 133 | controllerId = bb.getLong(); |
| 134 | sequenceId = bb.getInt(); |
| 135 | bb.get(this.srcMac, 0, 6); |
| 136 | bb.get(this.dstMac, 0, 6); |
| 137 | this.srcSwDpid = bb.getLong(); |
| 138 | this.srcPortNo = bb.getInt(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 139 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 140 | if (bb.hasRemaining()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 141 | this.payload = new Data(); |
| 142 | this.payload = payload.deserialize(data, bb.position(), bb.limit() - bb.position()); |
| 143 | this.payload.setParent(this); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 144 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 145 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 146 | return this; |
| 147 | } |
| 148 | |
| 149 | /* (non-Javadoc) |
| 150 | * @see java.lang.Object#hashCode() |
| 151 | */ |
| 152 | @Override |
| 153 | public int hashCode() { |
| 154 | final int prime = 883; |
| 155 | int result = super.hashCode(); |
Pavlin Radoslavov | 45233db | 2014-04-09 17:33:01 -0700 | [diff] [blame] | 156 | result = prime * result + Arrays.hashCode(srcMac); |
| 157 | result = prime * result + Arrays.hashCode(dstMac); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 158 | result = prime * result + (int) (srcSwDpid >> 32) + (int) srcSwDpid; |
| 159 | result = prime * result + srcPortNo; |
| 160 | return result; |
| 161 | } |
| 162 | |
| 163 | /* (non-Javadoc) |
| 164 | * @see java.lang.Object#equals(java.lang.Object) |
| 165 | */ |
| 166 | @Override |
| 167 | public boolean equals(Object obj) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame^] | 168 | if (this == obj) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 169 | return true; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame^] | 170 | } |
| 171 | if (!super.equals(obj)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 172 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame^] | 173 | } |
| 174 | if (!(obj instanceof BSNPROBE)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 175 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame^] | 176 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 177 | BSNPROBE other = (BSNPROBE) obj; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame^] | 178 | if (!Arrays.equals(srcMac, other.srcMac)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 179 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame^] | 180 | } |
| 181 | if (!Arrays.equals(dstMac, other.dstMac)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 182 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame^] | 183 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 184 | return (sequenceId == other.sequenceId && |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 185 | srcSwDpid == other.srcSwDpid && |
| 186 | srcPortNo == other.srcPortNo |
| 187 | ); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 188 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 189 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 190 | public String toString() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 191 | StringBuffer sb = new StringBuffer("\n"); |
| 192 | sb.append("BSN Probe packet"); |
| 193 | sb.append("\nSource Mac: "); |
| 194 | sb.append(HexString.toHexString(srcMac)); |
| 195 | sb.append("\nDestination Mac: "); |
| 196 | sb.append(HexString.toHexString(dstMac)); |
| 197 | sb.append("\nSource Switch: "); |
| 198 | sb.append(HexString.toHexString(srcSwDpid)); |
| 199 | sb.append(" port: " + srcPortNo); |
| 200 | sb.append("\nSequence No.:" + sequenceId); |
| 201 | |
| 202 | return sb.toString(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 203 | } |
| 204 | } |