Enhancements in BGP module
[netvirt.git] / bgpmanager / impl / src / main / java / org / opendaylight / netvirt / bgpmanager / commands / Bfd.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 static org.opendaylight.netvirt.bgpmanager.oam.BgpConstants.MAX_DETECT_MULT;
12 import static org.opendaylight.netvirt.bgpmanager.oam.BgpConstants.MIN_DETECT_MULT;
13 import static org.opendaylight.netvirt.bgpmanager.oam.BgpConstants.MIN_RX_MAX;
14 import static org.opendaylight.netvirt.bgpmanager.oam.BgpConstants.MIN_RX_MIN;
15 import static org.opendaylight.netvirt.bgpmanager.oam.BgpConstants.MIN_TX_MAX;
16 import static org.opendaylight.netvirt.bgpmanager.oam.BgpConstants.MIN_TX_MIN;
17
18 import java.io.PrintStream;
19
20 import org.apache.karaf.shell.commands.Argument;
21 import org.apache.karaf.shell.commands.Command;
22 import org.apache.karaf.shell.commands.Option;
23 import org.apache.karaf.shell.console.OsgiCommandSupport;
24 import org.opendaylight.netvirt.bgpmanager.BgpManager;
25 import org.opendaylight.netvirt.bgpmanager.oam.BgpConstants;
26
27 @Command(scope = "odl", name = "bfd-config",
28          description = "Add or delete BFD neighbor")
29 public class Bfd extends OsgiCommandSupport {
30     private static final String RX = "--min-rx";
31     private static final String TX = "--min-tx";
32     private static final String DM = "--detect-mult";
33     private static final String MH = "--multi-hop";
34
35     @Argument(index = 0, name = "add|del", description = "The desired operation",
36               required = true, multiValued = false)
37     String action = null;
38
39     @Option(name = RX, aliases = {"-r"},
40             description = "Minimum BFD receive interval in millisec"
41                     + "<" + MIN_RX_MIN + "-" + MIN_RX_MAX + ">",
42             required = false, multiValued = false)
43     String minRX = null;
44
45     @Option(name = TX, aliases = {"-t"},
46             description = "Minimum BFD transmit interval in millisec"
47             + "<" + MIN_TX_MIN + "-" + MIN_TX_MAX + ">",
48             required = false, multiValued = false)
49     String minTX = null;
50
51     @Option(name = DM, aliases = {"-d"},
52             description = "No of packet miss for marking session down"
53             + "<" + MIN_DETECT_MULT + "-" + MAX_DETECT_MULT + ">",
54             required = false, multiValued = false)
55     String detectMult = null;
56
57     @Option(name = MH, aliases = {"-m"},
58             description = "Multi-Hop or Single-Hop BFD"
59             + "<true/false>",
60             required = false, multiValued = false)
61     String multiHop = null;
62
63     private final BgpManager bgpManager;
64
65     public Bfd(BgpManager bgpManager) {
66         this.bgpManager = bgpManager;
67     }
68
69     private Object usage() {
70         session.getConsole().println(
71             "usage: bgp-config [" + RX + " min-rx-interval] [" + TX + " min-tx-interval] ["
72             + DM + " detect-multiplier] [" + MH + " true|false] <add|del>");
73         return null;
74     }
75
76     @Override
77     protected Object doExecute() throws Exception {
78         PrintStream ps = session.getConsole();
79         switch (action) {
80             case "add" :
81                 int minrx = BgpConstants.BFD_DEFAULT_MIN_RX;
82                 int mintx = BgpConstants.BFD_DEFAULT_MIN_TX;
83                 int detectmult = BgpConstants.BFD_DEFAULT_DETECT_MULT;
84                 boolean multihop = true;
85                 if (minRX != null) {
86                     if (!Commands.isValid(ps, minRX, Commands.Validators.INT, RX)) {
87                         return null;
88                     } else {
89                         minrx = Integer.parseInt(minRX);
90                         if (minrx < MIN_RX_MIN || minrx > MIN_RX_MAX) {
91                             ps.println("error: value of RX should be between 50 and 50000");
92                             return null;
93                         }
94                     }
95                 }
96
97                 if (minTX != null) {
98                     if (!Commands.isValid(ps, minTX, Commands.Validators.INT, TX)) {
99                         return null;
100                     } else {
101                         mintx = Integer.parseInt(minTX);
102                         if (mintx < MIN_TX_MIN || mintx > MIN_TX_MAX) {
103                             ps.println("error: value of TX should be between 1000 and 4294000");
104                             return null;
105                         }
106                     }
107                 }
108
109                 if (detectMult != null) {
110                     if (!Commands.isValid(ps, detectMult, Commands.Validators.INT, DM)) {
111                         return null;
112                     } else {
113                         detectmult = Integer.parseInt(detectMult);
114                         if (detectmult < MIN_DETECT_MULT || detectmult > MAX_DETECT_MULT) {
115                             ps.println("error: value of detectMult should be between 2 to 255");
116                             return null;
117                         }
118                     }
119                 }
120
121                 if (multiHop != null) {
122                     if (!multiHop.equals("true") && !multiHop.equals("false")) {
123                         ps.println("error: " + MH + "must be true or false");
124                         return null;
125                     }
126                     if (multiHop.equals("false")) {
127                         multihop = false;
128                     }
129                 }
130
131                 bgpManager.startBfd(detectmult, minrx, mintx, multihop);
132
133                 break;
134             case "del" :
135                 if (detectMult != null || minRX != null || minTX != null || multiHop != null) {
136                     session.getConsole().println("note: some option(s) not needed; ignored");
137                 }
138                 bgpManager.stopBfd();
139                 break;
140             default :
141                 return usage();
142         }
143         return null;
144     }
145 }