Migrating BGP PM Counters to Infrautils Counters 68/67268/13
authorKiran Kumar Darapu <kiran.kumar@altencalsoftlabs.com>
Thu, 18 Jan 2018 06:18:31 +0000 (11:48 +0530)
committerSam Hague <shague@redhat.com>
Mon, 12 Feb 2018 23:38:44 +0000 (23:38 +0000)
Existing BGP PM counters were using JMX MBeans,
Now new BGP PM counters were imoplemented using
Infrautils metrics counters APIs which will be common library
to populate JMX MBeans for these counters by leveraging
Codahale framework.

Change-Id: Ib458d151ffed331ccd2b717ad3abef0ebecf3f80
Signed-off-by: Kiran Kumar Darapu <kiran.kumar@altencalsoftlabs.com>
vpnservice/bgpmanager/bgpmanager-impl/pom.xml
vpnservice/bgpmanager/bgpmanager-impl/src/main/java/org/opendaylight/netvirt/bgpmanager/BgpConfigurationManager.java
vpnservice/bgpmanager/bgpmanager-impl/src/main/java/org/opendaylight/netvirt/bgpmanager/oam/BgpCounters.java
vpnservice/bgpmanager/bgpmanager-impl/src/main/java/org/opendaylight/netvirt/bgpmanager/oam/BgpCountersBroadcaster.java [deleted file]
vpnservice/bgpmanager/bgpmanager-impl/src/main/java/org/opendaylight/netvirt/bgpmanager/oam/BgpCountersBroadcasterMBean.java [deleted file]
vpnservice/bgpmanager/bgpmanager-impl/src/main/resources/org/opendaylight/blueprint/bgpmanager.xml

index 3e845990639395a76a8804372c309e281fda004c..1c13a9b04cf3466ccd08c6f11f7e630bce21b8f4 100644 (file)
@@ -72,7 +72,11 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
       <artifactId>truth</artifactId>
       <scope>test</scope>
     </dependency>
-
+    <dependency>
+      <groupId>org.opendaylight.infrautils</groupId>
+      <artifactId>metrics-api</artifactId>
+      <version>${infrautils.version}</version>
+    </dependency>
   </dependencies>
 
   <build>
index d9666288cbadfe313d61569400a2bc79bf8178ae..9801f767f3edba7e2320478ef4a9cec509ac7f0e 100755 (executable)
@@ -55,6 +55,7 @@ import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
 import org.opendaylight.genius.mdsalutil.NwConstants;
 import org.opendaylight.genius.utils.clustering.EntityOwnershipUtils;
+import org.opendaylight.infrautils.metrics.MetricProvider;
 import org.opendaylight.mdsal.eos.binding.api.Entity;
 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipCandidateRegistration;
 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListenerRegistration;
