blob: f185f65d36b924d23cddb46832941bb81a44cb29 [file] [log] [blame]
Thejaswi N K38879622015-12-08 22:14:47 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
27
28import java.util.ArrayList;
29import java.util.List;
30
31import static org.onosproject.net.config.Config.FieldPresence.MANDATORY;
32import static org.onosproject.net.config.Config.FieldPresence.OPTIONAL;
33import static com.google.common.base.Preconditions.checkNotNull;
34
35/**
36 * Configuration object for BGP.
37 */
38public class BgpAppConfig extends Config<ApplicationId> {
39 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
40 BgpController bgpController;
41
42 BgpCfg bgpConfig = null;
43
44 public static final String ROUTER_ID = "routerId";
45 public static final String LOCAL_AS = "localAs";
46 public static final String MAX_SESSION = "maxSession";
47 public static final String LS_CAPABILITY = "lsCapability";
48 public static final String HOLD_TIME = "holdTime";
49 public static final String LARGE_AS_CAPABILITY = "largeAsCapability";
Shashikanth VH580bdeb2016-02-19 17:26:03 +053050 public static final String FLOW_SPEC_CAPABILITY = "flowSpecCapability";
Shashikanth VHb650bfa2016-04-18 12:54:03 +053051 public static final String FLOW_SPEC_RPD_CAPABILITY = "flowSpecRpdCapability";
Thejaswi N K38879622015-12-08 22:14:47 +053052
53 public static final String BGP_PEER = "bgpPeer";
54 public static final String PEER_IP = "peerIp";
55 public static final String REMOTE_AS = "remoteAs";
56 public static final String PEER_HOLD_TIME = "peerHoldTime";
Thejaswi N K0008f1d2015-12-11 12:47:41 +053057 public static final String PEER_CONNECT_MODE = "connectMode";
58 public static final String PEER_CONNECT_PASSIVE = "passive";
59 public static final String PEER_CONNECT_ACTIVE = "active";
Thejaswi N K38879622015-12-08 22:14:47 +053060
61 static final int MAX_SHORT_AS_NUMBER = 65535;
62 static final long MAX_LONG_AS_NUMBER = 4294967295L;
63
Shashikanth VHde663832016-04-25 18:42:04 +053064 static final int MIN_SESSION_NUMBER = 1;
65 static final long MAX_SESSION_NUMBER = 21;
66
67 static final int MIN_HOLDTIME = 0;
68 static final long MAX_HOLDTIME = 65535;
69
Thejaswi N K38879622015-12-08 22:14:47 +053070 @Override
71 public boolean isValid() {
72 boolean fields = false;
73
74 this.bgpController = DefaultServiceDirectory.getService(BgpController.class);
75 bgpConfig = bgpController.getConfig();
76
77 fields = hasOnlyFields(ROUTER_ID, LOCAL_AS, MAX_SESSION, LS_CAPABILITY,
Shashikanth VHb650bfa2016-04-18 12:54:03 +053078 HOLD_TIME, LARGE_AS_CAPABILITY, FLOW_SPEC_CAPABILITY, FLOW_SPEC_RPD_CAPABILITY, BGP_PEER) &&
Thejaswi N K38879622015-12-08 22:14:47 +053079 isIpAddress(ROUTER_ID, MANDATORY) && isNumber(LOCAL_AS, MANDATORY) &&
Shashikanth VHde663832016-04-25 18:42:04 +053080 isNumber(MAX_SESSION, OPTIONAL, MIN_SESSION_NUMBER, MAX_SESSION_NUMBER)
81 && isNumber(HOLD_TIME, OPTIONAL, MIN_HOLDTIME, MAX_HOLDTIME) &&
Shashikanth VH580bdeb2016-02-19 17:26:03 +053082 isBoolean(LS_CAPABILITY, OPTIONAL) && isBoolean(LARGE_AS_CAPABILITY, OPTIONAL) &&
Shashikanth VHb650bfa2016-04-18 12:54:03 +053083 isString(FLOW_SPEC_CAPABILITY, OPTIONAL) && isBoolean(FLOW_SPEC_RPD_CAPABILITY, OPTIONAL);
Thejaswi N K38879622015-12-08 22:14:47 +053084
85 if (!fields) {
86 return fields;
87 }
88
89 return validateBgpConfiguration();
90 }
91
92 /**
93 * Returns routerId from the configuration.
94 *
95 * @return routerId
96 */
97 public String routerId() {
98 return get(ROUTER_ID, null);
99 }
100
101 /**
102 * Returns localAs number from the configuration.
103 *
104 * @return local As number
105 */
106 public int localAs() {
107 return Integer.parseInt(get(LOCAL_AS, null));
108 }
109
110 /**
111 * Returns max session from the configuration.
112 *
113 * @return max session
114 */
115 public int maxSession() {
116 return Integer.parseInt(get(MAX_SESSION, null));
117 }
118
119 /**
120 * Returns BGP-LS capability support from the configuration.
121 *
122 * @return true if BGP-LS capability is set else false
123 */
124 public boolean lsCapability() {
125 return Boolean.parseBoolean(get(LS_CAPABILITY, null));
126 }
127
128 /**
Shashikanth VHb650bfa2016-04-18 12:54:03 +0530129 * Returns flow spec route policy distribution capability support from the configuration.
130 *
131 * @return true if flow spec route policy distribution capability is set otherwise false
132 */
133 public boolean rpdCapability() {
134 return Boolean.parseBoolean(get(FLOW_SPEC_RPD_CAPABILITY, null));
135 }
136
137 /**
Thejaswi N K38879622015-12-08 22:14:47 +0530138 * Returns largeAs capability support from the configuration.
139 *
140 * @return largeAs capability
141 */
142 public boolean largeAsCapability() {
143 return Boolean.parseBoolean(get(LARGE_AS_CAPABILITY, null));
144 }
145
146 /**
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530147 * Returns flow specification capability support from the configuration.
148 *
149 * @return flow specification capability
150 */
151 public String flowSpecCapability() {
152 return get(FLOW_SPEC_CAPABILITY, null);
153 }
154
155 /**
Thejaswi N K38879622015-12-08 22:14:47 +0530156 * Returns holdTime of the local node from the configuration.
157 *
158 * @return holdTime
159 */
160 public short holdTime() {
161 return Short.parseShort(get(HOLD_TIME, null));
162 }
163
164 /**
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530165 * Validates the flow specification capability.
166 *
167 * @return true if valid else false
168 */
169 public boolean validateFlowSpec() {
170 if (flowSpecCapability() != null) {
171 String flowSpec = flowSpecCapability();
Shashikanth VH09792f02016-05-10 16:59:57 +0530172 if ((!flowSpec.equals("IPV4")) && (!flowSpec.equals("VPNV4")) && (!flowSpec.equals("IPV4_VPNV4"))) {
173 return false;
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530174 }
175 }
176
Shashikanth VH09792f02016-05-10 16:59:57 +0530177 return true;
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530178 }
179
180 /**
Shashikanth VHde663832016-04-25 18:42:04 +0530181 * Validates the hold time value.
182 *
183 * @return true if valid else false
184 */
185 public boolean validateHoldTime() {
186 if (holdTime() != 0) {
187 short holdTime = holdTime();
188 if ((holdTime == 1) || (holdTime == 2)) {
189 return false;
190 }
191 }
192 return true;
193 }
194
195 /**
Thejaswi N K38879622015-12-08 22:14:47 +0530196 * Validates the Bgp local and peer configuration.
197 *
198 * @return true if valid else false
199 */
200 public boolean validateBgpConfiguration() {
201
202 if (!validateLocalAs()) {
203 return false;
204 }
205
206 if (!validateRouterId()) {
207 return false;
208 }
209
210 if (!validateBgpPeers()) {
211 return false;
212 }
213
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530214 if (!validateFlowSpec()) {
215 return false;
216 }
Shashikanth VHde663832016-04-25 18:42:04 +0530217
218 if (!validateHoldTime()) {
219 return false;
220 }
Thejaswi N K38879622015-12-08 22:14:47 +0530221 return true;
222 }
223
224 /**
225 * Validates the Bgp As number.
226 *
227 * @return true if valid else false
228 */
229 public boolean validateLocalAs() {
230
231 long localAs = 0;
232 localAs = localAs();
233
Thejaswi N K38879622015-12-08 22:14:47 +0530234 if (largeAsCapability()) {
235
236 if (localAs == 0 || localAs >= MAX_LONG_AS_NUMBER) {
237 return false;
238 }
239 } else {
240 if (localAs == 0 || localAs >= MAX_SHORT_AS_NUMBER) {
241 return false;
242 }
243 }
244
245 return true;
246 }
247
248 /**
249 * Validates the Bgp peer As number.
250 *
Jian Lidfba7392016-01-22 16:46:58 -0800251 * @param remoteAs remote As number
Thejaswi N K38879622015-12-08 22:14:47 +0530252 * @return true if valid else false
253 */
254 public boolean validateRemoteAs(long remoteAs) {
255 if (largeAsCapability()) {
256
257 if (remoteAs == 0 || remoteAs >= MAX_LONG_AS_NUMBER) {
258 return false;
259 }
260 } else {
261 if (remoteAs == 0 || remoteAs >= MAX_SHORT_AS_NUMBER) {
262 return false;
263 }
264 }
265 return true;
266 }
267
268 /**
269 * Validates the Bgp Router ID configuration.
270 *
271 * @return true if valid else false
272 */
273 public boolean validateRouterId() {
274 String routerId = routerId();
Shashikanth VHde663832016-04-25 18:42:04 +0530275 // TODO: router ID validation
Thejaswi N K38879622015-12-08 22:14:47 +0530276 return true;
277 }
278
279 /**
280 * Validates the Bgp peer holdTime.
281 *
Jian Lidfba7392016-01-22 16:46:58 -0800282 * @param remoteAs remote As number
Thejaswi N K38879622015-12-08 22:14:47 +0530283 * @return true if valid else false
284 */
285 public boolean validatePeerHoldTime(long remoteAs) {
286 //TODO:Validate it later..
287 return true;
288 }
289
290 /**
291 * Validates the Bgp peer configuration.
292 *
293 * @return true if valid else false
294 */
295 public boolean validateBgpPeers() {
296 List<BgpPeerConfig> nodes;
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530297 String connectMode;
Thejaswi N K38879622015-12-08 22:14:47 +0530298
299 nodes = bgpPeer();
300 for (int i = 0; i < nodes.size(); i++) {
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530301 connectMode = nodes.get(i).connectMode();
Thejaswi N K38879622015-12-08 22:14:47 +0530302 if ((IpAddress.valueOf(nodes.get(i).hostname()) == null) ||
303 !validateRemoteAs(nodes.get(i).asNumber()) ||
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530304 !validatePeerHoldTime(nodes.get(i).holdTime()) ||
305 !(connectMode.equals(PEER_CONNECT_ACTIVE) || connectMode.equals(PEER_CONNECT_PASSIVE))) {
Thejaswi N K38879622015-12-08 22:14:47 +0530306 return false;
307 }
308 }
309
310 return true;
311 }
312
313 /**
314 * Returns the set of nodes read from network config.
315 *
316 * @return list of BgpPeerConfig or null
317 */
318 public List<BgpPeerConfig> bgpPeer() {
319 List<BgpPeerConfig> nodes = new ArrayList<BgpPeerConfig>();
320
321 JsonNode jsonNodes = object.get(BGP_PEER);
322 if (jsonNodes == null) {
323 return null;
324 }
325
326 jsonNodes.forEach(jsonNode -> nodes.add(new BgpPeerConfig(
327 jsonNode.path(PEER_IP).asText(),
328 jsonNode.path(REMOTE_AS).asInt(),
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530329 jsonNode.path(PEER_HOLD_TIME).asInt(),
330 jsonNode.path(PEER_CONNECT_MODE).asText())));
Thejaswi N K38879622015-12-08 22:14:47 +0530331
332 return nodes;
333 }
334
335 /**
336 * Configuration for Bgp peer nodes.
337 */
338 public static class BgpPeerConfig {
339
340 private final String hostname;
341 private final int asNumber;
342 private final short holdTime;
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530343 private final String connectMode;
Thejaswi N K38879622015-12-08 22:14:47 +0530344
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530345 public BgpPeerConfig(String hostname, int asNumber, int holdTime, String connectMode) {
Thejaswi N K38879622015-12-08 22:14:47 +0530346 this.hostname = checkNotNull(hostname);
347 this.asNumber = asNumber;
348 this.holdTime = (short) holdTime;
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530349 this.connectMode = connectMode;
Thejaswi N K38879622015-12-08 22:14:47 +0530350 }
351
352 /**
353 * Returns hostname of the peer node.
354 *
355 * @return hostname
356 */
357 public String hostname() {
358 return this.hostname;
359 }
360
361 /**
362 * Returns asNumber if peer.
363 *
364 * @return asNumber
365 */
366 public int asNumber() {
367 return this.asNumber;
368 }
369
370 /**
371 * Returns holdTime of the peer node.
372 *
373 * @return holdTime
374 */
375 public short holdTime() {
376 return this.holdTime;
377 }
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530378
379 /**
380 * Returns connection mode for the peer node.
381 *
382 * @return active or passive connection
383 */
384 public String connectMode() {
385 return this.connectMode;
386 }
Thejaswi N K38879622015-12-08 22:14:47 +0530387 }
388}