ovsdb enable checkstyle on error
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / ovsdb / transact / AutoAttachUpdateCommand.java
index 593bbb99986e7907933a46385f3e6b0167b91093..9a6aab0167dcc0126db36eda66e063276a7cb0e5 100644 (file)
@@ -10,12 +10,15 @@ package org.opendaylight.ovsdb.southbound.ovsdb.transact;
 
 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
 
+import com.google.common.base.Optional;
+import com.google.common.collect.Sets;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.concurrent.ExecutionException;
-
+import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
@@ -46,9 +49,6 @@ 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.collect.Sets;
-
 public class AutoAttachUpdateCommand implements TransactCommand {
     private static final Logger LOG = LoggerFactory.getLogger(AutoAttachUpdateCommand.class);
 
@@ -58,21 +58,16 @@ public class AutoAttachUpdateCommand implements TransactCommand {
         execute(transaction, state, TransactUtils.extractCreatedOrUpdated(events, OvsdbNodeAugmentation.class));
     }
 
+    @Override
+    public void execute(TransactionBuilder transaction, BridgeOperationalState state,
+                        Collection<DataTreeModification<Node>> modifications) {
+        execute(transaction, state, TransactUtils.extractCreatedOrUpdated(modifications, OvsdbNodeAugmentation.class));
+    }
+
     private void execute(TransactionBuilder transaction, BridgeOperationalState state,
                          Map<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> createdOrUpdated) {
 
-        // FIXME: Fix if loop after ovs community supports external_ids column in AutoAttach Table
-        if (true) {
-            try {
-                throw new UnsupportedOperationException("CRUD operations not supported from ODL for auto_attach column for"
-                        + " this version of ovsdb schema due to missing external_ids column");
-            } catch (UnsupportedOperationException e) {
-                LOG.debug(e.getMessage());
-            }
-            return;
-        }
-
-        for (Entry<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> ovsdbNodeEntry
+        for (final Entry<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> ovsdbNodeEntry
                 : createdOrUpdated.entrySet()) {
             updateAutoAttach(transaction, state, ovsdbNodeEntry.getKey(), ovsdbNodeEntry.getValue());
         }
@@ -82,16 +77,24 @@ public class AutoAttachUpdateCommand implements TransactCommand {
             InstanceIdentifier<OvsdbNodeAugmentation> iid,
             OvsdbNodeAugmentation ovsdbNode) {
 
-        List<Autoattach> autoAttachList = ovsdbNode.getAutoattach();
-        if (state.getBridgeNode(iid).isPresent()) {
+        if (!state.getBridgeNode(iid).isPresent()) {
             return;
         }
-        OvsdbNodeAugmentation currentOvsdbNode = state.getBridgeNode(iid).get().getAugmentation(OvsdbNodeAugmentation.class);
-        List<Autoattach> currentAutoAttach = currentOvsdbNode.getAutoattach();
-
+        final List<Autoattach> autoAttachList = ovsdbNode.getAutoattach();
         if (autoAttachList != null) {
-            for (Autoattach autoAttach : autoAttachList) {
-                AutoAttach autoAttachWrapper = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), AutoAttach.class);
+            if (true) {
+                // FIXME: Remove if loop after ovs community supports external_ids column in AutoAttach Table
+                LOG.info("UNSUPPORTED FUNCTIONALITY: Auto Attach related CRUD operations are not supported for"
+                        + " this version of OVSDB schema due to missing external_ids column.");
+                return;
+            }
+
+            final OvsdbNodeAugmentation currentOvsdbNode =
+                    state.getBridgeNode(iid).get().getAugmentation(OvsdbNodeAugmentation.class);
+            final List<Autoattach> currentAutoAttach = currentOvsdbNode.getAutoattach();
+            for (final Autoattach autoAttach : autoAttachList) {
+                final AutoAttach autoAttachWrapper =
+                        TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), AutoAttach.class);
                 if (autoAttach.getSystemName() != null) {
                     autoAttachWrapper.setSystemName(autoAttach.getSystemName());
                 }
@@ -99,24 +102,26 @@ public class AutoAttachUpdateCommand implements TransactCommand {
                     autoAttachWrapper.setSystemDescription(autoAttach.getSystemDescription());
                 }
 
-                List<Mappings> mappingsList = autoAttach.getMappings();
+                final List<Mappings> mappingsList = autoAttach.getMappings();
                 if (mappingsList != null && !mappingsList.isEmpty()) {
-                    Map<Long, Long> newMappings = new HashMap<>();
-                    for (Mappings mappings : mappingsList) {
-                        Long mappingsValue = new Long(mappings.getMappingsValue().toString());
+                    final Map<Long, Long> newMappings = new HashMap<>();
+                    for (final Mappings mappings : mappingsList) {
+                        final Long mappingsValue = new Long(mappings.getMappingsValue().toString());
                         newMappings.put(mappings.getMappingsKey(), mappingsValue);
                     }
                     autoAttachWrapper.setMappings(newMappings);
                 }
 
-                List<AutoattachExternalIds> externalIds = autoAttach.getAutoattachExternalIds();
-                Map<String, String> externalIdsMap = new HashMap<>();
+                final List<AutoattachExternalIds> externalIds = autoAttach.getAutoattachExternalIds();
+                final Map<String, String> externalIdsMap = new HashMap<>();
                 if (externalIds != null) {
-                    for (AutoattachExternalIds externalId : externalIds) {
-                        externalIdsMap.put(externalId.getAutoattachExternalIdKey(), externalId.getAutoattachExternalIdValue());
+                    for (final AutoattachExternalIds externalId : externalIds) {
+                        externalIdsMap.put(externalId.getAutoattachExternalIdKey(),
+                                externalId.getAutoattachExternalIdValue());
                     }
                 }
-                externalIdsMap.put(SouthboundConstants.AUTOATTACH_ID_EXTERNAL_ID_KEY, autoAttach.getAutoattachId().getValue());
+                externalIdsMap.put(SouthboundConstants.AUTOATTACH_ID_EXTERNAL_ID_KEY,
+                        autoAttach.getAutoattachId().getValue());
                 // FIXME: To be uncommented when Open vSwitch supports external_ids column
 //                try {
 //                    autoAttachWrapper.setExternalIds(ImmutableMap.copyOf(externalIdsMap));
@@ -124,10 +129,10 @@ public class AutoAttachUpdateCommand implements TransactCommand {
 //                    LOG.warn("Incomplete AutoAttach external IDs");
 //                }
 
-                Uuid aaUuid = getAutoAttachUuid(currentAutoAttach, autoAttach.getAutoattachId());
+                final Uuid aaUuid = getAutoAttachUuid(currentAutoAttach, autoAttach.getAutoattachId());
                 if (aaUuid != null) {
-                    UUID uuid = new UUID(aaUuid.getValue());
-                    AutoAttach newAutoAttach = TyperUtils.getTypedRowWrapper(
+                    final UUID uuid = new UUID(aaUuid.getValue());
+                    final AutoAttach newAutoAttach = TyperUtils.getTypedRowWrapper(
                             transaction.getDatabaseSchema(), AutoAttach.class, null);
                     newAutoAttach.getUuidColumn().setData(uuid);
                     LOG.trace("Updating autoattach table entries {}", uuid);
@@ -135,22 +140,23 @@ public class AutoAttachUpdateCommand implements TransactCommand {
                             .where(newAutoAttach.getUuidColumn().getSchema().opEqual(uuid)).build());
                     transaction.add(op.comment("Updating AutoAttach table: " + uuid));
                 } else {
-                    Uri bridgeUri = autoAttach.getBridgeId();
-                    String namedUuid = SouthboundMapper.getRandomUUID();
-                    Bridge bridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
+                    final Uri bridgeUri = autoAttach.getBridgeId();
+                    final String namedUuid = SouthboundMapper.getRandomUuid();
+                    final Bridge bridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
                     transaction.add(op.insert(autoAttachWrapper).withId(namedUuid));
-                    OvsdbBridgeAugmentation ovsdbBridgeAugmentation = getBridge(iid, bridgeUri);
+                    final OvsdbBridgeAugmentation ovsdbBridgeAugmentation = getBridge(iid, bridgeUri);
                     if (ovsdbBridgeAugmentation != null) {
                         bridge.setName(ovsdbBridgeAugmentation.getBridgeName().getValue());
                         bridge.setAutoAttach(Sets.newHashSet(new UUID(namedUuid)));
-                        LOG.trace("Create Autoattach table {}, "
-                                + "and mutate the bridge {}", autoAttach.getAutoattachId(), getBridge(iid, bridgeUri).getBridgeName().getValue());
+                        LOG.trace("Create Autoattach table {}, and mutate the bridge {}",
+                                autoAttach.getAutoattachId(), getBridge(iid, bridgeUri).getBridgeName().getValue());
                         transaction.add(op.mutate(bridge)
                                 .addMutation(bridge.getAutoAttachColumn().getSchema(),
                                         Mutator.INSERT,bridge.getAutoAttachColumn().getData())
                                 .where(bridge.getNameColumn().getSchema()
                                         .opEqual(bridge.getNameColumn().getData())).build());
-                        transaction.add(op.comment("Bridge: Mutating " + ovsdbBridgeAugmentation.getBridgeName().getValue()
+                        transaction.add(
+                                op.comment("Bridge: Mutating " + ovsdbBridgeAugmentation.getBridgeName().getValue()
                                 + " to add autoattach column " + namedUuid));
                     }
                 }
@@ -160,7 +166,7 @@ public class AutoAttachUpdateCommand implements TransactCommand {
 
     private OvsdbBridgeAugmentation getBridge(InstanceIdentifier<OvsdbNodeAugmentation> key,
             Uri bridgeUri) {
-        InstanceIdentifier<OvsdbBridgeAugmentation> bridgeIid = InstanceIdentifier
+        final InstanceIdentifier<OvsdbBridgeAugmentation> bridgeIid = InstanceIdentifier
                 .create(NetworkTopology.class)
                 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
                 .child(Node.class, new NodeKey(new NodeId(bridgeUri)))
@@ -168,7 +174,8 @@ public class AutoAttachUpdateCommand implements TransactCommand {
 
         OvsdbBridgeAugmentation bridge = null;
         try (ReadOnlyTransaction transaction = SouthboundProvider.getDb().newReadOnlyTransaction()) {
-            Optional<OvsdbBridgeAugmentation> bridgeOptional = transaction.read(LogicalDatastoreType.OPERATIONAL, bridgeIid).get();
+            final Optional<OvsdbBridgeAugmentation> bridgeOptional =
+                    transaction.read(LogicalDatastoreType.OPERATIONAL, bridgeIid).get();
             if (bridgeOptional.isPresent()) {
                 bridge = bridgeOptional.get();
             }
@@ -180,7 +187,7 @@ public class AutoAttachUpdateCommand implements TransactCommand {
 
     private Uuid getAutoAttachUuid(List<Autoattach> currentAutoAttach, Uri autoattachId) {
         if (currentAutoAttach != null && !currentAutoAttach.isEmpty()) {
-            for (Autoattach autoAttach : currentAutoAttach) {
+            for (final Autoattach autoAttach : currentAutoAttach) {
                 if (autoAttach.getAutoattachId().equals(autoattachId)) {
                     return autoAttach.getAutoattachUuid();
                 }