private static void addPortDesc(ONOSLLDP probe, String portDesc) { if (portDesc != null && !portDesc.isEmpty()) { byte[] bPortDesc = portDesc.getBytes(StandardCharsets.UTF_8); if (bPortDesc.length > LLDPTLV.MAX_LENGTH) { bPortDesc = Arrays.copyOf(bPortDesc, LLDPTLV.MAX_LENGTH); } LLDPTLV portDescTlv = new LLDPTLV() .setType(PORT_DESC_TLV_TYPE) .setLength((short) bPortDesc.length) .setValue(bPortDesc); probe.addOptionalTLV(portDescTlv); } }
@Override public byte[] serialize() { int length = 2 + this.chassisId.getLength() + 2 + this.portId.getLength() + 2 + this.ttl.getLength() + 2; for (final LLDPTLV tlv : this.optionalTLVList) { length += 2 + tlv.getLength(); } final byte[] data = new byte[length]; final ByteBuffer bb = ByteBuffer.wrap(data); bb.put(this.chassisId.serialize()); bb.put(this.portId.serialize()); bb.put(this.ttl.serialize()); for (final LLDPTLV tlv : this.optionalTLVList) { bb.put(tlv.serialize()); } bb.putShort((short) 0); // End of LLDPDU /* * if (this.parent != null && this.parent instanceof Ethernet) { * ((Ethernet) this.parent).setEtherType(this.ethType); } */ return data; }
public LLDPTLV getPortDescTLV() { for (LLDPTLV tlv : this.getOptionalTLVList()) { if (tlv.getType() == PORT_DESC_TLV_TYPE) { return tlv; } } log.error("Cannot find the port description tlv type."); return null; }
@Override public String toString() { return toStringHelper(getClass()) .add("chassisId", Arrays.toString(chassisId.getValue())) .add("portId", Arrays.toString(portId.getValue())) .add("ttl", Arrays.toString(ttl.getValue())) .add("ethType", Short.toString(ethType)) .toString(); // TODO: need to handle optionalTLVList } }
@Override public int hashCode() { final int prime = 883; int result = super.hashCode(); result = prime * result + (this.chassisId == null ? 0 : this.chassisId.hashCode()); result = prime * result + this.optionalTLVList.hashCode(); result = prime * result + (this.portId == null ? 0 : this.portId.hashCode()); result = prime * result + (this.ttl == null ? 0 : this.ttl.hashCode()); return result; }
@Override public byte[] serialize() { if (this.type != LLDPOrganizationalTLV.ORGANIZATIONAL_TLV_TYPE) { return super.serialize(); } final int valueLength = LLDPOrganizationalTLV.OUI_LENGTH + LLDPOrganizationalTLV.SUBTYPE_LENGTH + this.infoString.length; this.value = new byte[valueLength]; final ByteBuffer bb = ByteBuffer.wrap(this.value); bb.put(this.oui); bb.put(this.subType); bb.put(this.infoString); return super.serialize(); }
@Override public LLDPTLV deserialize(final ByteBuffer bb) throws DeserializationException { super.deserialize(bb); if (this.getType() != LLDPOrganizationalTLV.ORGANIZATIONAL_TLV_TYPE) { return this; } if (this.getLength() <= OUI_LENGTH + SUBTYPE_LENGTH) { throw new DeserializationException( "TLV length is less than required for organizational TLV"); } final ByteBuffer optionalField = ByteBuffer.wrap(this.value); final byte[] oui = new byte[LLDPOrganizationalTLV.OUI_LENGTH]; optionalField.get(oui); this.setOUI(oui); this.setSubType(optionalField.get()); final byte[] infoString = new byte[this.getLength() - LLDPOrganizationalTLV.OUI_LENGTH - LLDPOrganizationalTLV.SUBTYPE_LENGTH]; optionalField.get(infoString); this.setInfoString(infoString); return this; }
public String getPortNameString() { ByteBuffer portBB = ByteBuffer.wrap(this.getPortId().getValue()); byte type = portBB.get(); if (type == PORT_TLV_INTERFACE_NAME_SUBTYPE) { return new String(portBB.array(), portBB.position(), portBB.remaining(), StandardCharsets.UTF_8); } else { return null; } }
public void setPortName(final String portName) { byte[] port = ArrayUtils.addAll(new byte[] {PORT_TLV_INTERFACE_NAME_SUBTYPE}, portName.getBytes(StandardCharsets.UTF_8)); LLDPTLV portTLV = new LLDPTLV(); portTLV.setLength((short) port.length); portTLV.setType(PORT_TLV_TYPE); portTLV.setValue(port); this.setPortId(portTLV); }
public LLDPOrganizationalTLV getSigTLV() { for (LLDPTLV tlv : this.getOptionalTLVList()) { if (tlv.getType() == LLDPOrganizationalTLV.ORGANIZATIONAL_TLV_TYPE) { LLDPOrganizationalTLV orgTLV = (LLDPOrganizationalTLV) tlv; if (orgTLV.getSubType() == SIG_SUBTYPE) { return orgTLV; } } } return null; }
public String getPortDescString() { LLDPTLV tlv = getPortDescTLV(); if (tlv != null) { return new String(tlv.getValue(), StandardCharsets.UTF_8); } return null; }
public void setPortId(final int portNumber) { byte[] port = ArrayUtils.addAll(new byte[] {PORT_TLV_COMPONENT_SUBTYPE}, String.valueOf(portNumber).getBytes(StandardCharsets.UTF_8)); LLDPTLV portTLV = new LLDPTLV(); portTLV.setLength((short) port.length); portTLV.setType(PORT_TLV_TYPE); portTLV.setValue(port); this.setPortId(portTLV); }
public LLDPOrganizationalTLV getNameTLV() { for (LLDPTLV tlv : this.getOptionalTLVList()) { if (tlv.getType() == LLDPOrganizationalTLV.ORGANIZATIONAL_TLV_TYPE) { LLDPOrganizationalTLV orgTLV = (LLDPOrganizationalTLV) tlv; if (orgTLV.getSubType() == NAME_SUBTYPE) { return orgTLV; } } } return null; }
public Integer getPort() { ByteBuffer portBB = ByteBuffer.wrap(this.getPortId().getValue()); byte type = portBB.get(); if (type == PORT_TLV_COMPONENT_SUBTYPE) { return Integer.parseInt(new String(portBB.array(), portBB.position(), portBB.remaining(), StandardCharsets.UTF_8)); } else { return -1; } }
public void setChassisId(final ChassisId chassisId) { MacAddress chassisMac = MacAddress.valueOf(chassisId.value()); byte[] chassis = ArrayUtils.addAll(new byte[] {CHASSIS_TLV_SUBTYPE}, chassisMac.toBytes()); LLDPTLV chassisTLV = new LLDPTLV(); chassisTLV.setLength(CHASSIS_TLV_SIZE); chassisTLV.setType(CHASSIS_TLV_TYPE); chassisTLV.setValue(chassis); this.setChassisId(chassisTLV); }
public LLDPOrganizationalTLV getDeviceTLV() { for (LLDPTLV tlv : this.getOptionalTLVList()) { if (tlv.getType() == LLDPOrganizationalTLV.ORGANIZATIONAL_TLV_TYPE) { LLDPOrganizationalTLV orgTLV = (LLDPOrganizationalTLV) tlv; if (orgTLV.getSubType() == DEVICE_SUBTYPE) { return orgTLV; } } } return null; }
public short getTtlBySeconds() { ByteBuffer portBB = ByteBuffer.wrap(this.getTtl().getValue()); return portBB.getShort(); }
public ONOSLLDP(byte... subtype) { super(); for (byte st : subtype) { opttlvs.put(st, new LLDPOrganizationalTLV()); } // guarantee the following (name and device) TLVs exist opttlvs.putIfAbsent(NAME_SUBTYPE, new LLDPOrganizationalTLV()); opttlvs.putIfAbsent(DEVICE_SUBTYPE, new LLDPOrganizationalTLV()); setName(DEFAULT_NAME); setDevice(DEFAULT_DEVICE); setOptionalTLVList(Lists.newArrayList(opttlvs.values())); setTtl(new LLDPTLV().setType(TTL_TLV_TYPE) .setLength((short) ttlValue.length) .setValue(ttlValue)); }