Toshio Koide | ad17d5e | 2014-02-11 11:36:02 -0800 | [diff] [blame] | 1 | package net.onrc.onos.intent; |
| 2 | |
Toshio Koide | d48166c | 2014-02-21 19:18:06 -0800 | [diff] [blame] | 3 | import java.util.LinkedList; |
| 4 | |
| 5 | import com.esotericsoftware.kryo.serializers.FieldSerializer.Optional; |
| 6 | |
Toshio Koide | ad17d5e | 2014-02-11 11:36:02 -0800 | [diff] [blame] | 7 | /** |
| 8 | * @author Toshio Koide (t-koide@onlab.us) |
| 9 | */ |
Toshio Koide | d9fa2a8 | 2014-02-19 17:35:18 -0800 | [diff] [blame] | 10 | public class Intent { |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 11 | public enum IntentState { |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 12 | CREATED, |
| 13 | INST_REQ, |
| 14 | INST_NACK, |
| 15 | INST_ACK, |
| 16 | DEL_REQ, |
| 17 | DEL_PENDING, |
| 18 | DEL_ACK, |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 19 | REROUTE_REQ, |
Toshio Koide | 0e4d8d2 | 2014-02-14 10:56:10 -0800 | [diff] [blame] | 20 | } |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 21 | |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 22 | private String id; |
| 23 | private IntentState state = IntentState.CREATED; |
Toshio Koide | 0e4d8d2 | 2014-02-14 10:56:10 -0800 | [diff] [blame] | 24 | |
Toshio Koide | d48166c | 2014-02-21 19:18:06 -0800 | [diff] [blame] | 25 | @Optional(value="logs") |
| 26 | private LinkedList<String> logs = new LinkedList<>(); |
| 27 | |
Toshio Koide | 0e4d8d2 | 2014-02-14 10:56:10 -0800 | [diff] [blame] | 28 | /** |
| 29 | * Default constructor for Kryo deserialization |
| 30 | */ |
Toshio Koide | c406e79 | 2014-02-14 16:52:42 -0800 | [diff] [blame] | 31 | protected Intent() { |
Toshio Koide | d48166c | 2014-02-21 19:18:06 -0800 | [diff] [blame] | 32 | logs.add(String.format("created, time:%d", System.nanoTime())); // for measurement |
Toshio Koide | 0e4d8d2 | 2014-02-14 10:56:10 -0800 | [diff] [blame] | 33 | } |
Toshio Koide | 13986d1 | 2014-02-11 20:25:32 -0800 | [diff] [blame] | 34 | |
| 35 | public Intent(String id) { |
Toshio Koide | d48166c | 2014-02-21 19:18:06 -0800 | [diff] [blame] | 36 | logs.add(String.format("created, time:%d", System.nanoTime())); // for measurement |
Toshio Koide | 13986d1 | 2014-02-11 20:25:32 -0800 | [diff] [blame] | 37 | this.id = id; |
| 38 | } |
| 39 | |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 40 | public Intent(String id, IntentState state) { |
Toshio Koide | d48166c | 2014-02-21 19:18:06 -0800 | [diff] [blame] | 41 | logs.add(String.format("created, time:%d", System.nanoTime())); // for measurement |
| 42 | setState(state); |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 43 | this.id = id; |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Toshio Koide | 13986d1 | 2014-02-11 20:25:32 -0800 | [diff] [blame] | 46 | public String getId() { |
| 47 | return id; |
| 48 | } |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 49 | |
Toshio Koide | 0e4d8d2 | 2014-02-14 10:56:10 -0800 | [diff] [blame] | 50 | public IntentState getState() { |
| 51 | return state; |
| 52 | } |
Toshio Koide | b609b3b | 2014-02-14 18:25:52 -0800 | [diff] [blame] | 53 | |
Toshio Koide | 0e4d8d2 | 2014-02-14 10:56:10 -0800 | [diff] [blame] | 54 | public IntentState setState(IntentState newState) { |
Toshio Koide | d48166c | 2014-02-21 19:18:06 -0800 | [diff] [blame] | 55 | logs.add(String.format("setState, oldState:%s, newState:%s, time:%d", |
| 56 | state, newState, System.nanoTime())); // for measurement |
Toshio Koide | 0e4d8d2 | 2014-02-14 10:56:10 -0800 | [diff] [blame] | 57 | IntentState oldState = state; |
| 58 | state = newState; |
| 59 | return oldState; |
| 60 | } |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 61 | |
Toshio Koide | d48166c | 2014-02-21 19:18:06 -0800 | [diff] [blame] | 62 | public LinkedList<String> getLogs() { |
| 63 | return logs; |
| 64 | } |
| 65 | |
Toshio Koide | 13986d1 | 2014-02-11 20:25:32 -0800 | [diff] [blame] | 66 | @Override |
| 67 | public int hashCode() { |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 68 | 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 Koide | 13986d1 | 2014-02-11 20:25:32 -0800 | [diff] [blame] | 84 | } |
Toshio Koide | d9fa2a8 | 2014-02-19 17:35:18 -0800 | [diff] [blame] | 85 | |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 86 | @Override |
| 87 | public String toString() { |
| 88 | return id.toString() + ", " + state.toString(); |
| 89 | } |
Toshio Koide | ad17d5e | 2014-02-11 11:36:02 -0800 | [diff] [blame] | 90 | } |