blob: 635f1a1cdb6ef6ea3ca62b0add272d5a58c05087 [file] [log] [blame]
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.ovsdb.controller.driver;
17
andreaed976a42015-10-05 14:38:25 -070018import com.fasterxml.jackson.databind.JsonNode;
19import com.google.common.base.Function;
Pier Ventref5d72362016-07-17 12:02:14 +020020import com.google.common.collect.ImmutableList;
21import com.google.common.collect.ImmutableSet;
andreaed976a42015-10-05 14:38:25 -070022import com.google.common.collect.Lists;
23import com.google.common.collect.Maps;
24import com.google.common.collect.Sets;
25import com.google.common.util.concurrent.Futures;
26import com.google.common.util.concurrent.ListenableFuture;
27import com.google.common.util.concurrent.SettableFuture;
lishuai6c56f5e2015-11-17 16:38:19 +080028
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070029import io.netty.channel.Channel;
lishuai6c56f5e2015-11-17 16:38:19 +080030
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070031import org.onlab.packet.IpAddress;
andreaed976a42015-10-05 14:38:25 -070032import org.onosproject.net.DeviceId;
Hyunsun Moon1251e192016-06-07 16:57:05 -070033import org.onosproject.net.behaviour.BridgeDescription;
andreaed976a42015-10-05 14:38:25 -070034import org.onosproject.net.behaviour.ControllerInfo;
Pier Ventref5d72362016-07-17 12:02:14 +020035import org.onosproject.net.behaviour.MirroringStatistics;
36import org.onosproject.net.behaviour.MirroringName;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070037import org.onosproject.ovsdb.controller.OvsdbBridge;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070038import org.onosproject.ovsdb.controller.OvsdbClientService;
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070039import org.onosproject.ovsdb.controller.OvsdbInterface;
40import org.onosproject.ovsdb.controller.OvsdbInterface.Type;
Pier Ventref5d72362016-07-17 12:02:14 +020041import org.onosproject.ovsdb.controller.OvsdbMirror;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070042import org.onosproject.ovsdb.controller.OvsdbNodeId;
43import org.onosproject.ovsdb.controller.OvsdbPort;
44import org.onosproject.ovsdb.controller.OvsdbPortName;
45import org.onosproject.ovsdb.controller.OvsdbPortNumber;
46import org.onosproject.ovsdb.controller.OvsdbRowStore;
47import org.onosproject.ovsdb.controller.OvsdbStore;
48import org.onosproject.ovsdb.controller.OvsdbTableStore;
Pier Ventref5d72362016-07-17 12:02:14 +020049
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070050import org.onosproject.ovsdb.rfc.jsonrpc.Callback;
51import org.onosproject.ovsdb.rfc.message.OperationResult;
52import org.onosproject.ovsdb.rfc.message.TableUpdates;
53import org.onosproject.ovsdb.rfc.notation.Condition;
54import org.onosproject.ovsdb.rfc.notation.Mutation;
CNluciusa66c3972015-09-06 20:31:29 +080055import org.onosproject.ovsdb.rfc.notation.OvsdbMap;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070056import org.onosproject.ovsdb.rfc.notation.OvsdbSet;
57import org.onosproject.ovsdb.rfc.notation.Row;
Jonathan Hart51539b82015-10-29 09:53:04 -070058import org.onosproject.ovsdb.rfc.notation.Uuid;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070059import org.onosproject.ovsdb.rfc.operations.Delete;
60import org.onosproject.ovsdb.rfc.operations.Insert;
61import org.onosproject.ovsdb.rfc.operations.Mutate;
62import org.onosproject.ovsdb.rfc.operations.Operation;
63import org.onosproject.ovsdb.rfc.operations.Update;
64import org.onosproject.ovsdb.rfc.schema.ColumnSchema;
65import org.onosproject.ovsdb.rfc.schema.DatabaseSchema;
66import org.onosproject.ovsdb.rfc.schema.TableSchema;
67import org.onosproject.ovsdb.rfc.table.Bridge;
68import org.onosproject.ovsdb.rfc.table.Controller;
69import org.onosproject.ovsdb.rfc.table.Interface;
Pier Ventref5d72362016-07-17 12:02:14 +020070import org.onosproject.ovsdb.rfc.table.Mirror;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070071import org.onosproject.ovsdb.rfc.table.OvsdbTable;
72import org.onosproject.ovsdb.rfc.table.Port;
73import org.onosproject.ovsdb.rfc.table.TableGenerator;
74import org.onosproject.ovsdb.rfc.utils.ConditionUtil;
75import org.onosproject.ovsdb.rfc.utils.FromJsonUtil;
76import org.onosproject.ovsdb.rfc.utils.JsonRpcWriterUtil;
77import org.onosproject.ovsdb.rfc.utils.MutationUtil;
78import org.slf4j.Logger;
79import org.slf4j.LoggerFactory;
80
andreaed976a42015-10-05 14:38:25 -070081import java.net.InetSocketAddress;
82import java.util.ArrayList;
Pier Ventref5d72362016-07-17 12:02:14 +020083
andreaed976a42015-10-05 14:38:25 -070084import java.util.HashSet;
85import java.util.Iterator;
86import java.util.List;
87import java.util.Map;
Pier Ventref5d72362016-07-17 12:02:14 +020088import java.util.Objects;
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070089import java.util.Optional;
andreaed976a42015-10-05 14:38:25 -070090import java.util.Set;
91import java.util.concurrent.ConcurrentMap;
92import java.util.concurrent.ExecutionException;
93import java.util.concurrent.atomic.AtomicReference;
94import java.util.stream.Collectors;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070095
Hyunsun Moon1251e192016-06-07 16:57:05 -070096import static org.onosproject.ovsdb.controller.OvsdbConstant.*;
97
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070098/**
99 * An representation of an ovsdb client.
100 */
Hyunsun Moon1251e192016-06-07 16:57:05 -0700101public class DefaultOvsdbClient implements OvsdbProviderService, OvsdbClientService {
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700102
Hyunsun Moon1251e192016-06-07 16:57:05 -0700103 private final Logger log = LoggerFactory.getLogger(DefaultOvsdbClient.class);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700104
105 private Channel channel;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700106 private OvsdbAgent agent;
107 private boolean connected;
108 private OvsdbNodeId nodeId;
109 private Callback monitorCallBack;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700110 private OvsdbStore ovsdbStore = new OvsdbStore();
111
112 private final Map<String, String> requestMethod = Maps.newHashMap();
Hyunsun Moon1251e192016-06-07 16:57:05 -0700113 private final Map<String, SettableFuture<? extends Object>> requestResult = Maps.newHashMap();
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700114 private final Map<String, DatabaseSchema> schema = Maps.newHashMap();
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700115
Pier Ventref5d72362016-07-17 12:02:14 +0200116
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700117 /**
118 * Creates an OvsdbClient.
119 *
120 * @param nodeId ovsdb node id
121 */
122 public DefaultOvsdbClient(OvsdbNodeId nodeId) {
123 this.nodeId = nodeId;
124 }
125
126 @Override
127 public OvsdbNodeId nodeId() {
128 return nodeId;
129 }
130
131 @Override
132 public void setAgent(OvsdbAgent agent) {
133 if (this.agent == null) {
134 this.agent = agent;
135 }
136 }
137
138 @Override
139 public void setChannel(Channel channel) {
140 this.channel = channel;
141 }
142
143 @Override
144 public void setConnection(boolean connected) {
145 this.connected = connected;
146 }
147
148 @Override
149 public boolean isConnected() {
150 return this.connected;
151 }
152
153 @Override
154 public void nodeAdded() {
155 this.agent.addConnectedNode(nodeId, this);
156 }
157
158 @Override
159 public void nodeRemoved() {
160 this.agent.removeConnectedNode(nodeId);
161 channel.disconnect();
162 }
163
164 /**
165 * Gets the ovsdb table store.
166 *
167 * @param dbName the ovsdb database name
168 * @return ovsTableStore, empty if table store is find
169 */
170 private OvsdbTableStore getTableStore(String dbName) {
171 if (ovsdbStore == null) {
172 return null;
173 }
174 return ovsdbStore.getOvsdbTableStore(dbName);
175 }
176
177 /**
178 * Gets the ovsdb row store.
179 *
andreaed976a42015-10-05 14:38:25 -0700180 * @param dbName the ovsdb database name
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700181 * @param tableName the ovsdb table name
Hyunsun Moon6125c612015-10-15 10:54:44 -0700182 * @return ovsRowStore, empty store if no rows exist in the table
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700183 */
184 private OvsdbRowStore getRowStore(String dbName, String tableName) {
185 OvsdbTableStore tableStore = getTableStore(dbName);
186 if (tableStore == null) {
187 return null;
188 }
Hyunsun Moon6125c612015-10-15 10:54:44 -0700189
190 OvsdbRowStore rowStore = tableStore.getRows(tableName);
191 if (rowStore == null) {
192 rowStore = new OvsdbRowStore();
193 }
194 return rowStore;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700195 }
196
197 /**
198 * Gets the ovsdb row.
199 *
andreaed976a42015-10-05 14:38:25 -0700200 * @param dbName the ovsdb database name
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700201 * @param tableName the ovsdb table name
andreaed976a42015-10-05 14:38:25 -0700202 * @param uuid the key of the row
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700203 * @return row, empty if row is find
204 */
205 @Override
206 public Row getRow(String dbName, String tableName, String uuid) {
207 OvsdbTableStore tableStore = getTableStore(dbName);
208 if (tableStore == null) {
209 return null;
210 }
211 OvsdbRowStore rowStore = tableStore.getRows(tableName);
212 if (rowStore == null) {
213 return null;
214 }
215 return rowStore.getRow(uuid);
216 }
217
218 @Override
219 public void removeRow(String dbName, String tableName, String uuid) {
220 OvsdbTableStore tableStore = getTableStore(dbName);
221 if (tableStore == null) {
222 return;
223 }
224 OvsdbRowStore rowStore = tableStore.getRows(tableName);
225 if (rowStore == null) {
226 return;
227 }
228 rowStore.deleteRow(uuid);
229 }
230
231 @Override
232 public void updateOvsdbStore(String dbName, String tableName, String uuid,
233 Row row) {
234 OvsdbTableStore tableStore = ovsdbStore.getOvsdbTableStore(dbName);
235 if (tableStore == null) {
236 tableStore = new OvsdbTableStore();
237 }
238 OvsdbRowStore rowStore = tableStore.getRows(tableName);
239 if (rowStore == null) {
240 rowStore = new OvsdbRowStore();
241 }
242 rowStore.insertRow(uuid, row);
243 tableStore.createOrUpdateTable(tableName, rowStore);
244 ovsdbStore.createOrUpdateOvsdbStore(dbName, tableStore);
245 }
246
Pier Ventref5d72362016-07-17 12:02:14 +0200247 /**
248 * Gets the Mirror uuid.
249 *
250 * @param mirrorName mirror name
251 * @return mirror uuid, empty if no uuid is found
252 */
253 @Override
254 public String getMirrorUuid(String mirrorName) {
255 DatabaseSchema dbSchema = schema.get(DATABASENAME);
256 OvsdbRowStore rowStore = getRowStore(DATABASENAME, MIRROR);
257 if (rowStore == null) {
258 log.warn("The mirror uuid is null");
259 return null;
260 }
261
262 ConcurrentMap<String, Row> mirrorTableRows = rowStore.getRowStore();
263 if (mirrorTableRows == null) {
264 log.warn("The mirror uuid is null");
265 return null;
266 }
267
268 for (String uuid : mirrorTableRows.keySet()) {
269 Mirror mirror = (Mirror) TableGenerator
270 .getTable(dbSchema, mirrorTableRows.get(uuid), OvsdbTable.MIRROR);
271 String name = mirror.getName();
272 if (name.contains(mirrorName)) {
273 return uuid;
274 }
275 }
276 log.warn("Mirroring not found");
277 return null;
278 }
279
280 /**
281 * Gets mirrors of the device.
282 *
283 * @param deviceId target device id
284 * @return set of mirroring; empty if no mirror is found
285 */
286 @Override
287 public Set<MirroringStatistics> getMirroringStatistics(DeviceId deviceId) {
288 Uuid bridgeUuid = getBridgeUuid(deviceId);
289 if (bridgeUuid == null) {
290 log.warn("Couldn't find bridge {} in {}", deviceId, nodeId.getIpAddress());
291 return null;
292 }
293
294 List<MirroringStatistics> mirrorings = getMirrorings(bridgeUuid);
295 if (mirrorings == null) {
296 log.warn("Couldn't find mirrors in {}", nodeId.getIpAddress());
297 return null;
298 }
299 return ImmutableSet.copyOf(mirrorings);
300 }
301
302 /**
303 * Helper method which retrieves mirrorings statistics using bridge uuid.
304 *
305 * @param bridgeUuid the uuid of the bridge
306 * @return the list of the mirrorings statistics.
307 */
308 private List<MirroringStatistics> getMirrorings(Uuid bridgeUuid) {
309 DatabaseSchema dbSchema = schema.get(DATABASENAME);
310 if (dbSchema == null) {
311 log.warn("Unable to retrieve dbSchema {}", DATABASENAME);
312 return null;
313 }
314 OvsdbRowStore rowStore = getRowStore(DATABASENAME, BRIDGE);
315 if (rowStore == null) {
316 log.warn("Unable to retrieve rowStore {} of {}", BRIDGE, DATABASENAME);
317 return null;
318 }
319
320 Row bridgeRow = rowStore.getRow(bridgeUuid.value());
321 Bridge bridge = (Bridge) TableGenerator.
322 getTable(dbSchema, bridgeRow, OvsdbTable.BRIDGE);
323
324 Set<Uuid> mirroringsUuids = (Set<Uuid>) ((OvsdbSet) bridge
325 .getMirrorsColumn().data()).set();
326
327 OvsdbRowStore mirrorRowStore = getRowStore(DATABASENAME, MIRROR);
328 if (mirrorRowStore == null) {
329 log.warn("Unable to retrieve rowStore {} of {}", MIRROR, DATABASENAME);
330 return null;
331 }
332
333 List<MirroringStatistics> mirroringStatistics = new ArrayList<>();
334 ConcurrentMap<String, Row> mirrorTableRows = mirrorRowStore.getRowStore();
335 mirrorTableRows.forEach((key, row) -> {
336 if (!mirroringsUuids.contains(Uuid.uuid(key))) {
337 return;
338 }
339 Mirror mirror = (Mirror) TableGenerator
340 .getTable(dbSchema, row, OvsdbTable.MIRROR);
341 mirroringStatistics.add(MirroringStatistics.mirroringStatistics(mirror.getName(),
342 (Map<String, Integer>) ((OvsdbMap) mirror
343 .getStatisticsColumn().data()).map()));
344 });
345 return ImmutableList.copyOf(mirroringStatistics);
346 }
347
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700348 @Override
349 public String getPortUuid(String portName, String bridgeUuid) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700350 DatabaseSchema dbSchema = schema.get(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700351
Hyunsun Moon1251e192016-06-07 16:57:05 -0700352 Row bridgeRow = getRow(DATABASENAME, BRIDGE, bridgeUuid);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700353 Bridge bridge = (Bridge) TableGenerator.getTable(dbSchema, bridgeRow,
354 OvsdbTable.BRIDGE);
355 if (bridge != null) {
356 OvsdbSet setPorts = (OvsdbSet) bridge.getPortsColumn().data();
357 @SuppressWarnings("unchecked")
Jonathan Hart51539b82015-10-29 09:53:04 -0700358 Set<Uuid> ports = setPorts.set();
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700359 if (ports == null || ports.size() == 0) {
360 log.warn("The port uuid is null");
361 return null;
362 }
363
Jonathan Hart51539b82015-10-29 09:53:04 -0700364 for (Uuid uuid : ports) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700365 Row portRow = getRow(DATABASENAME, PORT, uuid.value());
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700366 Port port = (Port) TableGenerator.getTable(dbSchema, portRow,
367 OvsdbTable.PORT);
368 if (port != null && portName.equalsIgnoreCase(port.getName())) {
369 return uuid.value();
370 }
371 }
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700372 }
373 return null;
374 }
375
376 @Override
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700377 public String getBridgeUuid(String bridgeName) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700378 DatabaseSchema dbSchema = schema.get(DATABASENAME);
Hyunsun Moon1251e192016-06-07 16:57:05 -0700379 OvsdbRowStore rowStore = getRowStore(DATABASENAME, BRIDGE);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700380 if (rowStore == null) {
381 log.debug("The bridge uuid is null");
382 return null;
383 }
384
385 ConcurrentMap<String, Row> bridgeTableRows = rowStore.getRowStore();
386 if (bridgeTableRows == null) {
387 log.debug("The bridge uuid is null");
388 return null;
389 }
390
391 for (String uuid : bridgeTableRows.keySet()) {
392 Bridge bridge = (Bridge) TableGenerator
Hyunsun Moon1251e192016-06-07 16:57:05 -0700393 .getTable(dbSchema, bridgeTableRows.get(uuid), OvsdbTable.BRIDGE);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700394 if (bridge.getName().equals(bridgeName)) {
395 return uuid;
396 }
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700397 }
398 return null;
399 }
400
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700401 private String getOvsUuid(String dbName) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700402 OvsdbRowStore rowStore = getRowStore(DATABASENAME, DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700403 if (rowStore == null) {
404 log.debug("The bridge uuid is null");
405 return null;
406 }
407 ConcurrentMap<String, Row> ovsTableRows = rowStore.getRowStore();
408 if (ovsTableRows != null) {
409 for (String uuid : ovsTableRows.keySet()) {
410 Row row = ovsTableRows.get(uuid);
411 String tableName = row.tableName();
412 if (tableName.equals(dbName)) {
413 return uuid;
414 }
415 }
416 }
417 return null;
418 }
419
420 @Override
421 public void createPort(String bridgeName, String portName) {
422 String bridgeUuid = getBridgeUuid(bridgeName);
423 if (bridgeUuid == null) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700424 log.error("Can't find bridge {} in {}", bridgeName, nodeId.getIpAddress());
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700425 return;
426 }
427
Hyunsun Moon1251e192016-06-07 16:57:05 -0700428 DatabaseSchema dbSchema = schema.get(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700429 String portUuid = getPortUuid(portName, bridgeUuid);
Hyunsun Moon1251e192016-06-07 16:57:05 -0700430 Port port = (Port) TableGenerator.createTable(dbSchema, OvsdbTable.PORT);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700431 port.setName(portName);
432 if (portUuid == null) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700433 insertConfig(PORT, UUID, BRIDGE, PORTS, bridgeUuid, port.getRow());
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700434 } else {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700435 updateConfig(PORT, UUID, portUuid, port.getRow());
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700436 }
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700437 }
438
439 @Override
440 public void dropPort(String bridgeName, String portName) {
441 String bridgeUuid = getBridgeUuid(bridgeName);
442 if (bridgeUuid == null) {
443 log.error("Could not find Bridge {} in {}", bridgeName, nodeId);
444 return;
445 }
446
447 String portUuid = getPortUuid(portName, bridgeUuid);
448 if (portUuid != null) {
449 log.info("Port {} delete", portName);
Hyunsun Moon1251e192016-06-07 16:57:05 -0700450 deleteConfig(PORT, UUID, portUuid, BRIDGE, PORTS);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700451 }
452 }
453
Hyunsun Moon1251e192016-06-07 16:57:05 -0700454 @Deprecated
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700455 @Override
456 public void createBridge(String bridgeName) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700457 OvsdbBridge ovsdbBridge = OvsdbBridge.builder()
458 .name(bridgeName)
459 .build();
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700460
Hyunsun Moon1251e192016-06-07 16:57:05 -0700461 createBridge(ovsdbBridge);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700462 }
463
Hyunsun Moon1251e192016-06-07 16:57:05 -0700464 @Deprecated
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700465 @Override
lishuai6c56f5e2015-11-17 16:38:19 +0800466 public void createBridge(String bridgeName, String dpid, String exPortName) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700467 OvsdbBridge ovsdbBridge = OvsdbBridge.builder()
468 .name(bridgeName)
469 .failMode(BridgeDescription.FailMode.SECURE)
470 .datapathId(dpid)
471 .disableInBand()
472 .controllers(Lists.newArrayList(localController()))
473 .build();
lishuai6c56f5e2015-11-17 16:38:19 +0800474
Hyunsun Moon1251e192016-06-07 16:57:05 -0700475 createBridge(ovsdbBridge);
lishuai6c56f5e2015-11-17 16:38:19 +0800476
lishuai6c56f5e2015-11-17 16:38:19 +0800477 if (exPortName != null) {
478 createPort(bridgeName, exPortName);
479 }
Hyunsun Moon1251e192016-06-07 16:57:05 -0700480 }
lishuai6c56f5e2015-11-17 16:38:19 +0800481
Hyunsun Moon1251e192016-06-07 16:57:05 -0700482 @Deprecated
483 @Override
484 public boolean createBridge(String bridgeName, String dpid, List<ControllerInfo> controllers) {
485 OvsdbBridge ovsdbBridge = OvsdbBridge.builder()
486 .name(bridgeName)
487 .failMode(BridgeDescription.FailMode.SECURE)
488 .datapathId(dpid)
489 .disableInBand()
490 .controllers(controllers)
491 .build();
492
493 return createBridge(ovsdbBridge);
lishuai6c56f5e2015-11-17 16:38:19 +0800494 }
495
496 @Override
Hyunsun Moon1251e192016-06-07 16:57:05 -0700497 public boolean createBridge(OvsdbBridge ovsdbBridge) {
498 DatabaseSchema dbSchema = schema.get(DATABASENAME);
499 String ovsUuid = getOvsUuid(DATABASENAME);
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700500
501 if (dbSchema == null || ovsUuid == null) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700502 log.error("Can't find database Open_vSwitch");
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700503 return false;
504 }
505
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700506 Bridge bridge = (Bridge) TableGenerator.createTable(dbSchema, OvsdbTable.BRIDGE);
Hyunsun Moon1251e192016-06-07 16:57:05 -0700507 bridge.setOtherConfig(ovsdbBridge.otherConfigs());
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700508
Hyunsun Moon1251e192016-06-07 16:57:05 -0700509 if (ovsdbBridge.failMode().isPresent()) {
510 String failMode = ovsdbBridge.failMode().get().name().toLowerCase();
511 bridge.setFailMode(Sets.newHashSet(failMode));
Bob zhoue9795fd2016-05-12 20:18:45 +0800512 }
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700513
Hyunsun Moon1251e192016-06-07 16:57:05 -0700514 String bridgeUuid = getBridgeUuid(ovsdbBridge.name());
Hyunsun Moon98025542016-03-08 04:36:02 -0800515 if (bridgeUuid == null) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700516 bridge.setName(ovsdbBridge.name());
517 bridgeUuid = insertConfig(
518 BRIDGE, UUID, DATABASENAME, BRIDGES,
519 ovsUuid, bridge.getRow());
Hyunsun Moon98025542016-03-08 04:36:02 -0800520 } else {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700521 // update the bridge if it's already existing
522 updateConfig(BRIDGE, UUID, bridgeUuid, bridge.getRow());
Hyunsun Moon98025542016-03-08 04:36:02 -0800523 }
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700524
Hyunsun Moon1251e192016-06-07 16:57:05 -0700525 if (bridgeUuid == null) {
526 log.warn("Failed to create bridge {} on {}", ovsdbBridge.name(), nodeId);
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700527 return false;
528 }
529
Hyunsun Moon1251e192016-06-07 16:57:05 -0700530 createPort(ovsdbBridge.name(), ovsdbBridge.name());
531 setControllersWithUuid(Uuid.uuid(bridgeUuid), ovsdbBridge.controllers());
532
533 log.info("Created bridge {}", ovsdbBridge.name());
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700534 return true;
535 }
536
Hyunsun Moon1251e192016-06-07 16:57:05 -0700537 @Override
538 public ControllerInfo localController() {
539 IpAddress ipAddress = IpAddress.valueOf(((InetSocketAddress)
540 channel.localAddress()).getAddress());
541 return new ControllerInfo(ipAddress, OFPORT, "tcp");
andreaed976a42015-10-05 14:38:25 -0700542 }
543
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700544 private void setControllersWithUuid(Uuid bridgeUuid, List<ControllerInfo> controllers) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700545 DatabaseSchema dbSchema = schema.get(DATABASENAME);
andreaed976a42015-10-05 14:38:25 -0700546 if (dbSchema == null) {
547 log.debug("There is no schema");
548 return;
549 }
550 List<Controller> oldControllers = getControllers(bridgeUuid);
551 if (oldControllers == null) {
552 log.warn("There are no controllers");
553 return;
554 }
555
Jonathan Hart51539b82015-10-29 09:53:04 -0700556 Set<Uuid> newControllerUuids = new HashSet<>();
andreaed976a42015-10-05 14:38:25 -0700557
558 Set<ControllerInfo> newControllers = new HashSet<>(controllers);
559 List<Controller> removeControllers = new ArrayList<>();
560 oldControllers.forEach(controller -> {
561 ControllerInfo controllerInfo = new ControllerInfo((String) controller.getTargetColumn().data());
562 if (newControllers.contains(controllerInfo)) {
563 newControllers.remove(controllerInfo);
564 newControllerUuids.add(controller.getRow().uuid());
565 } else {
566 removeControllers.add(controller);
567 }
568 });
Hyunsun Moon1251e192016-06-07 16:57:05 -0700569 OvsdbRowStore controllerRowStore = getRowStore(DATABASENAME, CONTROLLER);
andreaed976a42015-10-05 14:38:25 -0700570 if (controllerRowStore == null) {
571 log.debug("There is no controller table");
572 return;
573 }
574
Hyunsun Moon1251e192016-06-07 16:57:05 -0700575 removeControllers.forEach(c -> deleteConfig(CONTROLLER, UUID, c.getRow().uuid().value(),
576 BRIDGE, "controller"));
andreaed976a42015-10-05 14:38:25 -0700577 newControllers.stream().map(c -> {
578 Controller controller = (Controller) TableGenerator
579 .createTable(dbSchema, OvsdbTable.CONTROLLER);
580 controller.setTarget(c.target());
581 return controller;
582 }).forEach(c -> {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700583 String uuid = insertConfig(CONTROLLER, UUID, BRIDGE, "controller", bridgeUuid.value(),
andreaed976a42015-10-05 14:38:25 -0700584 c.getRow());
Jonathan Hart51539b82015-10-29 09:53:04 -0700585 newControllerUuids.add(Uuid.uuid(uuid));
andreaed976a42015-10-05 14:38:25 -0700586
587 });
588
Hyunsun Moon1251e192016-06-07 16:57:05 -0700589 OvsdbRowStore rowStore = getRowStore(DATABASENAME, BRIDGE);
andreaed976a42015-10-05 14:38:25 -0700590 if (rowStore == null) {
591 log.debug("There is no bridge table");
592 return;
593 }
594
595 Row bridgeRow = rowStore.getRow(bridgeUuid.value());
596 Bridge bridge = (Bridge) TableGenerator.getTable(dbSchema, bridgeRow, OvsdbTable.BRIDGE);
597 bridge.setController(OvsdbSet.ovsdbSet(newControllerUuids));
Hyunsun Moon1251e192016-06-07 16:57:05 -0700598 updateConfig(BRIDGE, UUID, bridgeUuid.value(), bridge.getRow());
andreaed976a42015-10-05 14:38:25 -0700599 }
600
andreaed976a42015-10-05 14:38:25 -0700601 @Override
602 public void setControllersWithDeviceId(DeviceId deviceId, List<ControllerInfo> controllers) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700603 setControllersWithUuid(getBridgeUuid(deviceId), controllers);
andreaed976a42015-10-05 14:38:25 -0700604 }
605
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700606 @Override
607 public void dropBridge(String bridgeName) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700608 String bridgeUuid = getBridgeUuid(bridgeName);
609 if (bridgeUuid == null) {
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700610 log.warn("Could not find bridge in node", nodeId.getIpAddress());
611 return;
612 }
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700613 deleteConfig(BRIDGE, UUID, bridgeUuid, DATABASENAME, BRIDGES);
614 }
615
Pier Ventref5d72362016-07-17 12:02:14 +0200616 /**
617 * Creates a mirror port. Mirrors the traffic
618 * that goes to selectDstPort or comes from
619 * selectSrcPort or packets containing selectVlan
620 * to mirrorPort or to all ports that trunk mirrorVlan.
621 *
622 * @param mirror the OVSDB mirror description
623 * @return true if mirror creation is successful, false otherwise
624 */
625 @Override
626 public boolean createMirror(String bridgeName, OvsdbMirror mirror) {
627
628 /**
629 * Retrieves bridge's uuid. It is necessary to update
630 * Bridge table.
631 */
632 String bridgeUuid = getBridgeUuid(bridgeName);
633 if (bridgeUuid == null) {
634 log.warn("Couldn't find bridge {} in {}", bridgeName, nodeId.getIpAddress());
635 return false;
636 }
637
638 OvsdbMirror.Builder mirrorBuilder = OvsdbMirror.builder();
639
640 mirrorBuilder.mirroringName(mirror.mirroringName());
641 mirrorBuilder.selectAll(mirror.selectAll());
642
643 /**
644 * Retrieves the uuid of the monitored dst ports.
645 */
646 mirrorBuilder.monitorDstPorts(mirror.monitorDstPorts().parallelStream()
647 .map(dstPort -> {
648 String dstPortUuid = getPortUuid(dstPort.value(), bridgeUuid);
649 if (dstPortUuid != null) {
650 return Uuid.uuid(dstPortUuid);
651 }
652 log.warn("Couldn't find port {} in {}",
653 dstPort.value(), nodeId.getIpAddress());
654 return null;
655 })
656 .filter(Objects::nonNull)
657 .collect(Collectors.toSet())
658 );
659
660 /**
661 * Retrieves the uuid of the monitored src ports.
662 */
663 mirrorBuilder.monitorSrcPorts(mirror.monitorSrcPorts().parallelStream()
664 .map(srcPort -> {
665 String srcPortUuid = getPortUuid(srcPort.value(), bridgeUuid);
666 if (srcPortUuid != null) {
667 return Uuid.uuid(srcPortUuid);
668 }
669 log.warn("Couldn't find port {} in {}",
670 srcPort.value(), nodeId.getIpAddress());
671 return null;
672 }).filter(Objects::nonNull)
673 .collect(Collectors.toSet())
674 );
675
676 mirrorBuilder.monitorVlans(mirror.monitorVlans());
677 mirrorBuilder.mirrorPort(mirror.mirrorPort());
678 mirrorBuilder.mirrorVlan(mirror.mirrorVlan());
679 mirrorBuilder.externalIds(mirror.externalIds());
680 mirror = mirrorBuilder.build();
681
682 if (mirror.monitorDstPorts().size() == 0 &&
683 mirror.monitorSrcPorts().size() == 0 &&
684 mirror.monitorVlans().size() == 0) {
685 log.warn("Invalid monitoring data");
686 return false;
687 }
688
689 DatabaseSchema dbSchema = schema.get(DATABASENAME);
690
691 Mirror mirrorEntry = (Mirror) TableGenerator.createTable(dbSchema, OvsdbTable.MIRROR);
692 mirrorEntry.setName(mirror.mirroringName());
693 mirrorEntry.setSelectDstPort(mirror.monitorDstPorts());
694 mirrorEntry.setSelectSrcPort(mirror.monitorSrcPorts());
695 mirrorEntry.setSelectVlan(mirror.monitorVlans());
696 mirrorEntry.setExternalIds(mirror.externalIds());
697
698 /**
699 * If mirror port, retrieves the uuid of the mirror port.
700 */
701 if (mirror.mirrorPort() != null) {
702
703 String outputPortUuid = getPortUuid(mirror.mirrorPort().value(), bridgeUuid);
704 if (outputPortUuid == null) {
705 log.warn("Couldn't find port {} in {}", mirror.mirrorPort().value(), nodeId.getIpAddress());
706 return false;
707 }
708
709 mirrorEntry.setOutputPort(Uuid.uuid(outputPortUuid));
710
711 } else if (mirror.mirrorVlan() != null) {
712
713 mirrorEntry.setOutputVlan(mirror.mirrorVlan());
714
715 } else {
716 log.warn("Invalid mirror, no mirror port and no mirror vlan");
717 return false;
718 }
719
720 ArrayList<Operation> operations = Lists.newArrayList();
721 Insert mirrorInsert = new Insert(dbSchema.getTableSchema("Mirror"), "Mirror", mirrorEntry.getRow());
722 operations.add(mirrorInsert);
723
724 // update the bridge table
725 Condition condition = ConditionUtil.isEqual(UUID, Uuid.uuid(bridgeUuid));
726 Mutation mutation = MutationUtil.insert(MIRRORS, Uuid.uuid("Mirror"));
727 List<Condition> conditions = Lists.newArrayList(condition);
728 List<Mutation> mutations = Lists.newArrayList(mutation);
729 operations.add(new Mutate(dbSchema.getTableSchema("Bridge"), conditions, mutations));
730
731 transactConfig(DATABASENAME, operations);
732 log.info("Created mirror {}", mirror.mirroringName());
733 return true;
734 }
735
736 /**
737 * Drops the configuration for mirror.
738 *
739 * @param mirroringName
740 */
741 @Override
742 public void dropMirror(MirroringName mirroringName) {
743 String mirrorUuid = getMirrorUuid(mirroringName.name());
744 if (mirrorUuid != null) {
745 log.info("Deleted mirror {}", mirroringName.name());
746 deleteConfig(MIRROR, UUID, mirrorUuid, BRIDGE, MIRRORS);
747 }
748 log.warn("Unable to delete {}", mirroringName.name());
749 return;
750 }
751
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700752 @Deprecated
753 @Override
754 public boolean createTunnel(String bridgeName, String ifaceName, String tunnelType,
755 Map<String, String> options) {
756 OvsdbInterface ovsdbIface = OvsdbInterface.builder()
757 .name(ifaceName)
758 .type(Type.valueOf(tunnelType))
759 .options(options)
760 .build();
761
762 return createInterface(bridgeName, ovsdbIface);
763 }
764
765 @Deprecated
766 @Override
767 public void dropTunnel(IpAddress srcIp, IpAddress dstIp) {
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700768 }
769
770 @Override
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700771 public boolean createInterface(String bridgeName, OvsdbInterface ovsdbIface) {
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700772 String bridgeUuid = getBridgeUuid(bridgeName);
773 if (bridgeUuid == null) {
774 log.warn("Couldn't find bridge {} in {}", bridgeName, nodeId.getIpAddress());
775 return false;
776 }
777
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700778 if (getPortUuid(ovsdbIface.name(), bridgeUuid) != null) {
779 log.warn("Interface {} already exists", ovsdbIface.name());
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700780 // remove existing one and re-create?
781 return false;
782 }
783
784 ArrayList<Operation> operations = Lists.newArrayList();
Hyunsun Moon1251e192016-06-07 16:57:05 -0700785 DatabaseSchema dbSchema = schema.get(DATABASENAME);
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700786
Hyunsun Moon89478662016-06-09 17:52:34 -0700787 // insert a new port with the interface name
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700788 Port port = (Port) TableGenerator.createTable(dbSchema, OvsdbTable.PORT);
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700789 port.setName(ovsdbIface.name());
790 Insert portInsert = new Insert(dbSchema.getTableSchema(PORT), PORT, port.getRow());
791 portInsert.getRow().put(INTERFACES, Uuid.uuid(INTERFACE));
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700792 operations.add(portInsert);
793
Hyunsun Moon89478662016-06-09 17:52:34 -0700794 // update the bridge table with the new port
Hyunsun Moon1251e192016-06-07 16:57:05 -0700795 Condition condition = ConditionUtil.isEqual(UUID, Uuid.uuid(bridgeUuid));
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700796 Mutation mutation = MutationUtil.insert(PORTS, Uuid.uuid(PORT));
797 List<Condition> conditions = Lists.newArrayList(condition);
798 List<Mutation> mutations = Lists.newArrayList(mutation);
799 operations.add(new Mutate(dbSchema.getTableSchema(BRIDGE), conditions, mutations));
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700800
Hyunsun Moon89478662016-06-09 17:52:34 -0700801 // insert an interface
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700802 Interface intf = (Interface) TableGenerator.createTable(dbSchema, OvsdbTable.INTERFACE);
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700803 intf.setName(ovsdbIface.name());
804 intf.setType(ovsdbIface.typeToString());
805 intf.setOptions(ovsdbIface.options());
806 Insert intfInsert = new Insert(dbSchema.getTableSchema(INTERFACE), INTERFACE, intf.getRow());
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700807 operations.add(intfInsert);
808
Hyunsun Moon1251e192016-06-07 16:57:05 -0700809 transactConfig(DATABASENAME, operations);
Hyunsun Moon89478662016-06-09 17:52:34 -0700810 log.info("Created interface {}", ovsdbIface);
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700811 return true;
812 }
813
814 @Override
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700815 public boolean dropInterface(String ifaceName) {
816 OvsdbRowStore rowStore = getRowStore(DATABASENAME, BRIDGE);
817 if (rowStore == null) {
818 log.warn("Failed to get BRIDGE table");
819 return false;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700820 }
821
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700822 ConcurrentMap<String, Row> bridgeTableRows = rowStore.getRowStore();
823 if (bridgeTableRows == null) {
824 log.warn("Failed to get BRIDGE table rows");
825 return false;
826 }
827
828 // interface name is unique
829 Optional<String> bridgeId = bridgeTableRows.keySet().stream()
830 .filter(uuid -> getPortUuid(ifaceName, uuid) != null)
831 .findFirst();
832
833 if (bridgeId.isPresent()) {
834 String portId = getPortUuid(ifaceName, bridgeId.get());
835 deleteConfig(PORT, UUID, portId, BRIDGE, PORTS);
836 return true;
837 } else {
838 log.warn("Unable to find the interface with name {}", ifaceName);
839 return false;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700840 }
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700841 }
842
843 /**
844 * Delete transact config.
845 *
andreaed976a42015-10-05 14:38:25 -0700846 * @param childTableName child table name
847 * @param childColumnName child column name
848 * @param childUuid child row uuid
849 * @param parentTableName parent table name
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700850 * @param parentColumnName parent column
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700851 */
852 private void deleteConfig(String childTableName, String childColumnName,
andreaed976a42015-10-05 14:38:25 -0700853 String childUuid, String parentTableName,
854 String parentColumnName) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700855 DatabaseSchema dbSchema = schema.get(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700856 TableSchema childTableSchema = dbSchema.getTableSchema(childTableName);
857
858 ArrayList<Operation> operations = Lists.newArrayList();
859 if (parentTableName != null && parentColumnName != null) {
860 TableSchema parentTableSchema = dbSchema
861 .getTableSchema(parentTableName);
862 ColumnSchema parentColumnSchema = parentTableSchema
863 .getColumnSchema(parentColumnName);
864 List<Mutation> mutations = Lists.newArrayList();
865 Mutation mutation = MutationUtil.delete(parentColumnSchema.name(),
Jonathan Hart51539b82015-10-29 09:53:04 -0700866 Uuid.uuid(childUuid));
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700867 mutations.add(mutation);
868 List<Condition> conditions = Lists.newArrayList();
869 Condition condition = ConditionUtil.includes(parentColumnName,
Jonathan Hart51539b82015-10-29 09:53:04 -0700870 Uuid.uuid(childUuid));
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700871 conditions.add(condition);
872 Mutate op = new Mutate(parentTableSchema, conditions, mutations);
873 operations.add(op);
874 }
875
876 List<Condition> conditions = Lists.newArrayList();
Jonathan Hart51539b82015-10-29 09:53:04 -0700877 Condition condition = ConditionUtil.isEqual(childColumnName, Uuid.uuid(childUuid));
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700878 conditions.add(condition);
879 Delete del = new Delete(childTableSchema, conditions);
880 operations.add(del);
Hyunsun Moon1251e192016-06-07 16:57:05 -0700881 transactConfig(DATABASENAME, operations);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700882 }
883
884 /**
885 * Update transact config.
886 *
andreaed976a42015-10-05 14:38:25 -0700887 * @param tableName table name
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700888 * @param columnName column name
andreaed976a42015-10-05 14:38:25 -0700889 * @param uuid uuid
890 * @param row the config data
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700891 */
892 private void updateConfig(String tableName, String columnName, String uuid,
andreaed976a42015-10-05 14:38:25 -0700893 Row row) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700894 DatabaseSchema dbSchema = schema.get(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700895 TableSchema tableSchema = dbSchema.getTableSchema(tableName);
896
897 List<Condition> conditions = Lists.newArrayList();
Jonathan Hart51539b82015-10-29 09:53:04 -0700898 Condition condition = ConditionUtil.isEqual(columnName, Uuid.uuid(uuid));
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700899 conditions.add(condition);
900
901 Update update = new Update(tableSchema, row, conditions);
902
903 ArrayList<Operation> operations = Lists.newArrayList();
904 operations.add(update);
905
Hyunsun Moon1251e192016-06-07 16:57:05 -0700906 transactConfig(DATABASENAME, operations);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700907 }
908
909 /**
910 * Insert transact config.
911 *
andreaed976a42015-10-05 14:38:25 -0700912 * @param childTableName child table name
913 * @param childColumnName child column name
914 * @param parentTableName parent table name
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700915 * @param parentColumnName parent column
andreaed976a42015-10-05 14:38:25 -0700916 * @param parentUuid parent uuid
917 * @param row the config data
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700918 * @return uuid, empty if no uuid is find
919 */
920 private String insertConfig(String childTableName, String childColumnName,
andreaed976a42015-10-05 14:38:25 -0700921 String parentTableName, String parentColumnName,
922 String parentUuid, Row row) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700923 DatabaseSchema dbSchema = schema.get(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700924 TableSchema tableSchema = dbSchema.getTableSchema(childTableName);
925
Sho SHIMIZUff18f8c2016-03-11 14:43:53 -0800926 Insert insert = new Insert(tableSchema, childTableName, row);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700927
928 ArrayList<Operation> operations = Lists.newArrayList();
929 operations.add(insert);
930
931 if (parentTableName != null && parentColumnName != null) {
932 TableSchema parentTableSchema = dbSchema
933 .getTableSchema(parentTableName);
934 ColumnSchema parentColumnSchema = parentTableSchema
935 .getColumnSchema(parentColumnName);
936
937 List<Mutation> mutations = Lists.newArrayList();
938 Mutation mutation = MutationUtil.insert(parentColumnSchema.name(),
Sho SHIMIZUff18f8c2016-03-11 14:43:53 -0800939 Uuid.uuid(childTableName));
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700940 mutations.add(mutation);
941
942 List<Condition> conditions = Lists.newArrayList();
Hyunsun Moon1251e192016-06-07 16:57:05 -0700943 Condition condition = ConditionUtil.isEqual(UUID, Uuid.uuid(parentUuid));
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700944 conditions.add(condition);
945
946 Mutate op = new Mutate(parentTableSchema, conditions, mutations);
947 operations.add(op);
948 }
Hyunsun Moon1251e192016-06-07 16:57:05 -0700949 if (childTableName.equalsIgnoreCase(PORT)) {
950 log.debug("Handle port insert");
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700951 Insert intfInsert = handlePortInsertTable(row);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700952
953 if (intfInsert != null) {
954 operations.add(intfInsert);
955 }
956
957 Insert ins = (Insert) operations.get(0);
Hyunsun Moon1251e192016-06-07 16:57:05 -0700958 ins.getRow().put("interfaces", Uuid.uuid(INTERFACE));
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700959 }
960
961 List<OperationResult> results;
962 try {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700963 results = transactConfig(DATABASENAME, operations).get();
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700964 return results.get(0).getUuid().value();
965 } catch (InterruptedException e) {
966 log.warn("Interrupted while waiting to get result");
967 Thread.currentThread().interrupt();
968 } catch (ExecutionException e) {
969 log.error("Exception thrown while to get result");
970 }
971
972 return null;
973 }
974
975 /**
976 * Handles port insert.
977 *
andreaed976a42015-10-05 14:38:25 -0700978 * @param portRow row of port
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700979 * @return insert, empty if null
980 */
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700981 private Insert handlePortInsertTable(Row portRow) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700982 DatabaseSchema dbSchema = schema.get(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700983
Hyunsun Moon1251e192016-06-07 16:57:05 -0700984 TableSchema portTableSchema = dbSchema.getTableSchema(PORT);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700985 ColumnSchema portColumnSchema = portTableSchema.getColumnSchema("name");
986
987 String portName = (String) portRow.getColumn(portColumnSchema.name()).data();
Hyunsun Moon1251e192016-06-07 16:57:05 -0700988 Interface inf = (Interface) TableGenerator.createTable(dbSchema, OvsdbTable.INTERFACE);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700989 inf.setName(portName);
990
Hyunsun Moon1251e192016-06-07 16:57:05 -0700991 TableSchema intfTableSchema = dbSchema.getTableSchema(INTERFACE);
992 return new Insert(intfTableSchema, INTERFACE, inf.getRow());
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700993 }
994
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700995 @Override
996 public ListenableFuture<DatabaseSchema> getOvsdbSchema(String dbName) {
997 if (dbName == null) {
998 return null;
999 }
1000 DatabaseSchema databaseSchema = schema.get(dbName);
1001 if (databaseSchema == null) {
1002 List<String> dbNames = new ArrayList<String>();
1003 dbNames.add(dbName);
Hyunsun Moon1251e192016-06-07 16:57:05 -07001004 Function<JsonNode, DatabaseSchema> rowFunction = input -> {
1005 log.debug("Get ovsdb database schema {}", dbName);
1006 DatabaseSchema dbSchema = FromJsonUtil.jsonNodeToDbSchema(dbName, input);
1007 if (dbSchema == null) {
1008 log.debug("Get ovsdb database schema error");
1009 return null;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001010 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001011 schema.put(dbName, dbSchema);
1012 return dbSchema;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001013 };
1014
1015 ListenableFuture<JsonNode> input = getSchema(dbNames);
1016 if (input != null) {
1017 return Futures.transform(input, rowFunction);
1018 }
1019 return null;
1020 } else {
1021 return Futures.immediateFuture(databaseSchema);
1022 }
1023 }
1024
1025 @Override
1026 public ListenableFuture<TableUpdates> monitorTables(String dbName, String id) {
1027 if (dbName == null) {
1028 return null;
1029 }
1030 DatabaseSchema dbSchema = schema.get(dbName);
1031 if (dbSchema != null) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001032 Function<JsonNode, TableUpdates> rowFunction = input -> {
1033 log.debug("Get table updates");
1034 TableUpdates updates = FromJsonUtil.jsonNodeToTableUpdates(input, dbSchema);
1035 if (updates == null) {
1036 log.debug("Get table updates error");
1037 return null;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001038 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001039 return updates;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001040 };
1041 return Futures.transform(monitor(dbSchema, id), rowFunction);
1042 }
1043 return null;
1044 }
1045
Hyunsun Moondd14e8e2016-06-09 16:17:32 -07001046 private ListenableFuture<List<OperationResult>> transactConfig(String dbName,
1047 List<Operation> operations) {
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001048 if (dbName == null) {
1049 return null;
1050 }
1051 DatabaseSchema dbSchema = schema.get(dbName);
1052 if (dbSchema != null) {
andreaed976a42015-10-05 14:38:25 -07001053 Function<List<JsonNode>, List<OperationResult>> rowFunction = (input -> {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001054 log.debug("Get ovsdb operation result");
1055 List<OperationResult> result = FromJsonUtil.jsonNodeToOperationResult(input, operations);
andreaed976a42015-10-05 14:38:25 -07001056 if (result == null) {
1057 log.debug("The operation result is null");
1058 return null;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001059 }
andreaed976a42015-10-05 14:38:25 -07001060 return result;
1061 });
Hyunsun Moon1251e192016-06-07 16:57:05 -07001062 return Futures.transform(transact(dbSchema, operations), rowFunction);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001063 }
1064 return null;
1065 }
1066
1067 @Override
1068 public ListenableFuture<JsonNode> getSchema(List<String> dbnames) {
1069 String id = java.util.UUID.randomUUID().toString();
1070 String getSchemaString = JsonRpcWriterUtil.getSchemaStr(id, dbnames);
1071
1072 SettableFuture<JsonNode> sf = SettableFuture.create();
1073 requestResult.put(id, sf);
1074 requestMethod.put(id, "getSchema");
1075
1076 channel.writeAndFlush(getSchemaString);
1077 return sf;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001078 }
1079
1080 @Override
1081 public ListenableFuture<List<String>> echo() {
1082 String id = java.util.UUID.randomUUID().toString();
1083 String echoString = JsonRpcWriterUtil.echoStr(id);
1084
1085 SettableFuture<List<String>> sf = SettableFuture.create();
1086 requestResult.put(id, sf);
1087 requestMethod.put(id, "echo");
1088
1089 channel.writeAndFlush(echoString);
1090 return sf;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001091 }
1092
1093 @Override
1094 public ListenableFuture<JsonNode> monitor(DatabaseSchema dbSchema,
1095 String monitorId) {
1096 String id = java.util.UUID.randomUUID().toString();
1097 String monitorString = JsonRpcWriterUtil.monitorStr(id, monitorId,
1098 dbSchema);
1099
1100 SettableFuture<JsonNode> sf = SettableFuture.create();
1101 requestResult.put(id, sf);
1102 requestMethod.put(id, "monitor");
1103
1104 channel.writeAndFlush(monitorString);
1105 return sf;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001106 }
1107
1108 @Override
1109 public ListenableFuture<List<String>> listDbs() {
1110 String id = java.util.UUID.randomUUID().toString();
1111 String listDbsString = JsonRpcWriterUtil.listDbsStr(id);
1112
1113 SettableFuture<List<String>> sf = SettableFuture.create();
1114 requestResult.put(id, sf);
1115 requestMethod.put(id, "listDbs");
1116
1117 channel.writeAndFlush(listDbsString);
1118 return sf;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001119 }
1120
1121 @Override
1122 public ListenableFuture<List<JsonNode>> transact(DatabaseSchema dbSchema,
1123 List<Operation> operations) {
1124 String id = java.util.UUID.randomUUID().toString();
1125 String transactString = JsonRpcWriterUtil.transactStr(id, dbSchema,
1126 operations);
1127
1128 SettableFuture<List<JsonNode>> sf = SettableFuture.create();
1129 requestResult.put(id, sf);
1130 requestMethod.put(id, "transact");
1131
1132 channel.writeAndFlush(transactString);
1133 return sf;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001134 }
1135
andreaed976a42015-10-05 14:38:25 -07001136 @SuppressWarnings({"rawtypes", "unchecked"})
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001137 @Override
1138 public void processResult(JsonNode response) {
1139 log.debug("Handle result");
1140 String requestId = response.get("id").asText();
1141 SettableFuture sf = requestResult.get(requestId);
1142 if (sf == null) {
1143 log.debug("No such future to process");
1144 return;
1145 }
1146 String methodName = requestMethod.get(requestId);
Hyunsun Moon1251e192016-06-07 16:57:05 -07001147 sf.set(FromJsonUtil.jsonResultParser(response, methodName));
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001148 }
1149
1150 @Override
1151 public void processRequest(JsonNode requestJson) {
1152 log.debug("Handle request");
1153 if (requestJson.get("method").asText().equalsIgnoreCase("echo")) {
1154 log.debug("handle echo request");
1155
1156 String replyString = FromJsonUtil.getEchoRequestStr(requestJson);
1157 channel.writeAndFlush(replyString);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001158 } else {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001159 FromJsonUtil.jsonCallbackRequestParser(requestJson, monitorCallBack);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001160 }
1161 }
1162
1163 @Override
1164 public void setCallback(Callback monitorCallback) {
1165 this.monitorCallBack = monitorCallback;
1166 }
1167
1168 @Override
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001169 public Set<OvsdbBridge> getBridges() {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001170 Set<OvsdbBridge> ovsdbBridges = new HashSet<>();
1171 OvsdbTableStore tableStore = getTableStore(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001172 if (tableStore == null) {
MaoJianweidac220d2016-07-04 22:37:52 +08001173 return ovsdbBridges;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001174 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001175 OvsdbRowStore rowStore = tableStore.getRows(BRIDGE);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001176 if (rowStore == null) {
MaoJianweidac220d2016-07-04 22:37:52 +08001177 return ovsdbBridges;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001178 }
1179 ConcurrentMap<String, Row> rows = rowStore.getRowStore();
1180 for (String uuid : rows.keySet()) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001181 Row row = getRow(DATABASENAME, BRIDGE, uuid);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001182 OvsdbBridge ovsdbBridge = getOvsdbBridge(row);
1183 if (ovsdbBridge != null) {
1184 ovsdbBridges.add(ovsdbBridge);
1185 }
1186 }
1187 return ovsdbBridges;
1188 }
1189
1190 @Override
andreaed976a42015-10-05 14:38:25 -07001191 public Set<ControllerInfo> getControllers(DeviceId openflowDeviceId) {
Jonathan Hart51539b82015-10-29 09:53:04 -07001192 Uuid bridgeUuid = getBridgeUuid(openflowDeviceId);
andreaed976a42015-10-05 14:38:25 -07001193 if (bridgeUuid == null) {
1194 log.warn("bad bridge Uuid");
1195 return null;
1196 }
1197 List<Controller> controllers = getControllers(bridgeUuid);
1198 if (controllers == null) {
1199 log.warn("bad list of controllers");
1200 return null;
1201 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001202 return controllers.stream().map(controller -> new ControllerInfo(
1203 (String) controller.getTargetColumn()
1204 .data())).collect(Collectors.toSet());
andreaed976a42015-10-05 14:38:25 -07001205 }
1206
Jonathan Hart51539b82015-10-29 09:53:04 -07001207 private List<Controller> getControllers(Uuid bridgeUuid) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001208 DatabaseSchema dbSchema = schema.get(DATABASENAME);
andreaed976a42015-10-05 14:38:25 -07001209 if (dbSchema == null) {
1210 return null;
1211 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001212 OvsdbRowStore rowStore = getRowStore(DATABASENAME, BRIDGE);
andreaed976a42015-10-05 14:38:25 -07001213 if (rowStore == null) {
1214 log.debug("There is no bridge table");
1215 return null;
1216 }
1217
1218 Row bridgeRow = rowStore.getRow(bridgeUuid.value());
1219 Bridge bridge = (Bridge) TableGenerator.
1220 getTable(dbSchema, bridgeRow, OvsdbTable.BRIDGE);
1221
1222 //FIXME remove log
1223 log.warn("type of controller column", bridge.getControllerColumn()
1224 .data().getClass());
Jonathan Hart51539b82015-10-29 09:53:04 -07001225 Set<Uuid> controllerUuids = (Set<Uuid>) ((OvsdbSet) bridge
andreaed976a42015-10-05 14:38:25 -07001226 .getControllerColumn().data()).set();
andreaed976a42015-10-05 14:38:25 -07001227
Hyunsun Moon1251e192016-06-07 16:57:05 -07001228 OvsdbRowStore controllerRowStore = getRowStore(DATABASENAME, CONTROLLER);
andreaed976a42015-10-05 14:38:25 -07001229 if (controllerRowStore == null) {
1230 log.debug("There is no controller table");
1231 return null;
1232 }
1233
1234 List<Controller> ovsdbControllers = new ArrayList<>();
1235 ConcurrentMap<String, Row> controllerTableRows = controllerRowStore.getRowStore();
1236 controllerTableRows.forEach((key, row) -> {
Jonathan Hart51539b82015-10-29 09:53:04 -07001237 if (!controllerUuids.contains(Uuid.uuid(key))) {
andreaed976a42015-10-05 14:38:25 -07001238 return;
1239 }
1240 Controller controller = (Controller) TableGenerator
1241 .getTable(dbSchema, row, OvsdbTable.CONTROLLER);
1242 ovsdbControllers.add(controller);
1243 });
1244 return ovsdbControllers;
1245 }
1246
1247
Jonathan Hart51539b82015-10-29 09:53:04 -07001248 private Uuid getBridgeUuid(DeviceId openflowDeviceId) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001249 DatabaseSchema dbSchema = schema.get(DATABASENAME);
andreaed976a42015-10-05 14:38:25 -07001250 if (dbSchema == null) {
1251 return null;
1252 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001253 OvsdbRowStore rowStore = getRowStore(DATABASENAME, BRIDGE);
andreaed976a42015-10-05 14:38:25 -07001254 if (rowStore == null) {
1255 log.debug("There is no bridge table");
1256 return null;
1257 }
1258
1259 ConcurrentMap<String, Row> bridgeTableRows = rowStore.getRowStore();
Jonathan Hart51539b82015-10-29 09:53:04 -07001260 final AtomicReference<Uuid> uuid = new AtomicReference<>();
andreaed976a42015-10-05 14:38:25 -07001261 for (Map.Entry<String, Row> entry : bridgeTableRows.entrySet()) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001262 Bridge bridge = (Bridge) TableGenerator.getTable(
1263 dbSchema,
1264 entry.getValue(),
1265 OvsdbTable.BRIDGE);
1266
1267 if (matchesDpid(bridge, openflowDeviceId)) {
Jonathan Hart51539b82015-10-29 09:53:04 -07001268 uuid.set(Uuid.uuid(entry.getKey()));
andreaed976a42015-10-05 14:38:25 -07001269 break;
1270 }
1271 }
1272 if (uuid.get() == null) {
1273 log.debug("There is no bridge for {}", openflowDeviceId);
1274 }
1275 return uuid.get();
andreaed976a42015-10-05 14:38:25 -07001276 }
1277
1278 private static boolean matchesDpid(Bridge b, DeviceId deviceId) {
1279 String ofDpid = deviceId.toString().replace("of:", "");
1280 Set ofDeviceIds = ((OvsdbSet) b.getDatapathIdColumn().data()).set();
1281 //TODO Set<String>
1282 return ofDeviceIds.contains(ofDpid);
1283 }
1284
1285 @Override
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001286 public Set<OvsdbPort> getPorts() {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001287 Set<OvsdbPort> ovsdbPorts = new HashSet<>();
1288 OvsdbTableStore tableStore = getTableStore(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001289 if (tableStore == null) {
1290 return null;
1291 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001292 OvsdbRowStore rowStore = tableStore.getRows(INTERFACE);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001293 if (rowStore == null) {
1294 return null;
1295 }
1296 ConcurrentMap<String, Row> rows = rowStore.getRowStore();
1297 for (String uuid : rows.keySet()) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001298 Row row = getRow(DATABASENAME, INTERFACE, uuid);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001299 OvsdbPort ovsdbPort = getOvsdbPort(row);
1300 if (ovsdbPort != null) {
1301 ovsdbPorts.add(ovsdbPort);
1302 }
1303 }
1304 return ovsdbPorts;
1305 }
1306
1307 @Override
1308 public DatabaseSchema getDatabaseSchema(String dbName) {
1309 return schema.get(dbName);
1310 }
1311
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001312 private OvsdbPort getOvsdbPort(Row row) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001313 DatabaseSchema dbSchema = getDatabaseSchema(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001314 Interface intf = (Interface) TableGenerator
1315 .getTable(dbSchema, row, OvsdbTable.INTERFACE);
1316 if (intf == null) {
1317 return null;
1318 }
1319 long ofPort = getOfPort(intf);
1320 String portName = intf.getName();
1321 if ((ofPort < 0) || (portName == null)) {
1322 return null;
1323 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001324 return new OvsdbPort(new OvsdbPortNumber(ofPort), new OvsdbPortName(portName));
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001325 }
1326
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001327 private OvsdbBridge getOvsdbBridge(Row row) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001328 DatabaseSchema dbSchema = getDatabaseSchema(DATABASENAME);
1329 Bridge bridge = (Bridge) TableGenerator.getTable(dbSchema, row, OvsdbTable.BRIDGE);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001330 if (bridge == null) {
1331 return null;
1332 }
1333
1334 OvsdbSet datapathIdSet = (OvsdbSet) bridge.getDatapathIdColumn().data();
1335 @SuppressWarnings("unchecked")
1336 Set<String> datapathIds = datapathIdSet.set();
1337 if (datapathIds == null || datapathIds.size() == 0) {
1338 return null;
1339 }
1340 String datapathId = (String) datapathIds.toArray()[0];
1341 String bridgeName = bridge.getName();
1342 if ((datapathId == null) || (bridgeName == null)) {
1343 return null;
1344 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001345 return OvsdbBridge.builder().name(bridgeName).datapathId(datapathId).build();
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001346 }
1347
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001348 private long getOfPort(Interface intf) {
1349 OvsdbSet ofPortSet = (OvsdbSet) intf.getOpenFlowPortColumn().data();
1350 @SuppressWarnings("unchecked")
1351 Set<Integer> ofPorts = ofPortSet.set();
Hyunsun Moon1251e192016-06-07 16:57:05 -07001352 if (ofPorts == null || ofPorts.size() <= 0) {
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001353 log.debug("The ofport is null in {}", intf.getName());
1354 return -1;
1355 }
1356 // return (long) ofPorts.toArray()[0];
1357 Iterator<Integer> it = ofPorts.iterator();
1358 return Long.parseLong(it.next().toString());
1359 }
CNluciusa66c3972015-09-06 20:31:29 +08001360
1361 @Override
1362 public Set<OvsdbPort> getLocalPorts(Iterable<String> ifaceids) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001363 Set<OvsdbPort> ovsdbPorts = new HashSet<>();
1364 OvsdbTableStore tableStore = getTableStore(DATABASENAME);
CNluciusa66c3972015-09-06 20:31:29 +08001365 if (tableStore == null) {
1366 return null;
1367 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001368 OvsdbRowStore rowStore = tableStore.getRows(INTERFACE);
CNluciusa66c3972015-09-06 20:31:29 +08001369 if (rowStore == null) {
1370 return null;
1371 }
1372 ConcurrentMap<String, Row> rows = rowStore.getRowStore();
1373 for (String uuid : rows.keySet()) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001374 Row row = getRow(DATABASENAME, INTERFACE, uuid);
1375 DatabaseSchema dbSchema = getDatabaseSchema(DATABASENAME);
CNluciusa66c3972015-09-06 20:31:29 +08001376 Interface intf = (Interface) TableGenerator
1377 .getTable(dbSchema, row, OvsdbTable.INTERFACE);
1378 if (intf == null || getIfaceid(intf) == null) {
1379 continue;
1380 }
1381 String portName = intf.getName();
Hyunsun Moon1251e192016-06-07 16:57:05 -07001382 if (portName == null) {
1383 continue;
1384 }
CNluciusa66c3972015-09-06 20:31:29 +08001385 Set<String> ifaceidSet = Sets.newHashSet(ifaceids);
Hyunsun Moon1251e192016-06-07 16:57:05 -07001386 if (portName.startsWith(TYPEVXLAN) || !ifaceidSet.contains(getIfaceid(intf))) {
CNluciusa66c3972015-09-06 20:31:29 +08001387 continue;
1388 }
1389 long ofPort = getOfPort(intf);
Hyunsun Moon1251e192016-06-07 16:57:05 -07001390 if (ofPort < 0) {
CNluciusa66c3972015-09-06 20:31:29 +08001391 continue;
1392 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001393 ovsdbPorts.add(new OvsdbPort(new OvsdbPortNumber(ofPort),
1394 new OvsdbPortName(portName)));
CNluciusa66c3972015-09-06 20:31:29 +08001395 }
1396 return ovsdbPorts;
1397 }
1398
1399 private String getIfaceid(Interface intf) {
1400 OvsdbMap ovsdbMap = (OvsdbMap) intf.getExternalIdsColumn().data();
1401 @SuppressWarnings("unchecked")
1402 Map<String, String> externalIds = ovsdbMap.map();
1403 if (externalIds.isEmpty()) {
1404 log.warn("The external_ids is null");
1405 return null;
1406 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001407 String ifaceid = externalIds.get(EXTERNAL_ID_INTERFACE_ID);
CNluciusa66c3972015-09-06 20:31:29 +08001408 if (ifaceid == null) {
1409 log.warn("The ifaceid is null");
1410 return null;
1411 }
1412 return ifaceid;
1413 }
Hyunsun Moon5fb20a52015-09-25 17:02:33 -07001414
1415 @Override
1416 public void disconnect() {
1417 channel.disconnect();
1418 this.agent.removeConnectedNode(nodeId);
1419 }
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001420}