move vpnservice and cleanup poms
[netvirt.git] / 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.Argument;
12 import org.apache.karaf.shell.commands.Command;
13 import org.apache.karaf.shell.commands.Option;
14 import org.apache.karaf.shell.console.OsgiCommandSupport;
15 import org.opendaylight.netvirt.bgpmanager.BgpManager;
16 import org.opendaylight.netvirt.bgpmanager.thrift.gen.af_afi;
17 import org.opendaylight.netvirt.bgpmanager.thrift.gen.af_safi;
18 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.TcpMd5SignaturePasswordType;
19
20 @Command(scope = "odl", name = "bgp-nbr",
21         description = "Add or delete BGP neighbor")
22 public class Neighbor extends OsgiCommandSupport {
23     private static final String IP = "--ip-address";
24     private static final String AS = "--as-number";
25     private static final String MH = "--ebgp-multihop";
26     private static final String US = "--update-source";
27     private static final String AF = "--address-family";
28     private static final String MP = "--tcp-md5-password";
29
30     private static final String USAGE = new StringBuilder("usage: bgp-nbr")
31             .append(" [").append(IP).append(" nbr-ip-address]")
32             .append(" [").append(AS).append(" asnum]")
33             .append(" [").append(MP).append(" md5-shared-secret]")
34             .append(" [").append(MH).append(" hops]")
35             .append(" [").append(US).append(" source]")
36             .append(" [").append(AF).append(" lu/evpn/vpnv4/vpnv6]")
37             .append(" <add|del>").toString();
38
39     @Argument(index = 0, name = "add|del", description = "The desired operation",
40             required = true, multiValued = false)
41     String action = null;
42
43     @Option(name = IP, aliases = {"-i"},
44             description = "Neighbor's IP address",
45             required = false, multiValued = false)
46     String nbrIp = null;
47
48     @Option(name = AS, aliases = {"-a"},
49             description = "AS number",
50             required = false, multiValued = false)
51     String asNum = null;
52
53     @Option(name = MP, aliases = {"-p"},
54             description = "TCP MD5 Signature Option shared secret",
55             required = false, multiValued = false)
56     String md5PasswordOption = null;
57
58     @Option(name = MH, aliases = {"-e"},
59             description = "EBGP-multihop hops",
60             required = false, multiValued = false)
61     String multiHops = null;
62
63     @Option(name = US, aliases = {"-u"},
64             description = "Update source address",
65             required = false, multiValued = false)
66     String srcIp = null;
67
68     @Option(name = AF, aliases = {"-f"},
69             description = "Address family",
70             required = false, multiValued = false)
71     String addrFamily = null;
72
73     private final BgpManager bgpManager;
74
75     public Neighbor(BgpManager bgpManager) {
76         this.bgpManager = bgpManager;
77     }
78
79     private Object usage() {
80         session.getConsole().println(USAGE);
81         return null;
82     }
83
84     @Override
85     protected Object doExecute() throws Exception {
86         switch (action) {
87             case "add":
88                 if (nbrIp == null) {
89                     session.getConsole().println("error: " + IP + " needed");
90                     return null;
91                 }
92                 if (bgpManager.getConfig() == null) {
93                     session.getConsole().println("error: Bgp config is not present");
94                     return null;
95                 }
96                 long asn = 0;
97                 int hops = 0;
98                 if (!Commands.isValid(session.getConsole(), nbrIp, Commands.Validators.IPADDR, IP)) {
99                     return null;
100                 }
101                 if (asNum != null) {
102                     if (!Commands.isValid(session.getConsole(), asNum, Commands.Validators.INT, AS)) {
103                         return null;
104                     } else {
105                         asn = Long.parseLong(asNum);
106                     }
107                 }
108                 TcpMd5SignaturePasswordType md5Secret = null;
109                 if (md5PasswordOption != null) {
110                     try {
111                         md5Secret = new TcpMd5SignaturePasswordType(md5PasswordOption);
112                     } catch (IllegalArgumentException e) {
113                         session.getConsole().println(
114                                 new StringBuilder("error: invalid MD5 password ").append(e.getMessage()).toString());
115                         return null;
116                     }
117                 }
118                 bgpManager.addNeighbor(nbrIp, asn, md5Secret);
119
120                 if (multiHops != null) {
121                     if (!Commands.isValid(session.getConsole(), multiHops, Commands.Validators.INT, MH)) {
122                         return null;
123                     } else {
124                         hops = Integer.parseInt(multiHops);
125                     }
126                     bgpManager.addEbgpMultihop(nbrIp, hops);
127                 }
128                 if (srcIp != null) {
129                     if (!Commands.isValid(session.getConsole(), srcIp, Commands.Validators.IPADDR, US)) {
130                         return null;
131                     }
132                     bgpManager.addUpdateSource(nbrIp, srcIp);
133                 }
134                 if (addrFamily != null) {
135                     if (!addrFamily.equals("lu") && !addrFamily.equals("vpnv4")
136                             && !addrFamily.equals("vpnv6")
137                             && !addrFamily.equals("evpn")) {
138                         session.getConsole().println("error: " + AF + " must be lu/evpn/vpnv4/vpnv6 ");
139                         return null;
140                     }
141                     af_afi afi;
142                     af_safi safi;
143                     if (addrFamily.equals("vpnv6")) {
144                         afi = af_afi.findByValue(2);
145                         safi = af_safi.findByValue(5);
146                     } else if (addrFamily.equals("evpn")) {
147                         afi = af_afi.findByValue(3);
148                         safi = af_safi.findByValue(6);
149                     } else if (addrFamily.equals("lu")) {
150                         afi = af_afi.findByValue(1);
151                         safi = af_safi.findByValue(4);
152                     } else { // vpnv4
153                         afi = af_afi.findByValue(1);
154                         safi = af_safi.findByValue(5);
155                     }
156                     bgpManager.addAddressFamily(nbrIp, afi, safi);
157                 }
158                 break;
159             case "del":
160                 if (nbrIp == null) {
161                     session.getConsole().println("error: " + IP + " needed");
162                     return null;
163                 }
164                 if (!Commands.isValid(session.getConsole(), nbrIp, Commands.Validators.IPADDR, IP)) {
165                     return null;
166                 }
167                 if (asNum != null || multiHops != null || srcIp != null
168                         || addrFamily != null) {
169                     session.getConsole().println("note: some option(s) not needed; ignored");
170                 }
171                 bgpManager.deleteNeighbor(nbrIp);
172                 break;
173             default:
174                 return usage();
175         }
176         return null;
177     }
178 }