Migrate more List-based map setters 90/94590/2
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 10 Jan 2021 20:05:26 +0000 (21:05 +0100)
committerRobert Varga <nite@hq.sk>
Sun, 10 Jan 2021 23:21:27 +0000 (23:21 +0000)
There are a number of cases where we can use BindingMap and its
Builder to create a ready-made map.

Change-Id: I5205681583849bb95c81c8dc95728436aa0701c5
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/protocol/deserialization/instruction/AbstractActionInstructionDeserializer.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/protocol/deserialization/multipart/MultipartReplyGroupDescDeserializer.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/protocol/deserialization/multipart/MultipartReplyTableFeaturesDeserializer.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/statistics/StatisticsGatheringUtils.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/statistics/services/OpendaylightFlowTableStatisticsServiceImpl.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/translator/PortUpdateTranslator.java

index b74649238605e9df1de3e342b74c50d3a19c14a9..be2c99adba593257c356348e0dcd0a02f82da260 100644 (file)
@@ -5,20 +5,20 @@
  * 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.openflowplugin.impl.protocol.deserialization.instruction;
 
 import io.netty.buffer.ByteBuf;
-import java.util.ArrayList;
-import java.util.List;
+import java.util.Map;
 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistryInjector;
 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
 import org.opendaylight.openflowjava.protocol.impl.util.InstructionConstants;
 import org.opendaylight.openflowplugin.extension.api.path.ActionPath;
 import org.opendaylight.openflowplugin.impl.protocol.deserialization.util.ActionUtil;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
