2 * Copyright © 2015, 2017 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;
12 import java.net.UnknownHostException;
13 import java.util.List;
14 import org.apache.karaf.shell.commands.Command;
15 import org.apache.karaf.shell.commands.Option;
16 import org.apache.karaf.shell.console.OsgiCommandSupport;
17 import org.opendaylight.netvirt.bgpmanager.thrift.gen.af_afi;
18 import org.opendaylight.netvirt.bgpmanager.thrift.gen.af_safi;
19 import org.opendaylight.netvirt.bgpmanager.thrift.gen.protocol_type;
20 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.Bgp;
21 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.TcpMd5SignaturePasswordType;
22 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.Neighbors;
23 import org.opendaylight.yangtools.yang.common.Uint32;
25 @Command(scope = "odl", name = "configure-bgp", description = "")
26 public class ConfigureBgpCli extends OsgiCommandSupport {
27 private static final long AS_MIN = 0;
28 private static final long AS_MAX = 4294967295L;//2^32-1
30 @Option(name = "-op", aliases = {"--operation", "--op"}, description = "[start-bgp-server, stop-bgp-server, "
31 + "add-neighbor, delete-neighbor, add-route, delete-route,graceful-restart, enable-log ]",
32 required = false, multiValued = false)
35 //exec configure-bgp add-neighbor --ip <neighbor-ip> --as-num <as-num> --address-family <af> --use-source-ip
36 // <sip> --ebgp-multihops <em> --next-hop <nh>
37 //exec configure-bgp --op add-route/delete-route --rd <rd> --prefix <prefix> --nexthop <nexthop>
38 // --mac <mac> --l2vni <l2vni> --l3vni <l3vni>
40 @Option(name = "--as-num", description = "as number of the bgp neighbor", required = false, multiValued = false)
41 String asNumber = null;
43 @Option(name = "--ip", description = "ip of the bgp neighbor", required = false, multiValued = false)
46 @Option(name = "--tcp-md5-password", description = "RFC2385 TCP MD5 Signature Option shared secret",
47 required = false, multiValued = false)
48 String md5passwordOption = null;
50 @Option(name = "--address-family", description = "address family of the bgp neighbor "
51 + "lu|evpn|vpnv4|vpnv6",
52 required = false, multiValued = false)
53 String addressFamily = null;
55 @Option(name = "--use-source-ip", description = "source ip to be used for neighborship connection establishment",
56 required = false, multiValued = false)
57 String sourceIp = null;
59 @Option(name = "--ebgp-multihops", description = "ebgp multihops of the bgp neighbor",
60 required = false, multiValued = false)
61 String ebgpMultihops = null;
63 @Option(name = "--router-id", description = "router id of the bgp instance",
64 required = false, multiValued = false)
65 String routerId = null;
67 @Option(name = "--rd", description = "rd of the route",
68 required = false, multiValued = false)
71 @Option(name = "--prefix", description = "prefix of the route",
72 required = false, multiValued = false)
75 @Option(name = "--nexthop", description = "nexthop of the route",
76 required = false, multiValued = false)
77 String nexthop = null;
79 @Option(name = "--mac", description = "mac of the route",
80 required = false, multiValued = false)
83 @Option(name = "--l2vni", description = "l2vni of the route",
84 required = false, multiValued = false)
85 Uint32 l2vni = Uint32.ZERO;
87 @Option(name = "--l3vni", description = "l3vni",
88 required = false, multiValued = false)
89 Uint32 l3vni = Uint32.ZERO;
91 @Option(name = "--stalepath-time", description = "the time delay after bgp restart stalepaths are cleaned",
92 required = false, multiValued = false)
93 String stalePathTime = null;
95 @Option(name = "--log-file-path", description = "bgp log file path",
96 required = false, multiValued = false)
97 String logFile = null;
99 @Option(name = "--log-level", description = "log level emergencies,alerts,critical,errors,warnings,notifications,"
100 + "informational,debugging",
101 required = false, multiValued = false)
102 String logLevel = null;
105 emergencies, alerts, critical, errors, warnings, notifications, informational, debugging
108 private final BgpConfigurationManager bgpConfigurationManager;
110 public ConfigureBgpCli(BgpConfigurationManager bgpConfigurationManager) {
111 this.bgpConfigurationManager = bgpConfigurationManager;
115 protected Object doExecute() {
117 session.getConsole().println("Please provide valid operation");
119 session.getConsole().println(
120 "exec configure-bgp -op [start-bgp-server | stop-bgp-server | add-neighbor | delete-neighbor|"
121 + " add-route | delete-route | graceful-restart| enable-log ]");
124 case "start-bgp-server":
127 case "stop-bgp-server":
133 case "delete-neighbor":
142 case "graceful-restart":
149 session.getConsole().println("invalid operation");
151 session.getConsole().println(
152 "exec configure-bgp -op [start-bgp-server | stop-bgp-server | add-neighbor | "
153 + "delete-neighbor| graceful-restart| enable-log ]");
158 public boolean validateStalepathTime() {
160 int time = Integer.parseInt(stalePathTime);
161 if (time < 30 || time > 3600) {
162 session.getConsole().println("invalid stale path time valid range [30-3600]");
163 printGracefulRestartHelp();
166 } catch (NumberFormatException e) {
167 session.getConsole().println("invalid stale path time");
168 printGracefulRestartHelp();
174 private void configureGR() {
175 boolean validStalepathTime = validateStalepathTime();
176 if (!validStalepathTime) {
179 bgpConfigurationManager.addGracefulRestart(Integer.parseInt(stalePathTime));
182 private void deleteNeighbor() {
183 if (ip == null || !validateIp(ip)) {
184 session.getConsole().println("invalid neighbor ip");
185 printDeleteNeighborHelp();
188 long asNo = getAsNumber(ip);
190 session.getConsole().println("neighbor does not exist");
191 printDeleteNeighborHelp();
194 bgpConfigurationManager.delNeighbor(ip);
197 public long getAsNumber(String nbrIp) {
198 Bgp conf = bgpConfigurationManager.getConfig();
202 List<Neighbors> nbrs = conf.getNeighbors();
206 for (Neighbors nbr : nbrs) {
207 if (nbrIp.equals(nbr.getAddress().getValue())) {
208 return nbr.getRemoteAs().toJava();
214 private void stopBgp() {
215 Bgp conf = bgpConfigurationManager.getConfig();
219 List<Neighbors> nbrs = conf.getNeighbors();
220 if (nbrs != null && nbrs.size() > 0) {
221 session.getConsole().println(
222 "error: all BGP congiguration must be deleted before stopping the router instance");
225 bgpConfigurationManager.stopBgp();
228 private void usage() {
229 session.getConsole().println("usage:");
232 private void printStartBgpHelp() {
234 session.getConsole().println(
235 "exec configure-bgp -op start-bgp-server --as-num <asnum> --router-id <routerid> [--stalepath-time "
239 private void printAddNeighborHelp() {
241 session.getConsole().println(
242 "exec configure-bgp -op add-neighbor --ip <neighbor-ip> --as-num <as-num> [--address-family <af>] "
243 + "[--tcp-md5-password <password>] "
244 + "[--use-source-ip <sip>] [--ebgp-multihops <em> ]");
247 private void printDeleteNeighborHelp() {
249 session.getConsole().println("exec configure-bgp -op delete-neighbor --ip <neighbor-ip>");
252 void printEnableLogHelp() {
254 session.getConsole().println(
255 "exec configure-bgp -op enable-logging --filename <filename> --log-level "
256 + "[emergencies|alerts|critical|errors|warnings|notifications|informational|debugging]");
259 private void printGracefulRestartHelp() {
261 session.getConsole().println("exec configure-bgp -op graceful-restart --stalepath-time <30-3600>");
264 private void startBgp() {
265 boolean validRouterId = false;
267 if (bgpConfigurationManager.getConfig() != null && bgpConfigurationManager.getConfig().getAsId() != null) {
268 session.getConsole().println("bgp is already started please use stop-bgp-server and start again");
271 if (!validateAsNumber(asNumber)) {
275 validRouterId = validateIp(routerId);
276 if (!validRouterId) {
277 session.getConsole().println("invalid router id please supply valid ip address");
282 if (stalePathTime != null) {
283 boolean validStalepathTime = validateStalepathTime();
284 if (!validStalepathTime) {
288 bgpConfigurationManager.startBgp(Long.parseLong(asNumber), routerId,
289 stalePathTime == null ? 0 : Integer.parseInt(stalePathTime), false);
292 protected void addNeighbor() {
293 if (!validateAsNumber(asNumber)) {
294 printAddNeighborHelp();
298 boolean validIp = validateIp(ip);
300 session.getConsole().println("invalid neighbor ip");
301 printAddNeighborHelp();
305 TcpMd5SignaturePasswordType md5secret = null;
306 if (md5passwordOption != null) {
308 md5secret = new TcpMd5SignaturePasswordType(md5passwordOption);
309 } catch (IllegalArgumentException e) {
310 session.getConsole().println(
311 new StringBuilder("invalid MD5 password: ").append(e.getMessage()).toString());
312 printAddNeighborHelp();
317 if (sourceIp != null) {
318 validIp = validateIp(sourceIp);
320 session.getConsole().println("invalid source ip");
321 printAddNeighborHelp();
326 if (ebgpMultihops != null) {
328 long val = Long.parseLong(ebgpMultihops);
329 if (val < 1 || val > 255) {
330 session.getConsole().println("invalid ebgpMultihops number , valid range [1,255] ");
331 printAddNeighborHelp();
334 } catch (NumberFormatException e) {
335 session.getConsole().println("invalid ebgpMultihops number, valid range [1-255]");
336 printAddNeighborHelp();
340 if (addressFamily != null) {
341 if (!addressFamily.equals("lu") && !addressFamily.equals("vpnv4")
342 && !addressFamily.equals("vpnv6")
343 && !addressFamily.equals("evpn")) {
344 session.getConsole().println("error: Address family must be lu/evpn/vpnv4/vpnv6 ");
350 if (addressFamily.equals("vpnv6")) {
353 } else if (addressFamily.equals("evpn")) {
356 } else if (addressFamily.equals("lu")) {
359 } else if (addressFamily.equals("vpnv4")) {
363 session.getConsole().println(
364 "invalid addressFamily valid values SAFI_IPV4_LABELED_UNICAST | SAFI_MPLS_VPN");
365 printAddNeighborHelp();
368 bgpConfigurationManager.addAddressFamily(ip, afi, safi);
371 if (getAsNumber(ip) != -1) {
372 session.getConsole().println("neighbor with ip " + ip + " already exists");
375 bgpConfigurationManager.addNeighbor(ip, Long.parseLong(asNumber), md5secret);
376 if (addressFamily != null) {
377 bgpConfigurationManager.addAddressFamily(ip, af_afi.AFI_IP.getValue(),
378 af_safi.valueOf(addressFamily).getValue());
380 if (ebgpMultihops != null) {
381 bgpConfigurationManager.addEbgpMultihop(ip, Integer.parseInt(ebgpMultihops));
383 if (sourceIp != null) {
384 bgpConfigurationManager.addUpdateSource(ip, sourceIp);
388 protected void addRoute() {
389 bgpConfigurationManager.onUpdatePushRoute(protocol_type.PROTOCOL_EVPN, rd, prefix,
390 0, nexthop, mac, l3vni, l2vni, null, null);
393 protected void deleteRoute() {
394 bgpConfigurationManager.onUpdateWithdrawRoute(protocol_type.PROTOCOL_EVPN, rd, prefix,
398 private boolean validateIp(String inputIp) {
399 boolean validIp = false;
401 if (inputIp != null) {
402 InetAddress addr = InetAddress.getByName(inputIp);
403 if (addr.isMulticastAddress()) {
404 session.getConsole().println("ip cannot be multicast address");
407 if (addr.isLoopbackAddress()) {
408 session.getConsole().println("ip cannot be loopback address");
411 byte[] addrBytes = addr.getAddress();
412 int lastPart = addrBytes[3] & 0xFF;
413 int firstPart = addrBytes[0] & 0xFF;
414 if (firstPart == 0) {
415 return false;//cannot start with 0 "0.1.2.3"
417 if (lastPart == 0 || lastPart == 255) {
422 } catch (UnknownHostException e) {
428 private void enableBgpLogLevel() {
429 if (logFile == null) {
430 session.getConsole().println("Please provide log file name ");
432 session.getConsole().println(
433 "exec configure-bgp -op enable-log --log-file-path <logfile> --log-level <level>");
436 boolean validLoglevel = false;
438 LogLevels.valueOf(logLevel);
439 validLoglevel = true;
440 } catch (IllegalArgumentException e) {
443 if (!validLoglevel) {
444 session.getConsole().println(
445 "Please provide valid log level "
446 + "emergencies|alerts|critical|errors|warnings|notifications|informational|debugging");
448 session.getConsole().println(
449 "exec configure-bgp -op enable-log --log-file-path <logfile> --log-level <level>");
452 bgpConfigurationManager.addLogging(logFile, logLevel);
455 private boolean validateAsNumber(String strAsnum) {
458 long asNum = Long.parseLong(strAsnum);
459 if (asNum == 0L || asNum == 65535L || asNum == 23456L) {
460 session.getConsole().println("reserved AS Number supplied ");
463 if (asNum <= AS_MIN || asNum > AS_MAX) {
464 session.getConsole().println("invalid AS Number , supported range [1," + AS_MAX + "]");
467 } catch (NumberFormatException e) {
468 session.getConsole().println("invalid AS Number ");