Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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; |
| 19 | import org.apache.felix.scr.annotations.Reference; |
| 20 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 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; |
| 27 | |
| 28 | import java.util.ArrayList; |
| 29 | import java.util.List; |
| 30 | |
| 31 | import static org.onosproject.net.config.Config.FieldPresence.MANDATORY; |
| 32 | import static org.onosproject.net.config.Config.FieldPresence.OPTIONAL; |
| 33 | import static com.google.common.base.Preconditions.checkNotNull; |
| 34 | |
| 35 | /** |
| 36 | * Configuration object for BGP. |
| 37 | */ |
| 38 | public 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 VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 50 | public static final String FLOW_SPEC_CAPABILITY = "flowSpecCapability"; |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame] | 51 | public static final String FLOW_SPEC_RPD_CAPABILITY = "flowSpecRpdCapability"; |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 52 | |
| 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 K | 0008f1d | 2015-12-11 12:47:41 +0530 | [diff] [blame] | 57 | 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 K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 60 | |
| 61 | static final int MAX_SHORT_AS_NUMBER = 65535; |
| 62 | static final long MAX_LONG_AS_NUMBER = 4294967295L; |
| 63 | |
Shashikanth VH | de66383 | 2016-04-25 18:42:04 +0530 | [diff] [blame] | 64 | 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 K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 70 | @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 VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame] | 78 | HOLD_TIME, LARGE_AS_CAPABILITY, FLOW_SPEC_CAPABILITY, FLOW_SPEC_RPD_CAPABILITY, BGP_PEER) && |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 79 | isIpAddress(ROUTER_ID, MANDATORY) && isNumber(LOCAL_AS, MANDATORY) && |
Shashikanth VH | de66383 | 2016-04-25 18:42:04 +0530 | [diff] [blame] | 80 | isNumber(MAX_SESSION, OPTIONAL, MIN_SESSION_NUMBER, MAX_SESSION_NUMBER) |
| 81 | && isNumber(HOLD_TIME, OPTIONAL, MIN_HOLDTIME, MAX_HOLDTIME) && |
Shashikanth VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 82 | isBoolean(LS_CAPABILITY, OPTIONAL) && isBoolean(LARGE_AS_CAPABILITY, OPTIONAL) && |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame] | 83 | isString(FLOW_SPEC_CAPABILITY, OPTIONAL) && isBoolean(FLOW_SPEC_RPD_CAPABILITY, OPTIONAL); |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 84 | |
| 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 VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame] | 129 | * 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 K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 138 | * 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 VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 147 | * 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 K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 156 | * 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 VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 165 | * 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(); |
| 172 | if ((flowSpec.equals("IPV4")) || (flowSpec.equals("VPNV4")) || (flowSpec.equals("IPV4_VPNV4"))) { |
| 173 | return true; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | /** |
Shashikanth VH | de66383 | 2016-04-25 18:42:04 +0530 | [diff] [blame] | 181 | * 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 K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 196 | * 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 VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 214 | if (!validateFlowSpec()) { |
| 215 | return false; |
| 216 | } |
Shashikanth VH | de66383 | 2016-04-25 18:42:04 +0530 | [diff] [blame] | 217 | |
| 218 | if (!validateHoldTime()) { |
| 219 | return false; |
| 220 | } |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 221 | 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 K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 234 | 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 Li | dfba739 | 2016-01-22 16:46:58 -0800 | [diff] [blame] | 251 | * @param remoteAs remote As number |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 252 | * @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 VH | de66383 | 2016-04-25 18:42:04 +0530 | [diff] [blame] | 275 | // TODO: router ID validation |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 276 | return true; |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Validates the Bgp peer holdTime. |
| 281 | * |
Jian Li | dfba739 | 2016-01-22 16:46:58 -0800 | [diff] [blame] | 282 | * @param remoteAs remote As number |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 283 | * @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 K | 0008f1d | 2015-12-11 12:47:41 +0530 | [diff] [blame] | 297 | String connectMode; |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 298 | |
| 299 | nodes = bgpPeer(); |
| 300 | for (int i = 0; i < nodes.size(); i++) { |
Thejaswi N K | 0008f1d | 2015-12-11 12:47:41 +0530 | [diff] [blame] | 301 | connectMode = nodes.get(i).connectMode(); |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 302 | if ((IpAddress.valueOf(nodes.get(i).hostname()) == null) || |
| 303 | !validateRemoteAs(nodes.get(i).asNumber()) || |
Thejaswi N K | 0008f1d | 2015-12-11 12:47:41 +0530 | [diff] [blame] | 304 | !validatePeerHoldTime(nodes.get(i).holdTime()) || |
| 305 | !(connectMode.equals(PEER_CONNECT_ACTIVE) || connectMode.equals(PEER_CONNECT_PASSIVE))) { |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 306 | 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 K | 0008f1d | 2015-12-11 12:47:41 +0530 | [diff] [blame] | 329 | jsonNode.path(PEER_HOLD_TIME).asInt(), |
| 330 | jsonNode.path(PEER_CONNECT_MODE).asText()))); |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 331 | |
| 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 K | 0008f1d | 2015-12-11 12:47:41 +0530 | [diff] [blame] | 343 | private final String connectMode; |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 344 | |
Thejaswi N K | 0008f1d | 2015-12-11 12:47:41 +0530 | [diff] [blame] | 345 | public BgpPeerConfig(String hostname, int asNumber, int holdTime, String connectMode) { |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 346 | this.hostname = checkNotNull(hostname); |
| 347 | this.asNumber = asNumber; |
| 348 | this.holdTime = (short) holdTime; |
Thejaswi N K | 0008f1d | 2015-12-11 12:47:41 +0530 | [diff] [blame] | 349 | this.connectMode = connectMode; |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 350 | } |
| 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 K | 0008f1d | 2015-12-11 12:47:41 +0530 | [diff] [blame] | 378 | |
| 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 K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 387 | } |
| 388 | } |