blob: c9273aefa892f6498e54adc666813250494f8daa [file] [log] [blame]
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -07001package net.onrc.onos.datastore.topology;
2
3import java.nio.ByteBuffer;
4import java.util.HashMap;
5import java.util.Iterator;
6import java.util.Map;
7
8import net.onrc.onos.datastore.DataStoreClient;
9import net.onrc.onos.datastore.IKVTable.IKVEntry;
10import net.onrc.onos.datastore.RCProtos.LinkProperty;
11import net.onrc.onos.datastore.utils.KVObject;
12import net.onrc.onos.ofcontroller.networkgraph.LinkEvent;
13import net.onrc.onos.ofcontroller.networkgraph.PortEvent;
14
15import org.slf4j.Logger;
16import org.slf4j.LoggerFactory;
17
18import com.esotericsoftware.kryo.Kryo;
19import com.google.protobuf.ByteString;
20import com.google.protobuf.InvalidProtocolBufferException;
21
22/**
23 * Link object in data store.
24 */
25public class KVLink extends KVObject {
26 private static final Logger log = LoggerFactory.getLogger(KVLink.class);
27
28 private static final ThreadLocal<Kryo> linkKryo = new ThreadLocal<Kryo>() {
29 @Override
30 protected Kryo initialValue() {
31 Kryo kryo = new Kryo();
32 kryo.setRegistrationRequired(true);
33 kryo.setReferences(false);
34 kryo.register(byte[].class);
35 kryo.register(byte[][].class);
36 kryo.register(HashMap.class);
37 // TODO check if we should explicitly specify EnumSerializer
38 kryo.register(STATUS.class);
39 return kryo;
40 }
41 };
42
43 public static class SwitchPort {
44 public final Long dpid;
45 public final Long number;
46
47 public SwitchPort(final Long dpid, final Long number) {
48 this.dpid = dpid;
49 this.number = number;
50 }
51
52 public byte[] getPortID() {
53 return KVPort.getPortID(dpid, number);
54 }
55
56 public byte[] getSwitchID() {
57 return KVSwitch.getSwitchID(dpid);
58 }
59
60 @Override
61 public String toString() {
62 return "(" + Long.toHexString(dpid) + "@" + number + ")";
63 }
64
65 }
66
67 public static final String GLOBAL_LINK_TABLE_NAME = "G:Link";
68
69 // must not re-order enum members, ordinal will be sent over wire
70 public enum STATUS {
71 INACTIVE, ACTIVE;
72 }
73
74 private final SwitchPort src;
75 private final SwitchPort dst;
76 private STATUS status;
77
78 public static byte[] getLinkID(final Long src_dpid, final Long src_port_no,
79 final Long dst_dpid, final Long dst_port_no) {
80 return LinkEvent.getLinkID(src_dpid, src_port_no, dst_dpid,
81 dst_port_no).array();
82 }
83
84 public static long[] getLinkTupleFromKey(final byte[] key) {
85 return getLinkTupleFromKey(ByteBuffer.wrap(key));
86 }
87
88 public static long[] getLinkTupleFromKey(final ByteBuffer keyBuf) {
89 long[] tuple = new long[4];
90 if (keyBuf.getChar() != 'L') {
91 throw new IllegalArgumentException("Invalid Link key");
92 }
93 long[] src_port_pair = KVPort.getPortPairFromKey(keyBuf.slice());
94 keyBuf.position(2 + PortEvent.PORTID_BYTES);
95 long[] dst_port_pair = KVPort.getPortPairFromKey(keyBuf.slice());
96
97 tuple[0] = src_port_pair[0];
98 tuple[1] = src_port_pair[1];
99 tuple[2] = dst_port_pair[0];
100 tuple[3] = dst_port_pair[1];
101
102 return tuple;
103 }
104
105 public KVLink(final Long src_dpid, final Long src_port_no,
106 final Long dst_dpid, final Long dst_port_no) {
107 super(DataStoreClient.getClient().getTable(GLOBAL_LINK_TABLE_NAME), getLinkID(src_dpid,
108 src_port_no, dst_dpid, dst_port_no));
109
110 src = new SwitchPort(src_dpid, src_port_no);
111 dst = new SwitchPort(dst_dpid, dst_port_no);
112 status = STATUS.INACTIVE;
113 }
114
115 /**
116 * Get an instance from Key.
117 *
118 * @note You need to call `read()` to get the DB content.
119 * @param key
120 * @return KVLink instance
121 */
122 public static KVLink createFromKey(final byte[] key) {
123 long[] linkTuple = getLinkTupleFromKey(key);
124 return new KVLink(linkTuple[0], linkTuple[1], linkTuple[2],
125 linkTuple[3]);
126 }
127
128 public static Iterable<KVLink> getAllLinks() {
129 return new LinkEnumerator();
130 }
131
132 public static class LinkEnumerator implements Iterable<KVLink> {
133
134 @Override
135 public Iterator<KVLink> iterator() {
136 return new LinkIterator();
137 }
138 }
139
140 public static class LinkIterator extends AbstractObjectIterator<KVLink> {
141
142 public LinkIterator() {
143 super(DataStoreClient.getClient().getTable(GLOBAL_LINK_TABLE_NAME));
144 }
145
146 @Override
147 public KVLink next() {
148 IKVEntry o = enumerator.next();
149 KVLink e = KVLink.createFromKey(o.getKey());
150 e.deserialize(o.getValue(), o.getVersion());
151 return e;
152 }
153 }
154
155 public STATUS getStatus() {
156 return status;
157 }
158
159 public void setStatus(final STATUS status) {
160 this.status = status;
161 }
162
163 public SwitchPort getSrc() {
164 return src;
165 }
166
167 public SwitchPort getDst() {
168 return dst;
169 }
170
171 public byte[] getId() {
172 return getKey();
173 }
174
175 @Override
176 public byte[] serialize() {
177 Map<Object, Object> map = getPropertyMap();
178
179 LinkProperty.Builder link = LinkProperty.newBuilder();
180 link.setSrcSwId(ByteString.copyFrom(src.getSwitchID()));
181 link.setSrcPortId(ByteString.copyFrom(src.getPortID()));
182 link.setDstSwId(ByteString.copyFrom(dst.getSwitchID()));
183 link.setDstPortId(ByteString.copyFrom(dst.getPortID()));
184 link.setStatus(status.ordinal());
185
186 if (!map.isEmpty()) {
187 byte[] propMaps = serializePropertyMap(linkKryo.get(), map);
188 link.setValue(ByteString.copyFrom(propMaps));
189 }
190
191 return link.build().toByteArray();
192 }
193
194 @Override
195 protected boolean deserialize(final byte[] bytes) {
196 try {
197 boolean success = true;
198
199 LinkProperty link = LinkProperty.parseFrom(bytes);
200 byte[] props = link.getValue().toByteArray();
201 success &= deserializePropertyMap(linkKryo.get(), props);
202 this.status = STATUS.values()[link.getStatus()];
203
204 return success;
205 } catch (InvalidProtocolBufferException e) {
206 log.error("Deserializing Link: " + this + " failed.", e);
207 return false;
208 }
209 }
210
211 @Override
212 public String toString() {
213 // TODO output all properties?
214 return "[" + this.getClass().getSimpleName()
215 + " " + src + "->" + dst + " STATUS:" + status + "]";
216 }
217}