Bug 7607 : Bgp route not getting sync-up with fib
[netvirt.git] / vpnservice / bgpmanager / bgpmanager-impl / src / main / java / org / opendaylight / netvirt / bgpmanager / commands / Router.java
1 /*
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netvirt.bgpmanager.commands;
10
11 import java.util.List;
12 import org.apache.karaf.shell.commands.Argument;
13 import org.apache.karaf.shell.commands.Command;
14 import org.apache.karaf.shell.commands.Option;
15 import org.apache.karaf.shell.console.OsgiCommandSupport;
16 import org.opendaylight.netvirt.bgpmanager.BgpManager;
17 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.Bgp;
18 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.Neighbors;
19
20 @Command(scope = "odl", name = "bgp-rtr",
21         description = "Add or delete BGP router instance")
22 public class Router extends OsgiCommandSupport {
23     private static final String AS = "--as-number";
24     private static final String RID = "--router-id";
25     private static final String SP = "--stale-path-time";
26     private static final String FB = "--f-bit";
27
28     @Argument(name = "add|del", description = "The desired operation",
29             required = true, multiValued = false)
30     private String action = null;
31
32     @Option(name = AS, aliases = {"-a"},
33             description = "AS number",
34             required = false, multiValued = false)
35     private String asNum = null;
36
37     @Option(name = RID, aliases = {"-r"},
38             description = "Router ID",
39             required = false, multiValued = false)
40     private String rid = null;
41
42     @Option(name = SP, aliases = {"-s"},
43             description = "Stale-path time",
44             required = false, multiValued = false)
45     private String spt = null;
46
47     @Option(name = FB, aliases = {"-f"},
48             description = "F-bit",
49             required = false, multiValued = false)
50     private String fbit = null;
51
52     private Object usage() {
53         session.getConsole().println(
54                 "usage: bgp-rtr [" + AS + " as-number] [" + RID + " router-id] ["
55                         + SP + " stale-path-time] [" + FB + " on|off] <add | del>");
56         return null;
57     }
58
59     @Override
60     protected Object doExecute() throws Exception {
61         if (!Commands.bgpRunning(session.getConsole())) {
62             return null;
63         }
64         BgpManager bm = Commands.getBgpManager();
65         switch (action) {
66             case "add":
67                 // check: rtr already running?
68                 long asn = 0;
69                 int stalePath = 0;
70                 boolean fb = false;
71                 if (asNum == null) {
72                     session.getConsole().println("error: " + AS + " is needed");
73                     return null;
74                 }
75                 if (!Commands.isValid(session.getConsole(), asNum, Commands.Validators.INT, AS)) {
76                     return null;
77                 }
78                 asn = Long.valueOf(asNum);
79                 if (rid != null && !Commands.isValid(session.getConsole(), rid, Commands.Validators.IPADDR, RID)) {
80                     return null;
81                 }
82                 if (spt != null) {
83                     if (!Commands.isValid(session.getConsole(), spt, Commands.Validators.INT, SP)) {
84                         return null;
85                     } else {
86                         stalePath = Integer.valueOf(spt);
87                     }
88                 }
89                 if (fbit != null) {
90                     switch (fbit) {
91                         case "on":
92                             fb = true;
93                             break;
94                         case "off":
95                             fb = false;
96                             break;
97                         default:
98                             session.getConsole().println("error: " + FB + " must be on or off");
99                             return null;
100                     }
101                 }
102                 bm.startBgp(asn, rid, stalePath, fb);
103                 break;
104             case "del":
105                 // check: nothing to stop?
106                 if (asNum != null || rid != null || spt != null || fbit != null) {
107                     session.getConsole().println("note: option(s) not needed; ignored");
108                 }
109                 Bgp conf = bm.getConfig();
110                 if (conf == null) {
111                     session.getConsole().println("error : no BGP configs present");
112                     break;
113                 }
114                 List<Neighbors> nbrs = conf.getNeighbors();
115                 if (nbrs != null && nbrs.size() > 0) {
116                     session.getConsole().println("error: all BGP congiguration must be deleted "
117                             + "before stopping the router instance");
118                     break;
119                 }
120                 bm.stopBgp();
121                 break;
122             default:
123                 return usage();
124         }
125         return null;
126     }
127 }