@@ -223,6 +224,7 @@ public class BgpConfigurationManager {
     private final EntityOwnershipUtils entityOwnershipUtils;
     private final EntityOwnershipCandidateRegistration candidateRegistration;
     private final EntityOwnershipListenerRegistration entityListenerRegistration;
+    private final MetricProvider metricProvider;
 
     @Inject
     public BgpConfigurationManager(final DataBroker dataBroker,
@@ -230,13 +232,15 @@ public class BgpConfigurationManager {
             final FibDSWriter fibDSWriter,
             final IVpnLinkService vpnLinkSrvce,
             final BundleContext bundleContext,
-            final BgpUtil bgpUtil)
+            final BgpUtil bgpUtil,
+            final MetricProvider metricProvider)
             throws InterruptedException, ExecutionException, TimeoutException {
         this.dataBroker = dataBroker;
         this.fibDSWriter = fibDSWriter;
         this.vpnLinkService = vpnLinkSrvce;
         this.bundleContext = bundleContext;
         this.bgpUtil = bgpUtil;
+        this.metricProvider = metricProvider;
         String updatePort = getProperty(UPDATE_PORT, DEF_UPORT);
         hostStartup = getProperty(CONFIG_HOST, DEF_CHOST);
         portStartup = getProperty(CONFIG_PORT, DEF_CPORT);
@@ -1636,7 +1640,6 @@ public class BgpConfigurationManager {
                     String host = getConfigHost();
                     int port = getConfigPort();
                     LOG.info("connecting  to bgp host {} ", host);
-
                     bgpRouter.connect(host, port);
                     LOG.info("no config to push in bgp replay task ");
                     return;
@@ -2063,7 +2066,6 @@ public class BgpConfigurationManager {
         String host = getConfigHost();
         int port = getConfigPort();
         LOG.error("connecting  to bgp host {} ", host);
-
         boolean res = bgpRouter.connect(host, port);
         if (!res) {
             String msg = "Cannot connect to BGP config server at " + host + ":" + port;
@@ -2833,7 +2835,8 @@ public class BgpConfigurationManager {
     }
 
     private void startBgpCountersTask() {
-        if (getBgpCounters() == null && bgpCountersReference.compareAndSet(null, new BgpCounters(getBgpSdncMipIp()))) {
+        if (getBgpCounters() == null && bgpCountersReference.compareAndSet(null,
+                new BgpCounters(getBgpSdncMipIp(), metricProvider))) {
             bgpCountersTask = executor.scheduleAtFixedRate(bgpCountersReference.get(), 0, 120 * 1000,
                     TimeUnit.MILLISECONDS);
             LOG.info("Bgp Counters task scheduled for every two minutes.");
index f95d69d5a92e9338cfea82104a50140a00f16c4f..7a0cf2aa9d9ecc53adae7cad85c27251510285b9 100644 (file)
@@ -9,6 +9,7 @@
 package org.opendaylight.netvirt.bgpmanager.oam;
 
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
+
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.File;
@@ -17,7 +18,6 @@ import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.PrintWriter;
-import java.lang.management.ManagementFactory;
 import java.net.Inet4Address;
 import java.net.Inet6Address;
 import java.net.InetAddress;
@@ -29,12 +29,14 @@ import java.util.List;
 import java.util.Map;
 import java.util.Scanner;
 import java.util.concurrent.ConcurrentHashMap;
+
 import javax.annotation.Nonnull;
-import javax.management.InstanceNotFoundException;
-import javax.management.JMException;
-import javax.management.MBeanRegistrationException;
-import javax.management.MalformedObjectNameException;
-import javax.management.ObjectName;
+import javax.inject.Inject;
+
+import org.opendaylight.infrautils.metrics.Counter;
+import org.opendaylight.infrautils.metrics.Labeled;
+import org.opendaylight.infrautils.metrics.MetricDescriptor;
+import org.opendaylight.infrautils.metrics.MetricProvider;
 import org.opendaylight.netvirt.bgpmanager.thrift.gen.af_afi;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -50,24 +52,19 @@ public class BgpCounters implements Runnable, AutoCloseable {
 
     private static final Logger LOG = LoggerFactory.getLogger(BgpCounters.class);
 
-    private final Map<String, String> countersMap = new ConcurrentHashMap<>();
-    private final String bgpSdncMip;
+    private final Map<String, String> totalPfxMap = new ConcurrentHashMap<>();
 
-    private volatile BgpCountersBroadcaster bgpStatsBroadcaster;
+    private final String bgpSdncMip;
+    private final MetricProvider metricProvider;
 
-    public BgpCounters(String mipAddress) {
-        bgpSdncMip = mipAddress;
+    @Inject
+    public BgpCounters(String mipAddress, final MetricProvider metricProvider) {
+        this.metricProvider = metricProvider;
+        this.bgpSdncMip = mipAddress;
     }
 
     @Override
     public void close() {
-        if (bgpStatsBroadcaster != null) {
-            try {
-                ManagementFactory.getPlatformMBeanServer().unregisterMBean(bgpStatsON());
-            } catch (MBeanRegistrationException | InstanceNotFoundException | MalformedObjectNameException e) {
-                LOG.warn("Error unregistering BgpCountersBroadcaster", e);
-            }
-        }
     }
 
     @Override
@@ -83,35 +80,13 @@ public class BgpCounters implements Runnable, AutoCloseable {
             parseIpBgpSummary();
             parseIpBgpVpnv4All();
             parseIpBgpVpnv6All();
-            parseIpBgpVpnv6All();
             parseBgpL2vpnEvpnAll();
-            if (LOG.isDebugEnabled()) {
-                dumpCounters();
-            }
-            if (bgpStatsBroadcaster == null) {
-                //First time execution
-                try {
-                    bgpStatsBroadcaster = new BgpCountersBroadcaster();
-                    ManagementFactory.getPlatformMBeanServer().registerMBean(bgpStatsBroadcaster, bgpStatsON());
-                    LOG.info("BGP Counters MBean Registered :::");
-                } catch (JMException e) {
-                    LOG.error("Error registering BgpCountersBroadcaster", e);
-                    return;
-                }
-            }
-            bgpStatsBroadcaster.setBgpCountersMap(countersMap);
             LOG.debug("Finished updating the counters from BGP");
         } catch (IOException e) {
             LOG.error("Failed to publish bgp counters ", e);
         }
     }
 
-    private void dumpCounters() {
-        for (Map.Entry<String, String> entry : countersMap.entrySet()) {
-            LOG.debug("{}, Value = {}", entry.getKey(), entry.getValue());
-        }
-    }
-
     void fetchCmdOutputs(String filename, String cmdName) throws IOException {
         try (Socket socket = new Socket(bgpSdncMip, 2605);
              PrintWriter toRouter = new PrintWriter(socket.getOutputStream(), true);
@@ -249,13 +224,13 @@ public class BgpCounters implements Runnable, AutoCloseable {
                     final String rx = result[3];
                     final String tx = result[4];
 
-                    countersMap.put(
-                            BgpConstants.BGP_COUNTER_NBR_PKTS_RX + ":BGP_Nbr_IP_" + strIp + "_AS_" + as
-                                    + "_PktsReceived",
-                            rx);
-                    countersMap.put(
-                            BgpConstants.BGP_COUNTER_NBR_PKTS_TX + ":BGP_Nbr_IP_" + strIp + "_AS_" + as + "_PktsSent",
-                            tx);
+                    Counter counter = getCounter(BgpConstants.BGP_COUNTER_NBR_PKTS_RX, as,
+                            rx, null, strIp, null);
+                    updateCounter(counter, Long.parseLong(rx));
+
+                    counter = getCounter(BgpConstants.BGP_COUNTER_NBR_PKTS_TX, as,
+                            null, tx, strIp, null);
+                    updateCounter(counter, Long.parseLong(tx));
                 }
             }
         } catch (IOException e) {
@@ -365,20 +340,27 @@ public class BgpCounters implements Runnable, AutoCloseable {
         }
         /*populate the "BgpTotalPrefixes" counter by combining
         the prefixes that are calculated per RD basis*/
-        int bgpTotalPfxs = calculateBgpTotalPrefixes();
+        long bgpTotalPfxs = calculateBgpTotalPrefixes();
         LOG.trace("BGP Total Prefixes:{}",bgpTotalPfxs);
-        countersMap.put(BgpConstants.BGP_COUNTER_TOTAL_PFX,String.valueOf(bgpTotalPfxs));
+        Counter counter = getCounter(BgpConstants.BGP_COUNTER_TOTAL_PFX, null, null, null,
+                null, null);
+        updateCounter(counter, bgpTotalPfxs);
     }
 
     private int processRouteCount(String rd, int startIndex, List<String> inputStrs) {
         int num = startIndex;
-        int routeCount = 0;
-        String key = BgpConstants.BGP_COUNTER_RD_ROUTE_COUNT + ":BGP_RD_" + rd + "_route_count";
+        long routeCount = 0;
 
-        for (String str = inputStrs.get(num); str != null && !str.trim().equals("") && num < inputStrs.size();
+        String bgpRdRouteCountKey = BgpConstants.BGP_COUNTER_RD_ROUTE_COUNT + rd;
+        Counter counter = getCounter(BgpConstants.BGP_COUNTER_RD_ROUTE_COUNT, null, null, null,
+                null, rd);
+
+        for (String str = inputStrs.get(num); str != null && !str.trim().equals("")
+                && num < inputStrs.size();
                 str = inputStrs.get(num)) {
             if (str.contains("Route Distinguisher")) {
-                countersMap.put(key, Integer.toString(routeCount));
+                totalPfxMap.put(bgpRdRouteCountKey, Long.toString(routeCount));
+                updateCounter(counter, routeCount);
                 return num - 1;
             }
             routeCount++;
@@ -394,17 +376,17 @@ public class BgpCounters implements Runnable, AutoCloseable {
             // by sending a big number back.
             return Integer.MAX_VALUE;
         }
-        countersMap.put(key, Integer.toString(routeCount));
+        updateCounter(counter, routeCount);
         return num - 1;
     }
 
-    private int calculateBgpTotalPrefixes() {
-        return countersMap.entrySet().stream().filter(entry -> entry.getKey().contains(BgpConstants
-                .BGP_COUNTER_RD_ROUTE_COUNT)).map(Map.Entry::getValue).mapToInt(Integer::parseInt).sum();
+    private Long calculateBgpTotalPrefixes() {
+        return totalPfxMap.entrySet().stream()
+                .map(Map.Entry::getValue).mapToLong(Long::parseLong).sum();
     }
 
     private void resetCounters() {
-        countersMap.clear();
+        totalPfxMap.clear();
         resetFile("cmd_ip_bgp_summary.txt");
         resetFile("cmd_bgp_ipv4_unicast_statistics.txt");
         resetFile(BGP_VPNV4_FILE);
@@ -475,7 +457,99 @@ public class BgpCounters implements Runnable, AutoCloseable {
                                                    af_afi.AFI_IP);
     }
 
-    private static ObjectName bgpStatsON() throws MalformedObjectNameException {
-        return new ObjectName("SDNC.PM:type=BgpCountersBroadcaster");
+    /**
+     * This method updates Counter values.
+     * @param counter object of the Counter
+     * @param counterValue value of Counter
+     */
+    private void updateCounter(Counter counter, long counterValue) {
+        try {
+            /*Reset counter to zero*/
+            counter.decrement(counter.get());
+            /*Set counter to specified value*/
+            counter.increment(counterValue);
+        } catch (IllegalStateException e) {
+            LOG.error("Exception occured during updating the Counter {}", counter, e);
+        }
+    }
+
+    /**
+     * Returns the counter.
+     * This method returns counter and also creates counter if does not exist.
+     *
+     * @param counterName name of the counter.
+     * @param asValue as value.
+     * @param rxValue rx value.
+     * @param txValue tx value.
+     * @param neighborIp neighbor Ipaddress.
+     * @param rdValue rd value.
+     * @return counter object.
+     */
+    private Counter getCounter(String counterName, String asValue,
+            String rxValue, String txValue, String neighborIp, String rdValue) {
+        String counterTypeEntityCounter = "entitycounter";
+        String labelKeyEntityType = "entitytype";
+
+        String labelValEntityTypeBgpPeer = "bgp-peer";
+        String labelKeyAsId = "asid";
+        String labelKeyNeighborIp = "neighborip";
+
+        String labelValEntityTypeBgpRd = "bgp-rd";
+        String labelKeyRd = "rd";
+
+        String counterTypeAggregateCounter = "aggregatecounter";
+        String labelKeyCounterName = "name";
+
+        Counter counter = null;
+
+        if (rxValue != null) {
+            /*
+             * Following is the key pattern for Counter BgpNeighborPacketsReceived
+             * netvirt.bgpmanager.entitycounter{entitytype=bgp-peer, asid=value, neighborip=value, name=countername}
+             * */
+            Labeled<Labeled<Labeled<Labeled<Counter>>>> labeledCounter =
+                    metricProvider.newCounter(MetricDescriptor.builder().anchor(this).project("netvirt")
+                        .module("bgpmanager").id(counterTypeEntityCounter).build(),
+                        labelKeyEntityType, labelKeyAsId,
+                        labelKeyNeighborIp, labelKeyCounterName);
+            counter = labeledCounter.label(labelValEntityTypeBgpPeer).label(asValue)
+                    .label(neighborIp).label(counterName);
+        } else if (txValue != null) {
+            /*
+             * Following is the key pattern for Counter BgpNeighborPacketsSent
+             * netvirt.bgpmanager.entitycounter{entitytype=bgp-peer, asid=value, neighborip=value, name=countername}
+             * */
+            Labeled<Labeled<Labeled<Labeled<Counter>>>> labeledCounter =
+                    metricProvider.newCounter(MetricDescriptor.builder().anchor(this).project("netvirt")
+                        .module("bgpmanager").id(counterTypeEntityCounter).build(),
+                        labelKeyEntityType, labelKeyAsId,
+                        labelKeyNeighborIp, labelKeyCounterName);
+            counter = labeledCounter.label(labelValEntityTypeBgpPeer).label(asValue)
+                    .label(neighborIp).label(counterName);
+        } else if (rdValue != null) {
+            /*
+             * Following is the key pattern for Counter BgpRdRouteCount
+             * netvirt.bgpmanager.entitycounter{entitytype=bgp-rd, rd=value, name=countername}
+             * */
+            Labeled<Labeled<Labeled<Counter>>> labeledCounter =
+                    metricProvider.newCounter(MetricDescriptor.builder().anchor(this).project("netvirt")
+                        .module("bgpmanager").id(counterTypeEntityCounter).build(),
+                        labelKeyEntityType, labelKeyRd,
+                        labelKeyCounterName);
+            counter = labeledCounter.label(labelValEntityTypeBgpRd).label(rdValue)
+                    .label(counterName);
+        } else {
+            /*
+             * Following is the key pattern for Counter BgpTotalPrefixes:Bgp_Total_Prefixes
+             * netvirt.bgpmanager.aggregatecounter{name=countername}
+             * */
+            Labeled<Counter> labeledCounter =
+                    metricProvider.newCounter(MetricDescriptor.builder().anchor(this).project("netvirt")
+                        .module("bgpmanager").id(counterTypeAggregateCounter).build(),
+                        labelKeyCounterName);
+            counter = labeledCounter.label(counterName);
+        }
+        return counter;
     }
+
 }
diff --git a/vpnservice/bgpmanager/bgpmanager-impl/src/main/java/org/opendaylight/netvirt/bgpmanager/oam/BgpCountersBroadcaster.java b/vpnservice/bgpmanager/bgpmanager-impl/src/main/java/org/opendaylight/netvirt/bgpmanager/oam/BgpCountersBroadcaster.java
deleted file mode 100644 (file)
index 9937d0a..0000000
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Copyright © 2015, 2017 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-
-package org.opendaylight.netvirt.bgpmanager.oam;
-
-import java.util.HashMap;
-import java.util.Map;
-import javax.management.NotificationBroadcasterSupport;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-
-/**
- * Created by ECHIAPT on 9/25/2015.
- */
-public class BgpCountersBroadcaster extends NotificationBroadcasterSupport implements BgpCountersBroadcasterMBean  {
-    private static final Logger LOG = LoggerFactory.getLogger(BgpCountersBroadcaster.class);
-
-    private volatile Map<String, String> bgpCountersMap = new HashMap<>();
-
-    @Override
-    public Map<String, String> retrieveCounterMap() {
-        LOG.trace("Polled retrieveCounterMap");
-        Map<String, String> countersVal = new HashMap<>(bgpCountersMap);
-        for (Map.Entry<String, String> entry : countersVal.entrySet()) {
-            LOG.trace(entry.getKey() + ", Value from MBean= " + entry.getValue());
-        }
-        return countersVal;
-    }
-
-    public void setBgpCountersMap(Map<String, String> fetchedCountersMap) {
-        LOG.trace("putAll");
-        bgpCountersMap = new HashMap<>(fetchedCountersMap);
-    }
-}
diff --git a/vpnservice/bgpmanager/bgpmanager-impl/src/main/java/org/opendaylight/netvirt/bgpmanager/oam/BgpCountersBroadcasterMBean.java b/vpnservice/bgpmanager/bgpmanager-impl/src/main/java/org/opendaylight/netvirt/bgpmanager/oam/BgpCountersBroadcasterMBean.java
deleted file mode 100644 (file)
index 71d890d..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-/*
- * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-
-package org.opendaylight.netvirt.bgpmanager.oam;
-
-import java.util.Map;
-
-/**
- * Created by echiapt on 9/28/2015.
- */
-public interface BgpCountersBroadcasterMBean {
-    Map<String, String> retrieveCounterMap();
-}
-
index 3576646057df1d20e31e9b7cb52300dd05f09b04..75a103b0cb193bbbb1399af9170e3b7708ad048c 100644 (file)
@@ -11,6 +11,8 @@
   <reference id="iVpnLinkService"
              interface="org.opendaylight.netvirt.vpnmanager.api.intervpnlink.IVpnLinkService"
              availability="optional" />
+  <reference id="metricProvider"
+             interface="org.opendaylight.infrautils.metrics.MetricProvider" />
 
   <service ref="bgpManager" odl:type="default"
            interface="org.opendaylight.netvirt.bgpmanager.api.IBgpManager" />