Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.provider.bgp.cfg.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 19 | import org.osgi.service.component.annotations.Reference; |
| 20 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 21 | import org.onlab.osgi.DefaultServiceDirectory; |
| 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onosproject.bgp.controller.BgpCfg; |
| 24 | import org.onosproject.bgp.controller.BgpController; |
| 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.net.config.Config; |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 29 | |
| 30 | import java.util.ArrayList; |
| 31 | import java.util.List; |
| 32 | |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame] | 33 | import static com.google.common.base.Preconditions.checkNotNull; |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 34 | import static org.onosproject.net.config.Config.FieldPresence.MANDATORY; |
| 35 | import static org.onosproject.net.config.Config.FieldPresence.OPTIONAL; |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Configuration object for BGP. |
| 39 | */ |
| 40 | public class BgpAppConfig extends Config<ApplicationId> { |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 41 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 42 | BgpController bgpController; |
| 43 | |
| 44 | BgpCfg bgpConfig = null; |
| 45 | |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 46 | protected final Logger log = LoggerFactory.getLogger(BgpAppConfig.class); |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 47 | 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 VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 53 | public static final String FLOW_SPEC_CAPABILITY = "flowSpecCapability"; |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame] | 54 | public static final String FLOW_SPEC_RPD_CAPABILITY = "flowSpecRpdCapability"; |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame] | 55 | public static final String EVPN_CAPABILITY = "evpnCapability"; |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 56 | |
| 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 K | 0008f1d | 2015-12-11 12:47:41 +0530 | [diff] [blame] | 61 | 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 K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 64 | |
| 65 | static final int MAX_SHORT_AS_NUMBER = 65535; |
| 66 | static final long MAX_LONG_AS_NUMBER = 4294967295L; |
| 67 | |
Shashikanth VH | de66383 | 2016-04-25 18:42:04 +0530 | [diff] [blame] | 68 | 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 K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 74 | @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 Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame] | 82 | HOLD_TIME, LARGE_AS_CAPABILITY, FLOW_SPEC_CAPABILITY, |
| 83 | FLOW_SPEC_RPD_CAPABILITY, BGP_PEER, EVPN_CAPABILITY) && |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 84 | isIpAddress(ROUTER_ID, MANDATORY) && isNumber(LOCAL_AS, MANDATORY) && |
Shashikanth VH | de66383 | 2016-04-25 18:42:04 +0530 | [diff] [blame] | 85 | isNumber(MAX_SESSION, OPTIONAL, MIN_SESSION_NUMBER, MAX_SESSION_NUMBER) |
| 86 | && isNumber(HOLD_TIME, OPTIONAL, MIN_HOLDTIME, MAX_HOLDTIME) && |
Shashikanth VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 87 | isBoolean(LS_CAPABILITY, OPTIONAL) && isBoolean(LARGE_AS_CAPABILITY, OPTIONAL) && |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame] | 88 | isString(FLOW_SPEC_CAPABILITY, OPTIONAL) && isBoolean(FLOW_SPEC_RPD_CAPABILITY, OPTIONAL) |
| 89 | && isBoolean(EVPN_CAPABILITY, OPTIONAL); |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 90 | |
| 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 VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame] | 135 | * 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 K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 144 | * 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 VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 153 | * 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 K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 162 | * 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 VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 171 | * 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 VH | 09792f0 | 2016-05-10 16:59:57 +0530 | [diff] [blame] | 178 | if ((!flowSpec.equals("IPV4")) && (!flowSpec.equals("VPNV4")) && (!flowSpec.equals("IPV4_VPNV4"))) { |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 179 | log.debug("Flow specification capabality is false"); |
Shashikanth VH | 09792f0 | 2016-05-10 16:59:57 +0530 | [diff] [blame] | 180 | return false; |
Shashikanth VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 181 | } |
| 182 | } |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 183 | log.debug("Flow specification capabality is true"); |
Shashikanth VH | 09792f0 | 2016-05-10 16:59:57 +0530 | [diff] [blame] | 184 | return true; |
Shashikanth VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /** |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame] | 188 | * 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 VH | de66383 | 2016-04-25 18:42:04 +0530 | [diff] [blame] | 197 | * 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 K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 212 | * 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 VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 230 | if (!validateFlowSpec()) { |
| 231 | return false; |
| 232 | } |
Shashikanth VH | de66383 | 2016-04-25 18:42:04 +0530 | [diff] [blame] | 233 | |
| 234 | if (!validateHoldTime()) { |
| 235 | return false; |
| 236 | } |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 237 | 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 K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 250 | 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 Li | dfba739 | 2016-01-22 16:46:58 -0800 | [diff] [blame] | 267 | * @param remoteAs remote As number |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 268 | * @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 VH | de66383 | 2016-04-25 18:42:04 +0530 | [diff] [blame] | 291 | // TODO: router ID validation |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 292 | return true; |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Validates the Bgp peer holdTime. |
| 297 | * |
Jian Li | dfba739 | 2016-01-22 16:46:58 -0800 | [diff] [blame] | 298 | * @param remoteAs remote As number |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 299 | * @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 K | 0008f1d | 2015-12-11 12:47:41 +0530 | [diff] [blame] | 313 | String connectMode; |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 314 | |
| 315 | nodes = bgpPeer(); |
| 316 | for (int i = 0; i < nodes.size(); i++) { |
Thejaswi N K | 0008f1d | 2015-12-11 12:47:41 +0530 | [diff] [blame] | 317 | connectMode = nodes.get(i).connectMode(); |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 318 | if ((IpAddress.valueOf(nodes.get(i).hostname()) == null) || |
| 319 | !validateRemoteAs(nodes.get(i).asNumber()) || |
Thejaswi N K | 0008f1d | 2015-12-11 12:47:41 +0530 | [diff] [blame] | 320 | !validatePeerHoldTime(nodes.get(i).holdTime()) || |
| 321 | !(connectMode.equals(PEER_CONNECT_ACTIVE) || connectMode.equals(PEER_CONNECT_PASSIVE))) { |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 322 | log.debug("BGP peer configration false"); |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 323 | return false; |
| 324 | } |
| 325 | } |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 326 | log.debug("BGP peer configration true"); |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 327 | 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 K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 337 | 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 K | 0008f1d | 2015-12-11 12:47:41 +0530 | [diff] [blame] | 345 | jsonNode.path(PEER_HOLD_TIME).asInt(), |
| 346 | jsonNode.path(PEER_CONNECT_MODE).asText()))); |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 347 | |
| 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 K | 0008f1d | 2015-12-11 12:47:41 +0530 | [diff] [blame] | 359 | private final String connectMode; |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 360 | |
Thejaswi N K | 0008f1d | 2015-12-11 12:47:41 +0530 | [diff] [blame] | 361 | public BgpPeerConfig(String hostname, int asNumber, int holdTime, String connectMode) { |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 362 | this.hostname = checkNotNull(hostname); |
| 363 | this.asNumber = asNumber; |
| 364 | this.holdTime = (short) holdTime; |
Thejaswi N K | 0008f1d | 2015-12-11 12:47:41 +0530 | [diff] [blame] | 365 | this.connectMode = connectMode; |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 366 | } |
| 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 K | 0008f1d | 2015-12-11 12:47:41 +0530 | [diff] [blame] | 394 | |
| 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 K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 403 | } |
| 404 | } |