From: Tomas Slusny Date: Tue, 2 Aug 2016 10:58:01 +0000 (+0200) Subject: SONAR TD - FlatBatchUtil, DeviceInitializationUtils X-Git-Tag: release/carbon~188^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=81ec040affdf77f72ae1060436e7010386f410ed;p=openflowplugin.git SONAR TD - FlatBatchUtil, DeviceInitializationUtils - Removed unused fields - Reduced cyclomatic complexity of code - Added private constructor to DeviceInitializationUtils Change-Id: I280c9534ad87a6d6826e2a209d07edb1ff25c624 Signed-off-by: Tomas Slusny --- 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 52feaf74fe..ad5599f1ce 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,7 +19,6 @@ 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.concurrent.ExecutionException; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; @@ -89,6 +88,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. @@ -249,106 +252,154 @@ public class DeviceInitializationUtils { 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); + 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); } - 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); - } - - break; - - 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); } } + private static boolean writeDesc(final MultipartType type, + final MultipartReplyBody body, + final DeviceContext dContext, + final InstanceIdentifier nodeII) { + if (!MultipartType.OFPMPDESC.equals(type)) { + return false; + } + + 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); + 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) { final FlowCapableNodeBuilder flowCapableNodeBuilder = new FlowCapableNodeBuilder(); final InstanceIdentifier fNodeII = deviceContext.getDeviceInfo().getNodeInstanceIdentifier() @@ -407,9 +458,7 @@ 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()) { LOG.trace("Detailed error:", rpcError.getCause()); diff --git a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/util/FlatBatchUtil.java b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/util/FlatBatchUtil.java index 9850864e0d..e602eb2d42 100644 --- a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/util/FlatBatchUtil.java +++ b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/util/FlatBatchUtil.java @@ -36,16 +36,12 @@ import org.opendaylight.yangtools.yang.binding.DataContainer; import org.opendaylight.yangtools.yang.common.RpcError; import org.opendaylight.yangtools.yang.common.RpcResult; import org.opendaylight.yangtools.yang.common.RpcResultBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * provides flat batch util methods */ public final class FlatBatchUtil { - private static final Logger LOG = LoggerFactory.getLogger(FlatBatchUtil.class); - private FlatBatchUtil() { throw new IllegalStateException("This class should not be instantiated."); } @@ -67,31 +63,33 @@ public final class FlatBatchUtil { @VisibleForTesting static boolean decideBarrier(final EnumSet previousTypes, final BatchStepType type) { - final boolean needBarrier; - switch (type) { - case FLOW_ADD: - case FLOW_UPDATE: - needBarrier = previousTypes.contains(BatchStepType.GROUP_ADD) - || previousTypes.contains(BatchStepType.METER_ADD); - break; - case GROUP_ADD: - needBarrier = previousTypes.contains(BatchStepType.GROUP_ADD) - || previousTypes.contains(BatchStepType.GROUP_UPDATE); - break; - case GROUP_REMOVE: - needBarrier = previousTypes.contains(BatchStepType.FLOW_REMOVE) - || previousTypes.contains(BatchStepType.FLOW_UPDATE) - || previousTypes.contains(BatchStepType.GROUP_REMOVE) - || previousTypes.contains(BatchStepType.GROUP_UPDATE); - break; - case METER_REMOVE: - needBarrier = previousTypes.contains(BatchStepType.FLOW_REMOVE) - || previousTypes.contains(BatchStepType.FLOW_UPDATE); - break; - default: - needBarrier = false; - } - return needBarrier; + return isFlowBarrierNeeded(previousTypes, type) + || isGroupBarrierNeeded(previousTypes, type) + || isMeterBarrierNeeded(previousTypes, type); + } + + private static boolean isFlowBarrierNeeded(final EnumSet previousTypes, final BatchStepType type) { + return (type == BatchStepType.FLOW_ADD + || type == BatchStepType.FLOW_UPDATE) + && (previousTypes.contains(BatchStepType.GROUP_ADD) + || previousTypes.contains(BatchStepType.METER_ADD)); + } + + private static boolean isGroupBarrierNeeded(final EnumSet previousTypes, final BatchStepType type) { + return (type == BatchStepType.GROUP_ADD + && (previousTypes.contains(BatchStepType.GROUP_ADD) + || previousTypes.contains(BatchStepType.GROUP_UPDATE))) + || (type == BatchStepType.GROUP_REMOVE + && (previousTypes.contains(BatchStepType.FLOW_REMOVE) + || previousTypes.contains(BatchStepType.FLOW_UPDATE) + || previousTypes.contains(BatchStepType.GROUP_REMOVE) + || previousTypes.contains(BatchStepType.GROUP_UPDATE))); + } + + private static boolean isMeterBarrierNeeded(final EnumSet previousTypes, final BatchStepType type) { + return type == BatchStepType.METER_REMOVE + && (previousTypes.contains(BatchStepType.FLOW_REMOVE) + || previousTypes.contains(BatchStepType.FLOW_UPDATE)); } public static List assembleBatchPlan(List batches) {