Fix build faliures due to OFPlugin checktyle fixes
[netvirt.git] / vpnservice / bgpmanager / bgpmanager-impl / src / main / java / org / opendaylight / netvirt / bgpmanager / commands / Vrf.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 java.util.List;
12 import org.apache.karaf.shell.commands.Argument;
13 import org.apache.karaf.shell.commands.Command;
14 import org.apache.karaf.shell.commands.Option;
15 import org.apache.karaf.shell.console.OsgiCommandSupport;
16 import org.opendaylight.netvirt.bgpmanager.BgpManager;
17 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.AddressFamily;
18
19 @Command(scope = "odl", name = "bgp-vrf",
20         description = "Add or delete BGP VRFs")
21 public class Vrf extends OsgiCommandSupport {
22     private static final String RD = "--rd";
23     private static final String IR = "--import-rts";
24     private static final String ER = "--export-rts";
25     private static final String ADDRF = "--addr-family";
26
27     @Argument(name = "add|del", description = "The desired operation",
28             required = true, multiValued = false)
29     private final String action = null;
30
31     @Option(name = RD, aliases = {"-r"},
32             description = "Route distinguisher",
33             required = false, multiValued = false)
34     private final String rd = null;
35
36     @Option(name = IR, aliases = {"-i"},
37             description = "Import route-targets",
38             required = false, multiValued = true)
39     private final List<String> irts = null;
40
41     @Option(name = ER, aliases = {"-e"},
42             description = "Export route-targets",
43             required = false, multiValued = true)
44     private final List<String> erts = null;
45
46
47     @Option(name = ADDRF, aliases = {"-af"},
48             description = "AddressFamily IPV4 or IPV6 or L2VPN (IPv(4 or 6) is on MPLS, L2VPN uses IPV4)",
49             required = false, multiValued = false)
50     private final String addrf = null;
51
52     private final BgpManager bgpManager;
53
54     public Vrf(BgpManager bgpManager) {
55         this.bgpManager = bgpManager;
56     }
57
58     private Object usage() {
59         session.getConsole().println(
60                 "usage: bgp-vrf [" + RD + " rd] [<" + IR + " | " + ER + "> rt1] .. [<" + IR + " | " + ER
61                         + "> rtN] [" + ADDRF + " AddressFamily] <add|del>");
62         return null;
63     }
64
65     @Override
66     protected Object doExecute() throws Exception {
67         AddressFamily af = null;
68         if (addrf.compareToIgnoreCase("IPV_4") == 0) {
69             af = AddressFamily.IPV4;
70         } else if (addrf.compareToIgnoreCase("IPV_6") == 0) {
71             af = AddressFamily.IPV6;
72         } else if (addrf.compareToIgnoreCase("L2VPN") == 0) {
73             af = AddressFamily.L2VPN;
74         } else {
75             return usage();
76         }
77         switch (action) {
78             case "add":
79                 if (rd == null || irts == null || erts == null) {
80                     session.getConsole().println("error: all options needed");
81                     return null;
82                 }
83                 // check: rd exists? rd & rt's in format?
84                 bgpManager.addVrf(rd, irts, erts, af);
85                 break;
86             case "del":
87                 if (rd == null) {
88                     session.getConsole().println("error: " + RD + " needed");
89                     return null;
90                 }
91                 if (irts != null || erts != null) {
92                     session.getConsole().println("error: some option(s) not needed; ignored");
93                 }
94                 // check: rd exists? in format?
95                 bgpManager.deleteVrf(rd, true, af);
96                 break;
97             default:
98                 return usage();
99         }
100         return null;
101     }
102 }