blob: e540824b20c79168fbf6d6142855beaedf1c225b [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 VHf62b5c02015-11-20 22:23:08 +053019import java.util.Set;
Shashikanth VH6de20d32015-10-09 12:04:13 +053020import java.util.concurrent.ConcurrentHashMap;
Shashikanth VHf62b5c02015-11-20 22:23:08 +053021import java.util.concurrent.CopyOnWriteArraySet;
Shashikanth VH6de20d32015-10-09 12:04:13 +053022import java.util.concurrent.locks.Lock;
23import java.util.concurrent.locks.ReentrantLock;
Shashikanth VH9f8afb42015-11-04 18:00:30 +053024
Satish Ke107e662015-09-21 19:00:17 +053025import org.apache.felix.scr.annotations.Activate;
26import org.apache.felix.scr.annotations.Component;
27import org.apache.felix.scr.annotations.Deactivate;
28import org.apache.felix.scr.annotations.Service;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053029import org.onosproject.bgp.controller.BgpCfg;
30import org.onosproject.bgp.controller.BgpController;
31import org.onosproject.bgp.controller.BgpId;
32import org.onosproject.bgp.controller.BgpPeer;
Shashikanth VHf62b5c02015-11-20 22:23:08 +053033import org.onosproject.bgp.controller.BgpLinkListener;
34import org.onosproject.bgp.controller.BgpNodeListener;
Shashikanth VH9f8afb42015-11-04 18:00:30 +053035import org.onosproject.bgp.controller.BgpPeerManager;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053036import org.onosproject.bgpio.exceptions.BgpParseException;
37import org.onosproject.bgpio.protocol.BgpMessage;
Satish Ke107e662015-09-21 19:00:17 +053038import org.slf4j.Logger;
39import org.slf4j.LoggerFactory;
40
41@Component(immediate = true)
42@Service
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053043public class BgpControllerImpl implements BgpController {
Satish Ke107e662015-09-21 19:00:17 +053044
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053045 private static final Logger log = LoggerFactory.getLogger(BgpControllerImpl.class);
Satish Ke107e662015-09-21 19:00:17 +053046
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053047 protected ConcurrentHashMap<BgpId, BgpPeer> connectedPeers = new ConcurrentHashMap<BgpId, BgpPeer>();
Satish Ke107e662015-09-21 19:00:17 +053048
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053049 protected BgpPeerManagerImpl peerManager = new BgpPeerManagerImpl();
Shashikanth VHf62b5c02015-11-20 22:23:08 +053050
51 protected Set<BgpNodeListener> bgpNodeListener = new CopyOnWriteArraySet<>();
52 protected Set<BgpLinkListener> bgpLinkListener = new CopyOnWriteArraySet<>();
53
Satish Ke107e662015-09-21 19:00:17 +053054 final Controller ctrl = new Controller(this);
55
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053056 private BgpConfig bgpconfig = new BgpConfig();
Satish Ke107e662015-09-21 19:00:17 +053057
58 @Activate
59 public void activate() {
60 this.ctrl.start();
61 log.info("Started");
62 }
63
64 @Deactivate
65 public void deactivate() {
66 // Close all connected peers
Shashikanth VH6de20d32015-10-09 12:04:13 +053067 closeConnectedPeers();
Satish Ke107e662015-09-21 19:00:17 +053068 this.ctrl.stop();
69 log.info("Stopped");
70 }
71
72 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053073 public Iterable<BgpPeer> getPeers() {
Shashikanth VH6de20d32015-10-09 12:04:13 +053074 return this.connectedPeers.values();
75 }
76
77 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053078 public BgpPeer getPeer(BgpId bgpId) {
Shashikanth VH6de20d32015-10-09 12:04:13 +053079 return this.connectedPeers.get(bgpId);
80 }
81
82 @Override
Shashikanth VHf62b5c02015-11-20 22:23:08 +053083 public void addListener(BgpNodeListener listener) {
84 this.bgpNodeListener.add(listener);
85 }
86
87 @Override
88 public void removeListener(BgpNodeListener listener) {
89 this.bgpNodeListener.remove(listener);
90 }
91
92 @Override
93 public Set<BgpNodeListener> listener() {
94 return bgpNodeListener;
95 }
96
97 @Override
98 public void addLinkListener(BgpLinkListener listener) {
99 this.bgpLinkListener.add(listener);
100 }
101
102 @Override
103 public void removeLinkListener(BgpLinkListener listener) {
104 this.bgpLinkListener.remove(listener);
105 }
106
107 @Override
108 public Set<BgpLinkListener> linkListener() {
109 return bgpLinkListener;
110 }
111
112 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530113 public void writeMsg(BgpId bgpId, BgpMessage msg) {
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530114 this.getPeer(bgpId).sendMessage(msg);
Satish Ke107e662015-09-21 19:00:17 +0530115 }
116
117 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530118 public void processBGPPacket(BgpId bgpId, BgpMessage msg) throws BgpParseException {
Satish Ke107e662015-09-21 19:00:17 +0530119
120 switch (msg.getType()) {
121 case OPEN:
122 // TODO: Process Open message
123 break;
124 case KEEP_ALIVE:
125 // TODO: Process keepalive message
126 break;
127 case NOTIFICATION:
128 // TODO: Process notificatoin message
129 break;
130 case UPDATE:
131 // TODO: Process update message
132 break;
133 default:
134 // TODO: Process other message
135 break;
136 }
137 }
138
Shashikanth VH6de20d32015-10-09 12:04:13 +0530139 @Override
140 public void closeConnectedPeers() {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530141 BgpPeer bgpPeer;
142 for (BgpId id : this.connectedPeers.keySet()) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530143 bgpPeer = getPeer(id);
144 bgpPeer.disconnectPeer();
145 }
146 }
147
Satish Ke107e662015-09-21 19:00:17 +0530148 /**
Shashikanth VH6de20d32015-10-09 12:04:13 +0530149 * Implementation of an BGP Peer which is responsible for keeping track of connected peers and the state in which
150 * they are.
Satish Ke107e662015-09-21 19:00:17 +0530151 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530152 public class BgpPeerManagerImpl implements BgpPeerManager {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530153
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530154 private final Logger log = LoggerFactory.getLogger(BgpPeerManagerImpl.class);
Shashikanth VH6de20d32015-10-09 12:04:13 +0530155 private final Lock peerLock = new ReentrantLock();
156
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530157 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530158 public boolean addConnectedPeer(BgpId bgpId, BgpPeer bgpPeer) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530159
160 if (connectedPeers.get(bgpId) != null) {
161 this.log.error("Trying to add connectedPeer but found previous " + "value for bgp ip: {}",
162 bgpId.toString());
163 return false;
164 } else {
165 this.log.debug("Added Peer {}", bgpId.toString());
166 connectedPeers.put(bgpId, bgpPeer);
167 return true;
168 }
169 }
170
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530171 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530172 public boolean isPeerConnected(BgpId bgpId) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530173 if (connectedPeers.get(bgpId) == null) {
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530174 this.log.error("Is peer connected: bgpIp {}.", bgpId.toString());
Shashikanth VH6de20d32015-10-09 12:04:13 +0530175 return false;
176 }
177
178 return true;
179 }
180
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530181 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530182 public void removeConnectedPeer(BgpId bgpId) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530183 connectedPeers.remove(bgpId);
184 }
185
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530186 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530187 public BgpPeer getPeer(BgpId bgpId) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530188 return connectedPeers.get(bgpId);
189 }
190
191 /**
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530192 * Gets bgp peer instance.
193 *
194 * @param bgpController controller instance.
195 * @param sessionInfo bgp session info.
196 * @param pktStats packet statistics.
197 * @return BGPPeer peer instance.
198 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530199 public BgpPeer getBgpPeerInstance(BgpController bgpController, BgpSessionInfoImpl sessionInfo,
200 BgpPacketStatsImpl pktStats) {
201 BgpPeer bgpPeer = new BgpPeerImpl(bgpController, sessionInfo, pktStats);
Shashikanth VH6de20d32015-10-09 12:04:13 +0530202 return bgpPeer;
203 }
204
205 }
206
Vidyashree Rama7bd3d782015-11-23 08:46:33 +0530207 /**
208 * Returns controller.
209 *
210 * @return controller
211 */
212 public Controller controller() {
213 return this.ctrl;
214 }
215
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530216 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530217 public ConcurrentHashMap<BgpId, BgpPeer> connectedPeers() {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530218 return connectedPeers;
219 }
220
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530221 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530222 public BgpPeerManagerImpl peerManager() {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530223 return peerManager;
224 }
225
Satish Ke107e662015-09-21 19:00:17 +0530226 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530227 public BgpCfg getConfig() {
Satish Ke107e662015-09-21 19:00:17 +0530228 return this.bgpconfig;
229 }
Shashikanth VH6de20d32015-10-09 12:04:13 +0530230
231 @Override
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530232 public int connectedPeerCount() {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530233 return connectedPeers.size();
234 }
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530235}