private BgpManager bgpManager;
private final DataBroker broker;
private static final int MAX_RETRIES_BGP_COMMUNICATION = 1;
+ private enum BgpOp {
+ START_BGP, ADD_NGHBR, DEL_NGHBR
+ }
public BgpConfigurationManager(final DataBroker db, BgpConfiguration bgpCfg, BgpManager bgpMgr) {
broker = db;
private synchronized void removeBgpRouter(BgpRouter del)
{
+ bgpManager.disconnect();
+
+ bgpConfiguration.setRouterId("");
+ bgpConfiguration.setAsNum(0);
}
}
if(bgpConfiguration.isConfigUpdated()) {
- int retryCount = 0;
- boolean retry = false;
- do {
- try {
- if(retry)
- LOG.info("Retrying BgpService start.." + "retry count:" + retryCount);
- //bgpManager.waitForBgpInit();
- bgpManager.startBgpService();
- LOG.info("BgpService start done..");
- retry = false;
- } catch (TException t) {
- LOG.info("BgpService start failed..");
- retry = true;
- retryCount++;
- }
- } while(retry && retryCount <= MAX_RETRIES_BGP_COMMUNICATION);
-
+ configureBgpServer(BgpOp.START_BGP);
bgpConfiguration.unsetConfigUpdated();
}
if(value.getLocalAsNumber() == null || value.getLocalAsIdentifier() == null)
return;
- int retryCount = 0;
- boolean retry = false;
-
- do {
- try {
- //bgpManager.waitForBgpInit();
- bgpManager.startBgpService();
- retry = false;
- } catch (TException t) {
- retry = true;
- retryCount++;
- }
- } while(retry && retryCount <= MAX_RETRIES_BGP_COMMUNICATION);
+ configureBgpServer(BgpOp.START_BGP);
}
@Override
if ((gateway.getPeerAddressType() != null) && (gateway.getPeerAddressType() instanceof org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.bgp.rev130715.bgp.neighbors.bgp.neighbor.peer.address.type.IpAddress)) {
IpAddress neighborIPAddr = ((org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.bgp.rev130715.bgp.neighbors.bgp.neighbor.peer.address.type.IpAddress) gateway.getPeerAddressType()).getIpAddress();
LOG.info("Bgp Neighbor IP Address " + neighborIPAddr.getIpv4Address().getValue());
- bgpConfiguration.setNeighbourIp("");
- bgpConfiguration.setNeighbourAsNum(0);
- int retryCount = 0;
- boolean retry = false;
+ configureBgpServer(BgpOp.DEL_NGHBR);
- do {
- try {
- bgpManager.deleteNeighbor(neighborIPAddr.getIpv4Address().getValue());
- retry = false;
- } catch (TException t) {
- retry = true;
- retryCount++;
- }
- } while(retry && retryCount <= MAX_RETRIES_BGP_COMMUNICATION);
+ bgpConfiguration.setNeighbourIp("");
+ bgpConfiguration.setNeighbourAsNum(0);
}
}
}
private synchronized void updateBgpNeighbors(BgpNeighbors original, BgpNeighbors update) {
+
List<BgpNeighbor> bgpNeighborList = update.getBgpNeighbor();
+ //handle the case where there are no neighbors configured - single neighbor entry has been deleted
+ if(bgpNeighborList.isEmpty()) {
+ configureBgpServer(BgpOp.DEL_NGHBR);
+ return;
+ }
+
//We will always consider the first element of this list, since there can be just one DC Gateway
BgpNeighbor gateway = bgpNeighborList.get(0);
if(gateway != null) {
+ if(gateway.getAsNumber() != null ||
+ ((gateway.getPeerAddressType() != null) && (gateway.getPeerAddressType() instanceof org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.bgp.rev130715.bgp.neighbors.bgp.neighbor.peer.address.type.IpAddress))) {
+ //there is an updated neighbor, so we need to delete the old neighbor
+ configureBgpServer(BgpOp.DEL_NGHBR);
+ }
if(gateway.getAsNumber() != null) {
LOG.info("Bgp Neighbor AS number " + gateway.getAsNumber());
if(bgpConfiguration.getNeighbourAsNum() != gateway.getAsNumber()) {
}
}
if(bgpConfiguration.isConfigUpdated()) {
- //delete old neighbor
- BgpNeighbor old = original.getBgpNeighbor().get(0);
- IpAddress oldIPAddr = ((org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.bgp.rev130715.bgp.neighbors.bgp.neighbor.peer.address.type.IpAddress)old.getPeerAddressType()).getIpAddress();
- int retryCount = 0;
- boolean retry = false;
- do {
- try {
- bgpManager.deleteNeighbor(oldIPAddr.getIpv4Address().getValue());
- retry = false;
- } catch (TException t) {
- retry = true;
- retryCount++;
- }
- } while(retry && retryCount <= MAX_RETRIES_BGP_COMMUNICATION);
-
//add the newly configured neighbor
- retryCount = 0;
- retry = false;
- do {
- try {
- bgpManager.addNeighbor(bgpConfiguration.getNeighbourIp(), bgpConfiguration.getNeighbourAsNum());
- retry = false;
- } catch (TException t) {
- retry = true;
- retryCount++;
- }
- } while(retry && retryCount <= MAX_RETRIES_BGP_COMMUNICATION);
+ configureBgpServer(BgpOp.ADD_NGHBR);
}
}
}
if(bgpConfiguration.getNeighbourAsNum() != 0 && bgpConfiguration.getNeighbourIp() != null) {
- int retryCount = 0;
- boolean retry = false;
- do {
- try {
- bgpManager.addNeighbor(bgpConfiguration.getNeighbourIp(), bgpConfiguration.getNeighbourAsNum());
- retry = false;
- } catch (TException t) {
- retry = true;
- retryCount++;
- }
- } while(retry && retryCount <= MAX_RETRIES_BGP_COMMUNICATION);
+ configureBgpServer(BgpOp.ADD_NGHBR);
}
}
return InstanceIdentifier.create(BgpNeighbors.class);
}
+
@Override
public void close() throws Exception {
if (listenerRegistration != null) {
}
}
+
+ private void configureBgpServer(BgpOp bgpOp) {
+ int retryCount = 0;
+ boolean retry = false;
+ do {
+ try {
+ switch(bgpOp) {
+ case START_BGP:
+ bgpManager.startBgpService();
+ break;
+ case ADD_NGHBR:
+ bgpManager.addNeighbor(bgpConfiguration.getNeighbourIp(), bgpConfiguration.getNeighbourAsNum());
+ break;
+ case DEL_NGHBR:
+ bgpManager.deleteNeighbor(bgpConfiguration.getNeighbourIp());
+ break;
+ default:
+ LOG.info("Invalid configuration option");
+ }
+
+ retry = false;
+ } catch (TException t) {
+ retry = true;
+ retryCount++;
+ }
+ } while(retry && retryCount <= MAX_RETRIES_BGP_COMMUNICATION);
+ }
}
}
} catch (TException t) {
- //s_logger.error("Transport error while starting bgp server ", t);
s_logger.error("Could not set up thrift connection with bgp server");
+ //s_logger.trace("Transport error while starting bgp server ", t);
reInitConn();
throw t;
} catch (Exception e) {
- s_logger.error("Error while starting bgp server", e);
+ s_logger.error("Error while starting bgp server");
+ //s_logger.trace("Bgp Service not started due to exception", e);
return;
}
@Override
public void addPrefix(String rd, String prefix, String nextHop, int vpnLabel) throws Exception {
- if(bgpThriftClient == null) {
- s_logger.info("Add BGP prefix - bgpThriftClient is null. Unable to add BGP prefix.");
- return;
- }
- if(!hasBgpServiceStarted) {
+
+ if(bgpThriftClient == null || !hasBgpServiceStarted) {
fibDSWriter.addFibEntryToDS(rd, prefix, nextHop, vpnLabel);
+ return;
}
+
try {
bgpThriftClient.addPrefix(rd, prefix, nextHop, vpnLabel);
} catch (BgpRouterException b) {
@Override
public void deletePrefix(String rd, String prefix) throws Exception {
- if(bgpThriftClient == null) {
- s_logger.info("Delete BGP prefix - bgpThriftClient is null. Unable to delete BGP prefix.");
- return;
- }
- if(!hasBgpServiceStarted) {
+ if(bgpThriftClient == null || !hasBgpServiceStarted) {
fibDSWriter.removeFibEntryFromDS(rd, prefix);
+ return;
}
+
try {
bgpThriftClient.delPrefix(rd, prefix);
} catch (BgpRouterException b) {
s_logger.info("Connected to BGP server " + host + " on port " + port);
} catch (BgpRouterException b) {
s_logger.error("Failed to connect to BGP server " + host + " on port " + port + " due to BgpRouter Exception number " + b.getErrorCode());
- s_logger.error("BgpRouterException trace ", b);
+ //_logger.error("BgpRouterException trace ", b);
throw b;
} catch (TException t) {
- s_logger.error("Failed to initialize BGP Connection due to Transport error ", t);
+ s_logger.error("Failed to initialize BGP Connection due to Transport error ");
throw t;
}
catch (Exception e) {
- s_logger.error("Failed to initialize BGP Connection ", e);
+ s_logger.error("Failed to initialize BGP Connection ");
throw e;
}
}
public void disconnect() {
bgpThriftClient.disconnect();
}
-/*
- public void setRoute(Route r) {
- s_logger.info("Setting route in VPN Manager");
- //l3Manager.getVpnInstanceManager().addRoute(r.getRd(), r.getPrefix(), r.getNexthop(), r.getLabel());
- }*/
-
- /* For testing purposes */
- /*public String ribGet() {
- String family = "ipv4";
- String format = "json";
-
- try {
- List<Route> routeList = bgpThriftClient.getRoutes();
- Iterator<Route> iter = routeList.iterator();
- while(iter.hasNext()) {
- Route r = iter.next();
- System.out.println("Route:: vrf:" + r.getRd() + " Prefix: " + r.getPrefix() + " Nexthop: " + r.getNexthop() + "Label: " + r.getLabel());
- }
- } catch (Exception e) {
- s_logger.error("Failed getting bgp routes ", e);
- }
- return null;
- }*/
}