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