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 org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.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 | |
| 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 60 | protected BgpController bgpController; |
| 61 | |
| 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 63 | protected CoreService coreService; |
| 64 | |
| 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 66 | protected NetworkConfigRegistry configRegistry; |
| 67 | |
| 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 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. |
| 111 | * |
| 112 | * @return void |
| 113 | */ |
| 114 | private void readConfiguration() { |
| 115 | BgpCfg bgpConfig = null; |
| 116 | List<BgpAppConfig.BgpPeerConfig> nodes; |
| 117 | bgpConfig = bgpController.getConfig(); |
| 118 | BgpAppConfig config = configRegistry.getConfig(appId, BgpAppConfig.class); |
| 119 | |
| 120 | if (config == null) { |
| 121 | log.warn("No configuration found"); |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | /*Set the configuration */ |
| 126 | bgpConfig.setRouterId(config.routerId()); |
| 127 | bgpConfig.setAsNumber(config.localAs()); |
| 128 | bgpConfig.setLsCapability(config.lsCapability()); |
| 129 | bgpConfig.setHoldTime(config.holdTime()); |
| 130 | bgpConfig.setMaxSession(config.maxSession()); |
| 131 | bgpConfig.setLargeASCapability(config.largeAsCapability()); |
| 132 | |
| 133 | nodes = config.bgpPeer(); |
| 134 | for (int i = 0; i < nodes.size(); i++) { |
Thejaswi N K | 0008f1d | 2015-12-11 12:47:41 +0530 | [diff] [blame] | 135 | String connectMode = nodes.get(i).connectMode(); |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 136 | 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] | 137 | if (connectMode.equals(BgpAppConfig.PEER_CONNECT_ACTIVE)) { |
| 138 | bgpConfig.connectPeer(nodes.get(i).hostname()); |
| 139 | } |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
Thejaswi N K | 5ff45df | 2015-12-15 17:05:29 +0530 | [diff] [blame^] | 144 | * Read the configuration and update it to the BGP-LS south bound protocol. |
| 145 | */ |
| 146 | private void updateConfiguration() { |
| 147 | BgpCfg bgpConfig = null; |
| 148 | List<BgpAppConfig.BgpPeerConfig> nodes; |
| 149 | TreeMap<String, BgpPeerCfg> bgpPeerTree; |
| 150 | bgpConfig = bgpController.getConfig(); |
| 151 | BgpPeerCfg peer = null; |
| 152 | BgpAppConfig config = configRegistry.getConfig(appId, BgpAppConfig.class); |
| 153 | |
| 154 | if (config == null) { |
| 155 | log.warn("No configuration found"); |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | /* Update the self configuration */ |
| 161 | if (bgpController.connectedPeerCount() == 0) { |
| 162 | bgpConfig.setRouterId(config.routerId()); |
| 163 | bgpConfig.setAsNumber(config.localAs()); |
| 164 | bgpConfig.setLsCapability(config.lsCapability()); |
| 165 | bgpConfig.setHoldTime(config.holdTime()); |
| 166 | bgpConfig.setMaxSession(config.maxSession()); |
| 167 | bgpConfig.setLargeASCapability(config.largeAsCapability()); |
| 168 | } else { |
| 169 | log.info(" Self configuration cannot be modified as there is existing connections "); |
| 170 | } |
| 171 | |
| 172 | /* update the peer configuration */ |
| 173 | bgpPeerTree = bgpConfig.getPeerTree(); |
| 174 | if (bgpPeerTree.isEmpty()) { |
| 175 | log.info("There are no BGP peers to iterate"); |
| 176 | } else { |
| 177 | Set set = bgpPeerTree.entrySet(); |
| 178 | Iterator i = set.iterator(); |
| 179 | List<BgpPeerCfg> absPeerList = new ArrayList<BgpPeerCfg>(); |
| 180 | |
| 181 | boolean exists = false; |
| 182 | |
| 183 | while (i.hasNext()) { |
| 184 | Map.Entry me = (Map.Entry) i.next(); |
| 185 | peer = (BgpPeerCfg) me.getValue(); |
| 186 | |
| 187 | nodes = config.bgpPeer(); |
| 188 | for (int j = 0; j < nodes.size(); j++) { |
| 189 | String peerIp = nodes.get(j).hostname(); |
| 190 | if (peerIp.equals(peer.getPeerRouterId())) { |
| 191 | |
| 192 | if (bgpConfig.isPeerConnectable(peer.getPeerRouterId())) { |
| 193 | peer.setAsNumber(nodes.get(j).asNumber()); |
| 194 | peer.setHoldtime(nodes.get(j).holdTime()); |
| 195 | log.debug("Peer neighbor IP successfully modified :" + peer.getPeerRouterId()); |
| 196 | } else { |
| 197 | log.debug("Peer neighbor IP cannot be modified :" + peer.getPeerRouterId()); |
| 198 | } |
| 199 | |
| 200 | nodes.remove(j); |
| 201 | exists = true; |
| 202 | break; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | if (!exists) { |
| 207 | absPeerList.add(peer); |
| 208 | exists = false; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /* Remove the absent nodes. */ |
| 213 | for (int j = 0; j < absPeerList.size(); j++) { |
| 214 | bgpConfig.removePeer(absPeerList.get(j).getPeerRouterId()); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | |
| 219 | nodes = config.bgpPeer(); |
| 220 | for (int i = 0; i < nodes.size(); i++) { |
| 221 | String connectMode = nodes.get(i).connectMode(); |
| 222 | bgpConfig.addPeer(nodes.get(i).hostname(), nodes.get(i).asNumber(), nodes.get(i).holdTime()); |
| 223 | if (connectMode.equals(BgpAppConfig.PEER_CONNECT_ACTIVE)) { |
| 224 | bgpConfig.connectPeer(nodes.get(i).hostname()); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | } |
| 229 | |
| 230 | /** |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 231 | * BGP config listener to populate the configuration. |
| 232 | */ |
| 233 | private class InternalConfigListener implements NetworkConfigListener { |
| 234 | |
| 235 | @Override |
| 236 | public void event(NetworkConfigEvent event) { |
| 237 | if (!event.configClass().equals(BgpAppConfig.class)) { |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | switch (event.type()) { |
| 242 | case CONFIG_ADDED: |
| 243 | readConfiguration(); |
| 244 | break; |
| 245 | case CONFIG_UPDATED: |
Thejaswi N K | 5ff45df | 2015-12-15 17:05:29 +0530 | [diff] [blame^] | 246 | updateConfiguration(); |
Thejaswi N K | 3887962 | 2015-12-08 22:14:47 +0530 | [diff] [blame] | 247 | break; |
| 248 | case CONFIG_REMOVED: |
| 249 | default: |
| 250 | break; |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | } |