Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.topology; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 2 | |
Yuta HIGUCHI | b510728 | 2014-02-14 17:18:24 -0800 | [diff] [blame] | 3 | import java.nio.ByteBuffer; |
4 | |||||
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 5 | /** |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 6 | * Self-contained Port event Object. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 7 | * <p/> |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 8 | * TODO: We probably want common base class/interface for Self-Contained Event Object. |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 9 | */ |
10 | public class PortEvent { | ||||
11 | public static class SwitchPort { | ||||
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 12 | public final Long dpid; |
13 | public final Long number; | ||||
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 14 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 15 | /** |
16 | * Default constructor for Serializer to use. | ||||
17 | */ | ||||
18 | @Deprecated | ||||
19 | public SwitchPort() { | ||||
20 | dpid = null; | ||||
21 | number = null; | ||||
22 | } | ||||
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 23 | |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 24 | public SwitchPort(Long dpid, Long number) { |
25 | this.dpid = dpid; | ||||
26 | this.number = number; | ||||
27 | } | ||||
28 | |||||
29 | public Long getDpid() { | ||||
30 | return dpid; | ||||
31 | } | ||||
32 | |||||
33 | public Long getNumber() { | ||||
34 | return number; | ||||
35 | } | ||||
36 | |||||
37 | @Override | ||||
38 | public String toString() { | ||||
39 | return "(" + Long.toHexString(dpid) + "@" + number + ")"; | ||||
40 | } | ||||
41 | |||||
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 42 | @Override |
43 | public int hashCode() { | ||||
44 | final int prime = 31; | ||||
45 | int result = 1; | ||||
46 | result = prime * result + ((dpid == null) ? 0 : dpid.hashCode()); | ||||
47 | result = prime * result | ||||
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 48 | + ((number == null) ? 0 : number.hashCode()); |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 49 | return result; |
50 | } | ||||
51 | |||||
52 | @Override | ||||
53 | public boolean equals(Object obj) { | ||||
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 54 | if (this == obj) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 55 | return true; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 56 | } |
57 | if (obj == null) { | ||||
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 58 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 59 | } |
60 | if (getClass() != obj.getClass()) { | ||||
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 61 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 62 | } |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 63 | SwitchPort other = (SwitchPort) obj; |
64 | if (dpid == null) { | ||||
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 65 | if (other.dpid != null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 66 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 67 | } |
68 | } else if (!dpid.equals(other.dpid)) { | ||||
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 69 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 70 | } |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 71 | if (number == null) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 72 | if (other.number != null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 73 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 74 | } |
75 | } else if (!number.equals(other.number)) { | ||||
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 76 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 77 | } |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 78 | return true; |
79 | } | ||||
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 80 | } |
81 | |||||
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 82 | protected final SwitchPort id; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 83 | // TODO Add Hardware Address |
84 | // TODO Add Description | ||||
85 | |||||
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 86 | /** |
Yuta HIGUCHI | 9cc421b | 2014-02-24 15:34:44 -0800 | [diff] [blame] | 87 | * Default constructor for Serializer to use. |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 88 | */ |
Yuta HIGUCHI | 9cc421b | 2014-02-24 15:34:44 -0800 | [diff] [blame] | 89 | @Deprecated |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 90 | public PortEvent() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 91 | id = null; |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 92 | } |
93 | |||||
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 94 | public PortEvent(Long dpid, Long number) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 95 | this.id = new SwitchPort(dpid, number); |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 96 | } |
97 | |||||
98 | public Long getDpid() { | ||||
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 99 | return id.dpid; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 100 | } |
101 | |||||
102 | public Long getNumber() { | ||||
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 103 | return id.number; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 104 | } |
105 | |||||
106 | @Override | ||||
107 | public String toString() { | ||||
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 108 | return "[PortEvent 0x" + Long.toHexString(id.dpid) + "@" + id.number + "]"; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 109 | } |
110 | |||||
Yuta HIGUCHI | b510728 | 2014-02-14 17:18:24 -0800 | [diff] [blame] | 111 | public static final int PORTID_BYTES = SwitchEvent.SWITCHID_BYTES + 2 + 8; |
112 | |||||
Yuta HIGUCHI | a341b11 | 2014-02-23 15:42:00 -0800 | [diff] [blame] | 113 | public static ByteBuffer getPortID(Long dpid, Long number) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 114 | if (dpid == null) { |
115 | throw new IllegalArgumentException("dpid cannot be null"); | ||||
116 | } | ||||
117 | if (number == null) { | ||||
118 | throw new IllegalArgumentException("number cannot be null"); | ||||
119 | } | ||||
120 | return (ByteBuffer) ByteBuffer.allocate(PortEvent.PORTID_BYTES).putChar('S').putLong(dpid) | ||||
121 | .putChar('P').putLong(number).flip(); | ||||
Yuta HIGUCHI | b510728 | 2014-02-14 17:18:24 -0800 | [diff] [blame] | 122 | } |
123 | |||||
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 124 | public byte[] getID() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 125 | return getPortID(getDpid(), getNumber()).array(); |
Yuta HIGUCHI | a341b11 | 2014-02-23 15:42:00 -0800 | [diff] [blame] | 126 | } |
127 | |||||
128 | public ByteBuffer getIDasByteBuffer() { | ||||
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 129 | return getPortID(getDpid(), getNumber()); |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 130 | } |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 131 | } |