blob: 9ffddf68090510c45d2199fdd6f28e7a4c34b52f [file] [log] [blame]
Priyanka B965d2692015-10-14 15:25:31 +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 */
16package org.onosproject.bgpio.protocol.ver4;
17
18import java.util.LinkedList;
19import java.util.List;
20
21import org.jboss.netty.buffer.ChannelBuffer;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053022import org.onosproject.bgpio.exceptions.BgpParseException;
Priyanka B965d2692015-10-14 15:25:31 +053023import org.onosproject.bgpio.types.As4Path;
24import org.onosproject.bgpio.types.AsPath;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053025import org.onosproject.bgpio.types.BgpErrorType;
26import org.onosproject.bgpio.types.BgpValueType;
Priyanka B965d2692015-10-14 15:25:31 +053027import org.onosproject.bgpio.types.LocalPref;
28import org.onosproject.bgpio.types.Med;
29import org.onosproject.bgpio.types.NextHop;
30import org.onosproject.bgpio.types.Origin;
Priyanka B24cce0a2015-11-21 18:30:34 +053031import org.onosproject.bgpio.types.MpReachNlri;
32import org.onosproject.bgpio.types.MpUnReachNlri;
Priyanka B965d2692015-10-14 15:25:31 +053033import org.onosproject.bgpio.util.UnSupportedAttribute;
34import org.onosproject.bgpio.util.Validation;
35import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
37
38import com.google.common.base.MoreObjects;
39
40/**
41 * Provides Implementation of BGP Path Attribute.
42 */
43public class BgpPathAttributes {
44
45 /* Path attribute:
46 <attribute type, attribute length, attribute value>
47
48 0 1
49 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
50 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 | Attr. Flags |Attr. Type Code|
52 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53 REFERENCE : RFC 4271
54 */
55 protected static final Logger log = LoggerFactory.getLogger(BgpPathAttributes.class);
56
57 public static final int LINK_STATE_ATTRIBUTE_TYPE = 50;
58 public static final int MPREACHNLRI_TYPE = 14;
59 public static final int MPUNREACHNLRI_TYPE = 15;
60
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053061 private final List<BgpValueType> pathAttribute;
Priyanka B965d2692015-10-14 15:25:31 +053062
63 /**
64 * Initialize parameter.
65 */
66 public BgpPathAttributes() {
67 this.pathAttribute = null;
68 }
69
70 /**
71 * Constructor to initialize parameters for BGP path attributes.
72 *
73 * @param pathAttribute list of path attributes
74 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053075 public BgpPathAttributes(List<BgpValueType> pathAttribute) {
Priyanka B965d2692015-10-14 15:25:31 +053076 this.pathAttribute = pathAttribute;
77 }
78
79 /**
80 * Returns list of path attributes.
81 *
82 * @return list of path attributes
83 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053084 public List<BgpValueType> pathAttributes() {
Priyanka B965d2692015-10-14 15:25:31 +053085 return this.pathAttribute;
86 }
87
88 /**
89 * Reads from channelBuffer and parses BGP path attributes.
90 *
91 * @param cb channelBuffer
92 * @return object of BgpPathAttributes
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053093 * @throws BgpParseException while parsing BGP path attributes
Priyanka B965d2692015-10-14 15:25:31 +053094 */
95 public static BgpPathAttributes read(ChannelBuffer cb)
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053096 throws BgpParseException {
Priyanka B965d2692015-10-14 15:25:31 +053097
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053098 BgpValueType pathAttribute = null;
99 List<BgpValueType> pathAttributeList = new LinkedList<>();
Priyanka B965d2692015-10-14 15:25:31 +0530100 boolean isOrigin = false;
101 boolean isAsPath = false;
102 boolean isNextHop = false;
103 boolean isMpReach = false;
104 boolean isMpUnReach = false;
105 while (cb.readableBytes() > 0) {
106 cb.markReaderIndex();
107 byte flags = cb.readByte();
108 byte typeCode = cb.readByte();
109 cb.resetReaderIndex();
110 switch (typeCode) {
111 case Origin.ORIGIN_TYPE:
112 pathAttribute = Origin.read(cb);
113 isOrigin = ((Origin) pathAttribute).isOriginSet();
114 break;
115 case AsPath.ASPATH_TYPE:
116 pathAttribute = AsPath.read(cb);
117 isAsPath = ((AsPath) pathAttribute).isaspathSet();
118 break;
119 case As4Path.AS4PATH_TYPE:
120 pathAttribute = As4Path.read(cb);
121 break;
122 case NextHop.NEXTHOP_TYPE:
123 pathAttribute = NextHop.read(cb);
124 isNextHop = ((NextHop) pathAttribute).isNextHopSet();
125 break;
126 case Med.MED_TYPE:
127 pathAttribute = Med.read(cb);
128 break;
129 case LocalPref.LOCAL_PREF_TYPE:
130 pathAttribute = LocalPref.read(cb);
131 break;
Priyanka B24cce0a2015-11-21 18:30:34 +0530132 case MpReachNlri.MPREACHNLRI_TYPE:
133 pathAttribute = MpReachNlri.read(cb);
134 isMpReach = ((MpReachNlri) pathAttribute).isMpReachNlriSet();
Priyanka B965d2692015-10-14 15:25:31 +0530135 break;
Priyanka B24cce0a2015-11-21 18:30:34 +0530136 case MpUnReachNlri.MPUNREACHNLRI_TYPE:
137 pathAttribute = MpUnReachNlri.read(cb);
138 isMpUnReach = ((MpUnReachNlri) pathAttribute)
139 .isMpUnReachNlriSet();
Priyanka B965d2692015-10-14 15:25:31 +0530140 break;
141 case LINK_STATE_ATTRIBUTE_TYPE:
142 //TODO: To be merged later
143 break;
144 default:
145 //skip bytes for unsupported attribute types
146 UnSupportedAttribute.read(cb);
147 }
148 pathAttributeList.add(pathAttribute);
149 }
150
151 checkMandatoryAttr(isOrigin, isAsPath, isNextHop, isMpReach, isMpUnReach);
152 //TODO:if mp_reach or mp_unreach not present ignore the packet
153 return new BgpPathAttributes(pathAttributeList);
154 }
155
156 /**
157 * Checks mandatory attributes are presents, if not present throws exception.
158 *
159 * @param isOrigin say whether origin attribute is present
160 * @param isAsPath say whether aspath attribute is present
161 * @param isNextHop say whether nexthop attribute is present
162 * @param isMpReach say whether mpreach attribute is present
163 * @param isMpUnReach say whether mpunreach attribute is present
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530164 * @throws BgpParseException if mandatory path attribute is not present
Priyanka B965d2692015-10-14 15:25:31 +0530165 */
166 public static void checkMandatoryAttr(boolean isOrigin, boolean isAsPath,
167 boolean isNextHop, boolean isMpReach, boolean isMpUnReach)
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530168 throws BgpParseException {
Priyanka B965d2692015-10-14 15:25:31 +0530169 if (!isOrigin) {
170 log.debug("Mandatory Attributes not Present");
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530171 Validation.validateType(BgpErrorType.UPDATE_MESSAGE_ERROR,
172 BgpErrorType.MISSING_WELLKNOWN_ATTRIBUTE,
Priyanka B965d2692015-10-14 15:25:31 +0530173 Origin.ORIGIN_TYPE);
174 }
175 if (!isAsPath) {
176 log.debug("Mandatory Attributes not Present");
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530177 Validation.validateType(BgpErrorType.UPDATE_MESSAGE_ERROR,
178 BgpErrorType.MISSING_WELLKNOWN_ATTRIBUTE,
Priyanka B965d2692015-10-14 15:25:31 +0530179 AsPath.ASPATH_TYPE);
180 }
181 if (!isMpUnReach && !isMpReach && !isNextHop) {
182 log.debug("Mandatory Attributes not Present");
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530183 Validation.validateType(BgpErrorType.UPDATE_MESSAGE_ERROR,
184 BgpErrorType.MISSING_WELLKNOWN_ATTRIBUTE,
Priyanka B965d2692015-10-14 15:25:31 +0530185 NextHop.NEXTHOP_TYPE);
186 }
187 }
188
189 @Override
190 public String toString() {
191 return MoreObjects.toStringHelper(getClass())
192 .add("pathAttribute", pathAttribute)
193 .toString();
194 }
195}