blob: abf27d8e4b5b82bab97e768328973e183251d186 [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";
Thejaswi N K38879622015-12-08 22:14:47 +053051
52 public static final String BGP_PEER = "bgpPeer";
53 public static final String PEER_IP = "peerIp";
54 public static final String REMOTE_AS = "remoteAs";
55 public static final String PEER_HOLD_TIME = "peerHoldTime";
Thejaswi N K0008f1d2015-12-11 12:47:41 +053056 public static final String PEER_CONNECT_MODE = "connectMode";
57 public static final String PEER_CONNECT_PASSIVE = "passive";
58 public static final String PEER_CONNECT_ACTIVE = "active";
Thejaswi N K38879622015-12-08 22:14:47 +053059
60 static final int MAX_SHORT_AS_NUMBER = 65535;
61 static final long MAX_LONG_AS_NUMBER = 4294967295L;
62
63 @Override
64 public boolean isValid() {
65 boolean fields = false;
66
67 this.bgpController = DefaultServiceDirectory.getService(BgpController.class);
68 bgpConfig = bgpController.getConfig();
69
70 fields = hasOnlyFields(ROUTER_ID, LOCAL_AS, MAX_SESSION, LS_CAPABILITY,
Shashikanth VH580bdeb2016-02-19 17:26:03 +053071 HOLD_TIME, LARGE_AS_CAPABILITY, FLOW_SPEC_CAPABILITY, BGP_PEER) &&
Thejaswi N K38879622015-12-08 22:14:47 +053072 isIpAddress(ROUTER_ID, MANDATORY) && isNumber(LOCAL_AS, MANDATORY) &&
73 isNumber(MAX_SESSION, OPTIONAL, 20) && isNumber(HOLD_TIME, OPTIONAL, 180) &&
Shashikanth VH580bdeb2016-02-19 17:26:03 +053074 isBoolean(LS_CAPABILITY, OPTIONAL) && isBoolean(LARGE_AS_CAPABILITY, OPTIONAL) &&
75 isString(FLOW_SPEC_CAPABILITY, OPTIONAL);
Thejaswi N K38879622015-12-08 22:14:47 +053076
77 if (!fields) {
78 return fields;
79 }
80
81 return validateBgpConfiguration();
82 }
83
84 /**
85 * Returns routerId from the configuration.
86 *
87 * @return routerId
88 */
89 public String routerId() {
90 return get(ROUTER_ID, null);
91 }
92
93 /**
94 * Returns localAs number from the configuration.
95 *
96 * @return local As number
97 */
98 public int localAs() {
99 return Integer.parseInt(get(LOCAL_AS, null));
100 }
101
102 /**
103 * Returns max session from the configuration.
104 *
105 * @return max session
106 */
107 public int maxSession() {
108 return Integer.parseInt(get(MAX_SESSION, null));
109 }
110
111 /**
112 * Returns BGP-LS capability support from the configuration.
113 *
114 * @return true if BGP-LS capability is set else false
115 */
116 public boolean lsCapability() {
117 return Boolean.parseBoolean(get(LS_CAPABILITY, null));
118 }
119
120 /**
121 * Returns largeAs capability support from the configuration.
122 *
123 * @return largeAs capability
124 */
125 public boolean largeAsCapability() {
126 return Boolean.parseBoolean(get(LARGE_AS_CAPABILITY, null));
127 }
128
129 /**
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530130 * Returns flow specification capability support from the configuration.
131 *
132 * @return flow specification capability
133 */
134 public String flowSpecCapability() {
135 return get(FLOW_SPEC_CAPABILITY, null);
136 }
137
138 /**
Thejaswi N K38879622015-12-08 22:14:47 +0530139 * Returns holdTime of the local node from the configuration.
140 *
141 * @return holdTime
142 */
143 public short holdTime() {
144 return Short.parseShort(get(HOLD_TIME, null));
145 }
146
147 /**
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530148 * Validates the flow specification capability.
149 *
150 * @return true if valid else false
151 */
152 public boolean validateFlowSpec() {
153 if (flowSpecCapability() != null) {
154 String flowSpec = flowSpecCapability();
155 if ((flowSpec.equals("IPV4")) || (flowSpec.equals("VPNV4")) || (flowSpec.equals("IPV4_VPNV4"))) {
156 return true;
157 }
158 }
159
160 return false;
161 }
162
163 /**
Thejaswi N K38879622015-12-08 22:14:47 +0530164 * Validates the Bgp local and peer configuration.
165 *
166 * @return true if valid else false
167 */
168 public boolean validateBgpConfiguration() {
169
170 if (!validateLocalAs()) {
171 return false;
172 }
173
174 if (!validateRouterId()) {
175 return false;
176 }
177
178 if (!validateBgpPeers()) {
179 return false;
180 }
181
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530182 if (!validateFlowSpec()) {
183 return false;
184 }
Thejaswi N K38879622015-12-08 22:14:47 +0530185 return true;
186 }
187
188 /**
189 * Validates the Bgp As number.
190 *
191 * @return true if valid else false
192 */
193 public boolean validateLocalAs() {
194
195 long localAs = 0;
196 localAs = localAs();
197
198 if (bgpController.connectedPeerCount() != 0) {
199 return false;
200 }
201
202 if (largeAsCapability()) {
203
204 if (localAs == 0 || localAs >= MAX_LONG_AS_NUMBER) {
205 return false;
206 }
207 } else {
208 if (localAs == 0 || localAs >= MAX_SHORT_AS_NUMBER) {
209 return false;
210 }
211 }
212
213 return true;
214 }
215
216 /**
217 * Validates the Bgp peer As number.
218 *
Jian Lidfba7392016-01-22 16:46:58 -0800219 * @param remoteAs remote As number
Thejaswi N K38879622015-12-08 22:14:47 +0530220 * @return true if valid else false
221 */
222 public boolean validateRemoteAs(long remoteAs) {
223 if (largeAsCapability()) {
224
225 if (remoteAs == 0 || remoteAs >= MAX_LONG_AS_NUMBER) {
226 return false;
227 }
228 } else {
229 if (remoteAs == 0 || remoteAs >= MAX_SHORT_AS_NUMBER) {
230 return false;
231 }
232 }
233 return true;
234 }
235
236 /**
237 * Validates the Bgp Router ID configuration.
238 *
239 * @return true if valid else false
240 */
241 public boolean validateRouterId() {
242 String routerId = routerId();
243 if (bgpController.connectedPeerCount() != 0) {
244 return false;
245 }
246 return true;
247 }
248
249 /**
250 * Validates the Bgp peer holdTime.
251 *
Jian Lidfba7392016-01-22 16:46:58 -0800252 * @param remoteAs remote As number
Thejaswi N K38879622015-12-08 22:14:47 +0530253 * @return true if valid else false
254 */
255 public boolean validatePeerHoldTime(long remoteAs) {
256 //TODO:Validate it later..
257 return true;
258 }
259
260 /**
261 * Validates the Bgp peer configuration.
262 *
263 * @return true if valid else false
264 */
265 public boolean validateBgpPeers() {
266 List<BgpPeerConfig> nodes;
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530267 String connectMode;
Thejaswi N K38879622015-12-08 22:14:47 +0530268
269 nodes = bgpPeer();
270 for (int i = 0; i < nodes.size(); i++) {
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530271 connectMode = nodes.get(i).connectMode();
Thejaswi N K38879622015-12-08 22:14:47 +0530272 if ((IpAddress.valueOf(nodes.get(i).hostname()) == null) ||
273 !validateRemoteAs(nodes.get(i).asNumber()) ||
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530274 !validatePeerHoldTime(nodes.get(i).holdTime()) ||
275 !(connectMode.equals(PEER_CONNECT_ACTIVE) || connectMode.equals(PEER_CONNECT_PASSIVE))) {
Thejaswi N K38879622015-12-08 22:14:47 +0530276 return false;
277 }
278 }
279
280 return true;
281 }
282
283 /**
284 * Returns the set of nodes read from network config.
285 *
286 * @return list of BgpPeerConfig or null
287 */
288 public List<BgpPeerConfig> bgpPeer() {
289 List<BgpPeerConfig> nodes = new ArrayList<BgpPeerConfig>();
290
291 JsonNode jsonNodes = object.get(BGP_PEER);
292 if (jsonNodes == null) {
293 return null;
294 }
295
296 jsonNodes.forEach(jsonNode -> nodes.add(new BgpPeerConfig(
297 jsonNode.path(PEER_IP).asText(),
298 jsonNode.path(REMOTE_AS).asInt(),
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530299 jsonNode.path(PEER_HOLD_TIME).asInt(),
300 jsonNode.path(PEER_CONNECT_MODE).asText())));
Thejaswi N K38879622015-12-08 22:14:47 +0530301
302 return nodes;
303 }
304
305 /**
306 * Configuration for Bgp peer nodes.
307 */
308 public static class BgpPeerConfig {
309
310 private final String hostname;
311 private final int asNumber;
312 private final short holdTime;
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530313 private final String connectMode;
Thejaswi N K38879622015-12-08 22:14:47 +0530314
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530315 public BgpPeerConfig(String hostname, int asNumber, int holdTime, String connectMode) {
Thejaswi N K38879622015-12-08 22:14:47 +0530316 this.hostname = checkNotNull(hostname);
317 this.asNumber = asNumber;
318 this.holdTime = (short) holdTime;
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530319 this.connectMode = connectMode;
Thejaswi N K38879622015-12-08 22:14:47 +0530320 }
321
322 /**
323 * Returns hostname of the peer node.
324 *
325 * @return hostname
326 */
327 public String hostname() {
328 return this.hostname;
329 }
330
331 /**
332 * Returns asNumber if peer.
333 *
334 * @return asNumber
335 */
336 public int asNumber() {
337 return this.asNumber;
338 }
339
340 /**
341 * Returns holdTime of the peer node.
342 *
343 * @return holdTime
344 */
345 public short holdTime() {
346 return this.holdTime;
347 }
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530348
349 /**
350 * Returns connection mode for the peer node.
351 *
352 * @return active or passive connection
353 */
354 public String connectMode() {
355 return this.connectMode;
356 }
Thejaswi N K38879622015-12-08 22:14:47 +0530357 }
358}