blob: d719c653b3bbb81747c442cfb0e8fb1b714c02d1 [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.util.LinkedList;
20import java.util.List;
21
22import org.jboss.netty.buffer.ChannelBuffer;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053023import org.onosproject.bgpio.exceptions.BgpParseException;
24import org.onosproject.bgpio.protocol.BgpLSNlri;
25import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4;
26import org.onosproject.bgpio.protocol.linkstate.BgpPrefixIPv4LSNlriVer4;
Priyanka Be07ea4d2015-11-21 21:28:10 +053027import org.onosproject.bgpio.protocol.linkstate.BgpLinkLsNlriVer4;
Priyanka B3b695542015-10-14 17:16:27 +053028import org.onosproject.bgpio.util.Constants;
29import org.onosproject.bgpio.util.Validation;
30import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
33import com.google.common.base.MoreObjects;
34
35/**
36 * Provides Implementation of MpUnReach Nlri BGP Path Attribute.
37 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053038public class MpUnReachNlri implements BgpValueType {
Priyanka B3b695542015-10-14 17:16:27 +053039
Priyanka Be07ea4d2015-11-21 21:28:10 +053040 private static final Logger log = LoggerFactory.getLogger(MpUnReachNlri.class);
Priyanka B3b695542015-10-14 17:16:27 +053041 public static final byte MPUNREACHNLRI_TYPE = 15;
42 public static final byte LINK_NLRITYPE = 2;
Priyanka Be07ea4d2015-11-21 21:28:10 +053043
Priyanka B3b695542015-10-14 17:16:27 +053044 private boolean isMpUnReachNlri = false;
45 private final short afi;
46 private final byte safi;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053047 private final List<BgpLSNlri> mpUnReachNlri;
Priyanka B3b695542015-10-14 17:16:27 +053048 private final int length;
49
50 /**
51 * Constructor to initialize parameters.
52 *
53 * @param mpUnReachNlri MpUnReach Nlri attribute
54 * @param afi address family identifier
55 * @param safi subsequent address family identifier
56 * @param length of MpUnReachNlri
57 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053058 public MpUnReachNlri(List<BgpLSNlri> mpUnReachNlri, short afi, byte safi,
Priyanka B3b695542015-10-14 17:16:27 +053059 int length) {
60 this.mpUnReachNlri = mpUnReachNlri;
61 this.isMpUnReachNlri = true;
62 this.afi = afi;
63 this.safi = safi;
64 this.length = length;
65 }
66
67 /**
68 * Reads from ChannelBuffer and parses MpUnReachNlri.
69 *
70 * @param cb ChannelBuffer
71 * @return object of MpUnReachNlri
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053072 * @throws BgpParseException while parsing MpUnReachNlri
Priyanka B3b695542015-10-14 17:16:27 +053073 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053074 public static MpUnReachNlri read(ChannelBuffer cb) throws BgpParseException {
Priyanka B3b695542015-10-14 17:16:27 +053075 ChannelBuffer tempBuf = cb.copy();
76 Validation parseFlags = Validation.parseAttributeHeader(cb);
77 int len = parseFlags.isShort() ? parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_SHORT
78 : parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_BYTE;
79 ChannelBuffer data = tempBuf.readBytes(len);
80
81 if (!parseFlags.getFirstBit() && parseFlags.getSecondBit()
82 && parseFlags.getThirdBit()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053083 throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR,
84 BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data);
Priyanka B3b695542015-10-14 17:16:27 +053085 }
86
87 if (cb.readableBytes() < parseFlags.getLength()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053088 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
89 BgpErrorType.ATTRIBUTE_LENGTH_ERROR, parseFlags.getLength());
Priyanka B3b695542015-10-14 17:16:27 +053090 }
91
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053092 LinkedList<BgpLSNlri> mpUnReachNlri = new LinkedList<>();
93 BgpLSNlri bgpLSNlri = null;
Priyanka B3b695542015-10-14 17:16:27 +053094 short afi = 0;
95 byte safi = 0;
96 ChannelBuffer tempCb = cb.readBytes(parseFlags.getLength());
97 while (tempCb.readableBytes() > 0) {
98 afi = tempCb.readShort();
99 safi = tempCb.readByte();
100
101 //Supporting only for AFI 16388 / SAFI 71
102 if ((afi == Constants.AFI_VALUE) && (safi == Constants.SAFI_VALUE)
103 || (afi == Constants.AFI_VALUE) && (safi == Constants.VPN_SAFI_VALUE)) {
104 while (tempCb.readableBytes() > 0) {
105 short nlriType = tempCb.readShort();
106 short totNlriLen = tempCb.readShort();
107 if (tempCb.readableBytes() < totNlriLen) {
108 Validation.validateLen(
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530109 BgpErrorType.UPDATE_MESSAGE_ERROR,
110 BgpErrorType.ATTRIBUTE_LENGTH_ERROR, totNlriLen);
Priyanka B3b695542015-10-14 17:16:27 +0530111 }
112 tempBuf = tempCb.readBytes(totNlriLen);
113 switch (nlriType) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530114 case BgpNodeLSNlriVer4.NODE_NLRITYPE:
115 bgpLSNlri = BgpNodeLSNlriVer4.read(tempBuf, afi, safi);
Priyanka B3b695542015-10-14 17:16:27 +0530116 break;
Priyanka Be07ea4d2015-11-21 21:28:10 +0530117 case BgpLinkLsNlriVer4.LINK_NLRITYPE:
118 bgpLSNlri = BgpLinkLsNlriVer4.read(tempBuf, afi, safi);
Priyanka B3b695542015-10-14 17:16:27 +0530119 break;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530120 case BgpPrefixIPv4LSNlriVer4.PREFIX_IPV4_NLRITYPE:
121 bgpLSNlri = BgpPrefixIPv4LSNlriVer4.read(tempBuf, afi,
Priyanka B3b695542015-10-14 17:16:27 +0530122 safi);
123 break;
124 default:
125 log.debug("nlriType not supported" + nlriType);
126 }
127 mpUnReachNlri.add(bgpLSNlri);
128 }
129 } else {
130 //TODO: check with the values got from capability
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530131 throw new BgpParseException("Not Supporting afi " + afi
Priyanka B3b695542015-10-14 17:16:27 +0530132 + "safi " + safi);
133 }
134 }
135 return new MpUnReachNlri(mpUnReachNlri, afi, safi,
136 parseFlags.getLength());
137 }
138
139 @Override
140 public short getType() {
141 return MPUNREACHNLRI_TYPE;
142 }
143
144 /**
145 * Returns SAFI.
146 *
147 * @return SAFI
148 */
149 public byte safi() {
150 return this.safi;
151 }
152
153 /**
154 * Returns AFI.
155 *
156 * @return AFI
157 */
158 public short afi() {
159 return this.afi;
160 }
161
162 /**
163 * Returns list of MpUnReach Nlri.
164 *
165 * @return list of MpUnReach Nlri
166 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530167 public List<BgpLSNlri> mpUnReachNlri() {
Priyanka B3b695542015-10-14 17:16:27 +0530168 return this.mpUnReachNlri;
169 }
170
171 /**
172 * Returns whether MpReachNlri is present.
173 *
174 * @return whether MpReachNlri is present
175 */
176 public boolean isMpUnReachNlriSet() {
177 return this.isMpUnReachNlri;
178 }
179
180 /**
181 * Returns length of MpUnReach.
182 *
183 * @return length of MpUnReach
184 */
185 public int mpUnReachNlriLen() {
186 return this.length;
187 }
188
189 @Override
190 public int write(ChannelBuffer cb) {
191 //Not to be Implemented as of now
192 return 0;
193 }
194
195 @Override
196 public String toString() {
197 return MoreObjects.toStringHelper(getClass())
198 .add("mpReachNlri", mpUnReachNlri)
199 .add("afi", afi)
200 .add("safi", safi)
201 .add("length", length)
202 .toString();
203 }
204}