blob: 7189ef6279f5bb1a5c2414b3706b8a788006b6cc [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 B0ab34b92015-12-03 21:13:52 +053027import org.onosproject.bgpio.types.LinkStateAttributes;
Priyanka B965d2692015-10-14 15:25:31 +053028import org.onosproject.bgpio.types.LocalPref;
29import org.onosproject.bgpio.types.Med;
30import org.onosproject.bgpio.types.NextHop;
31import org.onosproject.bgpio.types.Origin;
Priyanka B24cce0a2015-11-21 18:30:34 +053032import org.onosproject.bgpio.types.MpReachNlri;
33import org.onosproject.bgpio.types.MpUnReachNlri;
Priyanka B965d2692015-10-14 15:25:31 +053034import org.onosproject.bgpio.util.UnSupportedAttribute;
35import org.onosproject.bgpio.util.Validation;
36import org.slf4j.Logger;
37import org.slf4j.LoggerFactory;
38
39import com.google.common.base.MoreObjects;
40
41/**
42 * Provides Implementation of BGP Path Attribute.
43 */
44public class BgpPathAttributes {
45
46 /* Path attribute:
47 <attribute type, attribute length, attribute value>
48
49 0 1
50 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
51 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52 | Attr. Flags |Attr. Type Code|
53 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54 REFERENCE : RFC 4271
55 */
56 protected static final Logger log = LoggerFactory.getLogger(BgpPathAttributes.class);
57
Priyanka B0ab34b92015-12-03 21:13:52 +053058 public static final int LINK_STATE_ATTRIBUTE_TYPE = 29;
Priyanka B965d2692015-10-14 15:25:31 +053059 public static final int MPREACHNLRI_TYPE = 14;
60 public static final int MPUNREACHNLRI_TYPE = 15;
61
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053062 private final List<BgpValueType> pathAttribute;
Priyanka B965d2692015-10-14 15:25:31 +053063
64 /**
65 * Initialize parameter.
66 */
67 public BgpPathAttributes() {
68 this.pathAttribute = null;
69 }
70
71 /**
72 * Constructor to initialize parameters for BGP path attributes.
73 *
74 * @param pathAttribute list of path attributes
75 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053076 public BgpPathAttributes(List<BgpValueType> pathAttribute) {
Priyanka B965d2692015-10-14 15:25:31 +053077 this.pathAttribute = pathAttribute;
78 }
79
80 /**
81 * Returns list of path attributes.
82 *
83 * @return list of path attributes
84 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053085 public List<BgpValueType> pathAttributes() {
Priyanka B965d2692015-10-14 15:25:31 +053086 return this.pathAttribute;
87 }
88
89 /**
90 * Reads from channelBuffer and parses BGP path attributes.
91 *
92 * @param cb channelBuffer
93 * @return object of BgpPathAttributes
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053094 * @throws BgpParseException while parsing BGP path attributes
Priyanka B965d2692015-10-14 15:25:31 +053095 */
96 public static BgpPathAttributes read(ChannelBuffer cb)
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053097 throws BgpParseException {
Priyanka B965d2692015-10-14 15:25:31 +053098
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053099 BgpValueType pathAttribute = null;
100 List<BgpValueType> pathAttributeList = new LinkedList<>();
Priyanka B965d2692015-10-14 15:25:31 +0530101 boolean isOrigin = false;
102 boolean isAsPath = false;
103 boolean isNextHop = false;
104 boolean isMpReach = false;
105 boolean isMpUnReach = false;
106 while (cb.readableBytes() > 0) {
107 cb.markReaderIndex();
108 byte flags = cb.readByte();
109 byte typeCode = cb.readByte();
110 cb.resetReaderIndex();
111 switch (typeCode) {
112 case Origin.ORIGIN_TYPE:
113 pathAttribute = Origin.read(cb);
114 isOrigin = ((Origin) pathAttribute).isOriginSet();
115 break;
116 case AsPath.ASPATH_TYPE:
117 pathAttribute = AsPath.read(cb);
118 isAsPath = ((AsPath) pathAttribute).isaspathSet();
119 break;
120 case As4Path.AS4PATH_TYPE:
121 pathAttribute = As4Path.read(cb);
122 break;
123 case NextHop.NEXTHOP_TYPE:
124 pathAttribute = NextHop.read(cb);
125 isNextHop = ((NextHop) pathAttribute).isNextHopSet();
126 break;
127 case Med.MED_TYPE:
128 pathAttribute = Med.read(cb);
129 break;
130 case LocalPref.LOCAL_PREF_TYPE:
131 pathAttribute = LocalPref.read(cb);
132 break;
Priyanka B24cce0a2015-11-21 18:30:34 +0530133 case MpReachNlri.MPREACHNLRI_TYPE:
134 pathAttribute = MpReachNlri.read(cb);
135 isMpReach = ((MpReachNlri) pathAttribute).isMpReachNlriSet();
Priyanka B965d2692015-10-14 15:25:31 +0530136 break;
Priyanka B24cce0a2015-11-21 18:30:34 +0530137 case MpUnReachNlri.MPUNREACHNLRI_TYPE:
138 pathAttribute = MpUnReachNlri.read(cb);
139 isMpUnReach = ((MpUnReachNlri) pathAttribute)
140 .isMpUnReachNlriSet();
Priyanka B965d2692015-10-14 15:25:31 +0530141 break;
142 case LINK_STATE_ATTRIBUTE_TYPE:
Priyanka B0ab34b92015-12-03 21:13:52 +0530143 pathAttribute = LinkStateAttributes.read(cb);
Priyanka B965d2692015-10-14 15:25:31 +0530144 break;
145 default:
146 //skip bytes for unsupported attribute types
147 UnSupportedAttribute.read(cb);
148 }
149 pathAttributeList.add(pathAttribute);
150 }
151
152 checkMandatoryAttr(isOrigin, isAsPath, isNextHop, isMpReach, isMpUnReach);
153 //TODO:if mp_reach or mp_unreach not present ignore the packet
154 return new BgpPathAttributes(pathAttributeList);
155 }
156
157 /**
158 * Checks mandatory attributes are presents, if not present throws exception.
159 *
160 * @param isOrigin say whether origin attribute is present
161 * @param isAsPath say whether aspath attribute is present
162 * @param isNextHop say whether nexthop attribute is present
163 * @param isMpReach say whether mpreach attribute is present
164 * @param isMpUnReach say whether mpunreach attribute is present
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530165 * @throws BgpParseException if mandatory path attribute is not present
Priyanka B965d2692015-10-14 15:25:31 +0530166 */
167 public static void checkMandatoryAttr(boolean isOrigin, boolean isAsPath,
168 boolean isNextHop, boolean isMpReach, boolean isMpUnReach)
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530169 throws BgpParseException {
Shashikanth VH1ecbe7b2015-12-02 22:54:23 +0530170 // Mandatory attributes validation not required for MP_UNREACH
171 if (isMpUnReach) {
172 return;
173 }
174
Priyanka B965d2692015-10-14 15:25:31 +0530175 if (!isOrigin) {
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 Origin.ORIGIN_TYPE);
180 }
181 if (!isAsPath) {
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 AsPath.ASPATH_TYPE);
186 }
187 if (!isMpUnReach && !isMpReach && !isNextHop) {
188 log.debug("Mandatory Attributes not Present");
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530189 Validation.validateType(BgpErrorType.UPDATE_MESSAGE_ERROR,
190 BgpErrorType.MISSING_WELLKNOWN_ATTRIBUTE,
Priyanka B965d2692015-10-14 15:25:31 +0530191 NextHop.NEXTHOP_TYPE);
192 }
193 }
194
195 @Override
196 public String toString() {
197 return MoreObjects.toStringHelper(getClass())
198 .add("pathAttribute", pathAttribute)
199 .toString();
200 }
201}