blob: d8378e3174b6f9e919a31dc9099d6f35cc4fd661 [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
19import static org.onlab.util.Tools.groupedThreads;
Shashikanth VH6de20d32015-10-09 12:04:13 +053020
21import java.util.concurrent.ConcurrentHashMap;
Satish Ke107e662015-09-21 19:00:17 +053022import java.util.concurrent.ExecutorService;
23import java.util.concurrent.Executors;
Shashikanth VH6de20d32015-10-09 12:04:13 +053024import java.util.concurrent.locks.Lock;
25import java.util.concurrent.locks.ReentrantLock;
Satish Ke107e662015-09-21 19:00:17 +053026import org.apache.felix.scr.annotations.Activate;
27import org.apache.felix.scr.annotations.Component;
28import org.apache.felix.scr.annotations.Deactivate;
29import org.apache.felix.scr.annotations.Service;
Shashikanth VH6de20d32015-10-09 12:04:13 +053030import org.onlab.packet.IpAddress;
Satish Ke107e662015-09-21 19:00:17 +053031import org.onosproject.bgp.controller.BGPCfg;
32import org.onosproject.bgp.controller.BGPController;
33import org.onosproject.bgp.controller.BGPId;
Shashikanth VH6de20d32015-10-09 12:04:13 +053034import org.onosproject.bgp.controller.BGPPacketStats;
35import org.onosproject.bgp.controller.BGPPeer;
Satish Ke107e662015-09-21 19:00:17 +053036import org.onosproject.bgpio.protocol.BGPMessage;
Shashikanth VH6de20d32015-10-09 12:04:13 +053037import org.onosproject.bgpio.protocol.BGPVersion;
Satish Ke107e662015-09-21 19:00:17 +053038import org.slf4j.Logger;
39import org.slf4j.LoggerFactory;
40
41@Component(immediate = true)
42@Service
43public class BGPControllerImpl implements BGPController {
44
45 private static final Logger log = LoggerFactory.getLogger(BGPControllerImpl.class);
46
47 private final ExecutorService executorMsgs = Executors.newFixedThreadPool(32,
48 groupedThreads("onos/bgp",
49 "event-stats-%d"));
50
51 private final ExecutorService executorBarrier = Executors.newFixedThreadPool(4,
52 groupedThreads("onos/bgp",
Shashikanth VH6de20d32015-10-09 12:04:13 +053053 "event-barrier-%d"));
54 protected ConcurrentHashMap<BGPId, BGPPeer> connectedPeers = new ConcurrentHashMap<BGPId, BGPPeer>();
Satish Ke107e662015-09-21 19:00:17 +053055
Shashikanth VH6de20d32015-10-09 12:04:13 +053056 protected BGPPeerManager peerManager = new BGPPeerManager();
Satish Ke107e662015-09-21 19:00:17 +053057 final Controller ctrl = new Controller(this);
58
59 private BGPConfig bgpconfig = new BGPConfig();
60
61 @Activate
62 public void activate() {
63 this.ctrl.start();
64 log.info("Started");
65 }
66
67 @Deactivate
68 public void deactivate() {
69 // Close all connected peers
Shashikanth VH6de20d32015-10-09 12:04:13 +053070 closeConnectedPeers();
Satish Ke107e662015-09-21 19:00:17 +053071 this.ctrl.stop();
72 log.info("Stopped");
73 }
74
75 @Override
Shashikanth VH6de20d32015-10-09 12:04:13 +053076 public Iterable<BGPPeer> getPeers() {
77 return this.connectedPeers.values();
78 }
79
80 @Override
81 public BGPPeer getPeer(BGPId bgpId) {
82 return this.connectedPeers.get(bgpId);
83 }
84
85 @Override
Satish Ke107e662015-09-21 19:00:17 +053086 public void writeMsg(BGPId bgpId, BGPMessage msg) {
87 // TODO: Send message
88 }
89
90 @Override
91 public void processBGPPacket(BGPId bgpId, BGPMessage msg) {
92
93 switch (msg.getType()) {
94 case OPEN:
95 // TODO: Process Open message
96 break;
97 case KEEP_ALIVE:
98 // TODO: Process keepalive message
99 break;
100 case NOTIFICATION:
101 // TODO: Process notificatoin message
102 break;
103 case UPDATE:
104 // TODO: Process update message
105 break;
106 default:
107 // TODO: Process other message
108 break;
109 }
110 }
111
Shashikanth VH6de20d32015-10-09 12:04:13 +0530112 @Override
113 public void closeConnectedPeers() {
114 BGPPeer bgpPeer;
115 for (BGPId id : this.connectedPeers.keySet()) {
116 bgpPeer = getPeer(id);
117 bgpPeer.disconnectPeer();
118 }
119 }
120
Satish Ke107e662015-09-21 19:00:17 +0530121 /**
Shashikanth VH6de20d32015-10-09 12:04:13 +0530122 * Implementation of an BGP Peer which is responsible for keeping track of connected peers and the state in which
123 * they are.
Satish Ke107e662015-09-21 19:00:17 +0530124 */
Shashikanth VH6de20d32015-10-09 12:04:13 +0530125 public class BGPPeerManager {
126
127 private final Logger log = LoggerFactory.getLogger(BGPPeerManager.class);
128 private final Lock peerLock = new ReentrantLock();
129
130 /**
131 * Add a BGP peer that has just connected to the system.
132 *
133 * @param bgpId the id of bgp peer to add
134 * @param bgpPeer the actual bgp peer object.
135 * @return true if added, false otherwise.
136 */
137 public boolean addConnectedPeer(BGPId bgpId, BGPPeer bgpPeer) {
138
139 if (connectedPeers.get(bgpId) != null) {
140 this.log.error("Trying to add connectedPeer but found previous " + "value for bgp ip: {}",
141 bgpId.toString());
142 return false;
143 } else {
144 this.log.debug("Added Peer {}", bgpId.toString());
145 connectedPeers.put(bgpId, bgpPeer);
146 return true;
147 }
148 }
149
150 /**
151 * Checks if the activation for this bgp peer is valid.
152 *
153 * @param bgpId the id of bgp peer to check
154 * @return true if valid, false otherwise
155 */
156 public boolean isPeerConnected(BGPId bgpId) {
157 if (connectedPeers.get(bgpId) == null) {
158 this.log.error("Trying to activate peer but is not in " + "connected peer: bgpIp {}. Aborting ..",
159 bgpId.toString());
160 return false;
161 }
162
163 return true;
164 }
165
166 /**
167 * Checks if the activation for this bgp peer is valid.
168 *
169 * @param routerid the routerid of bgp peer to check
170 * @return true if valid, false otherwise
171 */
172 public boolean isPeerConnected(String routerid) {
173
174 final BGPId bgpId;
175 bgpId = BGPId.bgpId(IpAddress.valueOf(routerid));
176
177 if (connectedPeers.get(bgpId) != null) {
178 this.log.info("Peer connection exist ");
179 return true;
180 }
181 this.log.info("Initiate connect request to " + "peer: bgpIp {}", bgpId.toString());
182
183 return false;
184 }
185
186 /**
187 * Clear all state in controller peer maps for a bgp peer that has
188 * disconnected from the local controller.
189 *
190 * @param bgpId the id of bgp peer to remove.
191 */
192 public void removeConnectedPeer(BGPId bgpId) {
193 connectedPeers.remove(bgpId);
194 }
195
196 /**
197 * Clear all state in controller peer maps for a bgp peer that has
198 * disconnected from the local controller.
199 *
200 * @param routerid the router id of bgp peer to remove.
201 */
202 public void removeConnectedPeer(String routerid) {
203 final BGPId bgpId;
204
205 bgpId = BGPId.bgpId(IpAddress.valueOf(routerid));
206
207 connectedPeers.remove(bgpId);
208 }
209
210 /**
211 * Gets bgp peer for connected peer map.
212 *
213 * @param routerid router id
214 * @return peer if available, null otherwise
215 */
216 public BGPPeer getPeer(String routerid) {
217 final BGPId bgpId;
218 bgpId = BGPId.bgpId(IpAddress.valueOf(routerid));
219
220 return connectedPeers.get(bgpId);
221 }
222
223 /**
224 * Gets bgp peer instance.
225 *
226 * @param bgpId bgp identifier.
227 * @param pv bgp version.
228 * @param pktStats packet statistics.
229 * @return BGPPeer peer instance.
230 */
231 public BGPPeer getBGPPeerInstance(BGPId bgpId, BGPVersion pv, BGPPacketStats pktStats) {
232 BGPPeer bgpPeer = new BGPPeerImpl();
233 bgpPeer.init(bgpId, pv, pktStats);
234 return bgpPeer;
235 }
236
237 }
238
239 /**
240 * Gets controller instance.
241 *
242 * @return Controller instance.
243 */
Satish Ke107e662015-09-21 19:00:17 +0530244 public Controller getController() {
245 return ctrl;
246 }
247
Shashikanth VH6de20d32015-10-09 12:04:13 +0530248 /**
249 * Gets connected peers.
250 *
251 * @return connectedPeers from connected Peers Map.
252 */
253 public ConcurrentHashMap<BGPId, BGPPeer> getConnectedPeers() {
254 return connectedPeers;
255 }
256
257 /**
258 * Gets peer manager.
259 *
260 * @return peerManager.
261 */
262 public BGPPeerManager getPeerManager() {
263 return peerManager;
264 }
265
Satish Ke107e662015-09-21 19:00:17 +0530266 @Override
267 public BGPCfg getConfig() {
268 return this.bgpconfig;
269 }
Shashikanth VH6de20d32015-10-09 12:04:13 +0530270
271 @Override
272 public int getBGPConnNumber() {
273 return connectedPeers.size();
274 }
Satish Ke107e662015-09-21 19:00:17 +0530275}