blob: b6a51cb0d098e6c1efcac335710eb4f8b32bf817 [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 Koide0e4d8d22014-02-14 10:56:10 -080057 IntentState oldState = state;
58 state = newState;
59 return oldState;
60 }
Toshio Koide4f308732014-02-18 15:19:48 -080061
Toshio Koided48166c2014-02-21 19:18:06 -080062 public LinkedList<String> getLogs() {
63 return logs;
64 }
65
Toshio Koide13986d12014-02-11 20:25:32 -080066 @Override
67 public int hashCode() {
Toshio Koidea10c0372014-02-20 17:28:10 -080068 return (id == null) ? 0 : id.hashCode();
69 }
70
71 @Override
72 public boolean equals(Object obj) {
73 if (this == obj)
74 return true;
75 if ((obj == null) || (getClass() != obj.getClass()))
76 return false;
77 Intent other = (Intent) obj;
78 if (id == null) {
79 if (other.id != null)
80 return false;
81 } else if (!id.equals(other.id))
82 return false;
83 return true;
Toshio Koide13986d12014-02-11 20:25:32 -080084 }
Toshio Koided9fa2a82014-02-19 17:35:18 -080085
Toshio Koide0c9106d2014-02-19 15:26:38 -080086 @Override
87 public String toString() {
88 return id.toString() + ", " + state.toString();
89 }
Toshio Koidead17d5e2014-02-11 11:36:02 -080090}