2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.bgpio.protocol.linkstate;
18 import java.util.List;
20 import org.jboss.netty.buffer.ChannelBuffer;
21 import org.onosproject.bgpio.exceptions.BgpParseException;
22 import org.onosproject.bgpio.protocol.BgpPrefixLSNlri;
23 import org.onosproject.bgpio.protocol.NlriType;
24 import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4.ProtocolType;
25 import org.onosproject.bgpio.types.BgpValueType;
26 import org.onosproject.bgpio.types.RouteDistinguisher;
27 import org.onosproject.bgpio.util.Constants;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import com.google.common.base.MoreObjects;
34 * Implementation of Prefix IPV4 LS NLRI.
36 public class BgpPrefixIPv4LSNlriVer4 implements BgpPrefixLSNlri {
39 * REFERENCE : draft-ietf-idr-ls-distribution-11
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
44 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48 // Local Node Descriptor (variable) //
49 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 // Prefix Descriptors (variable) //
51 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53 Figure : The IPv4/IPv6 Topology Prefix NLRI format
56 protected static final Logger log = LoggerFactory.getLogger(BgpPrefixIPv4LSNlriVer4.class);
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;
64 private BgpPrefixLSIdentifier bgpPrefixLSIdentifier;
69 public BgpPrefixIPv4LSNlriVer4() {
72 this.bgpPrefixLSIdentifier = null;
73 this.routeDistinguisher = null;
78 * Constructor to initialize parameters for BGP PrefixLSNlri.
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
86 public BgpPrefixIPv4LSNlriVer4(long identifier, byte protocolId, BgpPrefixLSIdentifier bgpPrefixLSIdentifier,
87 RouteDistinguisher routeDistinguisher, boolean isVpn) {
88 this.identifier = identifier;
89 this.protocolId = protocolId;
90 this.bgpPrefixLSIdentifier = bgpPrefixLSIdentifier;
91 this.routeDistinguisher = routeDistinguisher;
96 * Reads from channelBuffer and parses Prefix LS Nlri.
98 * @param cb ChannelBuffer
99 * @param afi Address family identifier
100 * @param safi Subsequent address family identifier
101 * @return object of BGPPrefixIPv4LSNlriVer4
102 * @throws BgpParseException while parsing Prefix LS Nlri
104 public static BgpPrefixIPv4LSNlriVer4 read(ChannelBuffer cb, short afi, byte safi) throws BgpParseException {
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);
115 byte protocolId = cb.readByte();
116 long identifier = cb.readLong();
118 BgpPrefixLSIdentifier bgpPrefixLSIdentifier = new BgpPrefixLSIdentifier();
119 bgpPrefixLSIdentifier = BgpPrefixLSIdentifier.parsePrefixIdendifier(cb, protocolId);
120 return new BgpPrefixIPv4LSNlriVer4(identifier, protocolId, bgpPrefixLSIdentifier, routeDistinguisher, isVpn);
124 public NlriType getNlriType() {
125 return NlriType.PREFIX_IPV4;
129 public NodeDescriptors getLocalNodeDescriptors() {
130 return this.bgpPrefixLSIdentifier.getLocalNodeDescriptors();
134 public long getIdentifier() {
135 return this.identifier;
139 * Set the prefix LS identifier.
141 * @param bgpPrefixLSIdentifier prefix identifier to set
143 public void setPrefixLSIdentifier(BgpPrefixLSIdentifier bgpPrefixLSIdentifier) {
144 this.bgpPrefixLSIdentifier = bgpPrefixLSIdentifier;
148 public ProtocolType getProtocolId() throws BgpParseException {
149 switch (protocolId) {
150 case Constants.ISIS_LEVELONE:
151 return ProtocolType.ISIS_LEVEL_ONE;
152 case Constants.ISIS_LEVELTWO:
153 return ProtocolType.ISIS_LEVEL_TWO;
154 case Constants.OSPFV2:
155 return ProtocolType.OSPF_V2;
156 case Constants.DIRECT:
157 return ProtocolType.DIRECT;
158 case Constants.STATIC_CONFIGURATION:
159 return ProtocolType.STATIC_CONFIGURATION;
160 case Constants.OSPFV3:
161 return ProtocolType.OSPF_V3;
163 throw new BgpParseException("protocol id not valid");
168 * Returns whether VPN is present or not.
170 * @return whether VPN is present or not
172 public boolean isVpnPresent() {
177 * Returns Prefix Identifier.
179 * @return Prefix Identifier
181 public BgpPrefixLSIdentifier getPrefixIdentifier() {
182 return this.bgpPrefixLSIdentifier;
186 public RouteDistinguisher getRouteDistinguisher() {
187 return this.routeDistinguisher;
191 public List<BgpValueType> getPrefixdescriptor() {
192 return this.bgpPrefixLSIdentifier.getPrefixdescriptor();
196 public String toString() {
197 return MoreObjects.toStringHelper(getClass())
199 .add("protocolId", protocolId)
200 .add("identifier", identifier)
201 .add("RouteDistinguisher ", routeDistinguisher)
202 .add("bgpPrefixLSIdentifier", bgpPrefixLSIdentifier)