Upgrade powermock to 2.0.0
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / transactions / md / OvsdbBridgeUpdateCommand.java
index 9ab33b7681d3f715d2ec2b2cdb40b3007b75db47..e21410997e10261534a846da331fb4c56cfad541 100644 (file)
@@ -5,18 +5,23 @@
  * 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.ovsdb.southbound.transactions.md;
 
+import static org.opendaylight.ovsdb.southbound.SouthboundUtil.schemaMismatchLog;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+import com.google.common.net.InetAddresses;
 import java.net.InetAddress;
 import java.net.NetworkInterface;
+import java.net.SocketException;
 import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
-
 import org.apache.commons.lang3.math.NumberUtils;
 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
@@ -27,12 +32,13 @@ import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
 import org.opendaylight.ovsdb.schema.openvswitch.Controller;
+import org.opendaylight.ovsdb.southbound.InstanceIdentifierCodec;
 import org.opendaylight.ovsdb.southbound.OvsdbConnectionInstance;
 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressBuilder;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
@@ -63,53 +69,59 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
-import com.google.common.net.InetAddresses;
-
 public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand {
     private static final Logger LOG = LoggerFactory.getLogger(OvsdbBridgeUpdateCommand.class);
-    private Map<UUID,Bridge> updatedBridgeRows;
-    private Map<UUID, Bridge> oldBridgeRows;
+    private final InstanceIdentifierCodec instanceIdentifierCodec;
+    private final Map<UUID,Bridge> updatedBridgeRows;
+    private final Map<UUID, Bridge> oldBridgeRows;
+    private final List<InstanceIdentifier<Node>> updatedBridges = new ArrayList<>();
 
-    public OvsdbBridgeUpdateCommand(OvsdbConnectionInstance key, TableUpdates updates,
-            DatabaseSchema dbSchema) {
+    public OvsdbBridgeUpdateCommand(InstanceIdentifierCodec instanceIdentifierCodec, OvsdbConnectionInstance key,
+            TableUpdates updates, DatabaseSchema dbSchema) {
         super(key,updates,dbSchema);
+        this.instanceIdentifierCodec = instanceIdentifierCodec;
         updatedBridgeRows = TyperUtils.extractRowsUpdated(Bridge.class, getUpdates(), getDbSchema());
         oldBridgeRows = TyperUtils.extractRowsOld(Bridge.class, getUpdates(), getDbSchema());
     }
 
     @Override
     public void execute(ReadWriteTransaction transaction) {
-        for (Entry<UUID, Bridge> entry : updatedBridgeRows.entrySet()) {
-            updateBridge(transaction, entry.getValue());
+        if (updatedBridgeRows == null || updatedBridgeRows.isEmpty()) {
+            return;
         }
-    }
 
-    private void updateBridge(ReadWriteTransaction transaction,
-            Bridge bridge) {
         final InstanceIdentifier<Node> connectionIId = getOvsdbConnectionInstance().getInstanceIdentifier();
         Optional<Node> connection = SouthboundUtil.readNode(transaction, connectionIId);
-        if (connection.isPresent()) {
-            LOG.debug("Connection {} is present",connection);
-
-            // Update the connection node to let it know it manages this bridge
-            Node connectionNode = buildConnectionNode(bridge);
-            transaction.merge(LogicalDatastoreType.OPERATIONAL, connectionIId, connectionNode);
-
-            // Update the bridge node with whatever data we are getting
-            InstanceIdentifier<Node> bridgeIid = getInstanceIdentifier(bridge);
-            Node bridgeNode = buildBridgeNode(bridge);
-            transaction.merge(LogicalDatastoreType.OPERATIONAL, bridgeIid, bridgeNode);
-            deleteEntries(transaction, protocolEntriesToRemove(bridgeIid,bridge));
-            deleteEntries(transaction, externalIdsToRemove(bridgeIid,bridge));
-            deleteEntries(transaction, bridgeOtherConfigsToRemove(bridgeIid,bridge));
+        if (!connection.isPresent()) {
+            return;
+        }
+
+        for (Entry<UUID, Bridge> entry : updatedBridgeRows.entrySet()) {
+            updateBridge(transaction, entry.getValue(), connectionIId);
         }
     }
 
-    private <T extends DataObject> void deleteEntries(ReadWriteTransaction transaction,
+    @VisibleForTesting
+    void updateBridge(ReadWriteTransaction transaction,
+            Bridge bridge, InstanceIdentifier<Node> connectionIId) {
+        // Update the connection node to let it know it manages this bridge
+        Node connectionNode = buildConnectionNode(bridge);
+        transaction.merge(LogicalDatastoreType.OPERATIONAL, connectionIId, connectionNode);
+
+        // Update the bridge node with whatever data we are getting
+        InstanceIdentifier<Node> bridgeIid = getInstanceIdentifier(bridge);
+        Node bridgeNode = buildBridgeNode(bridge);
+        transaction.merge(LogicalDatastoreType.OPERATIONAL, bridgeIid, bridgeNode);
+        updatedBridges.add(bridgeIid);
+        deleteEntries(transaction, protocolEntriesToRemove(bridgeIid, bridge));
+        deleteEntries(transaction, externalIdsToRemove(bridgeIid,bridge));
+        deleteEntries(transaction, bridgeOtherConfigsToRemove(bridgeIid,bridge));
+    }
+
+    @VisibleForTesting
+    <T extends DataObject> void deleteEntries(ReadWriteTransaction transaction,
             List<InstanceIdentifier<T>> entryIids) {
-        for (InstanceIdentifier<T> entryIid: entryIids) {
+        for (InstanceIdentifier<T> entryIid : entryIids) {
             transaction.delete(LogicalDatastoreType.OPERATIONAL, entryIid);
         }
     }
@@ -185,8 +197,7 @@ public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand {
                 }
             }
         } catch (SchemaVersionMismatchException e) {
-            // We don't care about the exception stack trace here
-            LOG.warn("protocol not supported by this version of ovsdb: {}", e.getMessage());
+            schemaMismatchLog("protocols", "Bridge", e);
         }
         return result;
     }
@@ -199,8 +210,9 @@ public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand {
 
         OvsdbNodeAugmentationBuilder ovsdbConnectionAugmentationBuilder = new OvsdbNodeAugmentationBuilder();
         List<ManagedNodeEntry> managedBridges = new ArrayList<>();
-        InstanceIdentifier<Node> bridgeIid = SouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(),
-                bridge);
+        InstanceIdentifier<Node> bridgeIid =
+                SouthboundMapper.createInstanceIdentifier(instanceIdentifierCodec, getOvsdbConnectionInstance(),
+                        bridge);
         ManagedNodeEntry managedBridge = new ManagedNodeEntryBuilder().setBridgeRef(
                 new OvsdbBridgeRef(bridgeIid)).build();
         managedBridges.add(managedBridge);
@@ -228,6 +240,8 @@ public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand {
         setFailMode(ovsdbBridgeAugmentationBuilder, bridge);
         setOpenFlowNodeRef(ovsdbBridgeAugmentationBuilder, bridge);
         setManagedBy(ovsdbBridgeAugmentationBuilder);
+        setAutoAttach(ovsdbBridgeAugmentationBuilder, bridge);
+        setStpEnalbe(ovsdbBridgeAugmentationBuilder,bridge);
         bridgeNodeBuilder.addAugmentation(OvsdbBridgeAugmentation.class, ovsdbBridgeAugmentationBuilder.build());
 
         LOG.debug("Built with the intent to store bridge data {}",
@@ -235,19 +249,32 @@ public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand {
         return bridgeNodeBuilder.build();
     }
 
+    private static void setAutoAttach(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder, Bridge bridge) {
+        try {
+            if (bridge.getAutoAttachColumn() != null
+                    && bridge.getAutoAttachColumn().getData() != null
+                    && !bridge.getAutoAttachColumn().getData().isEmpty()) {
+                Set<UUID> uuids = bridge.getAutoAttachColumn().getData();
+                for (UUID uuid : uuids) {
+                    ovsdbBridgeAugmentationBuilder.setAutoAttach(new Uuid(uuid.toString()));
+                }
+            }
+        } catch (SchemaVersionMismatchException e) {
+            schemaMismatchLog("auto_attach", "Bridge", e);
+        }
+    }
+
     private void setManagedBy(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder) {
         InstanceIdentifier<Node> connectionNodePath = getOvsdbConnectionInstance().getInstanceIdentifier();
         ovsdbBridgeAugmentationBuilder.setManagedBy(new OvsdbNodeRef(connectionNodePath));
     }
 
-    private void setDataPathType(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
-            Bridge bridge) {
+    private static void setDataPathType(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder, Bridge bridge) {
         ovsdbBridgeAugmentationBuilder.setDatapathType(
                 SouthboundMapper.createDatapathType(bridge.getDatapathTypeColumn().getData()));
     }
 
-    private void setFailMode(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
-            Bridge bridge) {
+    private static void setFailMode(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder, Bridge bridge) {
         if (bridge.getFailModeColumn() != null
                 && bridge.getFailModeColumn().getData() != null
                 && !bridge.getFailModeColumn().getData().isEmpty()) {
@@ -258,16 +285,14 @@ public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand {
         }
     }
 
-    private void setOtherConfig(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
-            Bridge bridge) {
+    private static void setOtherConfig(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder, Bridge bridge) {
         Map<String, String> otherConfigs = bridge
                 .getOtherConfigColumn().getData();
         if (otherConfigs != null && !otherConfigs.isEmpty()) {
-            Set<String> otherConfigKeys = otherConfigs.keySet();
             List<BridgeOtherConfigs> otherConfigList = new ArrayList<>();
-            String otherConfigValue;
-            for (String otherConfigKey : otherConfigKeys) {
-                otherConfigValue = otherConfigs.get(otherConfigKey);
+            for (Entry<String, String> entry : otherConfigs.entrySet()) {
+                String otherConfigKey = entry.getKey();
+                String otherConfigValue = entry.getValue();
                 if (otherConfigKey != null && otherConfigValue != null) {
                     otherConfigList.add(new BridgeOtherConfigsBuilder()
                             .setBridgeOtherConfigKey(otherConfigKey)
@@ -279,16 +304,14 @@ public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand {
         }
     }
 
-    private void setExternalIds(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
-            Bridge bridge) {
+    private static void setExternalIds(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder, Bridge bridge) {
         Map<String, String> externalIds = bridge.getExternalIdsColumn()
                 .getData();
         if (externalIds != null && !externalIds.isEmpty()) {
-            Set<String> externalIdKeys = externalIds.keySet();
             List<BridgeExternalIds> externalIdsList = new ArrayList<>();
-            String externalIdValue;
-            for (String externalIdKey : externalIdKeys) {
-                externalIdValue = externalIds.get(externalIdKey);
+            for (Entry<String, String> entry : externalIds.entrySet()) {
+                String externalIdKey = entry.getKey();
+                String externalIdValue = entry.getValue();
                 if (externalIdKey != null && externalIdValue != null) {
                     externalIdsList.add(new BridgeExternalIdsBuilder()
                             .setBridgeExternalIdKey(externalIdKey)
@@ -300,24 +323,31 @@ public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand {
         }
     }
 
-    private void setProtocol(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
-            Bridge bridge) {
-        if (SouthboundMapper.createMdsalProtocols(bridge) != null
-                && SouthboundMapper.createMdsalProtocols(bridge).size() > 0) {
-            ovsdbBridgeAugmentationBuilder.setProtocolEntry(SouthboundMapper.createMdsalProtocols(bridge));
+    private static void setProtocol(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder, Bridge bridge) {
+        final List<ProtocolEntry> protocols = SouthboundMapper.createMdsalProtocols(bridge);
+        if (!protocols.isEmpty()) {
+            ovsdbBridgeAugmentationBuilder.setProtocolEntry(protocols);
         }
     }
 
-    private void setDataPath(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
-            Bridge bridge) {
+    private static void setDataPath(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder, Bridge bridge) {
         DatapathId dpid = SouthboundMapper.createDatapathId(bridge);
         if (dpid != null) {
             ovsdbBridgeAugmentationBuilder.setDatapathId(dpid);
         }
     }
 
-    private void setOpenFlowNodeRef(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
-            Bridge bridge) {
+    private static void setStpEnalbe(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
+                              Bridge bridge) {
+        if (bridge.getStpEnableColumn() != null) {
+            Boolean stpEnable = bridge.getStpEnableColumn().getData();
+            if (stpEnable != null) {
+                ovsdbBridgeAugmentationBuilder.setStpEnable(stpEnable);
+            }
+        }
+    }
+
+    private void setOpenFlowNodeRef(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder, Bridge bridge) {
         Map<UUID, Controller> updatedControllerRows =
                 TyperUtils.extractRowsUpdated(Controller.class, getUpdates(), getDbSchema());
         LOG.debug("setOpenFlowNodeRef: updatedControllerRows: {}", updatedControllerRows);
@@ -326,15 +356,12 @@ public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand {
                 && controllerEntry.isIsConnected() != null && controllerEntry.isIsConnected()) {
                 String [] controllerTarget = controllerEntry.getTarget().getValue().split(":");
                 IpAddress bridgeControllerIpAddress = null;
-                PortNumber bridgeControllerPortNumber = null;
                 for (String targetElement : controllerTarget) {
                     if (InetAddresses.isInetAddress(targetElement)) {
-                        bridgeControllerIpAddress = new IpAddress(targetElement.toCharArray());
+                        bridgeControllerIpAddress = IpAddressBuilder.getDefaultInstance(targetElement);
                         continue;
                     }
-                    if (NumberUtils.isNumber(targetElement)) {
-                        bridgeControllerPortNumber = new PortNumber(
-                                Integer.valueOf(String.valueOf(targetElement)));
+                    if (NumberUtils.isCreatable(targetElement)) {
                         continue;
                     }
                 }
@@ -354,19 +381,34 @@ public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand {
                             }
                         }
                     }
-                } catch (Exception e) {
+                } catch (SocketException e) {
                     LOG.warn("Error getting local ip address", e);
                 }
             }
         }
     }
+
     private InstanceIdentifier<Node> getInstanceIdentifier(Bridge bridge) {
-        return SouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(),
+        return SouthboundMapper.createInstanceIdentifier(instanceIdentifierCodec, getOvsdbConnectionInstance(),
                 bridge);
     }
 
     private NodeId getNodeId(Bridge bridge) {
-        NodeKey nodeKey = getInstanceIdentifier(bridge).firstKeyOf(Node.class, NodeKey.class);
+        NodeKey nodeKey = getInstanceIdentifier(bridge).firstKeyOf(Node.class);
         return nodeKey.getNodeId();
     }
+
+    @Override
+    public void onSuccess() {
+        for (InstanceIdentifier<Node> updatedBridge : updatedBridges) {
+            LOG.debug("Updated bridge {} in operational datastore", updatedBridge);
+        }
+    }
+
+    @Override
+    public void onFailure(Throwable throwable) {
+        for (InstanceIdentifier<Node> updatedBridge : updatedBridges) {
+            LOG.error("Failed to update bridge {} in operational datastore", updatedBridge);
+        }
+    }
 }