blob: 23a4f5af6217baeb0808840930ac4813f6dc5ec9 [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;
19import org.apache.felix.scr.annotations.Reference;
20import org.apache.felix.scr.annotations.ReferenceCardinality;
21import 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> {
41 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
42 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
65 static final int MAX_SHORT_AS_NUMBER = 65535;
66 static final long MAX_LONG_AS_NUMBER = 4294967295L;
67
Shashikanth VHde663832016-04-25 18:42:04 +053068 static final int MIN_SESSION_NUMBER = 1;
69 static final long MAX_SESSION_NUMBER = 21;
70
71 static final int MIN_HOLDTIME = 0;
72 static final long MAX_HOLDTIME = 65535;
73
Thejaswi N K38879622015-12-08 22:14:47 +053074 @Override
75 public boolean isValid() {
76 boolean fields = false;
77
78 this.bgpController = DefaultServiceDirectory.getService(BgpController.class);
79 bgpConfig = bgpController.getConfig();
80
81 fields = hasOnlyFields(ROUTER_ID, LOCAL_AS, MAX_SESSION, LS_CAPABILITY,
Mohammad Shahid30fedc52017-08-09 11:49:40 +053082 HOLD_TIME, LARGE_AS_CAPABILITY, FLOW_SPEC_CAPABILITY,
83 FLOW_SPEC_RPD_CAPABILITY, BGP_PEER, EVPN_CAPABILITY) &&
Thejaswi N K38879622015-12-08 22:14:47 +053084 isIpAddress(ROUTER_ID, MANDATORY) && isNumber(LOCAL_AS, MANDATORY) &&
Shashikanth VHde663832016-04-25 18:42:04 +053085 isNumber(MAX_SESSION, OPTIONAL, MIN_SESSION_NUMBER, MAX_SESSION_NUMBER)
86 && isNumber(HOLD_TIME, OPTIONAL, MIN_HOLDTIME, MAX_HOLDTIME) &&
Shashikanth VH580bdeb2016-02-19 17:26:03 +053087 isBoolean(LS_CAPABILITY, OPTIONAL) && isBoolean(LARGE_AS_CAPABILITY, OPTIONAL) &&
Mohammad Shahid30fedc52017-08-09 11:49:40 +053088 isString(FLOW_SPEC_CAPABILITY, OPTIONAL) && isBoolean(FLOW_SPEC_RPD_CAPABILITY, OPTIONAL)
89 && isBoolean(EVPN_CAPABILITY, OPTIONAL);
Thejaswi N K38879622015-12-08 22:14:47 +053090
91 if (!fields) {
92 return fields;
93 }
94
95 return validateBgpConfiguration();
96 }
97
98 /**
99 * Returns routerId from the configuration.
100 *
101 * @return routerId
102 */
103 public String routerId() {
104 return get(ROUTER_ID, null);
105 }
106
107 /**
108 * Returns localAs number from the configuration.
109 *
110 * @return local As number
111 */
112 public int localAs() {
113 return Integer.parseInt(get(LOCAL_AS, null));
114 }
115
116 /**
117 * Returns max session from the configuration.
118 *
119 * @return max session
120 */
121 public int maxSession() {
122 return Integer.parseInt(get(MAX_SESSION, null));
123 }
124
125 /**
126 * Returns BGP-LS capability support from the configuration.
127 *
128 * @return true if BGP-LS capability is set else false
129 */
130 public boolean lsCapability() {
131 return Boolean.parseBoolean(get(LS_CAPABILITY, null));
132 }
133
134 /**
Shashikanth VHb650bfa2016-04-18 12:54:03 +0530135 * Returns flow spec route policy distribution capability support from the configuration.
136 *
137 * @return true if flow spec route policy distribution capability is set otherwise false
138 */
139 public boolean rpdCapability() {
140 return Boolean.parseBoolean(get(FLOW_SPEC_RPD_CAPABILITY, null));
141 }
142
143 /**
Thejaswi N K38879622015-12-08 22:14:47 +0530144 * Returns largeAs capability support from the configuration.
145 *
146 * @return largeAs capability
147 */
148 public boolean largeAsCapability() {
149 return Boolean.parseBoolean(get(LARGE_AS_CAPABILITY, null));
150 }
151
152 /**
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530153 * Returns flow specification capability support from the configuration.
154 *
155 * @return flow specification capability
156 */
157 public String flowSpecCapability() {
158 return get(FLOW_SPEC_CAPABILITY, null);
159 }
160
161 /**
Thejaswi N K38879622015-12-08 22:14:47 +0530162 * Returns holdTime of the local node from the configuration.
163 *
164 * @return holdTime
165 */
166 public short holdTime() {
167 return Short.parseShort(get(HOLD_TIME, null));
168 }
169
170 /**
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530171 * Validates the flow specification capability.
172 *
173 * @return true if valid else false
174 */
175 public boolean validateFlowSpec() {
176 if (flowSpecCapability() != null) {
177 String flowSpec = flowSpecCapability();
Shashikanth VH09792f02016-05-10 16:59:57 +0530178 if ((!flowSpec.equals("IPV4")) && (!flowSpec.equals("VPNV4")) && (!flowSpec.equals("IPV4_VPNV4"))) {
mohamedrahil00f6f262016-11-24 20:20:41 +0530179 log.debug("Flow specification capabality is false");
Shashikanth VH09792f02016-05-10 16:59:57 +0530180 return false;
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530181 }
182 }
mohamedrahil00f6f262016-11-24 20:20:41 +0530183 log.debug("Flow specification capabality is true");
Shashikanth VH09792f02016-05-10 16:59:57 +0530184 return true;
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530185 }
186
187 /**
Mohammad Shahid30fedc52017-08-09 11:49:40 +0530188 * Returns evpn capability support from the configuration.
189 *
190 * @return evpn capability
191 */
192 public boolean evpnCapability() {
193 return Boolean.parseBoolean(get(EVPN_CAPABILITY, null));
194 }
195
196 /**
Shashikanth VHde663832016-04-25 18:42:04 +0530197 * Validates the hold time value.
198 *
199 * @return true if valid else false
200 */
201 public boolean validateHoldTime() {
202 if (holdTime() != 0) {
203 short holdTime = holdTime();
204 if ((holdTime == 1) || (holdTime == 2)) {
205 return false;
206 }
207 }
208 return true;
209 }
210
211 /**
Thejaswi N K38879622015-12-08 22:14:47 +0530212 * Validates the Bgp local and peer configuration.
213 *
214 * @return true if valid else false
215 */
216 public boolean validateBgpConfiguration() {
217
218 if (!validateLocalAs()) {
219 return false;
220 }
221
222 if (!validateRouterId()) {
223 return false;
224 }
225
226 if (!validateBgpPeers()) {
227 return false;
228 }
229
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530230 if (!validateFlowSpec()) {
231 return false;
232 }
Shashikanth VHde663832016-04-25 18:42:04 +0530233
234 if (!validateHoldTime()) {
235 return false;
236 }
Thejaswi N K38879622015-12-08 22:14:47 +0530237 return true;
238 }
239
240 /**
241 * Validates the Bgp As number.
242 *
243 * @return true if valid else false
244 */
245 public boolean validateLocalAs() {
246
247 long localAs = 0;
248 localAs = localAs();
249
Thejaswi N K38879622015-12-08 22:14:47 +0530250 if (largeAsCapability()) {
251
252 if (localAs == 0 || localAs >= MAX_LONG_AS_NUMBER) {
253 return false;
254 }
255 } else {
256 if (localAs == 0 || localAs >= MAX_SHORT_AS_NUMBER) {
257 return false;
258 }
259 }
260
261 return true;
262 }
263
264 /**
265 * Validates the Bgp peer As number.
266 *
Jian Lidfba7392016-01-22 16:46:58 -0800267 * @param remoteAs remote As number
Thejaswi N K38879622015-12-08 22:14:47 +0530268 * @return true if valid else false
269 */
270 public boolean validateRemoteAs(long remoteAs) {
271 if (largeAsCapability()) {
272
273 if (remoteAs == 0 || remoteAs >= MAX_LONG_AS_NUMBER) {
274 return false;
275 }
276 } else {
277 if (remoteAs == 0 || remoteAs >= MAX_SHORT_AS_NUMBER) {
278 return false;
279 }
280 }
281 return true;
282 }
283
284 /**
285 * Validates the Bgp Router ID configuration.
286 *
287 * @return true if valid else false
288 */
289 public boolean validateRouterId() {
290 String routerId = routerId();
Shashikanth VHde663832016-04-25 18:42:04 +0530291 // TODO: router ID validation
Thejaswi N K38879622015-12-08 22:14:47 +0530292 return true;
293 }
294
295 /**
296 * Validates the Bgp peer holdTime.
297 *
Jian Lidfba7392016-01-22 16:46:58 -0800298 * @param remoteAs remote As number
Thejaswi N K38879622015-12-08 22:14:47 +0530299 * @return true if valid else false
300 */
301 public boolean validatePeerHoldTime(long remoteAs) {
302 //TODO:Validate it later..
303 return true;
304 }
305
306 /**
307 * Validates the Bgp peer configuration.
308 *
309 * @return true if valid else false
310 */
311 public boolean validateBgpPeers() {
312 List<BgpPeerConfig> nodes;
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530313 String connectMode;
Thejaswi N K38879622015-12-08 22:14:47 +0530314
315 nodes = bgpPeer();
316 for (int i = 0; i < nodes.size(); i++) {
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530317 connectMode = nodes.get(i).connectMode();
Thejaswi N K38879622015-12-08 22:14:47 +0530318 if ((IpAddress.valueOf(nodes.get(i).hostname()) == null) ||
319 !validateRemoteAs(nodes.get(i).asNumber()) ||
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530320 !validatePeerHoldTime(nodes.get(i).holdTime()) ||
321 !(connectMode.equals(PEER_CONNECT_ACTIVE) || connectMode.equals(PEER_CONNECT_PASSIVE))) {
mohamedrahil00f6f262016-11-24 20:20:41 +0530322 log.debug("BGP peer configration false");
Thejaswi N K38879622015-12-08 22:14:47 +0530323 return false;
324 }
325 }
mohamedrahil00f6f262016-11-24 20:20:41 +0530326 log.debug("BGP peer configration true");
Thejaswi N K38879622015-12-08 22:14:47 +0530327 return true;
328 }
329
330 /**
331 * Returns the set of nodes read from network config.
332 *
333 * @return list of BgpPeerConfig or null
334 */
335 public List<BgpPeerConfig> bgpPeer() {
336 List<BgpPeerConfig> nodes = new ArrayList<BgpPeerConfig>();
Thejaswi N K38879622015-12-08 22:14:47 +0530337 JsonNode jsonNodes = object.get(BGP_PEER);
338 if (jsonNodes == null) {
339 return null;
340 }
341
342 jsonNodes.forEach(jsonNode -> nodes.add(new BgpPeerConfig(
343 jsonNode.path(PEER_IP).asText(),
344 jsonNode.path(REMOTE_AS).asInt(),
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530345 jsonNode.path(PEER_HOLD_TIME).asInt(),
346 jsonNode.path(PEER_CONNECT_MODE).asText())));
Thejaswi N K38879622015-12-08 22:14:47 +0530347
348 return nodes;
349 }
350
351 /**
352 * Configuration for Bgp peer nodes.
353 */
354 public static class BgpPeerConfig {
355
356 private final String hostname;
357 private final int asNumber;
358 private final short holdTime;
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530359 private final String connectMode;
Thejaswi N K38879622015-12-08 22:14:47 +0530360
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530361 public BgpPeerConfig(String hostname, int asNumber, int holdTime, String connectMode) {
Thejaswi N K38879622015-12-08 22:14:47 +0530362 this.hostname = checkNotNull(hostname);
363 this.asNumber = asNumber;
364 this.holdTime = (short) holdTime;
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530365 this.connectMode = connectMode;
Thejaswi N K38879622015-12-08 22:14:47 +0530366 }
367
368 /**
369 * Returns hostname of the peer node.
370 *
371 * @return hostname
372 */
373 public String hostname() {
374 return this.hostname;
375 }
376
377 /**
378 * Returns asNumber if peer.
379 *
380 * @return asNumber
381 */
382 public int asNumber() {
383 return this.asNumber;
384 }
385
386 /**
387 * Returns holdTime of the peer node.
388 *
389 * @return holdTime
390 */
391 public short holdTime() {
392 return this.holdTime;
393 }
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530394
395 /**
396 * Returns connection mode for the peer node.
397 *
398 * @return active or passive connection
399 */
400 public String connectMode() {
401 return this.connectMode;
402 }
Thejaswi N K38879622015-12-08 22:14:47 +0530403 }
404}