Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 16 | package org.onlab.packet; |
| 17 | |
| 18 | import com.google.common.collect.Lists; |
Ayaka Koshibe | 12c8c08 | 2015-12-08 12:48:46 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Maps; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 20 | import org.apache.commons.lang.ArrayUtils; |
| 21 | |
| 22 | import java.nio.ByteBuffer; |
Ray Milkey | 241b96a | 2014-11-17 13:08:20 -0800 | [diff] [blame] | 23 | import java.nio.charset.StandardCharsets; |
jaegonkim | 47b1b4a | 2017-12-04 14:08:26 +0900 | [diff] [blame] | 24 | import java.util.Arrays; |
Jonathan Hart | 7838c51 | 2016-06-07 15:18:22 -0700 | [diff] [blame] | 25 | import java.util.HashMap; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 26 | |
Ayaka Koshibe | 12c8c08 | 2015-12-08 12:48:46 -0800 | [diff] [blame] | 27 | import static org.onlab.packet.LLDPOrganizationalTLV.OUI_LENGTH; |
| 28 | import static org.onlab.packet.LLDPOrganizationalTLV.SUBTYPE_LENGTH; |
| 29 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 30 | /** |
Ayaka Koshibe | 12c8c08 | 2015-12-08 12:48:46 -0800 | [diff] [blame] | 31 | * ONOS LLDP containing organizational TLV for ONOS device discovery. |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 32 | */ |
| 33 | public class ONOSLLDP extends LLDP { |
Charles Chan | 928ff8b | 2017-05-04 12:22:54 -0700 | [diff] [blame] | 34 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 35 | public static final String DEFAULT_DEVICE = "INVALID"; |
| 36 | public static final String DEFAULT_NAME = "ONOS Discovery"; |
| 37 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 38 | |
Ayaka Koshibe | 12c8c08 | 2015-12-08 12:48:46 -0800 | [diff] [blame] | 39 | protected static final byte NAME_SUBTYPE = 1; |
| 40 | protected static final byte DEVICE_SUBTYPE = 2; |
| 41 | protected static final byte DOMAIN_SUBTYPE = 3; |
| 42 | |
| 43 | private static final short NAME_LENGTH = OUI_LENGTH + SUBTYPE_LENGTH; |
| 44 | private static final short DEVICE_LENGTH = OUI_LENGTH + SUBTYPE_LENGTH; |
| 45 | private static final short DOMAIN_LENGTH = OUI_LENGTH + SUBTYPE_LENGTH; |
| 46 | |
Jonathan Hart | 7838c51 | 2016-06-07 15:18:22 -0700 | [diff] [blame] | 47 | private final HashMap<Byte, LLDPOrganizationalTLV> opttlvs = Maps.newHashMap(); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 48 | |
| 49 | // TLV constants: type, size and subtype |
| 50 | // Organizationally specific TLV also have packet offset and contents of TLV |
| 51 | // header |
| 52 | private static final byte CHASSIS_TLV_TYPE = 1; |
| 53 | private static final byte CHASSIS_TLV_SIZE = 7; |
| 54 | private static final byte CHASSIS_TLV_SUBTYPE = 4; |
| 55 | |
| 56 | private static final byte PORT_TLV_TYPE = 2; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 57 | private static final byte PORT_TLV_SUBTYPE = 2; |
| 58 | |
| 59 | private static final byte TTL_TLV_TYPE = 3; |
| 60 | |
jaegonkim | 47b1b4a | 2017-12-04 14:08:26 +0900 | [diff] [blame] | 61 | private static final byte PORT_DESC_TLV_TYPE = 4; |
| 62 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 63 | private final byte[] ttlValue = new byte[] {0, 0x78}; |
| 64 | |
Ayaka Koshibe | 12c8c08 | 2015-12-08 12:48:46 -0800 | [diff] [blame] | 65 | // Only needs to be accessed from LinkProbeFactory. |
Ray Milkey | 88cc343 | 2017-03-30 17:19:08 -0700 | [diff] [blame] | 66 | public ONOSLLDP(byte... subtype) { |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 67 | super(); |
Ayaka Koshibe | 12c8c08 | 2015-12-08 12:48:46 -0800 | [diff] [blame] | 68 | for (byte st : subtype) { |
| 69 | opttlvs.put(st, new LLDPOrganizationalTLV()); |
| 70 | } |
| 71 | // guarantee the following (name and device) TLVs exist |
| 72 | opttlvs.putIfAbsent(NAME_SUBTYPE, new LLDPOrganizationalTLV()); |
| 73 | opttlvs.putIfAbsent(DEVICE_SUBTYPE, new LLDPOrganizationalTLV()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 74 | setName(DEFAULT_NAME); |
| 75 | setDevice(DEFAULT_DEVICE); |
Ayaka Koshibe | 12c8c08 | 2015-12-08 12:48:46 -0800 | [diff] [blame] | 76 | |
Jonathan Hart | 7838c51 | 2016-06-07 15:18:22 -0700 | [diff] [blame] | 77 | setOptionalTLVList(Lists.newArrayList(opttlvs.values())); |
Yuta HIGUCHI | 3e848a8 | 2014-11-02 20:19:42 -0800 | [diff] [blame] | 78 | setTtl(new LLDPTLV().setType(TTL_TLV_TYPE) |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 79 | .setLength((short) ttlValue.length) |
| 80 | .setValue(ttlValue)); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | private ONOSLLDP(LLDP lldp) { |
| 84 | this.portId = lldp.getPortId(); |
| 85 | this.chassisId = lldp.getChassisId(); |
| 86 | this.ttl = lldp.getTtl(); |
| 87 | this.optionalTLVList = lldp.getOptionalTLVList(); |
| 88 | } |
| 89 | |
| 90 | public void setName(String name) { |
Ayaka Koshibe | 12c8c08 | 2015-12-08 12:48:46 -0800 | [diff] [blame] | 91 | LLDPOrganizationalTLV nametlv = opttlvs.get(NAME_SUBTYPE); |
| 92 | nametlv.setLength((short) (name.length() + NAME_LENGTH)); |
| 93 | nametlv.setInfoString(name); |
| 94 | nametlv.setSubType(NAME_SUBTYPE); |
Charles Chan | 928ff8b | 2017-05-04 12:22:54 -0700 | [diff] [blame] | 95 | nametlv.setOUI(MacAddress.ONOS.oui()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | public void setDevice(String device) { |
Ayaka Koshibe | 12c8c08 | 2015-12-08 12:48:46 -0800 | [diff] [blame] | 99 | LLDPOrganizationalTLV devicetlv = opttlvs.get(DEVICE_SUBTYPE); |
| 100 | devicetlv.setInfoString(device); |
| 101 | devicetlv.setLength((short) (device.length() + DEVICE_LENGTH)); |
| 102 | devicetlv.setSubType(DEVICE_SUBTYPE); |
Charles Chan | 928ff8b | 2017-05-04 12:22:54 -0700 | [diff] [blame] | 103 | devicetlv.setOUI(MacAddress.ONOS.oui()); |
Ayaka Koshibe | 12c8c08 | 2015-12-08 12:48:46 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | public void setDomainInfo(String domainId) { |
| 107 | LLDPOrganizationalTLV domaintlv = opttlvs.get(DOMAIN_SUBTYPE); |
| 108 | if (domaintlv == null) { |
| 109 | // maybe warn people not to set this if remote probes aren't. |
| 110 | return; |
| 111 | } |
| 112 | domaintlv.setInfoString(domainId); |
| 113 | domaintlv.setLength((short) (domainId.length() + DOMAIN_LENGTH)); |
| 114 | domaintlv.setSubType(DOMAIN_SUBTYPE); |
Charles Chan | 928ff8b | 2017-05-04 12:22:54 -0700 | [diff] [blame] | 115 | domaintlv.setOUI(MacAddress.ONOS.oui()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | public void setChassisId(final ChassisId chassisId) { |
| 119 | MacAddress chassisMac = MacAddress.valueOf(chassisId.value()); |
| 120 | byte[] chassis = ArrayUtils.addAll(new byte[] {CHASSIS_TLV_SUBTYPE}, |
Yuta HIGUCHI | 3e848a8 | 2014-11-02 20:19:42 -0800 | [diff] [blame] | 121 | chassisMac.toBytes()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 122 | |
| 123 | LLDPTLV chassisTLV = new LLDPTLV(); |
| 124 | chassisTLV.setLength(CHASSIS_TLV_SIZE); |
| 125 | chassisTLV.setType(CHASSIS_TLV_TYPE); |
| 126 | chassisTLV.setValue(chassis); |
| 127 | this.setChassisId(chassisTLV); |
| 128 | } |
| 129 | |
| 130 | public void setPortId(final int portNumber) { |
| 131 | byte[] port = ArrayUtils.addAll(new byte[] {PORT_TLV_SUBTYPE}, |
DongRyeol Cha | e0c98db | 2018-07-12 16:17:21 +0900 | [diff] [blame] | 132 | String.valueOf(portNumber).getBytes(StandardCharsets.UTF_8)); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 133 | |
| 134 | LLDPTLV portTLV = new LLDPTLV(); |
DongRyeol Cha | e0c98db | 2018-07-12 16:17:21 +0900 | [diff] [blame] | 135 | portTLV.setLength((short) port.length); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 136 | portTLV.setType(PORT_TLV_TYPE); |
| 137 | portTLV.setValue(port); |
| 138 | this.setPortId(portTLV); |
| 139 | } |
| 140 | |
| 141 | public LLDPOrganizationalTLV getNameTLV() { |
| 142 | for (LLDPTLV tlv : this.getOptionalTLVList()) { |
| 143 | if (tlv.getType() == LLDPOrganizationalTLV.ORGANIZATIONAL_TLV_TYPE) { |
| 144 | LLDPOrganizationalTLV orgTLV = (LLDPOrganizationalTLV) tlv; |
| 145 | if (orgTLV.getSubType() == NAME_SUBTYPE) { |
| 146 | return orgTLV; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | return null; |
| 151 | } |
| 152 | |
| 153 | public LLDPOrganizationalTLV getDeviceTLV() { |
| 154 | for (LLDPTLV tlv : this.getOptionalTLVList()) { |
| 155 | if (tlv.getType() == LLDPOrganizationalTLV.ORGANIZATIONAL_TLV_TYPE) { |
| 156 | LLDPOrganizationalTLV orgTLV = (LLDPOrganizationalTLV) tlv; |
| 157 | if (orgTLV.getSubType() == DEVICE_SUBTYPE) { |
| 158 | return orgTLV; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | return null; |
| 163 | } |
| 164 | |
Ayaka Koshibe | 12c8c08 | 2015-12-08 12:48:46 -0800 | [diff] [blame] | 165 | /** |
| 166 | * Gets the TLV associated with remote probing. This TLV will be null if |
| 167 | * remote probing is disabled. |
| 168 | * |
| 169 | * @return A TLV containing domain ID, or null. |
| 170 | */ |
| 171 | public LLDPOrganizationalTLV getDomainTLV() { |
| 172 | for (LLDPTLV tlv : this.getOptionalTLVList()) { |
| 173 | if (tlv.getType() == LLDPOrganizationalTLV.ORGANIZATIONAL_TLV_TYPE) { |
| 174 | LLDPOrganizationalTLV orgTLV = (LLDPOrganizationalTLV) tlv; |
| 175 | if (orgTLV.getSubType() == DOMAIN_SUBTYPE) { |
| 176 | return orgTLV; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | return null; |
| 181 | } |
| 182 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 183 | public String getNameString() { |
| 184 | LLDPOrganizationalTLV tlv = getNameTLV(); |
| 185 | if (tlv != null) { |
Ray Milkey | 241b96a | 2014-11-17 13:08:20 -0800 | [diff] [blame] | 186 | return new String(tlv.getInfoString(), StandardCharsets.UTF_8); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 187 | } |
| 188 | return null; |
| 189 | } |
| 190 | |
| 191 | public String getDeviceString() { |
| 192 | LLDPOrganizationalTLV tlv = getDeviceTLV(); |
| 193 | if (tlv != null) { |
Ray Milkey | 241b96a | 2014-11-17 13:08:20 -0800 | [diff] [blame] | 194 | return new String(tlv.getInfoString(), StandardCharsets.UTF_8); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 195 | } |
| 196 | return null; |
| 197 | } |
| 198 | |
Ayaka Koshibe | 12c8c08 | 2015-12-08 12:48:46 -0800 | [diff] [blame] | 199 | public String getDomainString() { |
| 200 | LLDPOrganizationalTLV tlv = getDomainTLV(); |
| 201 | if (tlv != null) { |
| 202 | return new String(tlv.getInfoString(), StandardCharsets.UTF_8); |
| 203 | } |
| 204 | return null; |
| 205 | } |
| 206 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 207 | public Integer getPort() { |
| 208 | ByteBuffer portBB = ByteBuffer.wrap(this.getPortId().getValue()); |
| 209 | portBB.position(1); |
DongRyeol Cha | e0c98db | 2018-07-12 16:17:21 +0900 | [diff] [blame] | 210 | |
| 211 | return Integer.parseInt(new String(portBB.array(), |
| 212 | portBB.position(), portBB.remaining(), StandardCharsets.UTF_8)); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Given an ethernet packet, determines if this is an LLDP from |
| 217 | * ONOS and returns the device the LLDP came from. |
| 218 | * @param eth an ethernet packet |
| 219 | * @return a the lldp packet or null |
| 220 | */ |
| 221 | public static ONOSLLDP parseONOSLLDP(Ethernet eth) { |
| 222 | if (eth.getEtherType() == Ethernet.TYPE_LLDP || |
| 223 | eth.getEtherType() == Ethernet.TYPE_BSN) { |
Jonathan Hart | 7838c51 | 2016-06-07 15:18:22 -0700 | [diff] [blame] | 224 | ONOSLLDP onosLldp = new ONOSLLDP((LLDP) eth.getPayload()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 225 | if (ONOSLLDP.DEFAULT_NAME.equals(onosLldp.getNameString())) { |
| 226 | return onosLldp; |
| 227 | } |
| 228 | } |
| 229 | return null; |
| 230 | } |
Ayaka Koshibe | 12c8c08 | 2015-12-08 12:48:46 -0800 | [diff] [blame] | 231 | |
| 232 | /** |
| 233 | * Creates a link probe for link discovery/verification. |
| 234 | * |
| 235 | * @param deviceId The device ID as a String |
| 236 | * @param chassisId The chassis ID of the device |
| 237 | * @param portNum Port number of port to send probe out of |
| 238 | * @return ONOSLLDP probe message |
| 239 | */ |
| 240 | public static ONOSLLDP onosLLDP(String deviceId, ChassisId chassisId, int portNum) { |
| 241 | ONOSLLDP probe = new ONOSLLDP(NAME_SUBTYPE, DEVICE_SUBTYPE); |
| 242 | probe.setPortId(portNum); |
| 243 | probe.setDevice(deviceId); |
| 244 | probe.setChassisId(chassisId); |
| 245 | return probe; |
| 246 | } |
jaegonkim | 47b1b4a | 2017-12-04 14:08:26 +0900 | [diff] [blame] | 247 | |
| 248 | /** |
| 249 | * Creates a link probe for link discovery/verification. |
| 250 | * |
| 251 | * @param deviceId The device ID as a String |
| 252 | * @param chassisId The chassis ID of the device |
| 253 | * @param portNum Port number of port to send probe out of |
| 254 | * @param portDesc Port description of port to send probe out of |
| 255 | * @return ONOSLLDP probe message |
| 256 | */ |
| 257 | public static ONOSLLDP onosLLDP(String deviceId, ChassisId chassisId, int portNum, String portDesc) { |
| 258 | |
| 259 | ONOSLLDP probe = onosLLDP(deviceId, chassisId, portNum); |
| 260 | |
| 261 | if (portDesc != null && !portDesc.isEmpty()) { |
| 262 | byte[] bPortDesc = portDesc.getBytes(StandardCharsets.UTF_8); |
| 263 | |
| 264 | if (bPortDesc.length > LLDPTLV.MAX_LENGTH) { |
| 265 | bPortDesc = Arrays.copyOf(bPortDesc, LLDPTLV.MAX_LENGTH); |
| 266 | } |
| 267 | LLDPTLV portDescTlv = new LLDPTLV() |
| 268 | .setType(PORT_DESC_TLV_TYPE) |
| 269 | .setLength((short) bPortDesc.length) |
| 270 | .setValue(bPortDesc); |
| 271 | probe.addOptionalTLV(portDescTlv); |
| 272 | } |
| 273 | return probe; |
| 274 | } |
| 275 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 276 | } |