blob: c6a960f03daa5444a97d24c2298cf3ca492329e3 [file] [log] [blame]
Toshio Koidead17d5e2014-02-11 11:36:02 -08001package net.onrc.onos.intent;
2
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 {
Nick Karanatsios88948d32014-02-18 15:14:30 -080011 public enum IntentState {
Toshio Koideb609b3b2014-02-14 18:25:52 -080012 CREATED,
13 INST_REQ,
14 INST_NACK,
15 INST_ACK,
16 DEL_REQ,
17 DEL_PENDING,
18 DEL_ACK,
Toshio Koidea10c0372014-02-20 17:28:10 -080019 REROUTE_REQ,
Toshio Koide0e4d8d22014-02-14 10:56:10 -080020 }
Toshio Koideb609b3b2014-02-14 18:25:52 -080021
Toshio Koidea10c0372014-02-20 17:28:10 -080022 private String id;
23 private IntentState state = IntentState.CREATED;
Toshio Koide0e4d8d22014-02-14 10:56:10 -080024
Toshio Koided48166c2014-02-21 19:18:06 -080025 @Optional(value="logs")
26 private LinkedList<String> logs = new LinkedList<>();
27
Toshio Koide0e4d8d22014-02-14 10:56:10 -080028 /**
29 * Default constructor for Kryo deserialization
30 */
Toshio Koidec406e792014-02-14 16:52:42 -080031 protected Intent() {
Toshio Koided48166c2014-02-21 19:18:06 -080032 logs.add(String.format("created, time:%d", System.nanoTime())); // for measurement
Toshio Koide0e4d8d22014-02-14 10:56:10 -080033 }
Toshio Koide13986d12014-02-11 20:25:32 -080034
35 public Intent(String id) {
Toshio Koided48166c2014-02-21 19:18:06 -080036 logs.add(String.format("created, time:%d", System.nanoTime())); // for measurement
Toshio Koide13986d12014-02-11 20:25:32 -080037 this.id = id;
38 }
39
Toshio Koideb609b3b2014-02-14 18:25:52 -080040 public Intent(String id, IntentState state) {
Toshio Koided48166c2014-02-21 19:18:06 -080041 logs.add(String.format("created, time:%d", System.nanoTime())); // for measurement
42 setState(state);
Toshio Koideb609b3b2014-02-14 18:25:52 -080043 this.id = id;
Toshio Koideb609b3b2014-02-14 18:25:52 -080044 }
45
Toshio Koide13986d12014-02-11 20:25:32 -080046 public String getId() {
47 return id;
48 }
Toshio Koideb609b3b2014-02-14 18:25:52 -080049
Toshio Koide0e4d8d22014-02-14 10:56:10 -080050 public IntentState getState() {
51 return state;
52 }
Toshio Koideb609b3b2014-02-14 18:25:52 -080053
Toshio Koide0e4d8d22014-02-14 10:56:10 -080054 public IntentState setState(IntentState newState) {
Toshio Koided48166c2014-02-21 19:18:06 -080055 logs.add(String.format("setState, oldState:%s, newState:%s, time:%d",
56 state, newState, System.nanoTime())); // for measurement
Toshio Koide6ed9f532014-02-25 13:29:31 -080057 if (logs.size() > 20) { // TODO this size should be configurable
58 logs.removeFirst();
59 }
Toshio Koide0e4d8d22014-02-14 10:56:10 -080060 IntentState oldState = state;
61 state = newState;
62 return oldState;
63 }
Toshio Koide4f308732014-02-18 15:19:48 -080064
Toshio Koided48166c2014-02-21 19:18:06 -080065 public LinkedList<String> getLogs() {
66 return logs;
67 }
68
Toshio Koide13986d12014-02-11 20:25:32 -080069 @Override
70 public int hashCode() {
Toshio Koidea10c0372014-02-20 17:28:10 -080071 return (id == null) ? 0 : id.hashCode();
72 }
73
74 @Override
75 public boolean equals(Object obj) {
76 if (this == obj)
77 return true;
78 if ((obj == null) || (getClass() != obj.getClass()))
79 return false;
80 Intent other = (Intent) obj;
81 if (id == null) {
82 if (other.id != null)
83 return false;
84 } else if (!id.equals(other.id))
85 return false;
86 return true;
Toshio Koide13986d12014-02-11 20:25:32 -080087 }
Toshio Koided9fa2a82014-02-19 17:35:18 -080088
Toshio Koide0c9106d2014-02-19 15:26:38 -080089 @Override
90 public String toString() {
91 return id.toString() + ", " + state.toString();
92 }
Toshio Koidead17d5e2014-02-11 11:36:02 -080093}