blob: b9260379d13a99129b9fa86b6ccef33fc50442ef [file] [log] [blame]
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -08001package net.onrc.onos.ofcontroller.networkgraph;
2
Yuta HIGUCHIb5107282014-02-14 17:18:24 -08003import java.nio.ByteBuffer;
4
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -08005/**
Yuta HIGUCHIdac4caa2014-02-11 18:51:35 -08006 * Self-contained Port event Object
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -08007 *
8 * TODO: We probably want common base class/interface for Self-Contained Event Object
9 *
10 */
11public class PortEvent {
12 public static class SwitchPort {
Toshio Koide0c9106d2014-02-19 15:26:38 -080013 public final Long dpid;
14 public final Long number;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080015
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080016 /**
17 * Default constructor.
18 */
19 public SwitchPort() {
20 dpid = null;
21 number = null;
22 }
23
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080024 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 Koide0c9106d2014-02-19 15:26:38 -080042 @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 HIGUCHI54ab8cd2014-02-11 09:43:34 -080073 }
74
Pavlin Radoslavov26d83402014-02-20 15:24:30 -080075 protected final SwitchPort id;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080076 // TODO Add Hardware Address
77 // TODO Add Description
78
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080079 /**
80 * Default constructor.
81 */
82 public PortEvent() {
83 id = null;
84 }
85
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080086 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 HIGUCHIb5107282014-02-14 17:18:24 -0800103 public static final int PORTID_BYTES = SwitchEvent.SWITCHID_BYTES + 2 + 8;
104
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800105 public static ByteBuffer getPortID(Long dpid, Long number) {
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800106 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)
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800113 .putChar('P').putLong(number);
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800114 }
115
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800116 public byte[] getID() {
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800117 return getPortID(getDpid(), getNumber()).array();
118 }
119
120 public ByteBuffer getIDasByteBuffer() {
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800121 return getPortID(getDpid(), getNumber());
122 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800123}