blob: 3b6686483f2c9967fac8138e4940c7e6c720165f [file] [log] [blame]
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -07001package net.onrc.onos.datastore.topology;
2
3import java.nio.ByteBuffer;
4import java.util.HashMap;
5import java.util.Iterator;
6import java.util.Map;
7
8import net.onrc.onos.datastore.DataStoreClient;
9import net.onrc.onos.datastore.IKVTable.IKVEntry;
10import net.onrc.onos.datastore.RCProtos.PortProperty;
11import net.onrc.onos.datastore.utils.ByteArrayUtil;
12import net.onrc.onos.datastore.utils.KVObject;
13import net.onrc.onos.ofcontroller.networkgraph.PortEvent;
14
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.
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 */
29public 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>() {
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 }
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 {
51 INACTIVE, ACTIVE;
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) {
60 return PortEvent.getPortID(dpid, number).array();
61 }
62
63 public static long[] getPortPairFromKey(final byte[] key) {
64 return getPortPairFromKey(ByteBuffer.wrap(key));
65 }
66
67 public static long[] getPortPairFromKey(final ByteBuffer keyBuf) {
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;
82
83 }
84
85 public static long getDpidFromKey(final byte[] key) {
86 return getPortPairFromKey(key)[0];
87 }
88
89 public static long getNumberFromKey(final byte[] key) {
90 return getPortPairFromKey(key)[1];
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) {
96 super(DataStoreClient.getClient().getTable(GLOBAL_PORT_TABLE_NAME), getPortID(dpid, number));
97
98 // TODO Auto-generated constructor stub
99
100 this.dpid = dpid;
101 this.number = number;
102 this.status = STATUS.INACTIVE;
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) {
113 long[] pair = getPortPairFromKey(key);
114 return new KVPort(pair[0], pair[1]);
115 }
116
117 public static Iterable<KVPort> getAllPorts() {
118 return new PortEnumerator();
119 }
120
121 public static class PortEnumerator implements Iterable<KVPort> {
122
123 @Override
124 public Iterator<KVPort> iterator() {
125 return new PortIterator();
126 }
127 }
128
129 public static class PortIterator extends AbstractObjectIterator<KVPort> {
130
131 public PortIterator() {
132 super(DataStoreClient.getClient().getTable(GLOBAL_PORT_TABLE_NAME));
133 }
134
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 }
142 }
143
144 public STATUS getStatus() {
145 return status;
146 }
147
148 public void setStatus(final STATUS status) {
149 this.status = status;
150 }
151
152 public Long getDpid() {
153 return dpid;
154 }
155
156 public Long getNumber() {
157 return number;
158 }
159
160 public byte[] getId() {
161 return getKey();
162 }
163
164 @Override
165 public byte[] serialize() {
166 Map<Object, Object> map = getPropertyMap();
167
168 PortProperty.Builder port = PortProperty.newBuilder();
169 port.setDpid(dpid);
170 port.setNumber(number);
171 port.setStatus(status.ordinal());
172
173 if (!map.isEmpty()) {
174 byte[] propMaps = serializePropertyMap(portKryo.get(), map);
175 port.setValue(ByteString.copyFrom(propMaps));
176 }
177
178 return port.build().toByteArray();
179 }
180
181 @Override
182 protected boolean deserialize(final byte[] bytes) {
183 try {
184 boolean success = true;
185
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()];
190
191 return success;
192 } catch (InvalidProtocolBufferException e) {
193 log.error("Deserializing Port: " + this + " failed.", e);
194 return false;
195 }
196 }
197
198 @Override
199 public String toString() {
200 // TODO output all properties?
201 return "[" + this.getClass().getSimpleName()
202 + " 0x" + Long.toHexString(dpid) + "@" + number
203 + " STATUS:" + status + "]";
204 }
205}