Bump MRI upstreams
[openflowplugin.git] / applications / forwardingrules-sync / src / main / java / org / opendaylight / openflowplugin / applications / frsync / util / ReconcileUtil.java
index f135fe266986451196b37c4e5ed036c36e718b56..af58030a84019442df3a69a5782b3d9bcbcefcdf 100644 (file)
@@ -1,16 +1,13 @@
-/**
+/*
  * Copyright (c) 2016 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.openflowplugin.applications.frsync.util;
 
 import com.google.common.base.Function;
-import com.google.common.base.MoreObjects;
-import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Iterables;
 import com.google.common.util.concurrent.AsyncFunction;
 import com.google.common.util.concurrent.Futures;
@@ -43,9 +40,12 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.ErrorType;
 import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
+import org.opendaylight.yangtools.yang.common.Uint32;
+import org.opendaylight.yangtools.yang.common.Uint8;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -53,11 +53,10 @@ import org.slf4j.LoggerFactory;
  * Util methods for group reconcil task (future chaining, transforms).
  */
 public final class ReconcileUtil {
-
     private static final Logger LOG = LoggerFactory.getLogger(ReconcileUtil.class);
 
     private ReconcileUtil() {
-        throw new IllegalStateException("This class should not be instantiated.");
+        // Hidden on purpose
     }
 
     /**
@@ -85,7 +84,7 @@ public final class ReconcileUtil {
                 }
             } else {
                 resultSink = RpcResultBuilder.<Void>failed()
-                        .withError(RpcError.ErrorType.APPLICATION, "previous " + previousItemAction + " failed");
+                        .withError(ErrorType.APPLICATION, "previous " + previousItemAction + " failed");
             }
             return resultSink.build();
         };
@@ -112,7 +111,7 @@ public final class ReconcileUtil {
                 }
             } else {
                 resultSink = RpcResultBuilder.<Void>failed()
-                        .withError(RpcError.ErrorType.APPLICATION, "action of " + actionDescription + " failed");
+                        .withError(ErrorType.APPLICATION, "action of " + actionDescription + " failed");
             }
             return resultSink.build();
         };
@@ -154,7 +153,7 @@ public final class ReconcileUtil {
      * @return list of safe synchronization steps with updates
      */
     public static List<ItemSyncBox<Group>> resolveAndDivideGroupDiffs(final NodeId nodeId,
-                                                                      final Map<Long, Group> installedGroupsArg,
+                                                                      final Map<Uint32, Group> installedGroupsArg,
                                                                       final Collection<Group> pendingGroups) {
         return resolveAndDivideGroupDiffs(nodeId, installedGroupsArg, pendingGroups, true);
     }
@@ -169,37 +168,29 @@ public final class ReconcileUtil {
      * @return list of safe synchronization steps
      */
     public static List<ItemSyncBox<Group>> resolveAndDivideGroupDiffs(final NodeId nodeId,
-                                                                      final Map<Long, Group> installedGroupsArg,
+                                                                      final Map<Uint32, Group> installedGroupsArg,
                                                                       final Collection<Group> pendingGroups,
                                                                       final boolean gatherUpdates) {
-        final Map<Long, Group> installedGroups = new HashMap<>(installedGroupsArg);
+        final Map<Uint32, Group> installedGroups = new HashMap<>(installedGroupsArg);
         final List<ItemSyncBox<Group>> plan = new ArrayList<>();
 
         while (!Iterables.isEmpty(pendingGroups)) {
             final ItemSyncBox<Group> stepPlan = new ItemSyncBox<>();
             final Iterator<Group> iterator = pendingGroups.iterator();
-            final Map<Long, Group> installIncrement = new HashMap<>();
+            final Map<Uint32, Group> installIncrement = new HashMap<>();
 
             while (iterator.hasNext()) {
                 final Group group = iterator.next();
 
                 final Group existingGroup = installedGroups.get(group.getGroupId().getValue());
                 if (existingGroup != null) {
-                    if (!gatherUpdates) {
+                    if (!gatherUpdates || group.equals(existingGroup)) {
                         iterator.remove();
-                    } else {
+                    } else if (checkGroupPrecondition(installedGroups.keySet(), group)) {
                         // check buckets and eventually update
-                        if (group.equals(existingGroup)) {
-                            iterator.remove();
-                        } else {
-                            if (checkGroupPrecondition(installedGroups.keySet(), group)) {
-                                iterator.remove();
-                                LOG.trace("Group {} on device {} differs - planned for update", group.getGroupId(),
-                                        nodeId);
-                                stepPlan.getItemsToUpdate().add(new ItemSyncBox.ItemUpdateTuple<>(existingGroup,
-                                        group));
-                            }
-                        }
+                        iterator.remove();
+                        LOG.trace("Group {} on device {} differs - planned for update", group.getGroupId(), nodeId);
+                        stepPlan.getItemsToUpdate().add(new ItemSyncBox.ItemUpdateTuple<>(existingGroup, group));
                     }
                 } else if (checkGroupPrecondition(installedGroups.keySet(), group)) {
                     iterator.remove();
@@ -222,14 +213,14 @@ public final class ReconcileUtil {
         return plan;
     }
 
-    public static boolean checkGroupPrecondition(final Set<Long> installedGroupIds, final Group pendingGroup) {
+    public static boolean checkGroupPrecondition(final Set<Uint32> installedGroupIds, final Group pendingGroup) {
         boolean okToInstall = true;
         // check each bucket in the pending group
-        for (Bucket bucket : pendingGroup.getBuckets().getBucket()) {
-            for (Action action : bucket.getAction()) {
+        for (Bucket bucket : pendingGroup.getBuckets().nonnullBucket().values()) {
+            for (Action action : bucket.nonnullAction().values()) {
                 // if the output action is a group
-                if (GroupActionCase.class.equals(action.getAction().getImplementedInterface())) {
-                    Long groupId = ((GroupActionCase) action.getAction()).getGroupAction().getGroupId();
+                if (GroupActionCase.class.equals(action.getAction().implementedInterface())) {
+                    Uint32 groupId = ((GroupActionCase) action.getAction()).getGroupAction().getGroupId();
                     // see if that output group is installed
                     if (!installedGroupIds.contains(groupId)) {
                         // if not installed, we have missing dependencies and cannot install this pending group
@@ -272,7 +263,7 @@ public final class ReconcileUtil {
      */
     public static ItemSyncBox<Meter> resolveMeterDiffs(final NodeId nodeId,
                                                        final Map<MeterId, Meter> meterOperationalMap,
-                                                       final List<Meter> metersConfigured,
+                                                       final Collection<Meter> metersConfigured,
                                                        final boolean gatherUpdates) {
         LOG.trace("resolving meters for {}", nodeId.getValue());
         final ItemSyncBox<Meter> syncBox = new ItemSyncBox<>();
@@ -298,7 +289,7 @@ public final class ReconcileUtil {
      * @param gatherUpdates      check content of pending item if present on device (and create update task eventually)
      * @return list of safe synchronization steps
      */
-    private static ItemSyncBox<Flow> resolveFlowDiffsInTable(final List<Flow> flowsConfigured,
+    private static ItemSyncBox<Flow> resolveFlowDiffsInTable(final Collection<Flow> flowsConfigured,
                                                             final Map<FlowDescriptor, Flow> flowOperationalMap,
                                                             final boolean gatherUpdates) {
         final ItemSyncBox<Flow> flowsSyncBox = new ItemSyncBox<>();
@@ -328,13 +319,13 @@ public final class ReconcileUtil {
      * @return map : key={@link TableKey}, value={@link ItemSyncBox} of safe synchronization steps
      */
     public static Map<TableKey, ItemSyncBox<Flow>> resolveFlowDiffsInAllTables(final NodeId nodeId,
-            final Map<Short, Table> tableOperationalMap, final List<Table> tablesConfigured,
+            final Map<Uint8, Table> tableOperationalMap, final Collection<Table> tablesConfigured,
             final boolean gatherUpdates) {
         LOG.trace("resolving flows in tables for {}", nodeId.getValue());
         final Map<TableKey, ItemSyncBox<Flow>> tableFlowSyncBoxes = new HashMap<>();
         for (final Table tableConfigured : tablesConfigured) {
-            final List<Flow> flowsConfigured = tableConfigured.getFlow();
-            if (flowsConfigured == null || flowsConfigured.isEmpty()) {
+            final Collection<Flow> flowsConfigured = tableConfigured.nonnullFlow().values();
+            if (flowsConfigured.isEmpty()) {
                 continue;
             }
 
@@ -343,7 +334,7 @@ public final class ReconcileUtil {
             // wrap existing (on device) flows in current table into map
             final Map<FlowDescriptor, Flow> flowOperationalMap = FlowCapableNodeLookups.wrapFlowsToMap(
                     tableOperational != null
-                            ? tableOperational.getFlow()
+                            ? tableOperational.nonnullFlow().values()
                             : null);
 
 
@@ -356,27 +347,15 @@ public final class ReconcileUtil {
         return tableFlowSyncBoxes;
     }
 
-    public static List<Group> safeGroups(FlowCapableNode node) {
-        if (node == null) {
-            return Collections.emptyList();
-        }
-
-        return MoreObjects.firstNonNull(node.getGroup(), ImmutableList.of());
+    public static Collection<Group> safeGroups(FlowCapableNode node) {
+        return node == null ? Collections.emptyList() : node.nonnullGroup().values();
     }
 
-    public static List<Table> safeTables(FlowCapableNode node) {
-        if (node == null) {
-            return Collections.emptyList();
-        }
-
-        return MoreObjects.firstNonNull(node.getTable(), ImmutableList.of());
+    public static Collection<Table> safeTables(FlowCapableNode node) {
+        return node == null ? Collections.emptyList() : node.nonnullTable().values();
     }
 
-    public static List<Meter> safeMeters(FlowCapableNode node) {
-        if (node == null) {
-            return Collections.emptyList();
-        }
-
-        return MoreObjects.firstNonNull(node.getMeter(), ImmutableList.of());
+    public static Collection<Meter> safeMeters(FlowCapableNode node) {
+        return node == null ? Collections.emptyList() : node.nonnullMeter().values();
     }
 }