blob: 83c06482b9e64a28d69a6faf2d59bfc7a70ecdd3 [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 VH26fd38a2016-04-26 18:11:37 +0530136
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;
Shashikanth VH26fd38a2016-04-26 18:11:37 +0530141 if (mpReach.bgpFlowSpecNlri() == null) {
Shashikanth VHafb2e002016-02-12 14:48:29 +0530142 isLinkstate = true;
143 }
144 } else if (attr instanceof MpUnReachNlri) {
145 MpUnReachNlri mpUnReach = (MpUnReachNlri) attr;
Shashikanth VH26fd38a2016-04-26 18:11:37 +0530146 if (mpUnReach.bgpFlowSpecNlri() == null) {
Shashikanth VHafb2e002016-02-12 14:48:29 +0530147 isLinkstate = true;
148 }
Shashikanth VH3fe37982015-11-30 11:50:07 +0530149 }
150 }
151 if (isLinkstate) {
152 peer.buildAdjRibIn(pathAttr);
153 }
Satish Ke107e662015-09-21 19:00:17 +0530154 break;
155 default:
156 // TODO: Process other message
157 break;
158 }
159 }
160
Shashikanth VH6de20d32015-10-09 12:04:13 +0530161 @Override
162 public void closeConnectedPeers() {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530163 BgpPeer bgpPeer;
164 for (BgpId id : this.connectedPeers.keySet()) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530165 bgpPeer = getPeer(id);
166 bgpPeer.disconnectPeer();
167 }
168 }
169
Satish Ke107e662015-09-21 19:00:17 +0530170 /**
Shashikanth VH6de20d32015-10-09 12:04:13 +0530171 * Implementation of an BGP Peer which is responsible for keeping track of connected peers and the state in which
172 * they are.
Satish Ke107e662015-09-21 19:00:17 +0530173 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530174 public class BgpPeerManagerImpl implements BgpPeerManager {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530175
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530176 private final Logger log = LoggerFactory.getLogger(BgpPeerManagerImpl.class);
Shashikanth VH6de20d32015-10-09 12:04:13 +0530177 private final Lock peerLock = new ReentrantLock();
178
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530179 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530180 public boolean addConnectedPeer(BgpId bgpId, BgpPeer bgpPeer) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530181
182 if (connectedPeers.get(bgpId) != null) {
183 this.log.error("Trying to add connectedPeer but found previous " + "value for bgp ip: {}",
184 bgpId.toString());
185 return false;
186 } else {
187 this.log.debug("Added Peer {}", bgpId.toString());
188 connectedPeers.put(bgpId, bgpPeer);
189 return true;
190 }
191 }
192
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530193 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530194 public boolean isPeerConnected(BgpId bgpId) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530195 if (connectedPeers.get(bgpId) == null) {
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530196 this.log.error("Is peer connected: bgpIp {}.", bgpId.toString());
Shashikanth VH6de20d32015-10-09 12:04:13 +0530197 return false;
198 }
199
200 return true;
201 }
202
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530203 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530204 public void removeConnectedPeer(BgpId bgpId) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530205 connectedPeers.remove(bgpId);
206 }
207
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530208 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530209 public BgpPeer getPeer(BgpId bgpId) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530210 return connectedPeers.get(bgpId);
211 }
212
213 /**
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530214 * Gets bgp peer instance.
215 *
216 * @param bgpController controller instance.
217 * @param sessionInfo bgp session info.
218 * @param pktStats packet statistics.
219 * @return BGPPeer peer instance.
220 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530221 public BgpPeer getBgpPeerInstance(BgpController bgpController, BgpSessionInfoImpl sessionInfo,
222 BgpPacketStatsImpl pktStats) {
223 BgpPeer bgpPeer = new BgpPeerImpl(bgpController, sessionInfo, pktStats);
Shashikanth VH6de20d32015-10-09 12:04:13 +0530224 return bgpPeer;
225 }
226
227 }
228
Vidyashree Rama7bd3d782015-11-23 08:46:33 +0530229 /**
230 * Returns controller.
231 *
232 * @return controller
233 */
234 public Controller controller() {
235 return this.ctrl;
236 }
237
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530238 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530239 public ConcurrentHashMap<BgpId, BgpPeer> connectedPeers() {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530240 return connectedPeers;
241 }
242
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530243 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530244 public BgpPeerManagerImpl peerManager() {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530245 return peerManager;
246 }
247
Satish Ke107e662015-09-21 19:00:17 +0530248 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530249 public BgpCfg getConfig() {
Satish Ke107e662015-09-21 19:00:17 +0530250 return this.bgpconfig;
251 }
Shashikanth VH6de20d32015-10-09 12:04:13 +0530252
253 @Override
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530254 public int connectedPeerCount() {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530255 return connectedPeers.size();
256 }
Shashikanth VH3fe37982015-11-30 11:50:07 +0530257
258 /**
259 * Gets the BGP local RIB.
260 *
261 * @return bgplocalRIB BGP local RIB.
262 */
263 @Override
264 public BgpLocalRib bgpLocalRib() {
Jonathan Hart51539b82015-10-29 09:53:04 -0700265 return bgplocalRib;
Shashikanth VH3fe37982015-11-30 11:50:07 +0530266 }
267
268 /**
269 * Gets the BGP local RIB with VPN.
270 *
271 * @return bgplocalRIBVpn BGP VPN local RIB .
272 */
273 @Override
274 public BgpLocalRib bgpLocalRibVpn() {
Jonathan Hart51539b82015-10-29 09:53:04 -0700275 return bgplocalRibVpn;
Shashikanth VH3fe37982015-11-30 11:50:07 +0530276 }
Priyanka Bfc51c952016-03-26 14:30:33 +0530277
278 @Override
279 public void addLinkListener(BgpLinkListener listener) {
280 this.bgpLinkListener.add(listener);
281 }
282
283 @Override
284 public void removeLinkListener(BgpLinkListener listener) {
285 this.bgpLinkListener.remove(listener);
286 }
287
288 @Override
289 public Set<BgpLinkListener> linkListener() {
290 return bgpLinkListener;
291 }
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530292}