blob: 079f80d86f0010c72031c73b2dd6108cbecb8221 [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
64 @Override
65 public boolean isValid() {
66 boolean fields = false;
67
68 this.bgpController = DefaultServiceDirectory.getService(BgpController.class);
69 bgpConfig = bgpController.getConfig();
70
71 fields = hasOnlyFields(ROUTER_ID, LOCAL_AS, MAX_SESSION, LS_CAPABILITY,
Shashikanth VHb650bfa2016-04-18 12:54:03 +053072 HOLD_TIME, LARGE_AS_CAPABILITY, FLOW_SPEC_CAPABILITY, FLOW_SPEC_RPD_CAPABILITY, BGP_PEER) &&
Thejaswi N K38879622015-12-08 22:14:47 +053073 isIpAddress(ROUTER_ID, MANDATORY) && isNumber(LOCAL_AS, MANDATORY) &&
74 isNumber(MAX_SESSION, OPTIONAL, 20) && isNumber(HOLD_TIME, OPTIONAL, 180) &&
Shashikanth VH580bdeb2016-02-19 17:26:03 +053075 isBoolean(LS_CAPABILITY, OPTIONAL) && isBoolean(LARGE_AS_CAPABILITY, OPTIONAL) &&
Shashikanth VHb650bfa2016-04-18 12:54:03 +053076 isString(FLOW_SPEC_CAPABILITY, OPTIONAL) && isBoolean(FLOW_SPEC_RPD_CAPABILITY, OPTIONAL);
Thejaswi N K38879622015-12-08 22:14:47 +053077
78 if (!fields) {
79 return fields;
80 }
81
82 return validateBgpConfiguration();
83 }
84
85 /**
86 * Returns routerId from the configuration.
87 *
88 * @return routerId
89 */
90 public String routerId() {
91 return get(ROUTER_ID, null);
92 }
93
94 /**
95 * Returns localAs number from the configuration.
96 *
97 * @return local As number
98 */
99 public int localAs() {
100 return Integer.parseInt(get(LOCAL_AS, null));
101 }
102
103 /**
104 * Returns max session from the configuration.
105 *
106 * @return max session
107 */
108 public int maxSession() {
109 return Integer.parseInt(get(MAX_SESSION, null));
110 }
111
112 /**
113 * Returns BGP-LS capability support from the configuration.
114 *
115 * @return true if BGP-LS capability is set else false
116 */
117 public boolean lsCapability() {
118 return Boolean.parseBoolean(get(LS_CAPABILITY, null));
119 }
120
121 /**
Shashikanth VHb650bfa2016-04-18 12:54:03 +0530122 * Returns flow spec route policy distribution capability support from the configuration.
123 *
124 * @return true if flow spec route policy distribution capability is set otherwise false
125 */
126 public boolean rpdCapability() {
127 return Boolean.parseBoolean(get(FLOW_SPEC_RPD_CAPABILITY, null));
128 }
129
130 /**
Thejaswi N K38879622015-12-08 22:14:47 +0530131 * Returns largeAs capability support from the configuration.
132 *
133 * @return largeAs capability
134 */
135 public boolean largeAsCapability() {
136 return Boolean.parseBoolean(get(LARGE_AS_CAPABILITY, null));
137 }
138
139 /**
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530140 * Returns flow specification capability support from the configuration.
141 *
142 * @return flow specification capability
143 */
144 public String flowSpecCapability() {
145 return get(FLOW_SPEC_CAPABILITY, null);
146 }
147
148 /**
Thejaswi N K38879622015-12-08 22:14:47 +0530149 * Returns holdTime of the local node from the configuration.
150 *
151 * @return holdTime
152 */
153 public short holdTime() {
154 return Short.parseShort(get(HOLD_TIME, null));
155 }
156
157 /**
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530158 * Validates the flow specification capability.
159 *
160 * @return true if valid else false
161 */
162 public boolean validateFlowSpec() {
163 if (flowSpecCapability() != null) {
164 String flowSpec = flowSpecCapability();
165 if ((flowSpec.equals("IPV4")) || (flowSpec.equals("VPNV4")) || (flowSpec.equals("IPV4_VPNV4"))) {
166 return true;
167 }
168 }
169
170 return false;
171 }
172
173 /**
Thejaswi N K38879622015-12-08 22:14:47 +0530174 * Validates the Bgp local and peer configuration.
175 *
176 * @return true if valid else false
177 */
178 public boolean validateBgpConfiguration() {
179
180 if (!validateLocalAs()) {
181 return false;
182 }
183
184 if (!validateRouterId()) {
185 return false;
186 }
187
188 if (!validateBgpPeers()) {
189 return false;
190 }
191
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530192 if (!validateFlowSpec()) {
193 return false;
194 }
Thejaswi N K38879622015-12-08 22:14:47 +0530195 return true;
196 }
197
198 /**
199 * Validates the Bgp As number.
200 *
201 * @return true if valid else false
202 */
203 public boolean validateLocalAs() {
204
205 long localAs = 0;
206 localAs = localAs();
207
208 if (bgpController.connectedPeerCount() != 0) {
209 return false;
210 }
211
212 if (largeAsCapability()) {
213
214 if (localAs == 0 || localAs >= MAX_LONG_AS_NUMBER) {
215 return false;
216 }
217 } else {
218 if (localAs == 0 || localAs >= MAX_SHORT_AS_NUMBER) {
219 return false;
220 }
221 }
222
223 return true;
224 }
225
226 /**
227 * Validates the Bgp peer As number.
228 *
Jian Lidfba7392016-01-22 16:46:58 -0800229 * @param remoteAs remote As number
Thejaswi N K38879622015-12-08 22:14:47 +0530230 * @return true if valid else false
231 */
232 public boolean validateRemoteAs(long remoteAs) {
233 if (largeAsCapability()) {
234
235 if (remoteAs == 0 || remoteAs >= MAX_LONG_AS_NUMBER) {
236 return false;
237 }
238 } else {
239 if (remoteAs == 0 || remoteAs >= MAX_SHORT_AS_NUMBER) {
240 return false;
241 }
242 }
243 return true;
244 }
245
246 /**
247 * Validates the Bgp Router ID configuration.
248 *
249 * @return true if valid else false
250 */
251 public boolean validateRouterId() {
252 String routerId = routerId();
253 if (bgpController.connectedPeerCount() != 0) {
254 return false;
255 }
256 return true;
257 }
258
259 /**
260 * Validates the Bgp peer holdTime.
261 *
Jian Lidfba7392016-01-22 16:46:58 -0800262 * @param remoteAs remote As number
Thejaswi N K38879622015-12-08 22:14:47 +0530263 * @return true if valid else false
264 */
265 public boolean validatePeerHoldTime(long remoteAs) {
266 //TODO:Validate it later..
267 return true;
268 }
269
270 /**
271 * Validates the Bgp peer configuration.
272 *
273 * @return true if valid else false
274 */
275 public boolean validateBgpPeers() {
276 List<BgpPeerConfig> nodes;
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530277 String connectMode;
Thejaswi N K38879622015-12-08 22:14:47 +0530278
279 nodes = bgpPeer();
280 for (int i = 0; i < nodes.size(); i++) {
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530281 connectMode = nodes.get(i).connectMode();
Thejaswi N K38879622015-12-08 22:14:47 +0530282 if ((IpAddress.valueOf(nodes.get(i).hostname()) == null) ||
283 !validateRemoteAs(nodes.get(i).asNumber()) ||
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530284 !validatePeerHoldTime(nodes.get(i).holdTime()) ||
285 !(connectMode.equals(PEER_CONNECT_ACTIVE) || connectMode.equals(PEER_CONNECT_PASSIVE))) {
Thejaswi N K38879622015-12-08 22:14:47 +0530286 return false;
287 }
288 }
289
290 return true;
291 }
292
293 /**
294 * Returns the set of nodes read from network config.
295 *
296 * @return list of BgpPeerConfig or null
297 */
298 public List<BgpPeerConfig> bgpPeer() {
299 List<BgpPeerConfig> nodes = new ArrayList<BgpPeerConfig>();
300
301 JsonNode jsonNodes = object.get(BGP_PEER);
302 if (jsonNodes == null) {
303 return null;
304 }
305
306 jsonNodes.forEach(jsonNode -> nodes.add(new BgpPeerConfig(
307 jsonNode.path(PEER_IP).asText(),
308 jsonNode.path(REMOTE_AS).asInt(),
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530309 jsonNode.path(PEER_HOLD_TIME).asInt(),
310 jsonNode.path(PEER_CONNECT_MODE).asText())));
Thejaswi N K38879622015-12-08 22:14:47 +0530311
312 return nodes;
313 }
314
315 /**
316 * Configuration for Bgp peer nodes.
317 */
318 public static class BgpPeerConfig {
319
320 private final String hostname;
321 private final int asNumber;
322 private final short holdTime;
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530323 private final String connectMode;
Thejaswi N K38879622015-12-08 22:14:47 +0530324
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530325 public BgpPeerConfig(String hostname, int asNumber, int holdTime, String connectMode) {
Thejaswi N K38879622015-12-08 22:14:47 +0530326 this.hostname = checkNotNull(hostname);
327 this.asNumber = asNumber;
328 this.holdTime = (short) holdTime;
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530329 this.connectMode = connectMode;
Thejaswi N K38879622015-12-08 22:14:47 +0530330 }
331
332 /**
333 * Returns hostname of the peer node.
334 *
335 * @return hostname
336 */
337 public String hostname() {
338 return this.hostname;
339 }
340
341 /**
342 * Returns asNumber if peer.
343 *
344 * @return asNumber
345 */
346 public int asNumber() {
347 return this.asNumber;
348 }
349
350 /**
351 * Returns holdTime of the peer node.
352 *
353 * @return holdTime
354 */
355 public short holdTime() {
356 return this.holdTime;
357 }
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530358
359 /**
360 * Returns connection mode for the peer node.
361 *
362 * @return active or passive connection
363 */
364 public String connectMode() {
365 return this.connectMode;
366 }
Thejaswi N K38879622015-12-08 22:14:47 +0530367 }
368}