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.qbgpConstants;
15 import org.opendaylight.netvirt.fibmanager.api.RouteOrigin;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
19 import java.util.List;
21 @Command(scope = "odl", name = "bgp-network",
22 description = "Add or delete BGP static routes")
23 public class Network extends OsgiCommandSupport {
24 private static final String RD = "--rd";
25 private static final String PFX = "--prefix";
26 private static final String NH = "--nexthop";
27 private static final String LB = "--label";
29 static final Logger LOGGER = LoggerFactory.getLogger(Network.class);
30 @Argument(name="add|del", description="The desired operation",
31 required=true, multiValued = false)
32 private String action = null;
34 @Option(name=RD, aliases={"-r"},
35 description="Route distinguisher",
36 required=false, multiValued=false)
37 private String rd = null;
39 @Option(name=PFX, aliases={"-p"},
40 description="prefix/length",
41 required=false, multiValued=false)
42 private String pfx = null;
44 @Option(name=NH, aliases={"-n"},
45 description="Nexthop",
46 required=false, multiValued=true)
47 private List<String> nh = null;
49 @Option(name=LB, aliases={"-l"},
51 required=false, multiValued=false)
52 private String lbl = null;
54 private RouteOrigin staticOrigin = RouteOrigin.STATIC;
56 private Object usage() {
58 "usage: bgp-network ["+RD+" rd] ["+PFX+" prefix/len] ["
59 +NH+" nexthop] ["+LB+" label] <add|del>");
64 protected Object doExecute() throws Exception {
65 if (!Commands.bgpRunning()) {
68 BgpManager bm = Commands.getBgpManager();
71 int label = qbgpConstants.LBL_EXPLICIT_NULL;
73 System.err.println("error: "+PFX+" is needed");
77 System.err.println("error: "+NH+" is needed");
80 //TODO: syntactic validation of prefix
81 for (String nextHop : nh) {
82 if (!Commands.isValid(nextHop, Commands.IPADDR, NH)) {
87 if (!Commands.isValid(lbl, Commands.INT, LB)) {
90 label = Integer.valueOf(lbl);
92 } else if (rd == null) {
93 System.err.println("error: "+RD+" is needed");
96 LOGGER.info("ADD: Adding Fib entry rd {} prefix {} nexthop {} label {}", rd, pfx, nh, label);
97 bm.addPrefix(rd, pfx, nh, label, staticOrigin);
98 LOGGER.info("ADD: Added Fib entry rd {} prefix {} nexthop {} label {}", rd, pfx, nh, label);
102 System.err.println("error: "+PFX+" is needed");
105 if (nh != null || lbl != null) {
106 System.err.println("note: some option(s) not needed; ignored");
108 LOGGER.info("REMOVE: Removing Fib entry rd {} prefix {}", rd, pfx);
109 bm.deletePrefix(rd, pfx);
110 LOGGER.info("REMOVE: Removed Fib entry rd {} prefix {}", rd, pfx);