e21f6f14720d9d510d72cf65ccc7ed1159acae14
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / core / Path.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
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
8  */
9
10 /**
11  * @file   Path.java
12  *
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
15  *
16  */
17 package org.opendaylight.controller.sal.core;
18
19 import java.io.Serializable;
20 import java.util.LinkedList;
21 import java.util.List;
22
23 import javax.xml.bind.annotation.XmlAccessType;
24 import javax.xml.bind.annotation.XmlAccessorType;
25 import javax.xml.bind.annotation.XmlElement;
26 import javax.xml.bind.annotation.XmlRootElement;
27
28 /**
29  * Describe a path as a sequence of Edge such that from
30  * each of its Head Node there is an link to the next Tail Node in the
31  * sequence
32  *
33  */
34 @XmlRootElement
35 @XmlAccessorType(XmlAccessType.NONE)
36 public class Path implements Serializable {
37     private static final long serialVersionUID = 1L;
38     @XmlElement
39     private List<Edge> edges;
40
41     /**
42      * Private constructor used for JAXB mapping
43      */
44     private Path() {
45         this.edges = null;
46     }
47
48     /**
49      * Construct an object representing a path, the constructor will
50      * check if the passed list of edges is such that for every
51      * consecutive edges the head node of the first edge coincide with
52      * the tail node of the subsequent in order for connectivity to be there.
53      *
54      * @param edges Edges of the path
55      *
56      */
57     public Path(List<Edge> edges) throws ConstructionException {
58         // Lets check if the list of edges is such that the head node
59         // of an edge is also the tail node of the subsequent one
60         boolean sequential = true;
61         if (edges.size() >= 2) {
62             for (int i = 0; i < edges.size() - 1; i++) {
63                 Edge current = edges.get(i);
64                 Edge next = edges.get(i + 1);
65                 if (!current.getHeadNodeConnector().getNode()
66                         .equals(
67                                 next.getTailNodeConnector()
68                                         .getNode())) {
69                     sequential = false;
70                 }
71             }
72         } else if (edges.size() == 0) {
73             throw new ConstructionException("Path is empty");
74         }
75
76         if (!sequential) {
77             throw new ConstructionException("Path is not sequential");
78         }
79
80         this.edges = edges;
81     }
82
83     /**
84      * Copy Construct for a path
85      *
86      * @param src Path to copy from
87      *
88      */
89     public Path(Path src) throws ConstructionException {
90         if (src != null) {
91             this.edges = new LinkedList<Edge>(src.getEdges());
92         } else {
93             throw new ConstructionException("src supplied was null");
94         }
95     }
96
97     /**
98      * get the First Node of the path
99      *
100      *
101      * @return The start Node of the Path
102      */
103     public Node getStartNode() {
104         return this.edges.get(0).getTailNodeConnector().getNode();
105     }
106
107     /**
108      * get the Last Node of the path
109      *
110      *
111      * @return The last Node of the Path
112      */
113     public Node getEndNode() {
114         return this.edges.get(this.edges.size() - 1).getHeadNodeConnector()
115                 .getNode();
116     }
117
118     /**
119      * getter method for the Path
120      *
121      *
122      * @return Return the list of edges that constitue the Path
123      */
124     public List<Edge> getEdges() {
125         return this.edges;
126     }
127
128     @Override
129     public int hashCode() {
130         final int prime = 31;
131         int result = 1;
132         result = prime * result + ((edges == null) ? 0 : edges.hashCode());
133         return result;
134     }
135
136     @Override
137     public boolean equals(Object obj) {
138         if (this == obj)
139             return true;
140         if (obj == null)
141             return false;
142         if (getClass() != obj.getClass())
143             return false;
144         Path other = (Path) obj;
145         if (edges == null) {
146             if (other.edges != null)
147                 return false;
148         } else if (!edges.equals(other.edges))
149             return false;
150         return true;
151     }
152
153     @Override
154     public String toString() {
155         StringBuilder sb = new StringBuilder();
156         sb.append("[");
157         for (int i = 0; i < this.edges.size(); i++) {
158             if (i != 0) {
159                 // add the comma to the previous element
160                 sb.append(",");
161             }
162             sb.append(this.edges.get(i).toString());
163         }
164         sb.append("]");
165         return sb.toString();
166     }
167 }