bgpmanager VPNv6 shell command update as well as OAM changes 55/56655/5
authorPhilippe Guibert <philippe.guibert@6wind.com>
Tue, 11 Apr 2017 16:10:50 +0000 (17:10 +0100)
committerSam Hague <shague@redhat.com>
Tue, 9 May 2017 12:31:07 +0000 (12:31 +0000)
bgp-network command is modified so as to support the ability to
configure prefixes with afi parameter, to be 1 ( IPv4) or 2 ( IPv6).
Moreover, this extension permits retrieving the number of prefixes VPNv6 stored
on quagga, like it has been done for VPNv4

Change-Id: I90a043d5d2a77789b0a5fe376994b201830ad5fa
Signed-off-by: Noel De Prandieres <prandieres@6wind.com>
Signed-off-by: Philippe Guibert <philippe.guibert@6wind.com>
vpnservice/bgpmanager/bgpmanager-impl/src/main/java/org/opendaylight/netvirt/bgpmanager/commands/Commands.java
vpnservice/bgpmanager/bgpmanager-impl/src/main/java/org/opendaylight/netvirt/bgpmanager/commands/Network.java
vpnservice/bgpmanager/bgpmanager-impl/src/main/java/org/opendaylight/netvirt/bgpmanager/oam/BgpAlarms.java
vpnservice/bgpmanager/bgpmanager-impl/src/main/java/org/opendaylight/netvirt/bgpmanager/oam/BgpCounters.java

index 4d1000d7ff97b27efea0068ab925d9cc144866c5..9e42e0e1bc7ae24105e1e661c3874dc396f1c7f1 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.netvirt.bgpmanager.commands;
 
 import java.io.PrintStream;
 import org.opendaylight.netvirt.bgpmanager.BgpManager;
