Priyanka B
Committed by Gerrit Code Review

[Emu] [ONOS-2588] Implement BGP Message parser for parsing BGP protocol messgaes…

… with encoding and decoding API

Change-Id: Id87f2bc98f1fd486123c86b7d715aadb504f5623
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.protocol;
17 +
18 +import java.util.List;
19 +
20 +import org.onosproject.bgpio.types.BGPValueType;
21 +import org.onosproject.bgpio.protocol.linkstate.NodeDescriptors;
22 +
23 +/**
24 + * Abstraction of an entity providing BGP-LS Link NLRI.
25 + */
26 +public interface BgpLinkLsNlri extends BGPLSNlri {
27 + /**
28 + * Returns local node descriptors.
29 + *
30 + * @return local node descriptors
31 + */
32 + NodeDescriptors getLocalNodeDescriptors();
33 +
34 + /**
35 + * Returns remote node descriptors.
36 + *
37 + * @return remote node descriptors
38 + */
39 + NodeDescriptors getRemoteNodeDescriptors();
40 +
41 + /**
42 + * Returns link descriptors.
43 + *
44 + * @return link descriptors
45 + */
46 + List<BGPValueType> getLinkDescriptors();
47 +}
...\ 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 +
17 +package org.onosproject.bgpio.types;
18 +
19 +import java.net.InetAddress;
20 +import java.util.LinkedList;
21 +import java.util.List;
22 +
23 +import org.jboss.netty.buffer.ChannelBuffer;
24 +import org.onlab.packet.Ip4Address;
25 +import org.onosproject.bgpio.exceptions.BGPParseException;
26 +import org.onosproject.bgpio.protocol.BGPLSNlri;
27 +import org.onosproject.bgpio.protocol.linkstate.BGPPrefixIPv4LSNlriVer4;
28 +import org.onosproject.bgpio.protocol.linkstate.BGPNodeLSNlriVer4;
29 +import org.onosproject.bgpio.util.Constants;
30 +import org.onosproject.bgpio.util.Validation;
31 +import org.slf4j.Logger;
32 +import org.slf4j.LoggerFactory;
33 +
34 +import com.google.common.base.MoreObjects;
35 +
36 +/*
37 + * Provides Implementation of MpReach Nlri BGP Path Attribute.
38 + */
39 +public class MpReachNlri implements BGPValueType {
40 +
41 + private static final Logger log = LoggerFactory.getLogger(MpReachNlri.class);
42 +
43 + public static final byte MPREACHNLRI_TYPE = 14;
44 + public static final byte LINK_NLRITYPE = 2;
45 +
46 + private boolean isMpReachNlri = false;
47 + private final List<BGPLSNlri> mpReachNlri;
48 + private final int length;
49 + private final short afi;
50 + private final byte safi;
51 + private final Ip4Address ipNextHop;
52 +
53 + /**
54 + * Constructor to initialize parameters.
55 + *
56 + * @param mpReachNlri MpReach Nlri attribute
57 + * @param afi address family identifier
58 + * @param safi subsequent address family identifier
59 + * @param ipNextHop nexthop IpAddress
60 + * @param length of MpReachNlri
61 + */
62 + public MpReachNlri(List<BGPLSNlri> mpReachNlri, short afi, byte safi, Ip4Address ipNextHop, int length) {
63 + this.mpReachNlri = mpReachNlri;
64 + this.isMpReachNlri = true;
65 + this.ipNextHop = ipNextHop;
66 + this.afi = afi;
67 + this.safi = safi;
68 + this.length = length;
69 + }
70 +
71 + /**
72 + * Returns whether MpReachNlri is present.
73 + *
74 + * @return whether MpReachNlri is present
75 + */
76 + public boolean isMpReachNlriSet() {
77 + return this.isMpReachNlri;
78 + }
79 +
80 + /**
81 + * Returns list of MpReach Nlri.
82 + *
83 + * @return list of MpReach Nlri
84 + */
85 + public List<BGPLSNlri> mpReachNlri() {
86 + return this.mpReachNlri;
87 + }
88 +
89 + /**
90 + * Returns length of MpReachNlri.
91 + *
92 + * @return length of MpReachNlri
93 + */
94 + public int mpReachNlriLen() {
95 + return this.length;
96 + }
97 +
98 + /**
99 + * Reads from ChannelBuffer and parses MpReachNlri.
100 + *
101 + * @param cb channelBuffer
102 + * @return object of MpReachNlri
103 + * @throws BGPParseException while parsing MpReachNlri
104 + */
105 + public static MpReachNlri read(ChannelBuffer cb) throws BGPParseException {
106 + ChannelBuffer tempBuf = cb.copy();
107 + Validation parseFlags = Validation.parseAttributeHeader(cb);
108 + int len = parseFlags.isShort() ? parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_SHORT :
109 + parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_BYTE;
110 + ChannelBuffer data = tempBuf.readBytes(len);
111 +
112 + if (cb.readableBytes() < parseFlags.getLength()) {
113 + Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.ATTRIBUTE_LENGTH_ERROR,
114 + parseFlags.getLength());
115 + }
116 + if (!parseFlags.getFirstBit() && parseFlags.getSecondBit() && parseFlags.getThirdBit()) {
117 + throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.ATTRIBUTE_FLAGS_ERROR, data);
118 + }
119 +
120 + BGPLSNlri bgpLSNlri = null;
121 + List<BGPLSNlri> mpReachNlri = new LinkedList<>();
122 + ChannelBuffer tempCb = cb.readBytes(parseFlags.getLength());
123 + short afi = 0;
124 + byte safi = 0;
125 + Ip4Address ipNextHop = null;
126 + while (tempCb.readableBytes() > 0) {
127 + afi = tempCb.readShort();
128 + safi = tempCb.readByte();
129 +
130 + //Supporting for AFI 16388 / SAFI 71 and VPN AFI 16388 / SAFI 128
131 + if ((afi == Constants.AFI_VALUE) && (safi == Constants.SAFI_VALUE) || (afi == Constants.AFI_VALUE)
132 + && (safi == Constants.VPN_SAFI_VALUE)) {
133 + byte nextHopLen = tempCb.readByte();
134 + //TODO: use Validation.toInetAddress once Validation is merged
135 + InetAddress ipAddress = (InetAddress) cb.readBytes(nextHopLen);
136 + if (ipAddress.isMulticastAddress()) {
137 + throw new BGPParseException("Multicast not supported");
138 + }
139 + ipNextHop = Ip4Address.valueOf(ipAddress);
140 + byte reserved = tempCb.readByte();
141 +
142 + while (tempCb.readableBytes() > 0) {
143 + short nlriType = tempCb.readShort();
144 + short totNlriLen = tempCb.readShort();
145 + if (tempCb.readableBytes() < totNlriLen) {
146 + Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR,
147 + BGPErrorType.ATTRIBUTE_LENGTH_ERROR, totNlriLen);
148 + }
149 + tempBuf = tempCb.readBytes(totNlriLen);
150 + switch (nlriType) {
151 + case BGPNodeLSNlriVer4.NODE_NLRITYPE:
152 + bgpLSNlri = BGPNodeLSNlriVer4.read(tempBuf, afi, safi);
153 + break;
154 + case LINK_NLRITYPE:
155 + //TODO: To be merged later
156 + break;
157 + case BGPPrefixIPv4LSNlriVer4.PREFIX_IPV4_NLRITYPE:
158 + bgpLSNlri = BGPPrefixIPv4LSNlriVer4.read(tempBuf, afi, safi);
159 + break;
160 + default:
161 + log.debug("nlriType not supported" + nlriType);
162 + }
163 + mpReachNlri.add(bgpLSNlri);
164 + }
165 + } else {
166 + //TODO: check with the values got from capability
167 + throw new BGPParseException("Not Supporting afi " + afi + "safi " + safi);
168 + }
169 + }
170 + return new MpReachNlri(mpReachNlri, afi, safi, ipNextHop, parseFlags.getLength());
171 + }
172 +
173 + @Override
174 + public short getType() {
175 + return MPREACHNLRI_TYPE;
176 + }
177 +
178 + /**
179 + * Returns AFI.
180 + *
181 + * @return AFI
182 + */
183 + public short afi() {
184 + return this.afi;
185 + }
186 +
187 + /**
188 + * Returns Nexthop IpAddress.
189 + *
190 + * @return Nexthop IpAddress
191 + */
192 + public Ip4Address nexthop4() {
193 + return this.ipNextHop;
194 + }
195 +
196 + /**
197 + * Returns SAFI.
198 + *
199 + * @return SAFI
200 + */
201 + public byte safi() {
202 + return this.safi;
203 + }
204 +
205 + @Override
206 + public int write(ChannelBuffer cb) {
207 + //Not to be Implemented as of now
208 + return 0;
209 + }
210 +
211 + @Override
212 + public String toString() {
213 + return MoreObjects.toStringHelper(getClass())
214 + .add("mpReachNlri", mpReachNlri)
215 + .add("afi", afi)
216 + .add("safi", safi)
217 + .add("ipNextHop", ipNextHop)
218 + .add("length", length)
219 + .toString();
220 + }
221 +}
...\ 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 +
17 +package org.onosproject.bgpio.types;
18 +
19 +import java.util.LinkedList;
20 +import java.util.List;
21 +
22 +import org.jboss.netty.buffer.ChannelBuffer;
23 +import org.onosproject.bgpio.exceptions.BGPParseException;
24 +import org.onosproject.bgpio.protocol.BGPLSNlri;
25 +import org.onosproject.bgpio.protocol.linkstate.BGPNodeLSNlriVer4;
26 +import org.onosproject.bgpio.protocol.linkstate.BGPPrefixIPv4LSNlriVer4;
27 +import org.onosproject.bgpio.util.Constants;
28 +import org.onosproject.bgpio.util.Validation;
29 +import org.slf4j.Logger;
30 +import org.slf4j.LoggerFactory;
31 +
32 +import com.google.common.base.MoreObjects;
33 +
34 +/**
35 + * Provides Implementation of MpUnReach Nlri BGP Path Attribute.
36 + */
37 +public class MpUnReachNlri implements BGPValueType {
38 +
39 + protected static final Logger log = LoggerFactory.getLogger(MpUnReachNlri.class);
40 +
41 + public static final byte MPUNREACHNLRI_TYPE = 15;
42 + public static final byte LINK_NLRITYPE = 2;
43 + private boolean isMpUnReachNlri = false;
44 + private final short afi;
45 + private final byte safi;
46 + private final List<BGPLSNlri> mpUnReachNlri;
47 + private final int length;
48 +
49 + /**
50 + * Constructor to initialize parameters.
51 + *
52 + * @param mpUnReachNlri MpUnReach Nlri attribute
53 + * @param afi address family identifier
54 + * @param safi subsequent address family identifier
55 + * @param length of MpUnReachNlri
56 + */
57 + public MpUnReachNlri(List<BGPLSNlri> mpUnReachNlri, short afi, byte safi,
58 + int length) {
59 + this.mpUnReachNlri = mpUnReachNlri;
60 + this.isMpUnReachNlri = true;
61 + this.afi = afi;
62 + this.safi = safi;
63 + this.length = length;
64 + }
65 +
66 + /**
67 + * Reads from ChannelBuffer and parses MpUnReachNlri.
68 + *
69 + * @param cb ChannelBuffer
70 + * @return object of MpUnReachNlri
71 + * @throws BGPParseException while parsing MpUnReachNlri
72 + */
73 + public static MpUnReachNlri read(ChannelBuffer cb) throws BGPParseException {
74 + ChannelBuffer tempBuf = cb.copy();
75 + Validation parseFlags = Validation.parseAttributeHeader(cb);
76 + int len = parseFlags.isShort() ? parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_SHORT
77 + : parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_BYTE;
78 + ChannelBuffer data = tempBuf.readBytes(len);
79 +
80 + if (!parseFlags.getFirstBit() && parseFlags.getSecondBit()
81 + && parseFlags.getThirdBit()) {
82 + throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR,
83 + BGPErrorType.ATTRIBUTE_FLAGS_ERROR, data);
84 + }
85 +
86 + if (cb.readableBytes() < parseFlags.getLength()) {
87 + Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR,
88 + BGPErrorType.ATTRIBUTE_LENGTH_ERROR, parseFlags.getLength());
89 + }
90 +
91 + LinkedList<BGPLSNlri> mpUnReachNlri = new LinkedList<>();
92 + BGPLSNlri bgpLSNlri = null;
93 + short afi = 0;
94 + byte safi = 0;
95 + ChannelBuffer tempCb = cb.readBytes(parseFlags.getLength());
96 + while (tempCb.readableBytes() > 0) {
97 + afi = tempCb.readShort();
98 + safi = tempCb.readByte();
99 +
100 + //Supporting only for AFI 16388 / SAFI 71
101 + if ((afi == Constants.AFI_VALUE) && (safi == Constants.SAFI_VALUE)
102 + || (afi == Constants.AFI_VALUE) && (safi == Constants.VPN_SAFI_VALUE)) {
103 + while (tempCb.readableBytes() > 0) {
104 + short nlriType = tempCb.readShort();
105 + short totNlriLen = tempCb.readShort();
106 + if (tempCb.readableBytes() < totNlriLen) {
107 + Validation.validateLen(
108 + BGPErrorType.UPDATE_MESSAGE_ERROR,
109 + BGPErrorType.ATTRIBUTE_LENGTH_ERROR, totNlriLen);
110 + }
111 + tempBuf = tempCb.readBytes(totNlriLen);
112 + switch (nlriType) {
113 + case BGPNodeLSNlriVer4.NODE_NLRITYPE:
114 + bgpLSNlri = BGPNodeLSNlriVer4.read(tempBuf, afi, safi);
115 + break;
116 + case LINK_NLRITYPE:
117 + //TODO: to be merged later
118 + break;
119 + case BGPPrefixIPv4LSNlriVer4.PREFIX_IPV4_NLRITYPE:
120 + bgpLSNlri = BGPPrefixIPv4LSNlriVer4.read(tempBuf, afi,
121 + safi);
122 + break;
123 + default:
124 + log.debug("nlriType not supported" + nlriType);
125 + }
126 + mpUnReachNlri.add(bgpLSNlri);
127 + }
128 + } else {
129 + //TODO: check with the values got from capability
130 + throw new BGPParseException("Not Supporting afi " + afi
131 + + "safi " + safi);
132 + }
133 + }
134 + return new MpUnReachNlri(mpUnReachNlri, afi, safi,
135 + parseFlags.getLength());
136 + }
137 +
138 + @Override
139 + public short getType() {
140 + return MPUNREACHNLRI_TYPE;
141 + }
142 +
143 + /**
144 + * Returns SAFI.
145 + *
146 + * @return SAFI
147 + */
148 + public byte safi() {
149 + return this.safi;
150 + }
151 +
152 + /**
153 + * Returns AFI.
154 + *
155 + * @return AFI
156 + */
157 + public short afi() {
158 + return this.afi;
159 + }
160 +
161 + /**
162 + * Returns list of MpUnReach Nlri.
163 + *
164 + * @return list of MpUnReach Nlri
165 + */
166 + public List<BGPLSNlri> mpUnReachNlri() {
167 + return this.mpUnReachNlri;
168 + }
169 +
170 + /**
171 + * Returns whether MpReachNlri is present.
172 + *
173 + * @return whether MpReachNlri is present
174 + */
175 + public boolean isMpUnReachNlriSet() {
176 + return this.isMpUnReachNlri;
177 + }
178 +
179 + /**
180 + * Returns length of MpUnReach.
181 + *
182 + * @return length of MpUnReach
183 + */
184 + public int mpUnReachNlriLen() {
185 + return this.length;
186 + }
187 +
188 + @Override
189 + public int write(ChannelBuffer cb) {
190 + //Not to be Implemented as of now
191 + return 0;
192 + }
193 +
194 + @Override
195 + public String toString() {
196 + return MoreObjects.toStringHelper(getClass())
197 + .add("mpReachNlri", mpUnReachNlri)
198 + .add("afi", afi)
199 + .add("safi", safi)
200 + .add("length", length)
201 + .toString();
202 + }
203 +}
...\ No newline at end of file ...\ No newline at end of file