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 org.jboss.netty.buffer.ChannelBuffer;
19 import org.onosproject.bgpio.exceptions.BgpParseException;
20 import org.onosproject.bgpio.protocol.BgpNodeLSNlri;
21 import org.onosproject.bgpio.protocol.NlriType;
22 import org.onosproject.bgpio.types.BgpErrorType;
23 import org.onosproject.bgpio.types.RouteDistinguisher;
24 import org.onosproject.bgpio.util.Constants;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import com.google.common.base.MoreObjects;
31 * Implementation of Node LS NLRI.
33 public class BgpNodeLSNlriVer4 implements BgpNodeLSNlri {
36 *REFERENCE : draft-ietf-idr-ls-distribution-11
38 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
41 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 // Local Node Descriptors (variable) //
46 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48 Figure : The Node NLRI format
51 protected static final Logger log = LoggerFactory.getLogger(BgpNodeLSNlriVer4.class);
53 public static final int NODE_NLRITYPE = 1;
54 public static final int IDENTIFIER_LENGTH = 16;
55 private long identifier;
56 private byte protocolId;
57 private BgpNodeLSIdentifier localNodeDescriptors;
58 private RouteDistinguisher routeDistinguisher;
59 private boolean isVpn;
62 * Enum to provide PROTOCOLTYPE.
64 public enum ProtocolType {
65 ISIS_LEVEL_ONE(1), ISIS_LEVEL_TWO(2), OSPF_V2(3), DIRECT(4), STATIC_CONFIGURATION(5), OSPF_V3(6);
69 * Assign val with the value as the protocol type.
71 * @param val protocol type
73 ProtocolType(int val) {
78 * Returns value of protocol type.
80 * @return protocol type
82 public byte getType() {
90 public BgpNodeLSNlriVer4() {
93 this.localNodeDescriptors = null;
94 this.routeDistinguisher = null;
99 * Constructors to initialize its parameters.
101 * @param identifier of LinkState Nlri
102 * @param protocolId of LinkState Nlri
103 * @param localNodeDescriptors local node descriptors
104 * @param isVpn true if VPN info is present
105 * @param routeDistinguisher unique for each VPN
107 public BgpNodeLSNlriVer4(long identifier, byte protocolId, BgpNodeLSIdentifier localNodeDescriptors, boolean isVpn,
108 RouteDistinguisher routeDistinguisher) {
109 this.identifier = identifier;
110 this.protocolId = protocolId;
111 this.localNodeDescriptors = localNodeDescriptors;
112 this.routeDistinguisher = routeDistinguisher;
117 * Reads from channelBuffer and parses Node LS Nlri.
119 * @param cb ChannelBuffer
120 * @param afi Address Family Identifier
121 * @param safi Subsequent Address Family Identifier
122 * @return object of this class
123 * @throws BgpParseException while parsing node descriptors
125 public static BgpNodeLSNlriVer4 read(ChannelBuffer cb, short afi, byte safi) throws BgpParseException {
126 boolean isVpn = false;
127 RouteDistinguisher routeDistinguisher = null;
128 if ((afi == Constants.AFI_VALUE) && (safi == Constants.VPN_SAFI_VALUE)) {
129 routeDistinguisher = new RouteDistinguisher();
130 routeDistinguisher = RouteDistinguisher.read(cb);
135 byte protocolId = cb.readByte();
136 long identifier = cb.readLong();
138 // Parse Local Node Descriptors
139 BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier();
140 localNodeDescriptors = BgpNodeLSIdentifier.parseLocalNodeDescriptors(cb, protocolId);
141 return new BgpNodeLSNlriVer4(identifier, protocolId, localNodeDescriptors, isVpn, routeDistinguisher);
145 public NlriType getNlriType() {
146 return NlriType.NODE;
150 public BgpNodeLSIdentifier getLocalNodeDescriptors() {
151 return this.localNodeDescriptors;
155 * Returns whether VPN is present or not.
157 * @return whether VPN is present or not
159 public boolean isVpnPresent() {
164 public RouteDistinguisher getRouteDistinguisher() {
165 return this.routeDistinguisher;
169 public long getIdentifier() {
170 return this.identifier;
174 * Set the node LS identifier.
176 * @param localNodeDescriptors node LS identifier to set
178 public void setNodeLSIdentifier(BgpNodeLSIdentifier localNodeDescriptors) {
179 this.localNodeDescriptors = localNodeDescriptors;
183 public ProtocolType getProtocolId() throws BgpParseException {
184 switch (protocolId) {
185 case Constants.ISIS_LEVELONE:
186 return ProtocolType.ISIS_LEVEL_ONE;
187 case Constants.ISIS_LEVELTWO:
188 return ProtocolType.ISIS_LEVEL_TWO;
189 case Constants.OSPFV2:
190 return ProtocolType.OSPF_V2;
191 case Constants.DIRECT:
192 return ProtocolType.DIRECT;
193 case Constants.STATIC_CONFIGURATION:
194 return ProtocolType.STATIC_CONFIGURATION;
195 case Constants.OSPFV3:
196 return ProtocolType.OSPF_V3;
198 throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, (byte) 0, null);
203 public String toString() {
204 return MoreObjects.toStringHelper(getClass())
206 .add("protocolId", protocolId)
207 .add("identifier", identifier)
208 .add("RouteDistinguisher ", routeDistinguisher)
209 .add("localNodeDescriptors", localNodeDescriptors)