blob: 01d369e45b8579dd76a0f56505e0008b24a3b3c7 [file] [log] [blame]
Priyanka Be412a852015-11-14 17:29:06 +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.linkstate;
17
18import java.util.List;
19
20import org.jboss.netty.buffer.ChannelBuffer;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053021import org.onosproject.bgpio.exceptions.BgpParseException;
Priyanka Be412a852015-11-14 17:29:06 +053022import org.onosproject.bgpio.protocol.BgpLinkLsNlri;
23import org.onosproject.bgpio.protocol.NlriType;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053024import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4.ProtocolType;
25import org.onosproject.bgpio.types.BgpErrorType;
26import org.onosproject.bgpio.types.BgpValueType;
Priyanka Be412a852015-11-14 17:29:06 +053027import org.onosproject.bgpio.types.RouteDistinguisher;
28import org.onosproject.bgpio.util.Constants;
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32import com.google.common.base.MoreObjects;
33
34/**
35 * Implementation of Link LS NLRI.
36 */
37public class BgpLinkLsNlriVer4 implements BgpLinkLsNlri {
38
39 /*
40 * REFERENCE : draft-ietf-idr-ls-distribution-11
41 0 1 2 3
42 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
43 +-+-+-+-+-+-+-+-+
44 | Protocol-ID |
45 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 | Identifier |
47 | (64 bits) |
48 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49 // Local Node Descriptors (variable) //
50 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 // Remote Node Descriptors (variable) //
52 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53 // Link Descriptors (variable) //
54 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55
56 Figure : The Link NLRI format
57 */
58 private static final Logger log = LoggerFactory.getLogger(BgpLinkLsNlriVer4.class);
59 public static final int LINK_NLRITYPE = 2;
60
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053061 private BgpLinkLSIdentifier linkLSIdentifier;
Priyanka Be412a852015-11-14 17:29:06 +053062 private byte protocolId;
63 private long identifier;
64 private RouteDistinguisher routeDistinguisher;
65 private boolean isVpn;
66
67 /**
68 * Initialize fields.
69 */
70 public BgpLinkLsNlriVer4() {
71 this.protocolId = 0;
72 this.identifier = 0;
73 this.linkLSIdentifier = null;
74 this.routeDistinguisher = null;
75 this.isVpn = false;
76 }
77
78 /**
79 * Constructor to initialize parameters for BGP LinkLSNlri.
80 *
81 * @param protocolId protocol Id
82 * @param identifier field in BGP LinkLSNlri
83 * @param linkLSIdentifier link LS identifier
84 * @param routeDistinguisher route distinguisher from message
85 * @param isVpn vpn info availability in message
86 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053087 public BgpLinkLsNlriVer4(byte protocolId, long identifier, BgpLinkLSIdentifier linkLSIdentifier,
Priyanka Be412a852015-11-14 17:29:06 +053088 RouteDistinguisher routeDistinguisher, boolean isVpn) {
89 this.protocolId = protocolId;
90 this.identifier = identifier;
91 this.linkLSIdentifier = linkLSIdentifier;
92 this.routeDistinguisher = routeDistinguisher;
93 this.isVpn = isVpn;
94 }
95
96 /**
97 * Reads from channelBuffer and parses Link LS Nlri.
98 *
99 * @param cb ChannelBuffer
100 * @param afi Address Family Identifier
101 * @param safi Subsequent Address Family Identifier
102 * @return object of this class
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530103 * @throws BgpParseException while parsing Link LS NLRI
Priyanka Be412a852015-11-14 17:29:06 +0530104 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530105 public static BgpLinkLsNlriVer4 read(ChannelBuffer cb, short afi, byte safi) throws BgpParseException {
Priyanka Be412a852015-11-14 17:29:06 +0530106 boolean isVpn = false;
107 RouteDistinguisher routeDistinguisher = null;
108 if ((afi == Constants.AFI_VALUE) && (safi == Constants.VPN_SAFI_VALUE)) {
109 routeDistinguisher = new RouteDistinguisher();
110 routeDistinguisher = RouteDistinguisher.read(cb);
111 isVpn = true;
112 } else {
113 isVpn = false;
114 }
115 byte protocolId = cb.readByte();
116 long identifier = cb.readLong();
117
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530118 BgpLinkLSIdentifier linkLSIdentifier = new BgpLinkLSIdentifier();
119 linkLSIdentifier = BgpLinkLSIdentifier.parseLinkIdendifier(cb, protocolId);
Priyanka Be412a852015-11-14 17:29:06 +0530120 return new BgpLinkLsNlriVer4(protocolId, identifier, linkLSIdentifier, routeDistinguisher, isVpn);
121 }
122
123 @Override
124 public NlriType getNlriType() {
125 return NlriType.LINK;
126 }
127
128 @Override
129 public long getIdentifier() {
130 return this.identifier;
131 }
132
133 /**
134 * Set the link LS identifier.
135 *
136 * @param linkLSIdentifier link LS identifier to set
137 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530138 public void setLinkLSIdentifier(BgpLinkLSIdentifier linkLSIdentifier) {
Priyanka Be412a852015-11-14 17:29:06 +0530139 this.linkLSIdentifier = linkLSIdentifier;
140 }
141
142 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530143 public ProtocolType getProtocolId() throws BgpParseException {
Priyanka Be412a852015-11-14 17:29:06 +0530144 switch (protocolId) {
145 case Constants.ISIS_LEVELONE:
Sho SHIMIZU98fc1e72015-11-23 10:25:22 -0800146 return ProtocolType.ISIS_LEVEL_ONE;
Priyanka Be412a852015-11-14 17:29:06 +0530147 case Constants.ISIS_LEVELTWO:
Sho SHIMIZU98fc1e72015-11-23 10:25:22 -0800148 return ProtocolType.ISIS_LEVEL_TWO;
Priyanka Be412a852015-11-14 17:29:06 +0530149 case Constants.OSPFV2:
Sho SHIMIZU98fc1e72015-11-23 10:25:22 -0800150 return ProtocolType.OSPF_V2;
Priyanka Be412a852015-11-14 17:29:06 +0530151 case Constants.DIRECT:
Sho SHIMIZU98fc1e72015-11-23 10:25:22 -0800152 return ProtocolType.DIRECT;
Priyanka Be412a852015-11-14 17:29:06 +0530153 case Constants.STATIC_CONFIGURATION:
Sho SHIMIZU98fc1e72015-11-23 10:25:22 -0800154 return ProtocolType.STATIC_CONFIGURATION;
Priyanka Be412a852015-11-14 17:29:06 +0530155 case Constants.OSPFV3:
Sho SHIMIZU98fc1e72015-11-23 10:25:22 -0800156 return ProtocolType.OSPF_V3;
Priyanka Be412a852015-11-14 17:29:06 +0530157 default:
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530158 throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, (byte) 0, null);
Priyanka Be412a852015-11-14 17:29:06 +0530159 }
160 }
161
162 @Override
163 public NodeDescriptors localNodeDescriptors() {
164 return this.linkLSIdentifier.localNodeDescriptors();
165 }
166
167 @Override
168 public NodeDescriptors remoteNodeDescriptors() {
169 return this.linkLSIdentifier.remoteNodeDescriptors();
170 }
171
172 /**
173 * Returns whether VPN is present or not.
174 *
175 * @return whether VPN is present or not
176 */
177 public boolean isVpnPresent() {
178 return this.isVpn;
179 }
180
181 @Override
182 public RouteDistinguisher getRouteDistinguisher() {
183 return this.routeDistinguisher;
184 }
185
186 /**
187 * Returns link identifier.
188 *
189 * @return link identifier
190 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530191 public BgpLinkLSIdentifier getLinkIdentifier() {
Priyanka Be412a852015-11-14 17:29:06 +0530192 return this.linkLSIdentifier;
193 }
194
195 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530196 public List<BgpValueType> linkDescriptors() {
Priyanka Be412a852015-11-14 17:29:06 +0530197 return this.linkLSIdentifier.linkDescriptors();
198 }
199
200 @Override
201 public String toString() {
202 return MoreObjects.toStringHelper(getClass())
203 .omitNullValues()
204 .add("protocolId", protocolId)
205 .add("identifier", identifier)
206 .add("RouteDistinguisher ", routeDistinguisher)
207 .add("linkLSIdentifier", linkLSIdentifier)
208 .toString();
209 }
Sho SHIMIZU98fc1e72015-11-23 10:25:22 -0800210}