blob: 85087fb7fd72320c1dd6a5df51b2a7e9cc6f8d21 [file] [log] [blame]
Satish Ke107e662015-09-21 19:00:17 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Satish Ke107e662015-09-21 19:00:17 +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 */
16
17package org.onosproject.bgp.controller.impl;
18
Satish Ke107e662015-09-21 19:00:17 +053019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Service;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053023import org.onosproject.bgp.controller.BgpCfg;
24import org.onosproject.bgp.controller.BgpController;
25import org.onosproject.bgp.controller.BgpId;
Priyanka Bfc51c952016-03-26 14:30:33 +053026import org.onosproject.bgp.controller.BgpLinkListener;
Shashikanth VH3fe37982015-11-30 11:50:07 +053027import org.onosproject.bgp.controller.BgpLocalRib;
Shashikanth VHf62b5c02015-11-20 22:23:08 +053028import org.onosproject.bgp.controller.BgpNodeListener;
Jonathan Hart51539b82015-10-29 09:53:04 -070029import org.onosproject.bgp.controller.BgpPeer;
Shashikanth VH9f8afb42015-11-04 18:00:30 +053030import org.onosproject.bgp.controller.BgpPeerManager;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053031import org.onosproject.bgpio.exceptions.BgpParseException;
32import org.onosproject.bgpio.protocol.BgpMessage;
Shashikanth VH3fe37982015-11-30 11:50:07 +053033import org.onosproject.bgpio.protocol.BgpUpdateMsg;
34import org.onosproject.bgpio.types.BgpValueType;
35import org.onosproject.bgpio.types.MpReachNlri;
36import org.onosproject.bgpio.types.MpUnReachNlri;
Satish Ke107e662015-09-21 19:00:17 +053037import org.slf4j.Logger;
38import org.slf4j.LoggerFactory;
39
Jonathan Hart51539b82015-10-29 09:53:04 -070040import java.util.Iterator;
41import java.util.List;
42import java.util.Set;
43import java.util.concurrent.ConcurrentHashMap;
44import java.util.concurrent.CopyOnWriteArraySet;
45import java.util.concurrent.locks.Lock;
46import java.util.concurrent.locks.ReentrantLock;
47
Satish Ke107e662015-09-21 19:00:17 +053048@Component(immediate = true)
49@Service
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053050public class BgpControllerImpl implements BgpController {
Satish Ke107e662015-09-21 19:00:17 +053051
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053052 private static final Logger log = LoggerFactory.getLogger(BgpControllerImpl.class);
Satish Ke107e662015-09-21 19:00:17 +053053
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053054 protected ConcurrentHashMap<BgpId, BgpPeer> connectedPeers = new ConcurrentHashMap<BgpId, BgpPeer>();
Satish Ke107e662015-09-21 19:00:17 +053055
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053056 protected BgpPeerManagerImpl peerManager = new BgpPeerManagerImpl();
Shashikanth VHf62b5c02015-11-20 22:23:08 +053057
Jonathan Hart51539b82015-10-29 09:53:04 -070058 private BgpLocalRib bgplocalRib = new BgpLocalRibImpl(this);
59 private BgpLocalRib bgplocalRibVpn = new BgpLocalRibImpl(this);
Shashikanth VH3fe37982015-11-30 11:50:07 +053060
Shashikanth VHf62b5c02015-11-20 22:23:08 +053061 protected Set<BgpNodeListener> bgpNodeListener = new CopyOnWriteArraySet<>();
Priyanka Bfc51c952016-03-26 14:30:33 +053062 protected Set<BgpLinkListener> bgpLinkListener = new CopyOnWriteArraySet<>();
Shashikanth VHf62b5c02015-11-20 22:23:08 +053063
Satish Ke107e662015-09-21 19:00:17 +053064 final Controller ctrl = new Controller(this);
65
Shashikanth VH3fe37982015-11-30 11:50:07 +053066 private BgpConfig bgpconfig = new BgpConfig(this);
Satish Ke107e662015-09-21 19:00:17 +053067
68 @Activate
69 public void activate() {
70 this.ctrl.start();
71 log.info("Started");
72 }
73
74 @Deactivate
75 public void deactivate() {
76 // Close all connected peers
Shashikanth VH6de20d32015-10-09 12:04:13 +053077 closeConnectedPeers();
Satish Ke107e662015-09-21 19:00:17 +053078 this.ctrl.stop();
79 log.info("Stopped");
80 }
81
82 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053083 public Iterable<BgpPeer> getPeers() {
Shashikanth VH6de20d32015-10-09 12:04:13 +053084 return this.connectedPeers.values();
85 }
86
87 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053088 public BgpPeer getPeer(BgpId bgpId) {
Shashikanth VH6de20d32015-10-09 12:04:13 +053089 return this.connectedPeers.get(bgpId);
90 }
91
92 @Override
Shashikanth VHf62b5c02015-11-20 22:23:08 +053093 public void addListener(BgpNodeListener listener) {
94 this.bgpNodeListener.add(listener);
95 }
96
97 @Override
98 public void removeListener(BgpNodeListener listener) {
99 this.bgpNodeListener.remove(listener);
100 }
101
102 @Override
103 public Set<BgpNodeListener> listener() {
104 return bgpNodeListener;
105 }
106
107 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530108 public void writeMsg(BgpId bgpId, BgpMessage msg) {
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530109 this.getPeer(bgpId).sendMessage(msg);
Satish Ke107e662015-09-21 19:00:17 +0530110 }
111
112 @Override
Jonathan Hart51539b82015-10-29 09:53:04 -0700113 public void processBgpPacket(BgpId bgpId, BgpMessage msg) throws BgpParseException {
Satish Ke107e662015-09-21 19:00:17 +0530114
Shashikanth VH3fe37982015-11-30 11:50:07 +0530115 BgpPeer peer = getPeer(bgpId);
116
Satish Ke107e662015-09-21 19:00:17 +0530117 switch (msg.getType()) {
118 case OPEN:
119 // TODO: Process Open message
120 break;
121 case KEEP_ALIVE:
122 // TODO: Process keepalive message
123 break;
124 case NOTIFICATION:
125 // TODO: Process notificatoin message
126 break;
127 case UPDATE:
Shashikanth VH3fe37982015-11-30 11:50:07 +0530128 BgpUpdateMsg updateMsg = (BgpUpdateMsg) msg;
129 List<BgpValueType> pathAttr = updateMsg.bgpPathAttributes().pathAttributes();
130 if (pathAttr == null) {
131 log.debug("llPathAttr is null, cannot process update message");
132 break;
133 }
134 Iterator<BgpValueType> listIterator = pathAttr.iterator();
135 boolean isLinkstate = false;
Shashikanth VHe3a73bc2016-02-22 20:11:31 +0530136 boolean isFlowSpec = false;
Shashikanth VH3fe37982015-11-30 11:50:07 +0530137 while (listIterator.hasNext()) {
138 BgpValueType attr = listIterator.next();
Shashikanth VHafb2e002016-02-12 14:48:29 +0530139 if (attr instanceof MpReachNlri) {
140 MpReachNlri mpReach = (MpReachNlri) attr;
141 if (mpReach.bgpFlowSpecInfo() == null) {
142 isLinkstate = true;
Shashikanth VHe3a73bc2016-02-22 20:11:31 +0530143 } else {
144 isFlowSpec = true;
Shashikanth VHafb2e002016-02-12 14:48:29 +0530145 }
146 } else if (attr instanceof MpUnReachNlri) {
147 MpUnReachNlri mpUnReach = (MpUnReachNlri) attr;
148 if (mpUnReach.bgpFlowSpecInfo() == null) {
149 isLinkstate = true;
Shashikanth VHe3a73bc2016-02-22 20:11:31 +0530150 } else {
151 isFlowSpec = true;
Shashikanth VHafb2e002016-02-12 14:48:29 +0530152 }
Shashikanth VH3fe37982015-11-30 11:50:07 +0530153 }
154 }
155 if (isLinkstate) {
156 peer.buildAdjRibIn(pathAttr);
Shashikanth VHe3a73bc2016-02-22 20:11:31 +0530157 } else if (isFlowSpec) {
158 peer.buildFlowSpecRib(pathAttr);
Shashikanth VH3fe37982015-11-30 11:50:07 +0530159 }
Satish Ke107e662015-09-21 19:00:17 +0530160 break;
161 default:
162 // TODO: Process other message
163 break;
164 }
165 }
166
Shashikanth VH6de20d32015-10-09 12:04:13 +0530167 @Override
168 public void closeConnectedPeers() {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530169 BgpPeer bgpPeer;
170 for (BgpId id : this.connectedPeers.keySet()) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530171 bgpPeer = getPeer(id);
172 bgpPeer.disconnectPeer();
173 }
174 }
175
Satish Ke107e662015-09-21 19:00:17 +0530176 /**
Shashikanth VH6de20d32015-10-09 12:04:13 +0530177 * Implementation of an BGP Peer which is responsible for keeping track of connected peers and the state in which
178 * they are.
Satish Ke107e662015-09-21 19:00:17 +0530179 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530180 public class BgpPeerManagerImpl implements BgpPeerManager {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530181
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530182 private final Logger log = LoggerFactory.getLogger(BgpPeerManagerImpl.class);
Shashikanth VH6de20d32015-10-09 12:04:13 +0530183 private final Lock peerLock = new ReentrantLock();
184
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530185 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530186 public boolean addConnectedPeer(BgpId bgpId, BgpPeer bgpPeer) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530187
188 if (connectedPeers.get(bgpId) != null) {
189 this.log.error("Trying to add connectedPeer but found previous " + "value for bgp ip: {}",
190 bgpId.toString());
191 return false;
192 } else {
193 this.log.debug("Added Peer {}", bgpId.toString());
194 connectedPeers.put(bgpId, bgpPeer);
195 return true;
196 }
197 }
198
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530199 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530200 public boolean isPeerConnected(BgpId bgpId) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530201 if (connectedPeers.get(bgpId) == null) {
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530202 this.log.error("Is peer connected: bgpIp {}.", bgpId.toString());
Shashikanth VH6de20d32015-10-09 12:04:13 +0530203 return false;
204 }
205
206 return true;
207 }
208
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530209 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530210 public void removeConnectedPeer(BgpId bgpId) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530211 connectedPeers.remove(bgpId);
212 }
213
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530214 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530215 public BgpPeer getPeer(BgpId bgpId) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530216 return connectedPeers.get(bgpId);
217 }
218
219 /**
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530220 * Gets bgp peer instance.
221 *
222 * @param bgpController controller instance.
223 * @param sessionInfo bgp session info.
224 * @param pktStats packet statistics.
225 * @return BGPPeer peer instance.
226 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530227 public BgpPeer getBgpPeerInstance(BgpController bgpController, BgpSessionInfoImpl sessionInfo,
228 BgpPacketStatsImpl pktStats) {
229 BgpPeer bgpPeer = new BgpPeerImpl(bgpController, sessionInfo, pktStats);
Shashikanth VH6de20d32015-10-09 12:04:13 +0530230 return bgpPeer;
231 }
232
233 }
234
Vidyashree Rama7bd3d782015-11-23 08:46:33 +0530235 /**
236 * Returns controller.
237 *
238 * @return controller
239 */
240 public Controller controller() {
241 return this.ctrl;
242 }
243
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530244 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530245 public ConcurrentHashMap<BgpId, BgpPeer> connectedPeers() {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530246 return connectedPeers;
247 }
248
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530249 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530250 public BgpPeerManagerImpl peerManager() {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530251 return peerManager;
252 }
253
Satish Ke107e662015-09-21 19:00:17 +0530254 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530255 public BgpCfg getConfig() {
Satish Ke107e662015-09-21 19:00:17 +0530256 return this.bgpconfig;
257 }
Shashikanth VH6de20d32015-10-09 12:04:13 +0530258
259 @Override
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530260 public int connectedPeerCount() {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530261 return connectedPeers.size();
262 }
Shashikanth VH3fe37982015-11-30 11:50:07 +0530263
264 /**
265 * Gets the BGP local RIB.
266 *
267 * @return bgplocalRIB BGP local RIB.
268 */
269 @Override
270 public BgpLocalRib bgpLocalRib() {
Jonathan Hart51539b82015-10-29 09:53:04 -0700271 return bgplocalRib;
Shashikanth VH3fe37982015-11-30 11:50:07 +0530272 }
273
274 /**
275 * Gets the BGP local RIB with VPN.
276 *
277 * @return bgplocalRIBVpn BGP VPN local RIB .
278 */
279 @Override
280 public BgpLocalRib bgpLocalRibVpn() {
Jonathan Hart51539b82015-10-29 09:53:04 -0700281 return bgplocalRibVpn;
Shashikanth VH3fe37982015-11-30 11:50:07 +0530282 }
Priyanka Bfc51c952016-03-26 14:30:33 +0530283
284 @Override
285 public void addLinkListener(BgpLinkListener listener) {
286 this.bgpLinkListener.add(listener);
287 }
288
289 @Override
290 public void removeLinkListener(BgpLinkListener listener) {
291 this.bgpLinkListener.remove(listener);
292 }
293
294 @Override
295 public Set<BgpLinkListener> linkListener() {
296 return bgpLinkListener;
297 }
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530298}