Merge "Upgrade ietf-{inet,yang}-types to 2013-07-15"
[vpnservice.git] / bgpmanager / bgpmanager-impl / src / main / java / org / opendaylight / bgpmanager / commands / Network.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 import org.opendaylight.bgpmanager.thrift.gen.qbgpConstants;
15
16 @Command(scope = "odl", name = "bgp-network", 
17          description = "Add or delete BGP static routes")
18 public class Network extends OsgiCommandSupport {
19     private static final String RD = "--rd";
20     private static final String PFX = "--prefix";
21     private static final String NH = "--nexthop";
22     private static final String LB = "--label";
23
24     @Argument(name="add|del", description="The desired operation", 
25               required=true, multiValued = false)
26     private String action = null;
27
28     @Option(name=RD, aliases={"-r"}, 
29             description="Route distinguisher", 
30             required=false, multiValued=false)
31     private String rd = null;
32
33     @Option(name=PFX, aliases={"-p"},
34             description="prefix/length", 
35             required=false, multiValued=false)
36     private String pfx = null;
37
38     @Option(name=NH, aliases={"-n"},
39             description="Nexthop", 
40             required=false, multiValued=false)
41     private String nh = null;
42
43     @Option(name=LB, aliases={"-l"},
44             description="Label", 
45             required=false, multiValued=false)
46     private String lbl = null;
47
48     private Object usage() {
49         System.err.println(
50             "usage: bgp-network ["+RD+" rd] ["+PFX+" prefix/len] ["
51             +NH+" nexthop] ["+LB+" label] <add|del>");
52         return null;
53     }       
54
55     @Override
56     protected Object doExecute() throws Exception {
57         if (!Commands.bgpRunning()) {
58             return null;
59         }
60         BgpManager bm = Commands.getBgpManager();
61         switch (action) {
62             case "add" : 
63                 int label = qbgpConstants.LBL_EXPLICIT_NULL;
64                 if (pfx == null ) {
65                     System.err.println("error: "+PFX+" is needed");
66                     return null;
67                 }
68                 if (nh == null) {
69                     System.err.println("error: "+NH+" is needed");
70                     return null;
71                 }
72                 //todo: syntactic validation of prefix
73                 if (!Commands.isValid(nh, Commands.IPADDR, NH)) {
74                     return null;
75                 }
76                 if (lbl != null) {
77                     if (!Commands.isValid(lbl, Commands.INT, LB)) {
78                         return null;
79                     } else {
80                         label = Integer.valueOf(lbl);
81                     } 
82                 } else if (rd == null) {
83                     System.err.println("error: "+RD+" is needed");
84                     return null;
85                 }
86                 bm.addPrefix(rd, pfx, nh, label); 
87                 break;
88             case "del" :  
89                 if (pfx == null) {
90                     System.err.println("error: "+PFX+" is needed");
91                     return null;
92                 }
93                 if (nh != null || lbl != null) {
94                     System.err.println("note: some option(s) not needed; ignored");
95                 }
96                 bm.deletePrefix(rd, pfx);
97                 break;
98             default :  
99                 return usage();
100         }
101         return null;
102     }
103 }