manually cherry pick this patch https://git.opendaylight.org/gerrit/#/c/34579/
[unimgr.git] / impl / src / main / java / org / opendaylight / unimgr / command / EvcAddCommand.java
similarity index 80%
rename from impl/src/main/java/org/opendaylight/unimgr/command/EvcCreateCommand.java
rename to impl/src/main/java/org/opendaylight/unimgr/command/EvcAddCommand.java
index 307a16d2a50c8c0535b418ca108e4835b8dd656a..e84a408da7ba6e21e9e8260e2e52cb14b3030b48 100644 (file)
@@ -1,17 +1,17 @@
 /*
- * Copyright (c) 2015 CableLabs and others.  All rights reserved.
+ * Copyright (c) 2016 CableLabs 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,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-package org.opendaylight.unimgr.command;
 
-import java.util.Map;
-import java.util.Map.Entry;
+package org.opendaylight.unimgr.command;
 
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.unimgr.api.AbstractCommand;
 import org.opendaylight.unimgr.impl.UnimgrConstants;
 import org.opendaylight.unimgr.impl.UnimgrMapper;
 import org.opendaylight.unimgr.utils.EvcUtils;
@@ -19,38 +19,38 @@ import org.opendaylight.unimgr.utils.MdsalUtils;
 import org.opendaylight.unimgr.utils.OvsdbUtils;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.EvcAugmentation;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
-import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.common.base.Optional;
 
-public class EvcCreateCommand extends AbstractCreateCommand {
+public class EvcAddCommand extends AbstractCommand<Link> {
 
-    private static final Logger LOG = LoggerFactory.getLogger(EvcCreateCommand.class);
+    private static final Logger LOG = LoggerFactory.getLogger(EvcAddCommand.class);
 
-    public EvcCreateCommand(DataBroker dataBroker,
-                            Map<InstanceIdentifier<?>, DataObject> changes) {
-        super.dataBroker = dataBroker;
-        super.changes = changes;
+    public EvcAddCommand(final DataBroker dataBroker, final DataTreeModification<Link> newEvcLink) {
+        super(dataBroker, newEvcLink);
     }
 
     @Override
     public void execute() {
-        for (Entry<InstanceIdentifier<?>, DataObject> created : changes.entrySet()) {
-            if ((created.getValue() != null) && (created.getValue() instanceof EvcAugmentation)) {
-                EvcAugmentation evc = (EvcAugmentation) created.getValue();
-                InstanceIdentifier<?> evcKey = created.getKey();
+        final InstanceIdentifier<?> evcKey = dataObject.getRootPath().getRootIdentifier();
+        Optional<Link> optLinks = MdsalUtils.readLink(dataBroker, LogicalDatastoreType.OPERATIONAL, evcKey);
+        if (!optLinks.isPresent()) {
+            final Link evcLink = dataObject.getRootNode().getDataAfter();
+            final EvcAugmentation evc = evcLink.getAugmentation(EvcAugmentation.class);
+            if (evc != null) {
                 // For now, we assume that there is 1 uni per source/destination
                 if ((evc.getUniDest() == null) || evc.getUniDest().isEmpty()) {
                     LOG.error("Destination UNI cannot be null.");
-                    break;
+                    return;
                 }
                 if ((evc.getUniSource() == null) || evc.getUniSource().isEmpty()) {
                     LOG.error("Source UNI cannot be null.");
-                    break;
+                    return;
                 }
                 LOG.info("New EVC created, source IP: {} destination IP {}.",
                         evc.getUniSource().iterator().next().getIpAddress().getIpv4Address(),
@@ -59,7 +59,7 @@ public class EvcCreateCommand extends AbstractCreateCommand {
                 InstanceIdentifier<Node> destinationUniIid;
                 //FIXME we are assuming that there is only 1 UNI source and destination
                 // per evc
-                InstanceIdentifier<?> iidSource = evc.getUniSource().iterator().next().getUni();
+                final InstanceIdentifier<?> iidSource = evc.getUniSource().iterator().next().getUni();
                 if (iidSource != null) {
                     sourceUniIid = iidSource.firstIdentifierOf(Node.class);
                 } else {
@@ -67,7 +67,7 @@ public class EvcCreateCommand extends AbstractCreateCommand {
                                                           evc.getUniSource().iterator().next().getIpAddress(),
                                                           LogicalDatastoreType.OPERATIONAL);
                 }
-                InstanceIdentifier<?> iidDest = evc.getUniDest().iterator().next().getUni();
+                final InstanceIdentifier<?> iidDest = evc.getUniDest().iterator().next().getUni();
                 if (iidDest != null) {
                     destinationUniIid = iidDest.firstIdentifierOf(Node.class);
                 } else {
@@ -75,10 +75,10 @@ public class EvcCreateCommand extends AbstractCreateCommand {
                                                                evc.getUniDest().iterator().next().getIpAddress(),
                                                                LogicalDatastoreType.OPERATIONAL);
                 }
-                Optional<Node> optionalUniSource = MdsalUtils.readNode(dataBroker,
+                final Optional<Node> optionalUniSource = MdsalUtils.readNode(dataBroker,
                                                                         LogicalDatastoreType.OPERATIONAL,
                                                                         sourceUniIid);
-                Optional<Node> optionalUniDestination = MdsalUtils.readNode(dataBroker,
+                final Optional<Node> optionalUniDestination = MdsalUtils.readNode(dataBroker,
                                                                              LogicalDatastoreType.OPERATIONAL,
                                                                              destinationUniIid);
                 Node uniSource;
@@ -88,31 +88,31 @@ public class EvcCreateCommand extends AbstractCreateCommand {
                     uniSource = optionalUniSource.get();
                     uniDestination = optionalUniDestination.get();
                     // Set source and destination
-                    UniAugmentation sourceUniAugmentation =
+                    final UniAugmentation sourceUniAugmentation =
                                         uniSource.getAugmentation(UniAugmentation.class);
-                    UniAugmentation destinationUniAugmentation =
+                    final UniAugmentation destinationUniAugmentation =
                                         uniDestination.getAugmentation(UniAugmentation.class);
-                    Optional<Node> optionalSourceOvsdbNode =
+                    final Optional<Node> optionalSourceOvsdbNode =
                             MdsalUtils.readNode(dataBroker,
                                                             LogicalDatastoreType.OPERATIONAL,
                                                             sourceUniAugmentation
                                                                 .getOvsdbNodeRef()
                                                                 .getValue());
-                    Optional<Node> optionalDestinationOvsdbNode =
+                    final Optional<Node> optionalDestinationOvsdbNode =
                             MdsalUtils.readNode(dataBroker,
                                                             LogicalDatastoreType.OPERATIONAL,
                                                             destinationUniAugmentation
                                                                 .getOvsdbNodeRef()
                                                                 .getValue());
                     if (optionalSourceOvsdbNode.isPresent() && optionalDestinationOvsdbNode.isPresent()) {
-                        InstanceIdentifier<Node> sourceBridgeIid =
+                        final InstanceIdentifier<Node> sourceBridgeIid =
                                 UnimgrMapper.getOvsdbBridgeNodeIid(optionalSourceOvsdbNode.get());
-                        Optional<Node> optionalSourceBr = MdsalUtils.readNode(dataBroker,
+                        final Optional<Node> optionalSourceBr = MdsalUtils.readNode(dataBroker,
                                                                                LogicalDatastoreType.OPERATIONAL,
                                                                                sourceBridgeIid);
-                        InstanceIdentifier<Node> destinationBridgeIid =
+                        final InstanceIdentifier<Node> destinationBridgeIid =
                                 UnimgrMapper.getOvsdbBridgeNodeIid(optionalDestinationOvsdbNode.get());
-                        Optional<Node> optionalDestinationBr = MdsalUtils.readNode(dataBroker,
+                        final Optional<Node> optionalDestinationBr = MdsalUtils.readNode(dataBroker,
                                                                                     LogicalDatastoreType.OPERATIONAL,
                                                                                     destinationBridgeIid);
                         //update ovsdb qos-entry and queues with max-rate to match evc ingress BW
@@ -144,11 +144,11 @@ public class EvcCreateCommand extends AbstractCreateCommand {
                                                         UnimgrConstants.DEFAULT_BRIDGE_NAME,
                                                         UnimgrConstants.DEFAULT_GRE_TUNNEL_NAME);
                             EvcUtils.updateEvcNode(LogicalDatastoreType.CONFIGURATION,
-                                                      evcKey,
-                                                      evc,
-                                                      sourceUniIid,
-                                                      destinationUniIid,
-                                                      dataBroker);
+                                    evcKey,
+                                    evc,
+                                    sourceUniIid,
+                                    destinationUniIid,
+                                    dataBroker);
                             EvcUtils.updateEvcNode(LogicalDatastoreType.OPERATIONAL,
                                                       evcKey,
                                                       evc,