blob: 2ceb4c72baea53620d994e63340145bb0479ea3b [file] [log] [blame]
Thejaswi N K38879622015-12-08 22:14:47 +05301/*
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 */
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
131 nodes = config.bgpPeer();
132 for (int i = 0; i < nodes.size(); i++) {
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530133 String connectMode = nodes.get(i).connectMode();
Thejaswi N K38879622015-12-08 22:14:47 +0530134 bgpConfig.addPeer(nodes.get(i).hostname(), nodes.get(i).asNumber(), nodes.get(i).holdTime());
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530135 if (connectMode.equals(BgpAppConfig.PEER_CONNECT_ACTIVE)) {
136 bgpConfig.connectPeer(nodes.get(i).hostname());
137 }
Thejaswi N K38879622015-12-08 22:14:47 +0530138 }
139 }
140
141 /**
Thejaswi N K5ff45df2015-12-15 17:05:29 +0530142 * Read the configuration and update it to the BGP-LS south bound protocol.
143 */
144 private void updateConfiguration() {
145 BgpCfg bgpConfig = null;
146 List<BgpAppConfig.BgpPeerConfig> nodes;
147 TreeMap<String, BgpPeerCfg> bgpPeerTree;
148 bgpConfig = bgpController.getConfig();
149 BgpPeerCfg peer = null;
150 BgpAppConfig config = configRegistry.getConfig(appId, BgpAppConfig.class);
151
152 if (config == null) {
153 log.warn("No configuration found");
154 return;
155 }
156
157
158 /* Update the self configuration */
159 if (bgpController.connectedPeerCount() == 0) {
160 bgpConfig.setRouterId(config.routerId());
161 bgpConfig.setAsNumber(config.localAs());
162 bgpConfig.setLsCapability(config.lsCapability());
163 bgpConfig.setHoldTime(config.holdTime());
164 bgpConfig.setMaxSession(config.maxSession());
165 bgpConfig.setLargeASCapability(config.largeAsCapability());
166 } else {
167 log.info(" Self configuration cannot be modified as there is existing connections ");
168 }
169
170 /* update the peer configuration */
171 bgpPeerTree = bgpConfig.getPeerTree();
172 if (bgpPeerTree.isEmpty()) {
173 log.info("There are no BGP peers to iterate");
174 } else {
175 Set set = bgpPeerTree.entrySet();
176 Iterator i = set.iterator();
177 List<BgpPeerCfg> absPeerList = new ArrayList<BgpPeerCfg>();
178
179 boolean exists = false;
180
181 while (i.hasNext()) {
182 Map.Entry me = (Map.Entry) i.next();
183 peer = (BgpPeerCfg) me.getValue();
184
185 nodes = config.bgpPeer();
186 for (int j = 0; j < nodes.size(); j++) {
187 String peerIp = nodes.get(j).hostname();
188 if (peerIp.equals(peer.getPeerRouterId())) {
189
190 if (bgpConfig.isPeerConnectable(peer.getPeerRouterId())) {
191 peer.setAsNumber(nodes.get(j).asNumber());
192 peer.setHoldtime(nodes.get(j).holdTime());
193 log.debug("Peer neighbor IP successfully modified :" + peer.getPeerRouterId());
194 } else {
195 log.debug("Peer neighbor IP cannot be modified :" + peer.getPeerRouterId());
196 }
197
198 nodes.remove(j);
199 exists = true;
200 break;
201 }
202 }
203
204 if (!exists) {
205 absPeerList.add(peer);
206 exists = false;
207 }
208 }
209
210 /* Remove the absent nodes. */
211 for (int j = 0; j < absPeerList.size(); j++) {
212 bgpConfig.removePeer(absPeerList.get(j).getPeerRouterId());
213 }
214 }
215
216
217 nodes = config.bgpPeer();
218 for (int i = 0; i < nodes.size(); i++) {
219 String connectMode = nodes.get(i).connectMode();
220 bgpConfig.addPeer(nodes.get(i).hostname(), nodes.get(i).asNumber(), nodes.get(i).holdTime());
221 if (connectMode.equals(BgpAppConfig.PEER_CONNECT_ACTIVE)) {
222 bgpConfig.connectPeer(nodes.get(i).hostname());
223 }
224 }
225
226 }
227
228 /**
Thejaswi N K38879622015-12-08 22:14:47 +0530229 * BGP config listener to populate the configuration.
230 */
231 private class InternalConfigListener implements NetworkConfigListener {
232
233 @Override
234 public void event(NetworkConfigEvent event) {
235 if (!event.configClass().equals(BgpAppConfig.class)) {
236 return;
237 }
238
239 switch (event.type()) {
240 case CONFIG_ADDED:
241 readConfiguration();
242 break;
243 case CONFIG_UPDATED:
Thejaswi N K5ff45df2015-12-15 17:05:29 +0530244 updateConfiguration();
Thejaswi N K38879622015-12-08 22:14:47 +0530245 break;
246 case CONFIG_REMOVED:
247 default:
248 break;
249 }
250 }
251 }
252}