ovsdb enable checkstyle on error
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / transactions / md / OpenVSwitchUpdateCommand.java
index 9fe2f7990151a3f162d6747df1579fb42c9318cc..bfc2a6f77014b3309db3a32fa2b71816c0ef5d66 100644 (file)
@@ -7,13 +7,14 @@
  */
 package org.opendaylight.ovsdb.southbound.transactions.md;
 
+import static org.opendaylight.ovsdb.southbound.SouthboundUtil.schemaMismatchLog;
+
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.NoSuchElementException;
 import java.util.Set;
-
 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
@@ -38,13 +39,14 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.re
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchExternalIdsKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigs;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigsBuilder;
-import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
-import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigsKey;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
 import org.slf4j.Logger;
@@ -52,8 +54,7 @@ import org.slf4j.LoggerFactory;
 
 public class OpenVSwitchUpdateCommand extends AbstractTransactionCommand {
 
-    private static final Logger LOG = LoggerFactory
-            .getLogger(OpenVSwitchUpdateCommand.class);
+    private static final Logger LOG = LoggerFactory.getLogger(OpenVSwitchUpdateCommand.class);
 
     public OpenVSwitchUpdateCommand(OvsdbConnectionInstance key, TableUpdates updates,
             DatabaseSchema dbSchema) {
@@ -71,7 +72,6 @@ public class OpenVSwitchUpdateCommand extends AbstractTransactionCommand {
 
         for (Entry<UUID, OpenVSwitch> entry : updatedOpenVSwitchRows.entrySet()) {
             OpenVSwitch openVSwitch = entry.getValue();
-            OpenVSwitch oldEntry = deletedOpenVSwitchRows.get(entry.getKey());
             final InstanceIdentifier<Node> nodePath = getInstanceIdentifier(openVSwitch);
 
             OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
@@ -79,8 +79,9 @@ public class OpenVSwitchUpdateCommand extends AbstractTransactionCommand {
             setVersion(ovsdbNodeBuilder, openVSwitch);
             setDataPathTypes(ovsdbNodeBuilder, openVSwitch);
             setInterfaceTypes(ovsdbNodeBuilder, openVSwitch);
+            OpenVSwitch oldEntry = deletedOpenVSwitchRows.get(entry.getKey());
             setExternalIds(transaction, ovsdbNodeBuilder, oldEntry, openVSwitch);
-            setOtherConfig(ovsdbNodeBuilder, openVSwitch);
+            setOtherConfig(transaction, ovsdbNodeBuilder, oldEntry, openVSwitch);
             ovsdbNodeBuilder.setConnectionInfo(getConnectionInfo());
 
             NodeBuilder nodeBuilder = new NodeBuilder();
@@ -92,24 +93,53 @@ public class OpenVSwitchUpdateCommand extends AbstractTransactionCommand {
         }
     }
 
-    private void setOtherConfig(OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
-            OpenVSwitch openVSwitch) {
-        Map<String, String> otherConfigs = openVSwitch.getOtherConfigColumn().getData();
-        if (otherConfigs != null && !otherConfigs.isEmpty()) {
-            Set<String> otherConfigKeys = otherConfigs.keySet();
-            List<OpenvswitchOtherConfigs> otherConfigsList = new ArrayList<OpenvswitchOtherConfigs>();
-            String otherConfigValue;
-            for (String otherConfigKey : otherConfigKeys) {
-                otherConfigValue = otherConfigs.get(otherConfigKey);
-                if (otherConfigKey != null && otherConfigValue != null) {
-                    otherConfigsList.add(new OpenvswitchOtherConfigsBuilder()
-                            .setOtherConfigKey(otherConfigKey)
-                            .setOtherConfigValue(otherConfigValue)
-                            .build());
-                }
+    private void setOtherConfig(ReadWriteTransaction transaction,
+            OvsdbNodeAugmentationBuilder ovsdbNodeBuilder, OpenVSwitch oldEntry, OpenVSwitch openVSwitch) {
+        Map<String, String> oldOtherConfigs = null;
+        Map<String, String> otherConfigs = null;
+
+        if (openVSwitch.getOtherConfigColumn() != null) {
+            otherConfigs = openVSwitch.getOtherConfigColumn().getData();
+        }
+        if (oldEntry != null && oldEntry.getOtherConfigColumn() != null) {
+            oldOtherConfigs = oldEntry.getOtherConfigColumn().getData();
+        }
+        if ((oldOtherConfigs == null) || oldOtherConfigs.isEmpty()) {
+            setNewOtherConfigs(ovsdbNodeBuilder, otherConfigs);
+        } else if (otherConfigs != null && !otherConfigs.isEmpty()) {
+            removeOldConfigs(transaction, oldOtherConfigs, openVSwitch);
+            setNewOtherConfigs(ovsdbNodeBuilder, otherConfigs);
+        }
+    }
+
+    private void removeOldConfigs(ReadWriteTransaction transaction, Map<String, String> oldOtherConfigs,
+            OpenVSwitch ovs) {
+        InstanceIdentifier<OvsdbNodeAugmentation> nodeAugmentataionIid = InstanceIdentifier
+                .create(NetworkTopology.class)
+                .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
+                .child(Node.class, new NodeKey(getNodeId(ovs))).augmentation(OvsdbNodeAugmentation.class);
+        Set<String> otherConfigKeys = oldOtherConfigs.keySet();
+        for (String otherConfigKey : otherConfigKeys) {
+            KeyedInstanceIdentifier<OpenvswitchOtherConfigs, OpenvswitchOtherConfigsKey> externalIid =
+                    nodeAugmentataionIid
+                    .child(OpenvswitchOtherConfigs.class, new OpenvswitchOtherConfigsKey(otherConfigKey));
+            transaction.delete(LogicalDatastoreType.OPERATIONAL, externalIid);
+        }
+    }
+
+    private void setNewOtherConfigs(OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
+            Map<String, String> otherConfigs) {
+        Set<String> otherConfigKeys = otherConfigs.keySet();
+        List<OpenvswitchOtherConfigs> otherConfigsList = new ArrayList<>();
+        String otherConfigValue;
+        for (String otherConfigKey : otherConfigKeys) {
+            otherConfigValue = otherConfigs.get(otherConfigKey);
+            if (otherConfigKey != null && otherConfigValue != null) {
+                otherConfigsList.add(new OpenvswitchOtherConfigsBuilder().setOtherConfigKey(otherConfigKey)
+                        .setOtherConfigValue(otherConfigValue).build());
             }
-            ovsdbNodeBuilder.setOpenvswitchOtherConfigs(otherConfigsList);
         }
+        ovsdbNodeBuilder.setOpenvswitchOtherConfigs(otherConfigsList);
     }
 
     private void setExternalIds(ReadWriteTransaction transaction,
@@ -149,7 +179,7 @@ public class OpenVSwitchUpdateCommand extends AbstractTransactionCommand {
     private void setNewExternalIds(OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
             Map<String, String> externalIds) {
         Set<String> externalIdKeys = externalIds.keySet();
-        List<OpenvswitchExternalIds> externalIdsList = new ArrayList<OpenvswitchExternalIds>();
+        List<OpenvswitchExternalIds> externalIdsList = new ArrayList<>();
         String externalIdValue;
         for (String externalIdKey : externalIdKeys) {
             externalIdValue = externalIds.get(externalIdKey);
@@ -166,17 +196,21 @@ public class OpenVSwitchUpdateCommand extends AbstractTransactionCommand {
             OpenVSwitch openVSwitch) {
         try {
             Set<String> iftypes = openVSwitch.getIfaceTypesColumn().getData();
-            List<InterfaceTypeEntry> ifEntryList = new ArrayList<InterfaceTypeEntry>();
+            List<InterfaceTypeEntry> ifEntryList = new ArrayList<>();
             for (String ifType : iftypes) {
-                InterfaceTypeEntry ifEntry = new InterfaceTypeEntryBuilder()
-                        .setInterfaceType(
-                                SouthboundMapper.createInterfaceType(ifType))
-                        .build();
-                ifEntryList.add(ifEntry);
+                if (SouthboundMapper.createInterfaceType(ifType) != null) {
+                    InterfaceTypeEntry ifEntry = new InterfaceTypeEntryBuilder()
+                            .setInterfaceType(
+                                    SouthboundMapper.createInterfaceType(ifType))
+                            .build();
+                    ifEntryList.add(ifEntry);
+                } else {
+                    LOG.warn("Interface type {} not present in model", ifType);
+                }
             }
             ovsdbNodeBuilder.setInterfaceTypeEntry(ifEntryList);
         } catch (SchemaVersionMismatchException e) {
-            LOG.debug("Iface types  not supported by this version of ovsdb",e);;
+            schemaMismatchLog("iface_types", SouthboundConstants.OPEN_V_SWITCH, e);
         }
     }
 
@@ -186,17 +220,21 @@ public class OpenVSwitchUpdateCommand extends AbstractTransactionCommand {
         try {
             Set<String> dptypes = openVSwitch.getDatapathTypesColumn()
                     .getData();
-            List<DatapathTypeEntry> dpEntryList = new ArrayList<DatapathTypeEntry>();
+            List<DatapathTypeEntry> dpEntryList = new ArrayList<>();
             for (String dpType : dptypes) {
-                DatapathTypeEntry dpEntry = new DatapathTypeEntryBuilder()
-                        .setDatapathType(
-                                SouthboundMapper.createDatapathType(dpType))
-                        .build();
-                dpEntryList.add(dpEntry);
+                if (SouthboundMapper.createDatapathType(dpType) != null) {
+                    DatapathTypeEntry dpEntry = new DatapathTypeEntryBuilder()
+                            .setDatapathType(
+                                    SouthboundMapper.createDatapathType(dpType))
+                            .build();
+                    dpEntryList.add(dpEntry);
+                } else {
+                    LOG.warn("Datapath type {} not present in model", dpType);
+                }
             }
             ovsdbNodeBuilder.setDatapathTypeEntry(dpEntryList);
         } catch (SchemaVersionMismatchException e) {
-            LOG.debug("Datapath types not supported by this version of ovsdb",e);
+            schemaMismatchLog("datapath_types", SouthboundConstants.OPEN_V_SWITCH, e);
         }
     }