blob: c45ff7188c6178e1996f8cbd5cf5dcdd744832ad [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska24c849c2014-10-27 09:53:05 -070015 */
alshabib7911a052014-10-16 17:49:37 -070016package org.onlab.packet;
17
18import com.google.common.collect.Lists;
Ayaka Koshibe12c8c082015-12-08 12:48:46 -080019import com.google.common.collect.Maps;
alshabib7911a052014-10-16 17:49:37 -070020import org.apache.commons.lang.ArrayUtils;
21
22import java.nio.ByteBuffer;
Ray Milkey241b96a2014-11-17 13:08:20 -080023import java.nio.charset.StandardCharsets;
jaegonkim47b1b4a2017-12-04 14:08:26 +090024import java.util.Arrays;
Jonathan Hart7838c512016-06-07 15:18:22 -070025import java.util.HashMap;
alshabib7911a052014-10-16 17:49:37 -070026
Ayaka Koshibe12c8c082015-12-08 12:48:46 -080027import static org.onlab.packet.LLDPOrganizationalTLV.OUI_LENGTH;
28import static org.onlab.packet.LLDPOrganizationalTLV.SUBTYPE_LENGTH;
29
alshabib7911a052014-10-16 17:49:37 -070030/**
Ayaka Koshibe12c8c082015-12-08 12:48:46 -080031 * ONOS LLDP containing organizational TLV for ONOS device discovery.
alshabib7911a052014-10-16 17:49:37 -070032 */
33public class ONOSLLDP extends LLDP {
Charles Chan928ff8b2017-05-04 12:22:54 -070034
alshabib7911a052014-10-16 17:49:37 -070035 public static final String DEFAULT_DEVICE = "INVALID";
36 public static final String DEFAULT_NAME = "ONOS Discovery";
37
alshabib7911a052014-10-16 17:49:37 -070038
Ayaka Koshibe12c8c082015-12-08 12:48:46 -080039 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 Hart7838c512016-06-07 15:18:22 -070047 private final HashMap<Byte, LLDPOrganizationalTLV> opttlvs = Maps.newHashMap();
alshabib7911a052014-10-16 17:49:37 -070048
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;
57 private static final byte PORT_TLV_SIZE = 5;
58 private static final byte PORT_TLV_SUBTYPE = 2;
59
60 private static final byte TTL_TLV_TYPE = 3;
61
jaegonkim47b1b4a2017-12-04 14:08:26 +090062 private static final byte PORT_DESC_TLV_TYPE = 4;
63
alshabib7911a052014-10-16 17:49:37 -070064 private final byte[] ttlValue = new byte[] {0, 0x78};
65
Ayaka Koshibe12c8c082015-12-08 12:48:46 -080066 // Only needs to be accessed from LinkProbeFactory.
Ray Milkey88cc3432017-03-30 17:19:08 -070067 public ONOSLLDP(byte... subtype) {
alshabib7911a052014-10-16 17:49:37 -070068 super();
Ayaka Koshibe12c8c082015-12-08 12:48:46 -080069 for (byte st : subtype) {
70 opttlvs.put(st, new LLDPOrganizationalTLV());
71 }
72 // guarantee the following (name and device) TLVs exist
73 opttlvs.putIfAbsent(NAME_SUBTYPE, new LLDPOrganizationalTLV());
74 opttlvs.putIfAbsent(DEVICE_SUBTYPE, new LLDPOrganizationalTLV());
alshabib7911a052014-10-16 17:49:37 -070075 setName(DEFAULT_NAME);
76 setDevice(DEFAULT_DEVICE);
Ayaka Koshibe12c8c082015-12-08 12:48:46 -080077
Jonathan Hart7838c512016-06-07 15:18:22 -070078 setOptionalTLVList(Lists.newArrayList(opttlvs.values()));
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -080079 setTtl(new LLDPTLV().setType(TTL_TLV_TYPE)
alshabib7911a052014-10-16 17:49:37 -070080 .setLength((short) ttlValue.length)
81 .setValue(ttlValue));
alshabib7911a052014-10-16 17:49:37 -070082 }
83
84 private ONOSLLDP(LLDP lldp) {
85 this.portId = lldp.getPortId();
86 this.chassisId = lldp.getChassisId();
87 this.ttl = lldp.getTtl();
88 this.optionalTLVList = lldp.getOptionalTLVList();
89 }
90
91 public void setName(String name) {
Ayaka Koshibe12c8c082015-12-08 12:48:46 -080092 LLDPOrganizationalTLV nametlv = opttlvs.get(NAME_SUBTYPE);
93 nametlv.setLength((short) (name.length() + NAME_LENGTH));
94 nametlv.setInfoString(name);
95 nametlv.setSubType(NAME_SUBTYPE);
Charles Chan928ff8b2017-05-04 12:22:54 -070096 nametlv.setOUI(MacAddress.ONOS.oui());
alshabib7911a052014-10-16 17:49:37 -070097 }
98
99 public void setDevice(String device) {
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800100 LLDPOrganizationalTLV devicetlv = opttlvs.get(DEVICE_SUBTYPE);
101 devicetlv.setInfoString(device);
102 devicetlv.setLength((short) (device.length() + DEVICE_LENGTH));
103 devicetlv.setSubType(DEVICE_SUBTYPE);
Charles Chan928ff8b2017-05-04 12:22:54 -0700104 devicetlv.setOUI(MacAddress.ONOS.oui());
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800105 }
106
107 public void setDomainInfo(String domainId) {
108 LLDPOrganizationalTLV domaintlv = opttlvs.get(DOMAIN_SUBTYPE);
109 if (domaintlv == null) {
110 // maybe warn people not to set this if remote probes aren't.
111 return;
112 }
113 domaintlv.setInfoString(domainId);
114 domaintlv.setLength((short) (domainId.length() + DOMAIN_LENGTH));
115 domaintlv.setSubType(DOMAIN_SUBTYPE);
Charles Chan928ff8b2017-05-04 12:22:54 -0700116 domaintlv.setOUI(MacAddress.ONOS.oui());
alshabib7911a052014-10-16 17:49:37 -0700117 }
118
119 public void setChassisId(final ChassisId chassisId) {
120 MacAddress chassisMac = MacAddress.valueOf(chassisId.value());
121 byte[] chassis = ArrayUtils.addAll(new byte[] {CHASSIS_TLV_SUBTYPE},
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -0800122 chassisMac.toBytes());
alshabib7911a052014-10-16 17:49:37 -0700123
124 LLDPTLV chassisTLV = new LLDPTLV();
125 chassisTLV.setLength(CHASSIS_TLV_SIZE);
126 chassisTLV.setType(CHASSIS_TLV_TYPE);
127 chassisTLV.setValue(chassis);
128 this.setChassisId(chassisTLV);
129 }
130
131 public void setPortId(final int portNumber) {
132 byte[] port = ArrayUtils.addAll(new byte[] {PORT_TLV_SUBTYPE},
133 ByteBuffer.allocate(4).putInt(portNumber).array());
134
135 LLDPTLV portTLV = new LLDPTLV();
136 portTLV.setLength(PORT_TLV_SIZE);
137 portTLV.setType(PORT_TLV_TYPE);
138 portTLV.setValue(port);
139 this.setPortId(portTLV);
140 }
141
142 public LLDPOrganizationalTLV getNameTLV() {
143 for (LLDPTLV tlv : this.getOptionalTLVList()) {
144 if (tlv.getType() == LLDPOrganizationalTLV.ORGANIZATIONAL_TLV_TYPE) {
145 LLDPOrganizationalTLV orgTLV = (LLDPOrganizationalTLV) tlv;
146 if (orgTLV.getSubType() == NAME_SUBTYPE) {
147 return orgTLV;
148 }
149 }
150 }
151 return null;
152 }
153
154 public LLDPOrganizationalTLV getDeviceTLV() {
155 for (LLDPTLV tlv : this.getOptionalTLVList()) {
156 if (tlv.getType() == LLDPOrganizationalTLV.ORGANIZATIONAL_TLV_TYPE) {
157 LLDPOrganizationalTLV orgTLV = (LLDPOrganizationalTLV) tlv;
158 if (orgTLV.getSubType() == DEVICE_SUBTYPE) {
159 return orgTLV;
160 }
161 }
162 }
163 return null;
164 }
165
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800166 /**
167 * Gets the TLV associated with remote probing. This TLV will be null if
168 * remote probing is disabled.
169 *
170 * @return A TLV containing domain ID, or null.
171 */
172 public LLDPOrganizationalTLV getDomainTLV() {
173 for (LLDPTLV tlv : this.getOptionalTLVList()) {
174 if (tlv.getType() == LLDPOrganizationalTLV.ORGANIZATIONAL_TLV_TYPE) {
175 LLDPOrganizationalTLV orgTLV = (LLDPOrganizationalTLV) tlv;
176 if (orgTLV.getSubType() == DOMAIN_SUBTYPE) {
177 return orgTLV;
178 }
179 }
180 }
181 return null;
182 }
183
alshabib7911a052014-10-16 17:49:37 -0700184 public String getNameString() {
185 LLDPOrganizationalTLV tlv = getNameTLV();
186 if (tlv != null) {
Ray Milkey241b96a2014-11-17 13:08:20 -0800187 return new String(tlv.getInfoString(), StandardCharsets.UTF_8);
alshabib7911a052014-10-16 17:49:37 -0700188 }
189 return null;
190 }
191
192 public String getDeviceString() {
193 LLDPOrganizationalTLV tlv = getDeviceTLV();
194 if (tlv != null) {
Ray Milkey241b96a2014-11-17 13:08:20 -0800195 return new String(tlv.getInfoString(), StandardCharsets.UTF_8);
alshabib7911a052014-10-16 17:49:37 -0700196 }
197 return null;
198 }
199
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800200 public String getDomainString() {
201 LLDPOrganizationalTLV tlv = getDomainTLV();
202 if (tlv != null) {
203 return new String(tlv.getInfoString(), StandardCharsets.UTF_8);
204 }
205 return null;
206 }
207
alshabib7911a052014-10-16 17:49:37 -0700208 public Integer getPort() {
209 ByteBuffer portBB = ByteBuffer.wrap(this.getPortId().getValue());
210 portBB.position(1);
211 return portBB.getInt();
212 }
213
214 /**
215 * Given an ethernet packet, determines if this is an LLDP from
216 * ONOS and returns the device the LLDP came from.
217 * @param eth an ethernet packet
218 * @return a the lldp packet or null
219 */
220 public static ONOSLLDP parseONOSLLDP(Ethernet eth) {
221 if (eth.getEtherType() == Ethernet.TYPE_LLDP ||
222 eth.getEtherType() == Ethernet.TYPE_BSN) {
Jonathan Hart7838c512016-06-07 15:18:22 -0700223 ONOSLLDP onosLldp = new ONOSLLDP((LLDP) eth.getPayload());
alshabib7911a052014-10-16 17:49:37 -0700224 if (ONOSLLDP.DEFAULT_NAME.equals(onosLldp.getNameString())) {
225 return onosLldp;
226 }
227 }
228 return null;
229 }
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800230
231 /**
232 * Creates a link probe for link discovery/verification.
233 *
234 * @param deviceId The device ID as a String
235 * @param chassisId The chassis ID of the device
236 * @param portNum Port number of port to send probe out of
237 * @return ONOSLLDP probe message
238 */
239 public static ONOSLLDP onosLLDP(String deviceId, ChassisId chassisId, int portNum) {
240 ONOSLLDP probe = new ONOSLLDP(NAME_SUBTYPE, DEVICE_SUBTYPE);
241 probe.setPortId(portNum);
242 probe.setDevice(deviceId);
243 probe.setChassisId(chassisId);
244 return probe;
245 }
jaegonkim47b1b4a2017-12-04 14:08:26 +0900246
247 /**
248 * Creates a link probe for link discovery/verification.
249 *
250 * @param deviceId The device ID as a String
251 * @param chassisId The chassis ID of the device
252 * @param portNum Port number of port to send probe out of
253 * @param portDesc Port description of port to send probe out of
254 * @return ONOSLLDP probe message
255 */
256 public static ONOSLLDP onosLLDP(String deviceId, ChassisId chassisId, int portNum, String portDesc) {
257
258 ONOSLLDP probe = onosLLDP(deviceId, chassisId, portNum);
259
260 if (portDesc != null && !portDesc.isEmpty()) {
261 byte[] bPortDesc = portDesc.getBytes(StandardCharsets.UTF_8);
262
263 if (bPortDesc.length > LLDPTLV.MAX_LENGTH) {
264 bPortDesc = Arrays.copyOf(bPortDesc, LLDPTLV.MAX_LENGTH);
265 }
266 LLDPTLV portDescTlv = new LLDPTLV()
267 .setType(PORT_DESC_TLV_TYPE)
268 .setLength((short) bPortDesc.length)
269 .setValue(bPortDesc);
270 probe.addOptionalTLV(portDescTlv);
271 }
272 return probe;
273 }
274
alshabib7911a052014-10-16 17:49:37 -0700275}