BGPManager module sync up
[netvirt.git] / vpnservice / bgpmanager / bgpmanager-impl / src / main / java / org / opendaylight / netvirt / 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.netvirt.bgpmanager.commands;
10
11 import org.apache.karaf.shell.commands.*;
12 import org.apache.karaf.shell.console.OsgiCommandSupport;
13 import org.opendaylight.netvirt.bgpmanager.BgpManager;
14 import org.opendaylight.netvirt.bgpmanager.thrift.gen.af_afi;
15 import org.opendaylight.netvirt.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                 if (bm.getConfig() == null) {
75                     System.err.println("error: Bgp config is not present");
76                     return null;
77                 }
78                 int asn = bm.getConfig().getAsId().getLocalAs().intValue(); 
79                 int hops = 0;
80                 if (!Commands.isValid(nbrIp, Commands.IPADDR, IP)) {
81                     return null;
82                 }
83                 if (asNum != null) {
84                     if (!Commands.isValid(asNum, Commands.INT, AS)) {
85                         return null;
86                     } else {
87                         asn = Integer.valueOf(asNum);
88                     }
89                 }
90                 bm.addNeighbor(nbrIp, asn);
91                 if (nHops != null) {
92                     if (!Commands.isValid(nHops, Commands.INT, MH)) {
93                         return null;
94                     } else {
95                         hops = Integer.valueOf(nHops);
96                     }
97                     bm.addEbgpMultihop(nbrIp, hops);
98                 }
99                 if (srcIp != null) { 
100                     if (!Commands.isValid(srcIp, Commands.IPADDR, US)) {
101                         return null;
102                     }
103                     bm.addUpdateSource(nbrIp, srcIp);
104                 }
105                 if (addrFamily != null) {
106                     if (!addrFamily.equals("lu"))  {
107                         System.err.println("error: "+AF+" must be lu");
108                         return null;
109                     }
110                     af_afi afi = af_afi.findByValue(1);
111                     af_safi safi = af_safi.findByValue(4);
112                     bm.addAddressFamily(nbrIp, afi, safi); 
113                 }
114                 break;
115             case "del" :  
116                 if (nbrIp == null) {
117                     System.err.println("error: "+IP+" needed");
118                     return null;
119                 }
120                 if (!Commands.isValid(nbrIp, Commands.IPADDR, IP)) {
121                     return null;
122                 }
123                 if (asNum != null || nHops != null || srcIp != null
124                 || addrFamily != null) {
125                     System.err.println("note: some option(s) not needed; ignored");
126                 }
127                 bm.deleteNeighbor(nbrIp);
128                 break;
129             default :  
130                 return usage();
131         }
132         return null;
133     }
134 }