blob: b99a08c84df9ecc4a37af5bc15a4b6e3a6939afa [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 Koided48166c2014-02-21 19:18:06 -08003import java.util.LinkedList;
4
5import com.esotericsoftware.kryo.serializers.FieldSerializer.Optional;
6
Toshio Koidead17d5e2014-02-11 11:36:02 -08007/**
Brian O'Connora581b9d2014-06-15 23:32:36 -07008 * This is the base class for the connectivity abstraction. It allows applications
9 * to specify end hosts, apply some basic filtering to traffic, and constrain traffic.
10 * <p>
11 * This class is subclassed to provide other "high level" intent types like shortest
12 * path connectivity and bandwidth constrained shortest path, as well as "low level"
13 * intent types like full specified/reserved path connectivity.
14 * <p>
15 * The reasoning behind "intent" is that the applications can provide some abstract
16 * representation of how traffic should flow be handled by the networking, allowing
17 * the network OS to compile, reserve and optimize the dataplane to satisfy those
18 * constraints.
Toshio Koidead17d5e2014-02-11 11:36:02 -080019 */
Toshio Koided9fa2a82014-02-19 17:35:18 -080020public class Intent {
Ray Milkey269ffb92014-04-03 14:43:30 -070021 public enum IntentState {
Brian O'Connora581b9d2014-06-15 23:32:36 -070022 /**
23 * Intent has been created.
24 */
Ray Milkey269ffb92014-04-03 14:43:30 -070025 CREATED,
Brian O'Connora581b9d2014-06-15 23:32:36 -070026
27 /**
28 * Installation of this intent has been requested.
29 */
Ray Milkey269ffb92014-04-03 14:43:30 -070030 INST_REQ,
Brian O'Connora581b9d2014-06-15 23:32:36 -070031
32 /**
33 * Intent was not installed.
34 */
Ray Milkey269ffb92014-04-03 14:43:30 -070035 INST_NACK,
Brian O'Connora581b9d2014-06-15 23:32:36 -070036
37 /**
38 * Intent has been successfully installed in the dataplane.
39 */
Ray Milkey269ffb92014-04-03 14:43:30 -070040 INST_ACK,
Brian O'Connora581b9d2014-06-15 23:32:36 -070041
42 /**
43 * A delete/removal of the intent from this dataplane has been requested.
44 */
Ray Milkey269ffb92014-04-03 14:43:30 -070045 DEL_REQ,
Brian O'Connora581b9d2014-06-15 23:32:36 -070046
47 /**
48 * The framework is in the process of removing this intent from the dataplane.
49 */
Ray Milkey269ffb92014-04-03 14:43:30 -070050 DEL_PENDING,
Brian O'Connora581b9d2014-06-15 23:32:36 -070051
52 /**
53 * Intent has been successfully removed from the dataplane.
54 */
Ray Milkey269ffb92014-04-03 14:43:30 -070055 DEL_ACK,
Brian O'Connora581b9d2014-06-15 23:32:36 -070056
57 /**
58 * Intent is pending reroute due to a network event.
59 */
Ray Milkey269ffb92014-04-03 14:43:30 -070060 REROUTE_REQ,
61 }
Toshio Koideb609b3b2014-02-14 18:25:52 -080062
Ray Milkey269ffb92014-04-03 14:43:30 -070063 private String id;
64 private IntentState state = IntentState.CREATED;
65 private boolean pathFrozen = false;
Toshio Koide0e4d8d22014-02-14 10:56:10 -080066
Ray Milkey269ffb92014-04-03 14:43:30 -070067 @Optional(value = "logs")
Brian O'Connora581b9d2014-06-15 23:32:36 -070068 private final LinkedList<String> logs = new LinkedList<>();
Toshio Koided48166c2014-02-21 19:18:06 -080069
Ray Milkey269ffb92014-04-03 14:43:30 -070070 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -070071 * Default constructor for Kryo deserialization.
Ray Milkey269ffb92014-04-03 14:43:30 -070072 */
73 protected Intent() {
74 logs.add(String.format("created, time:%d", System.nanoTime())); // for measurement
75 }
Toshio Koide13986d12014-02-11 20:25:32 -080076
Brian O'Connora581b9d2014-06-15 23:32:36 -070077 /**
78 * Constructor.
79 *
80 * @param id Intent ID
81 */
Ray Milkey269ffb92014-04-03 14:43:30 -070082 public Intent(String id) {
83 logs.add(String.format("created, time:%d", System.nanoTime())); // for measurement
84 this.id = id;
85 }
Toshio Koide13986d12014-02-11 20:25:32 -080086
Brian O'Connora581b9d2014-06-15 23:32:36 -070087 /**
88 * Constructor.
89 *
90 * @param id Intent ID
91 * @param state current state of the new Intent
92 */
Ray Milkey269ffb92014-04-03 14:43:30 -070093 public Intent(String id, IntentState state) {
94 logs.add(String.format("created, time:%d", System.nanoTime())); // for measurement
95 setState(state);
96 this.id = id;
97 }
Toshio Koideb609b3b2014-02-14 18:25:52 -080098
Brian O'Connora581b9d2014-06-15 23:32:36 -070099 /**
100 * Gets the Intent ID.
101 *
102 * @return Intent ID
103 */
Ray Milkey269ffb92014-04-03 14:43:30 -0700104 public String getId() {
105 return id;
106 }
Toshio Koideb609b3b2014-02-14 18:25:52 -0800107
Brian O'Connora581b9d2014-06-15 23:32:36 -0700108 /**
109 * Gets the current state of this Intent.
110 *
111 * @return current state of this Intent
112 */
Ray Milkey269ffb92014-04-03 14:43:30 -0700113 public IntentState getState() {
114 return state;
115 }
Toshio Koideb609b3b2014-02-14 18:25:52 -0800116
Brian O'Connora581b9d2014-06-15 23:32:36 -0700117 /**
118 * Sets the new state for this Intent, and stores the new state in
119 * this Intent's log.
120 *
121 * @param newState new state for this Intent
122 * @return the old state for this Intent
123 */
Ray Milkey269ffb92014-04-03 14:43:30 -0700124 public IntentState setState(IntentState newState) {
125 logs.add(String.format("setState, oldState:%s, newState:%s, time:%d",
126 state, newState, System.nanoTime())); // for measurement
127 if (logs.size() > 20) { // TODO this size should be configurable
128 logs.removeFirst();
129 }
130 IntentState oldState = state;
131 state = newState;
132 return oldState;
133 }
Toshio Koide4f308732014-02-18 15:19:48 -0800134
Brian O'Connora581b9d2014-06-15 23:32:36 -0700135 /**
136 * Checks to see if this Intent's path is frozen.
137 * <p>
138 * Frozen paths will not be rerouted when the network topology changes.
139 *
140 * @return true if frozen, false otherwise
141 */
Ray Milkey269ffb92014-04-03 14:43:30 -0700142 public boolean isPathFrozen() {
143 return pathFrozen;
144 }
Toshio Koide565d6dd2014-03-27 11:22:25 -0700145
Brian O'Connora581b9d2014-06-15 23:32:36 -0700146 /**
147 * Sets the new frozen state for this Intent.
148 *
149 * @param isFrozen the new frozen state for this Intent
150 */
Ray Milkey269ffb92014-04-03 14:43:30 -0700151 public void setPathFrozen(boolean isFrozen) {
152 pathFrozen = isFrozen;
153 }
Toshio Koide565d6dd2014-03-27 11:22:25 -0700154
Brian O'Connora581b9d2014-06-15 23:32:36 -0700155 /**
156 * Retrieves the logs for this intent which includes creation and state changes.
157 *
158 * @return a list of String log entries
159 */
Ray Milkey269ffb92014-04-03 14:43:30 -0700160 public LinkedList<String> getLogs() {
161 return logs;
162 }
Toshio Koided48166c2014-02-21 19:18:06 -0800163
Brian O'Connora581b9d2014-06-15 23:32:36 -0700164 /**
165 * Generates a hash code using the Intent ID.
166 *
167 * @return hashcode
168 */
Ray Milkey269ffb92014-04-03 14:43:30 -0700169 @Override
170 public int hashCode() {
171 return (id == null) ? 0 : id.hashCode();
172 }
Toshio Koidea10c0372014-02-20 17:28:10 -0800173
Brian O'Connora581b9d2014-06-15 23:32:36 -0700174 /**
175 * Compares two intent object by type (class) and Intent ID.
176 *
177 * @param obj other Intent
178 * @return true if equal, false otherwise
179 */
Ray Milkey269ffb92014-04-03 14:43:30 -0700180 @Override
181 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700182 if (this == obj) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700183 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700184 }
185 if ((obj == null) || (getClass() != obj.getClass())) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700186 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700187 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700188 Intent other = (Intent) obj;
189 if (id == null) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700190 if (other.id != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700191 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700192 }
193 } else if (!id.equals(other.id)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700194 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700195 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700196 return true;
197 }
Toshio Koided9fa2a82014-02-19 17:35:18 -0800198
Brian O'Connora581b9d2014-06-15 23:32:36 -0700199 /**
200 * Returns a String representation of this Intent.
201 *
202 * @return "Intent ID, State"
203 */
Ray Milkey269ffb92014-04-03 14:43:30 -0700204 @Override
205 public String toString() {
206 return id.toString() + ", " + state.toString();
207 }
Toshio Koidead17d5e2014-02-11 11:36:02 -0800208}