2 * Copyright (c) 2015 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.net.InetAddress;
12 import java.net.UnknownHostException;
13 import java.util.HashMap;
15 import java.util.concurrent.Future;
17 import org.apache.karaf.shell.commands.*;
18 import org.apache.karaf.shell.console.OsgiCommandSupport;
19 import org.opendaylight.bgpmanager.thrift.gen.af_afi;
20 import org.opendaylight.bgpmanager.thrift.gen.af_safi;
21 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.*;
22 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.*;
23 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.neighbors.*;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 import com.google.common.base.Optional;
32 import com.google.common.util.concurrent.FutureCallback;
33 import com.google.common.util.concurrent.Futures;
35 @Command(scope = "odl", name = "configure-bgp", description = "")
36 public class ConfigureBgpCli extends OsgiCommandSupport {
38 private static final Logger LOGGER = LoggerFactory.getLogger(ConfigureBgpCli.class);
40 private static BgpManager bgpManager;
42 public static void setBgpManager(BgpManager mgr) {
46 @Option(name = "-op", aliases = {"--operation","--op"}, description = "[start-bgp-server, stop-bgp-server, add-neighbor, delete-neighbor, graceful-restart, enable-log ]",
47 required = false, multiValued = false)
50 //exec configure-bgp add-neighbor --ip <neighbor-ip> --as-num <as-num> --address-family <af> --use-source-ip <sip> --ebgp-multihops <em> --next-hop <nh>
52 @Option(name = "--as-num", description = "as number of the bgp neighbor", required = false, multiValued = false)
53 String asNumber = null;
55 @Option(name = "--ip", description = "ip of the bgp neighbor", required = false, multiValued = false)
58 @Option(name = "--address-family", description = "address family of the bgp neighbor SAFI_IPV4_LABELED_UNICAST|SAFI_MPLS_VPN",
59 required = false, multiValued = false)
60 String addressFamily = null;
62 @Option(name = "--use-source-ip", description = "source ip to be used for neighborship connection establishment",
63 required = false, multiValued = false)
64 String sourceIp = null;
66 @Option(name = "--ebgp-multihops", description = "ebgp multihops of the bgp neighbor",
67 required = false, multiValued = false)
68 String ebgpMultihops = null;
70 @Option(name = "--router-id", description = "router id of the bgp instance",
71 required = false, multiValued = false)
72 String routerId = null;
74 @Option(name = "--stalepath-time", description = "the time delay after bgp restart stalepaths are cleaned",
75 required = false, multiValued = false)
76 String stalePathTime = null;
78 @Option(name = "--log-file-path", description = "bgp log file path",
79 required = false, multiValued = false)
80 String logFile = null;
82 @Option(name = "--log-level", description = "log level emergencies,alerts,critical,errors,warnings,notifications,informational,debugging",
83 required = false, multiValued = false)
84 String logLevel = null;
87 emergencies,alerts,critical,errors,warnings,notifications,informational,debugging;
91 protected Object doExecute() throws Exception {
94 System.out.println("Please provide valid operation");
96 System.out.println("exec configure-bgp -op [start-bgp-server | stop-bgp-server | add-neighbor | delete-neighbor| graceful-restart| enable-log ]");
99 case "start-bgp-server":
102 case "stop-bgp-server":
108 case "delete-neighbor":
111 case "graceful-restart":
118 System.out.println("invalid operation");
120 System.out.println("exec configure-bgp -op [start-bgp-server | stop-bgp-server | add-neighbor | delete-neighbor| graceful-restart| enable-log ]");
122 } catch (Exception e) {
123 log.error("failed to handle the command",e);
128 public boolean validateStalepathTime() {
130 int time = Integer.parseInt(stalePathTime);
131 if (time < 30 || time > 3600) {
132 System.out.println("invalid stale path time valid range [30-3600]");
133 printGracefulRestartHelp();
136 } catch (Exception e) {
137 System.out.println("invalid stale path time");
138 printGracefulRestartHelp();
144 private void configureGR() throws Exception {
145 boolean validStalepathTime = validateStalepathTime();
146 if (!validStalepathTime) {
149 bgpManager.configureGR(Integer.parseInt(stalePathTime));
152 private void deleteNeighbor() throws Exception {
153 if (ip == null || !validateIp(ip)) {
154 System.out.println("invalid neighbor ip");
155 printDeleteNeighborHelp();
158 long asNo = getAsNumber(ip);
160 System.out.println("neighbor does not exist");
161 printDeleteNeighborHelp();
164 bgpManager.deleteNeighbor(ip);
167 public long getAsNumber(String nbrIp) {
168 Bgp conf = bgpManager.getConfig();
172 List<Neighbors> nbrs = conf.getNeighbors();
176 for (Neighbors nbr : nbrs) {
177 if (nbrIp.equals(nbr.getAddress().getValue())) {
178 return nbr.getRemoteAs().longValue();
184 private void stopBgp() throws Exception {
185 Bgp conf = bgpManager.getConfig();
189 List<Neighbors> nbrs = conf.getNeighbors();
190 if (nbrs != null && nbrs.size() > 0) {
191 System.err.println("error: all BGP congiguration must be deleted before stopping the router instance");
194 bgpManager.stopBgp();
198 System.out.println("usage:");
201 void printStartBgpHelp() {
203 System.out.println("exec configure-bgp -op start-bgp-server --as-num <asnum> --router-id <routerid> [--stalepath-time <time>]");
206 void printAddNeighborHelp() {
208 System.out.println("exec configure-bgp -op add-neighbor --ip <neighbor-ip> --as-num <as-num> [--address-family <af>] [--use-source-ip <sip>] [--ebgp-multihops <em> ]");
211 void printDeleteNeighborHelp() {
213 System.out.println("exec configure-bgp -op delete-neighbor --ip <neighbor-ip>");
216 void printEnableLogHelp() {
218 System.out.println("exec configure-bgp -op enable-logging --filename <filename> --log-level [emergencies|alerts|critical|errors|warnings|notifications|informational|debugging]");
221 void printGracefulRestartHelp() {
223 System.out.println("exec configure-bgp -op graceful-restart --stalepath-time <30-3600>");
226 private void startBgp() throws Exception {
227 boolean validRouterId = false;
229 if (bgpManager.getConfig() != null &&
230 bgpManager.getConfig().getAsId() != null) {
231 System.out.println("bgp is already started please use stop-bgp-server and start again");
235 long asno = Long.valueOf(asNumber);
240 System.out.println("reserved as number supplied ");
244 if (asno <= 0 || asno > Integer.MAX_VALUE) {
245 System.out.println("invalid as number , supported range [1,"+Integer.MAX_VALUE+"]");
249 } catch (Exception e) {
250 System.out.println("invalid as number ");
254 validRouterId = validateIp(routerId);
255 if (!validRouterId) {
256 System.out.println("invalid router id please supply valid ip address");
261 if (stalePathTime != null) {
262 boolean validStalepathTime = validateStalepathTime();
263 if (!validStalepathTime) {
267 bgpManager.startBgp(Integer.parseInt(asNumber), routerId,
268 stalePathTime == null? 0 : Integer.parseInt(stalePathTime), false);
271 protected void addNeighbor() throws Exception {
273 long asno = Long.valueOf(asNumber);
278 System.out.println("reserved as number supplied ");
282 if (asno <= 0 || asno > Integer.MAX_VALUE) {
283 System.out.println("invalid as number , supported range [1,"+Integer.MAX_VALUE+"]");
284 printAddNeighborHelp();
287 } catch (Exception e) {
288 System.out.println("invalid as number");
289 printAddNeighborHelp();
292 boolean validIp = validateIp(ip);
294 System.out.println("invalid neighbor ip");
295 printAddNeighborHelp();
299 if (sourceIp != null) {
300 validIp = validateIp(sourceIp);
302 System.out.println("invalid source ip");
303 printAddNeighborHelp();
308 if (ebgpMultihops != null) {
310 long val = Long.valueOf(ebgpMultihops);
311 if (val < 1 || val > 255) {
312 System.out.println("invalid ebgpMultihops number , valid range [1,255] ");
313 printAddNeighborHelp();
316 } catch (Exception e) {
317 System.out.println("invalid ebgpMultihops number, valid range [1-255]");
318 printAddNeighborHelp();
322 if (addressFamily != null) {
324 af_safi.valueOf(addressFamily);
325 } catch (Exception e) {
326 System.out.println("invalid addressFamily valid values SAFI_IPV4_LABELED_UNICAST | SAFI_MPLS_VPN");
327 printAddNeighborHelp();
331 if (getAsNumber(ip) != -1) {
332 System.out.println("neighbor with ip "+ip+" already exists");
335 bgpManager.addNeighbor(ip, Long.valueOf(asNumber));
336 if (addressFamily != null) {
337 bgpManager.addAddressFamily(ip, af_afi.AFI_IP,
338 af_safi.valueOf(addressFamily));
340 if (ebgpMultihops != null) {
341 bgpManager.addEbgpMultihop(ip, Integer.parseInt(ebgpMultihops));
343 if (sourceIp != null) {
344 bgpManager.addUpdateSource(ip, sourceIp);
348 private boolean validateIp(String inputIp) {
349 boolean validIp = false;
351 if (inputIp != null) {
352 InetAddress addr = InetAddress.getByName(inputIp);
353 if (addr.isMulticastAddress()) {
354 System.out.println("ip cannot be multicast address");
357 if (addr.isLoopbackAddress()) {
358 System.out.println("ip cannot be loopback address");
361 byte addrBytes[] = addr.getAddress();
362 int lastPart = addrBytes[3] & 0xFF;
363 int firstPart = addrBytes[0] & 0xFF;
364 if (firstPart == 0) {
365 return false;//cannot start with 0 "0.1.2.3"
367 if (lastPart == 0 || lastPart == 255) {
372 } catch (Exception e) {
377 private void enableBgpLogLevel() throws Exception {
378 if (logFile == null) {
379 System.out.println("Please provide log file name ");
381 System.out.println("exec configure-bgp -op enable-log --log-file-path <logfile> --log-level <level>");
384 boolean validLoglevel = false;
386 LogLevels.valueOf(logLevel);
387 validLoglevel = true;
388 } catch (Exception e) {
390 if (!validLoglevel) {
391 System.out.println("Please provide valid log level emergencies|alerts|critical|errors|warnings|notifications|informational|debugging");
393 System.out.println("exec configure-bgp -op enable-log --log-file-path <logfile> --log-level <level>");
396 bgpManager.setQbgpLog(logFile, logLevel);