blob: 1f39c805224b07f32333e4e153bf045327cd14a9 [file] [log] [blame]
Priyanka B3b695542015-10-14 17:16:27 +05301/*
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
17package org.onosproject.bgpio.types;
18
19import java.net.InetAddress;
20import java.util.LinkedList;
21import java.util.List;
22
23import org.jboss.netty.buffer.ChannelBuffer;
24import org.onlab.packet.Ip4Address;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053025import org.onosproject.bgpio.exceptions.BgpParseException;
26import org.onosproject.bgpio.protocol.BgpLSNlri;
27import org.onosproject.bgpio.protocol.linkstate.BgpPrefixIPv4LSNlriVer4;
28import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4;
Priyanka Be07ea4d2015-11-21 21:28:10 +053029import org.onosproject.bgpio.protocol.linkstate.BgpLinkLsNlriVer4;
Priyanka B3b695542015-10-14 17:16:27 +053030import org.onosproject.bgpio.util.Constants;
31import org.onosproject.bgpio.util.Validation;
32import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
34
35import com.google.common.base.MoreObjects;
36
37/*
38 * Provides Implementation of MpReach Nlri BGP Path Attribute.
39 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053040public class MpReachNlri implements BgpValueType {
Priyanka B3b695542015-10-14 17:16:27 +053041
42 private static final Logger log = LoggerFactory.getLogger(MpReachNlri.class);
Priyanka B3b695542015-10-14 17:16:27 +053043 public static final byte MPREACHNLRI_TYPE = 14;
44 public static final byte LINK_NLRITYPE = 2;
45
46 private boolean isMpReachNlri = false;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053047 private final List<BgpLSNlri> mpReachNlri;
Priyanka B3b695542015-10-14 17:16:27 +053048 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 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053062 public MpReachNlri(List<BgpLSNlri> mpReachNlri, short afi, byte safi, Ip4Address ipNextHop, int length) {
Priyanka B3b695542015-10-14 17:16:27 +053063 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 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053085 public List<BgpLSNlri> mpReachNlri() {
Priyanka B3b695542015-10-14 17:16:27 +053086 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
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530103 * @throws BgpParseException while parsing MpReachNlri
Priyanka B3b695542015-10-14 17:16:27 +0530104 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530105 public static MpReachNlri read(ChannelBuffer cb) throws BgpParseException {
Priyanka B3b695542015-10-14 17:16:27 +0530106 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()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530113 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
Priyanka B3b695542015-10-14 17:16:27 +0530114 parseFlags.getLength());
115 }
116 if (!parseFlags.getFirstBit() && parseFlags.getSecondBit() && parseFlags.getThirdBit()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530117 throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data);
Priyanka B3b695542015-10-14 17:16:27 +0530118 }
119
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530120 BgpLSNlri bgpLSNlri = null;
121 List<BgpLSNlri> mpReachNlri = new LinkedList<>();
Priyanka B3b695542015-10-14 17:16:27 +0530122 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();
Priyanka Be07ea4d2015-11-21 21:28:10 +0530134 InetAddress ipAddress = Validation.toInetAddress(nextHopLen, cb);
Priyanka B3b695542015-10-14 17:16:27 +0530135 if (ipAddress.isMulticastAddress()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530136 throw new BgpParseException("Multicast not supported");
Priyanka B3b695542015-10-14 17:16:27 +0530137 }
138 ipNextHop = Ip4Address.valueOf(ipAddress);
139 byte reserved = tempCb.readByte();
140
141 while (tempCb.readableBytes() > 0) {
142 short nlriType = tempCb.readShort();
143 short totNlriLen = tempCb.readShort();
144 if (tempCb.readableBytes() < totNlriLen) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530145 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
146 BgpErrorType.ATTRIBUTE_LENGTH_ERROR, totNlriLen);
Priyanka B3b695542015-10-14 17:16:27 +0530147 }
148 tempBuf = tempCb.readBytes(totNlriLen);
149 switch (nlriType) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530150 case BgpNodeLSNlriVer4.NODE_NLRITYPE:
151 bgpLSNlri = BgpNodeLSNlriVer4.read(tempBuf, afi, safi);
Priyanka B3b695542015-10-14 17:16:27 +0530152 break;
Priyanka Be07ea4d2015-11-21 21:28:10 +0530153 case BgpLinkLsNlriVer4.LINK_NLRITYPE:
154 bgpLSNlri = BgpLinkLsNlriVer4.read(tempBuf, afi, safi);
Priyanka B3b695542015-10-14 17:16:27 +0530155 break;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530156 case BgpPrefixIPv4LSNlriVer4.PREFIX_IPV4_NLRITYPE:
157 bgpLSNlri = BgpPrefixIPv4LSNlriVer4.read(tempBuf, afi, safi);
Priyanka B3b695542015-10-14 17:16:27 +0530158 break;
159 default:
160 log.debug("nlriType not supported" + nlriType);
161 }
162 mpReachNlri.add(bgpLSNlri);
163 }
164 } else {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530165 throw new BgpParseException("Not Supporting afi " + afi + "safi " + safi);
Priyanka B3b695542015-10-14 17:16:27 +0530166 }
167 }
168 return new MpReachNlri(mpReachNlri, afi, safi, ipNextHop, parseFlags.getLength());
169 }
170
171 @Override
172 public short getType() {
173 return MPREACHNLRI_TYPE;
174 }
175
176 /**
177 * Returns AFI.
178 *
179 * @return AFI
180 */
181 public short afi() {
182 return this.afi;
183 }
184
185 /**
186 * Returns Nexthop IpAddress.
187 *
188 * @return Nexthop IpAddress
189 */
190 public Ip4Address nexthop4() {
191 return this.ipNextHop;
192 }
193
194 /**
195 * Returns SAFI.
196 *
197 * @return SAFI
198 */
199 public byte safi() {
200 return this.safi;
201 }
202
203 @Override
204 public int write(ChannelBuffer cb) {
205 //Not to be Implemented as of now
206 return 0;
207 }
208
209 @Override
210 public String toString() {
211 return MoreObjects.toStringHelper(getClass())
212 .add("mpReachNlri", mpReachNlri)
213 .add("afi", afi)
214 .add("safi", safi)
215 .add("ipNextHop", ipNextHop)
216 .add("length", length)
217 .toString();
218 }
219}