blob: f90b6b051e66d0ef5ee00bea0b331b3e6e80dd06 [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/**
8 * @author Toshio Koide (t-koide@onlab.us)
9 */
Toshio Koided9fa2a82014-02-19 17:35:18 -080010public class Intent {
Ray Milkey269ffb92014-04-03 14:43:30 -070011 public enum IntentState {
12 CREATED,
13 INST_REQ,
14 INST_NACK,
15 INST_ACK,
16 DEL_REQ,
17 DEL_PENDING,
18 DEL_ACK,
19 REROUTE_REQ,
20 }
Toshio Koideb609b3b2014-02-14 18:25:52 -080021
Ray Milkey269ffb92014-04-03 14:43:30 -070022 private String id;
23 private IntentState state = IntentState.CREATED;
24 private boolean pathFrozen = false;
Toshio Koide0e4d8d22014-02-14 10:56:10 -080025
Ray Milkey269ffb92014-04-03 14:43:30 -070026 @Optional(value = "logs")
27 private LinkedList<String> logs = new LinkedList<>();
Toshio Koided48166c2014-02-21 19:18:06 -080028
Ray Milkey269ffb92014-04-03 14:43:30 -070029 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -070030 * Default constructor for Kryo deserialization.
Ray Milkey269ffb92014-04-03 14:43:30 -070031 */
32 protected Intent() {
33 logs.add(String.format("created, time:%d", System.nanoTime())); // for measurement
34 }
Toshio Koide13986d12014-02-11 20:25:32 -080035
Ray Milkey269ffb92014-04-03 14:43:30 -070036 public Intent(String id) {
37 logs.add(String.format("created, time:%d", System.nanoTime())); // for measurement
38 this.id = id;
39 }
Toshio Koide13986d12014-02-11 20:25:32 -080040
Ray Milkey269ffb92014-04-03 14:43:30 -070041 public Intent(String id, IntentState state) {
42 logs.add(String.format("created, time:%d", System.nanoTime())); // for measurement
43 setState(state);
44 this.id = id;
45 }
Toshio Koideb609b3b2014-02-14 18:25:52 -080046
Ray Milkey269ffb92014-04-03 14:43:30 -070047 public String getId() {
48 return id;
49 }
Toshio Koideb609b3b2014-02-14 18:25:52 -080050
Ray Milkey269ffb92014-04-03 14:43:30 -070051 public IntentState getState() {
52 return state;
53 }
Toshio Koideb609b3b2014-02-14 18:25:52 -080054
Ray Milkey269ffb92014-04-03 14:43:30 -070055 public IntentState setState(IntentState newState) {
56 logs.add(String.format("setState, oldState:%s, newState:%s, time:%d",
57 state, newState, System.nanoTime())); // for measurement
58 if (logs.size() > 20) { // TODO this size should be configurable
59 logs.removeFirst();
60 }
61 IntentState oldState = state;
62 state = newState;
63 return oldState;
64 }
Toshio Koide4f308732014-02-18 15:19:48 -080065
Ray Milkey269ffb92014-04-03 14:43:30 -070066 public boolean isPathFrozen() {
67 return pathFrozen;
68 }
Toshio Koide565d6dd2014-03-27 11:22:25 -070069
Ray Milkey269ffb92014-04-03 14:43:30 -070070 public void setPathFrozen(boolean isFrozen) {
71 pathFrozen = isFrozen;
72 }
Toshio Koide565d6dd2014-03-27 11:22:25 -070073
Ray Milkey269ffb92014-04-03 14:43:30 -070074 public LinkedList<String> getLogs() {
75 return logs;
76 }
Toshio Koided48166c2014-02-21 19:18:06 -080077
Ray Milkey269ffb92014-04-03 14:43:30 -070078 @Override
79 public int hashCode() {
80 return (id == null) ? 0 : id.hashCode();
81 }
Toshio Koidea10c0372014-02-20 17:28:10 -080082
Ray Milkey269ffb92014-04-03 14:43:30 -070083 @Override
84 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -070085 if (this == obj) {
Ray Milkey269ffb92014-04-03 14:43:30 -070086 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -070087 }
88 if ((obj == null) || (getClass() != obj.getClass())) {
Ray Milkey269ffb92014-04-03 14:43:30 -070089 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070090 }
Ray Milkey269ffb92014-04-03 14:43:30 -070091 Intent other = (Intent) obj;
92 if (id == null) {
Ray Milkeyb29e6262014-04-09 16:02:14 -070093 if (other.id != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -070094 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070095 }
96 } else if (!id.equals(other.id)) {
Ray Milkey269ffb92014-04-03 14:43:30 -070097 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070098 }
Ray Milkey269ffb92014-04-03 14:43:30 -070099 return true;
100 }
Toshio Koided9fa2a82014-02-19 17:35:18 -0800101
Ray Milkey269ffb92014-04-03 14:43:30 -0700102 @Override
103 public String toString() {
104 return id.toString() + ", " + state.toString();
105 }
Toshio Koidead17d5e2014-02-11 11:36:02 -0800106}