Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.intent; |
Toshio Koide | ad17d5e | 2014-02-11 11:36:02 -0800 | [diff] [blame] | 2 | |
Toshio Koide | 7d3cee0 | 2014-06-05 18:56:19 -0700 | [diff] [blame] | 3 | import java.util.Objects; |
Toshio Koide | ad17d5e | 2014-02-11 11:36:02 -0800 | [diff] [blame] | 4 | |
| 5 | /** |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 6 | * 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 Koide | ad17d5e | 2014-02-11 11:36:02 -0800 | [diff] [blame] | 9 | */ |
| 10 | public class PathIntent extends Intent { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 11 | protected Path path; |
| 12 | protected double bandwidth; |
| 13 | protected Intent parentIntent; |
Toshio Koide | c406e79 | 2014-02-14 16:52:42 -0800 | [diff] [blame] | 14 | |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 15 | /** |
| 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 22 | public static String createFirstId(String parentId) { |
| 23 | return String.format("%s___0", parentId); |
| 24 | } |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 25 | |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 26 | /** |
| 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 33 | public static String createNextId(String currentId) { |
Ray Milkey | 45614c5 | 2014-04-07 16:30:54 -0700 | [diff] [blame] | 34 | String[] parts = currentId.split("___"); |
Yuta HIGUCHI | 0fe749a | 2014-05-27 09:35:16 -0700 | [diff] [blame] | 35 | return String.format("%s___%d", parts[0], Long.parseLong(parts[1]) + 1); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 36 | } |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 37 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 38 | /** |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 39 | * Default constructor for Kryo deserialization. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 40 | */ |
| 41 | protected PathIntent() { |
| 42 | } |
Toshio Koide | ad17d5e | 2014-02-11 11:36:02 -0800 | [diff] [blame] | 43 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 44 | /** |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 45 | * Constructor. |
| 46 | * |
| 47 | * @param id the PathIntent ID |
| 48 | * @param path the path for this Intent |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 49 | * @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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 52 | */ |
| 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 Koide | ad17d5e | 2014-02-11 11:36:02 -0800 | [diff] [blame] | 59 | |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 60 | /** |
| 61 | * Get the bandwidth specified for this Intent. |
| 62 | * TODO: specify unit |
| 63 | * |
| 64 | * @return this Intent's bandwidth |
| 65 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 66 | public double getBandwidth() { |
| 67 | return bandwidth; |
| 68 | } |
Toshio Koide | ad17d5e | 2014-02-11 11:36:02 -0800 | [diff] [blame] | 69 | |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 70 | /** |
| 71 | * Get the Path for this Intent. |
| 72 | * |
| 73 | * @return this Intent's Path |
| 74 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 75 | public Path getPath() { |
| 76 | return path; |
| 77 | } |
Toshio Koide | c406e79 | 2014-02-14 16:52:42 -0800 | [diff] [blame] | 78 | |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 79 | /** |
| 80 | * Get the parent Intent. |
| 81 | * |
| 82 | * @return the parent Intent |
| 83 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 84 | public Intent getParentIntent() { |
| 85 | return parentIntent; |
| 86 | } |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 87 | |
Toshio Koide | 7d3cee0 | 2014-06-05 18:56:19 -0700 | [diff] [blame] | 88 | /** |
| 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'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 108 | /** |
| 109 | * Generates a hash code using the Intent ID. |
| 110 | * |
| 111 | * @return hashcode |
| 112 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 113 | @Override |
Pavlin Radoslavov | 7fb1641 | 2014-04-11 18:45:19 -0700 | [diff] [blame] | 114 | public int hashCode() { |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 115 | return super.hashCode(); |
Pavlin Radoslavov | 7fb1641 | 2014-04-11 18:45:19 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 118 | /** |
| 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 Radoslavov | 7fb1641 | 2014-04-11 18:45:19 -0700 | [diff] [blame] | 124 | @Override |
| 125 | public boolean equals(Object obj) { |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 126 | return super.equals(obj); |
Pavlin Radoslavov | 7fb1641 | 2014-04-11 18:45:19 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame] | 129 | /** |
| 130 | * Returns a String representation of this Intent. |
| 131 | * |
| 132 | * @return "Intent ID, State, Path" |
| 133 | */ |
Pavlin Radoslavov | 7fb1641 | 2014-04-11 18:45:19 -0700 | [diff] [blame] | 134 | @Override |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 135 | public String toString() { |
| 136 | return String.format("%s, %s, %s", getId(), getState(), getPath()); |
| 137 | } |
Toshio Koide | ad17d5e | 2014-02-11 11:36:02 -0800 | [diff] [blame] | 138 | } |