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.link_state;
18 import java.util.LinkedList;
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.link_state.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_LevelOne;
152 case Constants.ISIS_LEVELTWO:
153 return PROTOCOLTYPE.ISIS_LevelTwo;
154 case Constants.OSPFV2:
155 return PROTOCOLTYPE.OSPFv2;
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.OSPFv3;
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 LinkedList<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)