a8b19b677709c22f473512896c324f641995b044
[netvirt.git] / bgpmanager / impl / src / main / java / org / opendaylight / netvirt / bgpmanager / commands / Multipath.java
1 /*
2  * Copyright (c) 2017 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.omg.CORBA.Object;
16 import org.opendaylight.netvirt.bgpmanager.BgpConfigurationManager;
17 import org.opendaylight.netvirt.bgpmanager.thrift.gen.af_afi;
18 import org.opendaylight.netvirt.bgpmanager.thrift.gen.af_safi;
19
20 @Command(scope = "odl", name = "multipath", description = "Enable/Disable multipaths")
21 public class Multipath extends OsgiCommandSupport {
22
23     private static final String AF = "--address-family";
24     private static final String RD = "--rd";
25     private static final String MAXPATH = "--maxpath";
26     private static final int MIN_MAXPATH = 1;
27     private static final int MAX_MAXPATH = 64;
28
29     @Option(name = RD, aliases = { "-r" },
30             description = "rd",
31             required = false,
32             multiValued = false)
33     String rd;
34
35     @Option(name = MAXPATH, aliases = { "-n" },
36             description = "max number of paths",
37             required = false,
38             multiValued = false)
39     String maxpath;
40
41     @Option(name = AF, aliases = {"-f"},
42             description = "Address family",
43             required = false, multiValued = false)
44
45     String addrFamily;
46
47
48     @Argument(name = "enable|disable|setmaxpath",
49             description = "The desired operation",
50             required = true, multiValued = false)
51
52     String multipathEnable;
53
54     private final BgpConfigurationManager bgpConfigurationManager;
55
56     public Multipath(BgpConfigurationManager bgpConfigurationManager) {
57         this.bgpConfigurationManager = bgpConfigurationManager;
58     }
59
60     @Override
61     protected Object doExecute() {
62         af_afi afi = af_afi.findByValue(1);
63         af_safi safi = af_safi.findByValue(5);
64
65         if (addrFamily != null) {
66             if (!addrFamily.equals("lu") && !addrFamily.equals("vpnv6")
67                  && !addrFamily.equals("evpn") && !addrFamily.equals("vpnv4")) {
68                 session.getConsole().println("error: " + AF + " must be lu/evpn/vpnv4/vpnv6 ");
69                 return null;
70             }
71             if (addrFamily.equals("vpnv6")) {
72                 afi = af_afi.findByValue(2);
73                 safi = af_safi.findByValue(5);
74             } else if (addrFamily.equals("evpn")) {
75                 afi = af_afi.findByValue(3);
76                 safi = af_safi.findByValue(6);
77             } else if (addrFamily.equals("lu")) {
78                 afi = af_afi.findByValue(1);
79                 safi = af_safi.findByValue(4);
80             } else { // vpnv4
81                 afi = af_afi.findByValue(1);
82                 safi = af_safi.findByValue(5);
83             }
84         }
85
86         if (maxpath != null) {
87             int imaxpath = Integer.parseInt(maxpath);
88             if (imaxpath < MIN_MAXPATH || imaxpath > MAX_MAXPATH) {
89                 session.getConsole().println("error: " + MAXPATH + " range[" + MIN_MAXPATH + " - " + MAX_MAXPATH + "]");
90                 return null;
91             }
92         }
93
94         if (multipathEnable != null) {
95
96             switch (multipathEnable) {
97                 case "enable":
98                     bgpConfigurationManager.setMultipathStatus(afi, safi, true);
99                     break;
100                 case "disable":
101                     bgpConfigurationManager.setMultipathStatus(afi, safi, false);
102                     break;
103                 case "setmaxpath":
104                     if (rd != null && maxpath != null) {
105                         bgpConfigurationManager.setMultipaths(rd, Integer.parseInt(maxpath));
106                     }
107                     break;
108
109                 default:
110                     return usage();
111             }
112         }
113
114         return null;
115     }
116
117     private Object usage() {
118         session.getConsole().println("odl:multipath  -f lu <enable|disable> \n"
119                 + "odl:multipath -f lu -r <rd> -n <maxpath> setmaxpath");
120         return null;
121     }
122 }
123