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 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 18 | import org.osgi.service.component.annotations.Activate; |
| 19 | import org.osgi.service.component.annotations.Component; |
| 20 | import org.osgi.service.component.annotations.Deactivate; |
| 21 | import org.osgi.service.component.annotations.Reference; |
| 22 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 23 | |
| 24 | import org.onosproject.bgp.controller.BgpCfg; |
Thejaswi N K | 5ff45df | 2015-12-15 17:05:29 +0530 | [diff] [blame] | 25 | import org.onosproject.bgp.controller.BgpPeerCfg; |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.core.CoreService; |
| 28 | import org.onosproject.net.config.ConfigFactory; |
| 29 | import org.onosproject.net.config.NetworkConfigEvent; |
| 30 | import org.onosproject.net.config.NetworkConfigListener; |
| 31 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 32 | import org.onosproject.net.config.NetworkConfigService; |
| 33 | import org.onosproject.net.config.basics.SubjectFactories; |
| 34 | import org.onosproject.net.provider.AbstractProvider; |
| 35 | import org.onosproject.net.provider.ProviderId; |
| 36 | import org.onosproject.bgp.controller.BgpController; |
| 37 | import org.slf4j.Logger; |
| 38 | import org.osgi.service.component.ComponentContext; |
| 39 | |
Thejaswi N K | 5ff45df | 2015-12-15 17:05:29 +0530 | [diff] [blame] | 40 | import java.util.ArrayList; |
| 41 | import java.util.Iterator; |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 42 | import java.util.List; |
Thejaswi N K | 5ff45df | 2015-12-15 17:05:29 +0530 | [diff] [blame] | 43 | import java.util.Map; |
| 44 | import java.util.Set; |
| 45 | import java.util.TreeMap; |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 46 | |
| 47 | import static org.slf4j.LoggerFactory.getLogger; |
| 48 | |
| 49 | /** |
| 50 | * BGP config provider to validate and populate the configuration. |
| 51 | */ |
| 52 | @Component(immediate = true) |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 53 | public class BgpCfgProvider extends AbstractProvider { |
| 54 | |
| 55 | private static final Logger log = getLogger(BgpCfgProvider.class); |
| 56 | |
| 57 | static final String PROVIDER_ID = "org.onosproject.provider.bgp.cfg"; |
| 58 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 60 | protected BgpController bgpController; |
| 61 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 63 | protected CoreService coreService; |
| 64 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 66 | protected NetworkConfigRegistry configRegistry; |
| 67 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 69 | protected NetworkConfigService configService; |
| 70 | |
| 71 | private final ConfigFactory configFactory = |
| 72 | new ConfigFactory(SubjectFactories.APP_SUBJECT_FACTORY, BgpAppConfig.class, "bgpapp") { |
| 73 | @Override |
| 74 | public BgpAppConfig createConfig() { |
| 75 | return new BgpAppConfig(); |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | private final NetworkConfigListener configListener = new InternalConfigListener(); |
| 80 | |
| 81 | private ApplicationId appId; |
| 82 | |
| 83 | /** |
| 84 | * Creates a Bgp config provider. |
| 85 | */ |
| 86 | public BgpCfgProvider() { |
| 87 | super(new ProviderId("bgp", PROVIDER_ID)); |
| 88 | } |
| 89 | |
| 90 | @Activate |
| 91 | public void activate(ComponentContext context) { |
| 92 | appId = coreService.registerApplication(PROVIDER_ID); |
| 93 | configService.addListener(configListener); |
| 94 | configRegistry.registerConfigFactory(configFactory); |
Thejaswi N K | 5ff45df | 2015-12-15 17:05:29 +0530 | [diff] [blame] | 95 | readConfiguration(); |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 96 | log.info("BGP cfg provider started"); |
| 97 | } |
| 98 | |
| 99 | @Deactivate |
| 100 | public void deactivate(ComponentContext context) { |
| 101 | configRegistry.unregisterConfigFactory(configFactory); |
| 102 | configService.removeListener(configListener); |
| 103 | } |
| 104 | |
| 105 | void setBgpController(BgpController bgpController) { |
| 106 | this.bgpController = bgpController; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Reads the configuration and set it to the BGP-LS south bound protocol. |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 111 | */ |
| 112 | private void readConfiguration() { |
| 113 | BgpCfg bgpConfig = null; |
| 114 | List<BgpAppConfig.BgpPeerConfig> nodes; |
| 115 | bgpConfig = bgpController.getConfig(); |
| 116 | BgpAppConfig config = configRegistry.getConfig(appId, BgpAppConfig.class); |
| 117 | |
| 118 | if (config == null) { |
| 119 | log.warn("No configuration found"); |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | /*Set the configuration */ |
| 124 | bgpConfig.setRouterId(config.routerId()); |
| 125 | bgpConfig.setAsNumber(config.localAs()); |
| 126 | bgpConfig.setLsCapability(config.lsCapability()); |
| 127 | bgpConfig.setHoldTime(config.holdTime()); |
| 128 | bgpConfig.setMaxSession(config.maxSession()); |
| 129 | bgpConfig.setLargeASCapability(config.largeAsCapability()); |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame] | 130 | bgpConfig.setEvpnCapability(config.evpnCapability()); |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 131 | |
Ray Milkey | 3188c9b | 2016-09-12 11:51:07 -0700 | [diff] [blame] | 132 | if (config.flowSpecCapability() == null) { |
Shashikanth VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 133 | bgpConfig.setFlowSpecCapability(BgpCfg.FlowSpec.NONE); |
Ray Milkey | 3188c9b | 2016-09-12 11:51:07 -0700 | [diff] [blame] | 134 | } else { |
| 135 | if (config.flowSpecCapability().equals("IPV4")) { |
| 136 | bgpConfig.setFlowSpecCapability(BgpCfg.FlowSpec.IPV4); |
| 137 | } else if (config.flowSpecCapability().equals("VPNV4")) { |
| 138 | bgpConfig.setFlowSpecCapability(BgpCfg.FlowSpec.VPNV4); |
| 139 | } else if (config.flowSpecCapability().equals("IPV4_VPNV4")) { |
| 140 | bgpConfig.setFlowSpecCapability(BgpCfg.FlowSpec.IPV4_VPNV4); |
| 141 | } else { |
| 142 | bgpConfig.setFlowSpecCapability(BgpCfg.FlowSpec.NONE); |
| 143 | } |
Shashikanth VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 144 | } |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame] | 145 | bgpConfig.setFlowSpecRpdCapability(config.rpdCapability()); |
Shashikanth VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 146 | |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 147 | nodes = config.bgpPeer(); |
| 148 | for (int i = 0; i < nodes.size(); i++) { |
Thejaswi N K | 0008f1d | 2015-12-11 12:47:41 +0530 | [diff] [blame] | 149 | String connectMode = nodes.get(i).connectMode(); |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 150 | bgpConfig.addPeer(nodes.get(i).hostname(), nodes.get(i).asNumber(), nodes.get(i).holdTime()); |
Thejaswi N K | 0008f1d | 2015-12-11 12:47:41 +0530 | [diff] [blame] | 151 | if (connectMode.equals(BgpAppConfig.PEER_CONNECT_ACTIVE)) { |
| 152 | bgpConfig.connectPeer(nodes.get(i).hostname()); |
| 153 | } |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
| 157 | /** |
Thejaswi N K | 5ff45df | 2015-12-15 17:05:29 +0530 | [diff] [blame] | 158 | * Read the configuration and update it to the BGP-LS south bound protocol. |
| 159 | */ |
| 160 | private void updateConfiguration() { |
| 161 | BgpCfg bgpConfig = null; |
| 162 | List<BgpAppConfig.BgpPeerConfig> nodes; |
| 163 | TreeMap<String, BgpPeerCfg> bgpPeerTree; |
| 164 | bgpConfig = bgpController.getConfig(); |
| 165 | BgpPeerCfg peer = null; |
| 166 | BgpAppConfig config = configRegistry.getConfig(appId, BgpAppConfig.class); |
| 167 | |
| 168 | if (config == null) { |
| 169 | log.warn("No configuration found"); |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | |
| 174 | /* Update the self configuration */ |
Shashikanth VH | de66383 | 2016-04-25 18:42:04 +0530 | [diff] [blame] | 175 | if (bgpController.connectedPeerCount() != 0) { |
| 176 | //TODO: If connections already exist, disconnect |
| 177 | bgpController.closeConnectedPeers(); |
| 178 | } |
| 179 | bgpConfig.setRouterId(config.routerId()); |
| 180 | bgpConfig.setAsNumber(config.localAs()); |
| 181 | bgpConfig.setLsCapability(config.lsCapability()); |
| 182 | bgpConfig.setHoldTime(config.holdTime()); |
| 183 | bgpConfig.setMaxSession(config.maxSession()); |
| 184 | bgpConfig.setLargeASCapability(config.largeAsCapability()); |
Shashikanth VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 185 | |
Ray Milkey | 3188c9b | 2016-09-12 11:51:07 -0700 | [diff] [blame] | 186 | if (config.flowSpecCapability() == null) { |
Shashikanth VH | de66383 | 2016-04-25 18:42:04 +0530 | [diff] [blame] | 187 | bgpConfig.setFlowSpecCapability(BgpCfg.FlowSpec.NONE); |
Ray Milkey | 3188c9b | 2016-09-12 11:51:07 -0700 | [diff] [blame] | 188 | } else { |
| 189 | if (config.flowSpecCapability().equals("IPV4")) { |
| 190 | bgpConfig.setFlowSpecCapability(BgpCfg.FlowSpec.IPV4); |
| 191 | } else if (config.flowSpecCapability().equals("VPNV4")) { |
| 192 | bgpConfig.setFlowSpecCapability(BgpCfg.FlowSpec.VPNV4); |
| 193 | } else if (config.flowSpecCapability().equals("IPV4_VPNV4")) { |
| 194 | bgpConfig.setFlowSpecCapability(BgpCfg.FlowSpec.IPV4_VPNV4); |
| 195 | } else { |
| 196 | bgpConfig.setFlowSpecCapability(BgpCfg.FlowSpec.NONE); |
| 197 | } |
Thejaswi N K | 5ff45df | 2015-12-15 17:05:29 +0530 | [diff] [blame] | 198 | } |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame] | 199 | bgpConfig.setFlowSpecRpdCapability(config.rpdCapability()); |
Thejaswi N K | 5ff45df | 2015-12-15 17:05:29 +0530 | [diff] [blame] | 200 | |
| 201 | /* update the peer configuration */ |
| 202 | bgpPeerTree = bgpConfig.getPeerTree(); |
| 203 | if (bgpPeerTree.isEmpty()) { |
| 204 | log.info("There are no BGP peers to iterate"); |
| 205 | } else { |
| 206 | Set set = bgpPeerTree.entrySet(); |
| 207 | Iterator i = set.iterator(); |
| 208 | List<BgpPeerCfg> absPeerList = new ArrayList<BgpPeerCfg>(); |
| 209 | |
| 210 | boolean exists = false; |
| 211 | |
| 212 | while (i.hasNext()) { |
| 213 | Map.Entry me = (Map.Entry) i.next(); |
| 214 | peer = (BgpPeerCfg) me.getValue(); |
| 215 | |
| 216 | nodes = config.bgpPeer(); |
| 217 | for (int j = 0; j < nodes.size(); j++) { |
| 218 | String peerIp = nodes.get(j).hostname(); |
| 219 | if (peerIp.equals(peer.getPeerRouterId())) { |
| 220 | |
| 221 | if (bgpConfig.isPeerConnectable(peer.getPeerRouterId())) { |
| 222 | peer.setAsNumber(nodes.get(j).asNumber()); |
| 223 | peer.setHoldtime(nodes.get(j).holdTime()); |
| 224 | log.debug("Peer neighbor IP successfully modified :" + peer.getPeerRouterId()); |
| 225 | } else { |
| 226 | log.debug("Peer neighbor IP cannot be modified :" + peer.getPeerRouterId()); |
| 227 | } |
| 228 | |
| 229 | nodes.remove(j); |
| 230 | exists = true; |
| 231 | break; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | if (!exists) { |
| 236 | absPeerList.add(peer); |
| 237 | exists = false; |
| 238 | } |
Shashikanth VH | de66383 | 2016-04-25 18:42:04 +0530 | [diff] [blame] | 239 | |
| 240 | if (peer.connectPeer() != null) { |
| 241 | peer.connectPeer().disconnectPeer(); |
| 242 | peer.setConnectPeer(null); |
| 243 | } |
Thejaswi N K | 5ff45df | 2015-12-15 17:05:29 +0530 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | /* Remove the absent nodes. */ |
| 247 | for (int j = 0; j < absPeerList.size(); j++) { |
| 248 | bgpConfig.removePeer(absPeerList.get(j).getPeerRouterId()); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | |
| 253 | nodes = config.bgpPeer(); |
| 254 | for (int i = 0; i < nodes.size(); i++) { |
| 255 | String connectMode = nodes.get(i).connectMode(); |
| 256 | bgpConfig.addPeer(nodes.get(i).hostname(), nodes.get(i).asNumber(), nodes.get(i).holdTime()); |
| 257 | if (connectMode.equals(BgpAppConfig.PEER_CONNECT_ACTIVE)) { |
| 258 | bgpConfig.connectPeer(nodes.get(i).hostname()); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | } |
| 263 | |
| 264 | /** |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 265 | * BGP config listener to populate the configuration. |
| 266 | */ |
| 267 | private class InternalConfigListener implements NetworkConfigListener { |
| 268 | |
| 269 | @Override |
| 270 | public void event(NetworkConfigEvent event) { |
| 271 | if (!event.configClass().equals(BgpAppConfig.class)) { |
| 272 | return; |
| 273 | } |
| 274 | |
| 275 | switch (event.type()) { |
| 276 | case CONFIG_ADDED: |
| 277 | readConfiguration(); |
| 278 | break; |
| 279 | case CONFIG_UPDATED: |
Thejaswi N K | 5ff45df | 2015-12-15 17:05:29 +0530 | [diff] [blame] | 280 | updateConfiguration(); |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 281 | break; |
| 282 | case CONFIG_REMOVED: |
| 283 | default: |
| 284 | break; |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | } |