blob: 51ab68bef490e92df63079dff46e8a100019eedd [file] [log] [blame]
Satish Ke107e662015-09-21 19:00:17 +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 */
16
17package org.onosproject.bgp.controller.impl;
18
Shashikanth VH3fe37982015-11-30 11:50:07 +053019import java.util.Iterator;
20import java.util.List;
Shashikanth VHf62b5c02015-11-20 22:23:08 +053021import java.util.Set;
Shashikanth VH6de20d32015-10-09 12:04:13 +053022import java.util.concurrent.ConcurrentHashMap;
Shashikanth VHf62b5c02015-11-20 22:23:08 +053023import java.util.concurrent.CopyOnWriteArraySet;
Shashikanth VH6de20d32015-10-09 12:04:13 +053024import java.util.concurrent.locks.Lock;
25import java.util.concurrent.locks.ReentrantLock;
Shashikanth VH9f8afb42015-11-04 18:00:30 +053026
Satish Ke107e662015-09-21 19:00:17 +053027import org.apache.felix.scr.annotations.Activate;
28import org.apache.felix.scr.annotations.Component;
29import org.apache.felix.scr.annotations.Deactivate;
30import org.apache.felix.scr.annotations.Service;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053031import org.onosproject.bgp.controller.BgpCfg;
32import org.onosproject.bgp.controller.BgpController;
33import org.onosproject.bgp.controller.BgpId;
Shashikanth VH3fe37982015-11-30 11:50:07 +053034import org.onosproject.bgp.controller.BgpLocalRib;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053035import org.onosproject.bgp.controller.BgpPeer;
Shashikanth VHf62b5c02015-11-20 22:23:08 +053036import org.onosproject.bgp.controller.BgpNodeListener;
Shashikanth VH9f8afb42015-11-04 18:00:30 +053037import org.onosproject.bgp.controller.BgpPeerManager;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053038import org.onosproject.bgpio.exceptions.BgpParseException;
39import org.onosproject.bgpio.protocol.BgpMessage;
Shashikanth VH3fe37982015-11-30 11:50:07 +053040import org.onosproject.bgpio.protocol.BgpUpdateMsg;
41import org.onosproject.bgpio.types.BgpValueType;
42import org.onosproject.bgpio.types.MpReachNlri;
43import org.onosproject.bgpio.types.MpUnReachNlri;
Satish Ke107e662015-09-21 19:00:17 +053044import org.slf4j.Logger;
45import org.slf4j.LoggerFactory;
46
47@Component(immediate = true)
48@Service
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053049public class BgpControllerImpl implements BgpController {
Satish Ke107e662015-09-21 19:00:17 +053050
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053051 private static final Logger log = LoggerFactory.getLogger(BgpControllerImpl.class);
Satish Ke107e662015-09-21 19:00:17 +053052
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053053 protected ConcurrentHashMap<BgpId, BgpPeer> connectedPeers = new ConcurrentHashMap<BgpId, BgpPeer>();
Satish Ke107e662015-09-21 19:00:17 +053054
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053055 protected BgpPeerManagerImpl peerManager = new BgpPeerManagerImpl();
Shashikanth VHf62b5c02015-11-20 22:23:08 +053056
Shashikanth VH3fe37982015-11-30 11:50:07 +053057 private BgpLocalRib bgplocalRIB = new BgpLocalRibImpl(this);
58 private BgpLocalRib bgplocalRIBVpn = new BgpLocalRibImpl(this);
59
Shashikanth VHf62b5c02015-11-20 22:23:08 +053060 protected Set<BgpNodeListener> bgpNodeListener = new CopyOnWriteArraySet<>();
Shashikanth VHf62b5c02015-11-20 22:23:08 +053061
Satish Ke107e662015-09-21 19:00:17 +053062 final Controller ctrl = new Controller(this);
63
Shashikanth VH3fe37982015-11-30 11:50:07 +053064 private BgpConfig bgpconfig = new BgpConfig(this);
Satish Ke107e662015-09-21 19:00:17 +053065
66 @Activate
67 public void activate() {
68 this.ctrl.start();
69 log.info("Started");
70 }
71
72 @Deactivate
73 public void deactivate() {
74 // Close all connected peers
Shashikanth VH6de20d32015-10-09 12:04:13 +053075 closeConnectedPeers();
Satish Ke107e662015-09-21 19:00:17 +053076 this.ctrl.stop();
77 log.info("Stopped");
78 }
79
80 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053081 public Iterable<BgpPeer> getPeers() {
Shashikanth VH6de20d32015-10-09 12:04:13 +053082 return this.connectedPeers.values();
83 }
84
85 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053086 public BgpPeer getPeer(BgpId bgpId) {
Shashikanth VH6de20d32015-10-09 12:04:13 +053087 return this.connectedPeers.get(bgpId);
88 }
89
90 @Override
Shashikanth VHf62b5c02015-11-20 22:23:08 +053091 public void addListener(BgpNodeListener listener) {
92 this.bgpNodeListener.add(listener);
93 }
94
95 @Override
96 public void removeListener(BgpNodeListener listener) {
97 this.bgpNodeListener.remove(listener);
98 }
99
100 @Override
101 public Set<BgpNodeListener> listener() {
102 return bgpNodeListener;
103 }
104
105 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530106 public void writeMsg(BgpId bgpId, BgpMessage msg) {
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530107 this.getPeer(bgpId).sendMessage(msg);
Satish Ke107e662015-09-21 19:00:17 +0530108 }
109
110 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530111 public void processBGPPacket(BgpId bgpId, BgpMessage msg) throws BgpParseException {
Satish Ke107e662015-09-21 19:00:17 +0530112
Shashikanth VH3fe37982015-11-30 11:50:07 +0530113 BgpPeer peer = getPeer(bgpId);
114
Satish Ke107e662015-09-21 19:00:17 +0530115 switch (msg.getType()) {
116 case OPEN:
117 // TODO: Process Open message
118 break;
119 case KEEP_ALIVE:
120 // TODO: Process keepalive message
121 break;
122 case NOTIFICATION:
123 // TODO: Process notificatoin message
124 break;
125 case UPDATE:
Shashikanth VH3fe37982015-11-30 11:50:07 +0530126 BgpUpdateMsg updateMsg = (BgpUpdateMsg) msg;
127 List<BgpValueType> pathAttr = updateMsg.bgpPathAttributes().pathAttributes();
128 if (pathAttr == null) {
129 log.debug("llPathAttr is null, cannot process update message");
130 break;
131 }
132 Iterator<BgpValueType> listIterator = pathAttr.iterator();
133 boolean isLinkstate = false;
134 while (listIterator.hasNext()) {
135 BgpValueType attr = listIterator.next();
136 if ((attr instanceof MpReachNlri) || (attr instanceof MpUnReachNlri)) {
137 isLinkstate = true;
138 }
139 }
140 if (isLinkstate) {
141 peer.buildAdjRibIn(pathAttr);
142 }
Satish Ke107e662015-09-21 19:00:17 +0530143 break;
144 default:
145 // TODO: Process other message
146 break;
147 }
148 }
149
Shashikanth VH6de20d32015-10-09 12:04:13 +0530150 @Override
151 public void closeConnectedPeers() {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530152 BgpPeer bgpPeer;
153 for (BgpId id : this.connectedPeers.keySet()) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530154 bgpPeer = getPeer(id);
155 bgpPeer.disconnectPeer();
156 }
157 }
158
Satish Ke107e662015-09-21 19:00:17 +0530159 /**
Shashikanth VH6de20d32015-10-09 12:04:13 +0530160 * Implementation of an BGP Peer which is responsible for keeping track of connected peers and the state in which
161 * they are.
Satish Ke107e662015-09-21 19:00:17 +0530162 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530163 public class BgpPeerManagerImpl implements BgpPeerManager {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530164
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530165 private final Logger log = LoggerFactory.getLogger(BgpPeerManagerImpl.class);
Shashikanth VH6de20d32015-10-09 12:04:13 +0530166 private final Lock peerLock = new ReentrantLock();
167
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530168 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530169 public boolean addConnectedPeer(BgpId bgpId, BgpPeer bgpPeer) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530170
171 if (connectedPeers.get(bgpId) != null) {
172 this.log.error("Trying to add connectedPeer but found previous " + "value for bgp ip: {}",
173 bgpId.toString());
174 return false;
175 } else {
176 this.log.debug("Added Peer {}", bgpId.toString());
177 connectedPeers.put(bgpId, bgpPeer);
178 return true;
179 }
180 }
181
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530182 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530183 public boolean isPeerConnected(BgpId bgpId) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530184 if (connectedPeers.get(bgpId) == null) {
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530185 this.log.error("Is peer connected: bgpIp {}.", bgpId.toString());
Shashikanth VH6de20d32015-10-09 12:04:13 +0530186 return false;
187 }
188
189 return true;
190 }
191
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530192 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530193 public void removeConnectedPeer(BgpId bgpId) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530194 connectedPeers.remove(bgpId);
195 }
196
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530197 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530198 public BgpPeer getPeer(BgpId bgpId) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530199 return connectedPeers.get(bgpId);
200 }
201
202 /**
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530203 * Gets bgp peer instance.
204 *
205 * @param bgpController controller instance.
206 * @param sessionInfo bgp session info.
207 * @param pktStats packet statistics.
208 * @return BGPPeer peer instance.
209 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530210 public BgpPeer getBgpPeerInstance(BgpController bgpController, BgpSessionInfoImpl sessionInfo,
211 BgpPacketStatsImpl pktStats) {
212 BgpPeer bgpPeer = new BgpPeerImpl(bgpController, sessionInfo, pktStats);
Shashikanth VH6de20d32015-10-09 12:04:13 +0530213 return bgpPeer;
214 }
215
216 }
217
Vidyashree Rama7bd3d782015-11-23 08:46:33 +0530218 /**
219 * Returns controller.
220 *
221 * @return controller
222 */
223 public Controller controller() {
224 return this.ctrl;
225 }
226
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530227 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530228 public ConcurrentHashMap<BgpId, BgpPeer> connectedPeers() {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530229 return connectedPeers;
230 }
231
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530232 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530233 public BgpPeerManagerImpl peerManager() {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530234 return peerManager;
235 }
236
Satish Ke107e662015-09-21 19:00:17 +0530237 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530238 public BgpCfg getConfig() {
Satish Ke107e662015-09-21 19:00:17 +0530239 return this.bgpconfig;
240 }
Shashikanth VH6de20d32015-10-09 12:04:13 +0530241
242 @Override
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530243 public int connectedPeerCount() {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530244 return connectedPeers.size();
245 }
Shashikanth VH3fe37982015-11-30 11:50:07 +0530246
247 /**
248 * Gets the BGP local RIB.
249 *
250 * @return bgplocalRIB BGP local RIB.
251 */
252 @Override
253 public BgpLocalRib bgpLocalRib() {
254 return bgplocalRIB;
255 }
256
257 /**
258 * Gets the BGP local RIB with VPN.
259 *
260 * @return bgplocalRIBVpn BGP VPN local RIB .
261 */
262 @Override
263 public BgpLocalRib bgpLocalRibVpn() {
264 return bgplocalRIBVpn;
265 }
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530266}