MRI version bumpup for Aluminium
[netvirt.git] / 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.Map;
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.neighborscontainer.Neighbors;
19 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.neighborscontainer.NeighborsKey;
20
21 @Command(scope = "odl", name = "bgp-rtr",
22         description = "Add or delete BGP router instance")
23 public class Router extends OsgiCommandSupport {
24     private static final String AS = "--as-number";
25     private static final String RID = "--router-id";
26     private static final String SP = "--stale-path-time";
27     private static final String FB = "--f-bit";
28
29     @Argument(name = "add|del", description = "The desired operation",
30             required = true, multiValued = false)
31     private final String action = null;
32
33     @Option(name = AS, aliases = {"-a"},
34             description = "AS number",
35             required = false, multiValued = false)
36     private final String asNum = null;
37
38     @Option(name = RID, aliases = {"-r"},
39             description = "Router ID",
40             required = false, multiValued = false)
41     private final String rid = null;
42
43     @Option(name = SP, aliases = {"-s"},
44             description = "Stale-path time",
45             required = false, multiValued = false)
46     private final String spt = null;
47
48     @Option(name = FB, aliases = {"-f"},
49             description = "F-bit",
50             required = false, multiValued = false)
51     private final String fbit = null;
52
53     private final BgpManager bgpManager;
54
55     public Router(BgpManager bgpManager) {
56         this.bgpManager = bgpManager;
57     }
58
59     private Object usage() {
60         session.getConsole().println(
61                 "usage: bgp-rtr [" + AS + " as-number] [" + RID + " router-id] ["
62                         + SP + " stale-path-time] [" + FB + " on|off] <add | del>");
63         return null;
64     }
65
66     @Override
67     protected Object doExecute() {
68         switch (action) {
69             case "add":
70                 // check: rtr already running?
71                 long asn = 0;
72                 int stalePath = 0;
73                 boolean fb = false;
74                 if (asNum == null) {
75                     session.getConsole().println("error: " + AS + " is needed");
76                     return null;
77                 }
78                 if (!Commands.isValid(session.getConsole(), asNum, Commands.Validators.ASNUM, AS)) {
79                     return null;
80                 }
81                 asn = Long.parseLong(asNum);
82                 if (rid != null && !Commands.isValid(session.getConsole(), rid, Commands.Validators.IPADDR, RID)) {
83                     return null;
84                 }
85                 if (spt != null) {
86                     if (!Commands.isValid(session.getConsole(), spt, Commands.Validators.INT, SP)) {
87                         return null;
88                     } else {
89                         stalePath = Integer.parseInt(spt);
90                     }
91                 }
92                 if (fbit != null) {
93                     switch (fbit) {
94                         case "on":
95                             fb = true;
96                             break;
97                         case "off":
98                             fb = false;
99                             break;
100                         default:
101                             session.getConsole().println("error: " + FB + " must be on or off");
102                             return null;
103                     }
104                 }
105                 bgpManager.startBgp(asn, rid, stalePath, fb);
106                 break;
107             case "del":
108                 // check: nothing to stop?
109                 if (asNum != null || rid != null || spt != null || fbit != null) {
110                     session.getConsole().println("note: option(s) not needed; ignored");
111                 }
112                 Bgp conf = bgpManager.getConfig();
113                 if (conf == null) {
114                     session.getConsole().println("error : no BGP configs present");
115                     break;
116                 }
117                 Map<NeighborsKey, Neighbors> keyNeighborsMap = conf.getNeighborsContainer() == null ? null
118                         : conf.getNeighborsContainer().getNeighbors();
119                 if (keyNeighborsMap != null && keyNeighborsMap.size() > 0) {
120                     session.getConsole().println("error: all BGP congiguration must be deleted "
121                             + "before stopping the router instance");
122                     break;
123                 }
124                 bgpManager.stopBgp();
125                 break;
126             default:
127                 return usage();
128         }
129         return null;
130     }
131 }