X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=openflowplugin-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fopenflowplugin%2Fimpl%2Futil%2FDeviceInitializationUtils.java;h=0bdbe08a9c82e01be756b0cabec21d027f6a2863;hb=9f8a5a168696e279ae49ed79efe4590abc49b9ba;hp=bd40f4c9f6e9a436549be66c89cbec0be738f3a4;hpb=fe05dabd56d7bf254b034a0804ba666eeb4fd6d6;p=openflowplugin.git diff --git a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/util/DeviceInitializationUtils.java b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/util/DeviceInitializationUtils.java index bd40f4c9f6..0bdbe08a9c 100644 --- a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/util/DeviceInitializationUtils.java +++ b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/util/DeviceInitializationUtils.java @@ -19,8 +19,8 @@ import java.net.InetSocketAddress; import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import java.util.Iterator; import java.util.List; +import java.util.Objects; import java.util.concurrent.ExecutionException; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue; @@ -38,9 +38,10 @@ import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey; import org.opendaylight.openflowplugin.impl.common.MultipartRequestInputFactory; import org.opendaylight.openflowplugin.impl.common.NodeStaticReplyTranslatorUtil; import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext; +import org.opendaylight.openflowplugin.openflow.md.core.sal.SwitchFeaturesUtil; +import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress; -import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector; @@ -59,6 +60,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111. import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Capabilities; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.CapabilitiesV10; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping; @@ -89,6 +91,10 @@ public class DeviceInitializationUtils { private static final Logger LOG = LoggerFactory.getLogger(DeviceInitializationUtils.class); + private DeviceInitializationUtils() { + // Hiding implicit constructor + } + /** * InitializationNodeInformation is good to call only for MASTER otherwise we will have not empty transaction * for every Cluster Node (SLAVE too) and we will get race-condition by closing Connection. @@ -104,7 +110,7 @@ public class DeviceInitializationUtils { final ConnectionContext connectionContext = Preconditions.checkNotNull(deviceContext.getPrimaryConnectionContext()); final short version = deviceInfo.getVersion(); LOG.trace("initalizeNodeInformation for node {}", deviceInfo.getNodeId()); - final SettableFuture returnFuture = SettableFuture.create(); + final SettableFuture returnFuture = SettableFuture.create(); addNodeToOperDS(deviceContext, returnFuture); final ListenableFuture>>> deviceFeaturesFuture; if (OFConstants.OFP_VERSION_1_0 == version) { @@ -156,7 +162,7 @@ public class DeviceInitializationUtils { private static void addNodeToOperDS(final DeviceContext deviceContext, final SettableFuture future) { Preconditions.checkArgument(deviceContext != null); final NodeBuilder nodeBuilder = new NodeBuilder().setId(deviceContext.getDeviceInfo().getNodeId()).setNodeConnector( - Collections.emptyList()); + Collections.emptyList()); try { deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, deviceContext.getDeviceInfo().getNodeInstanceIdentifier(), nodeBuilder.build()); @@ -203,9 +209,15 @@ public class DeviceInitializationUtils { createSuccessProcessingCallback(MultipartType.OFPMPGROUPFEATURES, deviceContext, deviceContext.getDeviceInfo().getNodeInstanceIdentifier(), replyGroupFeatures, convertorExecutor); - final ListenableFuture>> replyTableFeatures = getNodeStaticInfo( - MultipartType.OFPMPTABLEFEATURES, deviceContext, - deviceContext.getDeviceInfo().getNodeInstanceIdentifier(), deviceContext.getDeviceInfo().getVersion()); + final ListenableFuture>> replyTableFeatures; + + if (deviceContext.isSkipTableFeatures()) { + replyTableFeatures = RpcResultBuilder.>success().buildFuture(); + } else { + replyTableFeatures = getNodeStaticInfo( + MultipartType.OFPMPTABLEFEATURES, deviceContext, + deviceContext.getDeviceInfo().getNodeInstanceIdentifier(), deviceContext.getDeviceInfo().getVersion()); + } createSuccessProcessingCallback(MultipartType.OFPMPTABLEFEATURES, deviceContext, deviceContext.getDeviceInfo().getNodeInstanceIdentifier(), replyTableFeatures, convertorExecutor); @@ -229,105 +241,163 @@ public class DeviceInitializationUtils { static void translateAndWriteReply(final MultipartType type, final DeviceContext dContext, final InstanceIdentifier nodeII, final Collection result, final ConvertorExecutor convertorExecutor) { - try { - for (final MultipartReply reply : result) { - final MultipartReplyBody body = reply.getMultipartReplyBody(); - switch (type) { - case OFPMPDESC: - Preconditions.checkArgument(body instanceof MultipartReplyDescCase); - final MultipartReplyDesc replyDesc = ((MultipartReplyDescCase) body).getMultipartReplyDesc(); - final FlowCapableNode fcNode = NodeStaticReplyTranslatorUtil.nodeDescTranslator(replyDesc, - getIpAddressOf(dContext)); - final InstanceIdentifier fNodeII = nodeII.augmentation(FlowCapableNode.class); - dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, fNodeII, fcNode); - break; - - case OFPMPTABLEFEATURES: - Preconditions.checkArgument(body instanceof MultipartReplyTableFeaturesCase); - final MultipartReplyTableFeatures tableFeaturesMP = ((MultipartReplyTableFeaturesCase) body) - .getMultipartReplyTableFeatures(); - final List tableFeatures = NodeStaticReplyTranslatorUtil - .nodeTableFeatureTranslator(tableFeaturesMP, dContext.getDeviceInfo().getVersion(), convertorExecutor); - for (final TableFeatures tableFeature : tableFeatures) { - final Short tableId = tableFeature.getTableId(); - final KeyedInstanceIdentifier tableFeaturesII = - nodeII.augmentation(FlowCapableNode.class) - .child(TableFeatures.class, new TableFeaturesKey(tableId)); - dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tableFeaturesII, tableFeature); - - // write parent for table statistics - final KeyedInstanceIdentifier tableII = - nodeII.augmentation(FlowCapableNode.class) - .child(Table.class, new TableKey(tableId)); - final TableBuilder tableBld = new TableBuilder().setId(tableId) - .addAugmentation(FlowTableStatisticsData.class, - new FlowTableStatisticsDataBuilder().build()); - - dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tableII, tableBld.build()); - } - break; - - case OFPMPMETERFEATURES: - Preconditions.checkArgument(body instanceof MultipartReplyMeterFeaturesCase); - final MultipartReplyMeterFeatures meterFeatures = ((MultipartReplyMeterFeaturesCase) body) - .getMultipartReplyMeterFeatures(); - final NodeMeterFeatures mFeature = NodeStaticReplyTranslatorUtil - .nodeMeterFeatureTranslator(meterFeatures); - final InstanceIdentifier mFeatureII = nodeII - .augmentation(NodeMeterFeatures.class); - dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, mFeatureII, mFeature); - if (0L < mFeature.getMeterFeatures().getMaxMeter().getValue()) { - dContext.getDeviceState().setMeterAvailable(true); - } - break; - - case OFPMPGROUPFEATURES: - Preconditions.checkArgument(body instanceof MultipartReplyGroupFeaturesCase); - final MultipartReplyGroupFeatures groupFeatures = ((MultipartReplyGroupFeaturesCase) body) - .getMultipartReplyGroupFeatures(); - final NodeGroupFeatures gFeature = NodeStaticReplyTranslatorUtil - .nodeGroupFeatureTranslator(groupFeatures); - final InstanceIdentifier gFeatureII = nodeII - .augmentation(NodeGroupFeatures.class); - dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, gFeatureII, gFeature); - break; - - case OFPMPPORTDESC: - Preconditions.checkArgument(body instanceof MultipartReplyPortDescCase); - final MultipartReplyPortDesc portDesc = ((MultipartReplyPortDescCase) body) - .getMultipartReplyPortDesc(); - for (final PortGrouping port : portDesc.getPorts()) { - final short ofVersion = dContext.getDeviceInfo().getVersion(); - final TranslatorKey translatorKey = new TranslatorKey(ofVersion, PortGrouping.class.getName()); - final MessageTranslator translator = dContext.oook() - .lookupTranslator(translatorKey); - final FlowCapableNodeConnector fcNodeConnector = translator.translate(port, dContext.getDeviceInfo(), null); - - final BigInteger dataPathId = dContext.getPrimaryConnectionContext().getFeatures() - .getDatapathId(); - final NodeConnectorId nodeConnectorId = NodeStaticReplyTranslatorUtil.nodeConnectorId( - dataPathId.toString(), port.getPortNo(), ofVersion); - final NodeConnectorBuilder ncBuilder = new NodeConnectorBuilder().setId(nodeConnectorId); - ncBuilder.addAugmentation(FlowCapableNodeConnector.class, fcNodeConnector); - - ncBuilder.addAugmentation(FlowCapableNodeConnectorStatisticsData.class, - new FlowCapableNodeConnectorStatisticsDataBuilder().build()); - final NodeConnector connector = ncBuilder.build(); - - final InstanceIdentifier connectorII = nodeII.child(NodeConnector.class, - connector.getKey()); - dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, connectorII, connector); - } + if (Objects.nonNull(result)) { + try { + result.stream() + .map(MultipartReply::getMultipartReplyBody) + .forEach(multipartReplyBody -> { + if (!(writeDesc(type, multipartReplyBody, dContext, nodeII) + || writeTableFeatures(type, multipartReplyBody, dContext, nodeII, convertorExecutor) + || writeMeterFeatures(type, multipartReplyBody, dContext, nodeII) + || writeGroupFeatures(type, multipartReplyBody, dContext, nodeII) + || writePortDesc(type, multipartReplyBody, dContext, nodeII))) { + throw new IllegalArgumentException("Unexpected MultipartType " + type); + } + }); + } catch (final Exception e) { + LOG.debug("translateAndWriteReply: Failed to write node {} to DS ", dContext.getDeviceInfo().getNodeId().toString(), e); + } + } else { + LOG.debug("translateAndWriteReply: Failed to write node {} to DS because we failed to gather device" + + "info.", + dContext.getDeviceInfo().getNodeId().toString()); + } + } - break; + private static boolean writeDesc(final MultipartType type, + final MultipartReplyBody body, + final DeviceContext dContext, + final InstanceIdentifier nodeII) { + if (!MultipartType.OFPMPDESC.equals(type)) { + return false; + } - default: - throw new IllegalArgumentException("Unnexpected MultipartType " + type); - } - } - } catch (final Exception e) { - LOG.debug("translateAndWriteReply: Failed to write node {} to DS ", dContext.getDeviceInfo().getNodeId().toString(), e); + Preconditions.checkArgument(body instanceof MultipartReplyDescCase); + final MultipartReplyDesc replyDesc = ((MultipartReplyDescCase) body).getMultipartReplyDesc(); + final FlowCapableNode fcNode = NodeStaticReplyTranslatorUtil + .nodeDescTranslator(replyDesc, getIpAddressOf(dContext)) + .setSwitchFeatures(SwitchFeaturesUtil.getInstance().buildSwitchFeatures( + new GetFeaturesOutputBuilder(dContext.getPrimaryConnectionContext().getFeatures()).build())) + .build(); + + final InstanceIdentifier fNodeII = nodeII.augmentation(FlowCapableNode.class); + dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, fNodeII, fcNode); + return true; + } + + private static boolean writeTableFeatures(final MultipartType type, + final MultipartReplyBody body, + final DeviceContext dContext, + final InstanceIdentifier nodeII, + final ConvertorExecutor convertorExecutor) { + if (!MultipartType.OFPMPTABLEFEATURES.equals(type)) { + return false; } + + Preconditions.checkArgument(body instanceof MultipartReplyTableFeaturesCase); + final MultipartReplyTableFeatures tableFeaturesMP = ((MultipartReplyTableFeaturesCase) body) + .getMultipartReplyTableFeatures(); + final List tableFeatures = NodeStaticReplyTranslatorUtil + .nodeTableFeatureTranslator(tableFeaturesMP, dContext.getDeviceInfo().getVersion(), convertorExecutor); + for (final TableFeatures tableFeature : tableFeatures) { + final Short tableId = tableFeature.getTableId(); + final KeyedInstanceIdentifier tableFeaturesII = + nodeII.augmentation(FlowCapableNode.class) + .child(TableFeatures.class, new TableFeaturesKey(tableId)); + dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tableFeaturesII, tableFeature); + + // write parent for table statistics + final KeyedInstanceIdentifier tableII = + nodeII.augmentation(FlowCapableNode.class) + .child(Table.class, new TableKey(tableId)); + final TableBuilder tableBld = new TableBuilder().setId(tableId) + .addAugmentation(FlowTableStatisticsData.class, + new FlowTableStatisticsDataBuilder().build()); + + dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tableII, tableBld.build()); + } + + return true; + } + + private static boolean writeMeterFeatures(final MultipartType type, + final MultipartReplyBody body, + final DeviceContext dContext, + final InstanceIdentifier nodeII) { + if (!MultipartType.OFPMPMETERFEATURES.equals(type)) { + return false; + } + + Preconditions.checkArgument(body instanceof MultipartReplyMeterFeaturesCase); + final MultipartReplyMeterFeatures meterFeatures = ((MultipartReplyMeterFeaturesCase) body) + .getMultipartReplyMeterFeatures(); + final NodeMeterFeatures mFeature = NodeStaticReplyTranslatorUtil + .nodeMeterFeatureTranslator(meterFeatures); + final InstanceIdentifier mFeatureII = nodeII + .augmentation(NodeMeterFeatures.class); + dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, mFeatureII, mFeature); + if (0L < mFeature.getMeterFeatures().getMaxMeter().getValue()) { + dContext.getDeviceState().setMeterAvailable(true); + } + + return true; + } + + private static boolean writeGroupFeatures(final MultipartType type, + final MultipartReplyBody body, + final DeviceContext dContext, + final InstanceIdentifier nodeII) { + if (!MultipartType.OFPMPGROUPFEATURES.equals(type)) { + return false; + } + + Preconditions.checkArgument(body instanceof MultipartReplyGroupFeaturesCase); + final MultipartReplyGroupFeatures groupFeatures = ((MultipartReplyGroupFeaturesCase) body) + .getMultipartReplyGroupFeatures(); + final NodeGroupFeatures gFeature = NodeStaticReplyTranslatorUtil + .nodeGroupFeatureTranslator(groupFeatures); + final InstanceIdentifier gFeatureII = nodeII + .augmentation(NodeGroupFeatures.class); + dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, gFeatureII, gFeature); + + return true; + } + + private static boolean writePortDesc(final MultipartType type, + final MultipartReplyBody body, + final DeviceContext dContext, + final InstanceIdentifier nodeII) { + if (!MultipartType.OFPMPPORTDESC.equals(type)) { + return false; + } + + Preconditions.checkArgument(body instanceof MultipartReplyPortDescCase); + final MultipartReplyPortDesc portDesc = ((MultipartReplyPortDescCase) body) + .getMultipartReplyPortDesc(); + for (final PortGrouping port : portDesc.getPorts()) { + final short ofVersion = dContext.getDeviceInfo().getVersion(); + final TranslatorKey translatorKey = new TranslatorKey(ofVersion, PortGrouping.class.getName()); + final MessageTranslator translator = dContext.oook() + .lookupTranslator(translatorKey); + final FlowCapableNodeConnector fcNodeConnector = translator.translate(port, dContext.getDeviceInfo(), null); + + final BigInteger dataPathId = dContext.getPrimaryConnectionContext().getFeatures() + .getDatapathId(); + final NodeConnectorId nodeConnectorId = NodeStaticReplyTranslatorUtil.nodeConnectorId( + dataPathId.toString(), port.getPortNo(), ofVersion); + final NodeConnectorBuilder ncBuilder = new NodeConnectorBuilder().setId(nodeConnectorId); + ncBuilder.addAugmentation(FlowCapableNodeConnector.class, fcNodeConnector); + + ncBuilder.addAugmentation(FlowCapableNodeConnectorStatisticsData.class, + new FlowCapableNodeConnectorStatisticsDataBuilder().build()); + final NodeConnector connector = ncBuilder.build(); + + final InstanceIdentifier connectorII = nodeII.child(NodeConnector.class, + connector.getKey()); + dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, connectorII, connector); + } + + return true; } private static void createEmptyFlowCapableNodeInDs(final DeviceContext deviceContext) { @@ -359,7 +429,9 @@ public class DeviceInitializationUtils { // FIXME : remove after ovs tableFeatures fix private static void makeEmptyTables(final DeviceContext dContext, final InstanceIdentifier nodeII, final Short nrOfTables) { - LOG.debug("About to create {} empty tables.", nrOfTables); + if (LOG.isDebugEnabled()) { + LOG.debug("About to create {} empty tables.", nrOfTables); + } for (int i = 0; i < nrOfTables; i++) { final short tId = (short) i; final InstanceIdentifier tableII = nodeII.augmentation(FlowCapableNode.class).child(Table.class, @@ -388,11 +460,9 @@ public class DeviceInitializationUtils { LOG.info("Static node {} info: {} collected", deviceContext.getDeviceInfo().getNodeId(), type); translateAndWriteReply(type, deviceContext, nodeII, result, convertorExecutor); } else { - final Iterator rpcErrorIterator = rpcResult.getErrors().iterator(); - while (rpcErrorIterator.hasNext()) { - final RpcError rpcError = rpcErrorIterator.next(); + for (RpcError rpcError : rpcResult.getErrors()) { LOG.info("Failed to retrieve static node {} info: {}", type, rpcError.getMessage()); - if (null != rpcError.getCause()) { + if (LOG.isTraceEnabled() && Objects.nonNull(rpcError.getCause())) { LOG.trace("Detailed error:", rpcError.getCause()); } } @@ -411,7 +481,9 @@ public class DeviceInitializationUtils { } private static ListenableFuture>> getNodeStaticInfo(final MultipartType type, - final DeviceContext deviceContext, final InstanceIdentifier nodeII, final short version) { + final DeviceContext deviceContext, + final InstanceIdentifier nodeII, + final short version) { final OutboundQueue queue = deviceContext.getPrimaryConnectionContext().getOutboundQueueProvider(); @@ -426,6 +498,11 @@ public class DeviceInitializationUtils { final Xid xid = requestContext.getXid(); + if (Objects.isNull(xid)) { + LOG.debug("Xid is not present, so cancelling node static info gathering."); + return Futures.immediateCancelledFuture(); + } + LOG.trace("Hooking xid {} to device context - precaution.", reserved); final MultiMsgCollector multiMsgCollector = deviceContext.getMultiMsgCollector(requestContext); @@ -466,21 +543,30 @@ public class DeviceInitializationUtils { final ListenableFuture>>> deviceFeaturesFuture) { try { - LOG.trace("Waiting for protocol version 1.0"); + if (LOG.isTraceEnabled()) { + LOG.trace("Waiting for protocol version 1.0"); + } List>> results = deviceFeaturesFuture.get(); boolean allSucceeded = true; for (final RpcResult> rpcResult : results) { allSucceeded &= rpcResult.isSuccessful(); } if (allSucceeded) { - LOG.debug("Creating emtpy flow capable node: {}", deviceContext.getDeviceInfo().getNodeId().getValue()); + if (LOG.isDebugEnabled()) { + LOG.debug("Creating empty flow capable node: {}", deviceContext.getDeviceInfo().getLOGValue()); + } createEmptyFlowCapableNodeInDs(deviceContext); - LOG.debug("Creating emtpy tables for {}", deviceContext.getDeviceInfo().getNodeId().getValue()); + if (LOG.isDebugEnabled()) { + LOG.debug("Creating empty tables for {}", deviceContext.getDeviceInfo().getLOGValue()); + } makeEmptyTables(deviceContext, deviceContext.getDeviceInfo().getNodeInstanceIdentifier(), deviceContext.getPrimaryConnectionContext().getFeatures().getTables()); } } catch (InterruptedException | ExecutionException e) { - LOG.warn("Error occurred in preparation node {} for protocol 1.0", deviceContext.getDeviceInfo().getNodeId().getValue()); + LOG.warn("Error occurred in preparation node {} for protocol 1.0", deviceContext.getDeviceInfo().getLOGValue()); + if (LOG.isTraceEnabled()) { + LOG.trace("Error for node {} : ", deviceContext.getDeviceInfo().getLOGValue(), e); + } } } }