Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.networkgraph; |
| 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 | /** |
Yuta HIGUCHI | dac4caa | 2014-02-11 18:51:35 -0800 | [diff] [blame] | 6 | * Self-contained Port event Object |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 7 | * |
| 8 | * TODO: We probably want common base class/interface for Self-Contained Event Object |
| 9 | * |
| 10 | */ |
| 11 | public class PortEvent { |
| 12 | public static class SwitchPort { |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 13 | public final Long dpid; |
| 14 | public final Long number; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 15 | |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 16 | /** |
| 17 | * Default constructor. |
| 18 | */ |
| 19 | public SwitchPort() { |
| 20 | dpid = null; |
| 21 | number = null; |
| 22 | } |
| 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 |
| 48 | + ((number == null) ? 0 : number.hashCode()); |
| 49 | return result; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public boolean equals(Object obj) { |
| 54 | if (this == obj) |
| 55 | return true; |
| 56 | if (obj == null) |
| 57 | return false; |
| 58 | if (getClass() != obj.getClass()) |
| 59 | return false; |
| 60 | SwitchPort other = (SwitchPort) obj; |
| 61 | if (dpid == null) { |
| 62 | if (other.dpid != null) |
| 63 | return false; |
| 64 | } else if (!dpid.equals(other.dpid)) |
| 65 | return false; |
| 66 | if (number == null) { |
| 67 | if (other.number != null) |
| 68 | return false; |
| 69 | } else if (!number.equals(other.number)) |
| 70 | return false; |
| 71 | return true; |
| 72 | } |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | private final SwitchPort id; |
| 76 | // TODO Add Hardware Address |
| 77 | // TODO Add Description |
| 78 | |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 79 | /** |
| 80 | * Default constructor. |
| 81 | */ |
| 82 | public PortEvent() { |
| 83 | id = null; |
| 84 | } |
| 85 | |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 86 | public PortEvent(Long dpid, Long number) { |
| 87 | this.id = new SwitchPort(dpid, number); |
| 88 | } |
| 89 | |
| 90 | public Long getDpid() { |
| 91 | return id.dpid; |
| 92 | } |
| 93 | |
| 94 | public Long getNumber() { |
| 95 | return id.number; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public String toString() { |
| 100 | return "[PortEvent 0x" + Long.toHexString(id.dpid) + "@" + id.number + "]"; |
| 101 | } |
| 102 | |
Yuta HIGUCHI | b510728 | 2014-02-14 17:18:24 -0800 | [diff] [blame] | 103 | public static final int PORTID_BYTES = SwitchEvent.SWITCHID_BYTES + 2 + 8; |
| 104 | |
| 105 | public static byte[] getPortID(Long dpid, Long number) { |
| 106 | if (dpid == null) { |
| 107 | throw new IllegalArgumentException("dpid cannot be null"); |
| 108 | } |
| 109 | if (number == null) { |
| 110 | throw new IllegalArgumentException("number cannot be null"); |
| 111 | } |
| 112 | return ByteBuffer.allocate(PortEvent.PORTID_BYTES).putChar('S').putLong(dpid) |
| 113 | .putChar('P').putLong(number).array(); |
| 114 | } |
| 115 | |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 116 | public byte[] getID() { |
| 117 | return getPortID(getDpid(), getNumber()); |
| 118 | } |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 119 | } |