+import org.opendaylight.netvirt.bgpmanager.thrift.gen.af_afi;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
 
 public class Commands {
@@ -18,7 +19,7 @@ public class Commands {
     private static final long AS_MAX = 4294967295L;//2^32-1
 
     enum Validators {
-        IPADDR, INT, ASNUM
+        IPADDR, INT, ASNUM, AFI
     }
 
     public Commands(BgpManager bgpm) {
@@ -52,6 +53,19 @@ public class Commands {
                     return false;
                 }
                 break;
+            case AFI:
+                try {
+                    int afiValue = Integer.parseInt(val);
+                    if (afiValue < 1 || afiValue > af_afi.values().length) {
+                        ps.println("error: value of " + name
+                                + " is not an integer between 1(ipv4) and 2(ipv6), its value is " + val);
+                        return false;
+                    }
+                } catch (NumberFormatException nme) {
+                    ps.println("error: value of " + name + " is not an integer");
+                    return false;
+                }
+                break;
             default:
                 return false;
         }
index a2269dc42f16c8e5949853feae65cdf910ae12e6..dfc123ef2f46ea24c67bc5442921e5c0dc734498 100644 (file)
@@ -27,6 +27,7 @@ public class Network extends OsgiCommandSupport {
     private static final String PFX = "--prefix";
     private static final String NH = "--nexthop";
     private static final String LB = "--label";
+    private static final String AFI = "--afi";
 
     static final Logger LOGGER = LoggerFactory.getLogger(Network.class);
     @Argument(name = "add|del", description = "The desired operation",
@@ -53,12 +54,18 @@ public class Network extends OsgiCommandSupport {
             required = false, multiValued = false)
     private String lbl = null;
 
+    @Option(name = AFI, aliases = {"-a"},
+            description = "Address Family",
+            required = false, multiValued = false)
+    private String afi = "1";
+
     private RouteOrigin staticOrigin = RouteOrigin.STATIC;
 
     private Object usage() {
         session.getConsole().println(
                 "usage: bgp-network [" + RD + " rd] [" + PFX + " prefix/len] ["
-                        + NH + " nexthop] [" + LB + " label] <add|del>");
+                        + NH + " nexthop] [" + LB + " label] ["
+                        + AFI + " afi] <add|del>");
         return null;
     }
 
@@ -91,8 +98,9 @@ public class Network extends OsgiCommandSupport {
                     } else {
                         label = Integer.valueOf(lbl);
                     }
-                } else if (rd == null) {
-                    session.getConsole().println("error: " + RD + " is needed");
+                }
+                if (!Commands.isValid(session.getConsole(), afi, Commands.Validators.AFI, AFI)) {
+                    session.getConsole().println("error: " + AFI + " must be 1 (IPv4) or 2 (IPv6). Default is 1");
                     return null;
                 }
                 LOGGER.info("ADD: Adding Fib entry rd {} prefix {} nexthop {} label {}", rd, pfx, nh, label);
@@ -106,7 +114,12 @@ public class Network extends OsgiCommandSupport {
                     return null;
                 }
                 if (nh != null || lbl != null) {
-                    session.getConsole().println("note: some option(s) not needed; ignored");
+                    session.getConsole().println("note: some option(s) not needed as " + NH + " and/or " + LB
+                            + "; will be ignored");
+                }
+                if (!Commands.isValid(session.getConsole(), afi, Commands.Validators.AFI, AFI)) {
+                    session.getConsole().println("error: " + AFI + " must be 1 (IPv4) or 2 (IPv6). Default is 1");
+                    return null;
                 }
                 LOGGER.info("REMOVE: Removing Fib entry rd {} prefix {}", rd, pfx);
                 bm.deletePrefix(rd, pfx);
index 2c46759d09633005fcbcefe663a7d368f3fd6ba7..9c0ac0bc1a462a4dbe4939a705baffeec97c8be3 100644 (file)
@@ -8,9 +8,6 @@
 
 package org.opendaylight.netvirt.bgpmanager.oam;
 
-import static org.opendaylight.netvirt.bgpmanager.oam.BgpCounters.parseIpBgpVpnv4AllSummary;
-import static org.opendaylight.netvirt.bgpmanager.oam.BgpCounters.resetFile;
-
 import java.io.IOException;
 import java.util.HashMap;
 import java.util.List;
@@ -35,19 +32,26 @@ public class BgpAlarms extends TimerTask {
         List<Neighbors> nbrList = null;
         try {
             LOG.debug("Fetching neighbor status' from BGP");
-            resetFile("cmd_ip_bgp_vpnv4_all_summary.txt");
+            BgpCounters.resetFile(BgpCounters.BGP_VPNV4_SUMMARY_FILE);
+            BgpCounters.resetFile(BgpCounters.BGP_VPNV6_SUMMARY_FILE);
             neighborStatusMap.clear();
 
             if (bgpMgr != null && bgpMgr.getBgpCounters() != null) {
-                bgpMgr.getBgpCounters().fetchCmdOutputs("cmd_ip_bgp_vpnv4_all_summary.txt",
+                bgpMgr.getBgpCounters().fetchCmdOutputs(BgpCounters.BGP_VPNV4_SUMMARY_FILE,
                         "show ip bgp vpnv4 all summary");
                 if (bgpMgr.getConfig() != null) {
                     nbrList = bgpMgr.getConfig().getNeighbors();
                 }
-                parseIpBgpVpnv4AllSummary(neighborStatusMap);
+                BgpCounters.parseIpBgpVpnv4AllSummary(neighborStatusMap);
+
+                bgpMgr.getBgpCounters().fetchCmdOutputs(BgpCounters.BGP_VPNV6_SUMMARY_FILE,
+                        "show ip bgp vpnv6 all summary");
+                if (bgpMgr.getConfig() != null) {
+                    nbrList = bgpMgr.getConfig().getNeighbors();
+                }
+                BgpCounters.parseIpBgpVpnv6AllSummary(neighborStatusMap);
                 processNeighborStatusMap(neighborStatusMap, nbrList, neighborsRaisedAlarmStatusMap);
             }
-
             LOG.debug("Finished getting the status of BGP neighbors");
         } catch (IOException e) {
             LOG.error("Failed to publish bgp counters ", e);
index 0d3a0c85d5be53c5247432ed7299130a4cba0160..aa5045070b63f8231e3d210592017977971e668b 100644 (file)
@@ -31,6 +31,8 @@ import java.util.regex.Pattern;
 import javax.management.JMException;
 import javax.management.MBeanServer;
 import javax.management.ObjectName;
+
+import org.opendaylight.netvirt.bgpmanager.thrift.gen.af_afi;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -42,6 +44,10 @@ public class BgpCounters extends TimerTask {
     private MBeanServer bgpStatsServer = null;
     private Map<String, String> countersMap = new HashMap<>();
     private String bgpSdncMip = "127.0.0.1";
+    public static final String BGP_VPNV6_FILE = "cmd_ip_bgp_vpnv6_all.txt";
+    public static final String BGP_VPNV4_FILE = "cmd_ip_bgp_vpnv4_all.txt";
+    public static final String BGP_VPNV6_SUMMARY_FILE = "cmd_ip_bgp_vpnv6_all_summary.txt";
+    public static final String BGP_VPNV4_SUMMARY_FILE = "cmd_ip_bgp_vpnv4_all_summary.txt";
 
     public BgpCounters(String mipAddress) {
         bgpSdncMip = mipAddress;
@@ -54,9 +60,11 @@ public class BgpCounters extends TimerTask {
             resetCounters();
             fetchCmdOutputs("cmd_ip_bgp_summary.txt", "show ip bgp summary");
             fetchCmdOutputs("cmd_bgp_ipv4_unicast_statistics.txt", "show bgp ipv4 unicast statistics");
-            fetchCmdOutputs("cmd_ip_bgp_vpnv4_all.txt", "show ip bgp vpnv4 all");
+            fetchCmdOutputs(BGP_VPNV4_FILE, "show ip bgp vpnv4 all");
+            fetchCmdOutputs(BGP_VPNV6_FILE, "show ip bgp vpnv6 all");
             parseIpBgpSummary();
             parseIpBgpVpnv4All();
+            parseIpBgpVpnv6All();
             if (LOGGER.isDebugEnabled()) {
                 dumpCounters();
             }
@@ -119,6 +127,7 @@ public class BgpCounters extends TimerTask {
                     case '>':
                         // Fall through
                     case '#':
+                        sb.append((char) read);
                         prompt = sb.toString().trim();
                         break;
                     default:
@@ -146,6 +155,9 @@ public class BgpCounters extends TimerTask {
             cbuf = new char[1024];
             while ((read = fromRouter.read(cbuf)) != -1) {
                 sb.append(cbuf, 0, read);
+                if (sb.toString().trim().endsWith(prompt)) {
+                    break;
+                }
             }
 
             // Only keep output up to the last prompt
@@ -165,16 +177,22 @@ public class BgpCounters extends TimerTask {
         }
     }
 
-    private static boolean validate(final String ip) {
+    private static boolean validate(final String ip, af_afi afi) {
         if (ip == null || ip.equals("")) {
             return false;
         }
-        Pattern pattern = Pattern.compile("^(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])$");
+        String reg = "";
+
+        if (afi == af_afi.AFI_IP) {
+            reg = "^(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
+        } else {
+            reg = "\\A(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}\\z";
+        }
+        Pattern pattern = Pattern.compile(reg);
         Matcher matcher = pattern.matcher(ip);
         return matcher.matches();
     }
 
-
     /*
      * The below function parses the output of "show ip bgp summary" saved in a file.
      * Below is the snippet for the same :-
@@ -203,7 +221,7 @@ public class BgpCounters extends TimerTask {
                         return;
                     }
                     String strIp = result[0].trim();
-                    if (!validate(strIp)) {
+                    if (!validate(strIp, af_afi.AFI_IP)) {
                         return;
                     }
                     final String as = result[2];
@@ -276,7 +294,7 @@ public class BgpCounters extends TimerTask {
         </output>
      */
     private void parseIpBgpVpnv4All() {
-        File file = new File("cmd_ip_bgp_vpnv4_all.txt");
+        File file = new File(BGP_VPNV4_FILE);
         List<String> inputStrs = new ArrayList<>();
 
         try (Scanner scanner = new Scanner(file)) {
@@ -302,6 +320,46 @@ public class BgpCounters extends TimerTask {
         countersMap.put(BgpConstants.BGP_COUNTER_TOTAL_PFX,String.valueOf(bgpTotalPfxs));
     }
 
+    /*
+     *  The below function parses the output of "show ip bgp vpnv6 all" saved in a file.
+     *  Below is the sample output for the same :-
+     *  show ip bgp vpnv6 all
+        <output>
+        BGP table version is 0, local router ID is 10.183.181.21
+        ......
+        Route Distinguisher: 100:1
+        *>i2001:db8:0:2::/128   10.183.181.25            0    100      0 ?
+        *>i2001:db8:0:2::/128   10.183.181.25            0    100      0 ?
+        *>i2001:db8:0:2::/128   10.183.181.25            0    100      0 ?
+        *>i2001:db8:0:2::/128   10.183.181.25            0    100      0 ?
+        Route Distinguisher: 100:2
+        *>i2001:db9:0:3::/128   10.183.181.25            0    100      0 ?
+        *>i2001:db9:0:3::/128   10.183.181.25            0    100      0 ?
+        *>i2001:db9:0:3::/128   10.183.181.25            0    100      0 ?
+        </output>
+     */
+    private void parseIpBgpVpnv6All() {
+        File file = new File(BGP_VPNV6_FILE);
+        List<String> inputStrs = new ArrayList<>();
+
+        try (Scanner scanner = new Scanner(file)) {
+            while (scanner.hasNextLine()) {
+                inputStrs.add(scanner.nextLine());
+            }
+        } catch (IOException e) {
+            LOGGER.error("Could not process the file {}", file.getAbsolutePath());
+            return;
+        }
+        for (int i = 0; i < inputStrs.size(); i++) {
+            String instr = inputStrs.get(i);
+            if (instr.contains("Route Distinguisher")) {
+                String[] result = instr.split(":");
+                String rd = result[1].trim() + "_" + result[2].trim();
+                i = processRouteCount(rd, i + 1, inputStrs);
+            }
+        }
+    }
+
     private int processRouteCount(String rd, int startIndex, List<String> inputStrs) {
         int num = startIndex;
         int routeCount = 0;
@@ -339,7 +397,8 @@ public class BgpCounters extends TimerTask {
         countersMap.clear();
         resetFile("cmd_ip_bgp_summary.txt");
         resetFile("cmd_bgp_ipv4_unicast_statistics.txt");
-        resetFile("cmd_ip_bgp_vpnv4_all.txt");
+        resetFile(BGP_VPNV4_FILE);
+        resetFile(BGP_VPNV6_FILE);
     }
 
     static void resetFile(String fileName) {
@@ -353,8 +412,10 @@ public class BgpCounters extends TimerTask {
         }
     }
 
-    static Map<String, String> parseIpBgpVpnv4AllSummary(Map<String, String> countMap) {
-        File file = new File("cmd_ip_bgp_vpnv4_all_summary.txt");
+    static Map<String, String> parseIpBgpVpnAllSummary(Map<String, String> countMap,
+                                                       String cmdFile,
+                                                       af_afi afi) {
+        File file = new File(cmdFile);
 
         try (Scanner scanner = new Scanner(file)) {
             boolean startEntries = false;
@@ -369,7 +430,7 @@ public class BgpCounters extends TimerTask {
                         String strIp = result[0].trim();
                         LOGGER.trace("strIp " + strIp);
 
-                        if (!validate(strIp)) {
+                        if (!validate(strIp, afi)) {
                             break;
                         }
                         String statePfxRcvd = result[9];
@@ -384,4 +445,16 @@ public class BgpCounters extends TimerTask {
 
         return countMap;
     }
+
+    static Map<String, String> parseIpBgpVpnv4AllSummary(Map<String, String> countMap) {
+        return BgpCounters.parseIpBgpVpnAllSummary(countMap,
+                                                   BGP_VPNV4_SUMMARY_FILE,
+                                                   af_afi.AFI_IP);
+    }
+
+    static Map<String, String> parseIpBgpVpnv6AllSummary(Map<String, String> countMap) {
+        return BgpCounters.parseIpBgpVpnAllSummary(countMap,
+                                                   BGP_VPNV6_SUMMARY_FILE,
+                                                   af_afi.AFI_IPV6);
+    }
 }