Create Physical Port By Listening on DS changes 19/30319/8
authorPeng Zhang <pzhang@ctbri.com.cn>
Thu, 26 Nov 2015 14:14:22 +0000 (22:14 +0800)
committerPeng Zhang <pzhang@ctbri.com.cn>
Thu, 3 Dec 2015 12:33:43 +0000 (20:33 +0800)
PatchSet 2&3:
share code and change it according comments

PatchSet 4:
rebase on 'Create Physical Switch'

PathchSet 5:
1.rebase on 'Yang changes and cleanup'
2.finish creating physical port
3.rewrite HwvtepOperationalState

PatchSet 6:
change code according comments

PatchSet 7:
rebase on master branch

PathchSet 8:
fix a nullpoint bug

Change-Id: I425336aea3b055ee4d7e75bd26490e194340e3af
Signed-off-by: Peng Zhang <pzhang@ctbri.com.cn>
hwvtepsouthbound/hwvtepsouthbound-impl/src/main/java/org/opendaylight/ovsdb/hwvtepsouthbound/HwvtepDataChangeListener.java
hwvtepsouthbound/hwvtepsouthbound-impl/src/main/java/org/opendaylight/ovsdb/hwvtepsouthbound/HwvtepSouthboundMapper.java
hwvtepsouthbound/hwvtepsouthbound-impl/src/main/java/org/opendaylight/ovsdb/hwvtepsouthbound/HwvtepSouthboundUtil.java
hwvtepsouthbound/hwvtepsouthbound-impl/src/main/java/org/opendaylight/ovsdb/hwvtepsouthbound/transact/HwvtepOperationalState.java
hwvtepsouthbound/hwvtepsouthbound-impl/src/main/java/org/opendaylight/ovsdb/hwvtepsouthbound/transact/LogicalSwitchUpdateCommand.java
hwvtepsouthbound/hwvtepsouthbound-impl/src/main/java/org/opendaylight/ovsdb/hwvtepsouthbound/transact/PhysicalPortUpdateCommand.java
resources/commons/Ovsdb-HwvtepSouthbound-Collection.json.postman_collection

index 141b280822355c326e7d494d6c40c3109e87bf52..4d3daf5d1954c4eb8dec6062e468adbf8757fa32 100644 (file)
@@ -294,4 +294,4 @@ public class HwvtepDataChangeListener implements DataTreeChangeListener<Node>, A
         LOG.trace("Connection Change Map: {}", result);
         return result;
     }
-}
\ No newline at end of file
+}
index 8127029b1a1475a630f0dadb4bb49ed773df2548..9ea64e8876142b98dcc63252ef19b8b5eb7d3d78 100644 (file)
@@ -189,4 +189,7 @@ public class HwvtepSouthboundMapper {
         return tpKey;
     }
 
+    public static String getRandomUUID() {
+        return "Random_" + java.util.UUID.randomUUID().toString().replace("-", "");
+    }
 }
index fdef418d1f9fe0d92e371d26ac5ec0852e82d4ce..84388736089e1193ae85d7280f2764f469bbd4a6 100644 (file)
@@ -102,7 +102,7 @@ public class HwvtepSouthboundUtil {
         return result;
     }
 
