Merge "Upgrade ietf-{inet,yang}-types to 2013-07-15"
[vpnservice.git] / bgpmanager / bgpmanager-impl / src / main / java / org / opendaylight / bgpmanager / commands / Neighbor.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.af_afi;
15 import org.opendaylight.bgpmanager.thrift.gen.af_safi;
16
17 @Command(scope = "odl", name = "bgp-nbr", 
18          description = "Add or delete BGP neighbor")
19 public class Neighbor extends OsgiCommandSupport {
20     private static final String IP = "--ip-address";
21     private static final String AS = "--as-number";
22     private static final String MH = "--ebgp-multihop";
23     private static final String US = "--update-source";
24     private static final String AF = "--address-family";
25    
26     @Argument(index=0, name="add|del", description="The desired operation", 
27               required=true, multiValued = false)
28     String action = null;
29
30     @Option(name=IP, aliases = {"-i"}, 
31             description="Neighbor's IP address", 
32             required=false, multiValued=false)
33     String nbrIp = null; 
34
35     @Option(name=AS, aliases = {"-a"},  
36             description="AS number", 
37             required=false, multiValued=false)
38     String asNum = null;
39
40     @Option(name=MH, aliases = {"-e"},  
41             description="EBGP-multihop hops", 
42             required=false, multiValued=false)
43     String nHops = null;
44
45     @Option(name=US, aliases = {"-u"},  
46             description="Update source address", 
47             required=false, multiValued=false)
48     String srcIp = null;
49
50     @Option(name=AF, aliases = {"-f"},  
51             description="Address family", 
52             required=false, multiValued=false)
53     String addrFamily = null;
54
55     private Object usage() {
56         System.err.println(
57             "usage: bgp-nbr ["+IP+" nbr-ip-address] ["+AS+" asnum] ["
58             +MH+" hops] ["+US+" source] ["+AF+" lu] <add|del>");
59         return null;
60     }       
61
62     @Override
63     protected Object doExecute() throws Exception {
64         if (!Commands.bgpRunning()) {
65             return null;
66         }
67         BgpManager bm = Commands.getBgpManager();
68         switch (action) {
69             case "add" :  
70                 if (nbrIp == null) {
71                     System.err.println("error: "+IP+" needed");
72                     return null;
73                 }
74                 int asn = bm.getConfig().getAsId().getLocalAs().intValue(); 
75                 int hops = 0;
76                 if (!Commands.isValid(nbrIp, Commands.IPADDR, IP)) {
77                     return null;
78                 }
79                 if (asNum != null) {
80                     if (!Commands.isValid(asNum, Commands.INT, AS)) {
81                         return null;
82                     } else {
83                         asn = Integer.valueOf(asNum);
84                     }
85                 }
86                 bm.addNeighbor(nbrIp, asn);
87                 if (nHops != null) {
88                     if (!Commands.isValid(nHops, Commands.INT, MH)) {
89                         return null;
90                     } else {
91                         hops = Integer.valueOf(nHops);
92                     }
93                     bm.addEbgpMultihop(nbrIp, hops);
94                 }
95                 if (srcIp != null) { 
96                     if (!Commands.isValid(srcIp, Commands.IPADDR, US)) {
97                         return null;
98                     }
99                     bm.addUpdateSource(nbrIp, srcIp);
100                 }
101                 if (addrFamily != null) {
102                     if (!addrFamily.equals("lu"))  {
103                         System.err.println("error: "+AF+" must be lu");
104                         return null;
105                     }
106                     af_afi afi = af_afi.findByValue(1);
107                     af_safi safi = af_safi.findByValue(4);
108                     bm.addAddressFamily(nbrIp, afi, safi); 
109                 }
110                 break;
111             case "del" :  
112                 if (nbrIp == null) {
113                     System.err.println("error: "+IP+" needed");
114                     return null;
115                 }
116                 if (!Commands.isValid(nbrIp, Commands.IPADDR, IP)) {
117                     return null;
118                 }
119                 if (asNum != null || nHops != null || srcIp != null
120                 || addrFamily != null) {
121                     System.err.println("note: some option(s) not needed; ignored");
122                 }
123                 bm.deleteNeighbor(nbrIp);
124                 break;
125             default :  
126                 return usage();
127         }
128         return null;
129     }
130 }