blob: 2a18c08304c6eb9f52081af08f1278abb4185bba [file] [log] [blame]
Jonathan Hart472062d2014-04-03 10:56:48 -07001package net.onrc.onos.core.topology;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -08002
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
Ray Milkey269ffb92014-04-03 14:43:30 -07007 * <p/>
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -08008 * TODO: We probably want common base class/interface for Self-Contained Event Object
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -08009 */
10public class PortEvent {
11 public static class SwitchPort {
Ray Milkey269ffb92014-04-03 14:43:30 -070012 public final Long dpid;
13 public final Long number;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080014
Ray Milkey269ffb92014-04-03 14:43:30 -070015 /**
16 * Default constructor for Serializer to use.
17 */
18 @Deprecated
19 public SwitchPort() {
20 dpid = null;
21 number = null;
22 }
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080023
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
Ray Milkey269ffb92014-04-03 14:43:30 -070048 + ((number == null) ? 0 : number.hashCode());
Toshio Koide0c9106d2014-02-19 15:26:38 -080049 return result;
50 }
51
52 @Override
53 public boolean equals(Object obj) {
54 if (this == obj)
Ray Milkey269ffb92014-04-03 14:43:30 -070055 return true;
Toshio Koide0c9106d2014-02-19 15:26:38 -080056 if (obj == null)
Ray Milkey269ffb92014-04-03 14:43:30 -070057 return false;
Toshio Koide0c9106d2014-02-19 15:26:38 -080058 if (getClass() != obj.getClass())
Ray Milkey269ffb92014-04-03 14:43:30 -070059 return false;
Toshio Koide0c9106d2014-02-19 15:26:38 -080060 SwitchPort other = (SwitchPort) obj;
61 if (dpid == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -070062 if (other.dpid != null)
63 return false;
Toshio Koide0c9106d2014-02-19 15:26:38 -080064 } else if (!dpid.equals(other.dpid))
Ray Milkey269ffb92014-04-03 14:43:30 -070065 return false;
Toshio Koide0c9106d2014-02-19 15:26:38 -080066 if (number == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -070067 if (other.number != null)
68 return false;
Toshio Koide0c9106d2014-02-19 15:26:38 -080069 } else if (!number.equals(other.number))
Ray Milkey269ffb92014-04-03 14:43:30 -070070 return false;
Toshio Koide0c9106d2014-02-19 15:26:38 -080071 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 /**
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080080 * Default constructor for Serializer to use.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080081 */
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080082 @Deprecated
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080083 public PortEvent() {
Ray Milkey269ffb92014-04-03 14:43:30 -070084 id = null;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080085 }
86
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080087 public PortEvent(Long dpid, Long number) {
Ray Milkey269ffb92014-04-03 14:43:30 -070088 this.id = new SwitchPort(dpid, number);
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080089 }
90
91 public Long getDpid() {
Ray Milkey269ffb92014-04-03 14:43:30 -070092 return id.dpid;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080093 }
94
95 public Long getNumber() {
Ray Milkey269ffb92014-04-03 14:43:30 -070096 return id.number;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080097 }
98
99 @Override
100 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700101 return "[PortEvent 0x" + Long.toHexString(id.dpid) + "@" + id.number + "]";
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800102 }
103
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800104 public static final int PORTID_BYTES = SwitchEvent.SWITCHID_BYTES + 2 + 8;
105
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800106 public static ByteBuffer getPortID(Long dpid, Long number) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700107 if (dpid == null) {
108 throw new IllegalArgumentException("dpid cannot be null");
109 }
110 if (number == null) {
111 throw new IllegalArgumentException("number cannot be null");
112 }
113 return (ByteBuffer) ByteBuffer.allocate(PortEvent.PORTID_BYTES).putChar('S').putLong(dpid)
114 .putChar('P').putLong(number).flip();
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800115 }
116
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800117 public byte[] getID() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700118 return getPortID(getDpid(), getNumber()).array();
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800119 }
120
121 public ByteBuffer getIDasByteBuffer() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700122 return getPortID(getDpid(), getNumber());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800123 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800124}