+import org.opendaylight.yangtools.yang.binding.util.BindingMap;
 
 public abstract class AbstractActionInstructionDeserializer extends AbstractInstructionDeserializer
         implements DeserializerRegistryInjector {
@@ -41,7 +41,7 @@ public abstract class AbstractActionInstructionDeserializer extends AbstractInst
      * @param message Openflow buffered message
      * @return instruction length
      **/
-    protected static int readHeader(ByteBuf message) {
+    protected static int readHeader(final ByteBuf message) {
         message.skipBytes(Short.BYTES);
         return message.readUnsignedShort();
     }
@@ -53,35 +53,28 @@ public abstract class AbstractActionInstructionDeserializer extends AbstractInst
      * @param length  instruction length
      * @return list of actions
      **/
-    protected List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list
-            .Action> readActions(ByteBuf message, int length) {
+    protected Map<ActionKey, Action> readActions(final ByteBuf message, final int length) {
+        if (message.readableBytes() <= 0) {
+            return Map.of();
+        }
 
         final int instrLength = length - InstructionConstants.STANDARD_INSTRUCTION_LENGTH;
+        final var actions = BindingMap.<ActionKey, Action>orderedBuilder();
+        final int startIndex = message.readerIndex();
+        int offset = 0;
 
-        final List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list
-                .Action> actions = new ArrayList<>();
-
-        if (message.readableBytes() > 0) {
-            final int startIndex = message.readerIndex();
-            int offset = 0;
-
-            while ((message.readerIndex() - startIndex) < instrLength) {
-                actions.add(new ActionBuilder()
-                        .withKey(new ActionKey(offset))
-                        .setOrder(offset)
-                        .setAction(ActionUtil
-                                .readAction(EncodeConstants.OF13_VERSION_ID, message, registry, actionPath))
-                        .build());
-
-                offset++;
-            }
+        while (message.readerIndex() - startIndex < instrLength) {
+            actions.add(new ActionBuilder()
+                .setOrder(offset++)
+                .setAction(ActionUtil.readAction(EncodeConstants.OF13_VERSION_ID, message, registry, actionPath))
+                .build());
         }
 
-        return actions;
+        return actions.build();
     }
 
     @Override
-    public void injectDeserializerRegistry(DeserializerRegistry deserializerRegistry) {
+    public void injectDeserializerRegistry(final DeserializerRegistry deserializerRegistry) {
         registry = deserializerRegistry;
     }
 
index ab959bea9e37c37c2bfc3a2f30188030af3715d2..ff50955d92f38c92fd918275391841bd15a90a70 100644 (file)
@@ -5,18 +5,16 @@
  * 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.openflowplugin.impl.protocol.deserialization.multipart;
 
 import io.netty.buffer.ByteBuf;
-import java.util.ArrayList;
-import java.util.List;
 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistryInjector;
 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
 import org.opendaylight.openflowplugin.extension.api.path.ActionPath;
 import org.opendaylight.openflowplugin.impl.protocol.deserialization.util.ActionUtil;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.multipart.reply.multipart.reply.body.MultipartReplyGroupDescBuilder;
@@ -31,6 +29,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group
 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.desc.stats.reply.GroupDescStatsBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.desc.stats.reply.GroupDescStatsKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.multipart.reply.MultipartReplyBody;
+import org.opendaylight.yangtools.yang.binding.util.BindingMap;
 
 public class MultipartReplyGroupDescDeserializer implements OFDeserializer<MultipartReplyBody>,
         DeserializerRegistryInjector {
@@ -42,9 +41,9 @@ public class MultipartReplyGroupDescDeserializer implements OFDeserializer<Multi
     private DeserializerRegistry registry;
 
     @Override
-    public MultipartReplyBody deserialize(ByteBuf message) {
+    public MultipartReplyBody deserialize(final ByteBuf message) {
         final MultipartReplyGroupDescBuilder builder = new MultipartReplyGroupDescBuilder();
-        final List<GroupDescStats> items = new ArrayList<>();
+        final var items =  BindingMap.<GroupDescStatsKey, GroupDescStats>orderedBuilder();
 
         while (message.readableBytes() > 0) {
             final int itemLength = message.readUnsignedShort();
@@ -56,7 +55,7 @@ public class MultipartReplyGroupDescDeserializer implements OFDeserializer<Multi
             itemBuilder.setGroupId(new GroupId(message.readUnsignedInt()));
             itemBuilder.withKey(new GroupDescStatsKey(itemBuilder.getGroupId()));
 
-            final List<Bucket> subItems = new ArrayList<>();
+            final var subItems = BindingMap.<BucketKey, Bucket>orderedBuilder();
             int actualLength = GROUP_DESC_HEADER_LENGTH;
 
             long bucketKey = 0;
@@ -71,8 +70,7 @@ public class MultipartReplyGroupDescDeserializer implements OFDeserializer<Multi
                         .setWatchGroup(message.readUnsignedInt());
 
                 message.skipBytes(PADDING_IN_BUCKETS_HEADER);
-                final List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list
-                        .Action> actions = new ArrayList<>();
+                final var actions = BindingMap.<ActionKey, Action>orderedBuilder();
                 final int startIndex = message.readerIndex();
                 final int bucketLength = bucketsLength - BUCKETS_HEADER_LENGTH;
                 int offset = 0;
@@ -88,26 +86,19 @@ public class MultipartReplyGroupDescDeserializer implements OFDeserializer<Multi
                     offset++;
                 }
 
-                bucketBuilder.setAction(actions);
-                subItems.add(bucketBuilder.build());
+                subItems.add(bucketBuilder.setAction(actions.build()).build());
                 bucketKey++;
                 actualLength += bucketsLength;
             }
 
-            items.add(itemBuilder
-                    .setBuckets(new BucketsBuilder()
-                            .setBucket(subItems)
-                            .build())
-                    .build());
+            items.add(itemBuilder.setBuckets(new BucketsBuilder().setBucket(subItems.build()).build()).build());
         }
 
-        return builder
-                .setGroupDescStats(items)
-                .build();
+        return builder.setGroupDescStats(items.build()).build();
     }
 
     @Override
-    public void injectDeserializerRegistry(DeserializerRegistry deserializerRegistry) {
+    public void injectDeserializerRegistry(final DeserializerRegistry deserializerRegistry) {
         registry = deserializerRegistry;
     }
 
index 2069f8c6b58adc827fc7c33d9172ee294bbe36c1..e496af694d5f15507fb7f90d6fb456fcdd5aad50 100644 (file)
@@ -14,6 +14,7 @@ import static org.opendaylight.yangtools.yang.common.netty.ByteBufUtils.readUint
 import io.netty.buffer.ByteBuf;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistryInjector;
 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
@@ -59,6 +60,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table
 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.table.features.table.properties.TableFeatureProperties;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.table.features.table.properties.TableFeaturePropertiesBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.table.features.table.properties.TableFeaturePropertiesKey;
+import org.opendaylight.yangtools.yang.binding.util.BindingMap;
 import org.opendaylight.yangtools.yang.common.Uint8;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -332,8 +334,8 @@ public class MultipartReplyTableFeaturesDeserializer implements OFDeserializer<M
     }
 
     @SuppressWarnings("checkstyle:LineLength")
-    private List<Action> readActions(final ByteBuf message, final int length) {
-        final List<Action> actions = new ArrayList<>();
+    private Map<ActionKey, Action> readActions(final ByteBuf message, final int length) {
+        final var actions = BindingMap.<ActionKey, Action>orderedBuilder();
         final int startIndex = message.readerIndex();
         int offset = 0;
 
@@ -348,11 +350,13 @@ public class MultipartReplyTableFeaturesDeserializer implements OFDeserializer<M
 
                 offset++;
             } catch (ClassCastException | IllegalStateException e) {
+                LOG.debug("Failed to build action", e);
+                // FIXME: what are we guarding here?
                 message.skipBytes(2 * Short.BYTES);
             }
         }
 
-        return actions;
+        return actions.build();
     }
 
     @Override
index 060100544d35bf0cbdb64e49b9601d0b20cff7ba..76f906d046d2c3e56c4e3e614123db7c3f8c6ff3 100755 (executable)
@@ -185,7 +185,7 @@ public final class StatisticsGatheringUtils {
                     // merge is expensive and not applicable for lists
                     if (flowCapNodeOpt != null && flowCapNodeOpt.isPresent()) {
                         for (final Table tableData : flowCapNodeOpt.get().nonnullTable().values()) {
-                            final Table table = new TableBuilder(tableData).setFlow(Collections.emptyList()).build();
+                            final Table table = new TableBuilder(tableData).setFlow(Collections.emptyMap()).build();
                             final InstanceIdentifier<Table> iiToTable = instanceIdentifier
                                 .child(Table.class, tableData.key());
                             txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, iiToTable, table);
index ec653043938260d9505c4a68db51799dc2ad36ae..dca8421c1679c98c4795ab7035979934265b4490 100644 (file)
@@ -8,7 +8,6 @@
 package org.opendaylight.openflowplugin.impl.statistics.services;
 
 import com.google.common.util.concurrent.ListenableFuture;
-import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.atomic.AtomicLong;
 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
@@ -27,6 +26,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev13
 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.and.statistics.map.FlowTableAndStatisticsMapBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.and.statistics.map.FlowTableAndStatisticsMapKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
@@ -38,6 +38,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestTableCaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.table._case.MultipartRequestTableBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId;
+import org.opendaylight.yangtools.yang.binding.util.BindingMap;
 import org.opendaylight.yangtools.yang.common.Empty;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 
@@ -82,27 +83,25 @@ public final class OpendaylightFlowTableStatisticsServiceImpl extends
     }
 
     @Override
-    public GetFlowTablesStatisticsOutput buildTxCapableResult(TransactionId emulatedTxId) {
+    public GetFlowTablesStatisticsOutput buildTxCapableResult(final TransactionId emulatedTxId) {
         return new GetFlowTablesStatisticsOutputBuilder().setTransactionId(emulatedTxId).build();
     }
 
     @Override
-    public FlowTableStatisticsUpdate transformToNotification(List<MultipartReply> mpReplyList,
-                                                             TransactionId emulatedTxId) {
+    public FlowTableStatisticsUpdate transformToNotification(final List<MultipartReply> mpReplyList,
+                                                             final TransactionId emulatedTxId) {
         FlowTableStatisticsUpdateBuilder notification = new FlowTableStatisticsUpdateBuilder();
         notification.setId(getDeviceInfo().getNodeId());
         notification.setMoreReplies(Boolean.FALSE);
         notification.setTransactionId(emulatedTxId);
 
-        final List<FlowTableAndStatisticsMap> salFlowStats = new ArrayList<>();
-        notification.setFlowTableAndStatisticsMap(salFlowStats);
+        final var salFlowStats = BindingMap.<FlowTableAndStatisticsMapKey, FlowTableAndStatisticsMap>orderedBuilder();
         for (MultipartReply mpReply : mpReplyList) {
             MultipartReplyTableCase caseBody = (MultipartReplyTableCase) mpReply.getMultipartReplyBody();
             MultipartReplyTable replyBody = caseBody.getMultipartReplyTable();
-            List<TableStats> swTablesStats = replyBody.getTableStats();
 
             //TODO: Duplicate code: look at MultiReplyTranslatorUtil method translateTable
-            for (TableStats swTableStats : swTablesStats) {
+            for (TableStats swTableStats : replyBody.nonnullTableStats()) {
                 FlowTableAndStatisticsMapBuilder statisticsBuilder = new FlowTableAndStatisticsMapBuilder();
                 statisticsBuilder.setActiveFlows(new Counter32(swTableStats.getActiveCount()));
                 statisticsBuilder.setPacketsLookedUp(new Counter64(swTableStats.getLookupCount()));
@@ -112,6 +111,6 @@ public final class OpendaylightFlowTableStatisticsServiceImpl extends
             }
         }
 
-        return notification.build();
+        return notification.setFlowTableAndStatisticsMap(salFlowStats.build()).build();
     }
 }
index 30ca988e4c260b3917929fffb51b6d159c32eb03..63a30a1333247e85d5a570469b7db9a27352b9b4 100644 (file)
@@ -44,8 +44,7 @@ public class PortUpdateTranslator implements MessageTranslator<PortGrouping, Flo
             builder.setPeerFeatures(PortTranslatorUtil.translatePortFeatures(input.getPeerFeatures()));
             builder.setState(PortTranslatorUtil.translatePortState(input.getState()));
             builder.setSupported(PortTranslatorUtil.translatePortFeatures(input.getSupportedFeatures()));
-            builder.setQueue(Collections
-                    .emptyList());
+            builder.setQueue(Collections.emptyMap());
         }
         if (input instanceof PortStatusMessage) {
             if (((PortStatusMessage) input).getReason() != null) {