/*
- * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
+ * Copyright (c) 2016, 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,
*/
package org.opendaylight.genius.fcapsapp.performancecounter;
-import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
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 java.util.concurrent.Callable;
+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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public class FlowNodeConnectorInventoryTranslatorImpl extends NodeConnectorEventListener<FlowCapableNodeConnector> {
- public static final int STARTUP_LOOP_TICK = 500;
- public static final int STARTUP_LOOP_MAX_RETRIES = 8;
+@Singleton
+public class FlowNodeConnectorInventoryTranslatorImpl extends NodeConnectorEventListener<FlowCapableNodeConnector> {
private static final Logger LOG = LoggerFactory.getLogger(FlowNodeConnectorInventoryTranslatorImpl.class);
- private final EntityOwnershipService entityOwnershipService;
- private ListenerRegistration<FlowNodeConnectorInventoryTranslatorImpl> dataTreeChangeListenerRegistration;
-
- public static final String SEPARATOR = ":";
- private static final PMAgent pmAgent = new PMAgent();
-
- private static final InstanceIdentifier<FlowCapableNodeConnector> II_TO_FLOW_CAPABLE_NODE_CONNECTOR
- = InstanceIdentifier.builder(Nodes.class)
- .child(Node.class)
- .child(NodeConnector.class)
- .augmentation(FlowCapableNodeConnector.class)
- .build();
-
- private static Multimap<Long,String> dpnToPortMultiMap = Multimaps.synchronizedListMultimap(ArrayListMultimap.<Long,String>create());
-
- private static HashMap<String, String> nodeConnectorCountermap = new HashMap<>();
+ private static final int STARTUP_LOOP_TICK = 500;
+ private static final int STARTUP_LOOP_MAX_RETRIES = 8;
- public FlowNodeConnectorInventoryTranslatorImpl(final DataBroker dataBroker,final EntityOwnershipService eos) {
- super( FlowCapableNodeConnector.class);
- Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
+ private final EntityOwnershipUtils entityOwnershipUtils;
+ private final DataBroker dataBroker;
+ private ListenerRegistration<FlowNodeConnectorInventoryTranslatorImpl> dataTreeChangeListenerRegistration;
- entityOwnershipService = eos;
- final DataTreeIdentifier<FlowCapableNodeConnector> treeId =
- new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, getWildCardPath());
+ 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)
+ .augmentation(FlowCapableNodeConnector.class).build();
+
+ private static Multimap<Long, String> dpnToPortMultiMap = Multimaps
+ .synchronizedListMultimap(ArrayListMultimap.<Long, String>create());
+
+
+ @Inject
+ @SuppressWarnings("checkstyle:IllegalCatch")
+ public FlowNodeConnectorInventoryTranslatorImpl(final DataBroker dataBroker,
+ final EntityOwnershipUtils entityOwnershipUtils,
+ MetricProvider metricProvider) {
+ this.dataBroker = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
+ 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 {
- SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK,
- STARTUP_LOOP_MAX_RETRIES);
- dataTreeChangeListenerRegistration = looper.loopUntilNoException(new Callable<ListenerRegistration<FlowNodeConnectorInventoryTranslatorImpl>>() {
- @Override
- public ListenerRegistration<FlowNodeConnectorInventoryTranslatorImpl> call() throws Exception {
- return dataBroker.registerDataTreeChangeListener(treeId, FlowNodeConnectorInventoryTranslatorImpl.this);
- }
- });
- } catch (final Exception e) {
+ SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK, STARTUP_LOOP_MAX_RETRIES);
+ dataTreeChangeListenerRegistration = looper.loopUntilNoException(() -> dataBroker
+ .registerDataTreeChangeListener(treeId, FlowNodeConnectorInventoryTranslatorImpl.this));
+ } catch (Exception e) {
LOG.warn(" FlowNodeConnectorInventoryTranslatorImpl listener registration fail!");
LOG.debug("FlowNodeConnectorInventoryTranslatorImpl DataChange listener registration fail ..", e);
- throw new IllegalStateException("FlowNodeConnectorInventoryTranslatorImpl startup fail! System needs restart.", e);
+ throw new IllegalStateException(
+ "FlowNodeConnectorInventoryTranslatorImpl startup fail! System needs restart.", e);
}
}
-
protected InstanceIdentifier<FlowCapableNodeConnector> getWildCardPath() {
- return InstanceIdentifier.create(Nodes.class)
- .child(Node.class)
- .child(NodeConnector.class)
+ return InstanceIdentifier.create(Nodes.class).child(Node.class).child(NodeConnector.class)
.augmentation(FlowCapableNodeConnector.class);
}
+ @PreDestroy
@Override
public void close() {
if (dataTreeChangeListenerRegistration != null) {
- try {
- dataTreeChangeListenerRegistration.close();
- } catch (final Exception e) {
- LOG.warn("Error by stop FRM FlowNodeConnectorInventoryTranslatorImpl: {}", e.getMessage());
- LOG.debug("Error by stop FRM FlowNodeConnectorInventoryTranslatorImpl..", e);
- }
+ dataTreeChangeListenerRegistration.close();
dataTreeChangeListenerRegistration = null;
}
}
+
@Override
- public void remove(InstanceIdentifier<FlowCapableNodeConnector> identifier, FlowCapableNodeConnector del, InstanceIdentifier<FlowCapableNodeConnector> nodeConnIdent) {
- if(compareInstanceIdentifierTail(identifier,II_TO_FLOW_CAPABLE_NODE_CONNECTOR)) {
- String sNodeConnectorIdentifier = getNodeConnectorId(String.valueOf(nodeConnIdent.firstKeyOf(NodeConnector.class).getId()));
- long nDpId = getDpIdFromPortName(sNodeConnectorIdentifier);
- if (dpnToPortMultiMap.containsKey(nDpId)) {
- LOG.debug("Node Connector {} removed", sNodeConnectorIdentifier);
- dpnToPortMultiMap.remove(nDpId, sNodeConnectorIdentifier);
- sendNodeConnectorUpdation(nDpId);
- PortNameMapping.updatePortMap("openflow:" + nDpId + ":" + del.getName(), sNodeConnectorIdentifier, "DELETE");
+ public void remove(InstanceIdentifier<FlowCapableNodeConnector> identifier, FlowCapableNodeConnector del,
+ 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 (dpnToPortMultiMap.containsKey(dataPathId)) {
+ LOG.debug("Node Connector {} removed", nodeConnectorIdentifier);
+ dpnToPortMultiMap.remove(dataPathId, nodeConnectorIdentifier);
+ counter = packetInCounter.label("OFSwitch").label(String.valueOf(dataPathId)).label("portsperswitch");
+ counter.decrement();
+ PortNameMapping.updatePortMap("openflow:" + dataPathId + ":" + del.getName(), nodeConnectorIdentifier,
+ "DELETE");
}
}
}
@Override
- public void update(InstanceIdentifier<FlowCapableNodeConnector> identifier, FlowCapableNodeConnector original, FlowCapableNodeConnector update, InstanceIdentifier<FlowCapableNodeConnector> nodeConnIdent) {
- if(compareInstanceIdentifierTail(identifier,II_TO_FLOW_CAPABLE_NODE_CONNECTOR)) {
-
- //donot need to do anything as we are not considering updates here
- String sNodeConnectorIdentifier = getNodeConnectorId(String.valueOf(nodeConnIdent.firstKeyOf(NodeConnector.class).getId()));
- long nDpId = getDpIdFromPortName(sNodeConnectorIdentifier);
- if (isNodeOwner(getNodeId(nDpId))) {
- boolean original_portstatus = original.getConfiguration().isPORTDOWN();
- boolean update_portstatus = update.getConfiguration().isPORTDOWN();
-
- if (update_portstatus == true) {
- //port has gone down
- LOG.debug("Node Connector {} updated port is down", sNodeConnectorIdentifier);
- } else if (original_portstatus == true) {
- //port has come up
- LOG.debug("Node Connector {} updated port is up", sNodeConnectorIdentifier);
- }
- }
- }
+ public void update(InstanceIdentifier<FlowCapableNodeConnector> identifier, FlowCapableNodeConnector original,
+ FlowCapableNodeConnector update, InstanceIdentifier<FlowCapableNodeConnector> nodeConnIdent) {
+ // 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) {
- if (compareInstanceIdentifierTail(identifier,II_TO_FLOW_CAPABLE_NODE_CONNECTOR)){
-
- String sNodeConnectorIdentifier = getNodeConnectorId(String.valueOf(nodeConnIdent.firstKeyOf(NodeConnector.class).getId()));
- long nDpId = getDpIdFromPortName(sNodeConnectorIdentifier);
- if (isNodeOwner(getNodeId(nDpId))) {
- if (!dpnToPortMultiMap.containsEntry(nDpId, sNodeConnectorIdentifier)) {
- LOG.debug("Node Connector {} added", sNodeConnectorIdentifier);
- dpnToPortMultiMap.put(nDpId, sNodeConnectorIdentifier);
- sendNodeConnectorUpdation(nDpId);
- PortNameMapping.updatePortMap("openflow:" + nDpId + ":" + add.getName(), sNodeConnectorIdentifier, "ADD");
+ 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 (entityOwnershipUtils.isEntityOwner(FcapsConstants.SERVICE_ENTITY_TYPE,getNodeId(dataPathId))) {
+ if (!dpnToPortMultiMap.containsEntry(dataPathId, nodeConnectorIdentifier)) {
+ LOG.debug("Node Connector {} added", nodeConnectorIdentifier);
+ dpnToPortMultiMap.put(dataPathId, nodeConnectorIdentifier);
+ counter = packetInCounter.label("OFSwitch")
+ .label(String.valueOf(dataPathId)).label("portsperswitch");
+ counter.increment();
+ PortNameMapping.updatePortMap("openflow:" + dataPathId + ":" + add.getName(),
+ nodeConnectorIdentifier, "ADD");
} else {
LOG.error("Duplicate Event.Node Connector already added");
}
}
}
}
+
private String getNodeConnectorId(String node) {
- //Uri [_value=openflow:1:1]
- String temp[] = node.split("=");
- String dpnId = temp[1].substring(0,temp[1].length() - 1);
- return dpnId;
+ // Uri [_value=openflow:1:1]
+ String[] temp = node.split("=");
+ return temp[1].substring(0, temp[1].length() - 1);
}
- private String getNodeId(Long dpnId){
+ private String getNodeId(Long dpnId) {
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);
- Optional<EntityOwnershipState> eState = this.entityOwnershipService.getOwnershipState(entity);
- if(eState.isPresent()) {
- return eState.get().isOwner();
- }
- return false;
- }
private boolean compareInstanceIdentifierTail(InstanceIdentifier<?> identifier1,
- InstanceIdentifier<?> identifier2) {
- return Iterables.getLast(identifier1.getPathArguments()).equals(Iterables.getLast(identifier2.getPathArguments()));
+ InstanceIdentifier<?> identifier2) {
+ return Iterables.getLast(identifier1.getPathArguments())
+ .equals(Iterables.getLast(identifier2.getPathArguments()));
}
private long getDpIdFromPortName(String portName) {
return Long.parseLong(dpId);
}
- private void sendNodeConnectorUpdation(Long dpnId) {
- Collection<String> portname = dpnToPortMultiMap.get(dpnId);
- String nodeListPortsCountStr,counterkey;
- nodeListPortsCountStr = "dpnId_" + dpnId + "_NumberOfOFPorts";
- counterkey = "NumberOfOFPorts:" + nodeListPortsCountStr;
-
- if (portname.size()!=0) {
- nodeConnectorCountermap.put(counterkey, "" + portname.size());
- } else {
- nodeConnectorCountermap.remove(counterkey);
- }
- LOG.debug("NumberOfOFPorts:" + nodeListPortsCountStr + " portlistsize " + portname.size());
- pmAgent.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();
}
}