Priyanka B
Committed by Gerrit Code Review

[Emu] [ONOS-2602]Implement BGP Update Protocol Message Mpreach and MPunreach par…

…se and decode(Node, Link and Prefix)

Change-Id: Idba57626625cd1207b2b0fbddd39214faca337b6
1 +/*
2 + * Copyright 2015 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.bgpio.types;
17 +
18 +import java.net.InetAddress;
19 +import java.util.Objects;
20 +
21 +import org.jboss.netty.buffer.ChannelBuffer;
22 +import org.onlab.packet.Ip4Address;
23 +import org.onosproject.bgpio.exceptions.BGPParseException;
24 +import org.slf4j.Logger;
25 +import org.slf4j.LoggerFactory;
26 +
27 +import com.google.common.base.MoreObjects;
28 +import com.google.common.base.Preconditions;
29 +
30 +/**
31 + * Provides Implementation of IPv4AddressTlv.
32 + */
33 +public class IPv4AddressTlv implements BGPValueType {
34 + private static final Logger log = LoggerFactory.getLogger(IPv4AddressTlv.class);
35 + private static final int LENGTH = 4;
36 +
37 + private Ip4Address address;
38 + private short type;
39 +
40 + /**
41 + * Constructor to initialize parameters.
42 + *
43 + * @param address Ipv4 address of interface/neighbor
44 + * @param type address type
45 + */
46 + public IPv4AddressTlv(Ip4Address address, short type) {
47 + this.address = Preconditions.checkNotNull(address);
48 + this.type = type;
49 + }
50 +
51 + /**
52 + * Returns Ipv4 address of interface/neighbor.
53 + *
54 + * @return Ipv4 address of interface/neighbor
55 + */
56 + public Ip4Address getValue() {
57 + return address;
58 + }
59 +
60 + @Override
61 + public short getType() {
62 + return this.type;
63 + }
64 +
65 + @Override
66 + public int hashCode() {
67 + return Objects.hash(address);
68 + }
69 +
70 + @Override
71 + public boolean equals(Object obj) {
72 + if (this == obj) {
73 + return true;
74 + }
75 + if (obj instanceof IPv4AddressTlv) {
76 + IPv4AddressTlv other = (IPv4AddressTlv) obj;
77 + return Objects.equals(this.address, other.address) && Objects.equals(this.type, other.type);
78 + }
79 + return false;
80 + }
81 +
82 + @Override
83 + public int write(ChannelBuffer cb) {
84 + int iLenStartIndex = cb.writerIndex();
85 + cb.writeShort(type);
86 + cb.writeShort(LENGTH);
87 + cb.writeInt(address.toInt());
88 + return cb.writerIndex() - iLenStartIndex;
89 + }
90 +
91 + /**
92 + * Reads the channel buffer and returns object of IPv4AddressTlv.
93 + *
94 + * @param cb channelBuffer
95 + * @param type address type
96 + * @return object of IPv4AddressTlv
97 + * @throws BGPParseException while parsing IPv4AddressTlv
98 + */
99 + public static IPv4AddressTlv read(ChannelBuffer cb, short type) throws BGPParseException {
100 + //TODO: use Validation.toInetAddress once Validation is merged
101 + InetAddress ipAddress = (InetAddress) cb.readBytes(LENGTH);
102 + if (ipAddress.isMulticastAddress()) {
103 + throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR, (byte) 0, null);
104 + }
105 + Ip4Address address = Ip4Address.valueOf(ipAddress);
106 + return IPv4AddressTlv.of(address, type);
107 + }
108 +
109 + /**
110 + * Returns object of this class with specified values.
111 + *
112 + * @param address Ipv4 interface/neighbor Address
113 + * @param type says Ipv4 address of interface/neighbor tlv type
114 + * @return object of this class
115 + */
116 + public static IPv4AddressTlv of(final Ip4Address address , final short type) {
117 + return new IPv4AddressTlv(address, type);
118 + }
119 +
120 + @Override
121 + public String toString() {
122 + return MoreObjects.toStringHelper(getClass())
123 + .add("type", type)
124 + .add("LENGTH", LENGTH)
125 + .add("address", address)
126 + .toString();
127 + }
128 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2015 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.bgpio.types;
17 +
18 +import java.net.InetAddress;
19 +import java.util.Objects;
20 +
21 +import org.jboss.netty.buffer.ChannelBuffer;
22 +import org.onlab.packet.Ip6Address;
23 +import org.onosproject.bgpio.exceptions.BGPParseException;
24 +import org.slf4j.Logger;
25 +import org.slf4j.LoggerFactory;
26 +
27 +import com.google.common.base.MoreObjects;
28 +import com.google.common.base.Preconditions;
29 +
30 +/**
31 + * Provides Implementation of IPv6AddressTlv.
32 + */
33 +public class IPv6AddressTlv implements BGPValueType {
34 + private static final Logger log = LoggerFactory.getLogger(IPv6AddressTlv.class);
35 + private static final int LENGTH = 16;
36 +
37 + private final Ip6Address address;
38 + private short type;
39 +
40 + /**
41 + * Constructor to initialize parameters.
42 + *
43 + * @param address Ipv6 address of interface/neighbor
44 + * @param type address type
45 + */
46 + public IPv6AddressTlv(Ip6Address address, short type) {
47 + this.address = Preconditions.checkNotNull(address);
48 + this.type = type;
49 + }
50 +
51 + /**
52 + * Returns Ipv6 address of interface/neighbor.
53 + *
54 + * @return Ipv6 address of interface/neighbor
55 + */
56 + public Ip6Address getValue() {
57 + return address;
58 + }
59 +
60 + @Override
61 + public short getType() {
62 + return type;
63 + }
64 +
65 + @Override
66 + public int hashCode() {
67 + return Objects.hash(address);
68 + }
69 +
70 + @Override
71 + public boolean equals(Object obj) {
72 + if (this == obj) {
73 + return true;
74 + }
75 + if (obj instanceof IPv6AddressTlv) {
76 + IPv6AddressTlv other = (IPv6AddressTlv) obj;
77 + return Objects.equals(this.address, other.address) && Objects.equals(this.type, other.type);
78 + }
79 + return false;
80 + }
81 +
82 + @Override
83 + public int write(ChannelBuffer cb) {
84 + int iLenStartIndex = cb.writerIndex();
85 + cb.writeShort(type);
86 + cb.writeShort(LENGTH);
87 + cb.writeBytes(address.toOctets());
88 + return cb.writerIndex() - iLenStartIndex;
89 + }
90 +
91 + /**
92 + * Reads the channel buffer and returns object of IPv6AddressTlv.
93 + *
94 + * @param cb channelBuffer
95 + * @param type address type
96 + * @return object of IPv6AddressTlv
97 + * @throws BGPParseException while parsing IPv6AddressTlv
98 + */
99 + public static IPv6AddressTlv read(ChannelBuffer cb, short type) throws BGPParseException {
100 + //TODO: use Validation.toInetAddress once Validation is merged
101 + InetAddress ipAddress = (InetAddress) cb.readBytes(LENGTH);
102 + if (ipAddress.isMulticastAddress()) {
103 + throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR, (byte) 0, null);
104 + }
105 + Ip6Address address = Ip6Address.valueOf(ipAddress);
106 + return IPv6AddressTlv.of(address, type);
107 + }
108 +
109 + /**
110 + * Returns object of this class with specified values.
111 + *
112 + * @param address Ipv6 interface/neighbor address
113 + * @param type says Ipv6 address of interface/neighbor tlv type
114 + * @return object of this class
115 + */
116 + public static IPv6AddressTlv of(final Ip6Address address , final short type) {
117 + return new IPv6AddressTlv(address, type);
118 + }
119 +
120 + @Override
121 + public String toString() {
122 + return MoreObjects.toStringHelper(getClass())
123 + .add("type", type)
124 + .add("LENGTH", LENGTH)
125 + .add("address", address)
126 + .toString();
127 + }
128 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2015 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.bgpio.types;
17 +
18 +import java.util.Objects;
19 +
20 +import org.jboss.netty.buffer.ChannelBuffer;
21 +import org.slf4j.Logger;
22 +import org.slf4j.LoggerFactory;
23 +
24 +import com.google.common.base.MoreObjects;
25 +
26 +/**
27 + * Provides Implementation of Link Local/Remote IdentifiersTlv.
28 + */
29 +public class LinkLocalRemoteIdentifiersTlv implements BGPValueType {
30 + private static final Logger log = LoggerFactory.getLogger(LinkLocalRemoteIdentifiersTlv.class);
31 + public static final short TYPE = 258;
32 + private static final int LENGTH = 8;
33 +
34 + private final int linkLocalIdentifer;
35 + private final int linkRemoteIdentifer;
36 +
37 + /**
38 + * Constructor to initialize parameters.
39 + *
40 + * @param linkLocalIdentifer link local Identifer
41 + * @param linkRemoteIdentifer link remote Identifer
42 + */
43 + public LinkLocalRemoteIdentifiersTlv(int linkLocalIdentifer, int linkRemoteIdentifer) {
44 + this.linkLocalIdentifer = linkLocalIdentifer;
45 + this.linkRemoteIdentifer = linkRemoteIdentifer;
46 + }
47 +
48 + /**
49 + * Returns link remote Identifer.
50 + *
51 + * @return link remote Identifer
52 + */
53 + public int getLinkRemoteIdentifier() {
54 + return linkRemoteIdentifer;
55 + }
56 +
57 + /**
58 + * Returns link local Identifer.
59 + *
60 + * @return link local Identifer
61 + */
62 + public int getLinkLocalIdentifier() {
63 + return linkLocalIdentifer;
64 + }
65 +
66 + @Override
67 + public short getType() {
68 + return TYPE;
69 + }
70 +
71 + @Override
72 + public int hashCode() {
73 + return Objects.hash(linkLocalIdentifer, linkRemoteIdentifer);
74 + }
75 +
76 + @Override
77 + public boolean equals(Object obj) {
78 + if (this == obj) {
79 + return true;
80 + }
81 + if (obj instanceof LinkLocalRemoteIdentifiersTlv) {
82 + LinkLocalRemoteIdentifiersTlv other = (LinkLocalRemoteIdentifiersTlv) obj;
83 + return Objects.equals(this.linkLocalIdentifer, other.linkLocalIdentifer)
84 + && Objects.equals(this.linkRemoteIdentifer, other.linkRemoteIdentifer);
85 + }
86 + return false;
87 + }
88 +
89 + @Override
90 + public int write(ChannelBuffer cb) {
91 + int iLenStartIndex = cb.writerIndex();
92 + cb.writeShort(TYPE);
93 + cb.writeShort(LENGTH);
94 + cb.writeInt(linkLocalIdentifer);
95 + cb.writeInt(linkRemoteIdentifer);
96 + return cb.writerIndex() - iLenStartIndex;
97 + }
98 +
99 + /**
100 + * Reads the channel buffer and returns object of LinkLocalRemoteIdentifiersTlv.
101 + *
102 + * @param cb channelBuffer
103 + * @return object of LinkLocalRemoteIdentifiersTlv
104 + */
105 + public static LinkLocalRemoteIdentifiersTlv read(ChannelBuffer cb) {
106 + int linkLocalIdentifer = cb.readInt();
107 + int linkRemoteIdentifer = cb.readInt();
108 + return LinkLocalRemoteIdentifiersTlv.of(linkLocalIdentifer, linkRemoteIdentifer);
109 + }
110 +
111 + /**
112 + * Returns object of this class with specified link local identifer and link remote identifer.
113 + *
114 + * @param linkLocalIdentifer link local identifier
115 + * @param linkRemoteIdentifer link remote identifier
116 + * @return object of LinkLocalRemoteIdentifiersTlv
117 + */
118 + public static LinkLocalRemoteIdentifiersTlv of(final int linkLocalIdentifer, final int linkRemoteIdentifer) {
119 + return new LinkLocalRemoteIdentifiersTlv(linkLocalIdentifer, linkRemoteIdentifer);
120 + }
121 +
122 + @Override
123 + public String toString() {
124 + return MoreObjects.toStringHelper(getClass())
125 + .add("TYPE", TYPE)
126 + .add("LENGTH", LENGTH)
127 + .add("linkLocalIdentifer", linkLocalIdentifer)
128 + .add("linkRemoteIdentifer", linkRemoteIdentifer)
129 + .toString();
130 + }
131 +}
...\ No newline at end of file ...\ No newline at end of file