blob: 15a5b58894e8225624d0eabd296197a0233aa7e7 [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;
mohamedrahil00f6f262016-11-24 20:20:41 +053041import java.util.LinkedList;
42import java.util.Map;
43import java.util.TreeMap;
Jonathan Hart51539b82015-10-29 09:53:04 -070044import java.util.List;
45import java.util.Set;
46import java.util.concurrent.ConcurrentHashMap;
47import java.util.concurrent.CopyOnWriteArraySet;
48import java.util.concurrent.locks.Lock;
49import java.util.concurrent.locks.ReentrantLock;
50
Satish Ke107e662015-09-21 19:00:17 +053051@Component(immediate = true)
52@Service
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053053public class BgpControllerImpl implements BgpController {
Satish Ke107e662015-09-21 19:00:17 +053054
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053055 private static final Logger log = LoggerFactory.getLogger(BgpControllerImpl.class);
mohamedrahil00f6f262016-11-24 20:20:41 +053056 final Controller ctrl = new Controller(this);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053057 protected ConcurrentHashMap<BgpId, BgpPeer> connectedPeers = new ConcurrentHashMap<BgpId, BgpPeer>();
Satish Ke107e662015-09-21 19:00:17 +053058
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053059 protected BgpPeerManagerImpl peerManager = new BgpPeerManagerImpl();
Shashikanth VHf62b5c02015-11-20 22:23:08 +053060
Jonathan Hart51539b82015-10-29 09:53:04 -070061 private BgpLocalRib bgplocalRib = new BgpLocalRibImpl(this);
62 private BgpLocalRib bgplocalRibVpn = new BgpLocalRibImpl(this);
Shashikanth VH3fe37982015-11-30 11:50:07 +053063
Shashikanth VHf62b5c02015-11-20 22:23:08 +053064 protected Set<BgpNodeListener> bgpNodeListener = new CopyOnWriteArraySet<>();
Priyanka Bfc51c952016-03-26 14:30:33 +053065 protected Set<BgpLinkListener> bgpLinkListener = new CopyOnWriteArraySet<>();
mohamedrahil00f6f262016-11-24 20:20:41 +053066 protected BgpController bgpController;
Shashikanth VH3fe37982015-11-30 11:50:07 +053067 private BgpConfig bgpconfig = new BgpConfig(this);
mohamedrahil00f6f262016-11-24 20:20:41 +053068 private List<String> activeExceptionList = new LinkedList();
69 private LinkedList<String> closedExceptionList = new LinkedList<String>();
70 private Map<String, List<String>> activeSessionExceptionMap = new TreeMap<>();
71 private Map<String, List<String>> closedSessionExceptionMap = new TreeMap<>();
72
73 @Override
74 public void activeSessionExceptionAdd(String peerId, String exception) {
75 if (peerId != null) {
76 activeExceptionList.add(exception);
77 activeSessionExceptionMap.put(peerId, activeExceptionList);
78 } else {
79 log.debug("Peer Id is null");
80 }
81 if (activeExceptionList.size() > 10) {
82 activeExceptionList.clear();
83 activeExceptionList.add(exception);
84 activeSessionExceptionMap.put(peerId, activeExceptionList);
85 }
86 }
87
88
89 @Override
90 public void closedSessionExceptionAdd(String peerId, String exception) {
91 if (peerId != null) {
92 closedExceptionList.add(exception);
93 closedSessionExceptionMap.put(peerId, closedExceptionList);
94 } else {
95 log.debug("Peer Id is null");
96 }
97 if (closedExceptionList.size() > 10) {
98 closedExceptionList.clear();
99 closedExceptionList.add(exception);
100 closedSessionExceptionMap.put(peerId, closedExceptionList);
101 }
102 }
103
104 @Override
105 public Map<String, List<String>> activeSessionMap() {
106 return activeSessionExceptionMap;
107 }
108
109 @Override
110 public Map<String, List<String>> closedSessionMap() {
111 return closedSessionExceptionMap;
112 }
Satish Ke107e662015-09-21 19:00:17 +0530113
114 @Activate
115 public void activate() {
116 this.ctrl.start();
117 log.info("Started");
118 }
119
120 @Deactivate
121 public void deactivate() {
mohamedrahil00f6f262016-11-24 20:20:41 +0530122 activeSessionExceptionMap.clear();
123 closedSessionExceptionMap.clear();
Satish Ke107e662015-09-21 19:00:17 +0530124 // Close all connected peers
Shashikanth VH6de20d32015-10-09 12:04:13 +0530125 closeConnectedPeers();
Satish Ke107e662015-09-21 19:00:17 +0530126 this.ctrl.stop();
127 log.info("Stopped");
128 }
129
130 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530131 public Iterable<BgpPeer> getPeers() {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530132 return this.connectedPeers.values();
133 }
134
135 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530136 public BgpPeer getPeer(BgpId bgpId) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530137 return this.connectedPeers.get(bgpId);
138 }
139
140 @Override
Shashikanth VHf62b5c02015-11-20 22:23:08 +0530141 public void addListener(BgpNodeListener listener) {
142 this.bgpNodeListener.add(listener);
143 }
144
145 @Override
146 public void removeListener(BgpNodeListener listener) {
147 this.bgpNodeListener.remove(listener);
148 }
149
150 @Override
151 public Set<BgpNodeListener> listener() {
152 return bgpNodeListener;
153 }
154
155 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530156 public void writeMsg(BgpId bgpId, BgpMessage msg) {
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530157 this.getPeer(bgpId).sendMessage(msg);
Satish Ke107e662015-09-21 19:00:17 +0530158 }
159
160 @Override
Jonathan Hart51539b82015-10-29 09:53:04 -0700161 public void processBgpPacket(BgpId bgpId, BgpMessage msg) throws BgpParseException {
Satish Ke107e662015-09-21 19:00:17 +0530162
Shashikanth VH3fe37982015-11-30 11:50:07 +0530163 BgpPeer peer = getPeer(bgpId);
164
Satish Ke107e662015-09-21 19:00:17 +0530165 switch (msg.getType()) {
166 case OPEN:
167 // TODO: Process Open message
168 break;
169 case KEEP_ALIVE:
170 // TODO: Process keepalive message
171 break;
172 case NOTIFICATION:
173 // TODO: Process notificatoin message
174 break;
175 case UPDATE:
Shashikanth VH3fe37982015-11-30 11:50:07 +0530176 BgpUpdateMsg updateMsg = (BgpUpdateMsg) msg;
177 List<BgpValueType> pathAttr = updateMsg.bgpPathAttributes().pathAttributes();
178 if (pathAttr == null) {
179 log.debug("llPathAttr is null, cannot process update message");
180 break;
181 }
182 Iterator<BgpValueType> listIterator = pathAttr.iterator();
183 boolean isLinkstate = false;
Shashikanth VH26fd38a2016-04-26 18:11:37 +0530184
Shashikanth VH3fe37982015-11-30 11:50:07 +0530185 while (listIterator.hasNext()) {
186 BgpValueType attr = listIterator.next();
Shashikanth VHafb2e002016-02-12 14:48:29 +0530187 if (attr instanceof MpReachNlri) {
188 MpReachNlri mpReach = (MpReachNlri) attr;
Shashikanth VH26fd38a2016-04-26 18:11:37 +0530189 if (mpReach.bgpFlowSpecNlri() == null) {
Shashikanth VHafb2e002016-02-12 14:48:29 +0530190 isLinkstate = true;
191 }
192 } else if (attr instanceof MpUnReachNlri) {
193 MpUnReachNlri mpUnReach = (MpUnReachNlri) attr;
Shashikanth VH26fd38a2016-04-26 18:11:37 +0530194 if (mpUnReach.bgpFlowSpecNlri() == null) {
Shashikanth VHafb2e002016-02-12 14:48:29 +0530195 isLinkstate = true;
196 }
Shashikanth VH3fe37982015-11-30 11:50:07 +0530197 }
198 }
199 if (isLinkstate) {
200 peer.buildAdjRibIn(pathAttr);
201 }
Satish Ke107e662015-09-21 19:00:17 +0530202 break;
203 default:
204 // TODO: Process other message
205 break;
206 }
207 }
208
Shashikanth VH6de20d32015-10-09 12:04:13 +0530209 @Override
210 public void closeConnectedPeers() {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530211 BgpPeer bgpPeer;
212 for (BgpId id : this.connectedPeers.keySet()) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530213 bgpPeer = getPeer(id);
214 bgpPeer.disconnectPeer();
215 }
216 }
217
Satish Ke107e662015-09-21 19:00:17 +0530218 /**
Shashikanth VH6de20d32015-10-09 12:04:13 +0530219 * Implementation of an BGP Peer which is responsible for keeping track of connected peers and the state in which
220 * they are.
Satish Ke107e662015-09-21 19:00:17 +0530221 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530222 public class BgpPeerManagerImpl implements BgpPeerManager {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530223
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530224 private final Logger log = LoggerFactory.getLogger(BgpPeerManagerImpl.class);
Shashikanth VH6de20d32015-10-09 12:04:13 +0530225 private final Lock peerLock = new ReentrantLock();
226
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530227 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530228 public boolean addConnectedPeer(BgpId bgpId, BgpPeer bgpPeer) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530229
230 if (connectedPeers.get(bgpId) != null) {
231 this.log.error("Trying to add connectedPeer but found previous " + "value for bgp ip: {}",
232 bgpId.toString());
233 return false;
234 } else {
235 this.log.debug("Added Peer {}", bgpId.toString());
236 connectedPeers.put(bgpId, bgpPeer);
237 return true;
238 }
239 }
240
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530241 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530242 public boolean isPeerConnected(BgpId bgpId) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530243 if (connectedPeers.get(bgpId) == null) {
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530244 this.log.error("Is peer connected: bgpIp {}.", bgpId.toString());
Shashikanth VH6de20d32015-10-09 12:04:13 +0530245 return false;
246 }
247
248 return true;
249 }
250
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530251 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530252 public void removeConnectedPeer(BgpId bgpId) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530253 connectedPeers.remove(bgpId);
254 }
255
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530256 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530257 public BgpPeer getPeer(BgpId bgpId) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530258 return connectedPeers.get(bgpId);
259 }
260
261 /**
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530262 * Gets bgp peer instance.
263 *
264 * @param bgpController controller instance.
265 * @param sessionInfo bgp session info.
266 * @param pktStats packet statistics.
267 * @return BGPPeer peer instance.
268 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530269 public BgpPeer getBgpPeerInstance(BgpController bgpController, BgpSessionInfoImpl sessionInfo,
270 BgpPacketStatsImpl pktStats) {
271 BgpPeer bgpPeer = new BgpPeerImpl(bgpController, sessionInfo, pktStats);
Shashikanth VH6de20d32015-10-09 12:04:13 +0530272 return bgpPeer;
273 }
274
275 }
276
Vidyashree Rama7bd3d782015-11-23 08:46:33 +0530277 /**
278 * Returns controller.
279 *
280 * @return controller
281 */
282 public Controller controller() {
283 return this.ctrl;
284 }
285
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530286 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530287 public ConcurrentHashMap<BgpId, BgpPeer> connectedPeers() {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530288 return connectedPeers;
289 }
290
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530291 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530292 public BgpPeerManagerImpl peerManager() {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530293 return peerManager;
294 }
295
Satish Ke107e662015-09-21 19:00:17 +0530296 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530297 public BgpCfg getConfig() {
Satish Ke107e662015-09-21 19:00:17 +0530298 return this.bgpconfig;
299 }
Shashikanth VH6de20d32015-10-09 12:04:13 +0530300
301 @Override
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530302 public int connectedPeerCount() {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530303 return connectedPeers.size();
304 }
Shashikanth VH3fe37982015-11-30 11:50:07 +0530305
306 /**
307 * Gets the BGP local RIB.
308 *
309 * @return bgplocalRIB BGP local RIB.
310 */
311 @Override
312 public BgpLocalRib bgpLocalRib() {
Jonathan Hart51539b82015-10-29 09:53:04 -0700313 return bgplocalRib;
Shashikanth VH3fe37982015-11-30 11:50:07 +0530314 }
315
316 /**
317 * Gets the BGP local RIB with VPN.
318 *
319 * @return bgplocalRIBVpn BGP VPN local RIB .
320 */
321 @Override
322 public BgpLocalRib bgpLocalRibVpn() {
Jonathan Hart51539b82015-10-29 09:53:04 -0700323 return bgplocalRibVpn;
Shashikanth VH3fe37982015-11-30 11:50:07 +0530324 }
Priyanka Bfc51c952016-03-26 14:30:33 +0530325
326 @Override
327 public void addLinkListener(BgpLinkListener listener) {
328 this.bgpLinkListener.add(listener);
329 }
330
331 @Override
332 public void removeLinkListener(BgpLinkListener listener) {
333 this.bgpLinkListener.remove(listener);
334 }
335
336 @Override
337 public Set<BgpLinkListener> linkListener() {
338 return bgpLinkListener;
339 }
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530340}