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.netvirt.bgpmanager;
11 import java.net.InetAddress;
13 import org.apache.karaf.shell.commands.*;
14 import org.apache.karaf.shell.console.OsgiCommandSupport;
15 import org.opendaylight.netvirt.bgpmanager.thrift.gen.af_afi;
16 import org.opendaylight.netvirt.bgpmanager.thrift.gen.af_safi;
17 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.*;
18 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.*;
19 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.neighbors.*;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 @Command(scope = "odl", name = "configure-bgp", description = "")
25 public class ConfigureBgpCli extends OsgiCommandSupport {
27 private static final Logger LOGGER = LoggerFactory.getLogger(ConfigureBgpCli.class);
29 private static BgpManager bgpManager;
31 public static void setBgpManager(BgpManager mgr) {
35 @Option(name = "-op", aliases = {"--operation","--op"}, description = "[start-bgp-server, stop-bgp-server, add-neighbor, delete-neighbor, graceful-restart, enable-log ]",
36 required = false, multiValued = false)
39 //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>
41 @Option(name = "--as-num", description = "as number of the bgp neighbor", required = false, multiValued = false)
42 String asNumber = null;
44 @Option(name = "--ip", description = "ip of the bgp neighbor", required = false, multiValued = false)
47 @Option(name = "--address-family", description = "address family of the bgp neighbor SAFI_IPV4_LABELED_UNICAST|SAFI_MPLS_VPN",
48 required = false, multiValued = false)
49 String addressFamily = null;
51 @Option(name = "--use-source-ip", description = "source ip to be used for neighborship connection establishment",
52 required = false, multiValued = false)
53 String sourceIp = null;
55 @Option(name = "--ebgp-multihops", description = "ebgp multihops of the bgp neighbor",
56 required = false, multiValued = false)
57 String ebgpMultihops = null;
59 @Option(name = "--router-id", description = "router id of the bgp instance",
60 required = false, multiValued = false)
61 String routerId = null;
63 @Option(name = "--stalepath-time", description = "the time delay after bgp restart stalepaths are cleaned",
64 required = false, multiValued = false)
65 String stalePathTime = null;
67 @Option(name = "--log-file-path", description = "bgp log file path",
68 required = false, multiValued = false)
69 String logFile = null;
71 @Option(name = "--log-level", description = "log level emergencies,alerts,critical,errors,warnings,notifications,informational,debugging",
72 required = false, multiValued = false)
73 String logLevel = null;
76 emergencies,alerts,critical,errors,warnings,notifications,informational,debugging
80 protected Object doExecute() throws Exception {
83 System.out.println("Please provide valid operation");
85 System.out.println("exec configure-bgp -op [start-bgp-server | stop-bgp-server | add-neighbor | delete-neighbor| graceful-restart| enable-log ]");
88 case "start-bgp-server":
91 case "stop-bgp-server":
97 case "delete-neighbor":
100 case "graceful-restart":
107 System.out.println("invalid operation");
109 System.out.println("exec configure-bgp -op [start-bgp-server | stop-bgp-server | add-neighbor | delete-neighbor| graceful-restart| enable-log ]");
111 } catch (Exception e) {
112 log.error("failed to handle the command",e);
117 public boolean validateStalepathTime() {
119 int time = Integer.parseInt(stalePathTime);
120 if (time < 30 || time > 3600) {
121 System.out.println("invalid stale path time valid range [30-3600]");
122 printGracefulRestartHelp();
125 } catch (Exception e) {
126 System.out.println("invalid stale path time");
127 printGracefulRestartHelp();
133 private void configureGR() throws Exception {
134 boolean validStalepathTime = validateStalepathTime();
135 if (!validStalepathTime) {
138 bgpManager.configureGR(Integer.parseInt(stalePathTime));
141 private void deleteNeighbor() throws Exception {
142 if (ip == null || !validateIp(ip)) {
143 System.out.println("invalid neighbor ip");
144 printDeleteNeighborHelp();
147 long asNo = getAsNumber(ip);
149 System.out.println("neighbor does not exist");
150 printDeleteNeighborHelp();
153 bgpManager.deleteNeighbor(ip);
156 public long getAsNumber(String nbrIp) {
157 Bgp conf = bgpManager.getConfig();
161 List<Neighbors> nbrs = conf.getNeighbors();
165 for (Neighbors nbr : nbrs) {
166 if (nbrIp.equals(nbr.getAddress().getValue())) {
167 return nbr.getRemoteAs().longValue();
173 private void stopBgp() throws Exception {
174 Bgp conf = bgpManager.getConfig();
178 List<Neighbors> nbrs = conf.getNeighbors();
179 if (nbrs != null && nbrs.size() > 0) {
180 System.err.println("error: all BGP congiguration must be deleted before stopping the router instance");
183 bgpManager.stopBgp();
187 System.out.println("usage:");
190 void printStartBgpHelp() {
192 System.out.println("exec configure-bgp -op start-bgp-server --as-num <asnum> --router-id <routerid> [--stalepath-time <time>]");
195 void printAddNeighborHelp() {
197 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> ]");
200 void printDeleteNeighborHelp() {
202 System.out.println("exec configure-bgp -op delete-neighbor --ip <neighbor-ip>");
205 void printEnableLogHelp() {
207 System.out.println("exec configure-bgp -op enable-logging --filename <filename> --log-level [emergencies|alerts|critical|errors|warnings|notifications|informational|debugging]");
210 void printGracefulRestartHelp() {
212 System.out.println("exec configure-bgp -op graceful-restart --stalepath-time <30-3600>");
215 private void startBgp() throws Exception {
216 boolean validRouterId = false;
218 if (bgpManager.getConfig() != null &&
219 bgpManager.getConfig().getAsId() != null) {
220 System.out.println("bgp is already started please use stop-bgp-server and start again");
224 long asno = Long.valueOf(asNumber);
229 System.out.println("reserved as number supplied ");
233 if (asno <= 0 || asno > Integer.MAX_VALUE) {
234 System.out.println("invalid as number , supported range [1,"+Integer.MAX_VALUE+"]");
238 } catch (Exception e) {
239 System.out.println("invalid as number ");
243 validRouterId = validateIp(routerId);
244 if (!validRouterId) {
245 System.out.println("invalid router id please supply valid ip address");
250 if (stalePathTime != null) {
251 boolean validStalepathTime = validateStalepathTime();
252 if (!validStalepathTime) {
256 bgpManager.startBgp(Integer.parseInt(asNumber), routerId,
257 stalePathTime == null? 0 : Integer.parseInt(stalePathTime), false);
260 protected void addNeighbor() throws Exception {
262 long asno = Long.valueOf(asNumber);
267 System.out.println("reserved as number supplied ");
271 if (asno <= 0 || asno > Integer.MAX_VALUE) {
272 System.out.println("invalid as number , supported range [1,"+Integer.MAX_VALUE+"]");
273 printAddNeighborHelp();
276 } catch (Exception e) {
277 System.out.println("invalid as number");
278 printAddNeighborHelp();
281 boolean validIp = validateIp(ip);
283 System.out.println("invalid neighbor ip");
284 printAddNeighborHelp();
288 if (sourceIp != null) {
289 validIp = validateIp(sourceIp);
291 System.out.println("invalid source ip");
292 printAddNeighborHelp();
297 if (ebgpMultihops != null) {
299 long val = Long.valueOf(ebgpMultihops);
300 if (val < 1 || val > 255) {
301 System.out.println("invalid ebgpMultihops number , valid range [1,255] ");
302 printAddNeighborHelp();
305 } catch (Exception e) {
306 System.out.println("invalid ebgpMultihops number, valid range [1-255]");
307 printAddNeighborHelp();
311 if (addressFamily != null) {
313 af_safi.valueOf(addressFamily);
314 } catch (Exception e) {
315 System.out.println("invalid addressFamily valid values SAFI_IPV4_LABELED_UNICAST | SAFI_MPLS_VPN");
316 printAddNeighborHelp();
320 if (getAsNumber(ip) != -1) {
321 System.out.println("neighbor with ip "+ip+" already exists");
324 bgpManager.addNeighbor(ip, Long.valueOf(asNumber));
325 if (addressFamily != null) {
326 bgpManager.addAddressFamily(ip, af_afi.AFI_IP,
327 af_safi.valueOf(addressFamily));
329 if (ebgpMultihops != null) {
330 bgpManager.addEbgpMultihop(ip, Integer.parseInt(ebgpMultihops));
332 if (sourceIp != null) {
333 bgpManager.addUpdateSource(ip, sourceIp);
337 private boolean validateIp(String inputIp) {
338 boolean validIp = false;
340 if (inputIp != null) {
341 InetAddress addr = InetAddress.getByName(inputIp);
342 if (addr.isMulticastAddress()) {
343 System.out.println("ip cannot be multicast address");
346 if (addr.isLoopbackAddress()) {
347 System.out.println("ip cannot be loopback address");
350 byte addrBytes[] = addr.getAddress();
351 int lastPart = addrBytes[3] & 0xFF;
352 int firstPart = addrBytes[0] & 0xFF;
353 if (firstPart == 0) {
354 return false;//cannot start with 0 "0.1.2.3"
356 if (lastPart == 0 || lastPart == 255) {
361 } catch (Exception e) {
366 private void enableBgpLogLevel() throws Exception {
367 if (logFile == null) {
368 System.out.println("Please provide log file name ");
370 System.out.println("exec configure-bgp -op enable-log --log-file-path <logfile> --log-level <level>");
373 boolean validLoglevel = false;
375 LogLevels.valueOf(logLevel);
376 validLoglevel = true;
377 } catch (Exception e) {
379 if (!validLoglevel) {
380 System.out.println("Please provide valid log level emergencies|alerts|critical|errors|warnings|notifications|informational|debugging");
382 System.out.println("exec configure-bgp -op enable-log --log-file-path <logfile> --log-level <level>");
385 bgpManager.setQbgpLog(logFile, logLevel);