blob: 90a1cc6b9ab70361ac5e63cbc843a0d1cdc9b4c0 [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;
25import 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;
29import org.onosproject.bgpio.util.Constants;
30import org.onosproject.bgpio.util.Validation;
31import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
33
34import com.google.common.base.MoreObjects;
35
36/*
37 * Provides Implementation of MpReach Nlri BGP Path Attribute.
38 */
39public 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}