SubnetRoute enhancements to VPN Service models
[vpnservice.git] / bgpmanager / bgpmanager-impl / src / main / java / org / opendaylight / bgpmanager / BgpManager.java
1 /*
2  * Copyright (c) 2015 - 2016 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     @Override
143     public void advertisePrefix(String rd, String prefix, String nextHop, int vpnLabel) throws Exception {
144         bcm.addPrefix(rd, prefix, nextHop, vpnLabel);
145     }
146
147     @Override
148     public void withdrawPrefix(String rd, String prefix) throws Exception {
149         bcm.delPrefix(rd, prefix);
150     }
151
152     public void setQbgpLog(String fileName, String debugLevel) throws Exception {
153       bcm.addLogging(fileName, debugLevel);
154     }
155
156     public void delLogging() throws Exception {
157       bcm.delLogging();
158     }
159
160     public void startBgp(int asn, String routerId, int spt, boolean fbit) {
161       bcm.startBgp(asn, routerId, spt, fbit);
162     }
163
164     public void stopBgp() {
165       bcm.stopBgp();
166     }
167
168     public void startConfig(String host, int port) {
169       bcm.startConfig(host, port);
170     }
171
172     public void stopConfig() {
173       bcm.stopConfig();
174     }
175
176     @Override
177     public String getDCGwIP() {
178         Bgp conf = getConfig();
179         if (conf == null) {
180           return null;
181         }
182         List<Neighbors> nbrs = conf.getNeighbors();
183         if (nbrs == null) {
184           return null;
185         }
186         return nbrs.get(0).getAddress().getValue();
187     }
188
189     public MBeanServer getBgpAlarmServer() {
190         return qbgpAlarmServer;
191     }
192
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.
199             return;
200         }
201         errorSubCode = BgpAlarmErrorCodes.checkErrorSubcode(subcode);
202         if (errorSubCode == BgpAlarmErrorCodes.ERROR_IGNORE) {
203             // Need to report only those subcodes, defined in
204             // BgpAlarmErrorCodes enum class.
205             return;
206         }
207         String alarmString = "";
208         alarmString = "Alarm (" + code + "," + subcode + ") from neighbor " + pfx;
209         qbgpAlarmProducer.sendBgpAlarmInfo(pfx, code, subcode);
210     }
211
212     public Timer getBgpCountersTimer() {
213         return bgpCountersTimer;
214     }
215
216     public BgpCounters getBgpCounters() {
217         return bgpCounters;
218     }
219
220     public  void setBgpCountersTimer (Timer t) {
221         bgpCountersTimer = t;
222     }
223
224     public void startBgpCountersTask() {
225         if (getBgpCounters() == null) {
226
227             try {
228                 bgpCounters = new BgpCounters();
229                 setBgpCountersTimer(new Timer(true));
230                 getBgpCountersTimer().scheduleAtFixedRate(bgpCounters, 0, 120 * 1000);
231
232
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.");
236                 e.printStackTrace();
237             }
238
239             try {
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");
243             }
244         }
245     }
246
247     public void stopBgpCountersTask() {
248         Timer t = getBgpCountersTimer();
249         if (getBgpCounters() != null) {
250             t.cancel();
251             setBgpCountersTimer(null);
252             bgpCounters = null;
253         }
254     }
255
256     public FibDSWriter getFibWriter() {
257         return fibDSWriter;
258     } 
259
260     public DataBroker getBroker() {
261         return dataBroker;
262     } 
263
264     public String getConfigHost() {
265         return bcm.getConfigHost();
266     }
267
268     public int getConfigPort() {
269         return bcm.getConfigPort();
270     }
271
272     public void bgpRestarted() {
273         bcm.bgpRestarted();
274     }
275 }