Migrating FCAPSApplication PM Counters to Infrautils Counters
[genius.git] / fcapsapplication / fcapsapplication-impl / src / main / java / org / opendaylight / genius / fcapsapp / performancecounter / FlowNodeConnectorInventoryTranslatorImpl.java
index ab29193b396388b00e9bd3fea83dfe5a79a40957..23b0583809d66e5859206d7de5cc91ace4f629b2 100644 (file)
@@ -12,18 +12,19 @@ import com.google.common.collect.ArrayListMultimap;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Multimap;
 import com.google.common.collect.Multimaps;
-import java.util.Collection;
-import java.util.HashMap;
 import javax.annotation.PreDestroy;
 import javax.inject.Inject;
 import javax.inject.Singleton;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
-import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
-import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
-import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.genius.fcapsapp.FcapsConstants;
 import org.opendaylight.genius.fcapsapp.portinfo.PortNameMapping;
+import org.opendaylight.genius.utils.clustering.EntityOwnershipUtils;
+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.openflowplugin.common.wait.SimpleTaskRetryLooper;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
@@ -36,16 +37,18 @@ import org.slf4j.LoggerFactory;
 
 @Singleton
 public class FlowNodeConnectorInventoryTranslatorImpl extends NodeConnectorEventListener<FlowCapableNodeConnector> {
-    public static final int STARTUP_LOOP_TICK = 500;
-    public static final int STARTUP_LOOP_MAX_RETRIES = 8;
     private static final Logger LOG = LoggerFactory.getLogger(FlowNodeConnectorInventoryTranslatorImpl.class);
-    private final EntityOwnershipService entityOwnershipService;
+
+    private static final int STARTUP_LOOP_TICK = 500;
+    private static final int STARTUP_LOOP_MAX_RETRIES = 8;
+
+    private final EntityOwnershipUtils entityOwnershipUtils;
     private final DataBroker dataBroker;
     private ListenerRegistration<FlowNodeConnectorInventoryTranslatorImpl> dataTreeChangeListenerRegistration;
 
-    public static final String SEPARATOR = ":";
-    private final PMAgent agent;
-
+    private static final String SEPARATOR = ":";
+    private final MetricProvider metricProvider;
+    private final Labeled<Labeled<Labeled<Counter>>> packetInCounter;
     private static final InstanceIdentifier<FlowCapableNodeConnector>
             II_TO_FLOW_CAPABLE_NODE_CONNECTOR = InstanceIdentifier
             .builder(Nodes.class).child(Node.class).child(NodeConnector.class)
@@ -54,16 +57,18 @@ public class FlowNodeConnectorInventoryTranslatorImpl extends NodeConnectorEvent
     private static Multimap<Long, String> dpnToPortMultiMap = Multimaps
             .synchronizedListMultimap(ArrayListMultimap.<Long, String>create());
 
-    private static HashMap<String, String> nodeConnectorCountermap = new HashMap<>();
 
     @Inject
     @SuppressWarnings("checkstyle:IllegalCatch")
     public FlowNodeConnectorInventoryTranslatorImpl(final DataBroker dataBroker,
-            final EntityOwnershipService entityOwnershipService, final PMAgent agent) {
-        super(FlowCapableNodeConnector.class);
+                                                    final EntityOwnershipUtils entityOwnershipUtils,
+                                                    MetricProvider metricProvider) {
         this.dataBroker = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
-        this.entityOwnershipService = entityOwnershipService;
-        this.agent = agent;
+        this.entityOwnershipUtils = entityOwnershipUtils;
+        this.metricProvider = metricProvider;
+        packetInCounter =  metricProvider.newCounter(MetricDescriptor.builder().anchor(this)
+                .project("genius").module("fcapsapplication")
+                .id("entitycounter").build(), "entitytype", "switchid","name");
         final DataTreeIdentifier<FlowCapableNodeConnector> treeId = new DataTreeIdentifier<>(
                 LogicalDatastoreType.OPERATIONAL, getWildCardPath());
         try {
@@ -94,7 +99,8 @@ public class FlowNodeConnectorInventoryTranslatorImpl extends NodeConnectorEvent
 
     @Override
     public void remove(InstanceIdentifier<FlowCapableNodeConnector> identifier, FlowCapableNodeConnector del,
-            InstanceIdentifier<FlowCapableNodeConnector> nodeConnIdent) {
+                       InstanceIdentifier<FlowCapableNodeConnector> nodeConnIdent) {
+        Counter counter;
         if (compareInstanceIdentifierTail(identifier, II_TO_FLOW_CAPABLE_NODE_CONNECTOR)) {
             String nodeConnectorIdentifier = getNodeConnectorId(
                     String.valueOf(nodeConnIdent.firstKeyOf(NodeConnector.class).getId()));
@@ -102,7 +108,8 @@ public class FlowNodeConnectorInventoryTranslatorImpl extends NodeConnectorEvent
             if (dpnToPortMultiMap.containsKey(dataPathId)) {
                 LOG.debug("Node Connector {} removed", nodeConnectorIdentifier);
                 dpnToPortMultiMap.remove(dataPathId, nodeConnectorIdentifier);
-                sendNodeConnectorUpdation(dataPathId);
+                counter = packetInCounter.label("OFSwitch").label(String.valueOf(dataPathId)).label("portsperswitch");
+                counter.decrement();
                 PortNameMapping.updatePortMap("openflow:" + dataPathId + ":" + del.getName(), nodeConnectorIdentifier,
                         "DELETE");
             }
@@ -112,39 +119,25 @@ public class FlowNodeConnectorInventoryTranslatorImpl extends NodeConnectorEvent
     @Override
     public void update(InstanceIdentifier<FlowCapableNodeConnector> identifier, FlowCapableNodeConnector original,
             FlowCapableNodeConnector update, InstanceIdentifier<FlowCapableNodeConnector> nodeConnIdent) {
-        if (compareInstanceIdentifierTail(identifier, II_TO_FLOW_CAPABLE_NODE_CONNECTOR)) {
-            // Don't need to do anything as we are not considering updates here
-            String nodeConnectorIdentifier = getNodeConnectorId(
-                    String.valueOf(nodeConnIdent.firstKeyOf(NodeConnector.class).getId()));
-            long dataPathId = getDpIdFromPortName(nodeConnectorIdentifier);
-            if (isNodeOwner(getNodeId(dataPathId))) {
-                boolean originalPortStatus = original.getConfiguration().isPORTDOWN();
-                boolean updatePortStatus = update.getConfiguration().isPORTDOWN();
-
-                if (updatePortStatus) {
-                    // port has gone down
-                    LOG.debug("Node Connector {} updated port is down", nodeConnectorIdentifier);
-                } else if (originalPortStatus) {
-                    // port has come up
-                    LOG.debug("Node Connector {} updated port is up", nodeConnectorIdentifier);
-                }
-            }
-        }
+        // Don't need to do anything as we are not considering updates here
     }
 
     @Override
     public void add(InstanceIdentifier<FlowCapableNodeConnector> identifier, FlowCapableNodeConnector add,
             InstanceIdentifier<FlowCapableNodeConnector> nodeConnIdent) {
+        Counter counter;
         if (compareInstanceIdentifierTail(identifier, II_TO_FLOW_CAPABLE_NODE_CONNECTOR)) {
 
             String nodeConnectorIdentifier = getNodeConnectorId(
                     String.valueOf(nodeConnIdent.firstKeyOf(NodeConnector.class).getId()));
             long dataPathId = getDpIdFromPortName(nodeConnectorIdentifier);
-            if (isNodeOwner(getNodeId(dataPathId))) {
+            if (entityOwnershipUtils.isEntityOwner(FcapsConstants.SERVICE_ENTITY_TYPE,getNodeId(dataPathId))) {
                 if (!dpnToPortMultiMap.containsEntry(dataPathId, nodeConnectorIdentifier)) {
                     LOG.debug("Node Connector {} added", nodeConnectorIdentifier);
                     dpnToPortMultiMap.put(dataPathId, nodeConnectorIdentifier);
-                    sendNodeConnectorUpdation(dataPathId);
+                    counter = packetInCounter.label("OFSwitch")
+                            .label(String.valueOf(dataPathId)).label("portsperswitch");
+                    counter.increment();
                     PortNameMapping.updatePortMap("openflow:" + dataPathId + ":" + add.getName(),
                             nodeConnectorIdentifier, "ADD");
                 } else {
@@ -164,19 +157,6 @@ public class FlowNodeConnectorInventoryTranslatorImpl extends NodeConnectorEvent
         return "openflow:" + dpnId;
     }
 
-    /**
-     * Method checks if *this* instance of controller is owner of the given
-     * openflow node.
-     *
-     * @param nodeId
-     *            openflow node Id
-     * @return True if owner, else false
-     */
-    public boolean isNodeOwner(String nodeId) {
-        Entity entity = new Entity("openflow", nodeId);
-        return this.entityOwnershipService.getOwnershipState(entity).transform(EntityOwnershipState::isOwner).or(false);
-    }
-
     private boolean compareInstanceIdentifierTail(InstanceIdentifier<?> identifier1,
             InstanceIdentifier<?> identifier2) {
         return Iterables.getLast(identifier1.getPathArguments())
@@ -188,17 +168,10 @@ public class FlowNodeConnectorInventoryTranslatorImpl extends NodeConnectorEvent
         return Long.parseLong(dpId);
     }
 
-    private void sendNodeConnectorUpdation(Long dpnId) {
-        Collection<String> portname = dpnToPortMultiMap.get(dpnId);
-        String nodeListPortsCountStr = "dpnId_" + dpnId + "_NumberOfOFPorts";
-        String counterkey = "NumberOfOFPorts:" + nodeListPortsCountStr;
-
-        if (!portname.isEmpty()) {
-            nodeConnectorCountermap.put(counterkey, "" + portname.size());
-        } else {
-            nodeConnectorCountermap.remove(counterkey);
-        }
-        LOG.debug("NumberOfOFPorts:" + nodeListPortsCountStr + " portlistsize " + portname.size());
-        agent.connectToPMAgentForNOOfPorts(nodeConnectorCountermap);
+    public void nodeRemovedNotification(String node) {
+        Counter counter;
+        String[] switchId = node.split(":");
+        counter = packetInCounter.label("OFSwitch").label(String.valueOf(switchId[1])).label("portsperswitch");
+        counter.close();
     }
 }