3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
13 * @brief Describe a path as a sequence of Edge such that from
14 * each of its Head Node there is an link to the next Tail Node in the sequence
17 package org.opendaylight.controller.sal.core;
19 import java.io.Serializable;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.LinkedList;
23 import java.util.List;
25 import javax.xml.bind.annotation.XmlAccessType;
26 import javax.xml.bind.annotation.XmlAccessorType;
27 import javax.xml.bind.annotation.XmlElement;
28 import javax.xml.bind.annotation.XmlRootElement;
31 * Describe a path as a sequence of Edge such that from
32 * each of its Head Node there is an link to the next Tail Node in the
37 @XmlAccessorType(XmlAccessType.NONE)
38 public class Path implements Serializable {
39 private static final long serialVersionUID = 1L;
41 private List<Edge> edges;
44 * Private constructor used for JAXB mapping
46 @SuppressWarnings("unused")
52 * Construct an object representing a path, the constructor will
53 * check if the passed list of edges is such that for every
54 * consecutive edges the head node of the first edge coincide with
55 * the tail node of the subsequent in order for connectivity to be there.
57 * @param edges Edges of the path
60 public Path(List<Edge> edges) throws ConstructionException {
61 // Lets check if the list of edges is such that the head node
62 // of an edge is also the tail node of the subsequent one
63 boolean sequential = true;
64 if (edges.size() >= 2) {
65 for (int i = 0; i < edges.size() - 1; i++) {
66 Edge current = edges.get(i);
67 Edge next = edges.get(i + 1);
68 if (!current.getHeadNodeConnector().getNode()
70 next.getTailNodeConnector()
75 } else if (edges.size() == 0) {
76 throw new ConstructionException("Path is empty");
80 throw new ConstructionException("Path is not sequential");
87 * Create the reversed path
88 * @return The reversed path
90 public Path reverse() {
91 int j = edges.size(); // size always > 0
92 Edge[] aEdges = new Edge[j];
93 for (Edge e : edges) {
95 aEdges[j] = e.reverse();
99 rp = new Path(Arrays.asList(aEdges));
100 } catch (ConstructionException ce) {
107 * Return the list of nodes of this path, the first node is the start node
108 * @return the list of nodes
110 public List<Node> getNodes() {
111 List<Node> nl = new ArrayList<Node>();
112 nl.add(this.getStartNode());
113 for (Edge e : edges) {
114 nl.add(e.getHeadNodeConnector().getNode());
120 * Copy Construct for a path
122 * @param src Path to copy from
125 public Path(Path src) throws ConstructionException {
127 this.edges = new LinkedList<Edge>(src.getEdges());
129 throw new ConstructionException("src supplied was null");
134 * get the First Node of the path
137 * @return The start Node of the Path
139 public Node getStartNode() {
140 return this.edges.get(0).getTailNodeConnector().getNode();
144 * get the Last Node of the path
147 * @return The last Node of the Path
149 public Node getEndNode() {
150 return this.edges.get(this.edges.size() - 1).getHeadNodeConnector()
155 * getter method for the Path
158 * @return Return the list of edges that constitue the Path
160 public List<Edge> getEdges() {
165 public int hashCode() {
166 final int prime = 31;
168 result = prime * result + ((edges == null) ? 0 : edges.hashCode());
173 public boolean equals(Object obj) {
178 if (getClass() != obj.getClass())
180 Path other = (Path) obj;
182 if (other.edges != null)
184 } else if (!edges.equals(other.edges))
190 public String toString() {
191 StringBuilder sb = new StringBuilder();
193 for (int i = 0; i < this.edges.size(); i++) {
195 // add the comma to the previous element
198 sb.append(this.edges.get(i).toString());
201 return sb.toString();