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"; |
| 50 | |
| 51 | public static final String BGP_PEER = "bgpPeer"; |
| 52 | public static final String PEER_IP = "peerIp"; |
| 53 | public static final String REMOTE_AS = "remoteAs"; |
| 54 | public static final String PEER_HOLD_TIME = "peerHoldTime"; |
| 55 | |
| 56 | static final int MAX_SHORT_AS_NUMBER = 65535; |
| 57 | static final long MAX_LONG_AS_NUMBER = 4294967295L; |
| 58 | |
| 59 | @Override |
| 60 | public boolean isValid() { |
| 61 | boolean fields = false; |
| 62 | |
| 63 | this.bgpController = DefaultServiceDirectory.getService(BgpController.class); |
| 64 | bgpConfig = bgpController.getConfig(); |
| 65 | |
| 66 | fields = hasOnlyFields(ROUTER_ID, LOCAL_AS, MAX_SESSION, LS_CAPABILITY, |
| 67 | HOLD_TIME, LARGE_AS_CAPABILITY, BGP_PEER) && |
| 68 | isIpAddress(ROUTER_ID, MANDATORY) && isNumber(LOCAL_AS, MANDATORY) && |
| 69 | isNumber(MAX_SESSION, OPTIONAL, 20) && isNumber(HOLD_TIME, OPTIONAL, 180) && |
| 70 | isBoolean(LS_CAPABILITY, OPTIONAL) && isBoolean(LARGE_AS_CAPABILITY, OPTIONAL); |
| 71 | |
| 72 | if (!fields) { |
| 73 | return fields; |
| 74 | } |
| 75 | |
| 76 | return validateBgpConfiguration(); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Returns routerId from the configuration. |
| 81 | * |
| 82 | * @return routerId |
| 83 | */ |
| 84 | public String routerId() { |
| 85 | return get(ROUTER_ID, null); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Returns localAs number from the configuration. |
| 90 | * |
| 91 | * @return local As number |
| 92 | */ |
| 93 | public int localAs() { |
| 94 | return Integer.parseInt(get(LOCAL_AS, null)); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Returns max session from the configuration. |
| 99 | * |
| 100 | * @return max session |
| 101 | */ |
| 102 | public int maxSession() { |
| 103 | return Integer.parseInt(get(MAX_SESSION, null)); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Returns BGP-LS capability support from the configuration. |
| 108 | * |
| 109 | * @return true if BGP-LS capability is set else false |
| 110 | */ |
| 111 | public boolean lsCapability() { |
| 112 | return Boolean.parseBoolean(get(LS_CAPABILITY, null)); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Returns largeAs capability support from the configuration. |
| 117 | * |
| 118 | * @return largeAs capability |
| 119 | */ |
| 120 | public boolean largeAsCapability() { |
| 121 | return Boolean.parseBoolean(get(LARGE_AS_CAPABILITY, null)); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Returns holdTime of the local node from the configuration. |
| 126 | * |
| 127 | * @return holdTime |
| 128 | */ |
| 129 | public short holdTime() { |
| 130 | return Short.parseShort(get(HOLD_TIME, null)); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Validates the Bgp local and peer configuration. |
| 135 | * |
| 136 | * @return true if valid else false |
| 137 | */ |
| 138 | public boolean validateBgpConfiguration() { |
| 139 | |
| 140 | if (!validateLocalAs()) { |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | if (!validateRouterId()) { |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | if (!validateBgpPeers()) { |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | return true; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Validates the Bgp As number. |
| 157 | * |
| 158 | * @return true if valid else false |
| 159 | */ |
| 160 | public boolean validateLocalAs() { |
| 161 | |
| 162 | long localAs = 0; |
| 163 | localAs = localAs(); |
| 164 | |
| 165 | if (bgpController.connectedPeerCount() != 0) { |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | if (largeAsCapability()) { |
| 170 | |
| 171 | if (localAs == 0 || localAs >= MAX_LONG_AS_NUMBER) { |
| 172 | return false; |
| 173 | } |
| 174 | } else { |
| 175 | if (localAs == 0 || localAs >= MAX_SHORT_AS_NUMBER) { |
| 176 | return false; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Validates the Bgp peer As number. |
| 185 | * |
| 186 | * @return true if valid else false |
| 187 | */ |
| 188 | public boolean validateRemoteAs(long remoteAs) { |
| 189 | if (largeAsCapability()) { |
| 190 | |
| 191 | if (remoteAs == 0 || remoteAs >= MAX_LONG_AS_NUMBER) { |
| 192 | return false; |
| 193 | } |
| 194 | } else { |
| 195 | if (remoteAs == 0 || remoteAs >= MAX_SHORT_AS_NUMBER) { |
| 196 | return false; |
| 197 | } |
| 198 | } |
| 199 | return true; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Validates the Bgp Router ID configuration. |
| 204 | * |
| 205 | * @return true if valid else false |
| 206 | */ |
| 207 | public boolean validateRouterId() { |
| 208 | String routerId = routerId(); |
| 209 | if (bgpController.connectedPeerCount() != 0) { |
| 210 | return false; |
| 211 | } |
| 212 | return true; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Validates the Bgp peer holdTime. |
| 217 | * |
| 218 | * @return true if valid else false |
| 219 | */ |
| 220 | public boolean validatePeerHoldTime(long remoteAs) { |
| 221 | //TODO:Validate it later.. |
| 222 | return true; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Validates the Bgp peer configuration. |
| 227 | * |
| 228 | * @return true if valid else false |
| 229 | */ |
| 230 | public boolean validateBgpPeers() { |
| 231 | List<BgpPeerConfig> nodes; |
| 232 | |
| 233 | nodes = bgpPeer(); |
| 234 | for (int i = 0; i < nodes.size(); i++) { |
| 235 | if ((IpAddress.valueOf(nodes.get(i).hostname()) == null) || |
| 236 | !validateRemoteAs(nodes.get(i).asNumber()) || |
| 237 | !validatePeerHoldTime(nodes.get(i).holdTime())) { |
| 238 | return false; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | return true; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Returns the set of nodes read from network config. |
| 247 | * |
| 248 | * @return list of BgpPeerConfig or null |
| 249 | */ |
| 250 | public List<BgpPeerConfig> bgpPeer() { |
| 251 | List<BgpPeerConfig> nodes = new ArrayList<BgpPeerConfig>(); |
| 252 | |
| 253 | JsonNode jsonNodes = object.get(BGP_PEER); |
| 254 | if (jsonNodes == null) { |
| 255 | return null; |
| 256 | } |
| 257 | |
| 258 | jsonNodes.forEach(jsonNode -> nodes.add(new BgpPeerConfig( |
| 259 | jsonNode.path(PEER_IP).asText(), |
| 260 | jsonNode.path(REMOTE_AS).asInt(), |
| 261 | jsonNode.path(PEER_HOLD_TIME).asInt()))); |
| 262 | |
| 263 | return nodes; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Configuration for Bgp peer nodes. |
| 268 | */ |
| 269 | public static class BgpPeerConfig { |
| 270 | |
| 271 | private final String hostname; |
| 272 | private final int asNumber; |
| 273 | private final short holdTime; |
| 274 | |
| 275 | public BgpPeerConfig(String hostname, int asNumber, int holdTime) { |
| 276 | this.hostname = checkNotNull(hostname); |
| 277 | this.asNumber = asNumber; |
| 278 | this.holdTime = (short) holdTime; |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Returns hostname of the peer node. |
| 283 | * |
| 284 | * @return hostname |
| 285 | */ |
| 286 | public String hostname() { |
| 287 | return this.hostname; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Returns asNumber if peer. |
| 292 | * |
| 293 | * @return asNumber |
| 294 | */ |
| 295 | public int asNumber() { |
| 296 | return this.asNumber; |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Returns holdTime of the peer node. |
| 301 | * |
| 302 | * @return holdTime |
| 303 | */ |
| 304 | public short holdTime() { |
| 305 | return this.holdTime; |
| 306 | } |
| 307 | } |
| 308 | } |