blob: 957ef889192c5e71013b1680117aa908f245239b [file] [log] [blame]
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -08001package net.onrc.onos.datastore.topology;
2
3import java.nio.ByteBuffer;
4import java.util.HashMap;
5import java.util.Map;
6
7import org.slf4j.Logger;
8import org.slf4j.LoggerFactory;
9
10import com.esotericsoftware.kryo.Kryo;
11
12import net.onrc.onos.datastore.RCObject;
13import net.onrc.onos.datastore.RCTable;
14
15public class RCLink extends RCObject {
16 @SuppressWarnings("unused")
17 private static final Logger log = LoggerFactory.getLogger(RCLink.class);
18
19 private static final ThreadLocal<Kryo> linkKryo = new ThreadLocal<Kryo>() {
20 @Override
21 protected Kryo initialValue() {
22 Kryo kryo = new Kryo();
23 kryo.setRegistrationRequired(true);
24 kryo.setReferences(false);
25 kryo.register(byte[].class);
26 kryo.register(byte[][].class);
27 kryo.register(HashMap.class);
28 // TODO check if we should explicitly specify EnumSerializer
29 kryo.register(STATUS.class);
30 return kryo;
31 }
32 };
33
34 public static class SwitchPort {
35 public final Long dpid;
36 public final Long number;
37
38 public SwitchPort(Long dpid, Long number) {
39 this.dpid = dpid;
40 this.number = number;
41 }
42
43 public byte[] getPortID() {
44 return RCPort.getPortID(dpid, number);
45 }
46
47 public byte[] getSwitchID() {
48 return RCSwitch.getSwichID(dpid);
49 }
50 }
51
52 public static final String GLOBAL_LINK_TABLE_NAME = "G:Link";
53
54 // FIXME these should be Enum or some number, not String
55 private static final String PROP_STATUS = "status";
56 private static final String PROP_SRC_SW_ID = "src-sw-id";
57 private static final String PROP_SRC_PORT_ID = "src-port-id";
58 private static final String PROP_DST_SW_ID = "dst-sw-id";
59 private static final String PROP_DST_PORT_ID = "dst-port-id";
60
61 // must not re-order enum members, ordinal will be sent over wire
62 public enum STATUS {
63 INACTIVE, ACTIVE;
64 }
65
66 private final SwitchPort src;
67 private final SwitchPort dst;
68 private STATUS status;
69
70 public static final int LINKID_BYTES = 2 + RCPort.PORTID_BYTES * 2;
71
72 public static byte[] getLinkID(Long src_dpid, Long src_port_no,
73 Long dst_dpid, Long dst_port_no) {
74 return ByteBuffer.allocate(LINKID_BYTES).putChar('L')
75 .put(RCPort.getPortID(src_dpid, src_port_no))
76 .put(RCPort.getPortID(dst_dpid, dst_port_no)).array();
77 }
78
79 public RCLink(Long src_dpid, Long src_port_no, Long dst_dpid,
80 Long dst_port_no) {
81 super(RCTable.getTable(GLOBAL_LINK_TABLE_NAME), getLinkID(src_dpid,
82 src_port_no, dst_dpid, dst_port_no));
83
84 src = new SwitchPort(src_dpid, src_port_no);
85 dst = new SwitchPort(dst_dpid, dst_port_no);
86 status = STATUS.INACTIVE;
87 }
88
89 public STATUS getStatus() {
90 return status;
91 }
92
93 public void setStatus(STATUS status) {
94 this.status = status;
95 getObjectMap().put(PROP_STATUS, status);
96 }
97
98 public SwitchPort getSrc() {
99 return src;
100 }
101
102 public SwitchPort getDst() {
103 return dst;
104 }
105
106 public byte[] getId() {
107 return getKey();
108 }
109
110 @Override
111 public void serializeAndSetValue() {
112 Map<Object, Object> map = getObjectMap();
113
114 map.put(PROP_SRC_SW_ID, src.getSwitchID());
115 map.put(PROP_SRC_PORT_ID, src.getPortID());
116 map.put(PROP_DST_SW_ID, dst.getSwitchID());
117 map.put(PROP_DST_PORT_ID, dst.getPortID());
118
119 serializeAndSetValue(linkKryo.get(), map);
120 }
121
122 @Override
123 public Map<Object, Object> deserializeObjectFromValue() {
124 Map<Object, Object> map = deserializeObjectFromValue(linkKryo.get());
125
126 this.status = (STATUS) map.get(PROP_STATUS);
127 return map;
128 }
129
130 public static void main(String[] args) {
131 // TODO Auto-generated method stub
132
133 }
134
135}