blob: 2f733a49f4b3af9ab94777eb8ddf5f6b3305cfaf [file] [log] [blame]
Toshio Koidea03915e2014-07-01 18:39:52 -07001package net.onrc.onos.api.flowmanager;
2
Toshio Koide9aa4c0f2014-08-11 16:06:44 -07003import static com.google.common.base.Preconditions.checkNotNull;
4
Toshio Koidea03915e2014-07-01 18:39:52 -07005import java.util.ArrayList;
6import java.util.Iterator;
7import java.util.List;
8
9import net.onrc.onos.core.util.Dpid;
10
11/**
12 * Path representation for the flow manager.
13 */
14public class Path extends FlowLinks {
15 /**
16 * Default constructor to create an empty path.
17 */
18 public Path() {
19 super();
20 }
21
22 /**
Toshio Koide9aa4c0f2014-08-11 16:06:44 -070023 * Constructor to create the object from the list of FlowLinks.
24 *
25 * @param links the list of FlowLinks
26 * @throws IllegalArgumentException if the links does not form a single
27 * connected directed path topology
28 */
29 public Path(List<FlowLink> links) {
30 super();
31 checkNotNull(links);
32 for (FlowLink link : links) {
33 if (!addLink(link)) {
34 throw new IllegalArgumentException();
35 }
36 }
37 }
38
39 /**
40 * Adds FlowLink to the Path object.
41 *
42 * @param link the FlowLink object to be added
43 * @return true if succeeded, false otherwise
44 */
45 public boolean addLink(FlowLink link) {
46 // TODO check connectivity
47 checkNotNull(link);
48 return add(link);
49 }
50
51 /**
Toshio Koidea03915e2014-07-01 18:39:52 -070052 * Gets a list of switch DPIDs of the path.
53 *
54 * @return a list of Dpid objects
55 */
56 public List<Dpid> getDpids() {
57 if (size() < 1) {
58 return null;
59 }
60
61 List<Dpid> dpids = new ArrayList<Dpid>(size() + 1);
62 dpids.add(getSrcDpid());
Toshio Koide9aa4c0f2014-08-11 16:06:44 -070063 for (FlowLink link : this) {
Toshio Koidea03915e2014-07-01 18:39:52 -070064 dpids.add(link.getDstDpid());
65 }
66 return dpids;
67 }
68
69 /**
70 * Gets the DPID of the first switch.
71 *
72 * @return a Dpid object of the first switch.
73 */
74 public Dpid getSrcDpid() {
75 if (size() < 1) {
76 return null;
77 }
78 return get(0).getSrcDpid();
79 }
80
81 /**
82 * Gets the DPID of the last switch.
83 *
84 * @return a Dpid object of the last switch.
85 */
86 public Dpid getDstDpid() {
87 if (size() < 1) {
88 return null;
89 }
90 return get(size() - 1).getDstDpid();
91 }
92
93 /**
94 * Returns a string representation of the path.
95 *
96 * @return a string representation of the path.
97 */
98 @Override
99 public String toString() {
100 StringBuilder builder = new StringBuilder();
101 Iterator<FlowLink> i = this.iterator();
102 while (i.hasNext()) {
103 builder.append(i.next().toString());
104 if (i.hasNext()) {
105 builder.append(", ");
106 }
107 }
108 return builder.toString();
109 }
110}