blob: 49cb74bd802798b2b8e4eba02d17c8dc4d43a99b [file] [log] [blame]
Priyanka B0bee1f82015-10-13 15:39:49 +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 */
Jonathan Hart317f4762015-11-09 16:05:36 -080016package org.onosproject.bgpio.protocol.linkstate;
Priyanka B0bee1f82015-10-13 15:39:49 +053017
Priyanka B02040732015-11-29 11:30:29 +053018import java.util.List;
Priyanka B0bee1f82015-10-13 15:39:49 +053019
20import org.jboss.netty.buffer.ChannelBuffer;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053021import org.onosproject.bgpio.exceptions.BgpParseException;
22import org.onosproject.bgpio.protocol.BgpPrefixLSNlri;
Priyanka B0bee1f82015-10-13 15:39:49 +053023import 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.BgpValueType;
Priyanka B0bee1f82015-10-13 15:39:49 +053026import org.onosproject.bgpio.types.RouteDistinguisher;
27import org.onosproject.bgpio.util.Constants;
28import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
31import com.google.common.base.MoreObjects;
32
33/**
34 * Implementation of Prefix IPV4 LS NLRI.
35 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053036public class BgpPrefixIPv4LSNlriVer4 implements BgpPrefixLSNlri {
Priyanka B0bee1f82015-10-13 15:39:49 +053037
38 /*
39 * REFERENCE : draft-ietf-idr-ls-distribution-11
40 * 0 1 2 3
41 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
42 +-+-+-+-+-+-+-+-+
43 | Protocol-ID |
44 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 | Identifier |
46 | (64 bits) |
47 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48 // Local Node Descriptor (variable) //
49 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 // Prefix Descriptors (variable) //
51 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52
53 Figure : The IPv4/IPv6 Topology Prefix NLRI format
54 */
55
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053056 protected static final Logger log = LoggerFactory.getLogger(BgpPrefixIPv4LSNlriVer4.class);
Priyanka B0bee1f82015-10-13 15:39:49 +053057
58 public static final int PREFIX_IPV4_NLRITYPE = 3;
59 public static final int IDENTIFIER_LENGTH = 16;
60 private long identifier;
61 private byte protocolId;
62 private RouteDistinguisher routeDistinguisher;
63 private boolean isVpn;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053064 private BgpPrefixLSIdentifier bgpPrefixLSIdentifier;
Priyanka B0bee1f82015-10-13 15:39:49 +053065
66 /**
67 * Resets parameters.
68 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053069 public BgpPrefixIPv4LSNlriVer4() {
Priyanka B0bee1f82015-10-13 15:39:49 +053070 this.identifier = 0;
71 this.protocolId = 0;
72 this.bgpPrefixLSIdentifier = null;
73 this.routeDistinguisher = null;
74 this.isVpn = false;
75 }
76
77 /**
78 * Constructor to initialize parameters for BGP PrefixLSNlri.
79 *
80 * @param identifier field in BGP PrefixLSNlri
81 * @param protocolId protocol Id
82 * @param bgpPrefixLSIdentifier prefix LS Identifier
83 * @param routeDistinguisher RouteDistinguisher
84 * @param isVpn vpn availability in message
85 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053086 public BgpPrefixIPv4LSNlriVer4(long identifier, byte protocolId, BgpPrefixLSIdentifier bgpPrefixLSIdentifier,
Priyanka B0bee1f82015-10-13 15:39:49 +053087 RouteDistinguisher routeDistinguisher, boolean isVpn) {
88 this.identifier = identifier;
89 this.protocolId = protocolId;
90 this.bgpPrefixLSIdentifier = bgpPrefixLSIdentifier;
91 this.routeDistinguisher = routeDistinguisher;
92 this.isVpn = isVpn;
93 }
94
95 /**
96 * Reads from channelBuffer and parses Prefix LS Nlri.
97 *
98 * @param cb ChannelBuffer
99 * @param afi Address family identifier
100 * @param safi Subsequent address family identifier
101 * @return object of BGPPrefixIPv4LSNlriVer4
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530102 * @throws BgpParseException while parsing Prefix LS Nlri
Priyanka B0bee1f82015-10-13 15:39:49 +0530103 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530104 public static BgpPrefixIPv4LSNlriVer4 read(ChannelBuffer cb, short afi, byte safi) throws BgpParseException {
Priyanka B0bee1f82015-10-13 15:39:49 +0530105
106 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 BgpPrefixLSIdentifier bgpPrefixLSIdentifier = new BgpPrefixLSIdentifier();
119 bgpPrefixLSIdentifier = BgpPrefixLSIdentifier.parsePrefixIdendifier(cb, protocolId);
120 return new BgpPrefixIPv4LSNlriVer4(identifier, protocolId, bgpPrefixLSIdentifier, routeDistinguisher, isVpn);
Priyanka B0bee1f82015-10-13 15:39:49 +0530121 }
122
123 @Override
124 public NlriType getNlriType() {
125 return NlriType.PREFIX_IPV4;
126 }
127
128 @Override
129 public NodeDescriptors getLocalNodeDescriptors() {
130 return this.bgpPrefixLSIdentifier.getLocalNodeDescriptors();
131 }
132
133 @Override
134 public long getIdentifier() {
135 return this.identifier;
136 }
137
138 /**
139 * Set the prefix LS identifier.
140 *
141 * @param bgpPrefixLSIdentifier prefix identifier to set
142 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530143 public void setPrefixLSIdentifier(BgpPrefixLSIdentifier bgpPrefixLSIdentifier) {
Priyanka B0bee1f82015-10-13 15:39:49 +0530144 this.bgpPrefixLSIdentifier = bgpPrefixLSIdentifier;
145 }
146
147 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530148 public ProtocolType getProtocolId() throws BgpParseException {
Priyanka B0bee1f82015-10-13 15:39:49 +0530149 switch (protocolId) {
150 case Constants.ISIS_LEVELONE:
Sho SHIMIZU98fc1e72015-11-23 10:25:22 -0800151 return ProtocolType.ISIS_LEVEL_ONE;
Priyanka B0bee1f82015-10-13 15:39:49 +0530152 case Constants.ISIS_LEVELTWO:
Sho SHIMIZU98fc1e72015-11-23 10:25:22 -0800153 return ProtocolType.ISIS_LEVEL_TWO;
Priyanka B0bee1f82015-10-13 15:39:49 +0530154 case Constants.OSPFV2:
Sho SHIMIZU98fc1e72015-11-23 10:25:22 -0800155 return ProtocolType.OSPF_V2;
Priyanka B0bee1f82015-10-13 15:39:49 +0530156 case Constants.DIRECT:
Sho SHIMIZU98fc1e72015-11-23 10:25:22 -0800157 return ProtocolType.DIRECT;
Priyanka B0bee1f82015-10-13 15:39:49 +0530158 case Constants.STATIC_CONFIGURATION:
Sho SHIMIZU98fc1e72015-11-23 10:25:22 -0800159 return ProtocolType.STATIC_CONFIGURATION;
Priyanka B0bee1f82015-10-13 15:39:49 +0530160 case Constants.OSPFV3:
Sho SHIMIZU98fc1e72015-11-23 10:25:22 -0800161 return ProtocolType.OSPF_V3;
Priyanka B0bee1f82015-10-13 15:39:49 +0530162 default:
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530163 throw new BgpParseException("protocol id not valid");
Priyanka B0bee1f82015-10-13 15:39:49 +0530164 }
165 }
166
167 /**
168 * Returns whether VPN is present or not.
169 *
170 * @return whether VPN is present or not
171 */
172 public boolean isVpnPresent() {
173 return this.isVpn;
174 }
175
176 /**
177 * Returns Prefix Identifier.
178 *
179 * @return Prefix Identifier
180 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530181 public BgpPrefixLSIdentifier getPrefixIdentifier() {
Priyanka B0bee1f82015-10-13 15:39:49 +0530182 return this.bgpPrefixLSIdentifier;
183 }
184
185 @Override
186 public RouteDistinguisher getRouteDistinguisher() {
187 return this.routeDistinguisher;
188 }
189
190 @Override
Priyanka B02040732015-11-29 11:30:29 +0530191 public List<BgpValueType> getPrefixdescriptor() {
Priyanka B0bee1f82015-10-13 15:39:49 +0530192 return this.bgpPrefixLSIdentifier.getPrefixdescriptor();
193 }
194
195 @Override
196 public String toString() {
197 return MoreObjects.toStringHelper(getClass())
198 .omitNullValues()
199 .add("protocolId", protocolId)
200 .add("identifier", identifier)
201 .add("RouteDistinguisher ", routeDistinguisher)
202 .add("bgpPrefixLSIdentifier", bgpPrefixLSIdentifier)
203 .toString();
204 }
Jonathan Hart317f4762015-11-09 16:05:36 -0800205}