Use a utility function for key-value to map conversions
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / ovsdb / transact / TerminationPointCreateCommand.java
index f937bc9eaf400a52f8686d1a53f4b2bb193db04e..d6eba9c642666a1c094562adee0399984dbd106e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2015, 2016 Brocade Communications 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,
@@ -22,6 +22,7 @@ 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;
 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
+import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
 import org.opendaylight.ovsdb.lib.notation.Mutator;
 import org.opendaylight.ovsdb.lib.notation.UUID;
 import org.opendaylight.ovsdb.lib.operations.Mutate;
@@ -33,11 +34,13 @@ import org.opendaylight.ovsdb.schema.openvswitch.Port;
 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
 import org.opendaylight.ovsdb.southbound.SouthboundProvider;
+import org.opendaylight.ovsdb.utils.yang.YangUtils;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.InterfaceTypeBase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbPortInterfaceAttributes.VlanMode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceExternalIds;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceLldp;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceOtherConfigs;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Options;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.PortExternalIds;
@@ -51,7 +54,6 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.common.base.Optional;
-import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Sets;
 import com.google.common.util.concurrent.CheckedFuture;
 
@@ -77,7 +79,7 @@ public class TerminationPointCreateCommand extends AbstractTransactCommand {
                         getOperationalState().getBridgeTerminationPoint(terminationPointIid);
                 if (!terminationPointOptional.isPresent()) {
                     // Configure interface
-                    String interfaceUuid = "Interface_" + SouthboundMapper.getRandomUUID();;
+                    String interfaceUuid = "Interface_" + SouthboundMapper.getRandomUUID();
                     Interface ovsInterface =
                             TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Interface.class);
                     createInterface(terminationPoint, ovsInterface);
@@ -121,6 +123,7 @@ public class TerminationPointCreateCommand extends AbstractTransactCommand {
         createInterfaceOptions(terminationPoint, ovsInterface);
         createInterfaceOtherConfig(terminationPoint, ovsInterface);
         createInterfaceExternalIds(terminationPoint, ovsInterface);
+        createInterfaceLldp(terminationPoint, ovsInterface);
     }
 
     private void createInterfaceType(final OvsdbTerminationPointAugmentation terminationPoint,
@@ -171,14 +174,11 @@ public class TerminationPointCreateCommand extends AbstractTransactCommand {
 
         //Configure optional input
         if (terminationPoint.getOptions() != null) {
-            HashMap<String, String> optionsMap = new HashMap<String, String>();
-            for (Options option : terminationPoint.getOptions()) {
-                optionsMap.put(option.getOption(), option.getValue());
-            }
             try {
-                ovsInterface.setOptions(ImmutableMap.copyOf(optionsMap));
+                ovsInterface.setOptions(YangUtils.convertYangKeyValueListToMap(terminationPoint.getOptions(),
+                        Options::getOption, Options::getValue));
             } catch (NullPointerException e) {
-                LOG.warn("Incomplete OVSDB interface options");
+                LOG.warn("Incomplete OVSDB interface options", e);
             }
         }
     }
@@ -190,14 +190,11 @@ public class TerminationPointCreateCommand extends AbstractTransactCommand {
         List<InterfaceExternalIds> interfaceExternalIds =
                 terminationPoint.getInterfaceExternalIds();
         if (interfaceExternalIds != null && !interfaceExternalIds.isEmpty()) {
-            HashMap<String, String> externalIdsMap = new HashMap<String, String>();
-            for (InterfaceExternalIds externalId: interfaceExternalIds) {
-                externalIdsMap.put(externalId.getExternalIdKey(), externalId.getExternalIdValue());
-            }
             try {
-                ovsInterface.setExternalIds(ImmutableMap.copyOf(externalIdsMap));
+                ovsInterface.setExternalIds(YangUtils.convertYangKeyValueListToMap(interfaceExternalIds,
+                        InterfaceExternalIds::getExternalIdKey, InterfaceExternalIds::getExternalIdValue));
             } catch (NullPointerException e) {
-                LOG.warn("Incomplete OVSDB interface external_ids");
+                LOG.warn("Incomplete OVSDB interface external_ids", e);
             }
         }
     }
@@ -209,7 +206,7 @@ public class TerminationPointCreateCommand extends AbstractTransactCommand {
         List<InterfaceOtherConfigs> interfaceOtherConfigs =
                 terminationPoint.getInterfaceOtherConfigs();
         if (interfaceOtherConfigs != null && !interfaceOtherConfigs.isEmpty()) {
-            HashMap<String, String> otherConfigsMap = new HashMap<String, String>();
+            Map<String, String> otherConfigsMap = new HashMap<>();
             for (InterfaceOtherConfigs interfaceOtherConfig : interfaceOtherConfigs) {
                 otherConfigsMap.put(interfaceOtherConfig.getOtherConfigKey(),
                         interfaceOtherConfig.getOtherConfigValue());
@@ -222,20 +219,37 @@ public class TerminationPointCreateCommand extends AbstractTransactCommand {
         }
     }
 
+    private void createInterfaceLldp(
+            final OvsdbTerminationPointAugmentation terminationPoint,
+            final Interface ovsInterface) {
+
+        try {
+            List<InterfaceLldp> interfaceLldpList =
+                    terminationPoint.getInterfaceLldp();
+            if (interfaceLldpList != null && !interfaceLldpList.isEmpty()) {
+                try {
+                    ovsInterface.setLldp(YangUtils.convertYangKeyValueListToMap(interfaceLldpList,
+                            InterfaceLldp::getLldpKey, InterfaceLldp::getLldpValue));
+                } catch (NullPointerException e) {
+                    LOG.warn("Incomplete OVSDB interface lldp", e);
+                }
+            }
+        } catch (SchemaVersionMismatchException e) {
+            LOG.debug("lldp column for Interface Table unsupported for this version of ovsdb schema", e);
+        }
+    }
+
     private void createPortExternalIds(
             final OvsdbTerminationPointAugmentation terminationPoint,
             final Port port) {
 
         List<PortExternalIds> portExternalIds = terminationPoint.getPortExternalIds();
         if (portExternalIds != null && !portExternalIds.isEmpty()) {
-            HashMap<String, String> externalIdsMap = new HashMap<String, String>();
-            for (PortExternalIds externalId: portExternalIds) {
-                externalIdsMap.put(externalId.getExternalIdKey(), externalId.getExternalIdValue());
-            }
             try {
-                port.setExternalIds(ImmutableMap.copyOf(externalIdsMap));
+                port.setExternalIds(YangUtils.convertYangKeyValueListToMap(portExternalIds,
+                        PortExternalIds::getExternalIdKey, PortExternalIds::getExternalIdValue));
             } catch (NullPointerException e) {
-                LOG.warn("Incomplete OVSDB port external_ids");
+                LOG.warn("Incomplete OVSDB port external_ids", e);
             }
         }
     }
@@ -245,7 +259,7 @@ public class TerminationPointCreateCommand extends AbstractTransactCommand {
             final Port port) {
 
         if (terminationPoint.getVlanTag() != null) {
-            Set<Long> vlanTag = new HashSet<Long>();
+            Set<Long> vlanTag = new HashSet<>();
             vlanTag.add(terminationPoint.getVlanTag().getValue().longValue());
             port.setTag(vlanTag);
         }
@@ -256,7 +270,7 @@ public class TerminationPointCreateCommand extends AbstractTransactCommand {
             final Port port) {
 
         if (terminationPoint.getTrunks() != null && terminationPoint.getTrunks().size() > 0) {
-            Set<Long> portTrunks = new HashSet<Long>();
+            Set<Long> portTrunks = new HashSet<>();
             List<Trunks> modelTrunks = terminationPoint.getTrunks();
             for (Trunks trunk: modelTrunks) {
                 if (trunk.getTrunk() != null) {
@@ -271,7 +285,7 @@ public class TerminationPointCreateCommand extends AbstractTransactCommand {
             final OvsdbTerminationPointAugmentation terminationPoint,
             final Port port) {
         if (terminationPoint.getVlanMode() != null) {
-            Set<String> portVlanMode = new HashSet<String>();
+            Set<String> portVlanMode = new HashSet<>();
             VlanMode modelVlanMode = terminationPoint.getVlanMode();
             portVlanMode.add(SouthboundConstants.VLANMODES.values()[modelVlanMode.getIntValue() - 1].getMode());
             port.setVlanMode(portVlanMode);
@@ -284,13 +298,9 @@ public class TerminationPointCreateCommand extends AbstractTransactCommand {
         List<PortOtherConfigs> portOtherConfigs =
                 terminationPoint.getPortOtherConfigs();
         if (portOtherConfigs != null && !portOtherConfigs.isEmpty()) {
-            HashMap<String, String> otherConfigsMap = new HashMap<String, String>();
-            for (PortOtherConfigs portOtherConfig : portOtherConfigs) {
-                otherConfigsMap.put(portOtherConfig.getOtherConfigKey(),
-                        portOtherConfig.getOtherConfigValue());
-            }
             try {
-                ovsPort.setOtherConfig(ImmutableMap.copyOf(otherConfigsMap));
+                ovsPort.setOtherConfig(YangUtils.convertYangKeyValueListToMap(portOtherConfigs,
+                        PortOtherConfigs::getOtherConfigKey, PortOtherConfigs::getOtherConfigValue));
             } catch (NullPointerException e) {
                 LOG.warn("Incomplete OVSDB port other_config", e);
             }
@@ -326,7 +336,7 @@ public class TerminationPointCreateCommand extends AbstractTransactCommand {
     public static void stampInstanceIdentifier(TransactionBuilder transaction,InstanceIdentifier<TerminationPoint> iid,
             String interfaceName) {
         Port port = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Port.class);
-        port.setName(interfaceName);;
+        port.setName(interfaceName);
         port.setExternalIds(Collections.<String,String>emptyMap());
         Mutate mutate = TransactUtils.stampInstanceIdentifierMutation(transaction,
                 iid,