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