Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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"; |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 51 | |
| 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 K | 0008f1d | 2015-12-11 12:47:41 +0530 | [diff] [blame] | 56 | 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 K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 59 | |
| 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 VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 71 | HOLD_TIME, LARGE_AS_CAPABILITY, FLOW_SPEC_CAPABILITY, BGP_PEER) && |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 72 | isIpAddress(ROUTER_ID, MANDATORY) && isNumber(LOCAL_AS, MANDATORY) && |
| 73 | isNumber(MAX_SESSION, OPTIONAL, 20) && isNumber(HOLD_TIME, OPTIONAL, 180) && |
Shashikanth VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 74 | isBoolean(LS_CAPABILITY, OPTIONAL) && isBoolean(LARGE_AS_CAPABILITY, OPTIONAL) && |
| 75 | isString(FLOW_SPEC_CAPABILITY, OPTIONAL); |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 76 | |
| 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 VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 130 | * 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 K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 139 | * 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 VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 148 | * 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 K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 164 | * 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 VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 182 | if (!validateFlowSpec()) { |
| 183 | return false; |
| 184 | } |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 185 | 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 Li | dfba739 | 2016-01-22 16:46:58 -0800 | [diff] [blame] | 219 | * @param remoteAs remote As number |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 220 | * @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 Li | dfba739 | 2016-01-22 16:46:58 -0800 | [diff] [blame] | 252 | * @param remoteAs remote As number |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 253 | * @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 K | 0008f1d | 2015-12-11 12:47:41 +0530 | [diff] [blame] | 267 | String connectMode; |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 268 | |
| 269 | nodes = bgpPeer(); |
| 270 | for (int i = 0; i < nodes.size(); i++) { |
Thejaswi N K | 0008f1d | 2015-12-11 12:47:41 +0530 | [diff] [blame] | 271 | connectMode = nodes.get(i).connectMode(); |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 272 | if ((IpAddress.valueOf(nodes.get(i).hostname()) == null) || |
| 273 | !validateRemoteAs(nodes.get(i).asNumber()) || |
Thejaswi N K | 0008f1d | 2015-12-11 12:47:41 +0530 | [diff] [blame] | 274 | !validatePeerHoldTime(nodes.get(i).holdTime()) || |
| 275 | !(connectMode.equals(PEER_CONNECT_ACTIVE) || connectMode.equals(PEER_CONNECT_PASSIVE))) { |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 276 | 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 K | 0008f1d | 2015-12-11 12:47:41 +0530 | [diff] [blame] | 299 | jsonNode.path(PEER_HOLD_TIME).asInt(), |
| 300 | jsonNode.path(PEER_CONNECT_MODE).asText()))); |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 301 | |
| 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 K | 0008f1d | 2015-12-11 12:47:41 +0530 | [diff] [blame] | 313 | private final String connectMode; |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 314 | |
Thejaswi N K | 0008f1d | 2015-12-11 12:47:41 +0530 | [diff] [blame] | 315 | public BgpPeerConfig(String hostname, int asNumber, int holdTime, String connectMode) { |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 316 | this.hostname = checkNotNull(hostname); |
| 317 | this.asNumber = asNumber; |
| 318 | this.holdTime = (short) holdTime; |
Thejaswi N K | 0008f1d | 2015-12-11 12:47:41 +0530 | [diff] [blame] | 319 | this.connectMode = connectMode; |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 320 | } |
| 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 K | 0008f1d | 2015-12-11 12:47:41 +0530 | [diff] [blame] | 348 | |
| 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 K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 357 | } |
| 358 | } |