Merge "Updated BgpManager for Be"
[vpnservice.git] / bgpmanager / bgpmanager-impl / src / main / java / org / opendaylight / bgpmanager / BgpManager.java
1 /*
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.bgpmanager;
10
11 import java.lang.management.ManagementFactory;
12 import java.util.*;
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;
32
33 public class BgpManager implements BindingAwareProvider, AutoCloseable, IBgpManager {
34
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;
45
46     public BgpCounters bgpCounters;
47     public Timer bgpCountersTimer;
48
49     @Override
50     public void onSessionInitiated(ProviderContext session) {
51         try {
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);
61         }
62
63         // Set up the Infra for Posting BGP Alarms as JMX notifications.
64         try {
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());
71             e.printStackTrace();
72         }
73     }
74
75    @Override
76     public void close() throws Exception {
77         bcm.close(); 
78         LOGGER.info("BgpManager Closed");
79    }
80
81     /*public void setITMProvider(IITMProvider itmProvider) {
82         this.itmProvider = itmProvider;
83     }
84
85     public IITMProvider getItmProvider() { return this.itmProvider; } */
86
87     public Bgp getConfig() {
88       return bcm.get();
89     }
90
91     public void configureGR(int stalepathTime) throws TException {
92       bcm.addGracefulRestart(stalepathTime);
93     }
94
95     public void delGracefulRestart() throws Exception {
96       bcm.delGracefulRestart();
97     }
98
99     public void addNeighbor(String ipAddress, long asNum) throws TException {
100       bcm.addNeighbor(ipAddress, (int) asNum);
101     }
102
103     public void addEbgpMultihop(String ipAddress, int nhops) throws TException {
104       bcm.addEbgpMultihop(ipAddress, nhops);
105     }
106     
107     public void addUpdateSource(String ipAddress, String srcIp) throws TException {
108       bcm.addUpdateSource(ipAddress, srcIp);
109     }
110
111     public void addAddressFamily(String ipAddress, af_afi afi, af_safi safi) throws TException {
112       bcm.addAddressFamily(ipAddress, afi.getValue(), safi.getValue());
113     }
114
115     public void deleteNeighbor(String ipAddress) throws TException {
116       bcm.delNeighbor(ipAddress);
117     }
118
119     @Override
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)); 
123     }
124
125     @Override
126     public void deleteVrf(String rd) throws Exception {
127       bcm.delVrf(rd);
128     }
129
130     @Override
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);
134     }
135
136     @Override
137     public void deletePrefix(String rd, String prefix) throws Exception {
138       fibDSWriter.removeFibEntryFromDS(rd, prefix);
139       bcm.delPrefix(rd, prefix);
140     }
141
142     public void setQbgpLog(String fileName, String debugLevel) throws Exception {
143       bcm.addLogging(fileName, debugLevel);
144     }
145
146     public void delLogging() throws Exception {
147       bcm.delLogging();
148     }
149
150     public void startBgp(int asn, String routerId, int spt, boolean fbit) {
151       bcm.startBgp(asn, routerId, spt, fbit);
152     }
153
154     public void stopBgp() {
155       bcm.stopBgp();
156     }
157
158     public void startConfig(String host, int port) {
159       bcm.startConfig(host, port);
160     }
161
162     public void stopConfig() {
163       bcm.stopConfig();
164     }
165
166     @Override
167     public String getDCGwIP() {
168         Bgp conf = getConfig();
169         if (conf == null) {
170           return null;
171         }
172         List<Neighbors> nbrs = conf.getNeighbors();
173         if (nbrs == null) {
174           return null;
175         }
176         return nbrs.get(0).getAddress().getValue();
177     }
178
179     public MBeanServer getBgpAlarmServer() {
180         return qbgpAlarmServer;
181     }
182
183     public synchronized void sendNotificationEvent(String pfx, int code, int subcode) {
184         BgpAlarmErrorCodes errorSubCode;
185         if (code != BgpConstants.BGP_NOTIFY_CEASE_CODE) {
186             // CEASE Notifications alone have to be reported to the CBA.
187             // Silently return here. No need to log because tons
188             // of non-alarm notifications will be sent to the SDNc.
189             return;
190         }
191         errorSubCode = BgpAlarmErrorCodes.checkErrorSubcode(subcode);
192         if (errorSubCode == BgpAlarmErrorCodes.ERROR_IGNORE) {
193             // Need to report only those subcodes, defined in
194             // BgpAlarmErrorCodes enum class.
195             return;
196         }
197         String alarmString = "";
198         alarmString = "Alarm (" + code + "," + subcode + ") from neighbor " + pfx;
199         qbgpAlarmProducer.sendBgpAlarmInfo(pfx, code, subcode);
200     }
201
202     public Timer getBgpCountersTimer() {
203         return bgpCountersTimer;
204     }
205
206     public BgpCounters getBgpCounters() {
207         return bgpCounters;
208     }
209
210     public  void setBgpCountersTimer (Timer t) {
211         bgpCountersTimer = t;
212     }
213
214     public void startBgpCountersTask() {
215         if (getBgpCounters() == null) {
216
217             try {
218                 bgpCounters = new BgpCounters();
219                 setBgpCountersTimer(new Timer(true));
220                 getBgpCountersTimer().scheduleAtFixedRate(bgpCounters, 0, 120 * 1000);
221
222
223                 LOGGER.info("Bgp Counters task scheduled for every two minutes.");
224             } catch (Exception e) {
225                 System.out.println("Could not start the timertask for Bgp Counters.");
226                 e.printStackTrace();
227             }
228
229             try {
230                 setQbgpLog(BgpConstants.BGP_DEF_LOG_FILE, BgpConstants.BGP_DEF_LOG_LEVEL);
231             } catch (Exception e) {
232                 System.out.println("Could not set the default options for logging");
233             }
234         }
235     }
236
237     public void stopBgpCountersTask() {
238         Timer t = getBgpCountersTimer();
239         if (getBgpCounters() != null) {
240             t.cancel();
241             setBgpCountersTimer(null);
242             bgpCounters = null;
243         }
244     }
245
246     public FibDSWriter getFibWriter() {
247         return fibDSWriter;
248     } 
249
250     public DataBroker getBroker() {
251         return dataBroker;
252     } 
253
254     public String getConfigHost() {
255         return bcm.getConfigHost();
256     }
257
258     public int getConfigPort() {
259         return bcm.getConfigPort();
260     }
261
262     public void bgpRestarted() {
263         bcm.bgpRestarted();
264     }
265 }