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