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