Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.datastore.topology; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 2 | |
| 3 | import java.nio.ByteBuffer; |
| 4 | import java.util.HashMap; |
| 5 | import java.util.Iterator; |
| 6 | import java.util.Map; |
| 7 | |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 8 | import net.onrc.onos.core.datastore.DataStoreClient; |
| 9 | import net.onrc.onos.core.datastore.IKVTable.IKVEntry; |
| 10 | import net.onrc.onos.core.datastore.RCProtos.LinkProperty; |
| 11 | import net.onrc.onos.core.datastore.utils.KVObject; |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 12 | import net.onrc.onos.core.topology.LinkEvent; |
| 13 | import net.onrc.onos.core.topology.PortEvent; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 14 | |
| 15 | import org.slf4j.Logger; |
| 16 | import org.slf4j.LoggerFactory; |
| 17 | |
| 18 | import com.esotericsoftware.kryo.Kryo; |
| 19 | import com.google.protobuf.ByteString; |
| 20 | import com.google.protobuf.InvalidProtocolBufferException; |
| 21 | |
| 22 | /** |
| 23 | * Link object in data store. |
| 24 | */ |
| 25 | public 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>() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 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 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | public static class SwitchPort { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 44 | public final Long dpid; |
| 45 | public final Long number; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 46 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 47 | public SwitchPort(final Long dpid, final Long number) { |
| 48 | this.dpid = dpid; |
| 49 | this.number = number; |
| 50 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 51 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 52 | public byte[] getPortID() { |
| 53 | return KVPort.getPortID(dpid, number); |
| 54 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 55 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 56 | public byte[] getSwitchID() { |
| 57 | return KVSwitch.getSwitchID(dpid); |
| 58 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 59 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 60 | @Override |
| 61 | public String toString() { |
| 62 | return "(" + Long.toHexString(dpid) + "@" + number + ")"; |
| 63 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 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 { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 71 | INACTIVE, ACTIVE; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 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, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 79 | final Long dst_dpid, final Long dst_port_no) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 80 | return LinkEvent.getLinkID(src_dpid, src_port_no, dst_dpid, |
| 81 | dst_port_no).array(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | public static long[] getLinkTupleFromKey(final byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 85 | return getLinkTupleFromKey(ByteBuffer.wrap(key)); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | public static long[] getLinkTupleFromKey(final ByteBuffer keyBuf) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 89 | if (keyBuf.getChar() != 'L') { |
| 90 | throw new IllegalArgumentException("Invalid Link key"); |
| 91 | } |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 92 | final long[] srcPortPair = KVPort.getPortPairFromKey(keyBuf.slice()); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 93 | keyBuf.position(2 + PortEvent.PORTID_BYTES); |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 94 | final long[] dstPortPair = KVPort.getPortPairFromKey(keyBuf.slice()); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 95 | |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 96 | long[] tuple = new long[4]; |
| 97 | tuple[0] = srcPortPair[0]; |
| 98 | tuple[1] = srcPortPair[1]; |
| 99 | tuple[2] = dstPortPair[0]; |
| 100 | tuple[3] = dstPortPair[1]; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 101 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 102 | return tuple; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | public KVLink(final Long src_dpid, final Long src_port_no, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 106 | final Long dst_dpid, final Long dst_port_no) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 107 | super(DataStoreClient.getClient().getTable(GLOBAL_LINK_TABLE_NAME), getLinkID(src_dpid, |
| 108 | src_port_no, dst_dpid, dst_port_no)); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 109 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 110 | src = new SwitchPort(src_dpid, src_port_no); |
| 111 | dst = new SwitchPort(dst_dpid, dst_port_no); |
| 112 | status = STATUS.INACTIVE; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Get an instance from Key. |
| 117 | * |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 118 | * @param key |
| 119 | * @return KVLink instance |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 120 | * @note You need to call `read()` to get the DB content. |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 121 | */ |
| 122 | public static KVLink createFromKey(final byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 123 | long[] linkTuple = getLinkTupleFromKey(key); |
| 124 | return new KVLink(linkTuple[0], linkTuple[1], linkTuple[2], |
| 125 | linkTuple[3]); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | public static Iterable<KVLink> getAllLinks() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 129 | return new LinkEnumerator(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | public static class LinkEnumerator implements Iterable<KVLink> { |
| 133 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 134 | @Override |
| 135 | public Iterator<KVLink> iterator() { |
| 136 | return new LinkIterator(); |
| 137 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | public static class LinkIterator extends AbstractObjectIterator<KVLink> { |
| 141 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 142 | public LinkIterator() { |
| 143 | super(DataStoreClient.getClient().getTable(GLOBAL_LINK_TABLE_NAME)); |
| 144 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 145 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 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 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | public STATUS getStatus() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 156 | return status; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | public void setStatus(final STATUS status) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 160 | this.status = status; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | public SwitchPort getSrc() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 164 | return src; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | public SwitchPort getDst() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 168 | return dst; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | public byte[] getId() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 172 | return getKey(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public byte[] serialize() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 177 | Map<Object, Object> map = getPropertyMap(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 178 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 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()); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 185 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 186 | if (!map.isEmpty()) { |
| 187 | byte[] propMaps = serializePropertyMap(linkKryo.get(), map); |
| 188 | link.setValue(ByteString.copyFrom(propMaps)); |
| 189 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 190 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 191 | return link.build().toByteArray(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | @Override |
| 195 | protected boolean deserialize(final byte[] bytes) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 196 | try { |
| 197 | boolean success = true; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 198 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 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()]; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 203 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 204 | return success; |
| 205 | } catch (InvalidProtocolBufferException e) { |
| 206 | log.error("Deserializing Link: " + this + " failed.", e); |
| 207 | return false; |
| 208 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public String toString() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 213 | // TODO output all properties? |
| 214 | return "[" + this.getClass().getSimpleName() |
| 215 | + " " + src + "->" + dst + " STATUS:" + status + "]"; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 216 | } |
| 217 | } |