blob: 268962cbbb6c138e61040e06b6144f59960fe0f4 [file] [log] [blame]
Thejaswi N K38879622015-12-08 22:14:47 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thejaswi N K38879622015-12-08 22:14:47 +05303 *
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 */
16package org.onosproject.provider.bgp.cfg.impl;
17
18import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
Thejaswi N K38879622015-12-08 22:14:47 +053023
24import org.onosproject.bgp.controller.BgpCfg;
Thejaswi N K5ff45df2015-12-15 17:05:29 +053025import org.onosproject.bgp.controller.BgpPeerCfg;
Thejaswi N K38879622015-12-08 22:14:47 +053026import org.onosproject.core.ApplicationId;
27import org.onosproject.core.CoreService;
28import org.onosproject.net.config.ConfigFactory;
29import org.onosproject.net.config.NetworkConfigEvent;
30import org.onosproject.net.config.NetworkConfigListener;
31import org.onosproject.net.config.NetworkConfigRegistry;
32import org.onosproject.net.config.NetworkConfigService;
33import org.onosproject.net.config.basics.SubjectFactories;
34import org.onosproject.net.provider.AbstractProvider;
35import org.onosproject.net.provider.ProviderId;
36import org.onosproject.bgp.controller.BgpController;
37import org.slf4j.Logger;
38import org.osgi.service.component.ComponentContext;
39
Thejaswi N K5ff45df2015-12-15 17:05:29 +053040import java.util.ArrayList;
41import java.util.Iterator;
Thejaswi N K38879622015-12-08 22:14:47 +053042import java.util.List;
Thejaswi N K5ff45df2015-12-15 17:05:29 +053043import java.util.Map;
44import java.util.Set;
45import java.util.TreeMap;
Thejaswi N K38879622015-12-08 22:14:47 +053046
47import static org.slf4j.LoggerFactory.getLogger;
48
49/**
50 * BGP config provider to validate and populate the configuration.
51 */
52@Component(immediate = true)
Thejaswi N K38879622015-12-08 22:14:47 +053053public 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 K5ff45df2015-12-15 17:05:29 +053095 readConfiguration();
Thejaswi N K38879622015-12-08 22:14:47 +053096 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 K38879622015-12-08 22:14:47 +0530111 */
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());
130
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530131 if (config.flowSpecCapability().equals("IPV4")) {
132 bgpConfig.setFlowSpecCapability(BgpCfg.FlowSpec.IPV4);
133 } else if (config.flowSpecCapability().equals("VPNV4")) {
134 bgpConfig.setFlowSpecCapability(BgpCfg.FlowSpec.VPNV4);
135 } else if (config.flowSpecCapability().equals("IPV4_VPNV4")) {
136 bgpConfig.setFlowSpecCapability(BgpCfg.FlowSpec.IPV4_VPNV4);
137 } else {
138 bgpConfig.setFlowSpecCapability(BgpCfg.FlowSpec.NONE);
139 }
Shashikanth VHb650bfa2016-04-18 12:54:03 +0530140 bgpConfig.setFlowSpecRpdCapability(config.rpdCapability());
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530141
Thejaswi N K38879622015-12-08 22:14:47 +0530142 nodes = config.bgpPeer();
143 for (int i = 0; i < nodes.size(); i++) {
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530144 String connectMode = nodes.get(i).connectMode();
Thejaswi N K38879622015-12-08 22:14:47 +0530145 bgpConfig.addPeer(nodes.get(i).hostname(), nodes.get(i).asNumber(), nodes.get(i).holdTime());
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530146 if (connectMode.equals(BgpAppConfig.PEER_CONNECT_ACTIVE)) {
147 bgpConfig.connectPeer(nodes.get(i).hostname());
148 }
Thejaswi N K38879622015-12-08 22:14:47 +0530149 }
150 }
151
152 /**
Thejaswi N K5ff45df2015-12-15 17:05:29 +0530153 * Read the configuration and update it to the BGP-LS south bound protocol.
154 */
155 private void updateConfiguration() {
156 BgpCfg bgpConfig = null;
157 List<BgpAppConfig.BgpPeerConfig> nodes;
158 TreeMap<String, BgpPeerCfg> bgpPeerTree;
159 bgpConfig = bgpController.getConfig();
160 BgpPeerCfg peer = null;
161 BgpAppConfig config = configRegistry.getConfig(appId, BgpAppConfig.class);
162
163 if (config == null) {
164 log.warn("No configuration found");
165 return;
166 }
167
168
169 /* Update the self configuration */
170 if (bgpController.connectedPeerCount() == 0) {
171 bgpConfig.setRouterId(config.routerId());
172 bgpConfig.setAsNumber(config.localAs());
173 bgpConfig.setLsCapability(config.lsCapability());
174 bgpConfig.setHoldTime(config.holdTime());
175 bgpConfig.setMaxSession(config.maxSession());
176 bgpConfig.setLargeASCapability(config.largeAsCapability());
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530177
178 if (config.flowSpecCapability().equals("IPV4")) {
179 bgpConfig.setFlowSpecCapability(BgpCfg.FlowSpec.IPV4);
180 } else if (config.flowSpecCapability().equals("VPNV4")) {
181 bgpConfig.setFlowSpecCapability(BgpCfg.FlowSpec.VPNV4);
182 } else if (config.flowSpecCapability().equals("IPV4_VPNV4")) {
183 bgpConfig.setFlowSpecCapability(BgpCfg.FlowSpec.IPV4_VPNV4);
184 } else {
185 bgpConfig.setFlowSpecCapability(BgpCfg.FlowSpec.NONE);
186 }
Thejaswi N K5ff45df2015-12-15 17:05:29 +0530187 } else {
188 log.info(" Self configuration cannot be modified as there is existing connections ");
189 }
Shashikanth VHb650bfa2016-04-18 12:54:03 +0530190 bgpConfig.setFlowSpecRpdCapability(config.rpdCapability());
Thejaswi N K5ff45df2015-12-15 17:05:29 +0530191
192 /* update the peer configuration */
193 bgpPeerTree = bgpConfig.getPeerTree();
194 if (bgpPeerTree.isEmpty()) {
195 log.info("There are no BGP peers to iterate");
196 } else {
197 Set set = bgpPeerTree.entrySet();
198 Iterator i = set.iterator();
199 List<BgpPeerCfg> absPeerList = new ArrayList<BgpPeerCfg>();
200
201 boolean exists = false;
202
203 while (i.hasNext()) {
204 Map.Entry me = (Map.Entry) i.next();
205 peer = (BgpPeerCfg) me.getValue();
206
207 nodes = config.bgpPeer();
208 for (int j = 0; j < nodes.size(); j++) {
209 String peerIp = nodes.get(j).hostname();
210 if (peerIp.equals(peer.getPeerRouterId())) {
211
212 if (bgpConfig.isPeerConnectable(peer.getPeerRouterId())) {
213 peer.setAsNumber(nodes.get(j).asNumber());
214 peer.setHoldtime(nodes.get(j).holdTime());
215 log.debug("Peer neighbor IP successfully modified :" + peer.getPeerRouterId());
216 } else {
217 log.debug("Peer neighbor IP cannot be modified :" + peer.getPeerRouterId());
218 }
219
220 nodes.remove(j);
221 exists = true;
222 break;
223 }
224 }
225
226 if (!exists) {
227 absPeerList.add(peer);
228 exists = false;
229 }
230 }
231
232 /* Remove the absent nodes. */
233 for (int j = 0; j < absPeerList.size(); j++) {
234 bgpConfig.removePeer(absPeerList.get(j).getPeerRouterId());
235 }
236 }
237
238
239 nodes = config.bgpPeer();
240 for (int i = 0; i < nodes.size(); i++) {
241 String connectMode = nodes.get(i).connectMode();
242 bgpConfig.addPeer(nodes.get(i).hostname(), nodes.get(i).asNumber(), nodes.get(i).holdTime());
243 if (connectMode.equals(BgpAppConfig.PEER_CONNECT_ACTIVE)) {
244 bgpConfig.connectPeer(nodes.get(i).hostname());
245 }
246 }
247
248 }
249
250 /**
Thejaswi N K38879622015-12-08 22:14:47 +0530251 * BGP config listener to populate the configuration.
252 */
253 private class InternalConfigListener implements NetworkConfigListener {
254
255 @Override
256 public void event(NetworkConfigEvent event) {
257 if (!event.configClass().equals(BgpAppConfig.class)) {
258 return;
259 }
260
261 switch (event.type()) {
262 case CONFIG_ADDED:
263 readConfiguration();
264 break;
265 case CONFIG_UPDATED:
Thejaswi N K5ff45df2015-12-15 17:05:29 +0530266 updateConfiguration();
Thejaswi N K38879622015-12-08 22:14:47 +0530267 break;
268 case CONFIG_REMOVED:
269 default:
270 break;
271 }
272 }
273 }
274}