blob: 504930d5df1d6162af64e4b89385e7fd4fcba543 [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;
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
33import static org.onosproject.net.config.Config.FieldPresence.MANDATORY;
34import static org.onosproject.net.config.Config.FieldPresence.OPTIONAL;
35import static com.google.common.base.Preconditions.checkNotNull;
36
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";
Thejaswi N K38879622015-12-08 22:14:47 +053055
56 public static final String BGP_PEER = "bgpPeer";
57 public static final String PEER_IP = "peerIp";
58 public static final String REMOTE_AS = "remoteAs";
59 public static final String PEER_HOLD_TIME = "peerHoldTime";
Thejaswi N K0008f1d2015-12-11 12:47:41 +053060 public static final String PEER_CONNECT_MODE = "connectMode";
61 public static final String PEER_CONNECT_PASSIVE = "passive";
62 public static final String PEER_CONNECT_ACTIVE = "active";
Thejaswi N K38879622015-12-08 22:14:47 +053063
64 static final int MAX_SHORT_AS_NUMBER = 65535;
65 static final long MAX_LONG_AS_NUMBER = 4294967295L;
66
Shashikanth VHde663832016-04-25 18:42:04 +053067 static final int MIN_SESSION_NUMBER = 1;
68 static final long MAX_SESSION_NUMBER = 21;
69
70 static final int MIN_HOLDTIME = 0;
71 static final long MAX_HOLDTIME = 65535;
72
Thejaswi N K38879622015-12-08 22:14:47 +053073 @Override
74 public boolean isValid() {
75 boolean fields = false;
76
77 this.bgpController = DefaultServiceDirectory.getService(BgpController.class);
78 bgpConfig = bgpController.getConfig();
79
80 fields = hasOnlyFields(ROUTER_ID, LOCAL_AS, MAX_SESSION, LS_CAPABILITY,
Shashikanth VHb650bfa2016-04-18 12:54:03 +053081 HOLD_TIME, LARGE_AS_CAPABILITY, FLOW_SPEC_CAPABILITY, FLOW_SPEC_RPD_CAPABILITY, BGP_PEER) &&
Thejaswi N K38879622015-12-08 22:14:47 +053082 isIpAddress(ROUTER_ID, MANDATORY) && isNumber(LOCAL_AS, MANDATORY) &&
Shashikanth VHde663832016-04-25 18:42:04 +053083 isNumber(MAX_SESSION, OPTIONAL, MIN_SESSION_NUMBER, MAX_SESSION_NUMBER)
84 && isNumber(HOLD_TIME, OPTIONAL, MIN_HOLDTIME, MAX_HOLDTIME) &&
Shashikanth VH580bdeb2016-02-19 17:26:03 +053085 isBoolean(LS_CAPABILITY, OPTIONAL) && isBoolean(LARGE_AS_CAPABILITY, OPTIONAL) &&
Shashikanth VHb650bfa2016-04-18 12:54:03 +053086 isString(FLOW_SPEC_CAPABILITY, OPTIONAL) && isBoolean(FLOW_SPEC_RPD_CAPABILITY, OPTIONAL);
Thejaswi N K38879622015-12-08 22:14:47 +053087
88 if (!fields) {
89 return fields;
90 }
91
92 return validateBgpConfiguration();
93 }
94
95 /**
96 * Returns routerId from the configuration.
97 *
98 * @return routerId
99 */
100 public String routerId() {
101 return get(ROUTER_ID, null);
102 }
103
104 /**
105 * Returns localAs number from the configuration.
106 *
107 * @return local As number
108 */
109 public int localAs() {
110 return Integer.parseInt(get(LOCAL_AS, null));
111 }
112
113 /**
114 * Returns max session from the configuration.
115 *
116 * @return max session
117 */
118 public int maxSession() {
119 return Integer.parseInt(get(MAX_SESSION, null));
120 }
121
122 /**
123 * Returns BGP-LS capability support from the configuration.
124 *
125 * @return true if BGP-LS capability is set else false
126 */
127 public boolean lsCapability() {
128 return Boolean.parseBoolean(get(LS_CAPABILITY, null));
129 }
130
131 /**
Shashikanth VHb650bfa2016-04-18 12:54:03 +0530132 * Returns flow spec route policy distribution capability support from the configuration.
133 *
134 * @return true if flow spec route policy distribution capability is set otherwise false
135 */
136 public boolean rpdCapability() {
137 return Boolean.parseBoolean(get(FLOW_SPEC_RPD_CAPABILITY, null));
138 }
139
140 /**
Thejaswi N K38879622015-12-08 22:14:47 +0530141 * Returns largeAs capability support from the configuration.
142 *
143 * @return largeAs capability
144 */
145 public boolean largeAsCapability() {
146 return Boolean.parseBoolean(get(LARGE_AS_CAPABILITY, null));
147 }
148
149 /**
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530150 * Returns flow specification capability support from the configuration.
151 *
152 * @return flow specification capability
153 */
154 public String flowSpecCapability() {
155 return get(FLOW_SPEC_CAPABILITY, null);
156 }
157
158 /**
Thejaswi N K38879622015-12-08 22:14:47 +0530159 * Returns holdTime of the local node from the configuration.
160 *
161 * @return holdTime
162 */
163 public short holdTime() {
164 return Short.parseShort(get(HOLD_TIME, null));
165 }
166
167 /**
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530168 * Validates the flow specification capability.
169 *
170 * @return true if valid else false
171 */
172 public boolean validateFlowSpec() {
173 if (flowSpecCapability() != null) {
174 String flowSpec = flowSpecCapability();
Shashikanth VH09792f02016-05-10 16:59:57 +0530175 if ((!flowSpec.equals("IPV4")) && (!flowSpec.equals("VPNV4")) && (!flowSpec.equals("IPV4_VPNV4"))) {
mohamedrahil00f6f262016-11-24 20:20:41 +0530176 log.debug("Flow specification capabality is false");
Shashikanth VH09792f02016-05-10 16:59:57 +0530177 return false;
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530178 }
179 }
mohamedrahil00f6f262016-11-24 20:20:41 +0530180 log.debug("Flow specification capabality is true");
Shashikanth VH09792f02016-05-10 16:59:57 +0530181 return true;
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530182 }
183
184 /**
Shashikanth VHde663832016-04-25 18:42:04 +0530185 * Validates the hold time value.
186 *
187 * @return true if valid else false
188 */
189 public boolean validateHoldTime() {
190 if (holdTime() != 0) {
191 short holdTime = holdTime();
192 if ((holdTime == 1) || (holdTime == 2)) {
193 return false;
194 }
195 }
196 return true;
197 }
198
199 /**
Thejaswi N K38879622015-12-08 22:14:47 +0530200 * Validates the Bgp local and peer configuration.
201 *
202 * @return true if valid else false
203 */
204 public boolean validateBgpConfiguration() {
205
206 if (!validateLocalAs()) {
207 return false;
208 }
209
210 if (!validateRouterId()) {
211 return false;
212 }
213
214 if (!validateBgpPeers()) {
215 return false;
216 }
217
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530218 if (!validateFlowSpec()) {
219 return false;
220 }
Shashikanth VHde663832016-04-25 18:42:04 +0530221
222 if (!validateHoldTime()) {
223 return false;
224 }
Thejaswi N K38879622015-12-08 22:14:47 +0530225 return true;
226 }
227
228 /**
229 * Validates the Bgp As number.
230 *
231 * @return true if valid else false
232 */
233 public boolean validateLocalAs() {
234
235 long localAs = 0;
236 localAs = localAs();
237
Thejaswi N K38879622015-12-08 22:14:47 +0530238 if (largeAsCapability()) {
239
240 if (localAs == 0 || localAs >= MAX_LONG_AS_NUMBER) {
241 return false;
242 }
243 } else {
244 if (localAs == 0 || localAs >= MAX_SHORT_AS_NUMBER) {
245 return false;
246 }
247 }
248
249 return true;
250 }
251
252 /**
253 * Validates the Bgp peer As number.
254 *
Jian Lidfba7392016-01-22 16:46:58 -0800255 * @param remoteAs remote As number
Thejaswi N K38879622015-12-08 22:14:47 +0530256 * @return true if valid else false
257 */
258 public boolean validateRemoteAs(long remoteAs) {
259 if (largeAsCapability()) {
260
261 if (remoteAs == 0 || remoteAs >= MAX_LONG_AS_NUMBER) {
262 return false;
263 }
264 } else {
265 if (remoteAs == 0 || remoteAs >= MAX_SHORT_AS_NUMBER) {
266 return false;
267 }
268 }
269 return true;
270 }
271
272 /**
273 * Validates the Bgp Router ID configuration.
274 *
275 * @return true if valid else false
276 */
277 public boolean validateRouterId() {
278 String routerId = routerId();
Shashikanth VHde663832016-04-25 18:42:04 +0530279 // TODO: router ID validation
Thejaswi N K38879622015-12-08 22:14:47 +0530280 return true;
281 }
282
283 /**
284 * Validates the Bgp peer holdTime.
285 *
Jian Lidfba7392016-01-22 16:46:58 -0800286 * @param remoteAs remote As number
Thejaswi N K38879622015-12-08 22:14:47 +0530287 * @return true if valid else false
288 */
289 public boolean validatePeerHoldTime(long remoteAs) {
290 //TODO:Validate it later..
291 return true;
292 }
293
294 /**
295 * Validates the Bgp peer configuration.
296 *
297 * @return true if valid else false
298 */
299 public boolean validateBgpPeers() {
300 List<BgpPeerConfig> nodes;
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530301 String connectMode;
Thejaswi N K38879622015-12-08 22:14:47 +0530302
303 nodes = bgpPeer();
304 for (int i = 0; i < nodes.size(); i++) {
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530305 connectMode = nodes.get(i).connectMode();
Thejaswi N K38879622015-12-08 22:14:47 +0530306 if ((IpAddress.valueOf(nodes.get(i).hostname()) == null) ||
307 !validateRemoteAs(nodes.get(i).asNumber()) ||
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530308 !validatePeerHoldTime(nodes.get(i).holdTime()) ||
309 !(connectMode.equals(PEER_CONNECT_ACTIVE) || connectMode.equals(PEER_CONNECT_PASSIVE))) {
mohamedrahil00f6f262016-11-24 20:20:41 +0530310 log.debug("BGP peer configration false");
Thejaswi N K38879622015-12-08 22:14:47 +0530311 return false;
312 }
313 }
mohamedrahil00f6f262016-11-24 20:20:41 +0530314 log.debug("BGP peer configration true");
Thejaswi N K38879622015-12-08 22:14:47 +0530315 return true;
316 }
317
318 /**
319 * Returns the set of nodes read from network config.
320 *
321 * @return list of BgpPeerConfig or null
322 */
323 public List<BgpPeerConfig> bgpPeer() {
324 List<BgpPeerConfig> nodes = new ArrayList<BgpPeerConfig>();
Thejaswi N K38879622015-12-08 22:14:47 +0530325 JsonNode jsonNodes = object.get(BGP_PEER);
326 if (jsonNodes == null) {
327 return null;
328 }
329
330 jsonNodes.forEach(jsonNode -> nodes.add(new BgpPeerConfig(
331 jsonNode.path(PEER_IP).asText(),
332 jsonNode.path(REMOTE_AS).asInt(),
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530333 jsonNode.path(PEER_HOLD_TIME).asInt(),
334 jsonNode.path(PEER_CONNECT_MODE).asText())));
Thejaswi N K38879622015-12-08 22:14:47 +0530335
336 return nodes;
337 }
338
339 /**
340 * Configuration for Bgp peer nodes.
341 */
342 public static class BgpPeerConfig {
343
344 private final String hostname;
345 private final int asNumber;
346 private final short holdTime;
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530347 private final String connectMode;
Thejaswi N K38879622015-12-08 22:14:47 +0530348
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530349 public BgpPeerConfig(String hostname, int asNumber, int holdTime, String connectMode) {
Thejaswi N K38879622015-12-08 22:14:47 +0530350 this.hostname = checkNotNull(hostname);
351 this.asNumber = asNumber;
352 this.holdTime = (short) holdTime;
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530353 this.connectMode = connectMode;
Thejaswi N K38879622015-12-08 22:14:47 +0530354 }
355
356 /**
357 * Returns hostname of the peer node.
358 *
359 * @return hostname
360 */
361 public String hostname() {
362 return this.hostname;
363 }
364
365 /**
366 * Returns asNumber if peer.
367 *
368 * @return asNumber
369 */
370 public int asNumber() {
371 return this.asNumber;
372 }
373
374 /**
375 * Returns holdTime of the peer node.
376 *
377 * @return holdTime
378 */
379 public short holdTime() {
380 return this.holdTime;
381 }
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530382
383 /**
384 * Returns connection mode for the peer node.
385 *
386 * @return active or passive connection
387 */
388 public String connectMode() {
389 return this.connectMode;
390 }
Thejaswi N K38879622015-12-08 22:14:47 +0530391 }
392}