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; |
Yuta HIGUCHI | 1cd9029 | 2014-04-03 14:31:10 -0700 | [diff] [blame] | 10 | import net.onrc.onos.core.datastore.serializers.Topology.SwitchProperty; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 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.SwitchEvent; |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 13 | import net.onrc.onos.core.util.Dpid; |
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 | * Switch object in data store. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 24 | * <p/> |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 25 | * Note: This class will not maintain invariants. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 26 | * e.g., It will NOT automatically remove Ports on Switch, |
| 27 | * when deleting a Switch. |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 28 | */ |
| 29 | public class KVSwitch extends KVObject { |
| 30 | private static final Logger log = LoggerFactory.getLogger(KVSwitch.class); |
| 31 | |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 32 | private static final ThreadLocal<Kryo> SWITCH_KRYO = 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 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 47 | static final String SWITCH_TABLE_SUFFIX = ":Switch"; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 48 | |
| 49 | // must not re-order enum members, ordinal will be sent over wire |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Status. |
| 52 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 53 | public enum STATUS { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 54 | INACTIVE, ACTIVE; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | private final Long dpid; |
| 58 | private STATUS status; |
| 59 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 60 | /** |
| 61 | * Generate a SwitchID from dpid. |
| 62 | * |
| 63 | * @param dpid dpid of the switch |
| 64 | * @return SwitchID |
| 65 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 66 | public static byte[] getSwitchID(final Long dpid) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 67 | return SwitchEvent.getSwitchID(dpid).array(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 70 | /** |
| 71 | * Gets the DPID from SwitchID. |
| 72 | * |
| 73 | * @param key SwitchID |
| 74 | * @return dpid |
| 75 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 76 | public static long getDpidFromKey(final byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 77 | return getDpidFromKey(ByteBuffer.wrap(key)); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 80 | /** |
| 81 | * Gets the DPID from SwitchID. |
| 82 | * |
| 83 | * @param keyBuf SwitchID |
| 84 | * @return dpid |
| 85 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 86 | public static long getDpidFromKey(final ByteBuffer keyBuf) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 87 | if (keyBuf.getChar() != 'S') { |
| 88 | throw new IllegalArgumentException("Invalid Switch key"); |
| 89 | } |
| 90 | return keyBuf.getLong(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 93 | // FIXME Should the parameter be DPID here, or should caller specify the key? |
| 94 | // Should layer above have the control of the ID computation/generation? |
| 95 | /** |
| 96 | * KVSwitch constructor for default namespace. |
| 97 | * |
| 98 | * @param dpid dpid of this switch |
| 99 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 100 | public KVSwitch(final Long dpid) { |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 101 | this(dpid, KVObject.DEFAULT_NAMESPACE); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 105 | * KVSwitch constructor for specified namespace. |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 106 | * |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 107 | * @param dpid dpid of this switch |
| 108 | * @param namespace namespace to create this object |
| 109 | */ |
| 110 | public KVSwitch(final Long dpid, final String namespace) { |
| 111 | super(DataStoreClient.getClient() |
| 112 | .getTable(namespace + SWITCH_TABLE_SUFFIX), |
| 113 | getSwitchID(dpid), namespace); |
| 114 | |
| 115 | this.dpid = dpid; |
| 116 | this.status = STATUS.INACTIVE; |
| 117 | // may need to store namespace here or at KVObject. |
| 118 | } |
| 119 | |
| 120 | /** |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 121 | * KVSwitch constructor for default namespace. |
| 122 | * |
| 123 | * @param dpid dpid of this switch |
| 124 | */ |
| 125 | public KVSwitch(final Dpid dpid) { |
| 126 | this(dpid, KVObject.DEFAULT_NAMESPACE); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * KVSwitch constructor for specified namespace. |
| 131 | * |
| 132 | * @param dpid dpid of this switch |
| 133 | * @param namespace namespace to create this object |
| 134 | */ |
| 135 | public KVSwitch(final Dpid dpid, final String namespace) { |
| 136 | this(dpid.value(), namespace); |
| 137 | } |
| 138 | |
| 139 | /** |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 140 | * Gets an instance from SwitchID in default namespace. |
Jonathan Hart | 99ff20a | 2014-06-15 16:53:00 -0700 | [diff] [blame] | 141 | * <p/> |
| 142 | * Note: You need to call `read()` to get the DB content. |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 143 | * |
| 144 | * @param key SwitchID |
| 145 | * @return {@link KVSwitch} instance |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 146 | */ |
| 147 | public static KVSwitch createFromKey(final byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 148 | return new KVSwitch(getDpidFromKey(key)); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 151 | /** |
| 152 | * Gets an instance from SwitchID in specified namespace. |
Jonathan Hart | 99ff20a | 2014-06-15 16:53:00 -0700 | [diff] [blame] | 153 | * <p/> |
| 154 | * Note: You need to call `read()` to get the DB content. |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 155 | * |
| 156 | * @param key SwitchID |
| 157 | * @param namespace namespace to create this object |
| 158 | * @return {@link KVSwitch} instance |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 159 | */ |
| 160 | public static KVSwitch createFromKey(final byte[] key, final String namespace) { |
| 161 | return new KVSwitch(getDpidFromKey(key), namespace); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 164 | /** |
| 165 | * Gets all the switches in default namespace. |
| 166 | * |
| 167 | * @return All the {@link KVSwitch} |
| 168 | */ |
| 169 | public static Iterable<KVSwitch> getAllSwitches() { |
| 170 | return getAllSwitches(DEFAULT_NAMESPACE); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Gets all the switches in specified namespace. |
| 175 | * |
| 176 | * @param namespace Namespace to get all switches. |
| 177 | * @return All the {@link KVSwitch} |
| 178 | */ |
| 179 | public static Iterable<KVSwitch> getAllSwitches(final String namespace) { |
| 180 | return new SwitchEnumerator(namespace); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Utility class to provide Iterable interface. |
| 185 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 186 | public static class SwitchEnumerator implements Iterable<KVSwitch> { |
| 187 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 188 | private final String namespace; |
| 189 | |
| 190 | /** |
| 191 | * Constructor to iterate Ports in specified namespace. |
| 192 | * |
| 193 | * @param namespace namespace to iterate through |
| 194 | */ |
| 195 | public SwitchEnumerator(final String namespace) { |
| 196 | this.namespace = namespace; |
| 197 | } |
| 198 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 199 | @Override |
| 200 | public Iterator<KVSwitch> iterator() { |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 201 | return new SwitchIterator(namespace); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 202 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 205 | /** |
| 206 | * Utility class to provide Iterator over all the Switch objects. |
| 207 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 208 | public static class SwitchIterator extends AbstractObjectIterator<KVSwitch> { |
| 209 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 210 | /** |
| 211 | * Constructor to create an iterator to iterate all the Switches |
| 212 | * in specified namespace. |
| 213 | * |
| 214 | * @param namespace namespace to iterate through |
| 215 | */ |
| 216 | public SwitchIterator(final String namespace) { |
| 217 | super(DataStoreClient.getClient() |
| 218 | .getTable(namespace + SWITCH_TABLE_SUFFIX), namespace); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 219 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 220 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 221 | @Override |
| 222 | public KVSwitch next() { |
| 223 | IKVEntry o = enumerator.next(); |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 224 | KVSwitch e = KVSwitch.createFromKey(o.getKey(), namespace); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 225 | e.deserialize(o.getValue(), o.getVersion()); |
| 226 | return e; |
| 227 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 230 | /** |
| 231 | * Gets the status. |
| 232 | * |
| 233 | * @return status |
| 234 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 235 | public STATUS getStatus() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 236 | return status; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 239 | /** |
| 240 | * Sets the status. |
| 241 | * |
| 242 | * @param status new status |
| 243 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 244 | public void setStatus(final STATUS status) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 245 | this.status = status; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 248 | /** |
| 249 | * Gets the DPID of the switch this port is on. |
| 250 | * |
| 251 | * @return DPID of the switch this port is on |
| 252 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 253 | public Long getDpid() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 254 | return dpid; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Yuta HIGUCHI | cc56188 | 2014-05-22 10:31:32 -0700 | [diff] [blame] | 257 | /** |
| 258 | * Gets the SwitchID. |
| 259 | * |
| 260 | * @return SwitchID |
| 261 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 262 | public byte[] getId() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 263 | return getKey(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public byte[] serialize() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 268 | Map<Object, Object> map = getPropertyMap(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 269 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 270 | SwitchProperty.Builder sw = SwitchProperty.newBuilder(); |
| 271 | sw.setDpid(dpid); |
| 272 | sw.setStatus(status.ordinal()); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 273 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 274 | if (!map.isEmpty()) { |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 275 | byte[] propMaps = serializePropertyMap(SWITCH_KRYO.get(), map); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 276 | sw.setValue(ByteString.copyFrom(propMaps)); |
| 277 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 278 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 279 | return sw.build().toByteArray(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | @Override |
| 283 | protected boolean deserialize(final byte[] bytes) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 284 | try { |
| 285 | boolean success = true; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 286 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 287 | SwitchProperty sw = SwitchProperty.parseFrom(bytes); |
| 288 | byte[] props = sw.getValue().toByteArray(); |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 289 | success &= deserializePropertyMap(SWITCH_KRYO.get(), props); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 290 | this.status = STATUS.values()[sw.getStatus()]; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 291 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 292 | return success; |
| 293 | } catch (InvalidProtocolBufferException e) { |
| 294 | log.error("Deserializing Switch: " + this + " failed.", e); |
| 295 | return false; |
| 296 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | @Override |
| 300 | public String toString() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 301 | // TODO output all properties? |
| 302 | return "[" + this.getClass().getSimpleName() |
| 303 | + " 0x" + Long.toHexString(dpid) + " STATUS:" + status + "]"; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | } |