blob: 08c1fa21787917d282021309c7b54d811ac8bec7 [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 VH6de20d32015-10-09 12:04:13 +053019import java.util.concurrent.ConcurrentHashMap;
Shashikanth VH6de20d32015-10-09 12:04:13 +053020import java.util.concurrent.locks.Lock;
21import java.util.concurrent.locks.ReentrantLock;
Shashikanth VH9f8afb42015-11-04 18:00:30 +053022
Satish Ke107e662015-09-21 19:00:17 +053023import org.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Component;
25import org.apache.felix.scr.annotations.Deactivate;
26import org.apache.felix.scr.annotations.Service;
27import org.onosproject.bgp.controller.BGPCfg;
28import org.onosproject.bgp.controller.BGPController;
29import org.onosproject.bgp.controller.BGPId;
Shashikanth VH6de20d32015-10-09 12:04:13 +053030import org.onosproject.bgp.controller.BGPPeer;
Shashikanth VH9f8afb42015-11-04 18:00:30 +053031import org.onosproject.bgp.controller.BgpPeerManager;
32import org.onosproject.bgpio.exceptions.BGPParseException;
Satish Ke107e662015-09-21 19:00:17 +053033import org.onosproject.bgpio.protocol.BGPMessage;
34import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
36
37@Component(immediate = true)
38@Service
39public class BGPControllerImpl implements BGPController {
40
41 private static final Logger log = LoggerFactory.getLogger(BGPControllerImpl.class);
42
Shashikanth VH6de20d32015-10-09 12:04:13 +053043 protected ConcurrentHashMap<BGPId, BGPPeer> connectedPeers = new ConcurrentHashMap<BGPId, BGPPeer>();
Satish Ke107e662015-09-21 19:00:17 +053044
Shashikanth VH9f8afb42015-11-04 18:00:30 +053045 protected BGPPeerManagerImpl peerManager = new BGPPeerManagerImpl();
Satish Ke107e662015-09-21 19:00:17 +053046 final Controller ctrl = new Controller(this);
47
48 private BGPConfig bgpconfig = new BGPConfig();
49
50 @Activate
51 public void activate() {
52 this.ctrl.start();
53 log.info("Started");
54 }
55
56 @Deactivate
57 public void deactivate() {
58 // Close all connected peers
Shashikanth VH6de20d32015-10-09 12:04:13 +053059 closeConnectedPeers();
Satish Ke107e662015-09-21 19:00:17 +053060 this.ctrl.stop();
61 log.info("Stopped");
62 }
63
64 @Override
Shashikanth VH6de20d32015-10-09 12:04:13 +053065 public Iterable<BGPPeer> getPeers() {
66 return this.connectedPeers.values();
67 }
68
69 @Override
70 public BGPPeer getPeer(BGPId bgpId) {
71 return this.connectedPeers.get(bgpId);
72 }
73
74 @Override
Satish Ke107e662015-09-21 19:00:17 +053075 public void writeMsg(BGPId bgpId, BGPMessage msg) {
Shashikanth VH9f8afb42015-11-04 18:00:30 +053076 this.getPeer(bgpId).sendMessage(msg);
Satish Ke107e662015-09-21 19:00:17 +053077 }
78
79 @Override
Shashikanth VH9f8afb42015-11-04 18:00:30 +053080 public void processBGPPacket(BGPId bgpId, BGPMessage msg) throws BGPParseException {
Satish Ke107e662015-09-21 19:00:17 +053081
82 switch (msg.getType()) {
83 case OPEN:
84 // TODO: Process Open message
85 break;
86 case KEEP_ALIVE:
87 // TODO: Process keepalive message
88 break;
89 case NOTIFICATION:
90 // TODO: Process notificatoin message
91 break;
92 case UPDATE:
93 // TODO: Process update message
94 break;
95 default:
96 // TODO: Process other message
97 break;
98 }
99 }
100
Shashikanth VH6de20d32015-10-09 12:04:13 +0530101 @Override
102 public void closeConnectedPeers() {
103 BGPPeer bgpPeer;
104 for (BGPId id : this.connectedPeers.keySet()) {
105 bgpPeer = getPeer(id);
106 bgpPeer.disconnectPeer();
107 }
108 }
109
Satish Ke107e662015-09-21 19:00:17 +0530110 /**
Shashikanth VH6de20d32015-10-09 12:04:13 +0530111 * Implementation of an BGP Peer which is responsible for keeping track of connected peers and the state in which
112 * they are.
Satish Ke107e662015-09-21 19:00:17 +0530113 */
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530114 public class BGPPeerManagerImpl implements BgpPeerManager {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530115
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530116 private final Logger log = LoggerFactory.getLogger(BGPPeerManagerImpl.class);
Shashikanth VH6de20d32015-10-09 12:04:13 +0530117 private final Lock peerLock = new ReentrantLock();
118
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530119 @Override
Shashikanth VH6de20d32015-10-09 12:04:13 +0530120 public boolean addConnectedPeer(BGPId bgpId, BGPPeer bgpPeer) {
121
122 if (connectedPeers.get(bgpId) != null) {
123 this.log.error("Trying to add connectedPeer but found previous " + "value for bgp ip: {}",
124 bgpId.toString());
125 return false;
126 } else {
127 this.log.debug("Added Peer {}", bgpId.toString());
128 connectedPeers.put(bgpId, bgpPeer);
129 return true;
130 }
131 }
132
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530133 @Override
Shashikanth VH6de20d32015-10-09 12:04:13 +0530134 public boolean isPeerConnected(BGPId bgpId) {
135 if (connectedPeers.get(bgpId) == null) {
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530136 this.log.error("Is peer connected: bgpIp {}.", bgpId.toString());
Shashikanth VH6de20d32015-10-09 12:04:13 +0530137 return false;
138 }
139
140 return true;
141 }
142
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530143 @Override
Shashikanth VH6de20d32015-10-09 12:04:13 +0530144 public void removeConnectedPeer(BGPId bgpId) {
145 connectedPeers.remove(bgpId);
146 }
147
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530148 @Override
149 public BGPPeer getPeer(BGPId bgpId) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530150 return connectedPeers.get(bgpId);
151 }
152
153 /**
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530154 * Gets bgp peer instance.
155 *
156 * @param bgpController controller instance.
157 * @param sessionInfo bgp session info.
158 * @param pktStats packet statistics.
159 * @return BGPPeer peer instance.
160 */
161 public BGPPeer getBGPPeerInstance(BGPController bgpController, BgpSessionInfoImpl sessionInfo,
162 BGPPacketStatsImpl pktStats) {
163 BGPPeer bgpPeer = new BGPPeerImpl(bgpController, sessionInfo, pktStats);
Shashikanth VH6de20d32015-10-09 12:04:13 +0530164 return bgpPeer;
165 }
166
167 }
168
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530169 @Override
170 public ConcurrentHashMap<BGPId, BGPPeer> connectedPeers() {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530171 return connectedPeers;
172 }
173
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530174 @Override
175 public BGPPeerManagerImpl peerManager() {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530176 return peerManager;
177 }
178
Satish Ke107e662015-09-21 19:00:17 +0530179 @Override
180 public BGPCfg getConfig() {
181 return this.bgpconfig;
182 }
Shashikanth VH6de20d32015-10-09 12:04:13 +0530183
184 @Override
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530185 public int connectedPeerCount() {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530186 return connectedPeers.size();
187 }
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530188}