mohamed rahil
Committed by Jonathan Hart

ONOS-4082: Packet structures- TLV packet structures

Change-Id: Id67867d215e9e6215854bc35fc435a1b0da9bf3b
Showing 19 changed files with 1295 additions and 0 deletions
1 +<!--
2 + ~ Copyright 2016 Open Networking Laboratory
3 + ~
4 + ~ Licensed under the Apache License, Version 2.0 (the "License");
5 + ~ you may not use this file except in compliance with the License.
6 + ~ You may obtain a copy of the License at
7 + ~
8 + ~ http://www.apache.org/licenses/LICENSE-2.0
9 + ~
10 + ~ Unless required by applicable law or agreed to in writing, software
11 + ~ distributed under the License is distributed on an "AS IS" BASIS,
12 + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + ~ See the License for the specific language governing permissions and
14 + ~ limitations under the License.
15 + -->
16 +<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
17 + xmlns="http://maven.apache.org/POM/4.0.0"
18 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
19 + <modelVersion>4.0.0</modelVersion>
20 +
21 + <parent>
22 + <groupId>org.onosproject</groupId>
23 + <artifactId>onos-isis</artifactId>
24 + <version>1.6.0-SNAPSHOT</version>
25 + <relativePath>../pom.xml</relativePath>
26 + </parent>
27 +
28 + <artifactId>onos-isis-isisio</artifactId>
29 + <packaging>bundle</packaging>
30 +
31 + <description>ONOS ISIS controller protocol</description>
32 + <dependencies>
33 + <dependency>
34 + <groupId>org.onosproject</groupId>
35 + <artifactId>onos-isis-api</artifactId>
36 + <version>${project.version}</version>
37 + </dependency>
38 + <dependency>
39 + <groupId>io.netty</groupId>
40 + <artifactId>netty-buffer</artifactId>
41 + </dependency>
42 + <dependency>
43 + <groupId>org.easymock</groupId>
44 + <artifactId>easymock</artifactId>
45 + <scope>test</scope>
46 + </dependency>
47 + </dependencies>
48 +
49 +</project>
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +package org.onosproject.isis.io.isispacket;
18 +
19 +/**
20 + * Represents ISIS constant parameters.
21 + */
22 +public final class IsisConstantParameters {
23 +
24 + public static final int IRPDISCRIMINATOR = 131;
25 + public static final int PROOCOLID = 1;
26 + public static final int VERSION = 1;
27 + public static final int RESERVED = 0;
28 + public static final int MAXAREAADDRESS = 3;
29 + public static final int IDLENGTH = 6;
30 + public static final int PROTOCOLSUPPORTED = 6;
31 + public static final int PACKETMINIMUMLENGTH = 27;
32 + public static final int PDULENGTHPOSITION = 17;
33 + public static final int PDUHEADERFORREADFROM = 8;
34 +
35 + /**
36 + * Creates an instance of this class.
37 + */
38 + private IsisConstantParameters() {
39 +
40 + }
41 +}
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +/**
18 + * Implementation of the ISIS protocol.
19 + */
20 +package org.onosproject.isis.io.isispacket;
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +package org.onosproject.isis.io.isispacket.tlv;
18 +
19 +import com.google.common.base.MoreObjects;
20 +import com.google.common.primitives.Bytes;
21 +import io.netty.buffer.ByteBuf;
22 +import org.onosproject.isis.io.util.IsisUtil;
23 +
24 +import java.util.ArrayList;
25 +import java.util.List;
26 +
27 +/**
28 + * Represents the area address TLV.
29 + */
30 +public class AreaAddressTlv extends TlvHeader implements IsisTlv {
31 +
32 + private List<String> areaAddress = new ArrayList();
33 +
34 + /**
35 + * Sets TLV type and TLV length of area address TLV.
36 + *
37 + * @param tlvHeader tlvHeader.
38 + */
39 + public AreaAddressTlv(TlvHeader tlvHeader) {
40 +
41 + this.setTlvType(tlvHeader.tlvType());
42 + this.setTlvLength(tlvHeader.tlvLength());
43 +
44 + }
45 +
46 + /**
47 + * Gets the area address of area address TLV.
48 + *
49 + * @return area address
50 + */
51 + public List<String> areaAddress() {
52 + return this.areaAddress;
53 + }
54 +
55 + @Override
56 + public void readFrom(ByteBuf byteBuf) {
57 + while (byteBuf.readableBytes() > 0) {
58 + int addressLength = byteBuf.readByte();
59 + byte[] addressBytes = new byte[IsisUtil.THREE_BYTES];
60 + byteBuf.readBytes(addressBytes, 0, IsisUtil.THREE_BYTES);
61 + String areaAddress = IsisUtil.areaAddres(addressBytes);
62 + this.areaAddress.add(areaAddress);
63 + }
64 + }
65 +
66 + @Override
67 + public byte[] asBytes() {
68 + byte[] bytes = null;
69 +
70 + byte[] tlvHeader = tlvHeaderAsByteArray();
71 + byte[] tlvBody = tlvBodyAsBytes();
72 + bytes = Bytes.concat(tlvHeader, tlvBody);
73 +
74 + return bytes;
75 + }
76 +
77 + /**
78 + * Gets TLV body of area address TLV.
79 + *
80 + * @return byteArray TLV body of area address TLV
81 + */
82 + public byte[] tlvBodyAsBytes() {
83 +
84 + List<Byte> bytes = new ArrayList();
85 + for (String areaAddress : this.areaAddress) {
86 + bytes.add((byte) (areaAddress.length() / 2));
87 + bytes.addAll(IsisUtil.areaAddresToBytes(areaAddress));
88 + }
89 + byte[] byteArray = new byte[bytes.size()];
90 + int i = 0;
91 + for (byte byt : bytes) {
92 + byteArray[i++] = byt;
93 + }
94 + return byteArray;
95 + }
96 +
97 + @Override
98 + public String toString() {
99 + return MoreObjects.toStringHelper(getClass())
100 + .omitNullValues()
101 + .toString();
102 + }
103 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.isis.io.isispacket.tlv;
17 +
18 +import com.google.common.base.MoreObjects;
19 +import com.google.common.primitives.Bytes;
20 +import io.netty.buffer.ByteBuf;
21 +import org.onlab.packet.Ip4Address;
22 +import org.onosproject.isis.io.util.IsisUtil;
23 +
24 +import java.util.ArrayList;
25 +import java.util.List;
26 +
27 +/**
28 + * Represents IP interface address TLV.
29 + */
30 +public class IpInterfaceAddressTlv extends TlvHeader implements IsisTlv {
31 + private List<Ip4Address> interfaceAddress = new ArrayList();
32 +
33 + /**
34 + * Sets TLV type and TLV length of IP interface address TLV.
35 + *
36 + * @param tlvHeader tlvHeader.
37 + */
38 + public IpInterfaceAddressTlv(TlvHeader tlvHeader) {
39 +
40 + this.setTlvType(tlvHeader.tlvType());
41 + this.setTlvLength(tlvHeader.tlvLength());
42 +
43 + }
44 +
45 + /**
46 + * Gets interface address of interface address TLV.
47 + *
48 + * @return interfaceAddress interface address
49 + */
50 + public List<Ip4Address> interfaceAddress() {
51 + return interfaceAddress;
52 + }
53 +
54 + @Override
55 + public void readFrom(ByteBuf byteBuf) {
56 + while (byteBuf.readableBytes() >= 4) {
57 + byte[] addressbytes = new byte[IsisUtil.FOUR_BYTES];
58 + byteBuf.readBytes(addressbytes, 0, IsisUtil.FOUR_BYTES);
59 + this.interfaceAddress.add(Ip4Address.valueOf(addressbytes));
60 + }
61 +
62 + }
63 +
64 + @Override
65 + public byte[] asBytes() {
66 + byte[] bytes = null;
67 +
68 + byte[] tlvHeader = tlvHeaderAsByteArray();
69 + byte[] tlvBody = tlvBodyAsBytes();
70 + bytes = Bytes.concat(tlvHeader, tlvBody);
71 +
72 + return bytes;
73 + }
74 +
75 + /**
76 + * Gets TLV body of interface address TLV.
77 + *
78 + * @return byteArray TLV body of interface address TLV.
79 + */
80 + public byte[] tlvBodyAsBytes() {
81 +
82 + List<Byte> bytes = new ArrayList();
83 + for (Ip4Address ip4Address : this.interfaceAddress) {
84 + bytes.addAll(Bytes.asList(ip4Address.toOctets()));
85 + }
86 + byte[] byteArray = new byte[bytes.size()];
87 + int i = 0;
88 + for (byte byt : bytes) {
89 + byteArray[i++] = byt;
90 + }
91 + return byteArray;
92 + }
93 +
94 + @Override
95 + public String toString() {
96 + return MoreObjects.toStringHelper(getClass())
97 + .omitNullValues()
98 + .toString();
99 + }
100 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.isis.io.isispacket.tlv;
17 +
18 +import com.google.common.base.MoreObjects;
19 +import com.google.common.primitives.Bytes;
20 +import io.netty.buffer.ByteBuf;
21 +import org.onlab.packet.MacAddress;
22 +import org.onosproject.isis.io.util.IsisUtil;
23 +
24 +import java.util.ArrayList;
25 +import java.util.List;
26 +
27 +/**
28 + * Represents ISIS neighbor TLV.
29 + */
30 +public class IsisNeighborTlv extends TlvHeader implements IsisTlv {
31 +
32 + private List<MacAddress> neighbor = new ArrayList();
33 +
34 + /**
35 + * Sets TLV type and TLV length of ISIS neighbor TLV.
36 + *
37 + * @param tlvHeader tlvHeader.
38 + */
39 + public IsisNeighborTlv(TlvHeader tlvHeader) {
40 +
41 + this.setTlvType(tlvHeader.tlvType());
42 + this.setTlvLength(tlvHeader.tlvLength());
43 +
44 + }
45 +
46 + /**
47 + * Gets the MAC address of the neighbor TLV.
48 + *
49 + * @return neighbor MAC address of the neighbor TLV
50 + */
51 + public List<MacAddress> neighbor() {
52 + return this.neighbor;
53 + }
54 +
55 + @Override
56 + public void readFrom(ByteBuf byteBuf) {
57 + while (byteBuf.readableBytes() >= 6) {
58 + byte[] addressbytes = new byte[IsisUtil.SIX_BYTES];
59 + byteBuf.readBytes(addressbytes, 0, IsisUtil.SIX_BYTES);
60 + this.neighbor.add(MacAddress.valueOf(addressbytes));
61 + }
62 +
63 + }
64 +
65 + @Override
66 + public byte[] asBytes() {
67 + byte[] bytes = null;
68 +
69 + byte[] tlvHeader = tlvHeaderAsByteArray();
70 + byte[] tlvBody = tlvBodyAsBytes();
71 + bytes = Bytes.concat(tlvHeader, tlvBody);
72 +
73 + return bytes;
74 +
75 + }
76 +
77 + /**
78 + * Gets TLV body of neighbor TLV.
79 + *
80 + * @return byteArray TLV body of neighbor TLV
81 + */
82 + public byte[] tlvBodyAsBytes() {
83 +
84 + List<Byte> bytes = new ArrayList();
85 + for (MacAddress macAddress : this.neighbor) {
86 + bytes.addAll(Bytes.asList(macAddress.toBytes()));
87 + }
88 + byte[] byteArray = new byte[bytes.size()];
89 + int i = 0;
90 + for (byte byt : bytes) {
91 + byteArray[i++] = byt;
92 + }
93 + return byteArray;
94 + }
95 +
96 + @Override
97 + public String toString() {
98 + return MoreObjects.toStringHelper(getClass())
99 + .omitNullValues()
100 + .toString();
101 + }
102 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.isis.io.isispacket.tlv;
17 +
18 +/**
19 + * Represents ISIS TLV.
20 + */
21 +public interface IsisTlv {
22 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.isis.io.isispacket.tlv;
17 +
18 +import com.google.common.base.MoreObjects;
19 +import com.google.common.primitives.Bytes;
20 +import io.netty.buffer.ByteBuf;
21 +
22 +import java.util.ArrayList;
23 +import java.util.List;
24 +
25 +/**
26 + * Represents padding TLV.
27 + */
28 +public class PaddingTlv extends TlvHeader implements IsisTlv {
29 + private List<Byte> paddings = new ArrayList();
30 +
31 + /**
32 + * Sets TLV type and TLV length of padding TLV.
33 + *
34 + * @param tlvHeader tlvHeader.
35 + */
36 + public PaddingTlv(TlvHeader tlvHeader) {
37 + this.setTlvType(tlvHeader.tlvType());
38 + this.setTlvLength(tlvHeader.tlvLength());
39 + }
40 +
41 + @Override
42 + public void readFrom(ByteBuf byteBuf) {
43 + while (byteBuf.readableBytes() > 0) {
44 + this.paddings.add(byteBuf.readByte());
45 + }
46 + }
47 +
48 + @Override
49 + public byte[] asBytes() {
50 + byte[] bytes = null;
51 +
52 + byte[] tlvHeader = tlvHeaderAsByteArray();
53 + byte[] tlvBody = tlvBodyAsBytes();
54 + bytes = Bytes.concat(tlvHeader, tlvBody);
55 +
56 + return bytes;
57 + }
58 +
59 + /**
60 + * Gets TLV body padding TLV.
61 + *
62 + * @return areaArea TLV body padding TLV\\
63 + */
64 + public byte[] tlvBodyAsBytes() {
65 + byte[] areaArea = new byte[this.tlvLength()];
66 + return areaArea;
67 + }
68 +
69 + @Override
70 + public String toString() {
71 + return MoreObjects.toStringHelper(getClass())
72 + .omitNullValues()
73 + .toString();
74 + }
75 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.isis.io.isispacket.tlv;
17 +
18 +import com.google.common.primitives.Bytes;
19 +import io.netty.buffer.ByteBuf;
20 +
21 +import java.util.ArrayList;
22 +import java.util.List;
23 +
24 +/**
25 + * Represents Protocol supported TLV.
26 + */
27 +public class ProtocolSupportedTlv extends TlvHeader implements IsisTlv {
28 +
29 + private List<Byte> protocolSupported = new ArrayList();
30 +
31 + /**
32 + * Sets TLV type and TLV length of protocol supported TLV.
33 + *
34 + * @param tlvHeader tlvHeader.
35 + */
36 + public ProtocolSupportedTlv(TlvHeader tlvHeader) {
37 +
38 + this.setTlvType(tlvHeader.tlvType());
39 + this.setTlvLength(tlvHeader.tlvLength());
40 +
41 + }
42 +
43 + /**
44 + * Gets the Protocol Supported by the TLV.
45 + *
46 + * @return Protocol Supported
47 + */
48 + public List<Byte> protocolSupported() {
49 +
50 + return this.protocolSupported;
51 +
52 + }
53 +
54 + @Override
55 + public void readFrom(ByteBuf byteBuf) {
56 +
57 + while (byteBuf.readableBytes() > 0) {
58 + this.protocolSupported.add(byteBuf.readByte());
59 + }
60 + }
61 +
62 + @Override
63 + public byte[] asBytes() {
64 + byte[] bytes = null;
65 +
66 + byte[] tlvHeader = tlvHeaderAsByteArray();
67 + byte[] tlvBody = tlvBodyAsBytes();
68 + bytes = Bytes.concat(tlvHeader, tlvBody);
69 +
70 + return bytes;
71 + }
72 +
73 + /**
74 + * Gets TLV body of protocol supported TLV.
75 + *
76 + * @return byteArray TLV body of protocol supported TLV
77 + */
78 + public byte[] tlvBodyAsBytes() {
79 +
80 + List<Byte> bytes = new ArrayList();
81 + for (byte byt : this.protocolSupported) {
82 + bytes.add(byt);
83 + }
84 + byte[] byteArray = new byte[bytes.size()];
85 + int i = 0;
86 + for (byte byt : bytes) {
87 + byteArray[i++] = byt;
88 + }
89 + return byteArray;
90 + }
91 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.isis.io.isispacket.tlv;
17 +
18 +import io.netty.buffer.ByteBuf;
19 +import org.onosproject.isis.io.util.IsisUtil;
20 +
21 +/**
22 + * Represents TLV finder.
23 + */
24 +public class TlvFinder extends TlvHeader {
25 +
26 + /**
27 + * Sets the value for TLV header and the body of the TLV.
28 + *
29 + * @param tlvHeader tlvHeader
30 + * @param byteBuf byteBuf
31 + * @return isisTlv ISIS TLV
32 + */
33 + public static IsisTlv findTlv(TlvHeader tlvHeader, ByteBuf byteBuf) {
34 +
35 + IsisTlv isisTlv = null;
36 +
37 + switch (tlvHeader.tlvType()) {
38 + case IsisUtil.AREAADDRESS:
39 + AreaAddressTlv areaAddressTlv = new AreaAddressTlv(tlvHeader);
40 + areaAddressTlv.readFrom(byteBuf);
41 + isisTlv = areaAddressTlv;
42 + break;
43 + case IsisUtil.IPINTERFACEADDRESS:
44 + IpInterfaceAddressTlv ipTlv = new IpInterfaceAddressTlv(tlvHeader);
45 + ipTlv.readFrom(byteBuf);
46 + isisTlv = ipTlv;
47 + break;
48 + case IsisUtil.PROTOCOLSUPPORTED:
49 + ProtocolSupportedTlv psTlv = new ProtocolSupportedTlv(tlvHeader);
50 + psTlv.readFrom(byteBuf);
51 + isisTlv = psTlv;
52 + break;
53 + case IsisUtil.ISNEIGHBORS:
54 + IsisNeighborTlv isisNeighborTlv = new IsisNeighborTlv(tlvHeader);
55 + isisNeighborTlv.readFrom(byteBuf);
56 + isisTlv = isisNeighborTlv;
57 + break;
58 + case IsisUtil.PADDING:
59 + PaddingTlv paddingTlv = new PaddingTlv(tlvHeader);
60 + paddingTlv.readFrom(byteBuf);
61 + isisTlv = paddingTlv;
62 + break;
63 + default:
64 + break;
65 + }
66 +
67 + return isisTlv;
68 + }
69 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.isis.io.isispacket.tlv;
17 +
18 +import com.google.common.base.MoreObjects;
19 +import com.google.common.primitives.Bytes;
20 +import io.netty.buffer.ByteBuf;
21 +
22 +import java.util.ArrayList;
23 +import java.util.List;
24 +
25 +/**
26 + * Represents TLV header.
27 + */
28 +public class TlvHeader implements IsisTlv {
29 + private int tlvType;
30 + private int tlvLength;
31 +
32 + /**
33 + * Gets the TLV length of the TLV.
34 + *
35 + * @return tlvLength TLV length
36 + */
37 + public int tlvLength() {
38 + return tlvLength;
39 + }
40 +
41 + /**
42 + * Sets the TLV length for the mTLV.
43 + *
44 + * @param tlvLength TLV length
45 + */
46 + public void setTlvLength(int tlvLength) {
47 + this.tlvLength = tlvLength;
48 + }
49 +
50 + /**
51 + * Gets the TLV type of the TLV.
52 + *
53 + * @return tlvType TLV type
54 + */
55 + public int tlvType() {
56 + return tlvType;
57 + }
58 +
59 + /**
60 + * Sets TLV type for the TLV.
61 + *
62 + * @param tlvType TLV type
63 + */
64 + public void setTlvType(int tlvType) {
65 + this.tlvType = tlvType;
66 + }
67 +
68 + /**
69 + * Sets the TLV values of TLV from b yte buffer.
70 + *
71 + * @param byteBuf byteBuf.
72 + */
73 + public void readFrom(ByteBuf byteBuf) {
74 + //implemented in sub classes
75 + }
76 +
77 +
78 + /**
79 + * Gets the TLV of the TLV as bytes.
80 + *
81 + * @return null
82 + */
83 + public byte[] asBytes() {
84 + //implemented the subclasses
85 + return null;
86 + }
87 +
88 + /**
89 + * Gets the TLV header of the TLV.
90 + *
91 + * @return headerLst TLV of the TLV
92 + */
93 + public byte[] tlvHeaderAsByteArray() {
94 + List<Byte> headerLst = new ArrayList();
95 + headerLst.add((byte) this.tlvType);
96 + headerLst.add((byte) this.tlvLength);
97 + return Bytes.toArray(headerLst);
98 + }
99 +
100 + @Override
101 + public String toString() {
102 + return MoreObjects.toStringHelper(getClass())
103 + .omitNullValues()
104 + .add("tlvType", tlvType)
105 + .add("tlvLength", tlvLength)
106 + .toString();
107 + }
108 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.isis.io.isispacket.tlv;
17 +
18 +/**
19 + * Represents various values for TLV types.
20 + */
21 +public enum TlvType {
22 + AREAADDRESS(1),
23 + ISREACHABILITY(2),
24 + ISNEIGHBORS(6),
25 + PADDING(8),
26 + LSPENTRY(9),
27 + AUTHENTICATION(10),
28 + CHECKSUM(12),
29 + EXTENDEDISREACHABILITY(22),
30 + ISALIAS(24),
31 + IPINTERNALREACHABILITY(128),
32 + PROTOCOLSUPPORTED(129),
33 + IPEXTERNALREACHABILITY(130),
34 + IDRPINFORMATION(131),
35 + IPINTERFACEADDRESS(132);
36 +
37 + private int value;
38 +
39 + /**
40 + * Sets the TLV type value.
41 + * @param value value.
42 + */
43 + TlvType(int value) {
44 + this.value = value;
45 + }
46 +
47 + /**
48 + * Gets value.
49 + * @return value
50 + */
51 + public int value() {
52 + return value;
53 + }
54 +}
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.isis.io.isispacket.tlv;
17 +
18 +import com.google.common.primitives.Bytes;
19 +
20 +import java.util.ArrayList;
21 +import java.util.List;
22 +
23 +/**
24 + * Represents conversion of TLV's to bytes.
25 + */
26 +public final class TlvsToBytes {
27 + /**
28 + * Sets the ISIS TLV and returns in the form of bytes.
29 + *
30 + * @param isisTlv isisTlv
31 + * @return tlvBytes TLV bytes
32 + */
33 + public static List<Byte> tlvToBytes(IsisTlv isisTlv) {
34 +
35 + List<Byte> tlvBytes = new ArrayList();
36 + if (isisTlv instanceof AreaAddressTlv) {
37 + AreaAddressTlv areaAddressTlv = (AreaAddressTlv) isisTlv;
38 + tlvBytes.addAll(Bytes.asList(areaAddressTlv.asBytes()));
39 + } else if (isisTlv instanceof IpInterfaceAddressTlv) {
40 + IpInterfaceAddressTlv ipInterfaceAddressTlv = (IpInterfaceAddressTlv) isisTlv;
41 + tlvBytes.addAll(Bytes.asList(ipInterfaceAddressTlv.asBytes()));
42 + } else if (isisTlv instanceof ProtocolSupportedTlv) {
43 + ProtocolSupportedTlv protocolSupportedTlv = (ProtocolSupportedTlv) isisTlv;
44 + tlvBytes.addAll(Bytes.asList(protocolSupportedTlv.asBytes()));
45 + } else if (isisTlv instanceof PaddingTlv) {
46 + PaddingTlv paddingTlv = (PaddingTlv) isisTlv;
47 + tlvBytes.addAll(Bytes.asList(paddingTlv.asBytes()));
48 + } else if (isisTlv instanceof IsisNeighborTlv) {
49 + IsisNeighborTlv isisNeighborTlv = (IsisNeighborTlv) isisTlv;
50 + tlvBytes.addAll(Bytes.asList(isisNeighborTlv.asBytes()));
51 + } else {
52 + System.out.println("UNKNOWN TLV TYPE ::TlvsToBytes ");
53 + }
54 +
55 + return tlvBytes;
56 + }
57 + /**
58 + * Creates an instance.
59 + */
60 + private TlvsToBytes() {
61 + //private constructor
62 + }
63 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +/**
18 + * Implementation of the ISIS protocol.
19 + */
20 +package org.onosproject.isis.io.isispacket.tlv;
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +/**
18 + * Implementation of the ISIS protocol.
19 + */
20 +package org.onosproject.isis.io;
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.isis.io.util;
17 +
18 +
19 +import com.google.common.base.MoreObjects;
20 +import com.google.common.primitives.Bytes;
21 +import org.onosproject.isis.io.isispacket.tlv.PaddingTlv;
22 +import org.onosproject.isis.io.isispacket.tlv.TlvHeader;
23 +
24 +import javax.xml.bind.DatatypeConverter;
25 +import java.util.ArrayList;
26 +import java.util.List;
27 +import java.util.StringTokenizer;
28 +
29 +/**
30 + * Represents ISIS utils.
31 + */
32 +public final class IsisUtil {
33 + public static final int AREAADDRESS = 1;
34 + public static final int ISREACHABILITY = 2;
35 + public static final int ISNEIGHBORS = 6;
36 + public static final int PADDING = 8;
37 + public static final int LSPENTRY = 9;
38 + public static final int AUTHENTICATION = 10;
39 + public static final int CHECKSUM = 12;
40 + public static final int EXTENDEDISREACHABILITY = 22;
41 + public static final int ISALIAS = 24;
42 + public static final int IPINTERNALREACHABILITY = 128;
43 + public static final int PROTOCOLSUPPORTED = 129;
44 + public static final int IPEXTERNALREACHABILITY = 130;
45 + public static final int IDRPINFORMATION = 131;
46 + public static final int IPINTERFACEADDRESS = 132;
47 + public static final int L1HELLOPDU = 1;
48 + public static final int L2HELLOPDU = 2;
49 + public static final int P2PHELLOPDU = 3;
50 + public static final int L1LSPDU = 18;
51 + public static final int L2LSPDU = 20;
52 + public static final int L1CSNP = 24;
53 + public static final int L2CSNP = 25;
54 + public static final int L1PSNP = 26;
55 + public static final int L2PSNP = 27;
56 + public static final int L1L2_LS_PDUHEADERLENGTH = 27;
57 + public static final int P2PPDUHEADERLENGTH = 20;
58 + public static final int PSNPPDUHEADERLENGTH = 17;
59 + public static final char ETHER_FRAME_LEN = 1514;
60 + public static final int ID_SIX_BYTES = 6;
61 + public static final int ID_PLUS_ONE_BYTE = 7;
62 + public static final int ID_PLUS_TWO_BYTE = 8;
63 + public static final int THREE_BYTES = 3;
64 + public static final int SIX_BYTES = 6;
65 + public static final int FOUR_BYTES = 4;
66 + public static final int PADDING_FIXED_LENGTH = 255;
67 +
68 + /**
69 + * Creates an instance of this class.
70 + */
71 + private IsisUtil() {
72 +
73 + }
74 +
75 + /**
76 + * Parse byte array to string system ID.
77 + *
78 + * @param bytes system ID
79 + * @return systemId system ID.
80 + */
81 + public static String systemId(byte[] bytes) {
82 + String systemId = "";
83 + for (Byte byt : bytes) {
84 + String hexa = Integer.toHexString(Byte.toUnsignedInt(byt));
85 + if (hexa.length() % 2 != 0) {
86 + hexa = "0" + hexa;
87 + }
88 + systemId = systemId + hexa;
89 + if (systemId.length() == 4 || systemId.length() == 9) {
90 + systemId = systemId + ".";
91 + }
92 + }
93 + return systemId;
94 + }
95 +
96 + /**
97 + * Parse byte array to LAN ID.
98 + *
99 + * @param bytes LAN ID
100 + * @return systemId system ID.
101 + */
102 + public static String systemIdPlus(byte[] bytes) {
103 + String systemId = "";
104 + for (Byte byt : bytes) {
105 + String hexa = Integer.toHexString(Byte.toUnsignedInt(byt));
106 + if (hexa.length() % 2 != 0) {
107 + hexa = "0" + hexa;
108 + }
109 + systemId = systemId + hexa;
110 + if (systemId.length() == 4 || systemId.length() == 9
111 + || systemId.length() == 14) {
112 + systemId = systemId + ".";
113 + }
114 + }
115 + return systemId;
116 + }
117 +
118 + /**
119 + * Parse byte array to area address.
120 + *
121 + * @param bytes area address
122 + * @return areaAddress area address
123 + */
124 + public static String areaAddres(byte[] bytes) {
125 + int count = 0;
126 + String areaAddress = "";
127 + for (Byte byt : bytes) {
128 + String hexa = Integer.toHexString(Byte.toUnsignedInt(byt));
129 + if (hexa.length() % 2 != 0) {
130 + hexa = "0" + hexa;
131 + }
132 + if (count == 0) {
133 + hexa = hexa + ".";
134 + }
135 + areaAddress = areaAddress + hexa;
136 + count++;
137 + }
138 + return areaAddress;
139 + }
140 +
141 + /**
142 + * Parse area address to bytes.
143 + *
144 + * @param address area address
145 + * @return areaAddress area address
146 + */
147 + public static List<Byte> areaAddresToBytes(String address) {
148 + List<Byte> idLst = new ArrayList();
149 + StringTokenizer tokenizer = new StringTokenizer(address, ".");
150 + int count = 0;
151 + while (tokenizer.hasMoreElements()) {
152 + String str = tokenizer.nextToken();
153 + if (str.length() % 2 != 0) {
154 + str = "0" + str;
155 + }
156 + if (count > 0) {
157 +
158 + for (int i = 0; i < str.length(); i = i + 2) {
159 + idLst.add((byte) Integer.parseInt(str.substring(i, i + 2), 16));
160 + }
161 + } else {
162 + idLst.add((byte) Integer.parseInt(str, 16));
163 + }
164 + count++;
165 + }
166 + return idLst;
167 + }
168 +
169 + /**
170 + * Gets PDU header length.
171 + *
172 + * @param pduType PDU type
173 + * @return headerLength header length
174 + */
175 + public static int getPduHeaderLength(int pduType) {
176 + int headerLength = 0;
177 + switch (pduType) {
178 + case L1HELLOPDU:
179 + case L2HELLOPDU:
180 + case L1LSPDU:
181 + case L2LSPDU:
182 + headerLength = L1L2_LS_PDUHEADERLENGTH;
183 + break;
184 + case P2PHELLOPDU:
185 + headerLength = P2PPDUHEADERLENGTH;
186 + break;
187 + case L1PSNP:
188 + case L2PSNP:
189 + headerLength = PSNPPDUHEADERLENGTH;
190 + break;
191 + default:
192 + break;
193 + }
194 + return headerLength;
195 + }
196 +
197 + /**
198 + * Parse source and LAN ID.
199 + *
200 + * @param id source and LAN ID
201 + * @return sourceAndLanIdToBytes source and LAN ID
202 + */
203 + public static List<Byte> sourceAndLanIdToBytes(String id) {
204 + List<Byte> idLst = new ArrayList();
205 +
206 + StringTokenizer tokenizer = new StringTokenizer(id, ".");
207 + while (tokenizer.hasMoreElements()) {
208 + int i = 0;
209 + String str = tokenizer.nextToken();
210 + idLst.add((byte) Integer.parseInt(str.substring(0, i + 2), 16));
211 + if (str.length() > 2) {
212 + idLst.add((byte) Integer.parseInt(str.substring(i + 2, str.length()), 16));
213 + }
214 +
215 + }
216 + return idLst;
217 + }
218 +
219 + /**
220 + * Parse padding for PDU based on current length.
221 + *
222 + * @param currentLength current length
223 + * @return byteArray padding array
224 + */
225 + public static byte[] paddingForPdu(int currentLength) {
226 + List<Byte> bytes = new ArrayList<>();
227 + while (ETHER_FRAME_LEN > currentLength) {
228 + int length = ETHER_FRAME_LEN - currentLength;
229 + TlvHeader tlvHeader = new TlvHeader();
230 + tlvHeader.setTlvType(PADDING);
231 + if (length >= PADDING_FIXED_LENGTH) {
232 + tlvHeader.setTlvLength(PADDING_FIXED_LENGTH);
233 + } else {
234 + tlvHeader.setTlvLength(ETHER_FRAME_LEN - currentLength);
235 + }
236 + PaddingTlv tlv = new PaddingTlv(tlvHeader);
237 + bytes.addAll(Bytes.asList(tlv.asBytes()));
238 + currentLength = currentLength + tlv.tlvLength();
239 + }
240 + byte[] byteArray = new byte[bytes.size()];
241 + int i = 0;
242 + for (byte byt : bytes) {
243 + byteArray[i++] = byt;
244 + }
245 + return byteArray;
246 +
247 + }
248 +
249 + /**
250 + * Converts an integer to two bytes.
251 + *
252 + * @param numberToConvert number to convert
253 + * @return numInBytes given number as bytes
254 + */
255 + public static byte[] convertToTwoBytes(int numberToConvert) {
256 +
257 + byte[] numInBytes = new byte[2];
258 + String s1 = Integer.toHexString(numberToConvert);
259 + if (s1.length() % 2 != 0) {
260 + s1 = "0" + s1;
261 + }
262 + byte[] hexas = DatatypeConverter.parseHexBinary(s1);
263 + if (hexas.length == 1) {
264 + numInBytes[0] = 0;
265 + numInBytes[1] = hexas[0];
266 + } else {
267 + numInBytes[0] = hexas[0];
268 + numInBytes[1] = hexas[1];
269 + }
270 + return numInBytes;
271 + }
272 +
273 + /**
274 + * Converts a number to four bytes.
275 + *
276 + * @param numberToConvert number to convert
277 + * @return numInBytes given number as bytes
278 + */
279 + public static byte[] convertToFourBytes(int numberToConvert) {
280 +
281 + byte[] numInBytes = new byte[4];
282 + String s1 = Integer.toHexString(numberToConvert);
283 + if (s1.length() % 2 != 0) {
284 + s1 = "0" + s1;
285 + }
286 + byte[] hexas = DatatypeConverter.parseHexBinary(s1);
287 + if (hexas.length == 1) {
288 + numInBytes[0] = 0;
289 + numInBytes[1] = 0;
290 + numInBytes[2] = 0;
291 + numInBytes[3] = hexas[0];
292 + } else if (hexas.length == 2) {
293 + numInBytes[0] = 0;
294 + numInBytes[1] = 0;
295 + numInBytes[2] = hexas[0];
296 + numInBytes[3] = hexas[1];
297 + } else if (hexas.length == 3) {
298 + numInBytes[0] = 0;
299 + numInBytes[1] = hexas[0];
300 + numInBytes[2] = hexas[1];
301 + numInBytes[3] = hexas[2];
302 + } else {
303 + numInBytes[0] = hexas[0];
304 + numInBytes[1] = hexas[1];
305 + numInBytes[2] = hexas[2];
306 + numInBytes[3] = hexas[3];
307 + }
308 + return numInBytes;
309 + }
310 +
311 + @Override
312 + public String toString() {
313 + return MoreObjects.toStringHelper(getClass())
314 + .omitNullValues()
315 + .toString();
316 + }
317 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +/**
18 + * Implementation of the ISIS protocol.
19 + */
20 +package org.onosproject.isis.io.util;
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +/**
18 + * Implementation of the ISIS protocol.
19 + */
20 +package org.onosproject.isis;
...\ No newline at end of file ...\ No newline at end of file
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
33 33
34 <modules> 34 <modules>
35 <module>api</module> 35 <module>api</module>
36 + <module>isisio</module>
36 </modules> 37 </modules>
37 38
38 </project> 39 </project>
......