Fixed EVC create command. 68/29268/4
authorGabriel Robitaille-Montpetit <grmontpetit@inocybe.com>
Wed, 4 Nov 2015 19:30:44 +0000 (14:30 -0500)
committerGabriel Robitaille-Montpetit <grmontpetit@inocybe.com>
Wed, 4 Nov 2015 22:38:20 +0000 (17:38 -0500)
Change-Id: I9dc5eb7fed9056e5a8de4f867ebc981cf0e921ea
Signed-off-by: Gabriel Robitaille-Montpetit <grmontpetit@inocybe.com>
impl/src/main/java/org/opendaylight/unimgr/command/EvcCreateCommand.java
impl/src/main/java/org/opendaylight/unimgr/command/UniCreateCommand.java
impl/src/main/java/org/opendaylight/unimgr/impl/UnimgrUtils.java

index 837e7b165b8292d9a839bee9bc4d8b32ab8db463..466f225cd15b6cb048291455b35670243d92483f 100644 (file)
@@ -11,12 +11,20 @@ import java.util.Map;
 import java.util.Map.Entry;
 
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.unimgr.impl.UnimgrConstants;
+import org.opendaylight.unimgr.impl.UnimgrMapper;
+import org.opendaylight.unimgr.impl.UnimgrUtils;
 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.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 {
 
     private static final Logger LOG = LoggerFactory.getLogger(EvcCreateCommand.class);
@@ -29,65 +37,95 @@ public class EvcCreateCommand extends AbstractCreateCommand {
 
     @Override
     public void execute() {
-        for (Entry<InstanceIdentifier<?>, DataObject> created : changes
-                .entrySet()) {
+        for (Entry<InstanceIdentifier<?>, DataObject> created : changes.entrySet()) {
             if (created.getValue() != null && created.getValue() instanceof EvcAugmentation) {
-//                EvcAugmentation evc = (EvcAugmentation) created.getValue();
-//                LOG.info("New EVC created with id {}.", evc.getEvcId());
-//                if (evc.getUniDest() == null || evc.getUniDest().isEmpty()) {
-//                    LOG.error("Destination UNI cannot be null.");
-//                    break;
-//                }
-//                if (evc.getUniSource() == null || evc.getUniSource().isEmpty()) {
-//                    LOG.error("Source UNI cannot be null.");
-//                    break;
-//                }
-//                // Get the destination UNI
-//                InstanceIdentifier<Node> destinationNodeIid = (InstanceIdentifier<Node>) evc.getUniDest().iterator()
-//                        .next().getUni();
-//                Uni destinationUni = UnimgrUtils.readNode(dataBroker, destinationNodeIid).get()
-//                        .getAugmentation(UniAugmentation.class);
-//                NodeId ovsdbDestinationNodeId = UnimgrMapper.createNodeId(destinationUni.getIpAddress());
-//                // Get the source UNI
-//                InstanceIdentifier<Node> sourceIid = (InstanceIdentifier<Node>) evc.getUniSource().iterator().next()
-//                        .getUni();
-//                Uni sourceUni = UnimgrUtils.readNode(dataBroker, sourceIid).get()
-//                        .getAugmentation(UniAugmentation.class);
-//                NodeId ovsdbSourceNodeId = UnimgrMapper.createNodeId(sourceUni.getIpAddress());
-//
-//                // Set source
-//                Node sourceBr1 = UnimgrUtils.readNode(
-//                        dataBroker,
-//                        UnimgrMapper.getOvsdbBridgeNodeIID(ovsdbSourceNodeId,
-//                                UnimgrConstants.DEFAULT_BRIDGE_NAME)).get();
-//                UnimgrUtils.createTerminationPointNode(dataBroker,
-//                        destinationUni, sourceBr1,
-//                        UnimgrConstants.DEFAULT_BRIDGE_NAME,
-//                        UnimgrConstants.DEFAULT_INTERNAL_IFACE, null);
-//                Node sourceBr2 = UnimgrUtils.readNode(
-//                        dataBroker,
-//                        UnimgrMapper.getOvsdbBridgeNodeIID(ovsdbSourceNodeId,
-//                                UnimgrConstants.DEFAULT_BRIDGE_NAME)).get();
-//                UnimgrUtils.createGreTunnel(dataBroker, sourceUni,
-//                        destinationUni, sourceBr2,
-//                        UnimgrConstants.DEFAULT_BRIDGE_NAME, "gre0");
-//
-//                // Set destination
-//                Node destinationBr1 = UnimgrUtils.readNode(
-//                        dataBroker,
-//                        UnimgrMapper.getOvsdbBridgeNodeIID(ovsdbDestinationNodeId,
-//                                UnimgrConstants.DEFAULT_BRIDGE_NAME)).get();
-//                UnimgrUtils.createTerminationPointNode(dataBroker,
-//                        destinationUni, destinationBr1,
-//                        UnimgrConstants.DEFAULT_BRIDGE_NAME,
-//                        UnimgrConstants.DEFAULT_INTERNAL_IFACE, null);
-//                Node destinationBr2 = UnimgrUtils.readNode(
-//                        dataBroker,
-//                        UnimgrMapper.getOvsdbBridgeNodeIID(ovsdbDestinationNodeId,
-//                                UnimgrConstants.DEFAULT_BRIDGE_NAME)).get();
-//                UnimgrUtils.createGreTunnel(dataBroker, destinationUni,
-//                        sourceUni, destinationBr2,
-//                        UnimgrConstants.DEFAULT_BRIDGE_NAME, "gre0");
+                EvcAugmentation evc = (EvcAugmentation) created.getValue();
+                LOG.info("New EVC created, source IP: {} destination IP {}.",
+                        evc.getUniSource().iterator().next().getIpAddress().getIpv4Address(),
+                        evc.getUniDest().iterator().next().getIpAddress().getIpv4Address());
+                // 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;
+                }
+                if (evc.getUniSource() == null || evc.getUniSource().isEmpty()) {
+                    LOG.error("Source UNI cannot be null.");
+                    break;
+                }
+                InstanceIdentifier<Node> sourceUniIid;
+                InstanceIdentifier<Node> destinationUniIid;
+                if (evc.getUniSource().iterator().next().getUni() != null) {
+                    sourceUniIid = (InstanceIdentifier<Node>) evc.getUniSource().iterator().next().getUni();
+                } else {
+                    sourceUniIid = UnimgrMapper.createUniIid(dataBroker, evc.getUniSource().iterator().next().getIpAddress());
+                }
+                if (evc.getUniDest().iterator().next().getUni() != null) {
+                    destinationUniIid = UnimgrMapper.createUniIid(dataBroker, evc.getUniDest().iterator().next().getIpAddress());;
+                } else {
+                    destinationUniIid = (InstanceIdentifier<Node>) evc.getUniDest().iterator().next().getUni();
+                }
+                // The user has specified the instance identifier of the
+                // uni source and uni destination
+                Optional<Node> optionalUniSource = UnimgrUtils.readNode(dataBroker,
+                                                                        LogicalDatastoreType.CONFIGURATION,
+                                                                        sourceUniIid);
+                Optional<Node> optionalUniDestination = UnimgrUtils.readNode(dataBroker,
+                                                                             LogicalDatastoreType.CONFIGURATION,
+                                                                             destinationUniIid);
+                Node uniSource;
+                Node uniDestination;
+                // Retrieve the source and destination Unis
+                if (!optionalUniSource.isPresent() || !optionalUniDestination.isPresent()) {
+                    LOG.info("Unable to retrieve the Source and/or Destination Unis.");
+                    break;
+                } else {
+                    uniSource = optionalUniSource.get();
+                    uniDestination = optionalUniDestination.get();
+                }
+                // Set source and destination
+                InstanceIdentifier<Node> sourceBridgeIid =
+                        UnimgrMapper.getOvsdbBridgeNodeIID(uniSource.getNodeId(),
+                                                           UnimgrConstants.DEFAULT_BRIDGE_NAME);
+                Optional<Node> optionalSourceBr = UnimgrUtils.readNode(dataBroker,
+                                                                       LogicalDatastoreType.CONFIGURATION,
+                                                                       sourceBridgeIid);
+                InstanceIdentifier<Node> destinationBridgeIid =
+                        UnimgrMapper.getOvsdbBridgeNodeIID(uniSource.getNodeId(),
+                                                           UnimgrConstants.DEFAULT_BRIDGE_NAME);
+                Optional<Node> optionalDestinationBr = UnimgrUtils.readNode(dataBroker,
+                                                                            LogicalDatastoreType.CONFIGURATION,
+                                                                            destinationBridgeIid);
+                if (!optionalSourceBr.isPresent() || !optionalDestinationBr.isPresent()) {
+                    LOG.info("Unable to retrieve the source and/or destination bridge.");
+                    break;
+                } else {
+                    Node sourceBr = optionalSourceBr.get();
+                    Node destinationBr = optionalDestinationBr.get();
+                    UnimgrUtils.createTerminationPointNode(dataBroker,
+                                                           uniSource.getAugmentation(UniAugmentation.class),
+                                                           sourceBr,
+                                                           UnimgrConstants.DEFAULT_BRIDGE_NAME,
+                                                           UnimgrConstants.DEFAULT_TUNNEL_IFACE,
+                                                           UnimgrConstants.DEFAULT_GRE_NAME);
+                    UnimgrUtils.createGreTunnel(dataBroker,
+                                                uniSource.getAugmentation(UniAugmentation.class),
+                                                uniDestination.getAugmentation(UniAugmentation.class),
+                                                sourceBr,
+                                                UnimgrConstants.DEFAULT_BRIDGE_NAME,
+                                                "gre0");
+                    UnimgrUtils.createTerminationPointNode(dataBroker,
+                                                           uniSource.getAugmentation(UniAugmentation.class),
+                                                           destinationBr,
+                                                           UnimgrConstants.DEFAULT_BRIDGE_NAME,
+                                                           UnimgrConstants.DEFAULT_TUNNEL_IFACE,
+                                                           UnimgrConstants.DEFAULT_GRE_NAME);
+                    UnimgrUtils.createGreTunnel(dataBroker,
+                                                uniDestination.getAugmentation(UniAugmentation.class),
+                                                uniSource.getAugmentation(UniAugmentation.class),
+                                                destinationBr,
+                                                UnimgrConstants.DEFAULT_BRIDGE_NAME,
+                                                "gre0");
+                }
             }
         }
     }
index ab799c8b24c614e13d2db70825d3791dc31e4ec0..764e775249676ca979d7cf5fd8020198076bfac9 100644 (file)
@@ -63,14 +63,14 @@ public class UniCreateCommand extends AbstractCreateCommand {
                     Optional<Node> optionalNode = UnimgrUtils.readNode(dataBroker,
                                                                        LogicalDatastoreType.OPERATIONAL,
                                                                        ovsdbNodeRef.getValue());
-                    if (optionalNode.isPresent()) {
-                        Node ovsdbNode = optionalNode.get();
-                    } else {
+                    if (!optionalNode.isPresent()) {
                         LOG.info("Invalid OVSDB node instance identifier specified, "
                                + "attempting to retrieve the node.");
-                        Node ovsdbNode = UnimgrUtils.findOvsdbNode(dataBroker,
-                                                                   uni);
-                        if (ovsdbNode != null) {
+                        Optional<Node> optionalOvsdbNode = UnimgrUtils.findOvsdbNode(dataBroker,
+                                                                                     uni);
+                        Node ovsdbNode;
+                        if (optionalOvsdbNode.isPresent()) {
+                            ovsdbNode = optionalOvsdbNode.get();
                             LOG.info("Retrieved the OVSDB node {}", ovsdbNode.getNodeId());
                             UnimgrUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION,
                                                       uniKey,
@@ -92,9 +92,11 @@ public class UniCreateCommand extends AbstractCreateCommand {
                 } else {
                     // We assume the ovs is in passive mode
                     // Check if the ovsdb node exist
+                    Optional<Node> optionalOvsdbNode = UnimgrUtils.findOvsdbNode(dataBroker,
+                                                                                 uni);
                     Node ovsdbNode;
-                    if (UnimgrUtils.findOvsdbNode(dataBroker, uni) != null) {
-                        ovsdbNode = UnimgrUtils.findOvsdbNode(dataBroker, uni);
+                    if (optionalOvsdbNode.isPresent()) {
+                        ovsdbNode = optionalOvsdbNode.get();
                         LOG.info("Retrieved the OVSDB node");
                         UnimgrUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION,
                                                   uniKey,
index dcdfbb295e157d66f843f1563ac6c870450ff537..558e1e774216fd42ced9c2e8410ee95db692f4be 100644 (file)
@@ -345,9 +345,10 @@ public class UnimgrUtils {
         return result;
     }
 
-    public static Node findOvsdbNode(DataBroker dataBroker,
-                                     UniAugmentation uni) {
+    public static Optional<Node> findOvsdbNode(DataBroker dataBroker,
+                                               UniAugmentation uni) {
         List<Node> ovsdbNodes = getOvsdbNodes(dataBroker);
+        Optional<Node> optionalOvsdb;
         if (!ovsdbNodes.isEmpty()) {
             for (Node ovsdbNode : ovsdbNodes) {
                 OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode
@@ -357,11 +358,27 @@ public class UnimgrUtils {
                                          .getIpv4Address()
                         .equals(uni.getIpAddress().getIpv4Address())) {
                     LOG.info("Found ovsdb node");
-                    return ovsdbNode;
+                    optionalOvsdb = Optional.of(ovsdbNode);
+                    return optionalOvsdb;
                 }
             }
         }
-        return null;
+        return Optional.absent();
+    }
+
+    public static Optional<Node> findUniNode(DataBroker dataBroker,
+                                             IpAddress ipAddress) {
+        List<Node> uniNodes = getUniNodes(dataBroker);
+        if (!uniNodes.isEmpty()) {
+            for (Node uniNode : uniNodes) {
+                UniAugmentation uniAugmentation = uniNode.getAugmentation(UniAugmentation.class);
+                if (uniAugmentation.getIpAddress().equals(ipAddress)) {
+                    LOG.info("Found Uni node");
+                    return Optional.of(uniNode);
+                }
+            }
+        }
+        return Optional.absent();
     }
 
     public static ConnectionInfo getConnectionInfo(DataBroker dataBroker,