@Test public void testReadFrom() throws Exception { header = new TlvHeader(); header.setTlvLength(8); header.setTlvType(9); linkTlv = new LinkTlv(header); channelBuffer = ChannelBuffers.copiedBuffer(packet1); linkTlv.readFrom(channelBuffer); assertThat(linkTlv, is(notNullValue())); header = new TlvHeader(); header.setTlvLength(8); header.setTlvType(1); linkTlv = new LinkTlv(header); channelBuffer = ChannelBuffers.copiedBuffer(packet2); linkTlv.readFrom(channelBuffer); assertThat(linkTlv, is(notNullValue())); header = new TlvHeader(); header.setTlvLength(8); header.setTlvType(2); linkTlv = new LinkTlv(header); channelBuffer = ChannelBuffers.copiedBuffer(packet3); linkTlv.readFrom(channelBuffer); assertThat(linkTlv, is(notNullValue())); header = new TlvHeader(); header.setTlvLength(8); header.setTlvType(3); linkTlv = new LinkTlv(header); channelBuffer = ChannelBuffers.copiedBuffer(packet4); linkTlv.readFrom(channelBuffer); assertThat(linkTlv, is(notNullValue())); header = new TlvHeader(); header.setTlvLength(8); header.setTlvType(4); linkTlv = new LinkTlv(header); channelBuffer = ChannelBuffers.copiedBuffer(packet5); linkTlv.readFrom(channelBuffer); assertThat(linkTlv, is(notNullValue())); header = new TlvHeader(); header.setTlvLength(8); header.setTlvType(5); linkTlv = new LinkTlv(header); channelBuffer = ChannelBuffers.copiedBuffer(packet1); linkTlv.readFrom(channelBuffer); assertThat(linkTlv, is(notNullValue())); header = new TlvHeader(); header.setTlvLength(8); header.setTlvType(6); linkTlv = new LinkTlv(header); channelBuffer = ChannelBuffers.copiedBuffer(packet6); linkTlv.readFrom(channelBuffer); assertThat(linkTlv, is(notNullValue())); header = new TlvHeader(); header.setTlvLength(8); header.setTlvType(7); linkTlv = new LinkTlv(header); channelBuffer = ChannelBuffers.copiedBuffer(packet7); linkTlv.readFrom(channelBuffer); assertThat(linkTlv, is(notNullValue())); header = new TlvHeader(); header.setTlvLength(8); header.setTlvType(8); linkTlv = new LinkTlv(header); channelBuffer = ChannelBuffers.copiedBuffer(packet8); linkTlv.readFrom(channelBuffer); assertThat(linkTlv, is(notNullValue())); assertThat(linkTlv, instanceOf(LinkTlv.class)); }
public void readFrom(ChannelBuffer channelBuffer) throws Exception { while (channelBuffer.readableBytes() > 0) { TlvHeader tlvHeader = new TlvHeader(); tlvHeader.setTlvType(channelBuffer.readUnsignedShort()); tlvHeader.setTlvLength(channelBuffer.readUnsignedShort()); if (LinkSubTypes.LINK_TYPE.value() == tlvHeader.tlvType()) { LinkType linktype = new LinkType(tlvHeader); linktype.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(linktype); if (tlvHeader.tlvLength() < OspfUtil.FOUR_BYTES) { int readerIndex = channelBuffer.readerIndex() + (OspfUtil.FOUR_BYTES - tlvHeader.tlvLength()); channelBuffer.readerIndex(readerIndex); } } else if (LinkSubTypes.LINK_ID.value() == tlvHeader.tlvType()) { LinkId linkId = new LinkId(tlvHeader); linkId.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(linkId); } else if (LinkSubTypes.LOCAL_INTERFACE_IP_ADDRESS.value() == tlvHeader.tlvType()) { LocalInterfaceIpAddress localInterfaceIpAddress = new LocalInterfaceIpAddress(tlvHeader); localInterfaceIpAddress.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(localInterfaceIpAddress); } else if (LinkSubTypes.REMOTE_INTERFACE_IP_ADDRESS.value() == tlvHeader.tlvType()) { RemoteInterfaceIpAddress remoteInterfaceIpAddress = new RemoteInterfaceIpAddress(tlvHeader); remoteInterfaceIpAddress.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(remoteInterfaceIpAddress); } else if (LinkSubTypes.TRAFFIC_ENGINEERING_METRIC.value() == tlvHeader.tlvType()) { TrafficEngineeringMetric trafficEngineeringMetric = new TrafficEngineeringMetric(tlvHeader); trafficEngineeringMetric.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(trafficEngineeringMetric); } else if (LinkSubTypes.MAXIMUM_BANDWIDTH.value() == tlvHeader.tlvType()) { MaximumBandwidth maximumBandwidth = new MaximumBandwidth(tlvHeader); maximumBandwidth.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(maximumBandwidth); } else if (LinkSubTypes.MAXIMUM_RESERVABLE_BANDWIDTH.value() == tlvHeader.tlvType()) { MaximumReservableBandwidth maximumReservableBandwidth = new MaximumReservableBandwidth(tlvHeader); maximumReservableBandwidth.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(maximumReservableBandwidth); } else if (LinkSubTypes.UNRESERVED_BANDWIDTH.value() == tlvHeader.tlvType()) { UnreservedBandwidth unreservedBandwidth = new UnreservedBandwidth(tlvHeader); unreservedBandwidth.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(unreservedBandwidth); } else if (LinkSubTypes.ADMINISTRATIVE_GROUP.value() == tlvHeader.tlvType()) { AdministrativeGroup administrativeGroup = new AdministrativeGroup(tlvHeader); administrativeGroup.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(administrativeGroup); } else { UnknownLinkSubType unknownLinkSubType = new UnknownLinkSubType(tlvHeader); unknownLinkSubType.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(unknownLinkSubType); } } }
LinkTlv extends TlvHeader implements TopLevelTlv { public void readFrom(ChannelBuffer channelBuffer) throws Exception { while (channelBuffer.readableBytes() > 0) { TlvHeader tlvHeader = new TlvHeader(); tlvHeader.setTlvType(channelBuffer.readUnsignedShort()); tlvHeader.setTlvLength(channelBuffer.readUnsignedShort()); if (LinkSubTypes.LINK_TYPE.value() == tlvHeader.tlvType()) { LinkType linktype = new LinkType(tlvHeader); linktype.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(linktype); if (tlvHeader.tlvLength() < OspfUtil.FOUR_BYTES) { int readerIndex = channelBuffer.readerIndex() + (OspfUtil.FOUR_BYTES - tlvHeader.tlvLength()); channelBuffer.readerIndex(readerIndex); } } else if (LinkSubTypes.LINK_ID.value() == tlvHeader.tlvType()) { LinkId linkId = new LinkId(tlvHeader); linkId.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(linkId); } else if (LinkSubTypes.LOCAL_INTERFACE_IP_ADDRESS.value() == tlvHeader.tlvType()) { LocalInterfaceIpAddress localInterfaceIpAddress = new LocalInterfaceIpAddress(tlvHeader); localInterfaceIpAddress.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(localInterfaceIpAddress); } else if (LinkSubTypes.REMOTE_INTERFACE_IP_ADDRESS.value() == tlvHeader.tlvType()) { RemoteInterfaceIpAddress remoteInterfaceIpAddress = new RemoteInterfaceIpAddress(tlvHeader); remoteInterfaceIpAddress.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(remoteInterfaceIpAddress); } else if (LinkSubTypes.TRAFFIC_ENGINEERING_METRIC.value() == tlvHeader.tlvType()) { TrafficEngineeringMetric trafficEngineeringMetric = new TrafficEngineeringMetric(tlvHeader); trafficEngineeringMetric.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(trafficEngineeringMetric); } else if (LinkSubTypes.MAXIMUM_BANDWIDTH.value() == tlvHeader.tlvType()) { MaximumBandwidth maximumBandwidth = new MaximumBandwidth(tlvHeader); maximumBandwidth.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(maximumBandwidth); } else if (LinkSubTypes.MAXIMUM_RESERVABLE_BANDWIDTH.value() == tlvHeader.tlvType()) { MaximumReservableBandwidth maximumReservableBandwidth = new MaximumReservableBandwidth(tlvHeader); maximumReservableBandwidth.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(maximumReservableBandwidth); } else if (LinkSubTypes.UNRESERVED_BANDWIDTH.value() == tlvHeader.tlvType()) { UnreservedBandwidth unreservedBandwidth = new UnreservedBandwidth(tlvHeader); unreservedBandwidth.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(unreservedBandwidth); } else if (LinkSubTypes.ADMINISTRATIVE_GROUP.value() == tlvHeader.tlvType()) { AdministrativeGroup administrativeGroup = new AdministrativeGroup(tlvHeader); administrativeGroup.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(administrativeGroup); } else { UnknownLinkSubType unknownLinkSubType = new UnknownLinkSubType(tlvHeader); unknownLinkSubType.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(unknownLinkSubType); } } } }
LinkTlv extends TlvHeader implements TopLevelTlv { public void readFrom(ChannelBuffer channelBuffer) throws Exception { while (channelBuffer.readableBytes() > 0) { TlvHeader tlvHeader = new TlvHeader(); tlvHeader.setTlvType(channelBuffer.readUnsignedShort()); tlvHeader.setTlvLength(channelBuffer.readUnsignedShort()); if (LinkSubTypes.LINK_TYPE.value() == tlvHeader.tlvType()) { LinkType linktype = new LinkType(tlvHeader); linktype.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(linktype); if (tlvHeader.tlvLength() < OspfUtil.FOUR_BYTES) { int readerIndex = channelBuffer.readerIndex() + (OspfUtil.FOUR_BYTES - tlvHeader.tlvLength()); channelBuffer.readerIndex(readerIndex); } } else if (LinkSubTypes.LINK_ID.value() == tlvHeader.tlvType()) { LinkId linkId = new LinkId(tlvHeader); linkId.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(linkId); } else if (LinkSubTypes.LOCAL_INTERFACE_IP_ADDRESS.value() == tlvHeader.tlvType()) { LocalInterfaceIpAddress localInterfaceIpAddress = new LocalInterfaceIpAddress(tlvHeader); localInterfaceIpAddress.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(localInterfaceIpAddress); } else if (LinkSubTypes.REMOTE_INTERFACE_IP_ADDRESS.value() == tlvHeader.tlvType()) { RemoteInterfaceIpAddress remoteInterfaceIpAddress = new RemoteInterfaceIpAddress(tlvHeader); remoteInterfaceIpAddress.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(remoteInterfaceIpAddress); } else if (LinkSubTypes.TRAFFIC_ENGINEERING_METRIC.value() == tlvHeader.tlvType()) { TrafficEngineeringMetric trafficEngineeringMetric = new TrafficEngineeringMetric(tlvHeader); trafficEngineeringMetric.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(trafficEngineeringMetric); } else if (LinkSubTypes.MAXIMUM_BANDWIDTH.value() == tlvHeader.tlvType()) { MaximumBandwidth maximumBandwidth = new MaximumBandwidth(tlvHeader); maximumBandwidth.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(maximumBandwidth); } else if (LinkSubTypes.MAXIMUM_RESERVABLE_BANDWIDTH.value() == tlvHeader.tlvType()) { MaximumReservableBandwidth maximumReservableBandwidth = new MaximumReservableBandwidth(tlvHeader); maximumReservableBandwidth.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(maximumReservableBandwidth); } else if (LinkSubTypes.UNRESERVED_BANDWIDTH.value() == tlvHeader.tlvType()) { UnreservedBandwidth unreservedBandwidth = new UnreservedBandwidth(tlvHeader); unreservedBandwidth.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(unreservedBandwidth); } else if (LinkSubTypes.ADMINISTRATIVE_GROUP.value() == tlvHeader.tlvType()) { AdministrativeGroup administrativeGroup = new AdministrativeGroup(tlvHeader); administrativeGroup.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(administrativeGroup); } else { UnknownLinkSubType unknownLinkSubType = new UnknownLinkSubType(tlvHeader); unknownLinkSubType.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(unknownLinkSubType); } } } LinkTlv(TlvHeader header); }
LinkTlv extends TlvHeader implements TopLevelTlv { public void readFrom(ChannelBuffer channelBuffer) throws Exception { while (channelBuffer.readableBytes() > 0) { TlvHeader tlvHeader = new TlvHeader(); tlvHeader.setTlvType(channelBuffer.readUnsignedShort()); tlvHeader.setTlvLength(channelBuffer.readUnsignedShort()); if (LinkSubTypes.LINK_TYPE.value() == tlvHeader.tlvType()) { LinkType linktype = new LinkType(tlvHeader); linktype.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(linktype); if (tlvHeader.tlvLength() < OspfUtil.FOUR_BYTES) { int readerIndex = channelBuffer.readerIndex() + (OspfUtil.FOUR_BYTES - tlvHeader.tlvLength()); channelBuffer.readerIndex(readerIndex); } } else if (LinkSubTypes.LINK_ID.value() == tlvHeader.tlvType()) { LinkId linkId = new LinkId(tlvHeader); linkId.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(linkId); } else if (LinkSubTypes.LOCAL_INTERFACE_IP_ADDRESS.value() == tlvHeader.tlvType()) { LocalInterfaceIpAddress localInterfaceIpAddress = new LocalInterfaceIpAddress(tlvHeader); localInterfaceIpAddress.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(localInterfaceIpAddress); } else if (LinkSubTypes.REMOTE_INTERFACE_IP_ADDRESS.value() == tlvHeader.tlvType()) { RemoteInterfaceIpAddress remoteInterfaceIpAddress = new RemoteInterfaceIpAddress(tlvHeader); remoteInterfaceIpAddress.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(remoteInterfaceIpAddress); } else if (LinkSubTypes.TRAFFIC_ENGINEERING_METRIC.value() == tlvHeader.tlvType()) { TrafficEngineeringMetric trafficEngineeringMetric = new TrafficEngineeringMetric(tlvHeader); trafficEngineeringMetric.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(trafficEngineeringMetric); } else if (LinkSubTypes.MAXIMUM_BANDWIDTH.value() == tlvHeader.tlvType()) { MaximumBandwidth maximumBandwidth = new MaximumBandwidth(tlvHeader); maximumBandwidth.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(maximumBandwidth); } else if (LinkSubTypes.MAXIMUM_RESERVABLE_BANDWIDTH.value() == tlvHeader.tlvType()) { MaximumReservableBandwidth maximumReservableBandwidth = new MaximumReservableBandwidth(tlvHeader); maximumReservableBandwidth.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(maximumReservableBandwidth); } else if (LinkSubTypes.UNRESERVED_BANDWIDTH.value() == tlvHeader.tlvType()) { UnreservedBandwidth unreservedBandwidth = new UnreservedBandwidth(tlvHeader); unreservedBandwidth.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(unreservedBandwidth); } else if (LinkSubTypes.ADMINISTRATIVE_GROUP.value() == tlvHeader.tlvType()) { AdministrativeGroup administrativeGroup = new AdministrativeGroup(tlvHeader); administrativeGroup.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(administrativeGroup); } else { UnknownLinkSubType unknownLinkSubType = new UnknownLinkSubType(tlvHeader); unknownLinkSubType.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(unknownLinkSubType); } } } LinkTlv(TlvHeader header); List<LinkSubType> subTlvList(); void readFrom(ChannelBuffer channelBuffer); byte[] asBytes(); byte[] getTlvBodyAsByteArray(); @Override String toString(); }
LinkTlv extends TlvHeader implements TopLevelTlv { public void readFrom(ChannelBuffer channelBuffer) throws Exception { while (channelBuffer.readableBytes() > 0) { TlvHeader tlvHeader = new TlvHeader(); tlvHeader.setTlvType(channelBuffer.readUnsignedShort()); tlvHeader.setTlvLength(channelBuffer.readUnsignedShort()); if (LinkSubTypes.LINK_TYPE.value() == tlvHeader.tlvType()) { LinkType linktype = new LinkType(tlvHeader); linktype.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(linktype); if (tlvHeader.tlvLength() < OspfUtil.FOUR_BYTES) { int readerIndex = channelBuffer.readerIndex() + (OspfUtil.FOUR_BYTES - tlvHeader.tlvLength()); channelBuffer.readerIndex(readerIndex); } } else if (LinkSubTypes.LINK_ID.value() == tlvHeader.tlvType()) { LinkId linkId = new LinkId(tlvHeader); linkId.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(linkId); } else if (LinkSubTypes.LOCAL_INTERFACE_IP_ADDRESS.value() == tlvHeader.tlvType()) { LocalInterfaceIpAddress localInterfaceIpAddress = new LocalInterfaceIpAddress(tlvHeader); localInterfaceIpAddress.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(localInterfaceIpAddress); } else if (LinkSubTypes.REMOTE_INTERFACE_IP_ADDRESS.value() == tlvHeader.tlvType()) { RemoteInterfaceIpAddress remoteInterfaceIpAddress = new RemoteInterfaceIpAddress(tlvHeader); remoteInterfaceIpAddress.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(remoteInterfaceIpAddress); } else if (LinkSubTypes.TRAFFIC_ENGINEERING_METRIC.value() == tlvHeader.tlvType()) { TrafficEngineeringMetric trafficEngineeringMetric = new TrafficEngineeringMetric(tlvHeader); trafficEngineeringMetric.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(trafficEngineeringMetric); } else if (LinkSubTypes.MAXIMUM_BANDWIDTH.value() == tlvHeader.tlvType()) { MaximumBandwidth maximumBandwidth = new MaximumBandwidth(tlvHeader); maximumBandwidth.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(maximumBandwidth); } else if (LinkSubTypes.MAXIMUM_RESERVABLE_BANDWIDTH.value() == tlvHeader.tlvType()) { MaximumReservableBandwidth maximumReservableBandwidth = new MaximumReservableBandwidth(tlvHeader); maximumReservableBandwidth.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(maximumReservableBandwidth); } else if (LinkSubTypes.UNRESERVED_BANDWIDTH.value() == tlvHeader.tlvType()) { UnreservedBandwidth unreservedBandwidth = new UnreservedBandwidth(tlvHeader); unreservedBandwidth.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(unreservedBandwidth); } else if (LinkSubTypes.ADMINISTRATIVE_GROUP.value() == tlvHeader.tlvType()) { AdministrativeGroup administrativeGroup = new AdministrativeGroup(tlvHeader); administrativeGroup.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(administrativeGroup); } else { UnknownLinkSubType unknownLinkSubType = new UnknownLinkSubType(tlvHeader); unknownLinkSubType.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength())); subTlv.add(unknownLinkSubType); } } } LinkTlv(TlvHeader header); List<LinkSubType> subTlvList(); void readFrom(ChannelBuffer channelBuffer); byte[] asBytes(); byte[] getTlvBodyAsByteArray(); @Override String toString();  }