blob: 720b45f1c5b0fda4df580cb5c2fe9a341b1872d1 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
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**/
16
17/**
18 *
19 */
20package net.floodlightcontroller.packet;
21
22import java.nio.ByteBuffer;
23import java.util.Arrays;
24import org.openflow.util.HexString;
25
26/**
27 * @author Shudong Zhou (shudong.zhou@bigswitch.com)
28 *
29 */
30public 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;
37
38 public BSNPROBE() {
39 srcMac = new byte[6];
40 dstMac = new byte[6];
41 }
42
43
44 public long getControllerId() {
45 return this.controllerId;
46 }
47
48 public BSNPROBE setControllerId(long controllerId) {
49 this.controllerId = controllerId;
50 return this;
51 }
52
53 public int getSequenceId() {
54 return sequenceId;
55 }
56
57 public BSNPROBE setSequenceId(int sequenceId) {
58 this.sequenceId = sequenceId;
59 return this;
60 }
61
62 public byte[] getSrcMac() {
63 return this.srcMac;
64 }
65
66 public BSNPROBE setSrcMac(byte[] srcMac) {
67 this.srcMac = srcMac;
68 return this;
69 }
70
71 public byte[] getDstMac() {
72 return dstMac;
73 }
74
75 public BSNPROBE setDstMac(byte[] dstMac) {
76 this.dstMac = dstMac;
77 return this;
78 }
79
80 public long getSrcSwDpid() {
81 return srcSwDpid;
82 }
83
84 public BSNPROBE setSrcSwDpid(long srcSwDpid) {
85 this.srcSwDpid = srcSwDpid;
86 return this;
87 }
88
89 public int getSrcPortNo() {
90 return srcPortNo;
91 }
92
93 public BSNPROBE setSrcPortNo(int srcPortNo) {
94 this.srcPortNo = srcPortNo;
95 return this;
96 }
97
98 @Override
99 public byte[] serialize() {
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) {
105 payload.setParent(this);
106 payloadData = payload.serialize();
107 length += payloadData.length;
108 }
109
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);
118 if (payloadData != null)
119 bb.put(payloadData);
120
121 if (this.parent != null && this.parent instanceof BSN)
122 ((BSN)this.parent).setType(BSN.BSN_TYPE_PROBE);
123
124 return data;
125 }
126
127 @Override
128 public IPacket deserialize(byte[] data, int offset, int length) {
129 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
130
131 controllerId = bb.getLong();
132 sequenceId = bb.getInt();
133 bb.get(this.srcMac, 0, 6);
134 bb.get(this.dstMac, 0, 6);
135 this.srcSwDpid = bb.getLong();
136 this.srcPortNo = bb.getInt();
137
138 if (bb.hasRemaining()) {
139 this.payload = new Data();
140 this.payload = payload.deserialize(data, bb.position(), bb.limit() - bb.position());
141 this.payload.setParent(this);
142 }
143
144 return this;
145 }
146
147 /* (non-Javadoc)
148 * @see java.lang.Object#hashCode()
149 */
150 @Override
151 public int hashCode() {
152 final int prime = 883;
153 int result = super.hashCode();
154 result = prime * result + srcMac.hashCode();
155 result = prime * result + dstMac.hashCode();
156 result = prime * result + (int) (srcSwDpid >> 32) + (int) srcSwDpid;
157 result = prime * result + srcPortNo;
158 return result;
159 }
160
161 /* (non-Javadoc)
162 * @see java.lang.Object#equals(java.lang.Object)
163 */
164 @Override
165 public boolean equals(Object obj) {
166 if (this == obj)
167 return true;
168 if (!super.equals(obj))
169 return false;
170 if (!(obj instanceof BSNPROBE))
171 return false;
172 BSNPROBE other = (BSNPROBE) obj;
173 if (!Arrays.equals(srcMac, other.srcMac))
174 return false;
175 if (!Arrays.equals(dstMac, other.dstMac))
176 return false;
177 return (sequenceId == other.sequenceId &&
178 srcSwDpid == other.srcSwDpid &&
179 srcPortNo == other.srcPortNo
180 );
181 }
182
183 public String toString() {
184 StringBuffer sb = new StringBuffer("\n");
185 sb.append("BSN Probe packet");
186 sb.append("\nSource Mac: ");
187 sb.append(HexString.toHexString(srcMac));
188 sb.append("\nDestination Mac: ");
189 sb.append(HexString.toHexString(dstMac));
190 sb.append("\nSource Switch: ");
191 sb.append(HexString.toHexString(srcSwDpid));
192 sb.append(" port: " + srcPortNo);
193 sb.append("\nSequence No.:" + sequenceId);
194
195 return sb.toString();
196 }
197}