-    private static Optional<HwvtepGlobalAugmentation> getManagingNode(DataBroker db, HwvtepGlobalRef ref) {
+    public static Optional<HwvtepGlobalAugmentation> getManagingNode(DataBroker db, HwvtepGlobalRef ref) {
         try {
             ReadOnlyTransaction transaction = db.newReadOnlyTransaction();
             @SuppressWarnings("unchecked")
index bf5ca1bca0fdc2e978f2fe4cad99d4c117ce86ab..b09a5d9a076320bc32c151ad873a0d20bfa5357e 100644 (file)
@@ -13,74 +13,86 @@ 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.DataBroker;
 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.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
+import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
+import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundUtil;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.EncapsulationTypeBase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentation;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentation;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitchesKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.Switches;
 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.node.TerminationPoint;
-import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
 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.util.concurrent.CheckedFuture;
+import com.google.common.base.Preconditions;
 
 public class HwvtepOperationalState {
     private static final Logger LOG = LoggerFactory.getLogger(HwvtepOperationalState.class);
     private Map<InstanceIdentifier<Node>, Node> operationalNodes = new HashMap<>();
 
     public HwvtepOperationalState(DataBroker db, Collection<DataTreeModification<Node>> changes) {
-        ReadOnlyTransaction transaction = db.newReadOnlyTransaction();
         Map<InstanceIdentifier<Node>, Node> nodeCreateOrUpdate =
             TransactUtils.extractCreatedOrUpdatedOrRemoved(changes, Node.class);
         if (nodeCreateOrUpdate != null) {
+            final ReadWriteTransaction transaction = db.newReadWriteTransaction();
             for (Entry<InstanceIdentifier<Node>, Node> entry: nodeCreateOrUpdate.entrySet()) {
-                CheckedFuture<Optional<Node>, ReadFailedException> nodeFuture =
-                        transaction.read(LogicalDatastoreType.OPERATIONAL, entry.getKey());
-                try {
-                    Optional<Node> nodeOptional = nodeFuture.get();
-                    if (nodeOptional.isPresent()) {
-                        operationalNodes.put(entry.getKey(), nodeOptional.get());
+                Optional<Node> readNode = HwvtepSouthboundUtil.readNode(transaction, entry.getKey());
+                //add related globalNode or physicalSwitchNode to operationalNodes map
+                //for example, when creating physical port, logical switch is needed
+                //but logical switch is in HwvtepGlobalAugmentation rather than PhysicalSwitchAugmentation
+                if (readNode.isPresent()) {
+                    operationalNodes.put(entry.getKey(), readNode.get());
+                    HwvtepGlobalAugmentation hgAugmentation = readNode.get().getAugmentation(HwvtepGlobalAugmentation.class);
+                    PhysicalSwitchAugmentation psAugmentation = readNode.get().getAugmentation(PhysicalSwitchAugmentation.class);
+                    if (hgAugmentation != null) {
+                        for (Switches pswitch : hgAugmentation.getSwitches()) {
+                            @SuppressWarnings("unchecked")
+                            InstanceIdentifier<Node> psNodeIid = (InstanceIdentifier<Node>) pswitch.getSwitchRef().getValue();
+                            Optional<Node> psNode = HwvtepSouthboundUtil.readNode(transaction, psNodeIid);
+                            if (psNode.isPresent()) {
+                                operationalNodes.put(psNodeIid, psNode.get());
+                            }
+                        }
+                    }
+                    if (psAugmentation != null) {
+                        @SuppressWarnings("unchecked")
+                        InstanceIdentifier<Node> hgNodeIid = (InstanceIdentifier<Node>) psAugmentation.getManagedBy().getValue();
+                        Optional<Node> hgNode = HwvtepSouthboundUtil.readNode(transaction, hgNodeIid);
+                        if (hgNode.isPresent()) {
+                            operationalNodes.put(hgNodeIid, hgNode.get());
+                        }
                     }
-                } catch (InterruptedException | ExecutionException e) {
-                    LOG.warn("Error reading from datastore",e);
                 }
             }
         }
-        transaction.close();
     }
 
-    public Optional<Node> getGlobalNode(InstanceIdentifier<?> iid) {
+    private Optional<Node> getGlobalNode(InstanceIdentifier<?> iid) {
         InstanceIdentifier<Node> nodeIid = iid.firstIdentifierOf(Node.class);
         return Optional.fromNullable(operationalNodes.get(nodeIid));
     }
 
-    public Optional<LogicalSwitches> getLogicalSwitches(InstanceIdentifier<?> iid, LogicalSwitchesKey lswitchKey) {
+    public Optional<HwvtepGlobalAugmentation> getHwvtepGlobalAugmentation(InstanceIdentifier<?> iid) {
+        Preconditions.checkNotNull(iid);
         Optional<Node> nodeOptional = getGlobalNode(iid);
         if (nodeOptional.isPresent()) {
-            List<LogicalSwitches> lswitchList = nodeOptional.get().getAugmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
-            if (lswitchList != null) {
-                for (LogicalSwitches lswitch: lswitchList) {
-                    if (lswitch.getKey().equals(lswitchKey)) {
-                        return Optional.fromNullable(lswitch);
-                    }
-                }
-            }
+            return Optional.fromNullable(nodeOptional.get().getAugmentation(HwvtepGlobalAugmentation.class));
         }
         return Optional.absent();
     }
 
     public Optional<PhysicalSwitchAugmentation> getPhysicalSwitchAugmentation(InstanceIdentifier<?> iid) {
+        Preconditions.checkNotNull(iid);
         Optional<Node> nodeOptional = getGlobalNode(iid);
         if (nodeOptional.isPresent()) {
             return Optional.fromNullable(nodeOptional.get().getAugmentation(PhysicalSwitchAugmentation.class));
@@ -88,16 +100,28 @@ public class HwvtepOperationalState {
         return Optional.absent();
     }
 
-    public Optional<TerminationPoint> getHwvtepTerminationPoint(InstanceIdentifier<?> iid) {
-        if (iid != null) {
-            Optional<Node> nodeOptional = getGlobalNode(iid);
-            if (nodeOptional.isPresent() && nodeOptional.get().getTerminationPoint() != null) {
-                TerminationPointKey key = iid.firstKeyOf(TerminationPoint.class, TerminationPointKey.class);
-                if (key != null) {
-                    for (TerminationPoint tp:nodeOptional.get().getTerminationPoint()) {
-                        if (tp.getKey().equals(key)) {
-                            return Optional.of(tp);
-                        }
+    public Optional<List<TerminationPoint>> getTerminationPointList(InstanceIdentifier<?> iid) {
+        Preconditions.checkNotNull(iid);
+        Optional<Node> nodeOptional = getGlobalNode(iid);
+        if (nodeOptional.isPresent() && nodeOptional.get().getTerminationPoint() != null) {
+            return Optional.fromNullable(nodeOptional.get().getTerminationPoint());
+        }
+        return Optional.absent();
+    }
+
+    public Optional<LogicalSwitches> getLogicalSwitches(InstanceIdentifier<?> iid, LogicalSwitchesKey logicalSwitchesKey) {
+        Preconditions.checkNotNull(iid);
+        Optional<HwvtepGlobalAugmentation> nodeOptional = getHwvtepGlobalAugmentation(iid);
+        if (nodeOptional.isPresent()) {
+            HwvtepGlobalAugmentation hgAugmentation = nodeOptional.get();
+            List<LogicalSwitches> lswitchList = null;
+            if (hgAugmentation != null) {
+                lswitchList = hgAugmentation.getLogicalSwitches();
+            }
+            if (lswitchList != null) {
+                for (LogicalSwitches lswitch: lswitchList) {
+                    if (lswitch.getKey().equals(logicalSwitchesKey)) {
+                        return Optional.fromNullable(lswitch);
                     }
                 }
             }
@@ -105,18 +129,35 @@ public class HwvtepOperationalState {
         return Optional.absent();
     }
 
-    public Optional<HwvtepPhysicalLocatorAugmentation> getPhysicalLocatorAugmentation(InstanceIdentifier<?> iid) {
-        Optional<TerminationPoint> nodeOptional = getHwvtepTerminationPoint(iid);
+    public Optional<HwvtepPhysicalPortAugmentation> getPhysicalPortAugmentation(InstanceIdentifier<?> iid,
+            HwvtepNodeName hwvtepNodeName) {
+        Preconditions.checkNotNull(iid);
+        Optional<List<TerminationPoint>> nodeOptional = getTerminationPointList(iid);
         if (nodeOptional.isPresent()) {
-            return Optional.fromNullable(nodeOptional.get().getAugmentation(HwvtepPhysicalLocatorAugmentation.class));
+            List<TerminationPoint> tpList = nodeOptional.get();
+            for (TerminationPoint tp : tpList) {
+                HwvtepPhysicalPortAugmentation hppAugmentation = tp.getAugmentation(HwvtepPhysicalPortAugmentation.class);
+                if (hppAugmentation.getHwvtepNodeName().equals(hwvtepNodeName)) {
+                    return Optional.fromNullable(hppAugmentation);
+                }
+            }
         }
         return Optional.absent();
     }
 
-    public Optional<HwvtepPhysicalPortAugmentation> getPhysycalPortAugmentation(InstanceIdentifier<?> iid) {
-        Optional<TerminationPoint> tpOptional = getHwvtepTerminationPoint(iid);
-        if (tpOptional.isPresent()) {
-            return Optional.fromNullable(tpOptional.get().getAugmentation(HwvtepPhysicalPortAugmentation.class));
+    public Optional<HwvtepPhysicalLocatorAugmentation> getPhysicalLocatorAugmentation(InstanceIdentifier<?> iid,
+            IpAddress dstIp, Class<? extends EncapsulationTypeBase> encapType) {
+        Preconditions.checkNotNull(iid);
+        Optional<List<TerminationPoint>> nodeOptional = getTerminationPointList(iid);
+        if (nodeOptional.isPresent()) {
+            List<TerminationPoint> tpList = nodeOptional.get();
+            for (TerminationPoint tp : tpList) {
+                HwvtepPhysicalLocatorAugmentation hppAugmentation = tp.getAugmentation(HwvtepPhysicalLocatorAugmentation.class);
+                if (hppAugmentation.getDstIp().equals(dstIp)
+                        && hppAugmentation.getEncapsulationType().equals(encapType)) {
+                    return Optional.fromNullable(hppAugmentation);
+                }
+            }
         }
         return Optional.absent();
     }
index e8498c7f7ea876f40b9452b149c21527e44dd10a..2ae002d6ce472a3b4997ab7fe2e3835b0ebe3b3a 100644 (file)
@@ -118,7 +118,10 @@ public class LogicalSwitchUpdateCommand extends AbstractTransactCommand {
                 final DataObjectModification<Node> mod = change.getRootNode();
                 Node created = TransactUtils.getCreated(mod);
                 if (created != null) {
-                    List<LogicalSwitches> lswitchListUpdated = created.getAugmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
+                    List<LogicalSwitches> lswitchListUpdated = null;
+                    if (created.getAugmentation(HwvtepGlobalAugmentation.class) != null) {
+                        lswitchListUpdated = created.getAugmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
+                    }
                     if (lswitchListUpdated != null) {
                         result.put(key, lswitchListUpdated);
                     }
@@ -139,8 +142,14 @@ public class LogicalSwitchUpdateCommand extends AbstractTransactCommand {
                 Node updated = TransactUtils.getUpdated(mod);
                 Node before = mod.getDataBefore();
                 if (updated != null && before != null) {
-                    List<LogicalSwitches> lswitchListUpdated = updated.getAugmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
-                    List<LogicalSwitches> lswitchListBefore = before.getAugmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
+                    List<LogicalSwitches> lswitchListUpdated = null;
+                    List<LogicalSwitches> lswitchListBefore = null;
+                    if (updated.getAugmentation(HwvtepGlobalAugmentation.class) != null) {
+                        lswitchListUpdated = updated.getAugmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
+                    }
+                    if (before.getAugmentation(HwvtepGlobalAugmentation.class) != null) {
+                        lswitchListBefore = before.getAugmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
+                    }
                     if (lswitchListUpdated != null) {
                         if (lswitchListBefore != null) {
                             lswitchListUpdated.removeAll(lswitchListBefore);
index caef6cb8ef8d791f199829d3f6c62cb23dc05144..aff805ccae849c2c26325222f586cd4ebd578057 100644 (file)
@@ -8,14 +8,40 @@
 
 package org.opendaylight.ovsdb.hwvtepsouthbound.transact;
 
+import static org.opendaylight.ovsdb.lib.operations.Operations.op;
+
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
 
+import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
+import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundMapper;
+import org.opendaylight.ovsdb.lib.notation.Mutator;
+import org.opendaylight.ovsdb.lib.notation.UUID;
 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
+import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
+import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalPort;
+import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalSwitch;
+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.hwvtep.rev150901.HwvtepNodeName;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentation;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitchesKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindings;
 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.node.TerminationPoint;
+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 PhysicalPortUpdateCommand extends AbstractTransactCommand {
     private static final Logger LOG = LoggerFactory.getLogger(PhysicalPortUpdateCommand.class);
 
@@ -26,7 +52,197 @@ public class PhysicalPortUpdateCommand extends AbstractTransactCommand {
 
     @Override
     public void execute(TransactionBuilder transaction) {
-        //TODO
+        Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> createds =
+                extractCreated(getChanges(),HwvtepPhysicalPortAugmentation.class);
+        Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> createdPhysicalSwitches =
+                extractCreatedPhyscialSwitch(getChanges(),PhysicalSwitchAugmentation.class);
+        if (!createds.isEmpty()) {
+            for (Entry<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> created:
+                createds.entrySet()) {
+                updatePhysicalPort(transaction,  created.getKey(), created.getValue(), createdPhysicalSwitches);
+            }
+        }
+        Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> updateds =
+                extractUpdated(getChanges(),HwvtepPhysicalPortAugmentation.class);
+        if (!updateds.isEmpty()) {
+            for (Entry<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> updated:
+                updateds.entrySet()) {
+                updatePhysicalPort(transaction,  updated.getKey(), updated.getValue(), createdPhysicalSwitches);
+            }
+        }
     }
 
-}
+    private void updatePhysicalPort(TransactionBuilder transaction,
+            InstanceIdentifier<Node> psNodeiid,
+            List<HwvtepPhysicalPortAugmentation> listPort,
+            Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> createdPhysicalSwitches ) {
+        //Get physical switch which the port belong to: in operation DS or new created
+        PhysicalSwitchAugmentation physicalSwitchBelong = getPhysicalSwitchBelong(psNodeiid, createdPhysicalSwitches);
+        for (HwvtepPhysicalPortAugmentation port : listPort) {
+            LOG.debug("Creating a physical port named: {}", port.getHwvtepNodeName().getValue());
+            Optional<HwvtepPhysicalPortAugmentation> operationalPhysicalPortOptional =
+                    getOperationalState().getPhysicalPortAugmentation(psNodeiid, port.getHwvtepNodeName());
+            PhysicalPort physicalPort = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), PhysicalPort.class);
+            //get managing global node of physicalSwitchBelong
+            InstanceIdentifier<?> globalNodeIid = physicalSwitchBelong.getManagedBy().getValue();
+            setVlanBindings(globalNodeIid, physicalPort, port);
+            setDescription(physicalPort, port);
+            if (!operationalPhysicalPortOptional.isPresent()) {
+                //create a physical port
+                setName(physicalPort, port, operationalPhysicalPortOptional);
+                String portUuid = "PhysicalPort_" + HwvtepSouthboundMapper.getRandomUUID();
+                transaction.add(op.insert(physicalPort).withId(portUuid));
+                //update physical switch table
+                PhysicalSwitch physicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), PhysicalSwitch.class);
+                physicalSwitch.setName(physicalSwitchBelong.getHwvtepNodeName().getValue());
+                physicalSwitch.setPorts(Sets.newHashSet(new UUID(portUuid)));
+                LOG.info("execute: physical switch: {}", physicalSwitch);
+                transaction.add(op.mutate(physicalSwitch)
+                        .addMutation(physicalSwitch.getPortsColumn().getSchema(), Mutator.INSERT,
+                                physicalSwitch.getPortsColumn().getData())
+                        .where(physicalSwitch.getNameColumn().getSchema().opEqual(physicalSwitch.getNameColumn().getData()))
+                        .build());
+            } else {
+                //updated physical port only
+                HwvtepPhysicalPortAugmentation updatedPhysicalPort = operationalPhysicalPortOptional.get();
+                String existingPhysicalPortName = updatedPhysicalPort.getHwvtepNodeName().getValue();
+                PhysicalPort extraPhyscialPort =
+                        TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), PhysicalPort.class);
+                extraPhyscialPort.setName("");
+                transaction.add(op.update(physicalPort)
+                        .where(extraPhyscialPort.getNameColumn().getSchema().opEqual(existingPhysicalPortName))
+                        .build());
+            }
+        }
+    }
+
+    private PhysicalSwitchAugmentation getPhysicalSwitchBelong(InstanceIdentifier<Node> psNodeiid,
+            Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> createdPhysicalSwitches) {
+        Optional<PhysicalSwitchAugmentation> physicalSwitchOptional =
+                    getOperationalState().getPhysicalSwitchAugmentation(psNodeiid);
+        PhysicalSwitchAugmentation physicalSwitchAugmentation = null;
+        if (physicalSwitchOptional.isPresent()) {
+            physicalSwitchAugmentation = physicalSwitchOptional.get();
+        } else {
+            physicalSwitchAugmentation = createdPhysicalSwitches.get(psNodeiid);
+        }
+        return physicalSwitchAugmentation;
+    }
+
+    private void setName(PhysicalPort physicalPort, HwvtepPhysicalPortAugmentation inputPhysicalPort,
+            Optional<HwvtepPhysicalPortAugmentation> operationalLogicalSwitchOptional) {
+        if (inputPhysicalPort.getHwvtepNodeName() != null) {
+            physicalPort.setName(inputPhysicalPort.getHwvtepNodeName().getValue());
+        } else if (operationalLogicalSwitchOptional.isPresent()
+                && operationalLogicalSwitchOptional.get().getHwvtepNodeName() != null) {
+            physicalPort.setName(operationalLogicalSwitchOptional.get().getHwvtepNodeName().getValue());
+        }
+    }
+
+    private void setDescription(PhysicalPort physicalPort, HwvtepPhysicalPortAugmentation inputPhysicalPort) {
+        if (inputPhysicalPort.getHwvtepNodeDescription() != null) {
+            physicalPort.setDescription(inputPhysicalPort.getHwvtepNodeDescription().toString());
+        }
+    }
+
+    private void setVlanBindings(InstanceIdentifier<?> globalNodeIid, PhysicalPort physicalPort,
+            HwvtepPhysicalPortAugmentation inputPhysicalPort) {
+        if (inputPhysicalPort.getVlanBindings() != null) {
+            //get UUID by LogicalSwitchRef
+            Map<Long, UUID> bindingMap = new HashMap<Long, UUID>();
+            for (VlanBindings vlanBinding: inputPhysicalPort.getVlanBindings()) {
+                HwvtepNodeName lswitchName = new HwvtepNodeName(vlanBinding.getLogicalSwitchRef().getValue());
+                Optional<LogicalSwitches> operationalSwitchOptional =
+                        getOperationalState().getLogicalSwitches(globalNodeIid, new LogicalSwitchesKey(lswitchName));
+                if (operationalSwitchOptional.isPresent()) {
+                    Uuid logicalSwitchUuid = operationalSwitchOptional.get().getLogicalSwitchUuid();
+                    bindingMap.put(vlanBinding.getVlanIdKey().getValue().longValue(), new UUID(logicalSwitchUuid.getValue()));
+                }
+            }
+            physicalPort.setVlanBindings(bindingMap);
+        }
+    }
+
+    private Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> extractCreated(
+            Collection<DataTreeModification<Node>> changes, Class<HwvtepPhysicalPortAugmentation> class1) {
+        Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> result
+            = new HashMap<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>>();
+        if (changes != null && !changes.isEmpty()) {
+            for (DataTreeModification<Node> change : changes) {
+                final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
+                final DataObjectModification<Node> mod = change.getRootNode();
+                Node created = TransactUtils.getCreated(mod);
+                if (created != null) {
+                    List<HwvtepPhysicalPortAugmentation> portListUpdated = new ArrayList<HwvtepPhysicalPortAugmentation>();
+                    if (created.getTerminationPoint() != null) {
+                        for (TerminationPoint tp : created.getTerminationPoint()) {
+                            HwvtepPhysicalPortAugmentation hppAugmentation = tp.getAugmentation(HwvtepPhysicalPortAugmentation.class);
+                            if (hppAugmentation != null) {
+                                portListUpdated.add(hppAugmentation);
+                            }
+                        }
+                    }
+                    result.put(key, portListUpdated);
+                }
+            }
+        }
+        return result;
+    }
+
+    private Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> extractCreatedPhyscialSwitch(
+            Collection<DataTreeModification<Node>> changes, Class<PhysicalSwitchAugmentation> class1) {
+        Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> result
+            = new HashMap<InstanceIdentifier<Node>, PhysicalSwitchAugmentation>();
+        if (changes != null && !changes.isEmpty()) {
+            for (DataTreeModification<Node> change : changes) {
+                final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
+                final DataObjectModification<Node> mod = change.getRootNode();
+                Node created = TransactUtils.getCreated(mod);
+                if (created != null) {
+                    PhysicalSwitchAugmentation physicalSwitch = created.getAugmentation(PhysicalSwitchAugmentation.class);
+                    if (physicalSwitch != null) {
+                        result.put(key, physicalSwitch);
+                    }
+                }
+            }
+        }
+        return result;
+    }
+
+    private Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> extractUpdated(
+            Collection<DataTreeModification<Node>> changes, Class<HwvtepPhysicalPortAugmentation> class1) {
+        Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> result
+            = new HashMap<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>>();
+        if (changes != null && !changes.isEmpty()) {
+            for (DataTreeModification<Node> change : changes) {
+                final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
+                final DataObjectModification<Node> mod = change.getRootNode();
+                Node updated = TransactUtils.getUpdated(mod);
+                Node before = mod.getDataBefore();
+                if (updated != null && before != null) {
+                    List<HwvtepPhysicalPortAugmentation> portListUpdated = new ArrayList<HwvtepPhysicalPortAugmentation>();
+                    List<HwvtepPhysicalPortAugmentation> portListBefore = new ArrayList<HwvtepPhysicalPortAugmentation>();
+                    if (updated.getTerminationPoint() != null) {
+                        for (TerminationPoint tp : updated.getTerminationPoint()) {
+                            HwvtepPhysicalPortAugmentation hppAugmentation = tp.getAugmentation(HwvtepPhysicalPortAugmentation.class);
+                            if (hppAugmentation != null) {
+                                portListUpdated.add(hppAugmentation);
+                            }
+                        }
+                    }
+                    if (before.getTerminationPoint() != null) {
+                        for (TerminationPoint tp : before.getTerminationPoint()) {
+                            HwvtepPhysicalPortAugmentation hppAugmentation = tp.getAugmentation(HwvtepPhysicalPortAugmentation.class);
+                            if (hppAugmentation != null) {
+                                portListBefore.add(hppAugmentation);
+                            }
+                        }
+                    }
+                    portListUpdated.removeAll(portListBefore);
+                    result.put(key, portListUpdated);
+                }
+            }
+        }
+        return result;
+    }
+}
\ No newline at end of file
index 262ec0f108e7baef66ea5da9f0f76636957144cd..9a08d17937632a20d9f62f897e492f342160fedf 100755 (executable)
         "e25f67e4-3a3f-5da7-f7c8-41b371156719",
         "f7d33d50-7c8e-69e2-6d60-5c6155c5d210",
         "233b5ab5-8177-4416-6912-2477520a0654",
+        "567d7827-628d-07a4-3b95-5a208a244c43",
+        "f906c48b-ba3c-a3da-36e8-34c453538e2e",
+        "5d0c4d49-44f7-4165-ca5f-00cf2a9fca8b",
+        "34167a78-7de4-91a9-ef40-7592b28ead5c",
         "9bc22ca7-049c-af51-7c12-6bf71044b2ec",
         "f6d300f7-380a-d090-0d4a-2b2ddefe5104",
         "f9f71d74-a49d-b190-d929-b6772ce0ba73",
@@ -21,7 +25,8 @@
         "c8e8f3fd-3bfb-aafa-e3ec-a671a942f426",
         "d362ddc4-1c5f-67d5-e354-c2a8d2ba9d79",
         "538c71b3-e3e6-f01b-cc4c-d2b686686aa8",
-        "a13e6877-997e-84e1-c8e8-e83ef5e9a002"
+        "a13e6877-997e-84e1-c8e8-e83ef5e9a002",
+        "3c86ab7c-a7ee-6b71-3ec1-da7d20f97d1a"
     ],
     "owner":0,
     "sharedWithTeam":false,
             "preRequestScript": "",
             "tests": "",
             "rawModeData": ""
+        },
+        {
+            "collectionId": "19f6b1a8-4d54-62f8-6bd6-f52e0b6e40b8",
+            "id": "567d7827-628d-07a4-3b95-5a208a244c43",
+            "headers": "Authorization: Basic YWRtaW46YWRtaW4=\nContent-Type: application/json\n",
+            "url": "http://{{controllerHost}}:8181/restconf/config/network-topology:network-topology/topology/hwvtep:1/node/hwvtep:%2F%2F{{hwvtepNodeIp}}:6640%2Fphysicalswitch%2Fbr0",
+            "preRequestScript": "",
+            "pathVariables": {},
+            "method": "POST",
+            "data": [],
+            "dataMode": "raw",
+            "version": 2,
+            "tests": "",
+            "currentHelper": "normal",
+            "helperAttributes": {},
+            "time": 1448641495086,
+            "name": "Create Specific Config Physical Port",
+            "description": "",
+            "rawModeData": "{\n  \"network-topology:termination-point\": [\n        {\n            \"tp-id\": \"port0\",\n          \t\"hwvtep-node-name\": \"port0\",\n            \"hwvtep-node-description\": \"\",\n            \"vlan-bindings\": [\n          \t\t{\n                  \"vlan-id-key\": \"100\",\n                  \"logical-switch-ref\": \"ls0\"\n            \t}\n          \t]\n\t\t}\n    ]\n}"
+        },
+        {
+            "collectionId": "19f6b1a8-4d54-62f8-6bd6-f52e0b6e40b8",
+            "id": "f906c48b-ba3c-a3da-36e8-34c453538e2e",
+            "headers": "Authorization: Basic YWRtaW46YWRtaW4=\nContent-Type: application/json\n",
+            "url": "http://{{controllerHost}}:8181/restconf/config/network-topology:network-topology/topology/hwvtep:1/node/hwvtep:%2F%2F{{hwvtepNodeIp}}:6640%2Fphysicalswitch%2Fbr0/termination-point/port0",
+            "preRequestScript": "",
+            "pathVariables": {},
+            "method": "PUT",
+            "data": [],
+            "dataMode": "raw",
+            "version": 2,
+            "tests": "",
+            "currentHelper": "normal",
+            "helperAttributes": {},
+            "time": 1448643757985,
+            "name": "Update Specific Config Physical Port",
+            "description": "",
+            "rawModeData": "{\n  \"network-topology:termination-point\": [\n        {\n            \"tp-id\": \"port0\",\n          \t\"hwvtep-node-name\": \"port0\",\n            \"hwvtep-node-description\": \"\",\n            \"vlan-bindings\": [\n          \t\t{\n                  \"vlan-id-key\": \"100\",\n                  \"logical-switch-ref\": \"ls0\"\n            \t}\n          \t]\n\t\t}\n    ]\n}"
+        },
+        {
+            "collectionId": "19f6b1a8-4d54-62f8-6bd6-f52e0b6e40b8",
+            "id": "5d0c4d49-44f7-4165-ca5f-00cf2a9fca8b",
+            "headers": "Authorization: Basic YWRtaW46YWRtaW4=\nContent-Type: application/json\n",
+            "url": "http://{{controllerHost}}:8181/restconf/config/network-topology:network-topology/topology/hwvtep:1/node/hwvtep:%2F%2F{{hwvtepNodeIp}}:6640%2Fphysicalswitch%2Fbr0/termination-point/port0",
+            "preRequestScript": "",
+            "pathVariables": {},
+            "method": "GET",
+            "data": [],
+            "dataMode": "params",
+            "version": 2,
+            "tests": "",
+            "currentHelper": "normal",
+            "helperAttributes": {},
+            "time": 1448643846609,
+            "name": "Get Specific Config Physical Port",
+            "description": ""
+        },
+        {
+            "collectionId": "19f6b1a8-4d54-62f8-6bd6-f52e0b6e40b8",
+            "id": "34167a78-7de4-91a9-ef40-7592b28ead5c",
+            "headers": "Authorization: Basic YWRtaW46YWRtaW4=\nContent-Type: application/json\n",
+            "url": "http://{{controllerHost}}:8181/restconf/config/network-topology:network-topology/topology/hwvtep:1/node/hwvtep:%2F%2F{{hwvtepNodeIp}}:6640%2Fphysicalswitch%2Fbr0/termination-point/port0",
+            "preRequestScript": "",
+            "pathVariables": {},
+            "method": "DELETE",
+            "data": [],
+            "dataMode": "raw",
+            "version": 2,
+            "tests": "",
+            "currentHelper": "normal",
+            "helperAttributes": {},
+            "time": 1448643860663,
+            "name": "Delete Specific Config Physical Port",
+            "description": "",
+            "rawModeData": ""
+        },
+        {
+            "collectionId": "19f6b1a8-4d54-62f8-6bd6-f52e0b6e40b8",
+            "id": "3c86ab7c-a7ee-6b71-3ec1-da7d20f97d1a",
+            "headers": "Authorization: Basic YWRtaW46YWRtaW4=\nContent-Type: application/json\n",
+            "url": "http://{{controllerHost}}:8181/restconf/operational/network-topology:network-topology/topology/hwvtep:1/node/hwvtep:%2F%2F{{hwvtepNodeIp}}:6640%2Fphysicalswitch%2Fbr0/termination-point/port0",
+            "preRequestScript": "",
+            "pathVariables": {},
+            "method": "GET",
+            "data": [],
+            "dataMode": "params",
+            "version": 2,
+            "tests": "",
+            "currentHelper": "normal",
+            "helperAttributes": {},
+            "time": 1448718837283,
+            "name": "Get Specific Operational Physical Port",
+            "description": ""
         }
     ]
 }