AD-SAL: Filter packet-in based on container flow
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / core / Path.java
index 5584cd9461abc54d3e79a5c57737f576c0b9e816..3a4a192fc9b86c3df90576b393f7c23222974899 100644 (file)
  * @file   Path.java
  *
  * @brief  Describe a path as a sequence of Edge such that from
- * each of its Tail Node there is an link to the next Head Node in the sequence
+ * each of its Head Node there is an link to the next Tail Node in the sequence
  *
  */
 package org.opendaylight.controller.sal.core;
 
 import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
 import java.util.LinkedList;
 import java.util.List;
-import org.apache.commons.lang3.builder.HashCodeBuilder;
-import org.apache.commons.lang3.builder.EqualsBuilder;
 
-import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
 
 /**
  * Describe a path as a sequence of Edge such that from
- * each of its Tail Node there is an link to the next Head Node in the
+ * each of its Head Node there is an link to the next Tail Node in the
  * sequence
  *
  */
@@ -43,6 +44,7 @@ public class Path implements Serializable {
     /**
      * Private constructor used for JAXB mapping
      */
+    @SuppressWarnings("unused")
     private Path() {
         this.edges = null;
     }
@@ -50,25 +52,23 @@ public class Path implements Serializable {
     /**
      * Construct an object representing a path, the constructor will
      * check if the passed list of edges is such that for every
-     * consecutive edges the tailnode of the first edge coincide with
-     * the head node of the subsequent in order for connectivity to be there.
+     * consecutive edges the head node of the first edge coincide with
+     * the tail node of the subsequent in order for connectivity to be there.
      *
      * @param edges Edges of the path
      *
      */
     public Path(List<Edge> edges) throws ConstructionException {
-        // Lets check if the list of edges is such that the tail node
-        // of an edge is also the head node of the subsequent one
+        // Lets check if the list of edges is such that the head node
+        // of an edge is also the tail node of the subsequent one
         boolean sequential = true;
         if (edges.size() >= 2) {
             for (int i = 0; i < edges.size() - 1; i++) {
                 Edge current = edges.get(i);
                 Edge next = edges.get(i + 1);
-                if (!current.getHeadNodeConnector().getNode()
-                        .equals(
-                                next.getTailNodeConnector()
-                                        .getNode())) {
+                if (!current.getHeadNodeConnector().getNode().equals(next.getTailNodeConnector().getNode())) {
                     sequential = false;
+                    break;
                 }
             }
         } else if (edges.size() == 0) {
@@ -82,6 +82,39 @@ public class Path implements Serializable {
         this.edges = edges;
     }
 
+    /**
+     * Create the reversed path
+     * @return The reversed path
+     */
+    public Path reverse() {
+        int j = edges.size(); // size always > 0
+        Edge[]  aEdges = new Edge[j];
+        for (Edge e : edges) {
+            j--;
+            aEdges[j] = e.reverse();
+        }
+        Path rp;
+        try {
+         rp = new Path(Arrays.asList(aEdges));
+        } catch (ConstructionException ce) {
+            rp = null;
+        }
+        return rp;
+    }
+
+    /**
+     * Return the list of nodes of this path, the first node is the start node
+     * @return the list of nodes
+     */
+    public List<Node> getNodes() {
+        List<Node> nl = new ArrayList<Node>();
+        nl.add(this.getStartNode());
+        for (Edge e : edges) {
+            nl.add(e.getHeadNodeConnector().getNode());
+        }
+        return nl;
+    }
+
     /**
      * Copy Construct for a path
      *
@@ -121,20 +154,40 @@ public class Path implements Serializable {
      * getter method for the Path
      *
      *
-     * @return Return the list of edges that constitue the Path
+     * @return Return the list of edges that constitute the Path
      */
     public List<Edge> getEdges() {
-        return this.edges;
+        return (edges == null) ? Collections.<Edge>emptyList() : new ArrayList<Edge>(edges);
     }
 
     @Override
     public int hashCode() {
-        return HashCodeBuilder.reflectionHashCode(this);
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((edges == null) ? 0 : edges.hashCode());
+        return result;
     }
 
     @Override
     public boolean equals(Object obj) {
-        return EqualsBuilder.reflectionEquals(this, obj);
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        Path other = (Path) obj;
+        if (edges == null) {
+            if (other.edges != null) {
+                return false;
+            }
+        } else if (!edges.equals(other.edges)) {
+            return false;
+        }
+        return true;
     }
 
     @Override