blob: cbe884fe6841980465d72b7ae0571ee36d5db6c7 [file] [log] [blame]
Priyanka B0bee1f82015-10-13 15:39:49 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Priyanka B0bee1f82015-10-13 15:39:49 +05303 *
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
18import org.jboss.netty.buffer.ChannelBuffer;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053019import org.onosproject.bgpio.exceptions.BgpParseException;
20import org.onosproject.bgpio.protocol.BgpNodeLSNlri;
Priyanka B0bee1f82015-10-13 15:39:49 +053021import org.onosproject.bgpio.protocol.NlriType;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053022import org.onosproject.bgpio.types.BgpErrorType;
Priyanka B0bee1f82015-10-13 15:39:49 +053023import org.onosproject.bgpio.types.RouteDistinguisher;
24import org.onosproject.bgpio.util.Constants;
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
28import com.google.common.base.MoreObjects;
29
30/**
31 * Implementation of Node LS NLRI.
32 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053033public class BgpNodeLSNlriVer4 implements BgpNodeLSNlri {
Priyanka B0bee1f82015-10-13 15:39:49 +053034
35 /*
36 *REFERENCE : draft-ietf-idr-ls-distribution-11
37 0 1 2 3
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
39 +-+-+-+-+-+-+-+-+
40 | Protocol-ID |
41 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 | Identifier |
43 | (64 bits) |
44 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 // Local Node Descriptors (variable) //
46 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47
48 Figure : The Node NLRI format
49 */
50
Ray Milkey9c9cde42018-01-12 14:22:06 -080051 private static final Logger log = LoggerFactory.getLogger(BgpNodeLSNlriVer4.class);
Priyanka B0bee1f82015-10-13 15:39:49 +053052
53 public static final int NODE_NLRITYPE = 1;
54 public static final int IDENTIFIER_LENGTH = 16;
55 private long identifier;
56 private byte protocolId;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053057 private BgpNodeLSIdentifier localNodeDescriptors;
Priyanka B0bee1f82015-10-13 15:39:49 +053058 private RouteDistinguisher routeDistinguisher;
59 private boolean isVpn;
60
61 /**
62 * Enum to provide PROTOCOLTYPE.
63 */
Sho SHIMIZU98fc1e72015-11-23 10:25:22 -080064 public enum ProtocolType {
65 ISIS_LEVEL_ONE(1), ISIS_LEVEL_TWO(2), OSPF_V2(3), DIRECT(4), STATIC_CONFIGURATION(5), OSPF_V3(6);
Priyanka B0bee1f82015-10-13 15:39:49 +053066 int value;
67
68 /**
69 * Assign val with the value as the protocol type.
70 *
71 * @param val protocol type
72 */
Sho SHIMIZU98fc1e72015-11-23 10:25:22 -080073 ProtocolType(int val) {
Priyanka B0bee1f82015-10-13 15:39:49 +053074 value = val;
75 }
76
77 /**
78 * Returns value of protocol type.
79 *
80 * @return protocol type
81 */
82 public byte getType() {
83 return (byte) value;
84 }
85 }
86
87 /**
88 * Reset fields.
89 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053090 public BgpNodeLSNlriVer4() {
Priyanka B0bee1f82015-10-13 15:39:49 +053091 this.identifier = 0;
92 this.protocolId = 0;
93 this.localNodeDescriptors = null;
94 this.routeDistinguisher = null;
95 this.isVpn = false;
96 }
97
98 /**
99 * Constructors to initialize its parameters.
100 *
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
106 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530107 public BgpNodeLSNlriVer4(long identifier, byte protocolId, BgpNodeLSIdentifier localNodeDescriptors, boolean isVpn,
Priyanka B0bee1f82015-10-13 15:39:49 +0530108 RouteDistinguisher routeDistinguisher) {
109 this.identifier = identifier;
110 this.protocolId = protocolId;
111 this.localNodeDescriptors = localNodeDescriptors;
112 this.routeDistinguisher = routeDistinguisher;
113 this.isVpn = isVpn;
114 }
115
116 /**
117 * Reads from channelBuffer and parses Node LS Nlri.
118 *
119 * @param cb ChannelBuffer
120 * @param afi Address Family Identifier
121 * @param safi Subsequent Address Family Identifier
122 * @return object of this class
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530123 * @throws BgpParseException while parsing node descriptors
Priyanka B0bee1f82015-10-13 15:39:49 +0530124 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530125 public static BgpNodeLSNlriVer4 read(ChannelBuffer cb, short afi, byte safi) throws BgpParseException {
Priyanka B0bee1f82015-10-13 15:39:49 +0530126 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);
131 isVpn = true;
132 } else {
133 isVpn = false;
134 }
135 byte protocolId = cb.readByte();
136 long identifier = cb.readLong();
137
mohamedrahil00f6f262016-11-24 20:20:41 +0530138 log.debug("Parse local node descriptors");
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530139 BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier();
140 localNodeDescriptors = BgpNodeLSIdentifier.parseLocalNodeDescriptors(cb, protocolId);
141 return new BgpNodeLSNlriVer4(identifier, protocolId, localNodeDescriptors, isVpn, routeDistinguisher);
Priyanka B0bee1f82015-10-13 15:39:49 +0530142 }
143
144 @Override
145 public NlriType getNlriType() {
146 return NlriType.NODE;
147 }
148
149 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530150 public BgpNodeLSIdentifier getLocalNodeDescriptors() {
Priyanka B0bee1f82015-10-13 15:39:49 +0530151 return this.localNodeDescriptors;
152 }
153
154 /**
155 * Returns whether VPN is present or not.
156 *
157 * @return whether VPN is present or not
158 */
159 public boolean isVpnPresent() {
160 return this.isVpn;
161 }
162
163 @Override
164 public RouteDistinguisher getRouteDistinguisher() {
165 return this.routeDistinguisher;
166 }
167
168 @Override
169 public long getIdentifier() {
170 return this.identifier;
171 }
172
173 /**
174 * Set the node LS identifier.
175 *
176 * @param localNodeDescriptors node LS identifier to set
177 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530178 public void setNodeLSIdentifier(BgpNodeLSIdentifier localNodeDescriptors) {
Priyanka B0bee1f82015-10-13 15:39:49 +0530179 this.localNodeDescriptors = localNodeDescriptors;
180 }
181
182 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530183 public ProtocolType getProtocolId() throws BgpParseException {
Priyanka B0bee1f82015-10-13 15:39:49 +0530184 switch (protocolId) {
185 case Constants.ISIS_LEVELONE:
Sho SHIMIZU98fc1e72015-11-23 10:25:22 -0800186 return ProtocolType.ISIS_LEVEL_ONE;
Priyanka B0bee1f82015-10-13 15:39:49 +0530187 case Constants.ISIS_LEVELTWO:
Sho SHIMIZU98fc1e72015-11-23 10:25:22 -0800188 return ProtocolType.ISIS_LEVEL_TWO;
Priyanka B0bee1f82015-10-13 15:39:49 +0530189 case Constants.OSPFV2:
Sho SHIMIZU98fc1e72015-11-23 10:25:22 -0800190 return ProtocolType.OSPF_V2;
Priyanka B0bee1f82015-10-13 15:39:49 +0530191 case Constants.DIRECT:
Sho SHIMIZU98fc1e72015-11-23 10:25:22 -0800192 return ProtocolType.DIRECT;
Priyanka B0bee1f82015-10-13 15:39:49 +0530193 case Constants.STATIC_CONFIGURATION:
Sho SHIMIZU98fc1e72015-11-23 10:25:22 -0800194 return ProtocolType.STATIC_CONFIGURATION;
Priyanka B0bee1f82015-10-13 15:39:49 +0530195 case Constants.OSPFV3:
Sho SHIMIZU98fc1e72015-11-23 10:25:22 -0800196 return ProtocolType.OSPF_V3;
Priyanka B0bee1f82015-10-13 15:39:49 +0530197 default:
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530198 throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, (byte) 0, null);
Priyanka B0bee1f82015-10-13 15:39:49 +0530199 }
200 }
201
202 @Override
203 public String toString() {
204 return MoreObjects.toStringHelper(getClass())
205 .omitNullValues()
206 .add("protocolId", protocolId)
207 .add("identifier", identifier)
208 .add("RouteDistinguisher ", routeDistinguisher)
209 .add("localNodeDescriptors", localNodeDescriptors)
210 .toString();
211 }
Jonathan Hart317f4762015-11-09 16:05:36 -0800212}