blob: 0f18c757cfdee5d1d853e7b730fc0d446d38e531 [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 */
16package org.onosproject.bgpio.protocol.link_state;
17
18import java.util.LinkedList;
19
20import org.jboss.netty.buffer.ChannelBuffer;
21import org.onosproject.bgpio.exceptions.BGPParseException;
22import org.onosproject.bgpio.protocol.BGPPrefixLSNlri;
23import org.onosproject.bgpio.protocol.NlriType;
24import org.onosproject.bgpio.protocol.link_state.BGPNodeLSNlriVer4.PROTOCOLTYPE;
25import org.onosproject.bgpio.types.BGPValueType;
26import 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 */
36public class BGPPrefixIPv4LSNlriVer4 implements BGPPrefixLSNlri {
37
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
56 protected static final Logger log = LoggerFactory.getLogger(BGPPrefixIPv4LSNlriVer4.class);
57
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;
65
66 /**
67 * Resets parameters.
68 */
69 public BGPPrefixIPv4LSNlriVer4() {
70 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 */
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;
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
102 * @throws BGPParseException while parsing Prefix LS Nlri
103 */
104 public static BGPPrefixIPv4LSNlriVer4 read(ChannelBuffer cb, short afi, byte safi) throws BGPParseException {
105
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
118 BGPPrefixLSIdentifier bgpPrefixLSIdentifier = new BGPPrefixLSIdentifier();
119 bgpPrefixLSIdentifier = BGPPrefixLSIdentifier.parsePrefixIdendifier(cb, protocolId);
120 return new BGPPrefixIPv4LSNlriVer4(identifier, protocolId, bgpPrefixLSIdentifier, routeDistinguisher, isVpn);
121 }
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 */
143 public void setPrefixLSIdentifier(BGPPrefixLSIdentifier bgpPrefixLSIdentifier) {
144 this.bgpPrefixLSIdentifier = bgpPrefixLSIdentifier;
145 }
146
147 @Override
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;
162 default:
163 throw new BGPParseException("protocol id not valid");
164 }
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 */
181 public BGPPrefixLSIdentifier getPrefixIdentifier() {
182 return this.bgpPrefixLSIdentifier;
183 }
184
185 @Override
186 public RouteDistinguisher getRouteDistinguisher() {
187 return this.routeDistinguisher;
188 }
189
190 @Override
191 public LinkedList<BGPValueType> getPrefixdescriptor() {
192 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 }
205}