sunishvk
Committed by Gerrit Code Review

ONOS-4083,ONOS-4084:ISIS PDU TLV Data Structures

Change-Id: Ice0d42f2c886fa5f398b562e126614ed45f858a2
Showing 40 changed files with 3378 additions and 288 deletions
......@@ -37,7 +37,7 @@
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-buffer</artifactId>
<artifactId>netty</artifactId>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.isis.io.isispacket.tlv;
import com.google.common.base.MoreObjects;
import com.google.common.primitives.Bytes;
import org.jboss.netty.buffer.ChannelBuffer;
import org.onosproject.isis.io.util.IsisUtil;
import java.util.ArrayList;
import java.util.List;
/**
* Representation of adjacency state TLV of P2P neighbor.
*/
public class AdjacencyStateTlv extends TlvHeader implements IsisTlv {
private byte adjacencyType;
private int localCircuitId;
private String neighborSystemId;
private int neighborLocalCircuitId;
/**
* Creates an instance of adjacency state TLV..
*
* @param tlvHeader tlvHeader
*/
public AdjacencyStateTlv(TlvHeader tlvHeader) {
this.setTlvType(tlvHeader.tlvType());
this.setTlvLength(tlvHeader.tlvLength());
}
/**
* Returns local circuit ID for adjacency state TLV.
*
* @return local circuit ID
*/
public int localCircuitId() {
return localCircuitId;
}
/**
* Sets local circuit ID for adjacency state TLV.
*
* @param localCircuitId local circuit Id
*/
public void setLocalCircuitId(int localCircuitId) {
this.localCircuitId = localCircuitId;
}
/**
* Returns neighbor system ID for adjacency state TLV.
*
* @return neighbor system ID
*/
public String neighborSystemId() {
return neighborSystemId;
}
/**
* Sets neighbor system ID for adjacency state TLV.
*
* @param neighborSystemId neighbor system ID
*/
public void setNeighborSystemId(String neighborSystemId) {
this.neighborSystemId = neighborSystemId;
}
/**
* Returns neighbor local circuit ID for adjacency state TLV.
*
* @return neighbor local circuit ID
*/
public int neighborLocalCircuitId() {
return neighborLocalCircuitId;
}
/**
* Sets neighbor local circuit ID for adjacency state TLV.
*
* @param neighborLocalCircuitId neighbor local circuit ID
*/
public void setNeighborLocalCircuitId(int neighborLocalCircuitId) {
this.neighborLocalCircuitId = neighborLocalCircuitId;
}
/**
* Returns adjacency type of adjacency state TLV.
*
* @return adjacency type
*/
public byte adjacencyType() {
return adjacencyType;
}
/**
* Sets adjacency type for adjacency state TLV.
*
* @param adjacencyType adjacency type
*/
public void setAdjacencyType(byte adjacencyType) {
this.adjacencyType = adjacencyType;
}
@Override
public void readFrom(ChannelBuffer channelBuffer) {
this.setAdjacencyType(channelBuffer.readByte());
if (channelBuffer.readableBytes() > 0) {
this.setLocalCircuitId(channelBuffer.readInt());
byte[] tempByteArray = new byte[IsisUtil.ID_SIX_BYTES];
channelBuffer.readBytes(tempByteArray, 0, IsisUtil.ID_SIX_BYTES);
this.setNeighborSystemId(IsisUtil.systemId(tempByteArray));
this.setNeighborLocalCircuitId(channelBuffer.readInt());
}
}
@Override
public byte[] asBytes() {
byte[] bytes = null;
byte[] tlvHeader = tlvHeaderAsByteArray();
byte[] tlvBody = tlvBodyAsBytes();
tlvHeader[1] = (byte) tlvBody.length;
bytes = Bytes.concat(tlvHeader, tlvBody);
return bytes;
}
/**
* Returns adjacency type TLV body as byte array.
*
* @return byteArray TLV body of area address TLV
*/
private byte[] tlvBodyAsBytes() {
List<Byte> bytes = new ArrayList<>();
bytes.add(this.adjacencyType);
bytes.addAll(Bytes.asList(IsisUtil.convertToFourBytes(this.localCircuitId)));
bytes.addAll(IsisUtil.sourceAndLanIdToBytes(this.neighborSystemId));
bytes.addAll(Bytes.asList(IsisUtil.convertToFourBytes(this.neighborLocalCircuitId)));
return Bytes.toArray(bytes);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.omitNullValues()
.add("adjacencyType", adjacencyType)
.add("localCircuitId", localCircuitId)
.add("neighborSystemId", neighborSystemId)
.add("neighborLocalCircuitId", neighborLocalCircuitId)
.toString();
}
}
......@@ -13,52 +13,58 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.isis.io.isispacket.tlv;
import com.google.common.base.MoreObjects;
import com.google.common.primitives.Bytes;
import io.netty.buffer.ByteBuf;
import org.jboss.netty.buffer.ChannelBuffer;
import org.onosproject.isis.io.util.IsisUtil;
import java.util.ArrayList;
import java.util.List;
/**
* Represents the area address TLV.
* Representation of area address TLV.
*/
public class AreaAddressTlv extends TlvHeader implements IsisTlv {
private List<String> areaAddress = new ArrayList();
private List<String> areaAddress = new ArrayList<>();
/**
* Sets TLV type and TLV length of area address TLV.
* Creates an instance of area address TLV.
*
* @param tlvHeader tlvHeader.
* @param tlvHeader tlvHeader
*/
public AreaAddressTlv(TlvHeader tlvHeader) {
this.setTlvType(tlvHeader.tlvType());
this.setTlvLength(tlvHeader.tlvLength());
}
/**
* Adds the area address to the area address TLV.
*
* @param areaAddress area address
*/
public void addAddress(String areaAddress) {
this.areaAddress.add(areaAddress);
}
/**
* Gets the area address of area address TLV.
* Returns the area address of area address TLV.
*
* @return area address
* @return areaAddress area address
*/
public List<String> areaAddress() {
return this.areaAddress;
}
@Override
public void readFrom(ByteBuf byteBuf) {
while (byteBuf.readableBytes() > 0) {
int addressLength = byteBuf.readByte();
byte[] addressBytes = new byte[IsisUtil.THREE_BYTES];
byteBuf.readBytes(addressBytes, 0, IsisUtil.THREE_BYTES);
String areaAddress = IsisUtil.areaAddres(addressBytes);
public void readFrom(ChannelBuffer channelBuffer) {
while (channelBuffer.readableBytes() > 0) {
int addressLength = channelBuffer.readByte();
byte[] addressbytes = new byte[addressLength];
channelBuffer.readBytes(addressbytes, 0, addressLength);
String areaAddress = IsisUtil.areaAddres(addressbytes);
this.areaAddress.add(areaAddress);
}
}
......@@ -66,38 +72,32 @@ public class AreaAddressTlv extends TlvHeader implements IsisTlv {
@Override
public byte[] asBytes() {
byte[] bytes = null;
byte[] tlvHeader = tlvHeaderAsByteArray();
byte[] tlvBody = tlvBodyAsBytes();
tlvHeader[1] = (byte) tlvBody.length;
bytes = Bytes.concat(tlvHeader, tlvBody);
return bytes;
}
/**
* Gets TLV body of area address TLV.
* Returns TLV body of area address TLV.
*
* @return byteArray TLV body of area address TLV
*/
public byte[] tlvBodyAsBytes() {
List<Byte> bytes = new ArrayList();
private byte[] tlvBodyAsBytes() {
List<Byte> bytes = new ArrayList<>();
for (String areaAddress : this.areaAddress) {
bytes.add((byte) (areaAddress.length() / 2));
bytes.addAll(IsisUtil.areaAddresToBytes(areaAddress));
}
byte[] byteArray = new byte[bytes.size()];
int i = 0;
for (byte byt : bytes) {
byteArray[i++] = byt;
bytes.addAll(IsisUtil.areaAddressToBytes(areaAddress));
}
return byteArray;
return Bytes.toArray(bytes);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.omitNullValues()
.add("areaAddress", areaAddress)
.toString();
}
}
\ No newline at end of file
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.isis.io.isispacket.tlv;
import com.google.common.base.MoreObjects;
import com.google.common.primitives.Bytes;
import org.jboss.netty.buffer.ChannelBuffer;
/**
* Representation of host name TLV.
*/
public class HostNameTlv extends TlvHeader {
private String hostName;
/**
* Creates an instance of host name TLV.
*
* @param tlvHeader tlvHeader.
*/
public HostNameTlv(TlvHeader tlvHeader) {
this.setTlvType(tlvHeader.tlvType());
this.setTlvLength(tlvHeader.tlvLength());
}
/**
* Returns host name of host name TLV.
*
* @return host name
*/
public String hostName() {
return hostName;
}
/**
* Sets host name for host name TLV.
*
* @param hostName host name.
*/
public void setHostName(String hostName) {
this.hostName = hostName;
}
@Override
public void readFrom(ChannelBuffer channelBuffer) {
byte[] addressbytes = new byte[this.tlvLength()];
channelBuffer.readBytes(addressbytes, 0, this.tlvLength());
this.hostName = new String(addressbytes);
}
@Override
public byte[] asBytes() {
byte[] bytes = null;
byte[] tlvHeader = tlvHeaderAsByteArray();
byte[] tlvBody = tlvBodyAsBytes();
tlvHeader[1] = (byte) tlvBody.length;
bytes = Bytes.concat(tlvHeader, tlvBody);
return bytes;
}
/**
* Returns TLV body of host name TLV.
*
* @return byteArray TLV body of host name TLV
*/
private byte[] tlvBodyAsBytes() {
byte[] bytes = this.hostName.getBytes();
return bytes;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.omitNullValues()
.add("hostName", hostName)
.toString();
}
}
\ No newline at end of file
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.isis.io.isispacket.tlv;
import com.google.common.base.MoreObjects;
import org.jboss.netty.buffer.ChannelBuffer;
/**
* Representation of IDRP information TLV.
*/
public class IdrpInformationTlv extends TlvHeader implements IsisTlv {
private byte irdpInformationType;
private int externalInformation;
/**
* Creates an instance of IDRP information TLV.
*
* @param tlvHeader tlvHeader
*/
public IdrpInformationTlv(TlvHeader tlvHeader) {
this.setTlvType(tlvHeader.tlvType());
this.setTlvLength(tlvHeader.tlvLength());
}
/**
* Returns the external information of IDRP information TLV.
*
* @return external information
*/
public int externalInformation() {
return externalInformation;
}
/**
* Sets the external information for IDRP information TLV.
*
* @param externalInformation external information
*/
public void setExternalInformation(int externalInformation) {
this.externalInformation = externalInformation;
}
/**
* Returns the IDRP information of IDRP information TLV.
*
* @return IDRP information type
*/
public byte irdpInformationType() {
return irdpInformationType;
}
/**
* Sets the IDRP information for IDRP information TLV.
*
* @param irdpInformationType IDRP information type
*/
public void setIrdpInformationType(byte irdpInformationType) {
this.irdpInformationType = irdpInformationType;
}
@Override
public void readFrom(ChannelBuffer channelBuffer) {
//TODO
}
@Override
public byte[] asBytes() {
//TODO
return null;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.omitNullValues()
.add("externalInformation", externalInformation)
.add("irdpInformationType", irdpInformationType)
.toString();
}
}
\ No newline at end of file
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.isis.io.isispacket.tlv;
import com.google.common.base.MoreObjects;
import com.google.common.primitives.Bytes;
import org.jboss.netty.buffer.ChannelBuffer;
import org.onosproject.isis.io.isispacket.tlv.subtlv.SubTlvFinder;
import org.onosproject.isis.io.isispacket.tlv.subtlv.SubTlvToBytes;
import org.onosproject.isis.io.isispacket.tlv.subtlv.TrafficEngineeringSubTlv;
import org.onosproject.isis.io.util.IsisUtil;
import java.util.ArrayList;
import java.util.List;
/**
* Representation of IP extended reachability TLV.
*/
public class IpExtendedReachabilityTlv extends TlvHeader implements IsisTlv {
private String sysIdAndPseudoNumber;
private int defaultMetric;
private byte subTlvLength;
private List<TrafficEngineeringSubTlv> trafEnginSubTlv = new ArrayList<>();
/**
* Creates an instance of IP external reachability TLV.
*
* @param tlvHeader tlvHeader
*/
public IpExtendedReachabilityTlv(TlvHeader tlvHeader) {
this.setTlvType(tlvHeader.tlvType());
this.setTlvLength(tlvHeader.tlvLength());
}
/**
* Returns the system ID and pseudo number of IP external reachability TLV.
*
* @return sysIdAndPseudoNumber system ID and pseudo number
*/
public String sysIdAndPseudoNumber() {
return sysIdAndPseudoNumber;
}
/**
* Sets the system ID and pseudo number for IP external reachability TLV.
*
* @param sysIdAndPseudoNumber system ID and pseudo number
*/
public void setSysIdAndPseudoNumber(String sysIdAndPseudoNumber) {
this.sysIdAndPseudoNumber = sysIdAndPseudoNumber;
}
/**
* Adds the traffic engineering sub TLV to IP external reachability TLV.
*
* @param trafEnginSubTlv traffic engineering sub TLV
*/
public void addSubTlv(TrafficEngineeringSubTlv trafEnginSubTlv) {
this.trafEnginSubTlv.add(trafEnginSubTlv);
}
/**
* Returns the sub TLV length of IP external reachability TLV.
*
* @return sub TLV length
*/
public byte subTlvLength() {
return subTlvLength;
}
/**
* Sets the sub TLV length for IP external reachability TLV.
*
* @param subTlvLength sub TLV length
*/
public void setSubTlvLength(byte subTlvLength) {
this.subTlvLength = subTlvLength;
}
/**
* Returns default metric of IP external reachability TLV.
*
* @return default metric
*/
public int defaultMetric() {
return defaultMetric;
}
/**
* Sets default metric for IP external reachability TLV.
*
* @param defaultMetric default metric
*/
public void setDefaultMetric(int defaultMetric) {
this.defaultMetric = defaultMetric;
}
@Override
public void readFrom(ChannelBuffer channelBuffer) {
byte[] tempByteArray = new byte[IsisUtil.ID_PLUS_ONE_BYTE];
channelBuffer.readBytes(tempByteArray, 0, IsisUtil.ID_PLUS_ONE_BYTE);
this.setSysIdAndPseudoNumber(IsisUtil.systemIdPlus(tempByteArray));
this.setDefaultMetric(channelBuffer.readUnsignedMedium());
this.setSubTlvLength((byte) channelBuffer.readByte());
while (channelBuffer.readableBytes() > 0) {
TlvHeader tlvHeader = new TlvHeader();
tlvHeader.setTlvType(channelBuffer.readByte());
tlvHeader.setTlvLength(channelBuffer.readByte());
this.addSubTlv(SubTlvFinder.findSubTlv(tlvHeader,
channelBuffer.readBytes(tlvHeader.tlvLength())));
}
}
@Override
public byte[] asBytes() {
byte[] bytes = null;
byte[] tlvHeader = tlvHeaderAsByteArray();
byte[] tlvBody = tlvBodyAsBytes();
//systemID + pseudo number+length of subtlv=11l
tlvBody[10] = (byte) (tlvBody.length - 11);
tlvHeader[1] = (byte) tlvBody.length;
bytes = Bytes.concat(tlvHeader, tlvBody);
return bytes;
}
/**
* Returns TLV body of IP external reachability TLV.
*
* @return byteArray TLV body of IP external reachability TLV.
*/
private byte[] tlvBodyAsBytes() {
List<Byte> bodyLst = new ArrayList<>();
bodyLst.addAll(IsisUtil.sourceAndLanIdToBytes(this.sysIdAndPseudoNumber()));
bodyLst.addAll(Bytes.asList(IsisUtil.convertToThreeBytes(this.defaultMetric())));
bodyLst.add(this.subTlvLength());
for (TrafficEngineeringSubTlv trafficEngineeringSubTlv : this.trafEnginSubTlv) {
bodyLst.addAll(SubTlvToBytes.tlvToBytes(trafficEngineeringSubTlv));
}
return Bytes.toArray(bodyLst);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.omitNullValues()
.add("sysIdAndPseudoNumber", sysIdAndPseudoNumber)
.add("defaultMetric", defaultMetric)
.add("subTlvLength", subTlvLength)
.add("trafEnginSubTlv", trafEnginSubTlv)
.toString();
}
}
\ No newline at end of file
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.isis.io.isispacket.tlv;
import com.google.common.base.MoreObjects;
import com.google.common.primitives.Bytes;
import org.jboss.netty.buffer.ChannelBuffer;
import org.onosproject.isis.io.isispacket.tlv.subtlv.SubTlvFinder;
import org.onosproject.isis.io.isispacket.tlv.subtlv.SubTlvToBytes;
import org.onosproject.isis.io.isispacket.tlv.subtlv.TrafficEngineeringSubTlv;
import org.onosproject.isis.io.util.IsisUtil;
import java.util.ArrayList;
import java.util.List;
/**
* Representation of IP external reachability TLV.
*/
public class IpExternalReachabilityTlv extends TlvHeader implements IsisTlv {
private String sysIdAndPseudoNumber;
private int defaultMetric;
private byte subTlvLength;
private List<TrafficEngineeringSubTlv> trafEnginSubTlv = new ArrayList<>();
/**
* Sets TLV type and TLV length for IP external reachability TLV.
*
* @param tlvHeader tlvHeader
*/
public IpExternalReachabilityTlv(TlvHeader tlvHeader) {
this.setTlvType(tlvHeader.tlvType());
this.setTlvLength(tlvHeader.tlvLength());
}
/**
* Gets the system ID and pseudo number of IP external reachability TLV.
*
* @return sysIdAndPseudoNumber system ID and pseudo number
*/
public String sysIdAndPseudoNumber() {
return sysIdAndPseudoNumber;
}
/**
* Gets the system ID and pseudo number for IP external reachability TLV.
*
* @param sysIdAndPseudoNumber system ID and pseudo number
*/
public void setSysIdAndPseudoNumber(String sysIdAndPseudoNumber) {
this.sysIdAndPseudoNumber = sysIdAndPseudoNumber;
}
/**
* Adds the traffic engineering sub TLV to IP external reachability TLV.
*
* @param trafEnginSubTlv traffic engineering sub TLV
*/
public void addSubTlv(TrafficEngineeringSubTlv trafEnginSubTlv) {
this.trafEnginSubTlv.add(trafEnginSubTlv);
}
/**
* Gets the sub TLV length of IP external reachability TLV.
*
* @return sub TLV length
*/
public byte subTlvLength() {
return subTlvLength;
}
/**
* Sets the sub TLV length for IP external reachability TLV.
*
* @param subTlvLength sub TLV length
*/
public void setSubTlvLength(byte subTlvLength) {
this.subTlvLength = subTlvLength;
}
/**
* Gets default metric of IP external reachability TLV.
*
* @return default metric
*/
public int defaultMetric() {
return defaultMetric;
}
/**
* Sets default metric for IP external reachability TLV.
*
* @param defaultMetric default metric
*/
public void setDefaultMetric(int defaultMetric) {
this.defaultMetric = defaultMetric;
}
@Override
public void readFrom(ChannelBuffer channelBuffer) {
byte[] tempByteArray = new byte[IsisUtil.ID_PLUS_ONE_BYTE];
channelBuffer.readBytes(tempByteArray, 0, IsisUtil.ID_PLUS_ONE_BYTE);
this.setSysIdAndPseudoNumber(IsisUtil.systemIdPlus(tempByteArray));
this.setDefaultMetric(channelBuffer.readUnsignedMedium());
this.setSubTlvLength((byte) channelBuffer.readByte());
while (channelBuffer.readableBytes() > 0) {
TlvHeader tlvHeader = new TlvHeader();
tlvHeader.setTlvType(channelBuffer.readByte());
tlvHeader.setTlvLength(channelBuffer.readByte());
this.addSubTlv(SubTlvFinder.findSubTlv(tlvHeader,
channelBuffer.readBytes(tlvHeader.tlvLength())));
}
}
@Override
public byte[] asBytes() {
byte[] bytes = null;
byte[] tlvHeader = tlvHeaderAsByteArray();
byte[] tlvBody = tlvBodyAsBytes();
//systemID + pseudo number+length of subtlv=11l
tlvBody[10] = (byte) (tlvBody.length - 11);
tlvHeader[1] = (byte) tlvBody.length;
bytes = Bytes.concat(tlvHeader, tlvBody);
return bytes;
}
/**
* Gets TLV body of IP external reachability TLV.
*
* @return byteArray TLV body of IP external reachability TLV.
*/
public byte[] tlvBodyAsBytes() {
List<Byte> bodyLst = new ArrayList<>();
bodyLst.addAll(IsisUtil.sourceAndLanIdToBytes(this.sysIdAndPseudoNumber()));
bodyLst.addAll(Bytes.asList(IsisUtil.convertToThreeBytes(this.defaultMetric())));
bodyLst.add(this.subTlvLength());
for (TrafficEngineeringSubTlv trafficEngineeringSubTlv : this.trafEnginSubTlv) {
bodyLst.addAll(SubTlvToBytes.tlvToBytes(trafficEngineeringSubTlv));
}
return Bytes.toArray(bodyLst);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.omitNullValues()
.add("sysIdAndPseudoNumber", sysIdAndPseudoNumber)
.add("defaultMetric", defaultMetric)
.add("subTlvLength", subTlvLength)
.toString();
}
}
\ No newline at end of file
......@@ -17,7 +17,7 @@ package org.onosproject.isis.io.isispacket.tlv;
import com.google.common.base.MoreObjects;
import com.google.common.primitives.Bytes;
import io.netty.buffer.ByteBuf;
import org.jboss.netty.buffer.ChannelBuffer;
import org.onlab.packet.Ip4Address;
import org.onosproject.isis.io.util.IsisUtil;
......@@ -25,13 +25,14 @@ import java.util.ArrayList;
import java.util.List;
/**
* Represents IP interface address TLV.
* Representation of IP interface address TLV.
*/
public class IpInterfaceAddressTlv extends TlvHeader implements IsisTlv {
private List<Ip4Address> interfaceAddress = new ArrayList();
private List<Ip4Address> interfaceAddress = new ArrayList<>();
/**
* Sets TLV type and TLV length of IP interface address TLV.
* Creates an instance of IP interface address TLV.
*
* @param tlvHeader tlvHeader.
*/
......@@ -43,58 +44,60 @@ public class IpInterfaceAddressTlv extends TlvHeader implements IsisTlv {
}
/**
* Gets interface address of interface address TLV.
* Adds the interface address to IP interface address TLV.
*
* @param interfaceAddress interface address
*/
public void addInterfaceAddres(Ip4Address interfaceAddress) {
this.interfaceAddress.add(interfaceAddress);
}
/**
* Returns the interface address of IP interface address TLV.
*
* @return interfaceAddress interface address
* @return interface address
*/
public List<Ip4Address> interfaceAddress() {
return interfaceAddress;
}
@Override
public void readFrom(ByteBuf byteBuf) {
while (byteBuf.readableBytes() >= 4) {
public void readFrom(ChannelBuffer channelBuffer) {
while (channelBuffer.readableBytes() >= 4) {
byte[] addressbytes = new byte[IsisUtil.FOUR_BYTES];
byteBuf.readBytes(addressbytes, 0, IsisUtil.FOUR_BYTES);
channelBuffer.readBytes(addressbytes, 0, IsisUtil.FOUR_BYTES);
this.interfaceAddress.add(Ip4Address.valueOf(addressbytes));
}
}
@Override
public byte[] asBytes() {
byte[] bytes = null;
byte[] tlvHeader = tlvHeaderAsByteArray();
byte[] tlvBody = tlvBodyAsBytes();
tlvHeader[1] = (byte) tlvBody.length;
bytes = Bytes.concat(tlvHeader, tlvBody);
return bytes;
}
/**
* Gets TLV body of interface address TLV.
* Returns TLV body of IP interface address TLV.
*
* @return byteArray TLV body of interface address TLV.
* @return byteArray TLV body of IP interface address TLV
*/
public byte[] tlvBodyAsBytes() {
List<Byte> bytes = new ArrayList();
private byte[] tlvBodyAsBytes() {
List<Byte> bytes = new ArrayList<>();
for (Ip4Address ip4Address : this.interfaceAddress) {
bytes.addAll(Bytes.asList(ip4Address.toOctets()));
}
byte[] byteArray = new byte[bytes.size()];
int i = 0;
for (byte byt : bytes) {
byteArray[i++] = byt;
}
return byteArray;
return Bytes.toArray(bytes);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.omitNullValues()
.add("interfaceAddress", interfaceAddress)
.toString();
}
}
\ No newline at end of file
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.isis.io.isispacket.tlv;
import com.google.common.base.MoreObjects;
import com.google.common.primitives.Bytes;
import org.jboss.netty.buffer.ChannelBuffer;
import java.util.ArrayList;
import java.util.List;
/**
* Representation of IP internal reachability TLV.
*/
public class IpInternalReachabilityTlv extends TlvHeader implements IsisTlv {
private List<MetricOfInternalReachability> metricOfInternalReachability = new ArrayList<>();
/**
* Creates an instance of IP internal reachability TLV.
*
* @param tlvHeader tlvHeader.
*/
public IpInternalReachabilityTlv(TlvHeader tlvHeader) {
this.setTlvType(tlvHeader.tlvType());
this.setTlvLength(tlvHeader.tlvLength());
}
/**
* Adds the metric of internal reachability to internal reachability TLV.
*
* @param metricValue metric of internal reachability
*/
public void addInternalReachabilityMetric(MetricOfInternalReachability metricValue) {
this.metricOfInternalReachability.add(metricValue);
}
@Override
public void readFrom(ChannelBuffer channelBuffer) {
while (channelBuffer.readableBytes() > 0) {
MetricOfInternalReachability metricOfInternalReachability = new MetricOfInternalReachability();
metricOfInternalReachability.readFrom(channelBuffer);
this.metricOfInternalReachability.add(metricOfInternalReachability);
}
}
@Override
public byte[] asBytes() {
byte[] bytes = null;
byte[] tlvHeader = tlvHeaderAsByteArray();
byte[] tlvBody = tlvBodyAsBytes();
tlvHeader[1] = (byte) tlvBody.length;
bytes = Bytes.concat(tlvHeader, tlvBody);
return bytes;
}
/**
* Returns TLV body of internal reachability TLV.
*
* @return byteArray TLV body of area address TLV
*/
private byte[] tlvBodyAsBytes() {
List<Byte> bytes = new ArrayList<>();
for (MetricOfInternalReachability metricOfInternalReachability :
this.metricOfInternalReachability) {
bytes.addAll(Bytes.asList(metricOfInternalReachability.asBytes()));
}
return Bytes.toArray(bytes);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.omitNullValues()
.add("metricOfInternalReachability", metricOfInternalReachability)
.toString();
}
}
\ No newline at end of file
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.isis.io.isispacket.tlv;
import com.google.common.base.MoreObjects;
import com.google.common.primitives.Bytes;
import org.jboss.netty.buffer.ChannelBuffer;
import java.util.ArrayList;
import java.util.List;
/**
* Representation of IS reachability TLV.
*/
public class IsReachabilityTlv extends TlvHeader {
private int reserved;
private List<MetricsOfReachability> metricsOfReachabilities = new ArrayList<>();
/**
* Creates an instance of IS reachability TLV.
*
* @param tlvHeader tlvHeader.
*/
public IsReachabilityTlv(TlvHeader tlvHeader) {
this.setTlvType(tlvHeader.tlvType());
this.setTlvLength(tlvHeader.tlvLength());
}
/**
* Returns the reserved value of IS reachability TLV.
*
* @return reserved
*/
public int reserved() {
return reserved;
}
/**
* Sets the reserved value for IS reachability TLV.
*
* @param reserved reserved
*/
public void setReserved(int reserved) {
this.reserved = reserved;
}
/**
* Adds the metric of reachability to IS reachability TLV..
*
* @param metricsOfReachability metric of reachability
*/
public void addMeticsOfReachability(MetricsOfReachability metricsOfReachability) {
this.metricsOfReachabilities.add(metricsOfReachability);
}
@Override
public void readFrom(ChannelBuffer channelBuffer) {
this.setReserved(channelBuffer.readByte());
while (channelBuffer.readableBytes() > 0) {
MetricsOfReachability metricsOfReachability = new MetricsOfReachability();
metricsOfReachability.readFrom(channelBuffer);
this.metricsOfReachabilities.add(metricsOfReachability);
}
}
@Override
public byte[] asBytes() {
byte[] bytes = null;
byte[] tlvHeader = tlvHeaderAsByteArray();
byte[] tlvBody = tlvBodyAsBytes();
tlvHeader[1] = (byte) tlvBody.length;
bytes = Bytes.concat(tlvHeader, tlvBody);
return bytes;
}
/**
* Returns TLV body of IS reachability TLV.
*
* @return byteArray TLV body of area address TLV
*/
private byte[] tlvBodyAsBytes() {
List<Byte> bytes = new ArrayList<>();
bytes.add((byte) this.reserved());
for (MetricsOfReachability metricsOfReachability : this.metricsOfReachabilities) {
bytes.addAll(Bytes.asList(metricsOfReachability.asBytes()));
}
return Bytes.toArray(bytes);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.omitNullValues()
.add("metricsOfReachabilities", metricsOfReachabilities)
.toString();
}
}
......@@ -17,7 +17,7 @@ package org.onosproject.isis.io.isispacket.tlv;
import com.google.common.base.MoreObjects;
import com.google.common.primitives.Bytes;
import io.netty.buffer.ByteBuf;
import org.jboss.netty.buffer.ChannelBuffer;
import org.onlab.packet.MacAddress;
import org.onosproject.isis.io.util.IsisUtil;
......@@ -25,78 +25,77 @@ import java.util.ArrayList;
import java.util.List;
/**
* Represents ISIS neighbor TLV.
* Representation of ISIS neighbor TLV.
*/
public class IsisNeighborTlv extends TlvHeader implements IsisTlv {
private List<MacAddress> neighbor = new ArrayList();
private List<MacAddress> neighbor = new ArrayList<>();
/**
* Sets TLV type and TLV length of ISIS neighbor TLV.
* Creates an instance of ISIS neighbor TLV.
*
* @param tlvHeader tlvHeader.
* @param tlvHeader tlvHeader
*/
public IsisNeighborTlv(TlvHeader tlvHeader) {
this.setTlvType(tlvHeader.tlvType());
this.setTlvLength(tlvHeader.tlvLength());
}
/**
* Adds the MAC address of the neighbor to ISIS neighbor TLV.
*
* @param macAddress MAC address
*/
public void addNeighbor(MacAddress macAddress) {
neighbor.add(macAddress);
}
/**
* Gets the MAC address of the neighbor TLV.
* Returns the MAC address of the ISIS neighbor TLV.
*
* @return neighbor MAC address of the neighbor TLV
* @return neighbor
*/
public List<MacAddress> neighbor() {
return this.neighbor;
}
@Override
public void readFrom(ByteBuf byteBuf) {
while (byteBuf.readableBytes() >= 6) {
public void readFrom(ChannelBuffer channelBuffer) {
while (channelBuffer.readableBytes() >= 6) {
byte[] addressbytes = new byte[IsisUtil.SIX_BYTES];
byteBuf.readBytes(addressbytes, 0, IsisUtil.SIX_BYTES);
channelBuffer.readBytes(addressbytes, 0, IsisUtil.SIX_BYTES);
this.neighbor.add(MacAddress.valueOf(addressbytes));
}
}
@Override
public byte[] asBytes() {
byte[] bytes = null;
byte[] tlvHeader = tlvHeaderAsByteArray();
byte[] tlvBody = tlvBodyAsBytes();
tlvHeader[1] = (byte) tlvBody.length;
bytes = Bytes.concat(tlvHeader, tlvBody);
return bytes;
}
/**
* Gets TLV body of neighbor TLV.
* Returns TLV body of ISIS neighbor TLV.
*
* @return byteArray TLV body of neighbor TLV
* @return byteArray TLV body of area address TLV
*/
public byte[] tlvBodyAsBytes() {
List<Byte> bytes = new ArrayList();
private byte[] tlvBodyAsBytes() {
List<Byte> bytes = new ArrayList<>();
for (MacAddress macAddress : this.neighbor) {
bytes.addAll(Bytes.asList(macAddress.toBytes()));
}
byte[] byteArray = new byte[bytes.size()];
int i = 0;
for (byte byt : bytes) {
byteArray[i++] = byt;
}
return byteArray;
return Bytes.toArray(bytes);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.omitNullValues()
.add("neighbor", neighbor)
.toString();
}
}
\ No newline at end of file
......
......@@ -16,7 +16,7 @@
package org.onosproject.isis.io.isispacket.tlv;
/**
* Represents ISIS TLV.
* Representation of ISIS TLV.
*/
public interface IsisTlv {
}
\ No newline at end of file
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.isis.io.isispacket.tlv;
import com.google.common.base.MoreObjects;
import com.google.common.primitives.Bytes;
import org.jboss.netty.buffer.ChannelBuffer;
import java.util.ArrayList;
import java.util.List;
/**
* Representation of LSP entries TLV.
*/
public class LspEntriesTlv extends TlvHeader implements IsisTlv {
private List<LspEntry> lspEntryList = new ArrayList<>();
/**
* Creates an instance of LSP entries TLV.
*
* @param tlvHeader tlvHeader.
*/
public LspEntriesTlv(TlvHeader tlvHeader) {
this.setTlvType(tlvHeader.tlvType());
this.setTlvLength(tlvHeader.tlvLength());
}
/**
* Returns the LSP entry of LSP entries TLV.
*
* @return LSP entries
*/
public List<LspEntry> lspEntry() {
return lspEntryList;
}
/**
* Adds the the LSP entry to LSP entries TLV.
*
* @param lspEntry LSP entry
*/
public void addLspEntry(LspEntry lspEntry) {
this.lspEntryList.add(lspEntry);
}
@Override
public void readFrom(ChannelBuffer channelBuffer) {
while (channelBuffer.readableBytes() >= 16) {
LspEntry lspEntry = new LspEntry();
lspEntry.readFrom(channelBuffer.readBytes(16));
lspEntryList.add(lspEntry);
}
}
@Override
public byte[] asBytes() {
byte[] bytes = null;
byte[] tlvHeader = tlvHeaderAsByteArray();
byte[] tlvBody = tlvBodyAsBytes();
tlvHeader[1] = (byte) tlvBody.length;
bytes = Bytes.concat(tlvHeader, tlvBody);
return bytes;
}
/**
* Returns TLV body of LSP entries TLV.
*
* @return byteArray TLV body of LSP entries TLV
*/
private byte[] tlvBodyAsBytes() {
List<Byte> bytes = new ArrayList<>();
for (LspEntry lspEntry : lspEntryList) {
bytes.addAll(Bytes.asList(lspEntry.lspEntryAsBytes()));
}
return Bytes.toArray(bytes);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.omitNullValues()
.add("lspEntryList", lspEntryList)
.toString();
}
}
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.isis.io.isispacket.tlv;
import com.google.common.base.MoreObjects;
import com.google.common.primitives.Bytes;
import org.jboss.netty.buffer.ChannelBuffer;
import org.onosproject.isis.io.util.IsisUtil;
import java.util.ArrayList;
import java.util.List;
/**
* Representation of LSP entry.
*/
public class LspEntry {
private int lspSequenceNumber;
private int lspChecksum;
private int remainingTime;
private String lspId;
/**
* Returns LSP sequence number of LSP entry.
*
* @return LSP sequence number
*/
public int lspSequenceNumber() {
return lspSequenceNumber;
}
/**
* Sets LSP sequenceNumber for LSP entry.
*
* @param lspSequenceNumber lspSequenceNumber.
*/
public void setLspSequenceNumber(int lspSequenceNumber) {
this.lspSequenceNumber = lspSequenceNumber;
}
/**
* Returns LSP checksum of LSP entry.
*
* @return LSP checksum
*/
public int lspChecksum() {
return lspChecksum;
}
/**
* Sets LSP checksum for LSP entry.
*
* @param lspChecksum LSP checksum
*/
public void setLspChecksum(int lspChecksum) {
this.lspChecksum = lspChecksum;
}
/**
* Returns remaining time of LSP entry.
*
* @return remaining time
*/
public int remainingTime() {
return remainingTime;
}
/**
* Sets remaining time for LSP entry.
*
* @param remainingTime remaining time
*/
public void setRemainingTime(int remainingTime) {
this.remainingTime = remainingTime;
}
/**
* Returns LSP ID of LSP entry.
*
* @return LSP ID
*/
public String lspId() {
return lspId;
}
/**
* Sets LSP ID for LSp entry.
*
* @param lspId LSP ID
*/
public void setLspId(String lspId) {
this.lspId = lspId;
}
/**
* Sets the LSP entry values for LSP entry from byte buffer.
*
* @param channelBuffer channel Buffer instance
*/
public void readFrom(ChannelBuffer channelBuffer) {
this.setRemainingTime(channelBuffer.readUnsignedShort());
byte[] tempByteArray = new byte[IsisUtil.ID_PLUS_ONE_BYTE];
channelBuffer.readBytes(tempByteArray, 0, IsisUtil.ID_PLUS_ONE_BYTE);
this.setLspId(IsisUtil.systemIdPlus(tempByteArray));
this.setLspSequenceNumber(channelBuffer.readInt());
this.setLspChecksum(channelBuffer.readUnsignedByte());
}
/**
* Returns LSP entry values as bytes of LSP entry.
*
* @return byteArray LSP entry values as bytes of LSP entry
*/
public byte[] lspEntryAsBytes() {
List<Byte> bytes = new ArrayList<>();
bytes.addAll(Bytes.asList(IsisUtil.convertToTwoBytes(this.remainingTime())));
bytes.addAll(IsisUtil.sourceAndLanIdToBytes(this.lspId()));
bytes.addAll(Bytes.asList(IsisUtil.convertToFourBytes(this.lspSequenceNumber())));
bytes.addAll(Bytes.asList(IsisUtil.convertToTwoBytes(this.lspChecksum())));
return Bytes.toArray(bytes);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.omitNullValues()
.add("lspSequenceNumber", lspSequenceNumber)
.add("lspChecksum", lspChecksum)
.add("remainingTime", remainingTime)
.add("lspId", lspId)
.toString();
}
}
\ No newline at end of file
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.isis.io.isispacket.tlv;
import com.google.common.base.MoreObjects;
import com.google.common.primitives.Bytes;
import org.jboss.netty.buffer.ChannelBuffer;
import org.onlab.packet.Ip4Address;
import org.onosproject.isis.io.util.IsisUtil;
import java.util.ArrayList;
import java.util.List;
/**
* Representation of metric of internal reachability.
*/
public class MetricOfInternalReachability {
private Ip4Address ipAddress;
private Ip4Address subnetAddres;
private byte defaultMetric;
private byte delayMetric;
private byte expenseMetric;
private byte errorMetric;
private boolean delayMetricSupported;
private boolean expenseMetricSupported;
private boolean errorMetricSupported;
private boolean defaultIsInternal;
private boolean defaultDistributionDown;
private boolean delayIsInternal;
private boolean expenseIsInternal;
private boolean errorIsInternal;
/**
* Returns the IP address of metric of internal reachability.
*
* @return ipAddress IP address of metric of internal reachability
*/
public Ip4Address getIpAddress() {
return ipAddress;
}
/**
* Sets the IP address for metric of internal reachability.
*
* @param ipAddress ip address
*/
public void setIpAddress(Ip4Address ipAddress) {
this.ipAddress = ipAddress;
}
/**
* Returns the subnet address of metric of internal reachability.
*
* @return subnetAddres subnet address of metric of internal reachability
*/
public Ip4Address getSubnetAddres() {
return subnetAddres;
}
/**
* Sets the subnet address for metric of internal reachability.
*
* @param subnetAddres subnet address
*/
public void setSubnetAddres(Ip4Address subnetAddres) {
this.subnetAddres = subnetAddres;
}
/**
* Returns error metric is internal or not.
*
* @return true if internal else false
*/
public boolean isErrorIsInternal() {
return errorIsInternal;
}
/**
* Sets error metric is internal or not.
*
* @param errorIsInternal true if internal else false
*/
public void setErrorIsInternal(boolean errorIsInternal) {
this.errorIsInternal = errorIsInternal;
}
/**
* Returns expense metric is internal or not.
*
* @return true if internal else false
*/
public boolean isExpenseIsInternal() {
return expenseIsInternal;
}
/**
* Sets expense metric is internal or not.
*
* @param expenseIsInternal true if internal else false
*/
public void setExpenseIsInternal(boolean expenseIsInternal) {
this.expenseIsInternal = expenseIsInternal;
}
/**
* Returns delays metric is internal or not.
*
* @return true if internal else false
*/
public boolean isDelayIsInternal() {
return delayIsInternal;
}
/**
* Sets delay metric is internal or not.
*
* @param delayIsInternal true if internal else false
*/
public void setDelayIsInternal(boolean delayIsInternal) {
this.delayIsInternal = delayIsInternal;
}
/**
* Returns is default distribution is up or down.
*
* @return true if down else false
*/
public boolean isDefaultDistributionDown() {
return defaultDistributionDown;
}
/**
* Sets default distribution is up or down.
*
* @param defaultDistributionDown true if down else false
*/
public void setDefaultDistributionDown(boolean defaultDistributionDown) {
this.defaultDistributionDown = defaultDistributionDown;
}
/**
* Returns is default metric is internal or not.
*
* @return true if internal else false
*/
public boolean isDefaultIsInternal() {
return defaultIsInternal;
}
/**
* Sets default metric is internal or not.
*
* @param defaultIsInternal true is internal else false
*/
public void setDefaultIsInternal(boolean defaultIsInternal) {
this.defaultIsInternal = defaultIsInternal;
}
/**
* Returns error metric is supported or not.
*
* @return true if supported else false
*/
public boolean isErrorMetricSupported() {
return errorMetricSupported;
}
/**
* Sets error metric is supported or not.
*
* @param errorMetricSupported true if supported else false
*/
public void setErrorMetricSupported(boolean errorMetricSupported) {
this.errorMetricSupported = errorMetricSupported;
}
/**
* Returns expense metric is supported or not.
*
* @return true if supported else false
*/
public boolean isExpenseMetricSupported() {
return expenseMetricSupported;
}
/**
* Sets expense metric is supported or not.
*
* @param expenseMetricSupported true if supported else false
*/
public void setExpenseMetricSupported(boolean expenseMetricSupported) {
this.expenseMetricSupported = expenseMetricSupported;
}
/**
* Returns delay metric is supported or not.
*
* @return true if supported else false
*/
public boolean isDelayMetricSupported() {
return delayMetricSupported;
}
/**
* Sets delay metric is supported or not.
*
* @param delayMetricSupported true if supported else false
*/
public void setDelayMetricSupported(boolean delayMetricSupported) {
this.delayMetricSupported = delayMetricSupported;
}
/**
* Returns error metric of metric of internal reachability.
*
* @return errorMetric error metic
*/
public byte errorMetric() {
return errorMetric;
}
/**
* Sets error metric for metric of internal reachability.
*
* @param errorMetric error metric
*/
public void setErrorMetric(byte errorMetric) {
this.errorMetric = errorMetric;
}
/**
* Returns expense metric of metric of internal reachability.
*
* @return expense metric
*/
public byte expenseMetric() {
return expenseMetric;
}
/**
* Sets expense metric for metric of internal reachability.
*
* @param expenseMetric expense metric
*/
public void setExpenseMetric(byte expenseMetric) {
this.expenseMetric = expenseMetric;
}
/**
* Returns delay metric of metric of internal reachability.
*
* @return delay metric
*/
public byte delayMetric() {
return delayMetric;
}
/**
* Sets delay metric for metric of internal reachability.
*
* @param delayMetric delay metric
*/
public void setDelayMetric(byte delayMetric) {
this.delayMetric = delayMetric;
}
/**
* Returns default metric of metric of internal reachability.
*
* @return default metric
*/
public byte defaultMetric() {
return defaultMetric;
}
/**
* Sets default metric for metric of internal reachability.
*
* @param defaultMetric default metric
*/
public void setDefaultMetric(byte defaultMetric) {
this.defaultMetric = defaultMetric;
}
/**
* Sets the metric of internal reachability
* values for metric of internal reachability from byte buffer.
*
* @param channelBuffer channel Buffer instance
*/
public void readFrom(ChannelBuffer channelBuffer) {
byte metric = channelBuffer.readByte();
this.setDefaultMetric(metric);
String metricInBinary = Integer.toBinaryString(Byte.toUnsignedInt(metric));
metricInBinary = IsisUtil.toEightBitBinary(metricInBinary);
if (metricInBinary.charAt(1) == 0) {
this.setDefaultIsInternal(true);
} else {
this.setDefaultIsInternal(false);
}
if (metricInBinary.charAt(0) == 0) {
this.setDefaultDistributionDown(true);
} else {
this.setDefaultDistributionDown(false);
}
byte delayMetric = channelBuffer.readByte();
metricInBinary = Integer.toBinaryString(Byte.toUnsignedInt(delayMetric));
metricInBinary = IsisUtil.toEightBitBinary(metricInBinary);
this.setDelayMetric(Byte.parseByte(metricInBinary.substring(5, metricInBinary.length()), 2));
if (metricInBinary.charAt(1) == 0) {
this.setDelayIsInternal(true);
} else {
this.setDelayIsInternal(false);
}
if (metricInBinary.charAt(0) == 0) {
this.setDelayMetricSupported(true);
} else {
this.setDelayMetricSupported(false);
}
byte expenseMetric = channelBuffer.readByte();
metricInBinary = Integer.toBinaryString(Byte.toUnsignedInt(expenseMetric));
metricInBinary = IsisUtil.toEightBitBinary(metricInBinary);
this.setExpenseMetric(Byte.parseByte(metricInBinary.substring(5, metricInBinary.length()), 2));
if (metricInBinary.charAt(1) == 0) {
this.setExpenseIsInternal(true);
} else {
this.setExpenseIsInternal(false);
}
if (metricInBinary.charAt(0) == 0) {
this.setExpenseMetricSupported(true);
} else {
this.setExpenseMetricSupported(false);
}
byte errorMetric = channelBuffer.readByte();
metricInBinary = Integer.toBinaryString(Byte.toUnsignedInt(errorMetric));
metricInBinary = IsisUtil.toEightBitBinary(metricInBinary);
this.setErrorMetric(Byte.parseByte(metricInBinary.substring(5, metricInBinary.length()), 2));
if (metricInBinary.charAt(1) == 0) {
this.setErrorIsInternal(true);
} else {
this.setErrorIsInternal(false);
}
if (metricInBinary.charAt(0) == 0) {
this.setErrorMetricSupported(true);
} else {
this.setErrorMetricSupported(false);
}
List<Byte> byteList = new ArrayList<>();
while (channelBuffer.readableBytes() > 0) {
byteList.add(channelBuffer.readByte());
}
byte[] tempByteArray = new byte[IsisUtil.FOUR_BYTES];
channelBuffer.readBytes(tempByteArray, 0, IsisUtil.FOUR_BYTES);
this.setIpAddress(Ip4Address.valueOf(tempByteArray));
tempByteArray = new byte[IsisUtil.FOUR_BYTES];
channelBuffer.readBytes(tempByteArray, 0, IsisUtil.FOUR_BYTES);
this.setSubnetAddres(Ip4Address.valueOf(tempByteArray));
}
/**
* Returns metric of internal reachability values as bytes of metric of internal reachability.
*
* @return byteArray metric of internal reachability values as bytes of metric of internal reachability
*/
public byte[] asBytes() {
List<Byte> bytes = new ArrayList<>();
bytes.add(this.defaultMetric());
int temp = this.delayMetric();
String hsbBits = "";
if (this.isDelayMetricSupported()) {
hsbBits = "0" + hsbBits;
} else {
hsbBits = "1" + hsbBits;
}
if (this.isDelayIsInternal()) {
hsbBits = hsbBits + "0";
} else {
hsbBits = hsbBits + "1";
}
hsbBits = hsbBits + "00";
String binary = hsbBits + IsisUtil.toFourBitBinary(Integer.toBinaryString(temp));
bytes.add((byte) Integer.parseInt(binary, 2));
temp = this.expenseMetric();
hsbBits = "";
if (this.isExpenseMetricSupported()) {
hsbBits = "0" + hsbBits;
} else {
hsbBits = "1" + hsbBits;
}
if (this.isExpenseIsInternal()) {
hsbBits = hsbBits + "0";
} else {
hsbBits = hsbBits + "1";
}
hsbBits = hsbBits + "00";
binary = hsbBits + IsisUtil.toFourBitBinary(Integer.toBinaryString(temp));
bytes.add((byte) Integer.parseInt(binary, 2));
temp = this.errorMetric();
hsbBits = "";
if (this.isErrorMetricSupported()) {
hsbBits = "0" + hsbBits;
} else {
hsbBits = "1" + hsbBits;
}
if (this.isExpenseIsInternal()) {
hsbBits = hsbBits + "0";
} else {
hsbBits = hsbBits + "1";
}
hsbBits = hsbBits + "00";
binary = hsbBits + IsisUtil.toFourBitBinary(Integer.toBinaryString(temp));
bytes.add((byte) Integer.parseInt(binary, 2));
bytes.addAll(Bytes.asList(this.getIpAddress().toOctets()));
bytes.addAll(Bytes.asList(this.getSubnetAddres().toOctets()));
return Bytes.toArray(bytes);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.omitNullValues()
.add("ipAddress", ipAddress)
.add("subnetAddres", subnetAddres)
.add("defaultMetric", defaultMetric)
.add("delayMetric", delayMetric)
.add("expenseMetric", expenseMetric)
.add("errorMetric", errorMetric)
.add("delayMetricSupported", delayMetricSupported)
.add("expenseMetricSupported", expenseMetricSupported)
.add("errorMetricSupported", errorMetricSupported)
.add("defaultIsInternal", defaultIsInternal)
.add("defaultDistributionDown", defaultDistributionDown)
.add("delayIsInternal", delayIsInternal)
.add("expenseIsInternal", expenseIsInternal)
.add("errorIsInternal", errorIsInternal)
.toString();
}
}
\ No newline at end of file
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.isis.io.isispacket.tlv;
import com.google.common.base.MoreObjects;
import com.google.common.primitives.Bytes;
import org.jboss.netty.buffer.ChannelBuffer;
import org.onosproject.isis.io.util.IsisUtil;
import java.util.ArrayList;
import java.util.List;
/**
* Representation of metric of reachability.
*/
public class MetricsOfReachability {
private final String value1 = "10000000";
private final String value2 = "00000000";
private String neighborId;
private byte defaultMetric;
private byte delayMetric;
private byte expenseMetric;
private byte errorMetric;
private boolean delayMetricSupported;
private boolean expenseMetricSupported;
private boolean errorMetricSupported;
private boolean defaultIsInternal;
private boolean delayIsInternal;
private boolean expenseIsInternal;
private boolean errorIsInternal;
/**
* Returns delay metric is internal or not.
*
* @return true if internal else false
*/
public boolean isDelayIsInternal() {
return delayIsInternal;
}
/**
* Sets delay metric is internal or not.
*
* @param delayIsInternal true if internal else false
*/
public void setDelayIsInternal(boolean delayIsInternal) {
this.delayIsInternal = delayIsInternal;
}
/**
* Returns expense metric is internal or not.
*
* @return true if internal else false
*/
public boolean isExpenseIsInternal() {
return expenseIsInternal;
}
/**
* Sets expense metric is internal or not.
*
* @param expenseIsInternal true if internal else false
*/
public void setExpenseIsInternal(boolean expenseIsInternal) {
this.expenseIsInternal = expenseIsInternal;
}
/**
* Returns error metric is internal or not.
*
* @return true if internal else false
*/
public boolean isErrorIsInternal() {
return errorIsInternal;
}
/**
* Sets error metric is internal or not.
*
* @param errorIsInternal true if internal else false
*/
public void setErrorIsInternal(boolean errorIsInternal) {
this.errorIsInternal = errorIsInternal;
}
/**
* Returns default metric is internal or not.
*
* @return true if internal else false
*/
public boolean isDefaultIsInternal() {
return defaultIsInternal;
}
/**
* Sets default metric is internal or not.
*
* @param defaultIsInternal true if internal else false
*/
public void setDefaultIsInternal(boolean defaultIsInternal) {
this.defaultIsInternal = defaultIsInternal;
}
/**
* Returns delay metric is supported or not.
*
* @return true if supported else false
*/
public boolean isDelayMetricSupported() {
return delayMetricSupported;
}
/**
* Sets delay metric is supported or not.
*
* @param delayMetricSupported true if supported else false
*/
public void setDelayMetricSupported(boolean delayMetricSupported) {
this.delayMetricSupported = delayMetricSupported;
}
/**
* Returns expense metric is supported or not.
*
* @return true if supported else false
*/
public boolean isExpenseMetricSupported() {
return expenseMetricSupported;
}
/**
* Sets expense metric is supported or not.
*
* @param expenseMetricSupported true if supported else false
*/
public void setExpenseMetricSupported(boolean expenseMetricSupported) {
this.expenseMetricSupported = expenseMetricSupported;
}
/**
* Returns error metric is supported or not.
*
* @return true if supported else false
*/
public boolean isErrorMetricSupported() {
return errorMetricSupported;
}
/**
* Sets error metric is supported or not.
*
* @param errorMetricSupported true if supported else false
*/
public void setErrorMetricSupported(boolean errorMetricSupported) {
this.errorMetricSupported = errorMetricSupported;
}
/**
* Returns neighbor ID of metric of reachability.
*
* @return neighbor ID
*/
public String neighborId() {
return neighborId;
}
/**
* Sets neighbor ID for metric of reachability.
*
* @param neighborId neighbor ID
*/
public void setNeighborId(String neighborId) {
this.neighborId = neighborId;
}
/**
* Returns default metric of metric of reachability.
*
* @return default metric
*/
public byte defaultMetric() {
return defaultMetric;
}
/**
* Sets default metric for of reachability.
*
* @param defaultMetric default metric
*/
public void setDefaultMetric(byte defaultMetric) {
this.defaultMetric = defaultMetric;
}
/**
* Returns delay metric of metric of reachability.
*
* @return delay metric
*/
public byte delayMetric() {
return delayMetric;
}
/**
* Sets delay metric for metric of reachability.
*
* @param delayMetric delay metric.
*/
public void setDelayMetric(byte delayMetric) {
this.delayMetric = delayMetric;
}
/**
* Returns Expense metric of metric of reachability.
*
* @return Expense metric
*/
public byte expenseMetric() {
return expenseMetric;
}
/**
* Sets Expense metric for metric of reachability.
*
* @param expenseMetric Expense metric.
*/
public void setExpenseMetric(byte expenseMetric) {
this.expenseMetric = expenseMetric;
}
/**
* Returns Error metric of metric of reachability.
*
* @return Error metric
*/
public byte errorMetric() {
return errorMetric;
}
/**
* Sets Error metric for metric of reachability.
*
* @param errorMetric Error metric.
*/
public void setErrorMetric(byte errorMetric) {
this.errorMetric = errorMetric;
}
/**
* Sets the metric of reachability values for metric of reachability from byte buffer.
*
* @param channelBuffer channel Buffer instance
*/
public void readFrom(ChannelBuffer channelBuffer) {
byte metric = channelBuffer.readByte();
String metricInBinary = Integer.toBinaryString(Byte.toUnsignedInt(metric));
metricInBinary = IsisUtil.toEightBitBinary(metricInBinary);
this.setDefaultMetric(metric);
this.setDelayMetric(metric);
this.setExpenseMetric(metric);
this.setErrorMetric(metric);
this.setDelayMetric(metric);
if (metricInBinary.charAt(0) == 0) {
this.setDefaultIsInternal(true);
} else {
this.setDefaultIsInternal(false);
}
if (metricInBinary.charAt(0) == 0) {
this.setDelayMetricSupported(true);
this.setExpenseMetricSupported(true);
this.setErrorMetricSupported(true);
} else {
this.setDelayMetricSupported(false);
this.setExpenseMetricSupported(false);
this.setErrorMetricSupported(false);
}
byte temp = channelBuffer.readByte();
String internalBit = Integer.toBinaryString(Byte.toUnsignedInt(temp));
internalBit = IsisUtil.toEightBitBinary(internalBit);
if (internalBit.charAt(1) == 0) {
this.setDelayIsInternal(true);
}
temp = channelBuffer.readByte();
internalBit = Integer.toBinaryString(Byte.toUnsignedInt(temp));
internalBit = IsisUtil.toEightBitBinary(internalBit);
if (internalBit.charAt(1) == 0) {
this.setExpenseIsInternal(true);
}
temp = channelBuffer.readByte();
internalBit = Integer.toBinaryString(Byte.toUnsignedInt(temp));
internalBit = IsisUtil.toEightBitBinary(internalBit);
if (internalBit.charAt(1) == 0) {
this.setErrorIsInternal(true);
}
byte[] tempByteArray = new byte[IsisUtil.ID_PLUS_ONE_BYTE];
channelBuffer.readBytes(tempByteArray, 0, IsisUtil.ID_PLUS_ONE_BYTE);
this.setNeighborId(IsisUtil.systemIdPlus(tempByteArray));
}
/**
* Returns metric of reachability values as bytes of metric of reachability.
*
* @return byteArray metric of reachability values as bytes of metric of reachability
*/
public byte[] asBytes() {
List<Byte> bytes = new ArrayList<>();
bytes.add((byte) this.defaultMetric());
if (this.isDelayIsInternal()) {
bytes.add((byte) Integer.parseInt(value1));
} else {
bytes.add((byte) Integer.parseInt(value2));
}
if (this.isExpenseIsInternal()) {
bytes.add((byte) Integer.parseInt(value1));
} else {
bytes.add((byte) Integer.parseInt(value2));
}
if (this.isErrorIsInternal()) {
bytes.add((byte) Integer.parseInt(value1));
} else {
bytes.add((byte) Integer.parseInt(value2));
}
bytes.addAll(IsisUtil.sourceAndLanIdToBytes(this.neighborId()));
return Bytes.toArray(bytes);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.omitNullValues()
.add("neighborId", neighborId)
.add("defaultMetric", defaultMetric)
.add("delayMetric", delayMetric)
.add("expenseMetric", expenseMetric)
.add("errorMetric", errorMetric)
.add("delayMetricSupported", delayMetricSupported)
.add("expenseMetricSupported", expenseMetricSupported)
.add("errorMetricSupported", errorMetricSupported)
.add("defaultIsInternal", defaultIsInternal)
.add("delayIsInternal", delayIsInternal)
.add("expenseIsInternal", expenseIsInternal)
.add("errorIsInternal", errorIsInternal)
.toString();
}
}
......@@ -17,19 +17,19 @@ package org.onosproject.isis.io.isispacket.tlv;
import com.google.common.base.MoreObjects;
import com.google.common.primitives.Bytes;
import io.netty.buffer.ByteBuf;
import org.jboss.netty.buffer.ChannelBuffer;
import java.util.ArrayList;
import java.util.List;
/**
* Represents padding TLV.
* Representation of padding TLV.
*/
public class PaddingTlv extends TlvHeader implements IsisTlv {
private List<Byte> paddings = new ArrayList();
private List<Byte> paddings = new ArrayList<>();
/**
* Sets TLV type and TLV length of padding TLV.
* Creates an instance of padding TLV.
*
* @param tlvHeader tlvHeader.
*/
......@@ -39,9 +39,9 @@ public class PaddingTlv extends TlvHeader implements IsisTlv {
}
@Override
public void readFrom(ByteBuf byteBuf) {
while (byteBuf.readableBytes() > 0) {
this.paddings.add(byteBuf.readByte());
public void readFrom(ChannelBuffer channelBuffer) {
while (channelBuffer.readableBytes() > 0) {
this.paddings.add(channelBuffer.readByte());
}
}
......@@ -51,17 +51,18 @@ public class PaddingTlv extends TlvHeader implements IsisTlv {
byte[] tlvHeader = tlvHeaderAsByteArray();
byte[] tlvBody = tlvBodyAsBytes();
tlvHeader[1] = (byte) tlvBody.length;
bytes = Bytes.concat(tlvHeader, tlvBody);
return bytes;
}
/**
* Gets TLV body padding TLV.
* Returns TLV body of padding TLV.
*
* @return areaArea TLV body padding TLV\\
* @return byteArray TLV body of padding TLV
*/
public byte[] tlvBodyAsBytes() {
private byte[] tlvBodyAsBytes() {
byte[] areaArea = new byte[this.tlvLength()];
return areaArea;
}
......@@ -70,6 +71,7 @@ public class PaddingTlv extends TlvHeader implements IsisTlv {
public String toString() {
return MoreObjects.toStringHelper(getClass())
.omitNullValues()
.add("paddings", paddings)
.toString();
}
}
\ No newline at end of file
......
......@@ -15,21 +15,22 @@
*/
package org.onosproject.isis.io.isispacket.tlv;
import com.google.common.base.MoreObjects;
import com.google.common.primitives.Bytes;
import io.netty.buffer.ByteBuf;
import org.jboss.netty.buffer.ChannelBuffer;
import java.util.ArrayList;
import java.util.List;
/**
* Represents Protocol supported TLV.
* Representation of protocol supported TLV.
*/
public class ProtocolSupportedTlv extends TlvHeader implements IsisTlv {
private List<Byte> protocolSupported = new ArrayList();
private List<Byte> protocolSupported = new ArrayList<>();
/**
* Sets TLV type and TLV length of protocol supported TLV.
* Creates an instance of protocol supported TLV.
*
* @param tlvHeader tlvHeader.
*/
......@@ -41,21 +42,27 @@ public class ProtocolSupportedTlv extends TlvHeader implements IsisTlv {
}
/**
* Gets the Protocol Supported by the TLV.
* Adds the protocol supported to protocol supported TLV.
*
* @return Protocol Supported
* @param protocolValue protocol supported
*/
public List<Byte> protocolSupported() {
public void addProtocolSupported(byte protocolValue) {
protocolSupported.add(protocolValue);
}
/**
* Returns protocols supported of protocol supported TLV.
*
* @return protocol supported
*/
public List<Byte> protocolSupported() {
return this.protocolSupported;
}
@Override
public void readFrom(ByteBuf byteBuf) {
while (byteBuf.readableBytes() > 0) {
this.protocolSupported.add(byteBuf.readByte());
public void readFrom(ChannelBuffer channelBuffer) {
while (channelBuffer.readableBytes() > 0) {
this.protocolSupported.add(channelBuffer.readByte());
}
}
......@@ -65,8 +72,8 @@ public class ProtocolSupportedTlv extends TlvHeader implements IsisTlv {
byte[] tlvHeader = tlvHeaderAsByteArray();
byte[] tlvBody = tlvBodyAsBytes();
tlvHeader[1] = (byte) tlvBody.length;
bytes = Bytes.concat(tlvHeader, tlvBody);
return bytes;
}
......@@ -75,9 +82,8 @@ public class ProtocolSupportedTlv extends TlvHeader implements IsisTlv {
*
* @return byteArray TLV body of protocol supported TLV
*/
public byte[] tlvBodyAsBytes() {
List<Byte> bytes = new ArrayList();
private byte[] tlvBodyAsBytes() {
List<Byte> bytes = new ArrayList<>();
for (byte byt : this.protocolSupported) {
bytes.add(byt);
}
......@@ -88,4 +94,12 @@ public class ProtocolSupportedTlv extends TlvHeader implements IsisTlv {
}
return byteArray;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.omitNullValues()
.add("protocolSupported", protocolSupported)
.toString();
}
}
\ No newline at end of file
......
......@@ -15,55 +15,90 @@
*/
package org.onosproject.isis.io.isispacket.tlv;
import io.netty.buffer.ByteBuf;
import org.onosproject.isis.io.util.IsisUtil;
import org.jboss.netty.buffer.ChannelBuffer;
/**
* Represents TLV finder.
* Representation of TLV Finder.
*/
public class TlvFinder extends TlvHeader {
/**
* Sets the value for TLV header and the body of the TLV.
* Sets the value for TLV header.
*
* @param tlvHeader tlvHeader
* @param byteBuf byteBuf
* @return isisTlv ISIS TLV
* @param tlvHeader tlvHeader
* @param channelBuffer byteBuf
* @return isisTlv
*/
public static IsisTlv findTlv(TlvHeader tlvHeader, ByteBuf byteBuf) {
public static IsisTlv findTlv(TlvHeader tlvHeader, ChannelBuffer channelBuffer) {
IsisTlv isisTlv = null;
switch (tlvHeader.tlvType()) {
case IsisUtil.AREAADDRESS:
switch (TlvType.get(tlvHeader.tlvType())) {
case AREAADDRESS:
AreaAddressTlv areaAddressTlv = new AreaAddressTlv(tlvHeader);
areaAddressTlv.readFrom(byteBuf);
areaAddressTlv.readFrom(channelBuffer);
isisTlv = areaAddressTlv;
break;
case IsisUtil.IPINTERFACEADDRESS:
case AUTHENTICATION:
//TODO
break;
case EXTENDEDISREACHABILITY:
//TODO
break;
case IDRPINFORMATION:
IdrpInformationTlv idrpInformationTlv = new IdrpInformationTlv(tlvHeader);
idrpInformationTlv.readFrom(channelBuffer);
isisTlv = idrpInformationTlv;
break;
case IPEXTENDEDREACHABILITY:
IpExtendedReachabilityTlv iperTlv = new IpExtendedReachabilityTlv(tlvHeader);
iperTlv.readFrom(channelBuffer);
isisTlv = iperTlv;
break;
case IPINTERFACEADDRESS:
IpInterfaceAddressTlv ipTlv = new IpInterfaceAddressTlv(tlvHeader);
ipTlv.readFrom(byteBuf);
ipTlv.readFrom(channelBuffer);
isisTlv = ipTlv;
break;
case IsisUtil.PROTOCOLSUPPORTED:
case IPINTERNALREACHABILITY:
IpInternalReachabilityTlv iprTlv = new IpInternalReachabilityTlv(tlvHeader);
iprTlv.readFrom(channelBuffer);
isisTlv = iprTlv;
break;
case ISALIAS:
break;
case PROTOCOLSUPPORTED:
ProtocolSupportedTlv psTlv = new ProtocolSupportedTlv(tlvHeader);
psTlv.readFrom(byteBuf);
psTlv.readFrom(channelBuffer);
isisTlv = psTlv;
break;
case IsisUtil.ISNEIGHBORS:
case ISREACHABILITY:
IsReachabilityTlv isrTlv = new IsReachabilityTlv(tlvHeader);
isrTlv.readFrom(channelBuffer);
isisTlv = isrTlv;
break;
case ISNEIGHBORS:
IsisNeighborTlv isisNeighborTlv = new IsisNeighborTlv(tlvHeader);
isisNeighborTlv.readFrom(byteBuf);
isisNeighborTlv.readFrom(channelBuffer);
isisTlv = isisNeighborTlv;
break;
case IsisUtil.PADDING:
case LSPENTRY:
LspEntriesTlv lspEntriesTlv = new LspEntriesTlv(tlvHeader);
lspEntriesTlv.readFrom(channelBuffer);
isisTlv = lspEntriesTlv;
break;
case PADDING:
PaddingTlv paddingTlv = new PaddingTlv(tlvHeader);
paddingTlv.readFrom(byteBuf);
paddingTlv.readFrom(channelBuffer);
isisTlv = paddingTlv;
break;
case ADJACENCYSTATE:
AdjacencyStateTlv adjacencyStateTlv = new AdjacencyStateTlv(tlvHeader);
adjacencyStateTlv.readFrom(channelBuffer);
isisTlv = adjacencyStateTlv;
break;
default:
break;
}
return isisTlv;
}
}
\ No newline at end of file
......
......@@ -17,29 +17,29 @@ package org.onosproject.isis.io.isispacket.tlv;
import com.google.common.base.MoreObjects;
import com.google.common.primitives.Bytes;
import io.netty.buffer.ByteBuf;
import org.jboss.netty.buffer.ChannelBuffer;
import java.util.ArrayList;
import java.util.List;
/**
* Represents TLV header.
* Representation of TLV header.
*/
public class TlvHeader implements IsisTlv {
private int tlvType;
private int tlvLength;
/**
* Gets the TLV length of the TLV.
* Returns TLV length of TLV.
*
* @return tlvLength TLV length
* @return TLV length
*/
public int tlvLength() {
return tlvLength;
}
/**
* Sets the TLV length for the mTLV.
* Sets TLV length for TLV.
*
* @param tlvLength TLV length
*/
......@@ -48,16 +48,16 @@ public class TlvHeader implements IsisTlv {
}
/**
* Gets the TLV type of the TLV.
* Returns TLV type of TLV.
*
* @return tlvType TLV type
* @return TLV type
*/
public int tlvType() {
return tlvType;
}
/**
* Sets TLV type for the TLV.
* Sets TLV type for TLV.
*
* @param tlvType TLV type
*/
......@@ -66,32 +66,31 @@ public class TlvHeader implements IsisTlv {
}
/**
* Sets the TLV values of TLV from b yte buffer.
* Sets TLV values from channel buffer.
*
* @param byteBuf byteBuf.
* @param channelBuffer channel Buffer instance
*/
public void readFrom(ByteBuf byteBuf) {
public void readFrom(ChannelBuffer channelBuffer) {
//implemented in sub classes
}
/**
* Gets the TLV of the TLV as bytes.
* Returns TLV as byte array.
*
* @return null
* @return byteArray TLV body of area address TLV
*/
public byte[] asBytes() {
//implemented the subclasses
return null;
}
/**
* Gets the TLV header of the TLV.
* Returns TLV header of TLV as bytes.
*
* @return headerLst TLV of the TLV
* @return TLV header as bytes
*/
public byte[] tlvHeaderAsByteArray() {
List<Byte> headerLst = new ArrayList();
List<Byte> headerLst = new ArrayList<>();
headerLst.add((byte) this.tlvType);
headerLst.add((byte) this.tlvLength);
return Bytes.toArray(headerLst);
......
......@@ -15,8 +15,12 @@
*/
package org.onosproject.isis.io.isispacket.tlv;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
/**
* Represents various values for TLV types.
* Representation of various values for TLV types.
*/
public enum TlvType {
AREAADDRESS(1),
......@@ -25,19 +29,32 @@ public enum TlvType {
PADDING(8),
LSPENTRY(9),
AUTHENTICATION(10),
CHECKSUM(12),
HOSTNAME(137),
EXTENDEDISREACHABILITY(22),
ISALIAS(24),
IPINTERNALREACHABILITY(128),
PROTOCOLSUPPORTED(129),
IPEXTERNALREACHABILITY(130),
IPEXTENDEDREACHABILITY(135),
IDRPINFORMATION(131),
IPINTERFACEADDRESS(132);
IPINTERFACEADDRESS(132),
ADJACENCYSTATE(240);
// Reverse lookup table
private static final Map<Integer, TlvType> LOOKUP = new HashMap<>();
// Populate the lookup table on loading time
static {
for (TlvType isisTlvType : EnumSet.allOf(TlvType.class)) {
LOOKUP.put(isisTlvType.value(), isisTlvType);
}
}
private int value;
/**
* Sets the TLV type value.
*
* @param value value.
*/
TlvType(int value) {
......@@ -45,10 +62,21 @@ public enum TlvType {
}
/**
* Gets the enum instance from type value - reverse lookup purpose.
*
* @param tlvTypeValue TLV type value
* @return ISIS TLV type instance
*/
public static TlvType get(int tlvTypeValue) {
return LOOKUP.get(tlvTypeValue);
}
/**
* Gets value.
*
* @return value
*/
public int value() {
return value;
}
}
}
\ No newline at end of file
......
......@@ -16,23 +16,33 @@
package org.onosproject.isis.io.isispacket.tlv;
import com.google.common.primitives.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
/**
* Represents conversion of TLV's to bytes.
* Representation of conversion of TLV's to bytes.
*/
public final class TlvsToBytes {
protected static final Logger log = LoggerFactory.getLogger(TlvsToBytes.class);
/**
* Creates an instance.
*/
private TlvsToBytes() {
}
/**
* Sets the ISIS TLV and returns in the form of bytes.
*
* @param isisTlv isisTlv
* @return tlvBytes TLV bytes
* @param isisTlv isisTlv.
* @return tlvBytes
*/
public static List<Byte> tlvToBytes(IsisTlv isisTlv) {
List<Byte> tlvBytes = new ArrayList();
List<Byte> tlvBytes = new ArrayList<>();
if (isisTlv instanceof AreaAddressTlv) {
AreaAddressTlv areaAddressTlv = (AreaAddressTlv) isisTlv;
tlvBytes.addAll(Bytes.asList(areaAddressTlv.asBytes()));
......@@ -48,16 +58,33 @@ public final class TlvsToBytes {
} else if (isisTlv instanceof IsisNeighborTlv) {
IsisNeighborTlv isisNeighborTlv = (IsisNeighborTlv) isisTlv;
tlvBytes.addAll(Bytes.asList(isisNeighborTlv.asBytes()));
} else if (isisTlv instanceof AdjacencyStateTlv) {
AdjacencyStateTlv isisAdjacencyState
= (AdjacencyStateTlv) isisTlv;
tlvBytes.addAll(Bytes.asList(isisAdjacencyState.asBytes()));
} else if (isisTlv instanceof HostNameTlv) {
HostNameTlv hostNameTlv
= (HostNameTlv) isisTlv;
tlvBytes.addAll(Bytes.asList(hostNameTlv.asBytes()));
} else if (isisTlv instanceof IpExtendedReachabilityTlv) {
IpExtendedReachabilityTlv ipExtendedReachabilityTlv
= (IpExtendedReachabilityTlv) isisTlv;
tlvBytes.addAll(Bytes.asList(ipExtendedReachabilityTlv.asBytes()));
} else if (isisTlv instanceof IpInternalReachabilityTlv) {
IpInternalReachabilityTlv ipInternalReachabilityTlv
= (IpInternalReachabilityTlv) isisTlv;
tlvBytes.addAll(Bytes.asList(ipInternalReachabilityTlv.asBytes()));
} else if (isisTlv instanceof IsReachabilityTlv) {
IsReachabilityTlv isReachabilityTlv
= (IsReachabilityTlv) isisTlv;
tlvBytes.addAll(Bytes.asList(isReachabilityTlv.asBytes()));
} else if (isisTlv instanceof LspEntriesTlv) {
LspEntriesTlv lspEntriesTlv
= (LspEntriesTlv) isisTlv;
tlvBytes.addAll(Bytes.asList(lspEntriesTlv.asBytes()));
} else {
System.out.println("UNKNOWN TLV TYPE ::TlvsToBytes ");
log.debug("TlvsToBytes::UNKNOWN TLV TYPE ::TlvsToBytes ");
}
return tlvBytes;
}
/**
* Creates an instance.
*/
private TlvsToBytes() {
//private constructor
}
}
\ No newline at end of file
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.isis.io.isispacket.tlv.subtlv;
import com.google.common.base.MoreObjects;
import com.google.common.primitives.Bytes;
import org.jboss.netty.buffer.ChannelBuffer;
import org.onosproject.isis.io.isispacket.tlv.TlvHeader;
import org.onosproject.isis.io.util.IsisUtil;
/**
* Representation of an administrative group.
*/
public class AdministrativeGroup extends TlvHeader implements TrafficEngineeringSubTlv {
private int administrativeGroup;
/**
* Creates an administrative group instance.
*
* @param header Tlv Header instance
*/
public AdministrativeGroup(TlvHeader header) {
this.setTlvType(header.tlvType());
this.setTlvLength(header.tlvLength());
}
/**
* Gets administrative group value.
*
* @return administrative group value
*/
public int administrativeGroup() {
return administrativeGroup;
}
/**
* Sets administrative group value.
*
* @param administrativeGroup value
*/
public void setAdministrativeGroup(int administrativeGroup) {
this.administrativeGroup = administrativeGroup;
}
/**
* Gets administrative group value.
*
* @return administrativeGroup value
*/
public int getAdministrativeGroupValue() {
return this.administrativeGroup;
}
/**
* Reads bytes from channel buffer.
*
* @param channelBuffer Channel buffer instance
*/
public void readFrom(ChannelBuffer channelBuffer) {
byte[] tempByteArray = new byte[tlvLength()];
channelBuffer.readBytes(tempByteArray, 0, tlvLength());
this.setAdministrativeGroup(IsisUtil.byteToInteger(tempByteArray));
}
/**
* Returns administrative group as byte array.
*
* @return administrative group instance as byte array
*/
public byte[] asBytes() {
byte[] linkSubType = null;
byte[] linkSubTlvHeader = tlvHeaderAsByteArray();
byte[] linkSubTlvBody = tlvBodyAsBytes();
linkSubType = Bytes.concat(linkSubTlvHeader, linkSubTlvBody);
return linkSubType;
}
/**
* Gets administrative group body as byte array.
*
* @return byte array of sub tlv administrative group
*/
public byte[] tlvBodyAsBytes() {
byte[] linkSubTypeBody;
linkSubTypeBody = IsisUtil.convertToFourBytes(this.administrativeGroup);
return linkSubTypeBody;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.add("administrativeGroup", administrativeGroup)
.toString();
}
}
\ No newline at end of file
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.isis.io.isispacket.tlv.subtlv;
import com.google.common.base.MoreObjects;
import com.google.common.primitives.Bytes;
import org.jboss.netty.buffer.ChannelBuffer;
import org.onosproject.isis.io.isispacket.tlv.TlvHeader;
import org.onosproject.isis.io.util.IsisUtil;
/**
* Representation of maximum bandwidth TE value.
*/
public class MaximumBandwidth extends TlvHeader implements TrafficEngineeringSubTlv {
private float maximumBandwidth;
/**
* Creates an instance of maximum bandwidth.
*
* @param header tlv header instance
*/
public MaximumBandwidth(TlvHeader header) {
this.setTlvType(header.tlvType());
this.setTlvLength(header.tlvLength());
}
/**
* Sets value of maximum bandwidth.
*
* @param maximumBandwidth value of maximum bandwidth
*/
public void setMaximumBandwidth(float maximumBandwidth) {
this.maximumBandwidth = maximumBandwidth;
}
/**
* Gets value of maximum bandwidth.
*
* @return maximumBandwidth value of maximum bandwidth
*/
public float getMaximumBandwidthValue() {
return this.maximumBandwidth;
}
/**
* Reads bytes from channel buffer.
*
* @param channelBuffer channel buffer instance
*/
public void readFrom(ChannelBuffer channelBuffer) {
byte[] tempByteArray = new byte[tlvLength()];
channelBuffer.readBytes(tempByteArray, 0, tlvLength());
int maxBandwidth = (IsisUtil.byteToInteger(tempByteArray));
this.setMaximumBandwidth(Float.intBitsToFloat(maxBandwidth));
}
/**
* Gets byte array of maximum bandwidth sub tlv.
*
* @return byte array of maximum bandwidth sub tlv
*/
public byte[] asBytes() {
byte[] linkSubType = null;
byte[] linkSubTlvHeader = tlvHeaderAsByteArray();
byte[] linkSubTlvBody = tlvBodyAsBytes();
linkSubType = Bytes.concat(linkSubTlvHeader, linkSubTlvBody);
return linkSubType;
}
/**
* Gets maximum bandwidth sub tlv byte array.
*
* @return byte array of maximum bandwidth sub tlv
*/
public byte[] tlvBodyAsBytes() {
byte[] linkSubTypeBody;
linkSubTypeBody = IsisUtil.convertToFourBytes(Float.floatToIntBits(this.maximumBandwidth));
return linkSubTypeBody;
}
public String toString() {
return MoreObjects.toStringHelper(getClass())
.add("maximumBandwidth", maximumBandwidth)
.toString();
}
}
\ No newline at end of file
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.isis.io.isispacket.tlv.subtlv;
import com.google.common.base.MoreObjects;
import com.google.common.primitives.Bytes;
import org.jboss.netty.buffer.ChannelBuffer;
import org.onosproject.isis.io.isispacket.tlv.TlvHeader;
import org.onosproject.isis.io.util.IsisUtil;
/**
* Representation of maximum reservable bandwidth TE value.
*/
public class MaximumReservableBandwidth extends TlvHeader implements TrafficEngineeringSubTlv {
private float maximumReservableBandwidth;
/**
* Creates an instance of maximum reservable bandwidth.
*
* @param header tlv header
*/
public MaximumReservableBandwidth(TlvHeader header) {
this.setTlvType(header.tlvType());
this.setTlvLength(header.tlvLength());
}
/**
* Sets value of maximum reversible bandwidth.
*
* @param maximumBandwidth maximum reversible bandwidth
*/
public void setMaximumBandwidth(float maximumBandwidth) {
this.maximumReservableBandwidth = maximumBandwidth;
}
/**
* Gets value of maximum reversible bandwidth.
*
* @return maximumBandwidth maximum reversible bandwidth
*/
public float getMaximumBandwidthValue() {
return this.maximumReservableBandwidth;
}
/**
* Reads bytes from channel buffer.
*
* @param channelBuffer channel buffer instance
*/
public void readFrom(ChannelBuffer channelBuffer) {
byte[] tempByteArray = new byte[tlvLength()];
channelBuffer.readBytes(tempByteArray, 0, tlvLength());
int maxBandwidth = (IsisUtil.byteToInteger(tempByteArray));
this.setMaximumBandwidth(Float.intBitsToFloat(maxBandwidth));
}
/**
* Returns byte array of maximum reservable bandwidth.
*
* @return byte array of maximum reservable bandwidth
*/
public byte[] asBytes() {
byte[] linkSubType = null;
byte[] linkSubTlvHeader = tlvHeaderAsByteArray();
byte[] linkSubTlvBody = tlvBodyAsBytes();
linkSubType = Bytes.concat(linkSubTlvHeader, linkSubTlvBody);
return linkSubType;
}
/**
* Gets maximum reservable bandwidth sub tlv body as byte array.
*
* @return byte of maximum reservable bandwidth sub tlv body
*/
public byte[] tlvBodyAsBytes() {
byte[] linkSubTypeBody;
linkSubTypeBody = IsisUtil.convertToFourBytes(Float.floatToIntBits(this.maximumReservableBandwidth));
return linkSubTypeBody;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.add("maximumReservableBandwidth", maximumReservableBandwidth)
.toString();
}
}
\ No newline at end of file
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.isis.io.isispacket.tlv.subtlv;
import org.jboss.netty.buffer.ChannelBuffer;
import org.onosproject.isis.io.isispacket.tlv.TlvHeader;
/**
* Representation of sub tlv finder.
*/
public final class SubTlvFinder {
/**
* Creates an instance.
*/
private SubTlvFinder() {
}
/**
* Sets the value for TLV header and to find sub TLV and populate.
*
* @param tlvHeader tlvHeader
* @param channelBuffer byteBuf
* @return subTlv traffic engineering sub tlv
*/
public static TrafficEngineeringSubTlv findSubTlv(TlvHeader tlvHeader, ChannelBuffer channelBuffer) {
TrafficEngineeringSubTlv subTlv = null;
switch (SubTlvType.get(tlvHeader.tlvType())) {
case ADMINISTRATIVEGROUP:
AdministrativeGroup administrativeGroup = new AdministrativeGroup(tlvHeader);
administrativeGroup.readFrom(channelBuffer);
subTlv = administrativeGroup;
break;
case MAXIMUMBANDWIDTH:
MaximumBandwidth maximumBandwidth = new MaximumBandwidth(tlvHeader);
maximumBandwidth.readFrom(channelBuffer);
subTlv = maximumBandwidth;
break;
case MAXIMUMRESERVABLEBANDWIDTH:
MaximumReservableBandwidth maxResBandwidth = new MaximumReservableBandwidth(tlvHeader);
maxResBandwidth.readFrom(channelBuffer);
subTlv = maxResBandwidth;
break;
case TRAFFICENGINEERINGMETRIC:
TrafficEngineeringMetric teMetric = new TrafficEngineeringMetric(tlvHeader);
teMetric.readFrom(channelBuffer);
subTlv = teMetric;
break;
case UNRESERVEDBANDWIDTH:
UnreservedBandwidth unreservedBandwidth = new UnreservedBandwidth(tlvHeader);
unreservedBandwidth.readFrom(channelBuffer);
subTlv = unreservedBandwidth;
break;
default:
//TODO
break;
}
return subTlv;
}
}
\ No newline at end of file
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.isis.io.isispacket.tlv.subtlv;
import com.google.common.primitives.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
/**
* Representation of conversion of TLV's to bytes.
*/
public final class SubTlvToBytes {
protected static final Logger log = LoggerFactory.getLogger(SubTlvToBytes.class);
/**
* Creates an instance.
*/
private SubTlvToBytes() {
//private constructor
}
/**
* Sets the ISIS sub TLV and returns in the form of bytes.
*
* @param subTlv isisTlv.
* @return subTlvBytes
*/
public static List<Byte> tlvToBytes(TrafficEngineeringSubTlv subTlv) {
List<Byte> subTlvBytes = new ArrayList<>();
if (subTlv instanceof AdministrativeGroup) {
AdministrativeGroup administrativeGroup = (AdministrativeGroup) subTlv;
subTlvBytes.addAll(Bytes.asList(administrativeGroup.asBytes()));
} else if (subTlv instanceof MaximumBandwidth) {
MaximumBandwidth maximumBandwidth = (MaximumBandwidth) subTlv;
subTlvBytes.addAll(Bytes.asList(maximumBandwidth.asBytes()));
} else if (subTlv instanceof MaximumReservableBandwidth) {
MaximumReservableBandwidth maximumReservableBandwidth = (MaximumReservableBandwidth) subTlv;
subTlvBytes.addAll(Bytes.asList(maximumReservableBandwidth.asBytes()));
} else if (subTlv instanceof TrafficEngineeringMetric) {
TrafficEngineeringMetric trafficEngineeringMetric = (TrafficEngineeringMetric) subTlv;
subTlvBytes.addAll(Bytes.asList(trafficEngineeringMetric.asBytes()));
} else if (subTlv instanceof UnreservedBandwidth) {
UnreservedBandwidth unreservedBandwidth = (UnreservedBandwidth) subTlv;
subTlvBytes.addAll(Bytes.asList(unreservedBandwidth.asBytes()));
} else {
log.debug("TlvsToBytes::UNKNOWN TLV TYPE ::TlvsToBytes ");
}
return subTlvBytes;
}
}
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.isis.io.isispacket.tlv.subtlv;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
/**
* Representation of sub tlv type.
*/
public enum SubTlvType {
ADMINISTRATIVEGROUP(9),
MAXIMUMBANDWIDTH(6),
MAXIMUMRESERVABLEBANDWIDTH(7),
TRAFFICENGINEERINGMETRIC(5),
UNRESERVEDBANDWIDTH(8);
// Reverse lookup table
private static final Map<Integer, SubTlvType> LOOKUP = new HashMap<>();
// Populate the lookup table on loading time
static {
for (SubTlvType subTlvType : EnumSet.allOf(SubTlvType.class)) {
LOOKUP.put(subTlvType.value(), subTlvType);
}
}
private int value;
/**
* Sets the sub TLV type value.
*
* @param value value.
*/
SubTlvType(int value) {
this.value = value;
}
/**
* Gets the enum instance from type value - reverse lookup purpose.
*
* @param subTlvTypeValue TLV type value
* @return ISIS sub TLV type instance
*/
public static SubTlvType get(int subTlvTypeValue) {
return LOOKUP.get(subTlvTypeValue);
}
/**
* Gets value.
*
* @return value
*/
public int value() {
return value;
}
}
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.isis.io.isispacket.tlv.subtlv;
import com.google.common.base.MoreObjects;
import com.google.common.primitives.Bytes;
import org.jboss.netty.buffer.ChannelBuffer;
import org.onosproject.isis.io.isispacket.tlv.TlvHeader;
import org.onosproject.isis.io.util.IsisUtil;
/**
* Representation of traffic engineering metric TE value.
*/
public class TrafficEngineeringMetric extends TlvHeader implements TrafficEngineeringSubTlv {
private long trafficEngineeringMetric;
/**
* Creates an instance of traffic engineering metric .
*
* @param header tlv header instance
*/
public TrafficEngineeringMetric(TlvHeader header) {
this.setTlvType(header.tlvType());
this.setTlvLength(header.tlvLength());
}
/**
* Sets TE metric value.
*
* @param trafficEngineeringMetric value of trafficEngineeringMetric
*/
public void setTrafficEngineeringMetric(long trafficEngineeringMetric) {
this.trafficEngineeringMetric = trafficEngineeringMetric;
}
/**
* Gets TE metric value.
*
* @return value of traffic engineering metric
*/
public long getTrafficEngineeringMetricValue() {
return this.trafficEngineeringMetric;
}
/**
* Reads bytes from channel buffer .
*
* @param channelBuffer channel buffer instance
*/
public void readFrom(ChannelBuffer channelBuffer) {
byte[] tempByteArray = new byte[tlvLength()];
channelBuffer.readBytes(tempByteArray, 0, tlvLength());
this.setTrafficEngineeringMetric(IsisUtil.byteToLong(tempByteArray));
}
/**
* Gets instance as byte array.
*
* @return instance as byte array
*/
public byte[] asBytes() {
byte[] linkSubType = null;
byte[] linkSubTlvHeader = tlvHeaderAsByteArray();
byte[] linkSubTlvBody = tlvBodyAsBytes();
linkSubType = Bytes.concat(linkSubTlvHeader, linkSubTlvBody);
return linkSubType;
}
/**
* Gets trafficEngineeringMetric as byte array .
*
* @return byte array of trafficEngineeringMetric
*/
public byte[] tlvBodyAsBytes() {
byte[] linkSubTypeBody;
linkSubTypeBody = IsisUtil.convertToFourBytes(this.trafficEngineeringMetric);
return linkSubTypeBody;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.omitNullValues()
.add("trafficEngineeringMetric", trafficEngineeringMetric)
.toString();
}
}
\ No newline at end of file
......@@ -14,28 +14,10 @@
* limitations under the License.
*/
package org.onosproject.isis.io.isispacket;
package org.onosproject.isis.io.isispacket.tlv.subtlv;
/**
* Represents ISIS constant parameters.
* Representation of traffic engineering sub tlv.
*/
public final class IsisConstantParameters {
public static final int IRPDISCRIMINATOR = 131;
public static final int PROOCOLID = 1;
public static final int VERSION = 1;
public static final int RESERVED = 0;
public static final int MAXAREAADDRESS = 3;
public static final int IDLENGTH = 6;
public static final int PROTOCOLSUPPORTED = 6;
public static final int PACKETMINIMUMLENGTH = 27;
public static final int PDULENGTHPOSITION = 17;
public static final int PDUHEADERFORREADFROM = 8;
/**
* Creates an instance of this class.
*/
private IsisConstantParameters() {
}
}
public interface TrafficEngineeringSubTlv {
}
\ No newline at end of file
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.isis.io.isispacket.tlv.subtlv;
import com.google.common.base.MoreObjects;
import com.google.common.primitives.Bytes;
import org.jboss.netty.buffer.ChannelBuffer;
import org.onosproject.isis.io.isispacket.tlv.TlvHeader;
import org.onosproject.isis.io.util.IsisUtil;
import java.util.ArrayList;
import java.util.List;
/**
* Representation of an unreserved band width TE value.
*/
public class UnreservedBandwidth extends TlvHeader implements TrafficEngineeringSubTlv {
private List<Float> unReservedBandwidth = new ArrayList<>();
/**
* Creates an instance of unreserved band width.
*
* @param header tlv header instance
*/
public UnreservedBandwidth(TlvHeader header) {
this.setTlvType(header.tlvType());
this.setTlvLength(header.tlvLength());
}
/**
* Adds value of un reserved bandwidth .
*
* @param unreservedBandwidth value of un reserved bandwidth
*/
public void addUnReservedBandwidth(float unreservedBandwidth) {
this.unReservedBandwidth.add(unreservedBandwidth);
}
/**
* Gets list of un reserved bandwidth .
*
* @return List of un reserved bandwidth
*/
public List<Float> getUnReservedBandwidthValue() {
return this.unReservedBandwidth;
}
/**
* Reads bytes from channel buffer .
*
* @param channelBuffer channel buffer instance
*/
public void readFrom(ChannelBuffer channelBuffer) {
while (channelBuffer.readableBytes() >= IsisUtil.FOUR_BYTES) {
int maxReversibleBandwidth = channelBuffer.readInt();
this.addUnReservedBandwidth(Float.intBitsToFloat(maxReversibleBandwidth));
}
}
/**
* Gets instance as byte array.
*
* @return instance as byte array
*/
public byte[] asBytes() {
byte[] linkSubType = null;
byte[] linkSubTlvHeader = tlvHeaderAsByteArray();
byte[] linkSubTlvBody = tlvBodyAsBytes();
linkSubType = Bytes.concat(linkSubTlvHeader, linkSubTlvBody);
return linkSubType;
}
/**
* Gets unreserved bandwidth as byte array.
*
* @return unreserved bandwidth as byte array
*/
public byte[] tlvBodyAsBytes() {
List<Byte> linkSubTypeBody = new ArrayList<>();
if (this.unReservedBandwidth.size() < 8) {
int size = IsisUtil.EIGHT_BYTES - this.unReservedBandwidth.size();
for (int i = 0; i < size; i++) {
linkSubTypeBody.addAll(Bytes.asList(IsisUtil.convertToFourBytes(IsisUtil.INITIAL_BANDWIDTH)));
}
}
for (Float unreservedBandwidth : this.unReservedBandwidth) {
int unresBandwidth = Float.floatToIntBits(unreservedBandwidth);
linkSubTypeBody.addAll(Bytes.asList(IsisUtil.convertToFourBytes(unresBandwidth)));
}
return Bytes.toArray(linkSubTypeBody);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.omitNullValues()
.add("unReservedBandwidth", unReservedBandwidth)
.toString();
}
}
\ No newline at end of file
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Implementation of the ISIS protocol.
*/
package org.onosproject.isis.io.isispacket.tlv.subtlv;
\ No newline at end of file
......@@ -15,6 +15,6 @@
*/
/**
* Implementation of the ISIS protocol.
* Implementation of the ISIS protocol IO.
*/
package org.onosproject.isis.io;
\ No newline at end of file
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.isis.io.util;
import java.util.Arrays;
/**
* Calculates checksum for ISIS LSP packets.
*/
public class ChecksumCalculator {
/**
* Verifies the checksum is valid in given LSP packet bytes.
*
* @param lspPacket lsp as byte array
* @param lspChecksumPos1 position of checksum bit in packet
* @param lspChecksumPos2 position of checksum bit in packet
* @return true if valid else false
*/
public boolean validateLspCheckSum(byte[] lspPacket, int lspChecksumPos1, int lspChecksumPos2) {
byte[] checksum = calculateLspChecksum(lspPacket, lspChecksumPos1, lspChecksumPos2);
if (lspPacket[lspChecksumPos1] == checksum[0] && lspPacket[lspChecksumPos2] == checksum[1]) {
return true;
}
return false;
}
/**
* Calculates the LSP checksum.
*
* @param lspBytes as byte array
* @param lspChecksumPos1 position of checksum bit in packet
* @param lspChecksumPos2 position of checksum bit in packet
* @return checksum bytes
*/
public byte[] calculateLspChecksum(byte[] lspBytes, int lspChecksumPos1, int lspChecksumPos2) {
byte[] tempLsaByte = Arrays.copyOf(lspBytes, lspBytes.length);
int[] checksumOut = {0, 0};
tempLsaByte[lspChecksumPos1] = 0;
tempLsaByte[lspChecksumPos2] = 0;
byte[] byteCheckSum = {0, 0};
if (lspBytes != null) {
for (int i = 12; i < tempLsaByte.length; i++) {
checksumOut[0] = checksumOut[0] + ((int) tempLsaByte[i] & 0xFF);
checksumOut[1] = checksumOut[1] + checksumOut[0];
}
checksumOut[0] = checksumOut[0] % 255;
checksumOut[1] = checksumOut[1] % 255;
}
int byte1 = (int) ((tempLsaByte.length - lspChecksumPos1 - 1) * checksumOut[0] - checksumOut[1]) % 255;
if (byte1 <= 0) {
byte1 += 255;
}
int byte2 = 510 - checksumOut[0] - byte1;
if (byte2 > 255) {
byte2 -= 255;
}
byteCheckSum[0] = (byte) byte1;
byteCheckSum[1] = (byte) byte2;
return byteCheckSum;
}
}
\ No newline at end of file
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.isis.io.util;
/**
* Representation of ISIS constants.
*/
public final class IsisConstants {
public static final char PDU_LENGTH = 1497; // mtu (1500) - (3) LLC
public static final int MINIMUM_FRAME_LEN = 1521;
public static final int METADATA_LEN = 7;
public static final String SHOST = "127.0.0.1";
public static final int SPORT = 3000;
public static final byte L2 = 1;
public static final int IRPDISCRIMINATOR = 131;
public static final int ISISVERSION = 1;
public static final int RESERVED = 0;
public static final int MAXAREAADDRESS = 0;
public static final int IDLENGTH = 0;
public static final int PROTOCOLSUPPORTED = 204;
public static final int LOCALCIRCUITIDFORP2P = 130;
public static final int P2PHELLOHEADERLENGTH = 20;
public static final int HELLOHEADERLENGTH = 27;
public static final int CSNPDUHEADERLENGTH = 33;
public static final int PSNPDUHEADERLENGTH = 17;
public static final int PDULENGTHPOSITION = 17;
public static final int COMMONHEADERLENGTH = 8;
public static final int LSPMAXAGE = 1200;
public static final int LSPREFRESH = 900;
public static final int MAXSEQUENCENUMBER = Integer.MAX_VALUE;
public static final int STARTLSSEQUENCENUM = 1;
public static final int LENGTHPOSITION = 8;
public static final int RESERVEDPOSITION = 6;
public static final int CHECKSUMPOSITION = 24;
public static final String REFRESHLSP = "refreshLsp";
public static final String MAXAGELSP = "maxAgeLsp";
/**
* Non parameterized constructor.
*/
private IsisConstants() {
}
}
......@@ -15,11 +15,13 @@
*/
package org.onosproject.isis.io.util;
import com.google.common.base.MoreObjects;
import com.google.common.primitives.Bytes;
import org.onlab.packet.Ip4Address;
import org.onosproject.isis.io.isispacket.tlv.PaddingTlv;
import org.onosproject.isis.io.isispacket.tlv.TlvHeader;
import org.onosproject.isis.io.isispacket.tlv.TlvType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.xml.bind.DatatypeConverter;
import java.util.ArrayList;
......@@ -27,56 +29,57 @@ import java.util.List;
import java.util.StringTokenizer;
/**
* Represents ISIS utils.
* Representation of ISIS utils.
*/
public final class IsisUtil {
public static final int AREAADDRESS = 1;
public static final int ISREACHABILITY = 2;
public static final int ISNEIGHBORS = 6;
public static final int PADDING = 8;
public static final int LSPENTRY = 9;
public static final int AUTHENTICATION = 10;
public static final int CHECKSUM = 12;
public static final int EXTENDEDISREACHABILITY = 22;
public static final int ISALIAS = 24;
public static final int IPINTERNALREACHABILITY = 128;
public static final int PROTOCOLSUPPORTED = 129;
public static final int IPEXTERNALREACHABILITY = 130;
public static final int IDRPINFORMATION = 131;
public static final int IPINTERFACEADDRESS = 132;
public static final int L1HELLOPDU = 1;
public static final int L2HELLOPDU = 2;
public static final int P2PHELLOPDU = 3;
public static final int L1LSPDU = 18;
public static final int L2LSPDU = 20;
public static final int L1CSNP = 24;
public static final int L2CSNP = 25;
public static final int L1PSNP = 26;
public static final int L2PSNP = 27;
public static final int L1L2_LS_PDUHEADERLENGTH = 27;
public static final int P2PPDUHEADERLENGTH = 20;
public static final int PSNPPDUHEADERLENGTH = 17;
public static final char ETHER_FRAME_LEN = 1514;
public static final int ETHER_HEADER_LEN = 17;
public static final int ID_SIX_BYTES = 6;
public static final int ID_PLUS_ONE_BYTE = 7;
public static final int ID_PLUS_TWO_BYTE = 8;
public static final int THREE_BYTES = 3;
public static final int SIX_BYTES = 6;
public static final int EIGHT_BYTES = 8;
public static final int FOUR_BYTES = 4;
public static final int PADDING_FIXED_LENGTH = 255;
public static final int TLVHEADERLENGTH = 2;
public static final int INITIAL_BANDWIDTH = 12500000;
private static final Logger log = LoggerFactory.getLogger(IsisUtil.class);
/**
* Creates an instance of this class.
* Creates an instance.
*/
private IsisUtil() {
}
/**
* Checks given IPs are in same network or not.
*
* @param ip1 IP address
* @param ip2 IP address
* @param mask network mask
* @return true if both are in same network else false
*/
public static boolean sameNetwork(Ip4Address ip1, Ip4Address ip2, byte[] mask) {
try {
byte[] a1 = ip1.toOctets();
byte[] a2 = ip2.toOctets();
for (int i = 0; i < a1.length; i++) {
if ((a1[i] & mask[i]) != (a2[i] & mask[i])) {
return false;
}
}
} catch (Exception e) {
log.debug("Exception::IsisUtil::sameNetwork:: {}", e.getMessage());
}
return true;
}
/**
* Parse byte array to string system ID.
*
* @param bytes system ID
* @return systemId system ID.
* @return systemId system ID
*/
public static String systemId(byte[] bytes) {
String systemId = "";
......@@ -97,7 +100,7 @@ public final class IsisUtil {
* Parse byte array to LAN ID.
*
* @param bytes LAN ID
* @return systemId system ID.
* @return systemIdPlus system ID
*/
public static String systemIdPlus(byte[] bytes) {
String systemId = "";
......@@ -119,23 +122,18 @@ public final class IsisUtil {
* Parse byte array to area address.
*
* @param bytes area address
* @return areaAddress area address
* @return areaAddres area address
*/
public static String areaAddres(byte[] bytes) {
int count = 0;
String areaAddress = "";
String areaAddres = "";
for (Byte byt : bytes) {
String hexa = Integer.toHexString(Byte.toUnsignedInt(byt));
if (hexa.length() % 2 != 0) {
hexa = "0" + hexa;
}
if (count == 0) {
hexa = hexa + ".";
}
areaAddress = areaAddress + hexa;
count++;
areaAddres = areaAddres + hexa;
}
return areaAddress;
return areaAddres;
}
/**
......@@ -144,76 +142,98 @@ public final class IsisUtil {
* @param address area address
* @return areaAddress area address
*/
public static List<Byte> areaAddresToBytes(String address) {
List<Byte> idLst = new ArrayList();
StringTokenizer tokenizer = new StringTokenizer(address, ".");
int count = 0;
while (tokenizer.hasMoreElements()) {
String str = tokenizer.nextToken();
if (str.length() % 2 != 0) {
str = "0" + str;
}
if (count > 0) {
for (int i = 0; i < str.length(); i = i + 2) {
idLst.add((byte) Integer.parseInt(str.substring(i, i + 2), 16));
}
} else {
idLst.add((byte) Integer.parseInt(str, 16));
}
count++;
public static List<Byte> areaAddressToBytes(String address) {
List<Byte> idList = new ArrayList<>();
for (int i = 0; i < address.length(); i = i + 2) {
Character c1 = address.charAt(i);
Character c2 = address.charAt(i + 1);
String str = c1.toString() + c2.toString();
idList.add((byte) Integer.parseInt(str, 16));
}
return idLst;
return idList;
}
/**
* Adds the PDU length in packet.
*
* @param isisPacket ISIS packet
* @param lengthBytePos1 length byte position
* @param lengthBytePos2 length byte position
* @param reservedBytePos reserved byte position
* @return byte array with PDU length
*/
public static byte[] addLengthAndMarkItInReserved(byte[] isisPacket, int lengthBytePos1,
int lengthBytePos2, int reservedBytePos) {
//Set the length of the packet
//Get the total length of the packet
int length = isisPacket.length;
//Convert the lenth to two bytes as the length field is 2 bytes
byte[] lenthInTwoBytes = IsisUtil.convertToTwoBytes(length);
//isis header 3rd and 4th position represents length
isisPacket[lengthBytePos1] = lenthInTwoBytes[0]; //assign 1st byte in lengthBytePos1
isisPacket[lengthBytePos2] = lenthInTwoBytes[1]; //assign 2st byte in lengthBytePos2
isisPacket[reservedBytePos] = (byte) lengthBytePos1;
return isisPacket;
}
/**
* Gets PDU header length.
* Adds the checksum in packet.
*
* @param pduType PDU type
* @return headerLength header length
* @param isisPacket ISIS packet
* @param checksumBytePos1 checksum byte position
* @param checksumBytePos2 checksum byte position
* @return byte array with PDU length
*/
public static int getPduHeaderLength(int pduType) {
int headerLength = 0;
switch (pduType) {
case L1HELLOPDU:
case L2HELLOPDU:
case L1LSPDU:
case L2LSPDU:
headerLength = L1L2_LS_PDUHEADERLENGTH;
break;
case P2PHELLOPDU:
headerLength = P2PPDUHEADERLENGTH;
break;
case L1PSNP:
case L2PSNP:
headerLength = PSNPPDUHEADERLENGTH;
break;
default:
break;
public static byte[] addChecksum(byte[] isisPacket, int checksumBytePos1, int checksumBytePos2) {
//Set the checksum for the packet
//Convert the lenth to two bytes as the length field is 2 bytes
byte[] checksumInTwoBytes = new ChecksumCalculator().calculateLspChecksum(
isisPacket, checksumBytePos1, checksumBytePos2);
//isis header 3rd and 4th position represents length
isisPacket[checksumBytePos1] = checksumInTwoBytes[0];
isisPacket[checksumBytePos2] = checksumInTwoBytes[1];
return isisPacket;
}
/**
* Adds frame a packet of 1498 of size.
*
* @param isisPacket ISIS packet
* @param interfaceIndex interface index
* @return byte array with 1498 is the length
*/
public static byte[] framePacket(byte[] isisPacket, int interfaceIndex) {
//Set the length of the packet
//Get the total length of the packet
int length = isisPacket.length;
//PDU_LENGTH + 1 byte for interface index
if (length < IsisConstants.PDU_LENGTH + 1) {
byte[] bytes = new byte[IsisConstants.PDU_LENGTH + 1];
System.arraycopy(isisPacket, 0, bytes, 0, length);
bytes[IsisConstants.PDU_LENGTH] = (byte) interfaceIndex;
return bytes;
}
return headerLength;
return isisPacket;
}
/**
* Parse source and LAN ID.
*
* @param id source and LAN ID
* @return sourceAndLanIdToBytes source and LAN ID
* @return source and LAN ID
*/
public static List<Byte> sourceAndLanIdToBytes(String id) {
List<Byte> idLst = new ArrayList();
StringTokenizer tokenizer = new StringTokenizer(id, ".");
List<Byte> idList = new ArrayList<>();
StringTokenizer tokenizer = new StringTokenizer(id, "." + "-");
while (tokenizer.hasMoreElements()) {
int i = 0;
String str = tokenizer.nextToken();
idLst.add((byte) Integer.parseInt(str.substring(0, i + 2), 16));
idList.add((byte) Integer.parseInt(str.substring(0, i + 2), 16));
if (str.length() > 2) {
idLst.add((byte) Integer.parseInt(str.substring(i + 2, str.length()), 16));
idList.add((byte) Integer.parseInt(str.substring(i + 2, str.length()), 16));
}
}
return idLst;
return idList;
}
/**
......@@ -222,20 +242,20 @@ public final class IsisUtil {
* @param currentLength current length
* @return byteArray padding array
*/
public static byte[] paddingForPdu(int currentLength) {
public static byte[] getPaddingTlvs(int currentLength) {
List<Byte> bytes = new ArrayList<>();
while (ETHER_FRAME_LEN > currentLength) {
int length = ETHER_FRAME_LEN - currentLength;
while (IsisConstants.PDU_LENGTH > currentLength) {
int length = IsisConstants.PDU_LENGTH - currentLength;
TlvHeader tlvHeader = new TlvHeader();
tlvHeader.setTlvType(PADDING);
tlvHeader.setTlvType(TlvType.PADDING.value());
if (length >= PADDING_FIXED_LENGTH) {
tlvHeader.setTlvLength(PADDING_FIXED_LENGTH);
} else {
tlvHeader.setTlvLength(ETHER_FRAME_LEN - currentLength);
tlvHeader.setTlvLength(IsisConstants.PDU_LENGTH - (currentLength + TLVHEADERLENGTH));
}
PaddingTlv tlv = new PaddingTlv(tlvHeader);
bytes.addAll(Bytes.asList(tlv.asBytes()));
currentLength = currentLength + tlv.tlvLength();
currentLength = currentLength + tlv.tlvLength() + TLVHEADERLENGTH;
}
byte[] byteArray = new byte[bytes.size()];
int i = 0;
......@@ -243,7 +263,6 @@ public final class IsisUtil {
byteArray[i++] = byt;
}
return byteArray;
}
/**
......@@ -308,10 +327,138 @@ public final class IsisUtil {
return numInBytes;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.omitNullValues()
.toString();
/**
* Converts a byte to integer variable.
*
* @param bytesToConvert bytes to convert
* @return integer representation of bytes
*/
public static int byteToInteger(byte[] bytesToConvert) {
final StringBuilder builder = new StringBuilder();
for (byte eachByte : bytesToConvert) {
builder.append(String.format("%02x", eachByte));
}
int number = Integer.parseInt(builder.toString(), 16);
return number;
}
/**
* Converts a byte to long variable.
*
* @param bytesToConvert bytes to convert
* @return long representation of bytes
*/
public static long byteToLong(byte[] bytesToConvert) {
final StringBuilder builder = new StringBuilder();
for (byte eachByte : bytesToConvert) {
builder.append(String.format("%02x", eachByte));
}
long number = Long.parseLong(builder.toString(), 16);
return number;
}
/**
* Converts a number to four bytes.
*
* @param numberToConvert number to convert
* @return numInBytes given number as bytes
*/
public static byte[] convertToFourBytes(long numberToConvert) {
byte[] numInBytes = new byte[4];
String s1 = Long.toHexString(numberToConvert);
if (s1.length() % 2 != 0) {
s1 = "0" + s1;
}
if (s1.length() == 16) {
s1 = s1.substring(8, s1.length());
}
byte[] hexas = DatatypeConverter.parseHexBinary(s1);
if (hexas.length == 1) {
numInBytes[0] = 0;
numInBytes[1] = 0;
numInBytes[2] = 0;
numInBytes[3] = hexas[0];
} else if (hexas.length == 2) {
numInBytes[0] = 0;
numInBytes[1] = 0;
numInBytes[2] = hexas[0];
numInBytes[3] = hexas[1];
} else if (hexas.length == 3) {
numInBytes[0] = 0;
numInBytes[1] = hexas[0];
numInBytes[2] = hexas[1];
numInBytes[3] = hexas[2];
} else {
numInBytes[0] = hexas[0];
numInBytes[1] = hexas[1];
numInBytes[2] = hexas[2];
numInBytes[3] = hexas[3];
}
return numInBytes;
}
/**
* Converts a number to eight bit binary.
*
* @param binaryString string to binary
* @return numInBytes given number as bytes
*/
public static String toEightBitBinary(String binaryString) {
String eightBit = binaryString;
if (eightBit.length() % 8 != 0) {
int numOfZero = 8 - eightBit.length();
while (numOfZero > 0) {
eightBit = "0" + eightBit;
numOfZero--;
}
}
return eightBit;
}
/**
* Converts a number to four bit binary.
*
* @param binaryString string to binary
* @return numInBytes given number as bytes
*/
public static String toFourBitBinary(String binaryString) {
String fourBit = binaryString;
if (fourBit.length() % 4 != 0) {
int numOfZero = 4 - fourBit.length();
while (numOfZero > 0) {
fourBit = "0" + fourBit;
numOfZero--;
}
}
return fourBit;
}
/**
* Converts a number to three bytes.
*
* @param numberToConvert number to convert
* @return given number as bytes
*/
public static byte[] convertToThreeBytes(int numberToConvert) {
byte[] numInBytes = new byte[4];
String s1 = Integer.toHexString(numberToConvert);
if (s1.length() % 2 != 0) {
s1 = "0" + s1;
}
byte[] hexas = DatatypeConverter.parseHexBinary(s1);
if (hexas.length == 1) {
numInBytes[0] = 0;
numInBytes[1] = 0;
numInBytes[2] = hexas[0];
} else if (hexas.length == 2) {
numInBytes[0] = 0;
numInBytes[1] = hexas[0];
numInBytes[2] = hexas[1];
} else {
numInBytes[0] = hexas[0];
numInBytes[1] = hexas[1];
numInBytes[2] = hexas[2];
}
return numInBytes;
}
}
\ No newline at end of file
}
......
......@@ -15,6 +15,6 @@
*/
/**
* Implementation of the ISIS protocol.
* Implementation of the ISIS protocol util.
*/
package org.onosproject.isis.io.util;
\ No newline at end of file
......