blob: 6a14e85cc8f5fabf2a8d141004a73dbbc47459de [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.BgpNodeListener;
Shashikanth VH9f8afb42015-11-04 18:00:30 +053034import org.onosproject.bgp.controller.BgpPeerManager;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053035import org.onosproject.bgpio.exceptions.BgpParseException;
36import org.onosproject.bgpio.protocol.BgpMessage;
Satish Ke107e662015-09-21 19:00:17 +053037import org.slf4j.Logger;
38import org.slf4j.LoggerFactory;
39
40@Component(immediate = true)
41@Service
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053042public class BgpControllerImpl implements BgpController {
Satish Ke107e662015-09-21 19:00:17 +053043
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053044 private static final Logger log = LoggerFactory.getLogger(BgpControllerImpl.class);
Satish Ke107e662015-09-21 19:00:17 +053045
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053046 protected ConcurrentHashMap<BgpId, BgpPeer> connectedPeers = new ConcurrentHashMap<BgpId, BgpPeer>();
Satish Ke107e662015-09-21 19:00:17 +053047
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053048 protected BgpPeerManagerImpl peerManager = new BgpPeerManagerImpl();
Shashikanth VHf62b5c02015-11-20 22:23:08 +053049
50 protected Set<BgpNodeListener> bgpNodeListener = new CopyOnWriteArraySet<>();
Shashikanth VHf62b5c02015-11-20 22:23:08 +053051
Satish Ke107e662015-09-21 19:00:17 +053052 final Controller ctrl = new Controller(this);
53
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053054 private BgpConfig bgpconfig = new BgpConfig();
Satish Ke107e662015-09-21 19:00:17 +053055
56 @Activate
57 public void activate() {
58 this.ctrl.start();
59 log.info("Started");
60 }
61
62 @Deactivate
63 public void deactivate() {
64 // Close all connected peers
Shashikanth VH6de20d32015-10-09 12:04:13 +053065 closeConnectedPeers();
Satish Ke107e662015-09-21 19:00:17 +053066 this.ctrl.stop();
67 log.info("Stopped");
68 }
69
70 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053071 public Iterable<BgpPeer> getPeers() {
Shashikanth VH6de20d32015-10-09 12:04:13 +053072 return this.connectedPeers.values();
73 }
74
75 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053076 public BgpPeer getPeer(BgpId bgpId) {
Shashikanth VH6de20d32015-10-09 12:04:13 +053077 return this.connectedPeers.get(bgpId);
78 }
79
80 @Override
Shashikanth VHf62b5c02015-11-20 22:23:08 +053081 public void addListener(BgpNodeListener listener) {
82 this.bgpNodeListener.add(listener);
83 }
84
85 @Override
86 public void removeListener(BgpNodeListener listener) {
87 this.bgpNodeListener.remove(listener);
88 }
89
90 @Override
91 public Set<BgpNodeListener> listener() {
92 return bgpNodeListener;
93 }
94
95 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053096 public void writeMsg(BgpId bgpId, BgpMessage msg) {
Shashikanth VH9f8afb42015-11-04 18:00:30 +053097 this.getPeer(bgpId).sendMessage(msg);
Satish Ke107e662015-09-21 19:00:17 +053098 }
99
100 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530101 public void processBGPPacket(BgpId bgpId, BgpMessage msg) throws BgpParseException {
Satish Ke107e662015-09-21 19:00:17 +0530102
103 switch (msg.getType()) {
104 case OPEN:
105 // TODO: Process Open message
106 break;
107 case KEEP_ALIVE:
108 // TODO: Process keepalive message
109 break;
110 case NOTIFICATION:
111 // TODO: Process notificatoin message
112 break;
113 case UPDATE:
114 // TODO: Process update message
115 break;
116 default:
117 // TODO: Process other message
118 break;
119 }
120 }
121
Shashikanth VH6de20d32015-10-09 12:04:13 +0530122 @Override
123 public void closeConnectedPeers() {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530124 BgpPeer bgpPeer;
125 for (BgpId id : this.connectedPeers.keySet()) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530126 bgpPeer = getPeer(id);
127 bgpPeer.disconnectPeer();
128 }
129 }
130
Satish Ke107e662015-09-21 19:00:17 +0530131 /**
Shashikanth VH6de20d32015-10-09 12:04:13 +0530132 * Implementation of an BGP Peer which is responsible for keeping track of connected peers and the state in which
133 * they are.
Satish Ke107e662015-09-21 19:00:17 +0530134 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530135 public class BgpPeerManagerImpl implements BgpPeerManager {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530136
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530137 private final Logger log = LoggerFactory.getLogger(BgpPeerManagerImpl.class);
Shashikanth VH6de20d32015-10-09 12:04:13 +0530138 private final Lock peerLock = new ReentrantLock();
139
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530140 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530141 public boolean addConnectedPeer(BgpId bgpId, BgpPeer bgpPeer) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530142
143 if (connectedPeers.get(bgpId) != null) {
144 this.log.error("Trying to add connectedPeer but found previous " + "value for bgp ip: {}",
145 bgpId.toString());
146 return false;
147 } else {
148 this.log.debug("Added Peer {}", bgpId.toString());
149 connectedPeers.put(bgpId, bgpPeer);
150 return true;
151 }
152 }
153
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530154 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530155 public boolean isPeerConnected(BgpId bgpId) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530156 if (connectedPeers.get(bgpId) == null) {
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530157 this.log.error("Is peer connected: bgpIp {}.", bgpId.toString());
Shashikanth VH6de20d32015-10-09 12:04:13 +0530158 return false;
159 }
160
161 return true;
162 }
163
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530164 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530165 public void removeConnectedPeer(BgpId bgpId) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530166 connectedPeers.remove(bgpId);
167 }
168
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530169 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530170 public BgpPeer getPeer(BgpId bgpId) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530171 return connectedPeers.get(bgpId);
172 }
173
174 /**
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530175 * Gets bgp peer instance.
176 *
177 * @param bgpController controller instance.
178 * @param sessionInfo bgp session info.
179 * @param pktStats packet statistics.
180 * @return BGPPeer peer instance.
181 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530182 public BgpPeer getBgpPeerInstance(BgpController bgpController, BgpSessionInfoImpl sessionInfo,
183 BgpPacketStatsImpl pktStats) {
184 BgpPeer bgpPeer = new BgpPeerImpl(bgpController, sessionInfo, pktStats);
Shashikanth VH6de20d32015-10-09 12:04:13 +0530185 return bgpPeer;
186 }
187
188 }
189
Vidyashree Rama7bd3d782015-11-23 08:46:33 +0530190 /**
191 * Returns controller.
192 *
193 * @return controller
194 */
195 public Controller controller() {
196 return this.ctrl;
197 }
198
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530199 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530200 public ConcurrentHashMap<BgpId, BgpPeer> connectedPeers() {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530201 return connectedPeers;
202 }
203
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530204 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530205 public BgpPeerManagerImpl peerManager() {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530206 return peerManager;
207 }
208
Satish Ke107e662015-09-21 19:00:17 +0530209 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530210 public BgpCfg getConfig() {
Satish Ke107e662015-09-21 19:00:17 +0530211 return this.bgpconfig;
212 }
Shashikanth VH6de20d32015-10-09 12:04:13 +0530213
214 @Override
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530215 public int connectedPeerCount() {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530216 return connectedPeers.size();
217 }
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530218}