blob: f76c40157366a41ba6d040ed7c0e63b5cbafce23 [file] [log] [blame]
Thejaswi N K38879622015-12-08 22:14:47 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thejaswi N K38879622015-12-08 22:14:47 +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 */
16package org.onosproject.provider.bgp.cfg.impl;
17
18import com.fasterxml.jackson.databind.JsonNode;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070019import org.osgi.service.component.annotations.Reference;
20import org.osgi.service.component.annotations.ReferenceCardinality;
Thejaswi N K38879622015-12-08 22:14:47 +053021import org.onlab.osgi.DefaultServiceDirectory;
22import org.onlab.packet.IpAddress;
23import org.onosproject.bgp.controller.BgpCfg;
24import org.onosproject.bgp.controller.BgpController;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.net.config.Config;
mohamedrahil00f6f262016-11-24 20:20:41 +053027import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
Thejaswi N K38879622015-12-08 22:14:47 +053029
30import java.util.ArrayList;
31import java.util.List;
32
Mohammad Shahid30fedc52017-08-09 11:49:40 +053033import static com.google.common.base.Preconditions.checkNotNull;
Thejaswi N K38879622015-12-08 22:14:47 +053034import static org.onosproject.net.config.Config.FieldPresence.MANDATORY;
35import static org.onosproject.net.config.Config.FieldPresence.OPTIONAL;
Thejaswi N K38879622015-12-08 22:14:47 +053036
37/**
38 * Configuration object for BGP.
39 */
40public class BgpAppConfig extends Config<ApplicationId> {
Ray Milkeyd84f89b2018-08-17 14:54:17 -070041 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thejaswi N K38879622015-12-08 22:14:47 +053042 BgpController bgpController;
43
44 BgpCfg bgpConfig = null;
45
mohamedrahil00f6f262016-11-24 20:20:41 +053046 protected final Logger log = LoggerFactory.getLogger(BgpAppConfig.class);
Thejaswi N K38879622015-12-08 22:14:47 +053047 public static final String ROUTER_ID = "routerId";
48 public static final String LOCAL_AS = "localAs";
49 public static final String MAX_SESSION = "maxSession";
50 public static final String LS_CAPABILITY = "lsCapability";
51 public static final String HOLD_TIME = "holdTime";
52 public static final String LARGE_AS_CAPABILITY = "largeAsCapability";
Shashikanth VH580bdeb2016-02-19 17:26:03 +053053 public static final String FLOW_SPEC_CAPABILITY = "flowSpecCapability";
Shashikanth VHb650bfa2016-04-18 12:54:03 +053054 public static final String FLOW_SPEC_RPD_CAPABILITY = "flowSpecRpdCapability";
Mohammad Shahid30fedc52017-08-09 11:49:40 +053055 public static final String EVPN_CAPABILITY = "evpnCapability";
Thejaswi N K38879622015-12-08 22:14:47 +053056
57 public static final String BGP_PEER = "bgpPeer";
58 public static final String PEER_IP = "peerIp";
59 public static final String REMOTE_AS = "remoteAs";
60 public static final String PEER_HOLD_TIME = "peerHoldTime";
Thejaswi N K0008f1d2015-12-11 12:47:41 +053061 public static final String PEER_CONNECT_MODE = "connectMode";
62 public static final String PEER_CONNECT_PASSIVE = "passive";
63 public static final String PEER_CONNECT_ACTIVE = "active";
Thejaswi N K38879622015-12-08 22:14:47 +053064
Ankur Aggarwalf363d1a2019-11-11 14:56:18 +053065 public static final String CONNECTION_TYPE = "connectionType";
66 public static final String CONNECTION_TYPE_IPV4 = "IPV4";
67 public static final String CONNECTION_TYPE_IPV6 = "IPV6";
68 public static final String CONNECTION_TYPE_IPV4_AND_IPV6 = "IPV4_IPV6";
69
Thejaswi N K38879622015-12-08 22:14:47 +053070 static final int MAX_SHORT_AS_NUMBER = 65535;
71 static final long MAX_LONG_AS_NUMBER = 4294967295L;
72
Shashikanth VHde663832016-04-25 18:42:04 +053073 static final int MIN_SESSION_NUMBER = 1;
74 static final long MAX_SESSION_NUMBER = 21;
75
76 static final int MIN_HOLDTIME = 0;
77 static final long MAX_HOLDTIME = 65535;
78
Thejaswi N K38879622015-12-08 22:14:47 +053079 @Override
80 public boolean isValid() {
81 boolean fields = false;
82
83 this.bgpController = DefaultServiceDirectory.getService(BgpController.class);
84 bgpConfig = bgpController.getConfig();
85
86 fields = hasOnlyFields(ROUTER_ID, LOCAL_AS, MAX_SESSION, LS_CAPABILITY,
Mohammad Shahid30fedc52017-08-09 11:49:40 +053087 HOLD_TIME, LARGE_AS_CAPABILITY, FLOW_SPEC_CAPABILITY,
Ankur Aggarwalf363d1a2019-11-11 14:56:18 +053088 FLOW_SPEC_RPD_CAPABILITY, BGP_PEER, EVPN_CAPABILITY, CONNECTION_TYPE) &&
Thejaswi N K38879622015-12-08 22:14:47 +053089 isIpAddress(ROUTER_ID, MANDATORY) && isNumber(LOCAL_AS, MANDATORY) &&
Shashikanth VHde663832016-04-25 18:42:04 +053090 isNumber(MAX_SESSION, OPTIONAL, MIN_SESSION_NUMBER, MAX_SESSION_NUMBER)
91 && isNumber(HOLD_TIME, OPTIONAL, MIN_HOLDTIME, MAX_HOLDTIME) &&
Shashikanth VH580bdeb2016-02-19 17:26:03 +053092 isBoolean(LS_CAPABILITY, OPTIONAL) && isBoolean(LARGE_AS_CAPABILITY, OPTIONAL) &&
Mohammad Shahid30fedc52017-08-09 11:49:40 +053093 isString(FLOW_SPEC_CAPABILITY, OPTIONAL) && isBoolean(FLOW_SPEC_RPD_CAPABILITY, OPTIONAL)
Ankur Aggarwalf363d1a2019-11-11 14:56:18 +053094 && isBoolean(EVPN_CAPABILITY, OPTIONAL) && isString(CONNECTION_TYPE, OPTIONAL);
Thejaswi N K38879622015-12-08 22:14:47 +053095
96 if (!fields) {
97 return fields;
98 }
99
100 return validateBgpConfiguration();
101 }
102
103 /**
104 * Returns routerId from the configuration.
105 *
106 * @return routerId
107 */
108 public String routerId() {
109 return get(ROUTER_ID, null);
110 }
111
112 /**
113 * Returns localAs number from the configuration.
114 *
115 * @return local As number
116 */
117 public int localAs() {
118 return Integer.parseInt(get(LOCAL_AS, null));
119 }
120
121 /**
122 * Returns max session from the configuration.
123 *
124 * @return max session
125 */
126 public int maxSession() {
127 return Integer.parseInt(get(MAX_SESSION, null));
128 }
129
130 /**
131 * Returns BGP-LS capability support from the configuration.
132 *
133 * @return true if BGP-LS capability is set else false
134 */
135 public boolean lsCapability() {
136 return Boolean.parseBoolean(get(LS_CAPABILITY, null));
137 }
138
139 /**
Shashikanth VHb650bfa2016-04-18 12:54:03 +0530140 * Returns flow spec route policy distribution capability support from the configuration.
141 *
142 * @return true if flow spec route policy distribution capability is set otherwise false
143 */
144 public boolean rpdCapability() {
145 return Boolean.parseBoolean(get(FLOW_SPEC_RPD_CAPABILITY, null));
146 }
147
148 /**
Thejaswi N K38879622015-12-08 22:14:47 +0530149 * Returns largeAs capability support from the configuration.
150 *
151 * @return largeAs capability
152 */
153 public boolean largeAsCapability() {
154 return Boolean.parseBoolean(get(LARGE_AS_CAPABILITY, null));
155 }
156
157 /**
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530158 * Returns flow specification capability support from the configuration.
159 *
160 * @return flow specification capability
161 */
162 public String flowSpecCapability() {
163 return get(FLOW_SPEC_CAPABILITY, null);
164 }
165
166 /**
Thejaswi N K38879622015-12-08 22:14:47 +0530167 * Returns holdTime of the local node from the configuration.
168 *
169 * @return holdTime
170 */
171 public short holdTime() {
172 return Short.parseShort(get(HOLD_TIME, null));
173 }
174
175 /**
Ankur Aggarwalf363d1a2019-11-11 14:56:18 +0530176 * Returns BGP connection type from the configuration.
177 *
178 * @return BGP connection type
179 */
180 public String connectionType() {
181 return get(CONNECTION_TYPE, null);
182 }
183
184 /**
185 * Validates the BGP connection type.
186 *
187 * @return true if valid else false
188 */
189 public boolean validateConnectionType() {
190 if (connectionType() != null) {
191 String connectionType = connectionType();
192 if (!connectionType.equals(CONNECTION_TYPE_IPV4) && !connectionType.equals(CONNECTION_TYPE_IPV6)
193 && !connectionType.equals(CONNECTION_TYPE_IPV4_AND_IPV6)) {
194 log.error("Connection Type is invalid");
195 return false;
196 }
197 }
198 log.debug("Connection Type is valid");
199 return true;
200 }
201
202 /**
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530203 * Validates the flow specification capability.
204 *
205 * @return true if valid else false
206 */
207 public boolean validateFlowSpec() {
208 if (flowSpecCapability() != null) {
209 String flowSpec = flowSpecCapability();
Shashikanth VH09792f02016-05-10 16:59:57 +0530210 if ((!flowSpec.equals("IPV4")) && (!flowSpec.equals("VPNV4")) && (!flowSpec.equals("IPV4_VPNV4"))) {
mohamedrahil00f6f262016-11-24 20:20:41 +0530211 log.debug("Flow specification capabality is false");
Shashikanth VH09792f02016-05-10 16:59:57 +0530212 return false;
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530213 }
214 }
mohamedrahil00f6f262016-11-24 20:20:41 +0530215 log.debug("Flow specification capabality is true");
Shashikanth VH09792f02016-05-10 16:59:57 +0530216 return true;
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530217 }
218
219 /**
Mohammad Shahid30fedc52017-08-09 11:49:40 +0530220 * Returns evpn capability support from the configuration.
221 *
222 * @return evpn capability
223 */
224 public boolean evpnCapability() {
225 return Boolean.parseBoolean(get(EVPN_CAPABILITY, null));
226 }
227
228 /**
Shashikanth VHde663832016-04-25 18:42:04 +0530229 * Validates the hold time value.
230 *
231 * @return true if valid else false
232 */
233 public boolean validateHoldTime() {
234 if (holdTime() != 0) {
235 short holdTime = holdTime();
236 if ((holdTime == 1) || (holdTime == 2)) {
237 return false;
238 }
239 }
240 return true;
241 }
242
243 /**
Thejaswi N K38879622015-12-08 22:14:47 +0530244 * Validates the Bgp local and peer configuration.
245 *
246 * @return true if valid else false
247 */
248 public boolean validateBgpConfiguration() {
249
250 if (!validateLocalAs()) {
251 return false;
252 }
253
254 if (!validateRouterId()) {
255 return false;
256 }
257
258 if (!validateBgpPeers()) {
259 return false;
260 }
261
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530262 if (!validateFlowSpec()) {
263 return false;
264 }
Shashikanth VHde663832016-04-25 18:42:04 +0530265
266 if (!validateHoldTime()) {
267 return false;
268 }
Ankur Aggarwalf363d1a2019-11-11 14:56:18 +0530269
270 if (!validateConnectionType()) {
271 return false;
272 }
Thejaswi N K38879622015-12-08 22:14:47 +0530273 return true;
274 }
275
276 /**
277 * Validates the Bgp As number.
278 *
279 * @return true if valid else false
280 */
281 public boolean validateLocalAs() {
282
283 long localAs = 0;
284 localAs = localAs();
285
Thejaswi N K38879622015-12-08 22:14:47 +0530286 if (largeAsCapability()) {
287
288 if (localAs == 0 || localAs >= MAX_LONG_AS_NUMBER) {
289 return false;
290 }
291 } else {
292 if (localAs == 0 || localAs >= MAX_SHORT_AS_NUMBER) {
293 return false;
294 }
295 }
296
297 return true;
298 }
299
300 /**
301 * Validates the Bgp peer As number.
302 *
Jian Lidfba7392016-01-22 16:46:58 -0800303 * @param remoteAs remote As number
Thejaswi N K38879622015-12-08 22:14:47 +0530304 * @return true if valid else false
305 */
306 public boolean validateRemoteAs(long remoteAs) {
307 if (largeAsCapability()) {
308
309 if (remoteAs == 0 || remoteAs >= MAX_LONG_AS_NUMBER) {
310 return false;
311 }
312 } else {
313 if (remoteAs == 0 || remoteAs >= MAX_SHORT_AS_NUMBER) {
314 return false;
315 }
316 }
317 return true;
318 }
319
320 /**
321 * Validates the Bgp Router ID configuration.
322 *
323 * @return true if valid else false
324 */
325 public boolean validateRouterId() {
326 String routerId = routerId();
Shashikanth VHde663832016-04-25 18:42:04 +0530327 // TODO: router ID validation
Thejaswi N K38879622015-12-08 22:14:47 +0530328 return true;
329 }
330
331 /**
332 * Validates the Bgp peer holdTime.
333 *
Jian Lidfba7392016-01-22 16:46:58 -0800334 * @param remoteAs remote As number
Thejaswi N K38879622015-12-08 22:14:47 +0530335 * @return true if valid else false
336 */
337 public boolean validatePeerHoldTime(long remoteAs) {
338 //TODO:Validate it later..
339 return true;
340 }
341
342 /**
343 * Validates the Bgp peer configuration.
344 *
345 * @return true if valid else false
346 */
347 public boolean validateBgpPeers() {
348 List<BgpPeerConfig> nodes;
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530349 String connectMode;
Thejaswi N K38879622015-12-08 22:14:47 +0530350
351 nodes = bgpPeer();
352 for (int i = 0; i < nodes.size(); i++) {
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530353 connectMode = nodes.get(i).connectMode();
Thejaswi N K38879622015-12-08 22:14:47 +0530354 if ((IpAddress.valueOf(nodes.get(i).hostname()) == null) ||
355 !validateRemoteAs(nodes.get(i).asNumber()) ||
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530356 !validatePeerHoldTime(nodes.get(i).holdTime()) ||
357 !(connectMode.equals(PEER_CONNECT_ACTIVE) || connectMode.equals(PEER_CONNECT_PASSIVE))) {
mohamedrahil00f6f262016-11-24 20:20:41 +0530358 log.debug("BGP peer configration false");
Thejaswi N K38879622015-12-08 22:14:47 +0530359 return false;
360 }
361 }
mohamedrahil00f6f262016-11-24 20:20:41 +0530362 log.debug("BGP peer configration true");
Thejaswi N K38879622015-12-08 22:14:47 +0530363 return true;
364 }
365
366 /**
367 * Returns the set of nodes read from network config.
368 *
369 * @return list of BgpPeerConfig or null
370 */
371 public List<BgpPeerConfig> bgpPeer() {
372 List<BgpPeerConfig> nodes = new ArrayList<BgpPeerConfig>();
Thejaswi N K38879622015-12-08 22:14:47 +0530373 JsonNode jsonNodes = object.get(BGP_PEER);
374 if (jsonNodes == null) {
375 return null;
376 }
377
378 jsonNodes.forEach(jsonNode -> nodes.add(new BgpPeerConfig(
379 jsonNode.path(PEER_IP).asText(),
380 jsonNode.path(REMOTE_AS).asInt(),
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530381 jsonNode.path(PEER_HOLD_TIME).asInt(),
382 jsonNode.path(PEER_CONNECT_MODE).asText())));
Thejaswi N K38879622015-12-08 22:14:47 +0530383
384 return nodes;
385 }
386
387 /**
388 * Configuration for Bgp peer nodes.
389 */
390 public static class BgpPeerConfig {
391
392 private final String hostname;
393 private final int asNumber;
394 private final short holdTime;
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530395 private final String connectMode;
Thejaswi N K38879622015-12-08 22:14:47 +0530396
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530397 public BgpPeerConfig(String hostname, int asNumber, int holdTime, String connectMode) {
Thejaswi N K38879622015-12-08 22:14:47 +0530398 this.hostname = checkNotNull(hostname);
399 this.asNumber = asNumber;
400 this.holdTime = (short) holdTime;
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530401 this.connectMode = connectMode;
Thejaswi N K38879622015-12-08 22:14:47 +0530402 }
403
404 /**
405 * Returns hostname of the peer node.
406 *
407 * @return hostname
408 */
409 public String hostname() {
410 return this.hostname;
411 }
412
413 /**
414 * Returns asNumber if peer.
415 *
416 * @return asNumber
417 */
418 public int asNumber() {
419 return this.asNumber;
420 }
421
422 /**
423 * Returns holdTime of the peer node.
424 *
425 * @return holdTime
426 */
427 public short holdTime() {
428 return this.holdTime;
429 }
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530430
431 /**
432 * Returns connection mode for the peer node.
433 *
434 * @return active or passive connection
435 */
436 public String connectMode() {
437 return this.connectMode;
438 }
Thejaswi N K38879622015-12-08 22:14:47 +0530439 }
440}