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 2011, Big Switch Networks, Inc. |
| 3 | * Originally created by David Erickson, Stanford University |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | * not use this file except in compliance with the License. You may obtain |
| 7 | * a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | * License for the specific language governing permissions and limitations |
| 15 | * under the License. |
| 16 | **/ |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 17 | |
| 18 | /** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 19 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 20 | */ |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 21 | package net.onrc.onos.core.packet; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 22 | |
| 23 | import java.nio.ByteBuffer; |
| 24 | import java.util.ArrayList; |
| 25 | import java.util.List; |
| 26 | |
| 27 | /** |
| 28 | * @author David Erickson (daviderickson@cs.stanford.edu) |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 29 | */ |
| 30 | public class LLDP extends BasePacket { |
| 31 | protected LLDPTLV chassisId; |
| 32 | protected LLDPTLV portId; |
| 33 | protected LLDPTLV ttl; |
| 34 | protected List<LLDPTLV> optionalTLVList; |
| 35 | protected short ethType; |
| 36 | |
| 37 | public LLDP() { |
| 38 | this.optionalTLVList = new ArrayList<LLDPTLV>(); |
| 39 | this.ethType = Ethernet.TYPE_LLDP; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @return the chassisId |
| 44 | */ |
| 45 | public LLDPTLV getChassisId() { |
| 46 | return chassisId; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @param chassisId the chassisId to set |
| 51 | */ |
| 52 | public LLDP setChassisId(LLDPTLV chassisId) { |
| 53 | this.chassisId = chassisId; |
| 54 | return this; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @return the portId |
| 59 | */ |
| 60 | public LLDPTLV getPortId() { |
| 61 | return portId; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @param portId the portId to set |
| 66 | */ |
| 67 | public LLDP setPortId(LLDPTLV portId) { |
| 68 | this.portId = portId; |
| 69 | return this; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @return the ttl |
| 74 | */ |
| 75 | public LLDPTLV getTtl() { |
| 76 | return ttl; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @param ttl the ttl to set |
| 81 | */ |
| 82 | public LLDP setTtl(LLDPTLV ttl) { |
| 83 | this.ttl = ttl; |
| 84 | return this; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @return the optionalTLVList |
| 89 | */ |
| 90 | public List<LLDPTLV> getOptionalTLVList() { |
| 91 | return optionalTLVList; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @param optionalTLVList the optionalTLVList to set |
| 96 | */ |
| 97 | public LLDP setOptionalTLVList(List<LLDPTLV> optionalTLVList) { |
| 98 | this.optionalTLVList = optionalTLVList; |
| 99 | return this; |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public byte[] serialize() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 104 | int length = 2 + this.chassisId.getLength() + 2 + this.portId.getLength() + |
| 105 | 2 + this.ttl.getLength() + 2; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 106 | for (LLDPTLV tlv : this.optionalTLVList) { |
| 107 | length += 2 + tlv.getLength(); |
| 108 | } |
| 109 | |
| 110 | byte[] data = new byte[length]; |
| 111 | ByteBuffer bb = ByteBuffer.wrap(data); |
| 112 | bb.put(this.chassisId.serialize()); |
| 113 | bb.put(this.portId.serialize()); |
| 114 | bb.put(this.ttl.serialize()); |
| 115 | for (LLDPTLV tlv : this.optionalTLVList) { |
| 116 | bb.put(tlv.serialize()); |
| 117 | } |
| 118 | bb.putShort((short) 0); // End of LLDPDU |
| 119 | |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 120 | if (this.parent != null && this.parent instanceof Ethernet) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 121 | ((Ethernet) this.parent).setEtherType(ethType); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 122 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 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 | LLDPTLV tlv; |
| 131 | do { |
| 132 | tlv = new LLDPTLV().deserialize(bb); |
| 133 | |
| 134 | // if there was a failure to deserialize stop processing TLVs |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 135 | if (tlv == null) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 136 | break; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 137 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 138 | switch (tlv.getType()) { |
| 139 | case 0x0: |
| 140 | // can throw this one away, its just an end delimiter |
| 141 | break; |
| 142 | case 0x1: |
| 143 | this.chassisId = tlv; |
| 144 | break; |
| 145 | case 0x2: |
| 146 | this.portId = tlv; |
| 147 | break; |
| 148 | case 0x3: |
| 149 | this.ttl = tlv; |
| 150 | break; |
| 151 | default: |
| 152 | this.optionalTLVList.add(tlv); |
| 153 | break; |
| 154 | } |
| 155 | } while (tlv.getType() != 0 && bb.hasRemaining()); |
| 156 | return this; |
| 157 | } |
| 158 | |
| 159 | /* (non-Javadoc) |
| 160 | * @see java.lang.Object#hashCode() |
| 161 | */ |
| 162 | @Override |
| 163 | public int hashCode() { |
| 164 | final int prime = 883; |
| 165 | int result = super.hashCode(); |
| 166 | result = prime * result |
| 167 | + ((chassisId == null) ? 0 : chassisId.hashCode()); |
| 168 | result = prime * result + (optionalTLVList.hashCode()); |
| 169 | result = prime * result + ((portId == null) ? 0 : portId.hashCode()); |
| 170 | result = prime * result + ((ttl == null) ? 0 : ttl.hashCode()); |
| 171 | return result; |
| 172 | } |
| 173 | |
| 174 | /* (non-Javadoc) |
| 175 | * @see java.lang.Object#equals(java.lang.Object) |
| 176 | */ |
| 177 | @Override |
| 178 | public boolean equals(Object obj) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 179 | if (this == obj) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 180 | return true; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 181 | } |
| 182 | if (!super.equals(obj)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 183 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 184 | } |
| 185 | if (!(obj instanceof LLDP)) { |
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 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 188 | LLDP other = (LLDP) obj; |
| 189 | if (chassisId == null) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 190 | if (other.chassisId != null) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 191 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 192 | } |
| 193 | } else if (!chassisId.equals(other.chassisId)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 194 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 195 | } |
| 196 | if (!optionalTLVList.equals(other.optionalTLVList)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 197 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 198 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 199 | if (portId == null) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 200 | if (other.portId != null) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 201 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 202 | } |
| 203 | } else if (!portId.equals(other.portId)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 204 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 205 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 206 | if (ttl == null) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 207 | if (other.ttl != null) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 208 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 209 | } |
| 210 | } else if (!ttl.equals(other.ttl)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 211 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 212 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 213 | return true; |
| 214 | } |
| 215 | } |