From: Robert Varga Date: Wed, 28 May 2014 15:13:50 +0000 (+0200) Subject: BUG-625: migrate InventoryAndReadAdapter X-Git-Tag: release/helium~700^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=97f6ae6ef4fa1f2f57402d4daf9605be1788968d;ds=sidebyside BUG-625: migrate InventoryAndReadAdapter This patch migrates InventoryAndReadAdapte from xtend to Java, finishing the conversion. Change-Id: I9e810283ec96c8906b0aa029691ab49dc793ffe8 Signed-off-by: Robert Varga --- diff --git a/opendaylight/md-sal/compatibility/sal-compatibility/pom.xml b/opendaylight/md-sal/compatibility/sal-compatibility/pom.xml index a145a3b4e1..6c79e053e1 100644 --- a/opendaylight/md-sal/compatibility/sal-compatibility/pom.xml +++ b/opendaylight/md-sal/compatibility/sal-compatibility/pom.xml @@ -42,10 +42,6 @@ - - org.eclipse.xtend - xtend-maven-plugin - org.jacoco jacoco-maven-plugin diff --git a/opendaylight/md-sal/compatibility/sal-compatibility/src/main/java/org/opendaylight/controller/sal/compatibility/InventoryAndReadAdapter.java b/opendaylight/md-sal/compatibility/sal-compatibility/src/main/java/org/opendaylight/controller/sal/compatibility/InventoryAndReadAdapter.java new file mode 100644 index 0000000000..2830165425 --- /dev/null +++ b/opendaylight/md-sal/compatibility/sal-compatibility/src/main/java/org/opendaylight/controller/sal/compatibility/InventoryAndReadAdapter.java @@ -0,0 +1,770 @@ +/** + * Copyright (c) 2014 Cisco Systems, Inc. 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.controller.sal.compatibility; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.CopyOnWriteArrayList; + +import org.opendaylight.controller.md.sal.binding.util.TypeSafeDataReader; +import org.opendaylight.controller.sal.binding.api.data.DataBrokerService; +import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction; +import org.opendaylight.controller.sal.binding.api.data.DataProviderService; +import org.opendaylight.controller.sal.core.ConstructionException; +import org.opendaylight.controller.sal.core.Edge; +import org.opendaylight.controller.sal.core.Node; +import org.opendaylight.controller.sal.core.NodeConnector; +import org.opendaylight.controller.sal.core.NodeTable; +import org.opendaylight.controller.sal.core.Property; +import org.opendaylight.controller.sal.core.UpdateType; +import org.opendaylight.controller.sal.inventory.IPluginInInventoryService; +import org.opendaylight.controller.sal.inventory.IPluginOutInventoryService; +import org.opendaylight.controller.sal.reader.FlowOnNode; +import org.opendaylight.controller.sal.reader.IPluginInReadService; +import org.opendaylight.controller.sal.reader.IPluginOutReadService; +import org.opendaylight.controller.sal.reader.NodeConnectorStatistics; +import org.opendaylight.controller.sal.reader.NodeDescription; +import org.opendaylight.controller.sal.reader.NodeTableStatistics; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsUpdate; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.OpendaylightFlowStatisticsListener; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.OpendaylightFlowStatisticsService; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdate; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsListener; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsService; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.and.statistics.map.FlowTableAndStatisticsMap; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatistics; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.FlowTopologyDiscoveryService; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.Link; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemoved; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.OpendaylightInventoryListener; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.GenericStatistics; +import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.node.connector.statistics.Bytes; +import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.node.connector.statistics.Packets; +import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatistics; +import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData; +import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetAllNodeConnectorsStatisticsInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetNodeConnectorStatisticsInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.NodeConnectorStatisticsUpdate; +import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.OpendaylightPortStatisticsListener; +import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.OpendaylightPortStatisticsService; +import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.node.connector.statistics.and.port.number.map.NodeConnectorStatisticsAndPortNumberMap; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class InventoryAndReadAdapter implements IPluginInReadService, IPluginInInventoryService, OpendaylightInventoryListener, OpendaylightFlowStatisticsListener, OpendaylightFlowTableStatisticsListener, OpendaylightPortStatisticsListener { + private static final Logger LOG = LoggerFactory.getLogger(InventoryAndReadAdapter.class); + private static final short OPENFLOWV10_TABLE_ID = 0; + + private final InventoryNotificationProvider inventoryNotificationProvider = new InventoryNotificationProvider(); + private final Map> nodeToNodeConnectorsMap = new ConcurrentHashMap<>(); + private List inventoryPublisher = new CopyOnWriteArrayList<>(); + private List statisticsPublisher = new CopyOnWriteArrayList<>(); + + private OpendaylightFlowTableStatisticsService flowTableStatisticsService; + private OpendaylightPortStatisticsService nodeConnectorStatisticsService; + private OpendaylightFlowStatisticsService flowStatisticsService; + private FlowTopologyDiscoveryService topologyDiscovery; + private DataProviderService dataProviderService; + private DataBrokerService dataService; + + public DataBrokerService getDataService() { + return dataService; + } + + public void setDataService(final DataBrokerService dataService) { + this.dataService = dataService; + } + + public DataProviderService getDataProviderService() { + return dataProviderService; + } + + public void setDataProviderService(final DataProviderService dataProviderService) { + this.dataProviderService = dataProviderService; + } + + public OpendaylightFlowStatisticsService getFlowStatisticsService() { + return flowStatisticsService; + } + + public void setFlowStatisticsService(final OpendaylightFlowStatisticsService flowStatisticsService) { + this.flowStatisticsService = flowStatisticsService; + } + + public OpendaylightPortStatisticsService getNodeConnectorStatisticsService() { + return nodeConnectorStatisticsService; + } + + public void setNodeConnectorStatisticsService(final OpendaylightPortStatisticsService nodeConnectorStatisticsService) { + this.nodeConnectorStatisticsService = nodeConnectorStatisticsService; + } + + public OpendaylightFlowTableStatisticsService getFlowTableStatisticsService() { + return flowTableStatisticsService; + } + + public void setFlowTableStatisticsService(final OpendaylightFlowTableStatisticsService flowTableStatisticsService) { + this.flowTableStatisticsService = flowTableStatisticsService; + } + + public FlowTopologyDiscoveryService getTopologyDiscovery() { + return topologyDiscovery; + } + + public void setTopologyDiscovery(final FlowTopologyDiscoveryService topologyDiscovery) { + this.topologyDiscovery = topologyDiscovery; + } + + public List getStatisticsPublisher() { + return statisticsPublisher; + } + + public void setStatisticsPublisher(final List statisticsPublisher) { + this.statisticsPublisher = statisticsPublisher; + } + + public List getInventoryPublisher() { + return inventoryPublisher; + } + + public void setInventoryPublisher(final List inventoryPublisher) { + this.inventoryPublisher = inventoryPublisher; + } + + public void startAdapter() { + inventoryNotificationProvider.setDataProviderService(getDataProviderService()); + inventoryNotificationProvider.setInventoryPublisher(getInventoryPublisher()); + // inventoryNotificationProvider.start(); + } + + public boolean setInventoryPublisher(final IPluginOutInventoryService listener) { + return getInventoryPublisher().add(listener); + } + + public boolean unsetInventoryPublisher(final IPluginOutInventoryService listener) { + return getInventoryPublisher().remove(listener); + } + + public boolean setReadPublisher(final IPluginOutReadService listener) { + return getStatisticsPublisher().add(listener); + } + + public Boolean unsetReadPublisher(final IPluginOutReadService listener) { + if (listener != null) { + return getStatisticsPublisher().remove(listener); + } + return false; + } + + protected DataModificationTransaction startChange() { + return getDataProviderService().beginTransaction(); + } + + @Override + public long getTransmitRate(final NodeConnector connector) { + final FlowCapableNodeConnector nodeConnector = this.readOperFlowCapableNodeConnector(NodeMapping.toNodeConnectorRef(connector)); + return nodeConnector.getCurrentSpeed().longValue(); + } + + private FlowCapableNode readOperFlowCapableNode(final NodeRef ref) { + final org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node node = + (org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node)getDataService().readOperationalData(ref.getValue()); + if (node == null) { + return null; + } + + return node.getAugmentation(FlowCapableNode.class); + } + + private org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node readConfigNode(final Node node) { + final InstanceIdentifier nodeRef = + InstanceIdentifier.builder(Nodes.class) + .child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node.class, InventoryMapping.toNodeKey(node)) + .build(); + + return (org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node) startChange().readConfigurationData(nodeRef); + } + + private org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector readConfigNodeConnector(final NodeConnector connector) { + final InstanceIdentifier nodeConnectorRef = + InstanceIdentifier.builder(Nodes.class) + .child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node.class, InventoryMapping.toNodeKey(connector.getNode())) + .child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector.class, InventoryMapping.toNodeConnectorKey(connector)) + .toInstance(); + + return((org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector) startChange().readConfigurationData(nodeConnectorRef)); + } + + /** + * Read a table of a node from configuration data store. + * + * @param node Node id + * @param id Table id + * @return Table contents, or null if not present + */ + private Table readConfigTable(final Node node, final short id) { + final InstanceIdentifier tableRef = InstanceIdentifier.builder(Nodes.class) + .child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node.class, InventoryMapping.toNodeKey(node)) + .augmentation(FlowCapableNode.class) + .child(Table.class, new TableKey(id)) + .build(); + + return (Table) startChange().readConfigurationData(tableRef); + } + + @Override + public List readAllFlow(final Node node, final boolean cached) { + final ArrayList output = new ArrayList<>(); + final Table table = readConfigTable(node, OPENFLOWV10_TABLE_ID); + if (table != null) { + final List flows = table.getFlow(); + LOG.trace("Number of flows installed in table 0 of node {} : {}", node, flows.size()); + + for (final Flow flow : flows) { + final FlowStatisticsData statsFromDataStore = flow.getAugmentation(FlowStatisticsData.class); + if (statsFromDataStore != null) { + final FlowOnNode it = new FlowOnNode(ToSalConversionsUtils.toFlow(flow, node)); + output.add(addFlowStats(it, statsFromDataStore.getFlowStatistics())); + } + } + } + + // TODO (main): Shall we send request to the switch? It will make async request to the switch. + // Once the plugin receives a response, it will let the adaptor know through onFlowStatisticsUpdate() + // If we assume that md-sal statistics manager will always be running, then it is not required + // But if not, then sending request will collect the latest data for adaptor at least. + getFlowStatisticsService().getAllFlowsStatisticsFromAllFlowTables( + new GetAllFlowsStatisticsFromAllFlowTablesInputBuilder().setNode(NodeMapping.toNodeRef(node)).build()); + return output; + } + + @Override + public List readAllNodeConnector(final Node node, final boolean cached) { + final ArrayList ret = new ArrayList<>(); + + final org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node dsNode = readConfigNode(node); + if (dsNode != null) { + for (final org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector dsNodeConnector : dsNode.getNodeConnector()) { + final FlowCapableNodeConnectorStatistics stats = (dsNodeConnector.getAugmentation(FlowCapableNodeConnectorStatisticsData.class)); + if (stats != null) { + try { + ret.add(toNodeConnectorStatistics(stats.getFlowCapableNodeConnectorStatistics(), dsNode.getId(), dsNodeConnector.getId())); + } catch (ConstructionException e) { + LOG.warn("Failed to instantiate node connector statistics for node {} connector {}, ignoring it", + dsNode.getId(), dsNodeConnector.getId(), e); + } + } + } + } + + //TODO: Refer TODO (main) + getNodeConnectorStatisticsService().getAllNodeConnectorsStatistics( + new GetAllNodeConnectorsStatisticsInputBuilder().setNode(NodeMapping.toNodeRef(node)).build()); + return ret; + } + + @Override + public List readAllNodeTable(final Node node, final boolean cached) { + final NodeRef nodeRef = NodeMapping.toNodeRef(node); + + final ArrayList ret = new ArrayList<>(); + final FlowCapableNode dsFlowCapableNode = this.readOperFlowCapableNode(nodeRef); + if (dsFlowCapableNode != null) { + for (final Table table : dsFlowCapableNode.getTable()) { + final FlowTableStatisticsData tableStats = table.getAugmentation(FlowTableStatisticsData.class); + if (tableStats != null) { + try { + ret.add(toNodeTableStatistics(tableStats.getFlowTableStatistics(), table.getId(), node)); + } catch (ConstructionException e) { + LOG.warn("Failed to instantiate table statistics for node {} table {}, ignoring it", node, table.getId(), e); + } + } + } + } + + //TODO: Refer TODO (main) + getFlowTableStatisticsService().getFlowTablesStatistics(new GetFlowTablesStatisticsInputBuilder().setNode(nodeRef).build()); + return ret; + } + + @Override + public NodeDescription readDescription(final Node node, final boolean cached) { + return this.toNodeDescription(NodeMapping.toNodeRef(node)); + } + + @Override + public FlowOnNode readFlow(final Node node, final org.opendaylight.controller.sal.flowprogrammer.Flow targetFlow, final boolean cached) { + FlowOnNode ret = null; + final Table table = readConfigTable(node, OPENFLOWV10_TABLE_ID); + if (table != null) { + final List flows = table.getFlow(); + InventoryAndReadAdapter.LOG.trace("Number of flows installed in table 0 of node {} : {}", node, flows.size()); + + for (final Flow mdsalFlow : flows) { + if(FromSalConversionsUtils.flowEquals(mdsalFlow, MDFlowMapping.toMDSalflow(targetFlow))) { + final FlowStatisticsData statsFromDataStore = mdsalFlow.getAugmentation(FlowStatisticsData.class); + if (statsFromDataStore != null) { + InventoryAndReadAdapter.LOG.debug("Found matching flow in the data store flow table "); + ret = addFlowStats(new FlowOnNode(targetFlow), statsFromDataStore.getFlowStatistics()); + + // FIXME: break; ? + } + } + } + } + + //TODO: Refer TODO (main) + final GetFlowStatisticsFromFlowTableInputBuilder input = new GetFlowStatisticsFromFlowTableInputBuilder().setNode(NodeMapping.toNodeRef(node)); + input.fieldsFrom(MDFlowMapping.toMDSalflow(targetFlow)); + getFlowStatisticsService().getFlowStatisticsFromFlowTable(input.build()); + return ret; + } + + @Override + public NodeConnectorStatistics readNodeConnector(final NodeConnector connector, final boolean cached) { + final NodeConnectorId ncId = InventoryMapping.toNodeConnectorKey(connector).getId(); + + NodeConnectorStatistics ret = null; + final org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector nodeConnectorFromDS = readConfigNodeConnector(connector); + if (nodeConnectorFromDS != null) { + final FlowCapableNodeConnectorStatistics stats = nodeConnectorFromDS.getAugmentation(FlowCapableNodeConnectorStatisticsData.class); + if (stats != null) { + try { + ret = toNodeConnectorStatistics(stats.getFlowCapableNodeConnectorStatistics(), + InventoryMapping.toNodeKey(connector.getNode()).getId(), ncId); + } catch (ConstructionException e) { + LOG.warn("Failed to instantiate node connector statistics for connector {}, ignoring it", + connector, e); + } + } + } + + getNodeConnectorStatisticsService().getNodeConnectorStatistics( + new GetNodeConnectorStatisticsInputBuilder().setNode(NodeMapping.toNodeRef(connector.getNode())).setNodeConnectorId(ncId).build()); + return ret; + } + + @Override + public NodeTableStatistics readNodeTable(final NodeTable nodeTable, final boolean cached) { + NodeTableStatistics nodeStats = null; + final Table table = readConfigTable(nodeTable.getNode(), (short) nodeTable.getID()); + if (table != null) { + final FlowTableStatisticsData tableStats = table.getAugmentation(FlowTableStatisticsData.class); + if (tableStats != null) { + try { + nodeStats = toNodeTableStatistics(tableStats.getFlowTableStatistics(), table.getId(), nodeTable.getNode()); + } catch (ConstructionException e) { + LOG.warn("Failed to instantiate table statistics for node {} table {}, ignoring it", + nodeTable.getNode(), table.getId(), e); + } + } + } + + //TODO: Refer TODO (main) + getFlowTableStatisticsService().getFlowTablesStatistics( + new GetFlowTablesStatisticsInputBuilder().setNode(NodeMapping.toNodeRef(nodeTable.getNode())).build()); + return nodeStats; + } + + @Override + public void onNodeConnectorRemoved(final NodeConnectorRemoved update) { + // Never received + } + + @Override + public void onNodeRemoved(final NodeRemoved notification) { + this.removeNodeConnectors(notification.getNodeRef().getValue()); + try { + final Node aDNode = NodeMapping.toADNode(notification.getNodeRef()); + this.publishNodeUpdate(aDNode, UpdateType.REMOVED, Collections.emptySet()); + } catch (ConstructionException e) { + LOG.warn("Failed to construct node for {}, not propagating update", notification.getNodeRef(), e); + } + } + + @Override + public void onNodeConnectorUpdated(final NodeConnectorUpdated update) { + final NodeConnectorRef ref = update.getNodeConnectorRef(); + final UpdateType updateType; + if (!this.isKnownNodeConnector(ref.getValue())) { + this.recordNodeConnector(ref.getValue()); + updateType = UpdateType.ADDED; + } else { + updateType = UpdateType.CHANGED; + } + + try { + final NodeConnector nodeConnector; + nodeConnector = NodeMapping.toADNodeConnector(ref); + this.publishNodeConnectorUpdate(nodeConnector, updateType, NodeMapping.toADNodeConnectorProperties(update)); + } catch (ConstructionException e) { + LOG.warn("Failed to construct node connector for {}, not reporting the update", ref, e); + } + } + + @Override + public void onNodeUpdated(final NodeUpdated notification) { + final NodeRef ref = notification.getNodeRef(); + + final UpdateType updateType; + if (dataService.readOperationalData(ref.getValue()) == null) { + updateType = UpdateType.ADDED; + } else { + updateType = UpdateType.CHANGED; + } + + final Node aDNode; + try { + aDNode = NodeMapping.toADNode(ref); + } catch (ConstructionException e) { + LOG.warn("Failed to construct node for {}, not reporting the update", ref, e); + return; + } + + this.publishNodeUpdate(aDNode, updateType, NodeMapping.toADNodeProperties(notification)); + for (final IPluginOutReadService statsPublisher : getStatisticsPublisher()) { + final NodeDescription description = this.toNodeDescription(ref); + if (description != null) { + final InstanceIdentifier nodeRef = + InstanceIdentifier.builder(Nodes.class) + .child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node.class, new NodeKey(notification.getId())) + .toInstance(); + try { + statsPublisher.descriptionStatisticsUpdated(NodeMapping.toADNode(nodeRef), description); + } catch (ConstructionException e) { + LOG.warn("Failed to construct node for {}, not reporting the update to publisher {}", nodeRef, statsPublisher, e); + } + } + } + } + + @Override + public ConcurrentMap> getNodeProps() { + final ConcurrentHashMap> props = new ConcurrentHashMap<>(); + final Nodes nodes = this.readOperAllMDNodes(); + for (final org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node node : nodes.getNode()) { + final FlowCapableNode fcn = node.getAugmentation(FlowCapableNode.class); + if (fcn != null) { + final ConcurrentHashMap perNodePropMap = new ConcurrentHashMap(); + final HashSet perNodeProps = NodeMapping.toADNodeProperties(fcn, node.getId()); + if (perNodeProps != null) { + for (final Property perNodeProp : perNodeProps) { + perNodePropMap.put(perNodeProp.getName(), perNodeProp); + } + } + + try { + final Node adNode = new Node(NodeMapping.MD_SAL_TYPE, NodeMapping.toADNodeId(node.getId())); + props.put(adNode, perNodePropMap); + } catch (ConstructionException e) { + LOG.warn("Failed to construct node for {}, skipping it", node, e); + } + } + } + return props; + } + + private Nodes readOperAllMDNodes() { + final TypeSafeDataReader reader = TypeSafeDataReader.forReader(getDataService()); + return reader.readOperationalData(InstanceIdentifier.builder(Nodes.class).build()); + } + + @Override + public ConcurrentMap> getNodeConnectorProps(final Boolean refresh) { + final ConcurrentHashMap> props = new ConcurrentHashMap<>(); + for (final org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node node : this.readOperAllMDNodes().getNode()) { + for (final org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector nc : node.getNodeConnector()) { + final FlowCapableNodeConnector fcnc = nc.getAugmentation(FlowCapableNodeConnector.class); + if (fcnc != null) { + final ConcurrentHashMap ncpsm = new ConcurrentHashMap<>(); + final HashSet ncps = NodeMapping.toADNodeConnectorProperties(fcnc); + if (ncps != null) { + for (final Property p : ncps) { + ncpsm.put(p.getName(), p); + } + } + + try { + props.put(NodeMapping.toADNodeConnector(nc.getId(), node.getId()), ncpsm); + } catch (ConstructionException e) { + LOG.warn("Failed to instantiate node {} connector {}, not reporting it", node.getId(), nc.getId(), e); + } + } + } + } + return props; + } + + private FlowCapableNodeConnector readOperFlowCapableNodeConnector(final NodeConnectorRef ref) { + final org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector nc = + (org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector) + getDataService().readOperationalData(ref.getValue()); + return nc.getAugmentation(FlowCapableNodeConnector.class); + } + + private static NodeConnectorStatistics toNodeConnectorStatistics(final org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.NodeConnectorStatistics nodeConnectorStatistics, final NodeId nodeId, final NodeConnectorId nodeConnectorId) throws ConstructionException { + final NodeConnectorStatistics it = new NodeConnectorStatistics(); + + final Packets packets = nodeConnectorStatistics.getPackets(); + it.setReceivePacketCount(packets.getReceived().longValue()); + it.setTransmitPacketCount(packets.getTransmitted().longValue()); + + final Bytes bytes = nodeConnectorStatistics.getBytes(); + it.setReceiveByteCount(bytes.getReceived().longValue()); + it.setTransmitByteCount(bytes.getTransmitted().longValue()); + + it.setReceiveDropCount(nodeConnectorStatistics.getReceiveDrops().longValue()); + it.setTransmitDropCount(nodeConnectorStatistics.getTransmitDrops().longValue()); + it.setReceiveErrorCount(nodeConnectorStatistics.getReceiveErrors().longValue()); + it.setTransmitErrorCount(nodeConnectorStatistics.getTransmitErrors().longValue()); + it.setReceiveFrameErrorCount(nodeConnectorStatistics.getReceiveFrameError().longValue()); + it.setReceiveOverRunErrorCount(nodeConnectorStatistics.getReceiveOverRunError().longValue()); + it.setReceiveCRCErrorCount(nodeConnectorStatistics.getReceiveCrcError().longValue()); + it.setCollisionCount(nodeConnectorStatistics.getCollisionCount().longValue()); + + final InstanceIdentifier nodeConnectorRef = + InstanceIdentifier.builder(Nodes.class) + .child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node.class, new NodeKey(nodeId)) + .child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector.class, new NodeConnectorKey(nodeConnectorId)) + .build(); + it.setNodeConnector(NodeMapping.toADNodeConnector(new NodeConnectorRef(nodeConnectorRef))); + return it; + } + + private static NodeTableStatistics toNodeTableStatistics(final FlowTableStatistics tableStats, final Short tableId, final Node node) throws ConstructionException { + final NodeTableStatistics it = new NodeTableStatistics(); + it.setActiveCount(tableStats.getActiveFlows().getValue().intValue()); + it.setLookupCount(tableStats.getPacketsLookedUp().getValue().longValue()); + it.setMatchedCount(tableStats.getPacketsMatched().getValue().longValue()); + it.setName(tableId.toString()); + it.setNodeTable(new NodeTable(NodeMapping.MD_SAL_TYPE, tableId, node)); + return it; + } + + private NodeDescription toNodeDescription(final NodeRef nodeRef) { + final FlowCapableNode capableNode = this.readOperFlowCapableNode(nodeRef); + if (capableNode == null) { + return null; + } + + final NodeDescription it = new NodeDescription(); + it.setManufacturer(capableNode.getManufacturer()); + it.setSerialNumber(capableNode.getSerialNumber()); + it.setSoftware(capableNode.getSoftware()); + it.setDescription(capableNode.getDescription()); + return it; + } + + public Edge toADEdge(final Link link) throws ConstructionException { + NodeConnectorRef _source = link.getSource(); + NodeConnector _aDNodeConnector = NodeMapping.toADNodeConnector(_source); + NodeConnectorRef _destination = link.getDestination(); + NodeConnector _aDNodeConnector_1 = NodeMapping.toADNodeConnector(_destination); + Edge _edge = new Edge(_aDNodeConnector, _aDNodeConnector_1); + return _edge; + } + + /** + * OpendaylightFlowStatisticsListener interface implementation + */ + @Override + public void onAggregateFlowStatisticsUpdate(final AggregateFlowStatisticsUpdate notification) { + // Ignoring this notification as there does not seem to be a way to bubble this up to AD-SAL + } + + @Override + public void onFlowsStatisticsUpdate(final FlowsStatisticsUpdate notification) { + final ArrayList adsalFlowsStatistics = new ArrayList<>(); + final InstanceIdentifier nodeRef = + InstanceIdentifier.builder(Nodes.class) + .child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node.class, new NodeKey(notification.getId())) + .build(); + + final Node aDNode; + try { + aDNode = NodeMapping.toADNode(nodeRef); + } catch (ConstructionException e) { + LOG.warn("Failed to construct node for {}, ignoring it", notification.getId(), e); + return; + } + + for (final FlowAndStatisticsMapList flowStats : notification.getFlowAndStatisticsMapList()) { + if (flowStats.getTableId() == 0) { + adsalFlowsStatistics.add(InventoryAndReadAdapter.toFlowOnNode(flowStats, aDNode)); + } + } + for (final IPluginOutReadService statsPublisher : getStatisticsPublisher()) { + statsPublisher.nodeFlowStatisticsUpdated(aDNode, adsalFlowsStatistics); + } + } + + /** + * OpendaylightFlowTableStatisticsListener interface implementation + */ + @Override + public void onFlowTableStatisticsUpdate(final FlowTableStatisticsUpdate notification) { + ArrayList adsalFlowTableStatistics = new ArrayList<>(); + for (final FlowTableAndStatisticsMap stats : notification.getFlowTableAndStatisticsMap()) { + if (stats.getTableId().getValue() == 0) { + final NodeTableStatistics it = new NodeTableStatistics(); + it.setActiveCount(stats.getActiveFlows().getValue().intValue()); + it.setLookupCount(stats.getPacketsLookedUp().getValue().longValue()); + it.setMatchedCount(stats.getPacketsMatched().getValue().longValue()); + adsalFlowTableStatistics.add(it); + } + } + + final InstanceIdentifier nodeRef = + InstanceIdentifier.builder(Nodes.class) + .child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node.class, new NodeKey(notification.getId())) + .build(); + + final Node aDNode; + try { + aDNode = NodeMapping.toADNode(nodeRef); + } catch (ConstructionException e) { + LOG.warn("Failed to construct node for {}, ignoring it", notification.getId(), e); + return; + } + + for (final IPluginOutReadService statsPublisher : getStatisticsPublisher()) { + statsPublisher.nodeTableStatisticsUpdated(aDNode, adsalFlowTableStatistics); + } + } + + /** + * OpendaylightPortStatisticsUpdate interface implementation + */ + @Override + public void onNodeConnectorStatisticsUpdate(final NodeConnectorStatisticsUpdate notification) { + final ArrayList adsalPortStatistics = new ArrayList(); + for (final NodeConnectorStatisticsAndPortNumberMap nodeConnectorStatistics : notification.getNodeConnectorStatisticsAndPortNumberMap()) { + try { + adsalPortStatistics.add(toNodeConnectorStatistics( + nodeConnectorStatistics, notification.getId(), nodeConnectorStatistics.getNodeConnectorId())); + } catch (ConstructionException e) { + LOG.warn("Failed to create statistics for node {} connector {}, not updating them", + notification.getId(), nodeConnectorStatistics.getNodeConnectorId(), e); + } + } + + final InstanceIdentifier nodeRef = + InstanceIdentifier.builder(Nodes.class) + .child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node.class, new NodeKey(notification.getId())) + .build(); + + final Node aDNode; + try { + aDNode = NodeMapping.toADNode(nodeRef); + } catch (ConstructionException e) { + LOG.warn("Failed to construct node for {}, ignoring it", notification.getId(), e); + return; + } + + for (final IPluginOutReadService statsPublisher : getStatisticsPublisher()) { + statsPublisher.nodeConnectorStatisticsUpdated(aDNode, adsalPortStatistics); + } + } + + private static FlowOnNode toFlowOnNode(final FlowAndStatisticsMapList flowAndStatsMap, final Node node) { + final FlowOnNode it = new FlowOnNode(ToSalConversionsUtils.toFlow(flowAndStatsMap, node)); + return addFlowStats(it, flowAndStatsMap); + } + + private static FlowOnNode addFlowStats(final FlowOnNode node, final GenericStatistics stats) { + node.setByteCount(stats.getByteCount().getValue().longValue()); + node.setPacketCount(stats.getPacketCount().getValue().longValue()); + node.setDurationSeconds(stats.getDuration().getSecond().getValue().intValue()); + node.setDurationNanoseconds(stats.getDuration().getNanosecond().getValue().intValue()); + return node; + } + + @Override + public Set getConfiguredNotConnectedNodes() { + return Collections.emptySet(); + } + + private void publishNodeUpdate(final Node node, final UpdateType updateType, final Set properties) { + for (final IPluginOutInventoryService publisher : getInventoryPublisher()) { + publisher.updateNode(node, updateType, properties); + } + } + + private void publishNodeConnectorUpdate(final NodeConnector nodeConnector, final UpdateType updateType, final Set properties) { + for (final IPluginOutInventoryService publisher : getInventoryPublisher()) { + publisher.updateNodeConnector(nodeConnector, updateType, properties); + } + } + + private boolean isKnownNodeConnector(final InstanceIdentifier nodeConnectorIdentifier) { + final List path = nodeConnectorIdentifier.getPath(); + if (path.size() >= 3) { + final PathArgument nodePath = path.get(1); + final PathArgument nodeConnectorPath = path.get(2); + final List nodeConnectors = nodeToNodeConnectorsMap.get(nodePath); + if (nodeConnectors != null) { + return nodeConnectors.contains(nodeConnectorPath); + } + } + return false; + } + + private boolean recordNodeConnector(final InstanceIdentifier nodeConnectorIdentifier) { + final List path = nodeConnectorIdentifier.getPath(); + if (path.size() < 3) { + return false; + } + + final PathArgument nodePath = path.get(1); + final PathArgument nodeConnectorPath = path.get(2); + + synchronized (this) { + List nodeConnectors = this.nodeToNodeConnectorsMap.get(nodePath); + if (nodeConnectors == null) { + nodeConnectors = new ArrayList<>(); + this.nodeToNodeConnectorsMap.put(nodePath, nodeConnectors); + } + + return nodeConnectors.add(nodeConnectorPath); + } + } + + private List removeNodeConnectors(final InstanceIdentifier nodeIdentifier) { + return this.nodeToNodeConnectorsMap.remove(nodeIdentifier.getPath().get(1)); + } +} diff --git a/opendaylight/md-sal/compatibility/sal-compatibility/src/main/java/org/opendaylight/controller/sal/compatibility/InventoryAndReadAdapter.xtend b/opendaylight/md-sal/compatibility/sal-compatibility/src/main/java/org/opendaylight/controller/sal/compatibility/InventoryAndReadAdapter.xtend deleted file mode 100644 index 8908504f15..0000000000 --- a/opendaylight/md-sal/compatibility/sal-compatibility/src/main/java/org/opendaylight/controller/sal/compatibility/InventoryAndReadAdapter.xtend +++ /dev/null @@ -1,683 +0,0 @@ -/* - * Copyright (c) 2014 Cisco Systems, Inc. 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.controller.sal.compatibility - -import java.util.ArrayList -import java.util.Collections -import java.util.List -import java.util.Map -import java.util.Set -import java.util.concurrent.ConcurrentHashMap -import java.util.concurrent.CopyOnWriteArrayList -import java.util.concurrent.locks.Lock -import java.util.concurrent.locks.ReentrantLock -import org.opendaylight.controller.md.sal.binding.util.TypeSafeDataReader -import org.opendaylight.controller.sal.binding.api.data.DataBrokerService -import org.opendaylight.controller.sal.binding.api.data.DataProviderService -import org.opendaylight.controller.sal.core.Edge -import org.opendaylight.controller.sal.core.Node -import org.opendaylight.controller.sal.core.NodeTable -import org.opendaylight.controller.sal.core.UpdateType -import org.opendaylight.controller.sal.flowprogrammer.Flow -import org.opendaylight.controller.sal.inventory.IPluginInInventoryService -import org.opendaylight.controller.sal.inventory.IPluginOutInventoryService -import org.opendaylight.controller.sal.reader.FlowOnNode -import org.opendaylight.controller.sal.reader.IPluginInReadService -import org.opendaylight.controller.sal.reader.IPluginOutReadService -import org.opendaylight.controller.sal.reader.NodeConnectorStatistics -import org.opendaylight.controller.sal.reader.NodeDescription -import org.opendaylight.controller.sal.reader.NodeTableStatistics -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsUpdate -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesInputBuilder -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableInputBuilder -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.OpendaylightFlowStatisticsListener -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.OpendaylightFlowStatisticsService -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdate -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsInputBuilder -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsListener -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsService -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatistics -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.FlowTopologyDiscoveryService -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.Link -import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId -import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef -import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemoved -import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated -import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId -import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef -import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved -import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated -import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes -import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.OpendaylightInventoryListener -import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector -import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey -import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey -import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatistics -import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData -import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetAllNodeConnectorsStatisticsInputBuilder -import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetNodeConnectorStatisticsInputBuilder -import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.NodeConnectorStatisticsUpdate -import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.OpendaylightPortStatisticsListener -import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.OpendaylightPortStatisticsService -import org.opendaylight.yangtools.yang.binding.DataObject -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier -import org.slf4j.LoggerFactory - -import static extension org.opendaylight.controller.sal.common.util.Arguments.* -import static extension org.opendaylight.controller.sal.compatibility.NodeMapping.* - -class InventoryAndReadAdapter implements IPluginInReadService, - IPluginInInventoryService, - OpendaylightInventoryListener, - OpendaylightFlowStatisticsListener, - OpendaylightFlowTableStatisticsListener, - OpendaylightPortStatisticsListener { - - private static val LOG = LoggerFactory.getLogger(InventoryAndReadAdapter); - - private static val OPENFLOWV10_TABLE_ID = new Integer(0).shortValue; - @Property - DataBrokerService dataService; - - @Property - DataProviderService dataProviderService; - - @Property - OpendaylightFlowStatisticsService flowStatisticsService; - - @Property - OpendaylightPortStatisticsService nodeConnectorStatisticsService; - - @Property - OpendaylightFlowTableStatisticsService flowTableStatisticsService; - - @Property - FlowTopologyDiscoveryService topologyDiscovery; - - @Property - List statisticsPublisher = new CopyOnWriteArrayList(); - - @Property - List inventoryPublisher = new CopyOnWriteArrayList(); - - private final InventoryNotificationProvider inventoryNotificationProvider = new InventoryNotificationProvider(); - - private final Map> nodeToNodeConnectorsMap = new ConcurrentHashMap>(); - - private final Lock nodeToNodeConnectorsLock = new ReentrantLock(); - - - def startAdapter(){ - inventoryNotificationProvider.dataProviderService = dataProviderService; - inventoryNotificationProvider.inventoryPublisher = inventoryPublisher; - // inventoryNotificationProvider.start(); - } - - def start(){ - } - - def setInventoryPublisher(IPluginOutInventoryService listener){ - inventoryPublisher.add(listener); - } - - def unsetInventoryPublisher(IPluginOutInventoryService listener){ - inventoryPublisher.remove(listener); - } - - def setReadPublisher(IPluginOutReadService listener) { - statisticsPublisher.add(listener); - } - - def unsetReadPublisher (IPluginOutReadService listener) { - if( listener != null) - statisticsPublisher.remove(listener); - } - - protected def startChange() { - return dataProviderService.beginTransaction; - } - - override getTransmitRate(org.opendaylight.controller.sal.core.NodeConnector connector) { - val nodeConnector = readFlowCapableNodeConnector(connector.toNodeConnectorRef); - return nodeConnector.currentSpeed - } - - override readAllFlow(Node node, boolean cached) { - - val output = new ArrayList(); - val tableRef = InstanceIdentifier.builder(Nodes) - .child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node, InventoryMapping.toNodeKey(node)) - .augmentation(FlowCapableNode).child(Table, new TableKey(OPENFLOWV10_TABLE_ID)).toInstance(); - - val it = this.startChange(); - - val table= it.readConfigurationData(tableRef) as Table; - - if(table != null){ - LOG.trace("Number of flows installed in table 0 of node {} : {}",node,table.flow.size); - - for(flow : table.flow){ - - val adsalFlow = ToSalConversionsUtils.toFlow(flow,node); - val statsFromDataStore = flow.getAugmentation(FlowStatisticsData); - - if(statsFromDataStore != null){ - val it = new FlowOnNode(adsalFlow); - byteCount = statsFromDataStore.flowStatistics.byteCount.value.longValue; - packetCount = statsFromDataStore.flowStatistics.packetCount.value.longValue; - durationSeconds = statsFromDataStore.flowStatistics.duration.second.value.intValue; - durationNanoseconds = statsFromDataStore.flowStatistics.duration.nanosecond.value.intValue; - - output.add(it); - } - } - } - - //TODO (main): Shell we send request to the switch? It will make async request to the switch. - // Once plugin receive response, it will let adaptor know through onFlowStatisticsUpdate() - // If we assume that md-sal statistics manager will always be running, then its not required - // But if not, then sending request will collect the latest data for adaptor atleast. - val input = new GetAllFlowsStatisticsFromAllFlowTablesInputBuilder; - input.setNode(node.toNodeRef); - flowStatisticsService.getAllFlowsStatisticsFromAllFlowTables(input.build) - - return output; - } - - override readAllNodeConnector(Node node, boolean cached) { - - val ret = new ArrayList(); - val nodeRef = InstanceIdentifier.builder(Nodes) - .child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node, InventoryMapping.toNodeKey(node)) - .toInstance(); - - val provider = this.startChange(); - - val dsNode= provider.readConfigurationData(nodeRef) as org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node; - - if(dsNode != null){ - - for (dsNodeConnector : dsNode.nodeConnector){ - val nodeConnectorRef = InstanceIdentifier.builder(Nodes) - .child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node, InventoryMapping.toNodeKey(node)) - .child(NodeConnector, dsNodeConnector.key) - .toInstance(); - - val nodeConnectorFromDS = provider.readConfigurationData(nodeConnectorRef) as NodeConnector; - - if(nodeConnectorFromDS != null){ - val nodeConnectorStatsFromDs = nodeConnectorFromDS.getAugmentation(FlowCapableNodeConnectorStatisticsData) as FlowCapableNodeConnectorStatistics; - - ret.add(toNodeConnectorStatistics(nodeConnectorStatsFromDs.flowCapableNodeConnectorStatistics,dsNode.id,dsNodeConnector.id)); - } - } - } - - //TODO: Refer TODO (main) - val input = new GetAllNodeConnectorsStatisticsInputBuilder(); - input.setNode(node.toNodeRef); - nodeConnectorStatisticsService.getAllNodeConnectorsStatistics(input.build()); - return ret; - } - - override readAllNodeTable(Node node, boolean cached) { - val ret = new ArrayList(); - - val dsFlowCapableNode= readFlowCapableNode(node.toNodeRef) - - if(dsFlowCapableNode != null){ - - for (table : dsFlowCapableNode.table){ - - val tableStats = table.getAugmentation(FlowTableStatisticsData); - - if(tableStats != null){ - ret.add(toNodeTableStatistics(tableStats.flowTableStatistics,table.id,node)); - } - } - } - - //TODO: Refer TODO (main) - val input = new GetFlowTablesStatisticsInputBuilder(); - input.setNode(node.toNodeRef); - flowTableStatisticsService.getFlowTablesStatistics(input.build); - return ret; - } - - override readDescription(Node node, boolean cached) { - return toNodeDescription(node.toNodeRef); - } - - override readFlow(Node node, Flow targetFlow, boolean cached) { - var FlowOnNode ret= null; - - val tableRef = InstanceIdentifier.builder(Nodes) - .child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node, InventoryMapping.toNodeKey(node)) - .augmentation(FlowCapableNode).child(Table, new TableKey(OPENFLOWV10_TABLE_ID)).toInstance(); - - val it = this.startChange(); - - val table= it.readConfigurationData(tableRef) as Table; - - if(table != null){ - LOG.trace("Number of flows installed in table 0 of node {} : {}",node,table.flow.size); - - for(mdsalFlow : table.flow){ - if(FromSalConversionsUtils.flowEquals(mdsalFlow, MDFlowMapping.toMDSalflow(targetFlow))){ - val statsFromDataStore = mdsalFlow.getAugmentation(FlowStatisticsData); - - if(statsFromDataStore != null){ - LOG.debug("Found matching flow in the data store flow table "); - val it = new FlowOnNode(targetFlow); - byteCount = statsFromDataStore.flowStatistics.byteCount.value.longValue; - packetCount = statsFromDataStore.flowStatistics.packetCount.value.longValue; - durationSeconds = statsFromDataStore.flowStatistics.duration.second.value.intValue; - durationNanoseconds = statsFromDataStore.flowStatistics.duration.nanosecond.value.intValue; - - ret = it; - } - } - } - } - - //TODO: Refer TODO (main) - val input = new GetFlowStatisticsFromFlowTableInputBuilder; - input.setNode(node.toNodeRef); - input.fieldsFrom(MDFlowMapping.toMDSalflow(targetFlow)); - flowStatisticsService.getFlowStatisticsFromFlowTable(input.build) - - return ret; - - } - - override readNodeConnector(org.opendaylight.controller.sal.core.NodeConnector connector, boolean cached) { - var NodeConnectorStatistics nodeConnectorStatistics = null; - - val nodeConnectorRef = InstanceIdentifier.builder(Nodes) - .child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node, InventoryMapping.toNodeKey(connector.node)) - .child(NodeConnector, InventoryMapping.toNodeConnectorKey(connector)) - .toInstance(); - val provider = this.startChange(); - - val nodeConnectorFromDS = provider.readConfigurationData(nodeConnectorRef) as NodeConnector; - - if(nodeConnectorFromDS != null){ - val nodeConnectorStatsFromDs = nodeConnectorFromDS.getAugmentation(FlowCapableNodeConnectorStatisticsData) as FlowCapableNodeConnectorStatistics; - if(nodeConnectorStatsFromDs != null) { - nodeConnectorStatistics = toNodeConnectorStatistics(nodeConnectorStatsFromDs.flowCapableNodeConnectorStatistics, - InventoryMapping.toNodeKey(connector.node).id, - InventoryMapping.toNodeConnectorKey(connector).id); - } - } - - //TODO: Refer TODO (main) - val input = new GetNodeConnectorStatisticsInputBuilder(); - input.setNode(connector.node.toNodeRef); - input.setNodeConnectorId(InventoryMapping.toNodeConnectorKey(connector).id); - nodeConnectorStatisticsService.getNodeConnectorStatistics(input.build()); - return nodeConnectorStatistics; - } - - override readNodeTable(NodeTable nodeTable, boolean cached) { - var NodeTableStatistics nodeStats = null - - val tableRef = InstanceIdentifier.builder(Nodes) - .child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node, InventoryMapping.toNodeKey(nodeTable.node)) - .augmentation(FlowCapableNode).child(Table, new TableKey(nodeTable.ID as Short)).toInstance(); - - val it = this.startChange(); - - val table= it.readConfigurationData(tableRef) as Table; - - if(table != null){ - val tableStats = table.getAugmentation(FlowTableStatisticsData); - - if(tableStats != null){ - nodeStats = toNodeTableStatistics(tableStats.flowTableStatistics,table.id,nodeTable.node); - } - } - - //TODO: Refer TODO (main) - val input = new GetFlowTablesStatisticsInputBuilder(); - input.setNode(nodeTable.node.toNodeRef); - flowTableStatisticsService.getFlowTablesStatistics(input.build); - - return nodeStats; - } - - override onNodeConnectorRemoved(NodeConnectorRemoved update) { - // Never received - } - - override onNodeRemoved(NodeRemoved notification) { - val properties = Collections.emptySet(); - - removeNodeConnectors(notification.nodeRef.value); - - publishNodeUpdate(notification.nodeRef.toADNode, UpdateType.REMOVED, properties); - } - - override onNodeConnectorUpdated(NodeConnectorUpdated update) { - var updateType = UpdateType.CHANGED; - if(!isKnownNodeConnector(update.nodeConnectorRef.value)){ - updateType = UpdateType.ADDED; - recordNodeConnector(update.nodeConnectorRef.value); - } - - var nodeConnector = update.nodeConnectorRef.toADNodeConnector - - publishNodeConnectorUpdate(nodeConnector , updateType , update.toADNodeConnectorProperties); - } - - override onNodeUpdated(NodeUpdated notification) { - val InstanceIdentifier identifier = notification.nodeRef.value as InstanceIdentifier; - - var updateType = UpdateType.CHANGED; - if ( this._dataService.readOperationalData(identifier) == null ){ - updateType = UpdateType.ADDED; - } - publishNodeUpdate(notification.nodeRef.toADNode, updateType, notification.toADNodeProperties); - - //Notify the listeners of IPluginOutReadService - - for (statsPublisher : statisticsPublisher){ - val nodeRef = InstanceIdentifier.builder(Nodes).child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node,new NodeKey(notification.id)).toInstance; - val description = notification.nodeRef.toNodeDescription - if(description != null) { - statsPublisher.descriptionStatisticsUpdated(nodeRef.toADNode,description); - } - } - } - - override getNodeProps() { - val props = new ConcurrentHashMap>() - - val nodes = readAllMDNodes() - for (node : nodes.node ) { - val fcn = node.getAugmentation(FlowCapableNode) - if(fcn != null) { - val perNodeProps = fcn.toADNodeProperties(node.id) - val perNodePropMap = new ConcurrentHashMap - if(perNodeProps != null ) { - for(perNodeProp : perNodeProps) { - perNodePropMap.put(perNodeProp.name,perNodeProp) - } - } - props.put(new Node(MD_SAL_TYPE, node.id.toADNodeId),perNodePropMap) - } - } - return props; - } - - private def readAllMDNodes() { - val nodesRef = InstanceIdentifier.builder(Nodes) - .toInstance - val reader = TypeSafeDataReader.forReader(dataService) - return reader.readOperationalData(nodesRef) - } - - private def readAllMDNodeConnectors(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node node) { - val nodeRef = InstanceIdentifier.builder(Nodes) - .child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node,new NodeKey(node.id)) - .toInstance - val reader = TypeSafeDataReader.forReader(dataService) - return reader.readOperationalData(nodeRef).nodeConnector - } - - override getNodeConnectorProps(Boolean refresh) { - // Note, because the MD-SAL has a unified data store, we can ignore the Boolean refresh, as we have no secondary - // data store to refresh from - val props = new ConcurrentHashMap>() - val nodes = readAllMDNodes() - for (node : nodes.node) { - val ncs = node.readAllMDNodeConnectors - if(ncs != null) { - for( nc : ncs ) { - val fcnc = nc.getAugmentation(FlowCapableNodeConnector) - if(fcnc != null) { - val ncps = fcnc.toADNodeConnectorProperties - val ncpsm = new ConcurrentHashMap - if(ncps != null) { - for(p : ncps) { - ncpsm.put(p.name,p) - } - } - props.put(nc.id.toADNodeConnector(node.id),ncpsm) - } - } - } - } - return props - } - - private def FlowCapableNode readFlowCapableNode(NodeRef ref) { - val dataObject = dataService.readOperationalData(ref.value as InstanceIdentifier); - if(dataObject != null) { - val node = dataObject.checkInstanceOf( - org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node); - return node.getAugmentation(FlowCapableNode); - } - return null; - } - - private def FlowCapableNodeConnector readFlowCapableNodeConnector(NodeConnectorRef ref) { - val dataObject = dataService.readOperationalData(ref.value as InstanceIdentifier); - val node = dataObject.checkInstanceOf( - NodeConnector); - return node.getAugmentation(FlowCapableNodeConnector); - } - - private def toNodeConnectorStatistics( - org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.NodeConnectorStatistics nodeConnectorStatistics, NodeId nodeId, NodeConnectorId nodeConnectorId) { - - val it = new NodeConnectorStatistics(); - - receivePacketCount = nodeConnectorStatistics.packets.received.longValue; - transmitPacketCount = nodeConnectorStatistics.packets.transmitted.longValue; - - receiveByteCount = nodeConnectorStatistics.bytes.received.longValue; - transmitByteCount = nodeConnectorStatistics.bytes.transmitted.longValue; - - receiveDropCount = nodeConnectorStatistics.receiveDrops.longValue; - transmitDropCount = nodeConnectorStatistics.transmitDrops.longValue; - - receiveErrorCount = nodeConnectorStatistics.receiveErrors.longValue; - transmitErrorCount = nodeConnectorStatistics.transmitErrors.longValue; - - receiveFrameErrorCount = nodeConnectorStatistics.receiveFrameError.longValue; - receiveOverRunErrorCount = nodeConnectorStatistics.receiveOverRunError.longValue; - receiveCRCErrorCount = nodeConnectorStatistics.receiveCrcError.longValue; - collisionCount = nodeConnectorStatistics.collisionCount.longValue; - - val nodeConnectorRef = InstanceIdentifier.builder(Nodes) - .child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node,new NodeKey(nodeId)) - .child(NodeConnector,new NodeConnectorKey(nodeConnectorId)).toInstance; - - nodeConnector = NodeMapping.toADNodeConnector(new NodeConnectorRef(nodeConnectorRef)); - - return it; - } - - private def toNodeTableStatistics( - FlowTableStatistics tableStats, - Short tableId,Node node){ - var it = new NodeTableStatistics(); - - activeCount = tableStats.activeFlows.value.intValue; - lookupCount = tableStats.packetsLookedUp.value.intValue; - matchedCount = tableStats.packetsMatched.value.intValue; - name = tableId.toString; - nodeTable = new NodeTable(NodeMapping.MD_SAL_TYPE,tableId,node); - return it; - } - - private def toNodeDescription(NodeRef nodeRef){ - val capableNode = readFlowCapableNode(nodeRef); - if(capableNode !=null) { - val it = new NodeDescription() - manufacturer = capableNode.manufacturer - serialNumber = capableNode.serialNumber - software = capableNode.software - description = capableNode.description - - return it; - } - return null; - } - - - def Edge toADEdge(Link link) { - new Edge(link.source.toADNodeConnector,link.destination.toADNodeConnector) - } - - /* - * OpendaylightFlowStatisticsListener interface implementation - */ - override onAggregateFlowStatisticsUpdate(AggregateFlowStatisticsUpdate notification) { - //Ignoring this notification as there does not seem to be a way to bubble this up to AD-SAL - } - - override onFlowsStatisticsUpdate(FlowsStatisticsUpdate notification) { - - val adsalFlowsStatistics = new ArrayList(); - val nodeRef = InstanceIdentifier.builder(Nodes).child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node,new NodeKey(notification.id)).toInstance; - - for(flowStats : notification.flowAndStatisticsMapList){ - if(flowStats.tableId == 0) - adsalFlowsStatistics.add(toFlowOnNode(flowStats,nodeRef.toADNode)); - } - - for (statsPublisher : statisticsPublisher){ - statsPublisher.nodeFlowStatisticsUpdated(nodeRef.toADNode,adsalFlowsStatistics); - } - - } - /* - * OpendaylightFlowTableStatisticsListener interface implementation - */ - override onFlowTableStatisticsUpdate(FlowTableStatisticsUpdate notification) { - var adsalFlowTableStatistics = new ArrayList(); - - for(stats : notification.flowTableAndStatisticsMap){ - if (stats.tableId.value == 0){ - val it = new NodeTableStatistics(); - activeCount = stats.activeFlows.value.intValue; - lookupCount = stats.packetsLookedUp.value.longValue; - matchedCount = stats.packetsMatched.value.longValue; - - adsalFlowTableStatistics.add(it); - } - } - for (statsPublisher : statisticsPublisher){ - val nodeRef = InstanceIdentifier.builder(Nodes).child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node,new NodeKey(notification.id)).toInstance; - statsPublisher.nodeTableStatisticsUpdated(nodeRef.toADNode,adsalFlowTableStatistics); - } - } - - /* - * OpendaylightPortStatisticsUpdate interface implementation - */ - override onNodeConnectorStatisticsUpdate(NodeConnectorStatisticsUpdate notification) { - - val adsalPortStatistics = new ArrayList(); - - for(nodeConnectorStatistics : notification.nodeConnectorStatisticsAndPortNumberMap){ - adsalPortStatistics.add(toNodeConnectorStatistics(nodeConnectorStatistics,notification.id,nodeConnectorStatistics.nodeConnectorId)); - } - - for (statsPublisher : statisticsPublisher){ - val nodeRef = InstanceIdentifier.builder(Nodes).child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node,new NodeKey(notification.id)).toInstance; - statsPublisher.nodeConnectorStatisticsUpdated(nodeRef.toADNode,adsalPortStatistics); - } - - } - - private static def toFlowOnNode (FlowAndStatisticsMapList flowAndStatsMap,Node node){ - - val it = new FlowOnNode(ToSalConversionsUtils.toFlow(flowAndStatsMap,node)); - - byteCount = flowAndStatsMap.byteCount.value.longValue; - packetCount = flowAndStatsMap.packetCount.value.longValue; - durationSeconds = flowAndStatsMap.duration.second.value.intValue; - durationNanoseconds = flowAndStatsMap.duration.nanosecond.value.intValue; - - return it; - } - - override getConfiguredNotConnectedNodes() { - return Collections.emptySet(); - } - - - private def publishNodeUpdate(Node node, UpdateType updateType, Set properties){ - for( publisher : inventoryPublisher){ - publisher.updateNode(node, updateType, properties); - } - } - - private def publishNodeConnectorUpdate(org.opendaylight.controller.sal.core.NodeConnector nodeConnector, UpdateType updateType, Set properties){ - for( publisher : inventoryPublisher){ - publisher.updateNodeConnector(nodeConnector, updateType, properties); - } - } - - private def isKnownNodeConnector(InstanceIdentifier nodeConnectorIdentifier){ - if(nodeConnectorIdentifier.path.size() < 3) { - return false; - } - - val nodePath = nodeConnectorIdentifier.path.get(1); - val nodeConnectorPath = nodeConnectorIdentifier.getPath().get(2); - - val nodeConnectors = nodeToNodeConnectorsMap.get(nodePath); - - if(nodeConnectors == null){ - return false; - } - return nodeConnectors.contains(nodeConnectorPath); - } - - - private def recordNodeConnector(InstanceIdentifier nodeConnectorIdentifier){ - if(nodeConnectorIdentifier.path.size() < 3) { - return false; - } - - val nodePath = nodeConnectorIdentifier.path.get(1); - val nodeConnectorPath = nodeConnectorIdentifier.getPath().get(2); - - nodeToNodeConnectorsLock.lock(); - - try { - var nodeConnectors = nodeToNodeConnectorsMap.get(nodePath); - - if(nodeConnectors == null){ - nodeConnectors = new ArrayList(); - nodeToNodeConnectorsMap.put(nodePath, nodeConnectors); - } - - nodeConnectors.add(nodeConnectorPath); - } finally { - nodeToNodeConnectorsLock.unlock(); - } - } - - private def removeNodeConnectors(InstanceIdentifier nodeIdentifier){ - val nodePath = nodeIdentifier.path.get(1); - - nodeToNodeConnectorsMap.remove(nodePath); - } -} diff --git a/opendaylight/md-sal/compatibility/sal-compatibility/src/main/java/org/opendaylight/controller/sal/compatibility/NodeMapping.java b/opendaylight/md-sal/compatibility/sal-compatibility/src/main/java/org/opendaylight/controller/sal/compatibility/NodeMapping.java index 2c95252ac7..ba86ad99fb 100644 --- a/opendaylight/md-sal/compatibility/sal-compatibility/src/main/java/org/opendaylight/controller/sal/compatibility/NodeMapping.java +++ b/opendaylight/md-sal/compatibility/sal-compatibility/src/main/java/org/opendaylight/controller/sal/compatibility/NodeMapping.java @@ -61,314 +61,314 @@ import com.google.common.base.Objects; import com.google.common.base.Preconditions; public final class NodeMapping { - public final static String MD_SAL_TYPE = "MD_SAL"; - - private final static Class NODE_CLASS = Node.class; - - private final static Class NODECONNECTOR_CLASS = NodeConnector.class; - - private NodeMapping() { - throw new UnsupportedOperationException("Utility class. Instantiation is not allowed."); - } - - public static org.opendaylight.controller.sal.core.Node toADNode(final InstanceIdentifier node) throws ConstructionException { - NodeId nodeId = NodeMapping.toNodeId(node); - return NodeMapping.toADNode(nodeId); - } - - public static org.opendaylight.controller.sal.core.Node toADNode(final NodeId id) throws ConstructionException { - String aDNodeId = NodeMapping.toADNodeId(id); - return new org.opendaylight.controller.sal.core.Node(NodeMapping.MD_SAL_TYPE, aDNodeId); - } - - public static NodeId toNodeId(final InstanceIdentifier id) { - final NodeKey key = id.firstKeyOf(Node.class, NodeKey.class); - Preconditions.checkArgument(key != null, "No node identifier found in %s", id); - return key.getId(); - } - - public static String toADNodeId(final NodeId nodeId) { - return nodeId.getValue(); - } - - public static org.opendaylight.controller.sal.core.NodeConnector toADNodeConnector(final NodeConnectorRef source) throws ConstructionException { - final InstanceIdentifier id = Preconditions.checkNotNull(source.getValue()); - final NodeConnectorKey key = id.firstKeyOf(NodeConnector.class, NodeConnectorKey.class); - return NodeMapping.toADNodeConnector(key.getId(), NodeMapping.toNodeId(id)); - } - - public static org.opendaylight.controller.sal.core.NodeConnector toADNodeConnector(final NodeConnectorId ncid, final NodeId nid) throws ConstructionException { - String nodeConnectorType = NodeMapping.toNodeConnectorType(ncid, nid); - Object aDNodeConnectorId = NodeMapping.toADNodeConnectorId(ncid, nid); - org.opendaylight.controller.sal.core.Node aDNode = NodeMapping.toADNode(nid); - return new org.opendaylight.controller.sal.core.NodeConnector(nodeConnectorType, aDNodeConnectorId, aDNode); - } - - public static String toNodeConnectorType(final NodeConnectorId ncId, final NodeId nodeId) { - if (ncId.equals(toLocalNodeConnectorId(nodeId))) { - return NodeConnectorIDType.SWSTACK; - } else if (ncId.equals(toNormalNodeConnectorId(nodeId))) { - return NodeConnectorIDType.HWPATH; - } else if (ncId.equals(toControllerNodeConnectorId(nodeId))) { - return NodeConnectorIDType.CONTROLLER; - } - return MD_SAL_TYPE; - } - - public static Object toADNodeConnectorId(final NodeConnectorId nodeConnectorId, final NodeId nodeId) { - if (nodeConnectorId.equals(toLocalNodeConnectorId(nodeId)) || - nodeConnectorId.equals(toNormalNodeConnectorId(nodeId)) || - nodeConnectorId.equals(toControllerNodeConnectorId(nodeId))) { - return org.opendaylight.controller.sal.core.NodeConnector.SPECIALNODECONNECTORID; - } - return nodeConnectorId.getValue(); - } - - public static NodeConnectorId toControllerNodeConnectorId(final NodeId node) { - return new NodeConnectorId(node.getValue() + ":" + 4294967293L); - } - - public static NodeConnectorId toLocalNodeConnectorId(final NodeId node) { - return new NodeConnectorId(node.getValue() + ":" + 4294967294L); - } - - public static NodeConnectorId toNormalNodeConnectorId(final NodeId node) { - return new NodeConnectorId(node.getValue() + ":" + 4294967290L); - } - - public static NodeRef toNodeRef(final org.opendaylight.controller.sal.core.Node node) { - Preconditions.checkArgument(MD_SAL_TYPE.equals(node.getType())); - final String nodeId = Arguments.checkInstanceOf(node.getID(), String.class); - final NodeKey nodeKey = new NodeKey(new NodeId(nodeId)); - final InstanceIdentifier nodePath = InstanceIdentifier.builder(Nodes.class).child(NODE_CLASS, nodeKey).toInstance(); - return new NodeRef(nodePath); - } - - public static NodeConnectorRef toNodeConnectorRef(final org.opendaylight.controller.sal.core.NodeConnector nodeConnector) { - - final NodeRef node = NodeMapping.toNodeRef(nodeConnector.getNode()); - @SuppressWarnings("unchecked") - final InstanceIdentifier nodePath = ((InstanceIdentifier) node.getValue()); - NodeConnectorId nodeConnectorId = null; - - if (nodeConnector.getID().equals(org.opendaylight.controller.sal.core.NodeConnector.SPECIALNODECONNECTORID)) { - final NodeId nodeId = toNodeId(nodePath); - final String nodeConnectorType = nodeConnector.getType(); - if (nodeConnectorType.equals(NodeConnectorIDType.SWSTACK)) { - nodeConnectorId = toLocalNodeConnectorId(nodeId); - } else if (nodeConnectorType.equals(NodeConnectorIDType.HWPATH)) { - nodeConnectorId = toNormalNodeConnectorId(nodeId); - } else if (nodeConnectorType.equals(NodeConnectorIDType.CONTROLLER)) { - nodeConnectorId = toControllerNodeConnectorId(nodeId); + public final static String MD_SAL_TYPE = "MD_SAL"; + + private final static Class NODE_CLASS = Node.class; + + private final static Class NODECONNECTOR_CLASS = NodeConnector.class; + + private NodeMapping() { + throw new UnsupportedOperationException("Utility class. Instantiation is not allowed."); + } + + public static org.opendaylight.controller.sal.core.Node toADNode(final InstanceIdentifier node) throws ConstructionException { + NodeId nodeId = NodeMapping.toNodeId(node); + return NodeMapping.toADNode(nodeId); + } + + public static org.opendaylight.controller.sal.core.Node toADNode(final NodeId id) throws ConstructionException { + String aDNodeId = NodeMapping.toADNodeId(id); + return new org.opendaylight.controller.sal.core.Node(NodeMapping.MD_SAL_TYPE, aDNodeId); + } + + public static NodeId toNodeId(final InstanceIdentifier id) { + final NodeKey key = id.firstKeyOf(Node.class, NodeKey.class); + Preconditions.checkArgument(key != null, "No node identifier found in %s", id); + return key.getId(); + } + + public static String toADNodeId(final NodeId nodeId) { + return nodeId.getValue(); + } + + public static org.opendaylight.controller.sal.core.NodeConnector toADNodeConnector(final NodeConnectorRef source) throws ConstructionException { + final InstanceIdentifier id = Preconditions.checkNotNull(source.getValue()); + final NodeConnectorKey key = id.firstKeyOf(NodeConnector.class, NodeConnectorKey.class); + return NodeMapping.toADNodeConnector(key.getId(), NodeMapping.toNodeId(id)); + } + + public static org.opendaylight.controller.sal.core.NodeConnector toADNodeConnector(final NodeConnectorId ncid, final NodeId nid) throws ConstructionException { + String nodeConnectorType = NodeMapping.toNodeConnectorType(ncid, nid); + Object aDNodeConnectorId = NodeMapping.toADNodeConnectorId(ncid, nid); + org.opendaylight.controller.sal.core.Node aDNode = NodeMapping.toADNode(nid); + return new org.opendaylight.controller.sal.core.NodeConnector(nodeConnectorType, aDNodeConnectorId, aDNode); + } + + public static String toNodeConnectorType(final NodeConnectorId ncId, final NodeId nodeId) { + if (ncId.equals(toLocalNodeConnectorId(nodeId))) { + return NodeConnectorIDType.SWSTACK; + } else if (ncId.equals(toNormalNodeConnectorId(nodeId))) { + return NodeConnectorIDType.HWPATH; + } else if (ncId.equals(toControllerNodeConnectorId(nodeId))) { + return NodeConnectorIDType.CONTROLLER; } - } else { - nodeConnectorId = new NodeConnectorId(Arguments.checkInstanceOf(nodeConnector.getID(), String.class)); + return MD_SAL_TYPE; } - final NodeConnectorKey connectorKey = new NodeConnectorKey(nodeConnectorId); - final InstanceIdentifier path = nodePath.child(NODECONNECTOR_CLASS, connectorKey); - return new NodeConnectorRef(path); - } - public static org.opendaylight.controller.sal.core.Node toADNode(final NodeRef node) throws ConstructionException { - return NodeMapping.toADNode(node.getValue()); - } + public static Object toADNodeConnectorId(final NodeConnectorId nodeConnectorId, final NodeId nodeId) { + if (nodeConnectorId.equals(toLocalNodeConnectorId(nodeId)) || + nodeConnectorId.equals(toNormalNodeConnectorId(nodeId)) || + nodeConnectorId.equals(toControllerNodeConnectorId(nodeId))) { + return org.opendaylight.controller.sal.core.NodeConnector.SPECIALNODECONNECTORID; + } + return nodeConnectorId.getValue(); + } - public static HashSet toADNodeConnectorProperties(final NodeConnectorUpdated nc) { - final FlowCapableNodeConnectorUpdated fcncu = nc.getAugmentation(FlowCapableNodeConnectorUpdated.class); - if (!Objects.equal(fcncu, null)) { - return NodeMapping.toADNodeConnectorProperties(fcncu); + public static NodeConnectorId toControllerNodeConnectorId(final NodeId node) { + return new NodeConnectorId(node.getValue() + ":" + 4294967293L); } - return new HashSet(); - } - public static HashSet toADNodeConnectorProperties(final NodeConnector nc) { - final FlowCapableNodeConnector fcnc = nc.getAugmentation(FlowCapableNodeConnector.class); - if (!Objects.equal(fcnc, null)) { - return NodeMapping.toADNodeConnectorProperties(fcnc); + public static NodeConnectorId toLocalNodeConnectorId(final NodeId node) { + return new NodeConnectorId(node.getValue() + ":" + 4294967294L); } - return new HashSet(); - } - public static HashSet toADNodeConnectorProperties(final FlowNodeConnector fcncu) { + public static NodeConnectorId toNormalNodeConnectorId(final NodeId node) { + return new NodeConnectorId(node.getValue() + ":" + 4294967290L); + } + + public static NodeRef toNodeRef(final org.opendaylight.controller.sal.core.Node node) { + Preconditions.checkArgument(MD_SAL_TYPE.equals(node.getType())); + final String nodeId = Arguments.checkInstanceOf(node.getID(), String.class); + final NodeKey nodeKey = new NodeKey(new NodeId(nodeId)); + final InstanceIdentifier nodePath = InstanceIdentifier.builder(Nodes.class).child(NODE_CLASS, nodeKey).toInstance(); + return new NodeRef(nodePath); + } - final HashSet props = new HashSet<>(); - if (fcncu != null) { - if (fcncu.getCurrentFeature() != null && toAdBandwidth(fcncu.getCurrentFeature()) != null) { - props.add(toAdBandwidth(fcncu.getCurrentFeature())); + public static NodeConnectorRef toNodeConnectorRef(final org.opendaylight.controller.sal.core.NodeConnector nodeConnector) { + + final NodeRef node = NodeMapping.toNodeRef(nodeConnector.getNode()); + @SuppressWarnings("unchecked") + final InstanceIdentifier nodePath = ((InstanceIdentifier) node.getValue()); + NodeConnectorId nodeConnectorId = null; + + if (nodeConnector.getID().equals(org.opendaylight.controller.sal.core.NodeConnector.SPECIALNODECONNECTORID)) { + final NodeId nodeId = toNodeId(nodePath); + final String nodeConnectorType = nodeConnector.getType(); + if (nodeConnectorType.equals(NodeConnectorIDType.SWSTACK)) { + nodeConnectorId = toLocalNodeConnectorId(nodeId); + } else if (nodeConnectorType.equals(NodeConnectorIDType.HWPATH)) { + nodeConnectorId = toNormalNodeConnectorId(nodeId); + } else if (nodeConnectorType.equals(NodeConnectorIDType.CONTROLLER)) { + nodeConnectorId = toControllerNodeConnectorId(nodeId); + } + } else { + nodeConnectorId = new NodeConnectorId(Arguments.checkInstanceOf(nodeConnector.getID(), String.class)); } - if (fcncu.getAdvertisedFeatures() != null && toAdAdvertizedBandwidth(fcncu.getAdvertisedFeatures()) != null) { - props.add(toAdAdvertizedBandwidth(fcncu.getAdvertisedFeatures())); + final NodeConnectorKey connectorKey = new NodeConnectorKey(nodeConnectorId); + final InstanceIdentifier path = nodePath.child(NODECONNECTOR_CLASS, connectorKey); + return new NodeConnectorRef(path); + } + + public static org.opendaylight.controller.sal.core.Node toADNode(final NodeRef node) throws ConstructionException { + return NodeMapping.toADNode(node.getValue()); + } + + public static HashSet toADNodeConnectorProperties(final NodeConnectorUpdated nc) { + final FlowCapableNodeConnectorUpdated fcncu = nc.getAugmentation(FlowCapableNodeConnectorUpdated.class); + if (!Objects.equal(fcncu, null)) { + return NodeMapping.toADNodeConnectorProperties(fcncu); } - if (fcncu.getSupported() != null && toAdSupportedBandwidth(fcncu.getSupported()) != null) { - props.add(toAdSupportedBandwidth(fcncu.getSupported())); + return new HashSet(); + } + + public static HashSet toADNodeConnectorProperties(final NodeConnector nc) { + final FlowCapableNodeConnector fcnc = nc.getAugmentation(FlowCapableNodeConnector.class); + if (!Objects.equal(fcnc, null)) { + return NodeMapping.toADNodeConnectorProperties(fcnc); } - if (fcncu.getPeerFeatures() != null && toAdPeerBandwidth(fcncu.getPeerFeatures()) != null) { - props.add(toAdPeerBandwidth(fcncu.getPeerFeatures())); + return new HashSet(); + } + + public static HashSet toADNodeConnectorProperties(final FlowNodeConnector fcncu) { + + final HashSet props = new HashSet<>(); + if (fcncu != null) { + if (fcncu.getCurrentFeature() != null && toAdBandwidth(fcncu.getCurrentFeature()) != null) { + props.add(toAdBandwidth(fcncu.getCurrentFeature())); + } + if (fcncu.getAdvertisedFeatures() != null && toAdAdvertizedBandwidth(fcncu.getAdvertisedFeatures()) != null) { + props.add(toAdAdvertizedBandwidth(fcncu.getAdvertisedFeatures())); + } + if (fcncu.getSupported() != null && toAdSupportedBandwidth(fcncu.getSupported()) != null) { + props.add(toAdSupportedBandwidth(fcncu.getSupported())); + } + if (fcncu.getPeerFeatures() != null && toAdPeerBandwidth(fcncu.getPeerFeatures()) != null) { + props.add(toAdPeerBandwidth(fcncu.getPeerFeatures())); + } + if (fcncu.getName() != null && toAdName(fcncu.getName()) != null) { + props.add(toAdName(fcncu.getName())); + } + if (fcncu.getConfiguration() != null && toAdConfig(fcncu.getConfiguration()) != null) { + props.add(toAdConfig(fcncu.getConfiguration())); + } + if (fcncu.getState() != null && toAdState(fcncu.getState()) != null) { + props.add(toAdState(fcncu.getState())); + } } - if (fcncu.getName() != null && toAdName(fcncu.getName()) != null) { - props.add(toAdName(fcncu.getName())); + return props; + } + + public static Name toAdName(final String name) { + return new Name(name); + } + + public static Config toAdConfig(final PortConfig pc) { + Config config = null; + if (pc.isPORTDOWN()) { + config = new Config(Config.ADMIN_DOWN); + } else { + config = new Config(Config.ADMIN_UP); + } + return config; + } + + public static org.opendaylight.controller.sal.core.State toAdState(final State s) { + + org.opendaylight.controller.sal.core.State state = null; + if (s.isLinkDown()) { + state = new org.opendaylight.controller.sal.core.State(org.opendaylight.controller.sal.core.State.EDGE_DOWN); + } else { + state = new org.opendaylight.controller.sal.core.State(org.opendaylight.controller.sal.core.State.EDGE_UP); } - if (fcncu.getConfiguration() != null && toAdConfig(fcncu.getConfiguration()) != null) { - props.add(toAdConfig(fcncu.getConfiguration())); + return state; + } + + public static Bandwidth toAdBandwidth(final PortFeatures pf) { + Bandwidth bw = null; + if (pf.isTenMbHd() || pf.isTenMbFd()) { + bw = new Bandwidth(Bandwidth.BW10Mbps); + } else if (pf.isHundredMbHd() || pf.isHundredMbFd()) { + bw = new Bandwidth(Bandwidth.BW100Mbps); + } else if (pf.isOneGbHd() || pf.isOneGbFd()) { + bw = new Bandwidth(Bandwidth.BW1Gbps); + } else if (pf.isOneGbFd()) { + bw = new Bandwidth(Bandwidth.BW10Gbps); + } else if (pf.isTenGbFd()) { + bw = new Bandwidth(Bandwidth.BW10Gbps); + } else if (pf.isFortyGbFd()) { + bw = new Bandwidth(Bandwidth.BW40Gbps); + } else if (pf.isHundredGbFd()) { + bw = new Bandwidth(Bandwidth.BW100Gbps); + } else if (pf.isOneTbFd()) { + bw = new Bandwidth(Bandwidth.BW1Tbps); } - if (fcncu.getState() != null && toAdState(fcncu.getState()) != null) { - props.add(toAdState(fcncu.getState())); + return bw; + } + + public static AdvertisedBandwidth toAdAdvertizedBandwidth(final PortFeatures pf) { + AdvertisedBandwidth abw = null; + final Bandwidth bw = toAdBandwidth(pf); + if (bw != null) { + abw = new AdvertisedBandwidth(bw.getValue()); } + return abw; } - return props; - } - - public static Name toAdName(final String name) { - return new Name(name); - } - - public static Config toAdConfig(final PortConfig pc) { - Config config = null; - if (pc.isPORTDOWN()) { - config = new Config(Config.ADMIN_DOWN); - } else { - config = new Config(Config.ADMIN_UP); - } - return config; - } - - public static org.opendaylight.controller.sal.core.State toAdState(final State s) { - - org.opendaylight.controller.sal.core.State state = null; - if (s.isLinkDown()) { - state = new org.opendaylight.controller.sal.core.State(org.opendaylight.controller.sal.core.State.EDGE_DOWN); - } else { - state = new org.opendaylight.controller.sal.core.State(org.opendaylight.controller.sal.core.State.EDGE_UP); - } - return state; - } - - public static Bandwidth toAdBandwidth(final PortFeatures pf) { - Bandwidth bw = null; - if (pf.isTenMbHd() || pf.isTenMbFd()) { - bw = new Bandwidth(Bandwidth.BW10Mbps); - } else if (pf.isHundredMbHd() || pf.isHundredMbFd()) { - bw = new Bandwidth(Bandwidth.BW100Mbps); - } else if (pf.isOneGbHd() || pf.isOneGbFd()) { - bw = new Bandwidth(Bandwidth.BW1Gbps); - } else if (pf.isOneGbFd()) { - bw = new Bandwidth(Bandwidth.BW10Gbps); - } else if (pf.isTenGbFd()) { - bw = new Bandwidth(Bandwidth.BW10Gbps); - } else if (pf.isFortyGbFd()) { - bw = new Bandwidth(Bandwidth.BW40Gbps); - } else if (pf.isHundredGbFd()) { - bw = new Bandwidth(Bandwidth.BW100Gbps); - } else if (pf.isOneTbFd()) { - bw = new Bandwidth(Bandwidth.BW1Tbps); - } - return bw; - } - - public static AdvertisedBandwidth toAdAdvertizedBandwidth(final PortFeatures pf) { - AdvertisedBandwidth abw = null; - final Bandwidth bw = toAdBandwidth(pf); - if (bw != null) { - abw = new AdvertisedBandwidth(bw.getValue()); - } - return abw; - } - - public static SupportedBandwidth toAdSupportedBandwidth(final PortFeatures pf) { - SupportedBandwidth sbw = null; - final Bandwidth bw = toAdBandwidth(pf); - if (bw != null) { - sbw = new SupportedBandwidth(bw.getValue()); - } - return sbw; - } - - public static PeerBandwidth toAdPeerBandwidth(final PortFeatures pf) { - PeerBandwidth pbw = null; - final Bandwidth bw = toAdBandwidth(pf); - if (bw != null) { - pbw = new PeerBandwidth(bw.getValue()); - } - return pbw; - } - - public static HashSet toADNodeProperties(final NodeUpdated nu) { - final FlowCapableNodeUpdated fcnu = nu.getAugmentation(FlowCapableNodeUpdated.class); - if (fcnu != null) { - return toADNodeProperties(fcnu, nu.getId()); - } - return new HashSet(); - } - - public static HashSet toADNodeProperties(final FlowNode fcnu, final NodeId id) { - - final HashSet props = new HashSet<>(); - - if (fcnu != null) { - props.add(toADTimestamp()); - - // props.add(fcnu.supportedActions.toADActions) - TODO - if (id != null) { - props.add(toADMacAddress(id)); + + public static SupportedBandwidth toAdSupportedBandwidth(final PortFeatures pf) { + SupportedBandwidth sbw = null; + final Bandwidth bw = toAdBandwidth(pf); + if (bw != null) { + sbw = new SupportedBandwidth(bw.getValue()); } - SwitchFeatures switchFeatures = fcnu.getSwitchFeatures(); - if (switchFeatures != null) { - if (switchFeatures.getMaxTables() != null) { - props.add(toADTables(switchFeatures.getMaxTables())); - } - if (switchFeatures.getCapabilities() != null) { - props.add(toADCapabiliities(switchFeatures.getCapabilities())); + return sbw; + } + + public static PeerBandwidth toAdPeerBandwidth(final PortFeatures pf) { + PeerBandwidth pbw = null; + final Bandwidth bw = toAdBandwidth(pf); + if (bw != null) { + pbw = new PeerBandwidth(bw.getValue()); + } + return pbw; + } + + public static HashSet toADNodeProperties(final NodeUpdated nu) { + final FlowCapableNodeUpdated fcnu = nu.getAugmentation(FlowCapableNodeUpdated.class); + if (fcnu != null) { + return toADNodeProperties(fcnu, nu.getId()); + } + return new HashSet(); + } + + public static HashSet toADNodeProperties(final FlowNode fcnu, final NodeId id) { + + final HashSet props = new HashSet<>(); + + if (fcnu != null) { + props.add(toADTimestamp()); + + // props.add(fcnu.supportedActions.toADActions) - TODO + if (id != null) { + props.add(toADMacAddress(id)); } - if (switchFeatures.getMaxBuffers() != null) { - props.add(toADBuffers(switchFeatures.getMaxBuffers())); + SwitchFeatures switchFeatures = fcnu.getSwitchFeatures(); + if (switchFeatures != null) { + if (switchFeatures.getMaxTables() != null) { + props.add(toADTables(switchFeatures.getMaxTables())); + } + if (switchFeatures.getCapabilities() != null) { + props.add(toADCapabiliities(switchFeatures.getCapabilities())); + } + if (switchFeatures.getMaxBuffers() != null) { + props.add(toADBuffers(switchFeatures.getMaxBuffers())); + } } } + return props; } - return props; - } - - public static TimeStamp toADTimestamp() { - final Date date = new Date(); - final TimeStamp timestamp = new TimeStamp(date.getTime(), "connectedSince"); - return timestamp; - } - - public static MacAddress toADMacAddress(final NodeId id) { - final String nodeId = id.getValue().replaceAll("openflow:", ""); - BigInteger nodeIdRaw = new BigInteger(nodeId); - long lNodeId = nodeIdRaw.longValue(); - byte[] bytesFromDpid = ToSalConversionsUtils.bytesFromDpid(lNodeId); - return new MacAddress(bytesFromDpid); - } - - public static Tables toADTables(final Short tables) { - return new Tables(tables.byteValue()); - } - - public static Capabilities toADCapabiliities(final List> capabilities) { - - int b = 0; - for (Class capability : capabilities) { - if (capability.equals(FlowFeatureCapabilityFlowStats.class)) { - b = Capabilities.CapabilitiesType.FLOW_STATS_CAPABILITY.getValue() | b; - } else if (capability.equals(FlowFeatureCapabilityTableStats.class)) { - b = Capabilities.CapabilitiesType.TABLE_STATS_CAPABILITY.getValue() | b; - } else if (capability.equals(FlowFeatureCapabilityPortStats.class)) { - b = Capabilities.CapabilitiesType.PORT_STATS_CAPABILITY.getValue() | b; - } else if (capability.equals(FlowFeatureCapabilityStp.class)) { - b = Capabilities.CapabilitiesType.STP_CAPABILITY.getValue() | b; - } else if (capability.equals(FlowFeatureCapabilityIpReasm.class)) { - b = Capabilities.CapabilitiesType.IP_REASSEM_CAPABILITY.getValue() | b; - } else if (capability.equals(FlowFeatureCapabilityQueueStats.class)) { - b = Capabilities.CapabilitiesType.QUEUE_STATS_CAPABILITY.getValue() | b; - } else if (capability.equals(FlowFeatureCapabilityArpMatchIp.class)) { - b = Capabilities.CapabilitiesType.ARP_MATCH_IP_CAPABILITY.getValue() | b; + + public static TimeStamp toADTimestamp() { + final Date date = new Date(); + final TimeStamp timestamp = new TimeStamp(date.getTime(), "connectedSince"); + return timestamp; + } + + public static MacAddress toADMacAddress(final NodeId id) { + final String nodeId = id.getValue().replaceAll("openflow:", ""); + BigInteger nodeIdRaw = new BigInteger(nodeId); + long lNodeId = nodeIdRaw.longValue(); + byte[] bytesFromDpid = ToSalConversionsUtils.bytesFromDpid(lNodeId); + return new MacAddress(bytesFromDpid); + } + + public static Tables toADTables(final Short tables) { + return new Tables(tables.byteValue()); + } + + public static Capabilities toADCapabiliities(final List> capabilities) { + + int b = 0; + for (Class capability : capabilities) { + if (capability.equals(FlowFeatureCapabilityFlowStats.class)) { + b = Capabilities.CapabilitiesType.FLOW_STATS_CAPABILITY.getValue() | b; + } else if (capability.equals(FlowFeatureCapabilityTableStats.class)) { + b = Capabilities.CapabilitiesType.TABLE_STATS_CAPABILITY.getValue() | b; + } else if (capability.equals(FlowFeatureCapabilityPortStats.class)) { + b = Capabilities.CapabilitiesType.PORT_STATS_CAPABILITY.getValue() | b; + } else if (capability.equals(FlowFeatureCapabilityStp.class)) { + b = Capabilities.CapabilitiesType.STP_CAPABILITY.getValue() | b; + } else if (capability.equals(FlowFeatureCapabilityIpReasm.class)) { + b = Capabilities.CapabilitiesType.IP_REASSEM_CAPABILITY.getValue() | b; + } else if (capability.equals(FlowFeatureCapabilityQueueStats.class)) { + b = Capabilities.CapabilitiesType.QUEUE_STATS_CAPABILITY.getValue() | b; + } else if (capability.equals(FlowFeatureCapabilityArpMatchIp.class)) { + b = Capabilities.CapabilitiesType.ARP_MATCH_IP_CAPABILITY.getValue() | b; + } } + return new Capabilities(b); } - return new Capabilities(b); - } - public static Buffers toADBuffers(final Long buffers) { - return new Buffers(buffers.intValue()); - } + public static Buffers toADBuffers(final Long buffers) { + return new Buffers(buffers.intValue()); + } }