2 * Copyright (c) 2015 - 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.bgpmanager;
11 import java.lang.management.ManagementFactory;
13 import java.util.concurrent.CountDownLatch;
14 import javax.management.*;
15 import org.apache.thrift.TException;
16 import org.opendaylight.bgpmanager.api.IBgpManager;
17 import org.opendaylight.bgpmanager.commands.Commands;
18 import org.opendaylight.bgpmanager.oam.*;
19 import org.opendaylight.bgpmanager.thrift.gen.af_afi;
20 import org.opendaylight.bgpmanager.thrift.gen.af_safi;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
23 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
24 //import org.opendaylight.vpnservice.itm.api.IITMProvider;
25 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.Bgp;
26 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.Neighbors;
27 import org.osgi.framework.Bundle;
28 import org.osgi.framework.BundleContext;
29 import org.osgi.framework.FrameworkUtil;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class BgpManager implements BindingAwareProvider, AutoCloseable, IBgpManager {
35 private static final Logger LOGGER = LoggerFactory.getLogger(BgpManager.class);
36 private BgpConfigurationManager bcm;
37 private FibDSWriter fibDSWriter;
38 //private IITMProvider itmProvider;
39 private DataBroker dataBroker;
40 private BgpAlarmBroadcaster qbgpAlarmProducer = null;
41 private MBeanServer qbgpAlarmServer = null;
42 private NotificationFilter qbgpAlarmFilter = null;
43 final static int DEFAULT_STALEPATH_TIME = 210;
44 final static boolean DEFAULT_FBIT = true;
46 public BgpCounters bgpCounters;
47 public Timer bgpCountersTimer;
50 public void onSessionInitiated(ProviderContext session) {
52 dataBroker = session.getSALService(DataBroker.class);
53 fibDSWriter = new FibDSWriter(dataBroker);
54 BgpUtil.setBroker(dataBroker);
55 bcm = new BgpConfigurationManager(this);
56 Commands commands = new Commands(this);
57 ConfigureBgpCli.setBgpManager(this);
58 LOGGER.info("BgpManager started");
59 } catch (Exception e) {
60 LOGGER.error("Failed to start BgpManager: "+e);
63 // Set up the Infra for Posting BGP Alarms as JMX notifications.
65 qbgpAlarmProducer = new BgpAlarmBroadcaster();
66 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
67 ObjectName alarmObj = new ObjectName("SDNC.FM:name=BgpAlarmObj");
68 mbs.registerMBean(qbgpAlarmProducer, alarmObj);
69 } catch (JMException e) {
70 LOGGER.error("Adding a NotificationBroadcaster failed." + e.toString());
76 public void close() throws Exception {
78 LOGGER.info("BgpManager Closed");
81 /*public void setITMProvider(IITMProvider itmProvider) {
82 this.itmProvider = itmProvider;
85 public IITMProvider getItmProvider() { return this.itmProvider; } */
87 public Bgp getConfig() {
91 public void configureGR(int stalepathTime) throws TException {
92 bcm.addGracefulRestart(stalepathTime);
95 public void delGracefulRestart() throws Exception {
96 bcm.delGracefulRestart();
99 public void addNeighbor(String ipAddress, long asNum) throws TException {
100 bcm.addNeighbor(ipAddress, (int) asNum);
103 public void addEbgpMultihop(String ipAddress, int nhops) throws TException {
104 bcm.addEbgpMultihop(ipAddress, nhops);
107 public void addUpdateSource(String ipAddress, String srcIp) throws TException {
108 bcm.addUpdateSource(ipAddress, srcIp);
111 public void addAddressFamily(String ipAddress, af_afi afi, af_safi safi) throws TException {
112 bcm.addAddressFamily(ipAddress, afi.getValue(), safi.getValue());
115 public void deleteNeighbor(String ipAddress) throws TException {
116 bcm.delNeighbor(ipAddress);
120 public void addVrf(String rd, Collection<String> importRts, Collection<String> exportRts) throws Exception {
121 bcm.addVrf(rd, new ArrayList<String>(importRts),
122 new ArrayList<String>(exportRts));
126 public void deleteVrf(String rd) throws Exception {
131 public void addPrefix(String rd, String prefix, String nextHop, int vpnLabel) throws Exception {
132 fibDSWriter.addFibEntryToDS(rd, prefix, nextHop, vpnLabel);
133 bcm.addPrefix(rd, prefix, nextHop, vpnLabel);
137 public void deletePrefix(String rd, String prefix) throws Exception {
138 fibDSWriter.removeFibEntryFromDS(rd, prefix);
139 bcm.delPrefix(rd, prefix);
143 public void advertisePrefix(String rd, String prefix, String nextHop, int vpnLabel) throws Exception {
144 bcm.addPrefix(rd, prefix, nextHop, vpnLabel);
148 public void withdrawPrefix(String rd, String prefix) throws Exception {
149 bcm.delPrefix(rd, prefix);
152 public void setQbgpLog(String fileName, String debugLevel) throws Exception {
153 bcm.addLogging(fileName, debugLevel);
156 public void delLogging() throws Exception {
160 public void startBgp(int asn, String routerId, int spt, boolean fbit) {
161 bcm.startBgp(asn, routerId, spt, fbit);
164 public void stopBgp() {
168 public void startConfig(String host, int port) {
169 bcm.startConfig(host, port);
172 public void stopConfig() {
177 public String getDCGwIP() {
178 Bgp conf = getConfig();
182 List<Neighbors> nbrs = conf.getNeighbors();
186 return nbrs.get(0).getAddress().getValue();
189 public MBeanServer getBgpAlarmServer() {
190 return qbgpAlarmServer;
193 public synchronized void sendNotificationEvent(String pfx, int code, int subcode) {
194 BgpAlarmErrorCodes errorSubCode;
195 if (code != BgpConstants.BGP_NOTIFY_CEASE_CODE) {
196 // CEASE Notifications alone have to be reported to the CBA.
197 // Silently return here. No need to log because tons
198 // of non-alarm notifications will be sent to the SDNc.
201 errorSubCode = BgpAlarmErrorCodes.checkErrorSubcode(subcode);
202 if (errorSubCode == BgpAlarmErrorCodes.ERROR_IGNORE) {
203 // Need to report only those subcodes, defined in
204 // BgpAlarmErrorCodes enum class.
207 String alarmString = "";
208 alarmString = "Alarm (" + code + "," + subcode + ") from neighbor " + pfx;
209 qbgpAlarmProducer.sendBgpAlarmInfo(pfx, code, subcode);
212 public Timer getBgpCountersTimer() {
213 return bgpCountersTimer;
216 public BgpCounters getBgpCounters() {
220 public void setBgpCountersTimer (Timer t) {
221 bgpCountersTimer = t;
224 public void startBgpCountersTask() {
225 if (getBgpCounters() == null) {
228 bgpCounters = new BgpCounters();
229 setBgpCountersTimer(new Timer(true));
230 getBgpCountersTimer().scheduleAtFixedRate(bgpCounters, 0, 120 * 1000);
233 LOGGER.info("Bgp Counters task scheduled for every two minutes.");
234 } catch (Exception e) {
235 System.out.println("Could not start the timertask for Bgp Counters.");
240 setQbgpLog(BgpConstants.BGP_DEF_LOG_FILE, BgpConstants.BGP_DEF_LOG_LEVEL);
241 } catch (Exception e) {
242 System.out.println("Could not set the default options for logging");
247 public void stopBgpCountersTask() {
248 Timer t = getBgpCountersTimer();
249 if (getBgpCounters() != null) {
251 setBgpCountersTimer(null);
256 public FibDSWriter getFibWriter() {
260 public DataBroker getBroker() {
264 public String getConfigHost() {
265 return bcm.getConfigHost();
268 public int getConfigPort() {
269 return bcm.getConfigPort();
272 public void bgpRestarted() {