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.Collections;
23 import java.util.LinkedList;
24 import java.util.List;
26 import javax.xml.bind.annotation.XmlAccessType;
27 import javax.xml.bind.annotation.XmlAccessorType;
28 import javax.xml.bind.annotation.XmlElement;
29 import javax.xml.bind.annotation.XmlRootElement;
32 * Describe a path as a sequence of Edge such that from
33 * each of its Head Node there is an link to the next Tail Node in the
38 @XmlAccessorType(XmlAccessType.NONE)
39 public class Path implements Serializable {
40 private static final long serialVersionUID = 1L;
42 private List<Edge> edges;
45 * Private constructor used for JAXB mapping
47 @SuppressWarnings("unused")
53 * Construct an object representing a path, the constructor will
54 * check if the passed list of edges is such that for every
55 * consecutive edges the head node of the first edge coincide with
56 * the tail node of the subsequent in order for connectivity to be there.
58 * @param edges Edges of the path
61 public Path(List<Edge> edges) throws ConstructionException {
62 // Lets check if the list of edges is such that the head node
63 // of an edge is also the tail node of the subsequent one
64 boolean sequential = true;
65 if (edges.size() >= 2) {
66 for (int i = 0; i < edges.size() - 1; i++) {
67 Edge current = edges.get(i);
68 Edge next = edges.get(i + 1);
69 if (!current.getHeadNodeConnector().getNode().equals(next.getTailNodeConnector().getNode())) {
74 } else if (edges.size() == 0) {
75 throw new ConstructionException("Path is empty");
79 throw new ConstructionException("Path is not sequential");
86 * Create the reversed path
87 * @return The reversed path
89 public Path reverse() {
90 int j = edges.size(); // size always > 0
91 Edge[] aEdges = new Edge[j];
92 for (Edge e : edges) {
94 aEdges[j] = e.reverse();
98 rp = new Path(Arrays.asList(aEdges));
99 } catch (ConstructionException ce) {
106 * Return the list of nodes of this path, the first node is the start node
107 * @return the list of nodes
109 public List<Node> getNodes() {
110 List<Node> nl = new ArrayList<Node>();
111 nl.add(this.getStartNode());
112 for (Edge e : edges) {
113 nl.add(e.getHeadNodeConnector().getNode());
119 * Copy Construct for a path
121 * @param src Path to copy from
124 public Path(Path src) throws ConstructionException {
126 this.edges = new LinkedList<Edge>(src.getEdges());
128 throw new ConstructionException("src supplied was null");
133 * get the First Node of the path
136 * @return The start Node of the Path
138 public Node getStartNode() {
139 return this.edges.get(0).getTailNodeConnector().getNode();
143 * get the Last Node of the path
146 * @return The last Node of the Path
148 public Node getEndNode() {
149 return this.edges.get(this.edges.size() - 1).getHeadNodeConnector()
154 * getter method for the Path
157 * @return Return the list of edges that constitute the Path
159 public List<Edge> getEdges() {
160 return (edges == null) ? Collections.<Edge>emptyList() : new ArrayList<Edge>(edges);
164 public int hashCode() {
165 final int prime = 31;
167 result = prime * result + ((edges == null) ? 0 : edges.hashCode());
172 public boolean equals(Object obj) {
179 if (getClass() != obj.getClass()) {
182 Path other = (Path) obj;
184 if (other.edges != null) {
187 } else if (!edges.equals(other.edges)) {
194 public String toString() {
195 StringBuilder sb = new StringBuilder();
197 for (int i = 0; i < this.edges.size(); i++) {
199 // add the comma to the previous element
202 sb.append(this.edges.get(i).toString());
205 return sb.toString();