Merge "Upgrade ietf-{inet,yang}-types to 2013-07-15"
[vpnservice.git] / bgpmanager / bgpmanager-impl / src / main / java / org / opendaylight / 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.bgpmanager.commands;
10
11 import org.apache.karaf.shell.commands.*;
12 import org.apache.karaf.shell.console.OsgiCommandSupport;
13 import org.opendaylight.bgpmanager.BgpManager;
14
15 @Command(scope = "odl", name = "bgp-rtr", 
16          description = "Add or delete BGP router instance")
17 public class Router extends OsgiCommandSupport {
18     private static final String AS = "--as-number";
19     private static final String RID = "--router-id";
20     private static final String SP = "--stale-path-time";
21     private static final String FB = "--f-bit";
22
23     @Argument(name="add|del", description="The desired operation", 
24               required=true, multiValued = false)
25     private String action = null;
26
27     @Option(name=AS, aliases={"-a"}, 
28             description="AS number", 
29             required=false, multiValued=false)
30     private String asNum = null;
31
32     @Option(name=RID, aliases={"-r"},
33             description="Router ID", 
34             required=false, multiValued=false)
35     private String rid = null;
36
37     @Option(name=SP, aliases={"-s"},
38             description="Stale-path time", 
39             required=false, multiValued=false)
40     private String spt = null;
41
42     @Option(name=FB, aliases={"-f"},
43             description="F-bit", 
44             required=false, multiValued=false)
45     private String fbit = null;
46
47     private Object usage() {
48         System.err.println(
49             "usage: bgp-rtr ["+AS+" as-number] ["+RID+" router-id] ["
50             +SP+" stale-path-time] ["+FB+" on|off] <add | del>");
51         return null;
52     }       
53
54     @Override
55     protected Object doExecute() throws Exception {
56         if (!Commands.bgpRunning()) {
57             return null;
58         }
59         BgpManager bm = Commands.getBgpManager();
60         switch (action) {
61             case "add" : 
62                 // check: rtr already running?
63                 int asn = 0;
64                 int s = 0;
65                 boolean fb = false; 
66                 if (asNum == null) {
67                     System.err.println("error: "+AS+" is needed");
68                     return null;
69                 }
70                 if (!Commands.isValid(asNum, Commands.INT, AS)) {
71                     return null;
72                 }
73                 asn = Integer.valueOf(asNum);
74                 if (rid != null && 
75                 !Commands.isValid(rid, Commands.IPADDR, RID)) {
76                     return null;
77                 }
78                 if (spt != null) {
79                     if (!Commands.isValid(spt, Commands.INT, SP)) {
80                         return null;
81                     } else {
82                         s = Integer.valueOf(spt);
83                     }
84                 }
85                 if (fbit != null) {
86                     switch (fbit) {
87                         case "on": 
88                             fb = true;
89                             break;
90                         case "off": 
91                             fb = false;
92                             break;
93                         default: 
94                             System.err.println("error: "+FB+" must be on or off");
95                             return null;
96                     }
97                 } 
98                 bm.startBgp(asn, rid, s, fb);
99                 break;
100             case "del" : 
101                 // check: nothing to stop?
102                 if (asNum != null || rid != null || spt != null ||
103                 fbit != null) {
104                     System.err.println("note: option(s) not needed; ignored");
105                 }
106                 bm.stopBgp();
107                 break;
108             default :  
109                 return usage();
110         }
111         return null;
112     }
113 }