Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 1 | package net.onrc.onos.datastore.topology; |
| 2 | |
| 3 | import java.nio.ByteBuffer; |
| 4 | import java.util.HashMap; |
| 5 | import java.util.Iterator; |
| 6 | import java.util.Map; |
| 7 | |
| 8 | import net.onrc.onos.datastore.DataStoreClient; |
| 9 | import net.onrc.onos.datastore.IKVTable.IKVEntry; |
| 10 | import net.onrc.onos.datastore.RCProtos.PortProperty; |
| 11 | import net.onrc.onos.datastore.utils.ByteArrayUtil; |
| 12 | import net.onrc.onos.datastore.utils.KVObject; |
| 13 | import net.onrc.onos.ofcontroller.networkgraph.PortEvent; |
| 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 | * Port object in data store. |
| 24 | * |
| 25 | * Note: This class will not maintain invariants. |
| 26 | * e.g., It will NOT automatically remove Links or Devices on Port, |
| 27 | * when deleting a Port. |
| 28 | */ |
| 29 | public class KVPort extends KVObject { |
| 30 | private static final Logger log = LoggerFactory.getLogger(KVPort.class); |
| 31 | |
| 32 | private static final ThreadLocal<Kryo> portKryo = new ThreadLocal<Kryo>() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 33 | @Override |
| 34 | protected Kryo initialValue() { |
| 35 | Kryo kryo = new Kryo(); |
| 36 | kryo.setRegistrationRequired(true); |
| 37 | kryo.setReferences(false); |
| 38 | kryo.register(byte[].class); |
| 39 | kryo.register(byte[][].class); |
| 40 | kryo.register(HashMap.class); |
| 41 | // TODO check if we should explicitly specify EnumSerializer |
| 42 | kryo.register(STATUS.class); |
| 43 | return kryo; |
| 44 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | public static final String GLOBAL_PORT_TABLE_NAME = "G:Port"; |
| 48 | |
| 49 | // must not re-order enum members, ordinal will be sent over wire |
| 50 | public enum STATUS { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 51 | INACTIVE, ACTIVE; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | private final Long dpid; |
| 55 | private final Long number; |
| 56 | |
| 57 | private STATUS status; |
| 58 | |
| 59 | public static byte[] getPortID(final Long dpid, final Long number) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 60 | return PortEvent.getPortID(dpid, number).array(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | public static long[] getPortPairFromKey(final byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 64 | return getPortPairFromKey(ByteBuffer.wrap(key)); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | public static long[] getPortPairFromKey(final ByteBuffer keyBuf) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 68 | long[] pair = new long[2]; |
| 69 | if (keyBuf.getChar() != 'S') { |
| 70 | throw new IllegalArgumentException("Invalid Port key:" + keyBuf |
| 71 | + " " |
| 72 | + ByteArrayUtil.toHexStringBuffer(keyBuf.array(), ":")); |
| 73 | } |
| 74 | pair[0] = keyBuf.getLong(); |
| 75 | if (keyBuf.getChar() != 'P') { |
| 76 | throw new IllegalArgumentException("Invalid Port key:" + keyBuf |
| 77 | + " " |
| 78 | + ByteArrayUtil.toHexStringBuffer(keyBuf.array(), ":")); |
| 79 | } |
| 80 | pair[1] = keyBuf.getLong(); |
| 81 | return pair; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 82 | |
| 83 | } |
| 84 | |
| 85 | public static long getDpidFromKey(final byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 86 | return getPortPairFromKey(key)[0]; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | public static long getNumberFromKey(final byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 90 | return getPortPairFromKey(key)[1]; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | // FIXME specify DPID,number here, or Should caller specify the key it self? |
| 94 | // In other words, should layer above have the control of the ID? |
| 95 | public KVPort(final Long dpid, final Long number) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 96 | super(DataStoreClient.getClient().getTable(GLOBAL_PORT_TABLE_NAME), getPortID(dpid, number)); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 97 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 98 | // TODO Auto-generated constructor stub |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 99 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 100 | this.dpid = dpid; |
| 101 | this.number = number; |
| 102 | this.status = STATUS.INACTIVE; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Get an instance from Key. |
| 107 | * |
| 108 | * @note You need to call `read()` to get the DB content. |
| 109 | * @param key |
| 110 | * @return KVPort instance |
| 111 | */ |
| 112 | public static KVPort createFromKey(final byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 113 | long[] pair = getPortPairFromKey(key); |
| 114 | return new KVPort(pair[0], pair[1]); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | public static Iterable<KVPort> getAllPorts() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 118 | return new PortEnumerator(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | public static class PortEnumerator implements Iterable<KVPort> { |
| 122 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 123 | @Override |
| 124 | public Iterator<KVPort> iterator() { |
| 125 | return new PortIterator(); |
| 126 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | public static class PortIterator extends AbstractObjectIterator<KVPort> { |
| 130 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 131 | public PortIterator() { |
| 132 | super(DataStoreClient.getClient().getTable(GLOBAL_PORT_TABLE_NAME)); |
| 133 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 134 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 135 | @Override |
| 136 | public KVPort next() { |
| 137 | IKVEntry o = enumerator.next(); |
| 138 | KVPort e = KVPort.createFromKey(o.getKey()); |
| 139 | e.deserialize(o.getValue(), o.getVersion()); |
| 140 | return e; |
| 141 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | public STATUS getStatus() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 145 | return status; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | public void setStatus(final STATUS status) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 149 | this.status = status; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | public Long getDpid() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 153 | return dpid; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | public Long getNumber() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 157 | return number; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | public byte[] getId() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 161 | return getKey(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public byte[] serialize() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 166 | Map<Object, Object> map = getPropertyMap(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 167 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 168 | PortProperty.Builder port = PortProperty.newBuilder(); |
| 169 | port.setDpid(dpid); |
| 170 | port.setNumber(number); |
| 171 | port.setStatus(status.ordinal()); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 172 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 173 | if (!map.isEmpty()) { |
| 174 | byte[] propMaps = serializePropertyMap(portKryo.get(), map); |
| 175 | port.setValue(ByteString.copyFrom(propMaps)); |
| 176 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 177 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 178 | return port.build().toByteArray(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | @Override |
| 182 | protected boolean deserialize(final byte[] bytes) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 183 | try { |
| 184 | boolean success = true; |
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 | PortProperty port = PortProperty.parseFrom(bytes); |
| 187 | byte[] props = port.getValue().toByteArray(); |
| 188 | success &= deserializePropertyMap(portKryo.get(), props); |
| 189 | this.status = STATUS.values()[port.getStatus()]; |
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 success; |
| 192 | } catch (InvalidProtocolBufferException e) { |
| 193 | log.error("Deserializing Port: " + this + " failed.", e); |
| 194 | return false; |
| 195 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public String toString() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame^] | 200 | // TODO output all properties? |
| 201 | return "[" + this.getClass().getSimpleName() |
| 202 | + " 0x" + Long.toHexString(dpid) + "@" + number |
| 203 | + " STATUS:" + status + "]"; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 204 | } |
| 205 | } |