blob: f2518a63bd9c6ed839ae8f40e06850557d155a7e [file] [log] [blame]
Jonathan Hartaa380972014-04-03 10:24:46 -07001package net.onrc.onos.core.intent;
Toshio Koidead17d5e2014-02-11 11:36:02 -08002
Toshio Koide7d3cee02014-06-05 18:56:19 -07003import java.util.Objects;
Toshio Koidead17d5e2014-02-11 11:36:02 -08004
5/**
Brian O'Connora581b9d2014-06-15 23:32:36 -07006 * The PathIntent is a "low-level" intent that is the manifestation
7 * of a "high-level" intent. It contains the path through the network
8 * and a pointer to the "high-level" intent.
Toshio Koidead17d5e2014-02-11 11:36:02 -08009 */
10public class PathIntent extends Intent {
Ray Milkey269ffb92014-04-03 14:43:30 -070011 protected Path path;
12 protected double bandwidth;
13 protected Intent parentIntent;
Toshio Koidec406e792014-02-14 16:52:42 -080014
Brian O'Connora581b9d2014-06-15 23:32:36 -070015 /**
16 * Generate the PathIntent ID for the first path for a
17 * parent Intent. It is derived from the parent Intent ID.
18 *
19 * @param parentId parent Intent ID
20 * @return new PathIntent ID
21 */
Ray Milkey269ffb92014-04-03 14:43:30 -070022 public static String createFirstId(String parentId) {
23 return String.format("%s___0", parentId);
24 }
Toshio Koidea10c0372014-02-20 17:28:10 -080025
Brian O'Connora581b9d2014-06-15 23:32:36 -070026 /**
27 * Generate the PathIntent ID for subsequent paths used to
28 * satisfy a parent Intent. It is derived from the previous ID.
29 *
30 * @param currentId current PathIntent's ID
31 * @return new PathIntent ID
32 */
Ray Milkey269ffb92014-04-03 14:43:30 -070033 public static String createNextId(String currentId) {
Ray Milkey45614c52014-04-07 16:30:54 -070034 String[] parts = currentId.split("___");
Yuta HIGUCHI0fe749a2014-05-27 09:35:16 -070035 return String.format("%s___%d", parts[0], Long.parseLong(parts[1]) + 1);
Ray Milkey269ffb92014-04-03 14:43:30 -070036 }
Toshio Koidea10c0372014-02-20 17:28:10 -080037
Ray Milkey269ffb92014-04-03 14:43:30 -070038 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -070039 * Default constructor for Kryo deserialization.
Ray Milkey269ffb92014-04-03 14:43:30 -070040 */
41 protected PathIntent() {
42 }
Toshio Koidead17d5e2014-02-11 11:36:02 -080043
Ray Milkey269ffb92014-04-03 14:43:30 -070044 /**
Brian O'Connora581b9d2014-06-15 23:32:36 -070045 * Constructor.
46 *
47 * @param id the PathIntent ID
48 * @param path the path for this Intent
Ray Milkey269ffb92014-04-03 14:43:30 -070049 * @param bandwidth bandwidth which should be allocated for the path.
50 * If 0, no intent for bandwidth allocation (best effort).
51 * @param parentIntent parent intent. If null, this is root intent.
Ray Milkey269ffb92014-04-03 14:43:30 -070052 */
53 public PathIntent(String id, Path path, double bandwidth, Intent parentIntent) {
54 super(id);
55 this.path = path;
56 this.bandwidth = bandwidth;
57 this.parentIntent = parentIntent;
58 }
Toshio Koidead17d5e2014-02-11 11:36:02 -080059
Brian O'Connora581b9d2014-06-15 23:32:36 -070060 /**
61 * Get the bandwidth specified for this Intent.
62 * TODO: specify unit
63 *
64 * @return this Intent's bandwidth
65 */
Ray Milkey269ffb92014-04-03 14:43:30 -070066 public double getBandwidth() {
67 return bandwidth;
68 }
Toshio Koidead17d5e2014-02-11 11:36:02 -080069
Brian O'Connora581b9d2014-06-15 23:32:36 -070070 /**
71 * Get the Path for this Intent.
72 *
73 * @return this Intent's Path
74 */
Ray Milkey269ffb92014-04-03 14:43:30 -070075 public Path getPath() {
76 return path;
77 }
Toshio Koidec406e792014-02-14 16:52:42 -080078
Brian O'Connora581b9d2014-06-15 23:32:36 -070079 /**
80 * Get the parent Intent.
81 *
82 * @return the parent Intent
83 */
Ray Milkey269ffb92014-04-03 14:43:30 -070084 public Intent getParentIntent() {
85 return parentIntent;
86 }
Toshio Koide93be5d62014-02-23 19:30:57 -080087
Toshio Koide7d3cee02014-06-05 18:56:19 -070088 /**
89 * Checks the specified PathIntent have the same fields of
90 * path, bandwidth and parentIntent's id with this PathIntent.
91 *
92 * @param target target PathIntent instance
93 * @return true if the specified intent has the same fields, otherwise false
94 */
95 public boolean hasSameFields(PathIntent target) {
96 if (target == null) {
97 return false;
98 }
99 if (!Objects.equals(getPath(), target.getPath())) {
100 return false;
101 }
102 if (getBandwidth() != target.getBandwidth()) {
103 return false;
104 }
105 return Objects.equals(getParentIntent(), target.getParentIntent());
106 }
107
Brian O'Connora581b9d2014-06-15 23:32:36 -0700108 /**
109 * Generates a hash code using the Intent ID.
110 *
111 * @return hashcode
112 */
Ray Milkey269ffb92014-04-03 14:43:30 -0700113 @Override
Pavlin Radoslavov7fb16412014-04-11 18:45:19 -0700114 public int hashCode() {
Brian O'Connora581b9d2014-06-15 23:32:36 -0700115 return super.hashCode();
Pavlin Radoslavov7fb16412014-04-11 18:45:19 -0700116 }
117
Brian O'Connora581b9d2014-06-15 23:32:36 -0700118 /**
119 * Compares two intent object by type (class) and Intent ID.
120 *
121 * @param obj other Intent
122 * @return true if equal, false otherwise
123 */
Pavlin Radoslavov7fb16412014-04-11 18:45:19 -0700124 @Override
125 public boolean equals(Object obj) {
Brian O'Connora581b9d2014-06-15 23:32:36 -0700126 return super.equals(obj);
Pavlin Radoslavov7fb16412014-04-11 18:45:19 -0700127 }
128
Brian O'Connora581b9d2014-06-15 23:32:36 -0700129 /**
130 * Returns a String representation of this Intent.
131 *
132 * @return "Intent ID, State, Path"
133 */
Pavlin Radoslavov7fb16412014-04-11 18:45:19 -0700134 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -0700135 public String toString() {
136 return String.format("%s, %s, %s", getId(), getState(), getPath());
137 }
Toshio Koidead17d5e2014-02-11 11:36:02 -0800138}