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.commands;
11 import org.apache.karaf.shell.commands.*;
12 import org.apache.karaf.shell.console.OsgiCommandSupport;
13 import org.opendaylight.netvirt.bgpmanager.BgpManager;
14 import org.opendaylight.netvirt.bgpmanager.thrift.gen.af_afi;
15 import org.opendaylight.netvirt.bgpmanager.thrift.gen.af_safi;
17 @Command(scope = "odl", name = "bgp-nbr",
18 description = "Add or delete BGP neighbor")
19 public class Neighbor extends OsgiCommandSupport {
20 private static final String IP = "--ip-address";
21 private static final String AS = "--as-number";
22 private static final String MH = "--ebgp-multihop";
23 private static final String US = "--update-source";
24 private static final String AF = "--address-family";
26 @Argument(index=0, name="add|del", description="The desired operation",
27 required=true, multiValued = false)
30 @Option(name=IP, aliases = {"-i"},
31 description="Neighbor's IP address",
32 required=false, multiValued=false)
35 @Option(name=AS, aliases = {"-a"},
36 description="AS number",
37 required=false, multiValued=false)
40 @Option(name=MH, aliases = {"-e"},
41 description="EBGP-multihop hops",
42 required=false, multiValued=false)
45 @Option(name=US, aliases = {"-u"},
46 description="Update source address",
47 required=false, multiValued=false)
50 @Option(name=AF, aliases = {"-f"},
51 description="Address family",
52 required=false, multiValued=false)
53 String addrFamily = null;
55 private Object usage() {
57 "usage: bgp-nbr ["+IP+" nbr-ip-address] ["+AS+" asnum] ["
58 +MH+" hops] ["+US+" source] ["+AF+" lu] <add|del>");
63 protected Object doExecute() throws Exception {
64 if (!Commands.bgpRunning()) {
67 BgpManager bm = Commands.getBgpManager();
71 System.err.println("error: "+IP+" needed");
74 if (bm.getConfig() == null) {
75 System.err.println("error: Bgp config is not present");
78 int asn = bm.getConfig().getAsId().getLocalAs().intValue();
80 if (!Commands.isValid(nbrIp, Commands.IPADDR, IP)) {
84 if (!Commands.isValid(asNum, Commands.INT, AS)) {
87 asn = Integer.valueOf(asNum);
90 bm.addNeighbor(nbrIp, asn);
92 if (!Commands.isValid(nHops, Commands.INT, MH)) {
95 hops = Integer.valueOf(nHops);
97 bm.addEbgpMultihop(nbrIp, hops);
100 if (!Commands.isValid(srcIp, Commands.IPADDR, US)) {
103 bm.addUpdateSource(nbrIp, srcIp);
105 if (addrFamily != null) {
106 if (!addrFamily.equals("lu")) {
107 System.err.println("error: "+AF+" must be lu");
110 af_afi afi = af_afi.findByValue(1);
111 af_safi safi = af_safi.findByValue(4);
112 bm.addAddressFamily(nbrIp, afi, safi);
117 System.err.println("error: "+IP+" needed");
120 if (!Commands.isValid(nbrIp, Commands.IPADDR, IP)) {
123 if (asNum != null || nHops != null || srcIp != null
124 || addrFamily != null) {
125 System.err.println("note: some option(s) not needed; ignored");
127 bm.deleteNeighbor(nbrIp);