Merge "Fixes for the below NAT issues : 1) After cluster reboot, SNAT traffic is...
[netvirt.git] / vpnservice / bgpmanager / bgpmanager-impl / src / main / java / org / opendaylight / netvirt / bgpmanager / commands / Network.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.*;
12 import org.apache.karaf.shell.console.OsgiCommandSupport;
13 import org.opendaylight.netvirt.bgpmanager.BgpManager;
14 import org.opendaylight.netvirt.bgpmanager.api.RouteOrigin;
15 import org.opendaylight.netvirt.bgpmanager.thrift.gen.qbgpConstants;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 import java.util.List;
20
21 @Command(scope = "odl", name = "bgp-network",
22          description = "Add or delete BGP static routes")
23 public class Network extends OsgiCommandSupport {
24     private static final String RD = "--rd";
25     private static final String PFX = "--prefix";
26     private static final String NH = "--nexthop";
27     private static final String LB = "--label";
28
29     static final Logger LOGGER = LoggerFactory.getLogger(Network.class);
30     @Argument(name="add|del", description="The desired operation",
31               required=true, multiValued = false)
32     private String action = null;
33
34     @Option(name=RD, aliases={"-r"},
35             description="Route distinguisher",
36             required=false, multiValued=false)
37     private String rd = null;
38
39     @Option(name=PFX, aliases={"-p"},
40             description="prefix/length",
41             required=false, multiValued=false)
42     private String pfx = null;
43
44     @Option(name=NH, aliases={"-n"},
45             description="Nexthop",
46             required=false, multiValued=true)
47     private List<String> nh = null;
48
49     @Option(name=LB, aliases={"-l"},
50             description="Label",
51             required=false, multiValued=false)
52     private String lbl = null;
53
54     private RouteOrigin staticOrigin = RouteOrigin.STATIC;
55
56     private Object usage() {
57         System.err.println(
58             "usage: bgp-network ["+RD+" rd] ["+PFX+" prefix/len] ["
59             +NH+" nexthop] ["+LB+" label] <add|del>");
60         return null;
61     }
62
63     @Override
64     protected Object doExecute() throws Exception {
65         if (!Commands.bgpRunning()) {
66             return null;
67         }
68         BgpManager bm = Commands.getBgpManager();
69         switch (action) {
70             case "add":
71                 int label = qbgpConstants.LBL_EXPLICIT_NULL;
72                 if (pfx == null ) {
73                     System.err.println("error: "+PFX+" is needed");
74                     return null;
75                 }
76                 if (nh == null) {
77                     System.err.println("error: "+NH+" is needed");
78                     return null;
79                 }
80                 //TODO: syntactic validation of prefix
81                 for (String nextHop : nh) {
82                     if (!Commands.isValid(nextHop, Commands.IPADDR, NH)) {
83                         return null;
84                     }
85                 }
86                 if (lbl != null) {
87                     if (!Commands.isValid(lbl, Commands.INT, LB)) {
88                         return null;
89                     } else {
90                         label = Integer.valueOf(lbl);
91                     }
92                 } else if (rd == null) {
93                     System.err.println("error: "+RD+" is needed");
94                     return null;
95                 }
96                 LOGGER.info("ADD: Adding Fib entry rd {} prefix {} nexthop {} label {}", rd, pfx, nh, label);
97                 bm.addPrefix(rd, pfx, nh, label, staticOrigin);
98                 LOGGER.info("ADD: Added Fib entry rd {} prefix {} nexthop {} label {}", rd, pfx, nh, label);
99                 break;
100             case "del":
101                 if (pfx == null) {
102                     System.err.println("error: "+PFX+" is needed");
103                     return null;
104                 }
105                 if (nh != null || lbl != null) {
106                     System.err.println("note: some option(s) not needed; ignored");
107                 }
108                 LOGGER.info("REMOVE: Removing Fib entry rd {} prefix {}", rd, pfx);
109                 bm.deletePrefix(rd, pfx);
110                 LOGGER.info("REMOVE: Removed Fib entry rd {} prefix {}", rd, pfx);
111                 break;
112             default:
113                 return usage();
114         }
115         return null;
116     }
117 }