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