blob: 5721b78922f8f63bd0eda2609e19aced78987766 [file] [log] [blame]
Jonathan Hart6df90172014-04-03 10:13:11 -07001package net.onrc.onos.core.datastore.topology;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -07002
3import java.nio.ByteBuffer;
4import java.util.HashMap;
5import java.util.Iterator;
6import java.util.Map;
7
Jonathan Hart6df90172014-04-03 10:13:11 -07008import net.onrc.onos.core.datastore.DataStoreClient;
9import net.onrc.onos.core.datastore.IKVTable.IKVEntry;
Yuta HIGUCHI1cd90292014-04-03 14:31:10 -070010import net.onrc.onos.core.datastore.serializers.Topology.PortProperty;
Jonathan Hart6df90172014-04-03 10:13:11 -070011import net.onrc.onos.core.datastore.utils.ByteArrayUtil;
12import net.onrc.onos.core.datastore.utils.KVObject;
Jonathan Hart472062d2014-04-03 10:56:48 -070013import net.onrc.onos.core.topology.PortEvent;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070014
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 * Port object in data store.
Ray Milkey269ffb92014-04-03 14:43:30 -070024 * <p/>
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070025 * Note: This class will not maintain invariants.
Ray Milkey269ffb92014-04-03 14:43:30 -070026 * e.g., It will NOT automatically remove Links or Devices on Port,
27 * when deleting a Port.
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070028 */
29public class KVPort extends KVObject {
30 private static final Logger log = LoggerFactory.getLogger(KVPort.class);
31
Ray Milkey5c9f2db2014-04-09 10:31:21 -070032 private static final ThreadLocal<Kryo> PORT_KRYO = new ThreadLocal<Kryo>() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070033 @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 HIGUCHI66ca1bf2014-03-12 18:34:09 -070045 };
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 HIGUCHI826b4a42014-03-24 13:10:33 -070051 INACTIVE, ACTIVE;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070052 }
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 HIGUCHI826b4a42014-03-24 13:10:33 -070060 return PortEvent.getPortID(dpid, number).array();
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070061 }
62
63 public static long[] getPortPairFromKey(final byte[] key) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070064 return getPortPairFromKey(ByteBuffer.wrap(key));
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070065 }
66
67 public static long[] getPortPairFromKey(final ByteBuffer keyBuf) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070068 if (keyBuf.getChar() != 'S') {
69 throw new IllegalArgumentException("Invalid Port key:" + keyBuf
70 + " "
71 + ByteArrayUtil.toHexStringBuffer(keyBuf.array(), ":"));
72 }
Yuta HIGUCHIa14eb172014-03-24 15:03:23 -070073 long[] pair = new long[2];
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070074 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 HIGUCHI66ca1bf2014-03-12 18:34:09 -070082
83 }
84
85 public static long getDpidFromKey(final byte[] key) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070086 return getPortPairFromKey(key)[0];
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070087 }
88
89 public static long getNumberFromKey(final byte[] key) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070090 return getPortPairFromKey(key)[1];
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070091 }
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 HIGUCHI826b4a42014-03-24 13:10:33 -070096 super(DataStoreClient.getClient().getTable(GLOBAL_PORT_TABLE_NAME), getPortID(dpid, number));
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070097
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070098 // TODO Auto-generated constructor stub
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070099
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700100 this.dpid = dpid;
101 this.number = number;
102 this.status = STATUS.INACTIVE;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700103 }
104
105 /**
106 * Get an instance from Key.
107 *
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700108 * @param key
109 * @return KVPort instance
Ray Milkey269ffb92014-04-03 14:43:30 -0700110 * @note You need to call `read()` to get the DB content.
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700111 */
112 public static KVPort createFromKey(final byte[] key) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700113 long[] pair = getPortPairFromKey(key);
114 return new KVPort(pair[0], pair[1]);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700115 }
116
117 public static Iterable<KVPort> getAllPorts() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700118 return new PortEnumerator();
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700119 }
120
121 public static class PortEnumerator implements Iterable<KVPort> {
122
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700123 @Override
124 public Iterator<KVPort> iterator() {
125 return new PortIterator();
126 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700127 }
128
129 public static class PortIterator extends AbstractObjectIterator<KVPort> {
130
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700131 public PortIterator() {
132 super(DataStoreClient.getClient().getTable(GLOBAL_PORT_TABLE_NAME));
133 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700134
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700135 @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 HIGUCHI66ca1bf2014-03-12 18:34:09 -0700142 }
143
144 public STATUS getStatus() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700145 return status;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700146 }
147
148 public void setStatus(final STATUS status) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700149 this.status = status;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700150 }
151
152 public Long getDpid() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700153 return dpid;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700154 }
155
156 public Long getNumber() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700157 return number;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700158 }
159
160 public byte[] getId() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700161 return getKey();
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700162 }
163
164 @Override
165 public byte[] serialize() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700166 Map<Object, Object> map = getPropertyMap();
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700167
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700168 PortProperty.Builder port = PortProperty.newBuilder();
169 port.setDpid(dpid);
170 port.setNumber(number);
171 port.setStatus(status.ordinal());
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700172
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700173 if (!map.isEmpty()) {
Ray Milkey5c9f2db2014-04-09 10:31:21 -0700174 byte[] propMaps = serializePropertyMap(PORT_KRYO.get(), map);
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700175 port.setValue(ByteString.copyFrom(propMaps));
176 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700177
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700178 return port.build().toByteArray();
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700179 }
180
181 @Override
182 protected boolean deserialize(final byte[] bytes) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700183 try {
184 boolean success = true;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700185
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700186 PortProperty port = PortProperty.parseFrom(bytes);
187 byte[] props = port.getValue().toByteArray();
Ray Milkey5c9f2db2014-04-09 10:31:21 -0700188 success &= deserializePropertyMap(PORT_KRYO.get(), props);
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700189 this.status = STATUS.values()[port.getStatus()];
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700190
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700191 return success;
192 } catch (InvalidProtocolBufferException e) {
193 log.error("Deserializing Port: " + this + " failed.", e);
194 return false;
195 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700196 }
197
198 @Override
199 public String toString() {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -0700200 // TODO output all properties?
201 return "[" + this.getClass().getSimpleName()
202 + " 0x" + Long.toHexString(dpid) + "@" + number
203 + " STATUS:" + status + "]";
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700204 }
205}