blob: d5fed14e23c0efd8b199d93791c35b00ef3410cc [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();
Jon Hallcbd1b392017-01-18 20:15:44 -0800359 if (ports == null || ports.isEmpty()) {
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700360 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 }
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700435 }
436
437 @Override
438 public void dropPort(String bridgeName, String portName) {
439 String bridgeUuid = getBridgeUuid(bridgeName);
440 if (bridgeUuid == null) {
441 log.error("Could not find Bridge {} in {}", bridgeName, nodeId);
442 return;
443 }
444
445 String portUuid = getPortUuid(portName, bridgeUuid);
446 if (portUuid != null) {
447 log.info("Port {} delete", portName);
Hyunsun Moon1251e192016-06-07 16:57:05 -0700448 deleteConfig(PORT, UUID, portUuid, BRIDGE, PORTS);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700449 }
450 }
451
Hyunsun Moon1251e192016-06-07 16:57:05 -0700452 @Deprecated
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700453 @Override
454 public void createBridge(String bridgeName) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700455 OvsdbBridge ovsdbBridge = OvsdbBridge.builder()
456 .name(bridgeName)
457 .build();
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700458
Hyunsun Moon1251e192016-06-07 16:57:05 -0700459 createBridge(ovsdbBridge);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700460 }
461
Hyunsun Moon1251e192016-06-07 16:57:05 -0700462 @Deprecated
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700463 @Override
lishuai6c56f5e2015-11-17 16:38:19 +0800464 public void createBridge(String bridgeName, String dpid, String exPortName) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700465 OvsdbBridge ovsdbBridge = OvsdbBridge.builder()
466 .name(bridgeName)
467 .failMode(BridgeDescription.FailMode.SECURE)
468 .datapathId(dpid)
469 .disableInBand()
470 .controllers(Lists.newArrayList(localController()))
471 .build();
lishuai6c56f5e2015-11-17 16:38:19 +0800472
Hyunsun Moon1251e192016-06-07 16:57:05 -0700473 createBridge(ovsdbBridge);
lishuai6c56f5e2015-11-17 16:38:19 +0800474
lishuai6c56f5e2015-11-17 16:38:19 +0800475 if (exPortName != null) {
476 createPort(bridgeName, exPortName);
477 }
Hyunsun Moon1251e192016-06-07 16:57:05 -0700478 }
lishuai6c56f5e2015-11-17 16:38:19 +0800479
Hyunsun Moon1251e192016-06-07 16:57:05 -0700480 @Deprecated
481 @Override
482 public boolean createBridge(String bridgeName, String dpid, List<ControllerInfo> controllers) {
483 OvsdbBridge ovsdbBridge = OvsdbBridge.builder()
484 .name(bridgeName)
485 .failMode(BridgeDescription.FailMode.SECURE)
486 .datapathId(dpid)
487 .disableInBand()
488 .controllers(controllers)
489 .build();
490
491 return createBridge(ovsdbBridge);
lishuai6c56f5e2015-11-17 16:38:19 +0800492 }
493
494 @Override
Hyunsun Moon1251e192016-06-07 16:57:05 -0700495 public boolean createBridge(OvsdbBridge ovsdbBridge) {
496 DatabaseSchema dbSchema = schema.get(DATABASENAME);
497 String ovsUuid = getOvsUuid(DATABASENAME);
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700498
499 if (dbSchema == null || ovsUuid == null) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700500 log.error("Can't find database Open_vSwitch");
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700501 return false;
502 }
503
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700504 Bridge bridge = (Bridge) TableGenerator.createTable(dbSchema, OvsdbTable.BRIDGE);
Hyunsun Moon1251e192016-06-07 16:57:05 -0700505 bridge.setOtherConfig(ovsdbBridge.otherConfigs());
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700506
Hyunsun Moon1251e192016-06-07 16:57:05 -0700507 if (ovsdbBridge.failMode().isPresent()) {
508 String failMode = ovsdbBridge.failMode().get().name().toLowerCase();
509 bridge.setFailMode(Sets.newHashSet(failMode));
Bob zhoue9795fd2016-05-12 20:18:45 +0800510 }
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700511
Hyunsun Moon1251e192016-06-07 16:57:05 -0700512 String bridgeUuid = getBridgeUuid(ovsdbBridge.name());
Hyunsun Moon98025542016-03-08 04:36:02 -0800513 if (bridgeUuid == null) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700514 bridge.setName(ovsdbBridge.name());
515 bridgeUuid = insertConfig(
516 BRIDGE, UUID, DATABASENAME, BRIDGES,
517 ovsUuid, bridge.getRow());
Hyunsun Moon98025542016-03-08 04:36:02 -0800518 } else {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700519 // update the bridge if it's already existing
520 updateConfig(BRIDGE, UUID, bridgeUuid, bridge.getRow());
Hyunsun Moon98025542016-03-08 04:36:02 -0800521 }
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700522
Hyunsun Moon1251e192016-06-07 16:57:05 -0700523 if (bridgeUuid == null) {
524 log.warn("Failed to create bridge {} on {}", ovsdbBridge.name(), nodeId);
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700525 return false;
526 }
527
Hyunsun Moon1251e192016-06-07 16:57:05 -0700528 createPort(ovsdbBridge.name(), ovsdbBridge.name());
529 setControllersWithUuid(Uuid.uuid(bridgeUuid), ovsdbBridge.controllers());
530
531 log.info("Created bridge {}", ovsdbBridge.name());
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700532 return true;
533 }
534
Hyunsun Moon1251e192016-06-07 16:57:05 -0700535 @Override
536 public ControllerInfo localController() {
537 IpAddress ipAddress = IpAddress.valueOf(((InetSocketAddress)
538 channel.localAddress()).getAddress());
539 return new ControllerInfo(ipAddress, OFPORT, "tcp");
andreaed976a42015-10-05 14:38:25 -0700540 }
541
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700542 private void setControllersWithUuid(Uuid bridgeUuid, List<ControllerInfo> controllers) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700543 DatabaseSchema dbSchema = schema.get(DATABASENAME);
andreaed976a42015-10-05 14:38:25 -0700544 if (dbSchema == null) {
545 log.debug("There is no schema");
546 return;
547 }
548 List<Controller> oldControllers = getControllers(bridgeUuid);
549 if (oldControllers == null) {
550 log.warn("There are no controllers");
551 return;
552 }
553
Jonathan Hart51539b82015-10-29 09:53:04 -0700554 Set<Uuid> newControllerUuids = new HashSet<>();
andreaed976a42015-10-05 14:38:25 -0700555
556 Set<ControllerInfo> newControllers = new HashSet<>(controllers);
557 List<Controller> removeControllers = new ArrayList<>();
558 oldControllers.forEach(controller -> {
559 ControllerInfo controllerInfo = new ControllerInfo((String) controller.getTargetColumn().data());
560 if (newControllers.contains(controllerInfo)) {
561 newControllers.remove(controllerInfo);
562 newControllerUuids.add(controller.getRow().uuid());
563 } else {
564 removeControllers.add(controller);
565 }
566 });
Hyunsun Moon1251e192016-06-07 16:57:05 -0700567 OvsdbRowStore controllerRowStore = getRowStore(DATABASENAME, CONTROLLER);
andreaed976a42015-10-05 14:38:25 -0700568 if (controllerRowStore == null) {
569 log.debug("There is no controller table");
570 return;
571 }
572
Hyunsun Moon1251e192016-06-07 16:57:05 -0700573 removeControllers.forEach(c -> deleteConfig(CONTROLLER, UUID, c.getRow().uuid().value(),
574 BRIDGE, "controller"));
andreaed976a42015-10-05 14:38:25 -0700575 newControllers.stream().map(c -> {
576 Controller controller = (Controller) TableGenerator
577 .createTable(dbSchema, OvsdbTable.CONTROLLER);
578 controller.setTarget(c.target());
579 return controller;
580 }).forEach(c -> {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700581 String uuid = insertConfig(CONTROLLER, UUID, BRIDGE, "controller", bridgeUuid.value(),
andreaed976a42015-10-05 14:38:25 -0700582 c.getRow());
Jonathan Hart51539b82015-10-29 09:53:04 -0700583 newControllerUuids.add(Uuid.uuid(uuid));
andreaed976a42015-10-05 14:38:25 -0700584
585 });
586
Hyunsun Moon1251e192016-06-07 16:57:05 -0700587 OvsdbRowStore rowStore = getRowStore(DATABASENAME, BRIDGE);
andreaed976a42015-10-05 14:38:25 -0700588 if (rowStore == null) {
589 log.debug("There is no bridge table");
590 return;
591 }
592
593 Row bridgeRow = rowStore.getRow(bridgeUuid.value());
594 Bridge bridge = (Bridge) TableGenerator.getTable(dbSchema, bridgeRow, OvsdbTable.BRIDGE);
595 bridge.setController(OvsdbSet.ovsdbSet(newControllerUuids));
Hyunsun Moon1251e192016-06-07 16:57:05 -0700596 updateConfig(BRIDGE, UUID, bridgeUuid.value(), bridge.getRow());
andreaed976a42015-10-05 14:38:25 -0700597 }
598
andreaed976a42015-10-05 14:38:25 -0700599 @Override
600 public void setControllersWithDeviceId(DeviceId deviceId, List<ControllerInfo> controllers) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700601 setControllersWithUuid(getBridgeUuid(deviceId), controllers);
andreaed976a42015-10-05 14:38:25 -0700602 }
603
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700604 @Override
605 public void dropBridge(String bridgeName) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700606 String bridgeUuid = getBridgeUuid(bridgeName);
607 if (bridgeUuid == null) {
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700608 log.warn("Could not find bridge in node", nodeId.getIpAddress());
609 return;
610 }
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700611 deleteConfig(BRIDGE, UUID, bridgeUuid, DATABASENAME, BRIDGES);
612 }
613
Pier Ventref5d72362016-07-17 12:02:14 +0200614 /**
615 * Creates a mirror port. Mirrors the traffic
616 * that goes to selectDstPort or comes from
617 * selectSrcPort or packets containing selectVlan
618 * to mirrorPort or to all ports that trunk mirrorVlan.
619 *
620 * @param mirror the OVSDB mirror description
621 * @return true if mirror creation is successful, false otherwise
622 */
623 @Override
624 public boolean createMirror(String bridgeName, OvsdbMirror mirror) {
625
626 /**
627 * Retrieves bridge's uuid. It is necessary to update
628 * Bridge table.
629 */
630 String bridgeUuid = getBridgeUuid(bridgeName);
631 if (bridgeUuid == null) {
632 log.warn("Couldn't find bridge {} in {}", bridgeName, nodeId.getIpAddress());
633 return false;
634 }
635
636 OvsdbMirror.Builder mirrorBuilder = OvsdbMirror.builder();
637
638 mirrorBuilder.mirroringName(mirror.mirroringName());
639 mirrorBuilder.selectAll(mirror.selectAll());
640
641 /**
642 * Retrieves the uuid of the monitored dst ports.
643 */
644 mirrorBuilder.monitorDstPorts(mirror.monitorDstPorts().parallelStream()
645 .map(dstPort -> {
646 String dstPortUuid = getPortUuid(dstPort.value(), bridgeUuid);
647 if (dstPortUuid != null) {
648 return Uuid.uuid(dstPortUuid);
649 }
650 log.warn("Couldn't find port {} in {}",
651 dstPort.value(), nodeId.getIpAddress());
652 return null;
653 })
654 .filter(Objects::nonNull)
655 .collect(Collectors.toSet())
656 );
657
658 /**
659 * Retrieves the uuid of the monitored src ports.
660 */
661 mirrorBuilder.monitorSrcPorts(mirror.monitorSrcPorts().parallelStream()
662 .map(srcPort -> {
663 String srcPortUuid = getPortUuid(srcPort.value(), bridgeUuid);
664 if (srcPortUuid != null) {
665 return Uuid.uuid(srcPortUuid);
666 }
667 log.warn("Couldn't find port {} in {}",
668 srcPort.value(), nodeId.getIpAddress());
669 return null;
670 }).filter(Objects::nonNull)
671 .collect(Collectors.toSet())
672 );
673
674 mirrorBuilder.monitorVlans(mirror.monitorVlans());
675 mirrorBuilder.mirrorPort(mirror.mirrorPort());
676 mirrorBuilder.mirrorVlan(mirror.mirrorVlan());
677 mirrorBuilder.externalIds(mirror.externalIds());
678 mirror = mirrorBuilder.build();
679
Jon Hallcbd1b392017-01-18 20:15:44 -0800680 if (mirror.monitorDstPorts().isEmpty() &&
681 mirror.monitorSrcPorts().isEmpty() &&
682 mirror.monitorVlans().isEmpty()) {
Pier Ventref5d72362016-07-17 12:02:14 +0200683 log.warn("Invalid monitoring data");
684 return false;
685 }
686
687 DatabaseSchema dbSchema = schema.get(DATABASENAME);
688
689 Mirror mirrorEntry = (Mirror) TableGenerator.createTable(dbSchema, OvsdbTable.MIRROR);
690 mirrorEntry.setName(mirror.mirroringName());
691 mirrorEntry.setSelectDstPort(mirror.monitorDstPorts());
692 mirrorEntry.setSelectSrcPort(mirror.monitorSrcPorts());
693 mirrorEntry.setSelectVlan(mirror.monitorVlans());
694 mirrorEntry.setExternalIds(mirror.externalIds());
695
696 /**
697 * If mirror port, retrieves the uuid of the mirror port.
698 */
699 if (mirror.mirrorPort() != null) {
700
701 String outputPortUuid = getPortUuid(mirror.mirrorPort().value(), bridgeUuid);
702 if (outputPortUuid == null) {
703 log.warn("Couldn't find port {} in {}", mirror.mirrorPort().value(), nodeId.getIpAddress());
704 return false;
705 }
706
707 mirrorEntry.setOutputPort(Uuid.uuid(outputPortUuid));
708
709 } else if (mirror.mirrorVlan() != null) {
710
711 mirrorEntry.setOutputVlan(mirror.mirrorVlan());
712
713 } else {
714 log.warn("Invalid mirror, no mirror port and no mirror vlan");
715 return false;
716 }
717
718 ArrayList<Operation> operations = Lists.newArrayList();
719 Insert mirrorInsert = new Insert(dbSchema.getTableSchema("Mirror"), "Mirror", mirrorEntry.getRow());
720 operations.add(mirrorInsert);
721
722 // update the bridge table
723 Condition condition = ConditionUtil.isEqual(UUID, Uuid.uuid(bridgeUuid));
724 Mutation mutation = MutationUtil.insert(MIRRORS, Uuid.uuid("Mirror"));
725 List<Condition> conditions = Lists.newArrayList(condition);
726 List<Mutation> mutations = Lists.newArrayList(mutation);
727 operations.add(new Mutate(dbSchema.getTableSchema("Bridge"), conditions, mutations));
728
729 transactConfig(DATABASENAME, operations);
730 log.info("Created mirror {}", mirror.mirroringName());
731 return true;
732 }
733
734 /**
735 * Drops the configuration for mirror.
736 *
Ray Milkeyef794342016-11-09 16:20:29 -0800737 * @param mirroringName name of mirror to drop
Pier Ventref5d72362016-07-17 12:02:14 +0200738 */
739 @Override
740 public void dropMirror(MirroringName mirroringName) {
741 String mirrorUuid = getMirrorUuid(mirroringName.name());
742 if (mirrorUuid != null) {
743 log.info("Deleted mirror {}", mirroringName.name());
744 deleteConfig(MIRROR, UUID, mirrorUuid, BRIDGE, MIRRORS);
745 }
746 log.warn("Unable to delete {}", mirroringName.name());
747 return;
748 }
749
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700750 @Deprecated
751 @Override
752 public boolean createTunnel(String bridgeName, String ifaceName, String tunnelType,
753 Map<String, String> options) {
754 OvsdbInterface ovsdbIface = OvsdbInterface.builder()
755 .name(ifaceName)
756 .type(Type.valueOf(tunnelType))
757 .options(options)
758 .build();
759
760 return createInterface(bridgeName, ovsdbIface);
761 }
762
763 @Deprecated
764 @Override
765 public void dropTunnel(IpAddress srcIp, IpAddress dstIp) {
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700766 }
767
768 @Override
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700769 public boolean createInterface(String bridgeName, OvsdbInterface ovsdbIface) {
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700770 String bridgeUuid = getBridgeUuid(bridgeName);
771 if (bridgeUuid == null) {
772 log.warn("Couldn't find bridge {} in {}", bridgeName, nodeId.getIpAddress());
773 return false;
774 }
775
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700776 if (getPortUuid(ovsdbIface.name(), bridgeUuid) != null) {
777 log.warn("Interface {} already exists", ovsdbIface.name());
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700778 // remove existing one and re-create?
779 return false;
780 }
781
782 ArrayList<Operation> operations = Lists.newArrayList();
Hyunsun Moon1251e192016-06-07 16:57:05 -0700783 DatabaseSchema dbSchema = schema.get(DATABASENAME);
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700784
Hyunsun Moon89478662016-06-09 17:52:34 -0700785 // insert a new port with the interface name
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700786 Port port = (Port) TableGenerator.createTable(dbSchema, OvsdbTable.PORT);
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700787 port.setName(ovsdbIface.name());
788 Insert portInsert = new Insert(dbSchema.getTableSchema(PORT), PORT, port.getRow());
789 portInsert.getRow().put(INTERFACES, Uuid.uuid(INTERFACE));
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700790 operations.add(portInsert);
791
Hyunsun Moon89478662016-06-09 17:52:34 -0700792 // update the bridge table with the new port
Hyunsun Moon1251e192016-06-07 16:57:05 -0700793 Condition condition = ConditionUtil.isEqual(UUID, Uuid.uuid(bridgeUuid));
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700794 Mutation mutation = MutationUtil.insert(PORTS, Uuid.uuid(PORT));
795 List<Condition> conditions = Lists.newArrayList(condition);
796 List<Mutation> mutations = Lists.newArrayList(mutation);
797 operations.add(new Mutate(dbSchema.getTableSchema(BRIDGE), conditions, mutations));
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700798
Hyunsun Moon89478662016-06-09 17:52:34 -0700799 // insert an interface
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700800 Interface intf = (Interface) TableGenerator.createTable(dbSchema, OvsdbTable.INTERFACE);
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700801 intf.setName(ovsdbIface.name());
802 intf.setType(ovsdbIface.typeToString());
803 intf.setOptions(ovsdbIface.options());
804 Insert intfInsert = new Insert(dbSchema.getTableSchema(INTERFACE), INTERFACE, intf.getRow());
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700805 operations.add(intfInsert);
806
Hyunsun Moon1251e192016-06-07 16:57:05 -0700807 transactConfig(DATABASENAME, operations);
Hyunsun Moon89478662016-06-09 17:52:34 -0700808 log.info("Created interface {}", ovsdbIface);
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700809 return true;
810 }
811
812 @Override
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700813 public boolean dropInterface(String ifaceName) {
814 OvsdbRowStore rowStore = getRowStore(DATABASENAME, BRIDGE);
815 if (rowStore == null) {
816 log.warn("Failed to get BRIDGE table");
817 return false;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700818 }
819
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700820 ConcurrentMap<String, Row> bridgeTableRows = rowStore.getRowStore();
821 if (bridgeTableRows == null) {
822 log.warn("Failed to get BRIDGE table rows");
823 return false;
824 }
825
826 // interface name is unique
827 Optional<String> bridgeId = bridgeTableRows.keySet().stream()
828 .filter(uuid -> getPortUuid(ifaceName, uuid) != null)
829 .findFirst();
830
831 if (bridgeId.isPresent()) {
832 String portId = getPortUuid(ifaceName, bridgeId.get());
833 deleteConfig(PORT, UUID, portId, BRIDGE, PORTS);
834 return true;
835 } else {
836 log.warn("Unable to find the interface with name {}", ifaceName);
837 return false;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700838 }
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700839 }
840
841 /**
842 * Delete transact config.
843 *
andreaed976a42015-10-05 14:38:25 -0700844 * @param childTableName child table name
845 * @param childColumnName child column name
846 * @param childUuid child row uuid
847 * @param parentTableName parent table name
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700848 * @param parentColumnName parent column
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700849 */
850 private void deleteConfig(String childTableName, String childColumnName,
andreaed976a42015-10-05 14:38:25 -0700851 String childUuid, String parentTableName,
852 String parentColumnName) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700853 DatabaseSchema dbSchema = schema.get(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700854 TableSchema childTableSchema = dbSchema.getTableSchema(childTableName);
855
856 ArrayList<Operation> operations = Lists.newArrayList();
857 if (parentTableName != null && parentColumnName != null) {
858 TableSchema parentTableSchema = dbSchema
859 .getTableSchema(parentTableName);
860 ColumnSchema parentColumnSchema = parentTableSchema
861 .getColumnSchema(parentColumnName);
862 List<Mutation> mutations = Lists.newArrayList();
863 Mutation mutation = MutationUtil.delete(parentColumnSchema.name(),
Jonathan Hart51539b82015-10-29 09:53:04 -0700864 Uuid.uuid(childUuid));
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700865 mutations.add(mutation);
866 List<Condition> conditions = Lists.newArrayList();
867 Condition condition = ConditionUtil.includes(parentColumnName,
Jonathan Hart51539b82015-10-29 09:53:04 -0700868 Uuid.uuid(childUuid));
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700869 conditions.add(condition);
870 Mutate op = new Mutate(parentTableSchema, conditions, mutations);
871 operations.add(op);
872 }
873
874 List<Condition> conditions = Lists.newArrayList();
Jonathan Hart51539b82015-10-29 09:53:04 -0700875 Condition condition = ConditionUtil.isEqual(childColumnName, Uuid.uuid(childUuid));
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700876 conditions.add(condition);
877 Delete del = new Delete(childTableSchema, conditions);
878 operations.add(del);
Hyunsun Moon1251e192016-06-07 16:57:05 -0700879 transactConfig(DATABASENAME, operations);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700880 }
881
882 /**
883 * Update transact config.
884 *
andreaed976a42015-10-05 14:38:25 -0700885 * @param tableName table name
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700886 * @param columnName column name
andreaed976a42015-10-05 14:38:25 -0700887 * @param uuid uuid
888 * @param row the config data
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700889 */
890 private void updateConfig(String tableName, String columnName, String uuid,
andreaed976a42015-10-05 14:38:25 -0700891 Row row) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700892 DatabaseSchema dbSchema = schema.get(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700893 TableSchema tableSchema = dbSchema.getTableSchema(tableName);
894
895 List<Condition> conditions = Lists.newArrayList();
Jonathan Hart51539b82015-10-29 09:53:04 -0700896 Condition condition = ConditionUtil.isEqual(columnName, Uuid.uuid(uuid));
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700897 conditions.add(condition);
898
899 Update update = new Update(tableSchema, row, conditions);
900
901 ArrayList<Operation> operations = Lists.newArrayList();
902 operations.add(update);
903
Hyunsun Moon1251e192016-06-07 16:57:05 -0700904 transactConfig(DATABASENAME, operations);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700905 }
906
907 /**
908 * Insert transact config.
909 *
andreaed976a42015-10-05 14:38:25 -0700910 * @param childTableName child table name
911 * @param childColumnName child column name
912 * @param parentTableName parent table name
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700913 * @param parentColumnName parent column
andreaed976a42015-10-05 14:38:25 -0700914 * @param parentUuid parent uuid
915 * @param row the config data
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700916 * @return uuid, empty if no uuid is find
917 */
918 private String insertConfig(String childTableName, String childColumnName,
andreaed976a42015-10-05 14:38:25 -0700919 String parentTableName, String parentColumnName,
920 String parentUuid, Row row) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700921 DatabaseSchema dbSchema = schema.get(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700922 TableSchema tableSchema = dbSchema.getTableSchema(childTableName);
923
Sho SHIMIZUff18f8c2016-03-11 14:43:53 -0800924 Insert insert = new Insert(tableSchema, childTableName, row);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700925
926 ArrayList<Operation> operations = Lists.newArrayList();
927 operations.add(insert);
928
929 if (parentTableName != null && parentColumnName != null) {
930 TableSchema parentTableSchema = dbSchema
931 .getTableSchema(parentTableName);
932 ColumnSchema parentColumnSchema = parentTableSchema
933 .getColumnSchema(parentColumnName);
934
935 List<Mutation> mutations = Lists.newArrayList();
936 Mutation mutation = MutationUtil.insert(parentColumnSchema.name(),
Sho SHIMIZUff18f8c2016-03-11 14:43:53 -0800937 Uuid.uuid(childTableName));
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700938 mutations.add(mutation);
939
940 List<Condition> conditions = Lists.newArrayList();
Hyunsun Moon1251e192016-06-07 16:57:05 -0700941 Condition condition = ConditionUtil.isEqual(UUID, Uuid.uuid(parentUuid));
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700942 conditions.add(condition);
943
944 Mutate op = new Mutate(parentTableSchema, conditions, mutations);
945 operations.add(op);
946 }
Hyunsun Moon1251e192016-06-07 16:57:05 -0700947 if (childTableName.equalsIgnoreCase(PORT)) {
948 log.debug("Handle port insert");
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700949 Insert intfInsert = handlePortInsertTable(row);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700950
951 if (intfInsert != null) {
952 operations.add(intfInsert);
953 }
954
955 Insert ins = (Insert) operations.get(0);
Hyunsun Moon1251e192016-06-07 16:57:05 -0700956 ins.getRow().put("interfaces", Uuid.uuid(INTERFACE));
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700957 }
958
959 List<OperationResult> results;
960 try {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700961 results = transactConfig(DATABASENAME, operations).get();
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700962 return results.get(0).getUuid().value();
963 } catch (InterruptedException e) {
964 log.warn("Interrupted while waiting to get result");
965 Thread.currentThread().interrupt();
966 } catch (ExecutionException e) {
967 log.error("Exception thrown while to get result");
968 }
969
970 return null;
971 }
972
973 /**
974 * Handles port insert.
975 *
andreaed976a42015-10-05 14:38:25 -0700976 * @param portRow row of port
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700977 * @return insert, empty if null
978 */
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700979 private Insert handlePortInsertTable(Row portRow) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700980 DatabaseSchema dbSchema = schema.get(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700981
Hyunsun Moon1251e192016-06-07 16:57:05 -0700982 TableSchema portTableSchema = dbSchema.getTableSchema(PORT);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700983 ColumnSchema portColumnSchema = portTableSchema.getColumnSchema("name");
984
985 String portName = (String) portRow.getColumn(portColumnSchema.name()).data();
Hyunsun Moon1251e192016-06-07 16:57:05 -0700986 Interface inf = (Interface) TableGenerator.createTable(dbSchema, OvsdbTable.INTERFACE);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700987 inf.setName(portName);
988
Hyunsun Moon1251e192016-06-07 16:57:05 -0700989 TableSchema intfTableSchema = dbSchema.getTableSchema(INTERFACE);
990 return new Insert(intfTableSchema, INTERFACE, inf.getRow());
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700991 }
992
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700993 @Override
994 public ListenableFuture<DatabaseSchema> getOvsdbSchema(String dbName) {
995 if (dbName == null) {
996 return null;
997 }
998 DatabaseSchema databaseSchema = schema.get(dbName);
999 if (databaseSchema == null) {
1000 List<String> dbNames = new ArrayList<String>();
1001 dbNames.add(dbName);
Hyunsun Moon1251e192016-06-07 16:57:05 -07001002 Function<JsonNode, DatabaseSchema> rowFunction = input -> {
1003 log.debug("Get ovsdb database schema {}", dbName);
1004 DatabaseSchema dbSchema = FromJsonUtil.jsonNodeToDbSchema(dbName, input);
1005 if (dbSchema == null) {
1006 log.debug("Get ovsdb database schema error");
1007 return null;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001008 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001009 schema.put(dbName, dbSchema);
1010 return dbSchema;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001011 };
1012
1013 ListenableFuture<JsonNode> input = getSchema(dbNames);
1014 if (input != null) {
1015 return Futures.transform(input, rowFunction);
1016 }
1017 return null;
1018 } else {
1019 return Futures.immediateFuture(databaseSchema);
1020 }
1021 }
1022
1023 @Override
1024 public ListenableFuture<TableUpdates> monitorTables(String dbName, String id) {
1025 if (dbName == null) {
1026 return null;
1027 }
1028 DatabaseSchema dbSchema = schema.get(dbName);
1029 if (dbSchema != null) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001030 Function<JsonNode, TableUpdates> rowFunction = input -> {
1031 log.debug("Get table updates");
1032 TableUpdates updates = FromJsonUtil.jsonNodeToTableUpdates(input, dbSchema);
1033 if (updates == null) {
1034 log.debug("Get table updates error");
1035 return null;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001036 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001037 return updates;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001038 };
1039 return Futures.transform(monitor(dbSchema, id), rowFunction);
1040 }
1041 return null;
1042 }
1043
Hyunsun Moondd14e8e2016-06-09 16:17:32 -07001044 private ListenableFuture<List<OperationResult>> transactConfig(String dbName,
1045 List<Operation> operations) {
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001046 if (dbName == null) {
1047 return null;
1048 }
1049 DatabaseSchema dbSchema = schema.get(dbName);
1050 if (dbSchema != null) {
andreaed976a42015-10-05 14:38:25 -07001051 Function<List<JsonNode>, List<OperationResult>> rowFunction = (input -> {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001052 log.debug("Get ovsdb operation result");
1053 List<OperationResult> result = FromJsonUtil.jsonNodeToOperationResult(input, operations);
andreaed976a42015-10-05 14:38:25 -07001054 if (result == null) {
1055 log.debug("The operation result is null");
1056 return null;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001057 }
andreaed976a42015-10-05 14:38:25 -07001058 return result;
1059 });
Hyunsun Moon1251e192016-06-07 16:57:05 -07001060 return Futures.transform(transact(dbSchema, operations), rowFunction);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001061 }
1062 return null;
1063 }
1064
1065 @Override
1066 public ListenableFuture<JsonNode> getSchema(List<String> dbnames) {
1067 String id = java.util.UUID.randomUUID().toString();
1068 String getSchemaString = JsonRpcWriterUtil.getSchemaStr(id, dbnames);
1069
1070 SettableFuture<JsonNode> sf = SettableFuture.create();
1071 requestResult.put(id, sf);
1072 requestMethod.put(id, "getSchema");
1073
1074 channel.writeAndFlush(getSchemaString);
1075 return sf;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001076 }
1077
1078 @Override
1079 public ListenableFuture<List<String>> echo() {
1080 String id = java.util.UUID.randomUUID().toString();
1081 String echoString = JsonRpcWriterUtil.echoStr(id);
1082
1083 SettableFuture<List<String>> sf = SettableFuture.create();
1084 requestResult.put(id, sf);
1085 requestMethod.put(id, "echo");
1086
1087 channel.writeAndFlush(echoString);
1088 return sf;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001089 }
1090
1091 @Override
1092 public ListenableFuture<JsonNode> monitor(DatabaseSchema dbSchema,
1093 String monitorId) {
1094 String id = java.util.UUID.randomUUID().toString();
1095 String monitorString = JsonRpcWriterUtil.monitorStr(id, monitorId,
1096 dbSchema);
1097
1098 SettableFuture<JsonNode> sf = SettableFuture.create();
1099 requestResult.put(id, sf);
1100 requestMethod.put(id, "monitor");
1101
1102 channel.writeAndFlush(monitorString);
1103 return sf;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001104 }
1105
1106 @Override
1107 public ListenableFuture<List<String>> listDbs() {
1108 String id = java.util.UUID.randomUUID().toString();
1109 String listDbsString = JsonRpcWriterUtil.listDbsStr(id);
1110
1111 SettableFuture<List<String>> sf = SettableFuture.create();
1112 requestResult.put(id, sf);
1113 requestMethod.put(id, "listDbs");
1114
1115 channel.writeAndFlush(listDbsString);
1116 return sf;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001117 }
1118
1119 @Override
1120 public ListenableFuture<List<JsonNode>> transact(DatabaseSchema dbSchema,
1121 List<Operation> operations) {
1122 String id = java.util.UUID.randomUUID().toString();
1123 String transactString = JsonRpcWriterUtil.transactStr(id, dbSchema,
1124 operations);
1125
1126 SettableFuture<List<JsonNode>> sf = SettableFuture.create();
1127 requestResult.put(id, sf);
1128 requestMethod.put(id, "transact");
1129
1130 channel.writeAndFlush(transactString);
1131 return sf;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001132 }
1133
andreaed976a42015-10-05 14:38:25 -07001134 @SuppressWarnings({"rawtypes", "unchecked"})
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001135 @Override
1136 public void processResult(JsonNode response) {
1137 log.debug("Handle result");
1138 String requestId = response.get("id").asText();
1139 SettableFuture sf = requestResult.get(requestId);
1140 if (sf == null) {
1141 log.debug("No such future to process");
1142 return;
1143 }
1144 String methodName = requestMethod.get(requestId);
Hyunsun Moon1251e192016-06-07 16:57:05 -07001145 sf.set(FromJsonUtil.jsonResultParser(response, methodName));
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001146 }
1147
1148 @Override
1149 public void processRequest(JsonNode requestJson) {
1150 log.debug("Handle request");
1151 if (requestJson.get("method").asText().equalsIgnoreCase("echo")) {
1152 log.debug("handle echo request");
1153
1154 String replyString = FromJsonUtil.getEchoRequestStr(requestJson);
1155 channel.writeAndFlush(replyString);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001156 } else {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001157 FromJsonUtil.jsonCallbackRequestParser(requestJson, monitorCallBack);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001158 }
1159 }
1160
1161 @Override
1162 public void setCallback(Callback monitorCallback) {
1163 this.monitorCallBack = monitorCallback;
1164 }
1165
1166 @Override
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001167 public Set<OvsdbBridge> getBridges() {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001168 Set<OvsdbBridge> ovsdbBridges = new HashSet<>();
1169 OvsdbTableStore tableStore = getTableStore(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001170 if (tableStore == null) {
MaoJianweidac220d2016-07-04 22:37:52 +08001171 return ovsdbBridges;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001172 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001173 OvsdbRowStore rowStore = tableStore.getRows(BRIDGE);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001174 if (rowStore == null) {
MaoJianweidac220d2016-07-04 22:37:52 +08001175 return ovsdbBridges;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001176 }
1177 ConcurrentMap<String, Row> rows = rowStore.getRowStore();
1178 for (String uuid : rows.keySet()) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001179 Row row = getRow(DATABASENAME, BRIDGE, uuid);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001180 OvsdbBridge ovsdbBridge = getOvsdbBridge(row);
1181 if (ovsdbBridge != null) {
1182 ovsdbBridges.add(ovsdbBridge);
1183 }
1184 }
1185 return ovsdbBridges;
1186 }
1187
1188 @Override
andreaed976a42015-10-05 14:38:25 -07001189 public Set<ControllerInfo> getControllers(DeviceId openflowDeviceId) {
Jonathan Hart51539b82015-10-29 09:53:04 -07001190 Uuid bridgeUuid = getBridgeUuid(openflowDeviceId);
andreaed976a42015-10-05 14:38:25 -07001191 if (bridgeUuid == null) {
1192 log.warn("bad bridge Uuid");
1193 return null;
1194 }
1195 List<Controller> controllers = getControllers(bridgeUuid);
1196 if (controllers == null) {
1197 log.warn("bad list of controllers");
1198 return null;
1199 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001200 return controllers.stream().map(controller -> new ControllerInfo(
1201 (String) controller.getTargetColumn()
1202 .data())).collect(Collectors.toSet());
andreaed976a42015-10-05 14:38:25 -07001203 }
1204
Jonathan Hart51539b82015-10-29 09:53:04 -07001205 private List<Controller> getControllers(Uuid bridgeUuid) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001206 DatabaseSchema dbSchema = schema.get(DATABASENAME);
andreaed976a42015-10-05 14:38:25 -07001207 if (dbSchema == null) {
1208 return null;
1209 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001210 OvsdbRowStore rowStore = getRowStore(DATABASENAME, BRIDGE);
andreaed976a42015-10-05 14:38:25 -07001211 if (rowStore == null) {
1212 log.debug("There is no bridge table");
1213 return null;
1214 }
1215
1216 Row bridgeRow = rowStore.getRow(bridgeUuid.value());
1217 Bridge bridge = (Bridge) TableGenerator.
1218 getTable(dbSchema, bridgeRow, OvsdbTable.BRIDGE);
1219
1220 //FIXME remove log
1221 log.warn("type of controller column", bridge.getControllerColumn()
1222 .data().getClass());
Jonathan Hart51539b82015-10-29 09:53:04 -07001223 Set<Uuid> controllerUuids = (Set<Uuid>) ((OvsdbSet) bridge
andreaed976a42015-10-05 14:38:25 -07001224 .getControllerColumn().data()).set();
andreaed976a42015-10-05 14:38:25 -07001225
Hyunsun Moon1251e192016-06-07 16:57:05 -07001226 OvsdbRowStore controllerRowStore = getRowStore(DATABASENAME, CONTROLLER);
andreaed976a42015-10-05 14:38:25 -07001227 if (controllerRowStore == null) {
1228 log.debug("There is no controller table");
1229 return null;
1230 }
1231
1232 List<Controller> ovsdbControllers = new ArrayList<>();
1233 ConcurrentMap<String, Row> controllerTableRows = controllerRowStore.getRowStore();
1234 controllerTableRows.forEach((key, row) -> {
Jonathan Hart51539b82015-10-29 09:53:04 -07001235 if (!controllerUuids.contains(Uuid.uuid(key))) {
andreaed976a42015-10-05 14:38:25 -07001236 return;
1237 }
1238 Controller controller = (Controller) TableGenerator
1239 .getTable(dbSchema, row, OvsdbTable.CONTROLLER);
1240 ovsdbControllers.add(controller);
1241 });
1242 return ovsdbControllers;
1243 }
1244
1245
Jonathan Hart51539b82015-10-29 09:53:04 -07001246 private Uuid getBridgeUuid(DeviceId openflowDeviceId) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001247 DatabaseSchema dbSchema = schema.get(DATABASENAME);
andreaed976a42015-10-05 14:38:25 -07001248 if (dbSchema == null) {
1249 return null;
1250 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001251 OvsdbRowStore rowStore = getRowStore(DATABASENAME, BRIDGE);
andreaed976a42015-10-05 14:38:25 -07001252 if (rowStore == null) {
1253 log.debug("There is no bridge table");
1254 return null;
1255 }
1256
1257 ConcurrentMap<String, Row> bridgeTableRows = rowStore.getRowStore();
Jonathan Hart51539b82015-10-29 09:53:04 -07001258 final AtomicReference<Uuid> uuid = new AtomicReference<>();
andreaed976a42015-10-05 14:38:25 -07001259 for (Map.Entry<String, Row> entry : bridgeTableRows.entrySet()) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001260 Bridge bridge = (Bridge) TableGenerator.getTable(
1261 dbSchema,
1262 entry.getValue(),
1263 OvsdbTable.BRIDGE);
1264
1265 if (matchesDpid(bridge, openflowDeviceId)) {
Jonathan Hart51539b82015-10-29 09:53:04 -07001266 uuid.set(Uuid.uuid(entry.getKey()));
andreaed976a42015-10-05 14:38:25 -07001267 break;
1268 }
1269 }
1270 if (uuid.get() == null) {
1271 log.debug("There is no bridge for {}", openflowDeviceId);
1272 }
1273 return uuid.get();
andreaed976a42015-10-05 14:38:25 -07001274 }
1275
1276 private static boolean matchesDpid(Bridge b, DeviceId deviceId) {
1277 String ofDpid = deviceId.toString().replace("of:", "");
1278 Set ofDeviceIds = ((OvsdbSet) b.getDatapathIdColumn().data()).set();
1279 //TODO Set<String>
1280 return ofDeviceIds.contains(ofDpid);
1281 }
1282
1283 @Override
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001284 public Set<OvsdbPort> getPorts() {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001285 Set<OvsdbPort> ovsdbPorts = new HashSet<>();
1286 OvsdbTableStore tableStore = getTableStore(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001287 if (tableStore == null) {
1288 return null;
1289 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001290 OvsdbRowStore rowStore = tableStore.getRows(INTERFACE);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001291 if (rowStore == null) {
1292 return null;
1293 }
1294 ConcurrentMap<String, Row> rows = rowStore.getRowStore();
1295 for (String uuid : rows.keySet()) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001296 Row row = getRow(DATABASENAME, INTERFACE, uuid);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001297 OvsdbPort ovsdbPort = getOvsdbPort(row);
1298 if (ovsdbPort != null) {
1299 ovsdbPorts.add(ovsdbPort);
1300 }
1301 }
1302 return ovsdbPorts;
1303 }
1304
1305 @Override
1306 public DatabaseSchema getDatabaseSchema(String dbName) {
1307 return schema.get(dbName);
1308 }
1309
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001310 private OvsdbPort getOvsdbPort(Row row) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001311 DatabaseSchema dbSchema = getDatabaseSchema(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001312 Interface intf = (Interface) TableGenerator
1313 .getTable(dbSchema, row, OvsdbTable.INTERFACE);
1314 if (intf == null) {
1315 return null;
1316 }
1317 long ofPort = getOfPort(intf);
1318 String portName = intf.getName();
1319 if ((ofPort < 0) || (portName == null)) {
1320 return null;
1321 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001322 return new OvsdbPort(new OvsdbPortNumber(ofPort), new OvsdbPortName(portName));
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001323 }
1324
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001325 private OvsdbBridge getOvsdbBridge(Row row) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001326 DatabaseSchema dbSchema = getDatabaseSchema(DATABASENAME);
1327 Bridge bridge = (Bridge) TableGenerator.getTable(dbSchema, row, OvsdbTable.BRIDGE);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001328 if (bridge == null) {
1329 return null;
1330 }
1331
1332 OvsdbSet datapathIdSet = (OvsdbSet) bridge.getDatapathIdColumn().data();
1333 @SuppressWarnings("unchecked")
1334 Set<String> datapathIds = datapathIdSet.set();
Jon Hallcbd1b392017-01-18 20:15:44 -08001335 if (datapathIds == null || datapathIds.isEmpty()) {
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001336 return null;
1337 }
1338 String datapathId = (String) datapathIds.toArray()[0];
1339 String bridgeName = bridge.getName();
1340 if ((datapathId == null) || (bridgeName == null)) {
1341 return null;
1342 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001343 return OvsdbBridge.builder().name(bridgeName).datapathId(datapathId).build();
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001344 }
1345
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001346 private long getOfPort(Interface intf) {
1347 OvsdbSet ofPortSet = (OvsdbSet) intf.getOpenFlowPortColumn().data();
1348 @SuppressWarnings("unchecked")
1349 Set<Integer> ofPorts = ofPortSet.set();
Jon Hallcbd1b392017-01-18 20:15:44 -08001350 if (ofPorts == null || ofPorts.isEmpty()) {
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001351 log.debug("The ofport is null in {}", intf.getName());
1352 return -1;
1353 }
1354 // return (long) ofPorts.toArray()[0];
1355 Iterator<Integer> it = ofPorts.iterator();
1356 return Long.parseLong(it.next().toString());
1357 }
CNluciusa66c3972015-09-06 20:31:29 +08001358
1359 @Override
1360 public Set<OvsdbPort> getLocalPorts(Iterable<String> ifaceids) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001361 Set<OvsdbPort> ovsdbPorts = new HashSet<>();
1362 OvsdbTableStore tableStore = getTableStore(DATABASENAME);
CNluciusa66c3972015-09-06 20:31:29 +08001363 if (tableStore == null) {
1364 return null;
1365 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001366 OvsdbRowStore rowStore = tableStore.getRows(INTERFACE);
CNluciusa66c3972015-09-06 20:31:29 +08001367 if (rowStore == null) {
1368 return null;
1369 }
1370 ConcurrentMap<String, Row> rows = rowStore.getRowStore();
1371 for (String uuid : rows.keySet()) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001372 Row row = getRow(DATABASENAME, INTERFACE, uuid);
1373 DatabaseSchema dbSchema = getDatabaseSchema(DATABASENAME);
CNluciusa66c3972015-09-06 20:31:29 +08001374 Interface intf = (Interface) TableGenerator
1375 .getTable(dbSchema, row, OvsdbTable.INTERFACE);
1376 if (intf == null || getIfaceid(intf) == null) {
1377 continue;
1378 }
1379 String portName = intf.getName();
Hyunsun Moon1251e192016-06-07 16:57:05 -07001380 if (portName == null) {
1381 continue;
1382 }
CNluciusa66c3972015-09-06 20:31:29 +08001383 Set<String> ifaceidSet = Sets.newHashSet(ifaceids);
Hyunsun Moon1251e192016-06-07 16:57:05 -07001384 if (portName.startsWith(TYPEVXLAN) || !ifaceidSet.contains(getIfaceid(intf))) {
CNluciusa66c3972015-09-06 20:31:29 +08001385 continue;
1386 }
1387 long ofPort = getOfPort(intf);
Hyunsun Moon1251e192016-06-07 16:57:05 -07001388 if (ofPort < 0) {
CNluciusa66c3972015-09-06 20:31:29 +08001389 continue;
1390 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001391 ovsdbPorts.add(new OvsdbPort(new OvsdbPortNumber(ofPort),
1392 new OvsdbPortName(portName)));
CNluciusa66c3972015-09-06 20:31:29 +08001393 }
1394 return ovsdbPorts;
1395 }
1396
1397 private String getIfaceid(Interface intf) {
1398 OvsdbMap ovsdbMap = (OvsdbMap) intf.getExternalIdsColumn().data();
1399 @SuppressWarnings("unchecked")
1400 Map<String, String> externalIds = ovsdbMap.map();
1401 if (externalIds.isEmpty()) {
1402 log.warn("The external_ids is null");
1403 return null;
1404 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001405 String ifaceid = externalIds.get(EXTERNAL_ID_INTERFACE_ID);
CNluciusa66c3972015-09-06 20:31:29 +08001406 if (ifaceid == null) {
1407 log.warn("The ifaceid is null");
1408 return null;
1409 }
1410 return ifaceid;
1411 }
Hyunsun Moon5fb20a52015-09-25 17:02:33 -07001412
1413 @Override
1414 public void disconnect() {
1415 channel.disconnect();
1416 this.agent.removeConnectedNode(nodeId);
1417 }
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001418}