blob: 3f8ec1c8f344dcca7ce40b4f0ac1c26c3821be5e [file] [log] [blame]
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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
18import io.netty.channel.Channel;
lishuai6c56f5e2015-11-17 16:38:19 +080019
Ray Milkeyb1250322017-06-05 17:18:17 -070020import java.net.InetSocketAddress;
21import java.util.ArrayList;
22import java.util.HashMap;
23import java.util.HashSet;
24import java.util.Iterator;
25import java.util.List;
26import java.util.Map;
27import java.util.Objects;
28import java.util.Optional;
29import java.util.Set;
30import java.util.concurrent.ConcurrentMap;
31import java.util.concurrent.ExecutionException;
32import java.util.concurrent.TimeUnit;
33import java.util.concurrent.TimeoutException;
34import java.util.concurrent.atomic.AtomicReference;
Saritha1583a6f2017-06-16 14:42:58 +053035import java.util.function.Consumer;
36import java.util.Collections;
Ray Milkeyb1250322017-06-05 17:18:17 -070037import java.util.stream.Collectors;
38
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070039import org.onlab.packet.IpAddress;
andreaed976a42015-10-05 14:38:25 -070040import org.onosproject.net.DeviceId;
Frank Wange11a98d2016-10-26 17:04:03 +080041import org.onosproject.net.PortNumber;
andreaed976a42015-10-05 14:38:25 -070042import org.onosproject.net.behaviour.ControllerInfo;
Pier Ventref5d72362016-07-17 12:02:14 +020043import org.onosproject.net.behaviour.MirroringName;
Ray Milkeyb1250322017-06-05 17:18:17 -070044import org.onosproject.net.behaviour.MirroringStatistics;
Frank Wange11a98d2016-10-26 17:04:03 +080045import org.onosproject.net.behaviour.QosId;
46import org.onosproject.net.behaviour.QueueId;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070047import org.onosproject.ovsdb.controller.OvsdbBridge;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070048import org.onosproject.ovsdb.controller.OvsdbClientService;
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070049import org.onosproject.ovsdb.controller.OvsdbInterface;
Pier Ventref5d72362016-07-17 12:02:14 +020050import org.onosproject.ovsdb.controller.OvsdbMirror;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070051import org.onosproject.ovsdb.controller.OvsdbNodeId;
52import org.onosproject.ovsdb.controller.OvsdbPort;
53import org.onosproject.ovsdb.controller.OvsdbPortName;
54import org.onosproject.ovsdb.controller.OvsdbPortNumber;
Frank Wange11a98d2016-10-26 17:04:03 +080055import org.onosproject.ovsdb.controller.OvsdbQos;
56import org.onosproject.ovsdb.controller.OvsdbQueue;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070057import org.onosproject.ovsdb.controller.OvsdbRowStore;
58import org.onosproject.ovsdb.controller.OvsdbStore;
59import org.onosproject.ovsdb.controller.OvsdbTableStore;
Saritha1583a6f2017-06-16 14:42:58 +053060import org.onosproject.ovsdb.rfc.exception.ColumnSchemaNotFoundException;
61import org.onosproject.ovsdb.rfc.exception.VersionMismatchException;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070062import org.onosproject.ovsdb.rfc.jsonrpc.Callback;
63import org.onosproject.ovsdb.rfc.message.OperationResult;
64import org.onosproject.ovsdb.rfc.message.TableUpdates;
Frank Wange11a98d2016-10-26 17:04:03 +080065import org.onosproject.ovsdb.rfc.notation.Column;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070066import org.onosproject.ovsdb.rfc.notation.Condition;
67import org.onosproject.ovsdb.rfc.notation.Mutation;
CNluciusa66c3972015-09-06 20:31:29 +080068import org.onosproject.ovsdb.rfc.notation.OvsdbMap;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070069import org.onosproject.ovsdb.rfc.notation.OvsdbSet;
70import org.onosproject.ovsdb.rfc.notation.Row;
Jonathan Hart51539b82015-10-29 09:53:04 -070071import org.onosproject.ovsdb.rfc.notation.Uuid;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070072import org.onosproject.ovsdb.rfc.operations.Delete;
73import org.onosproject.ovsdb.rfc.operations.Insert;
74import org.onosproject.ovsdb.rfc.operations.Mutate;
75import org.onosproject.ovsdb.rfc.operations.Operation;
76import org.onosproject.ovsdb.rfc.operations.Update;
77import org.onosproject.ovsdb.rfc.schema.ColumnSchema;
78import org.onosproject.ovsdb.rfc.schema.DatabaseSchema;
79import org.onosproject.ovsdb.rfc.schema.TableSchema;
80import org.onosproject.ovsdb.rfc.table.Bridge;
81import org.onosproject.ovsdb.rfc.table.Controller;
82import org.onosproject.ovsdb.rfc.table.Interface;
Pier Ventref5d72362016-07-17 12:02:14 +020083import org.onosproject.ovsdb.rfc.table.Mirror;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070084import org.onosproject.ovsdb.rfc.table.OvsdbTable;
85import org.onosproject.ovsdb.rfc.table.Port;
Frank Wange11a98d2016-10-26 17:04:03 +080086import org.onosproject.ovsdb.rfc.table.Qos;
87import org.onosproject.ovsdb.rfc.table.Queue;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070088import org.onosproject.ovsdb.rfc.table.TableGenerator;
89import org.onosproject.ovsdb.rfc.utils.ConditionUtil;
90import org.onosproject.ovsdb.rfc.utils.FromJsonUtil;
91import org.onosproject.ovsdb.rfc.utils.JsonRpcWriterUtil;
92import org.onosproject.ovsdb.rfc.utils.MutationUtil;
93import org.slf4j.Logger;
94import org.slf4j.LoggerFactory;
95
Ray Milkeyb1250322017-06-05 17:18:17 -070096import com.fasterxml.jackson.databind.JsonNode;
97import com.google.common.base.Function;
98import com.google.common.collect.ImmutableList;
99import com.google.common.collect.ImmutableSet;
100import com.google.common.collect.Lists;
101import com.google.common.collect.Maps;
102import com.google.common.collect.Sets;
103import com.google.common.util.concurrent.Futures;
104import com.google.common.util.concurrent.ListenableFuture;
105import com.google.common.util.concurrent.SettableFuture;
Pier Ventref5d72362016-07-17 12:02:14 +0200106
Ray Milkeyb1250322017-06-05 17:18:17 -0700107import static org.onosproject.ovsdb.controller.OvsdbConstant.BRIDGE;
108import static org.onosproject.ovsdb.controller.OvsdbConstant.BRIDGES;
109import static org.onosproject.ovsdb.controller.OvsdbConstant.CONTROLLER;
110import static org.onosproject.ovsdb.controller.OvsdbConstant.DATABASENAME;
111import static org.onosproject.ovsdb.controller.OvsdbConstant.EXTERNAL_ID;
112import static org.onosproject.ovsdb.controller.OvsdbConstant.EXTERNAL_ID_INTERFACE_ID;
113import static org.onosproject.ovsdb.controller.OvsdbConstant.INTERFACE;
114import static org.onosproject.ovsdb.controller.OvsdbConstant.INTERFACES;
115import static org.onosproject.ovsdb.controller.OvsdbConstant.MIRROR;
116import static org.onosproject.ovsdb.controller.OvsdbConstant.MIRRORS;
117import static org.onosproject.ovsdb.controller.OvsdbConstant.OFPORT;
118import static org.onosproject.ovsdb.controller.OvsdbConstant.PORT;
119import static org.onosproject.ovsdb.controller.OvsdbConstant.PORTS;
120import static org.onosproject.ovsdb.controller.OvsdbConstant.PORT_QOS;
121import static org.onosproject.ovsdb.controller.OvsdbConstant.QOS;
122import static org.onosproject.ovsdb.controller.OvsdbConstant.QOS_EXTERNAL_ID_KEY;
123import static org.onosproject.ovsdb.controller.OvsdbConstant.QUEUE;
124import static org.onosproject.ovsdb.controller.OvsdbConstant.QUEUES;
125import static org.onosproject.ovsdb.controller.OvsdbConstant.QUEUE_EXTERNAL_ID_KEY;
126import static org.onosproject.ovsdb.controller.OvsdbConstant.TYPEVXLAN;
127import static org.onosproject.ovsdb.controller.OvsdbConstant.UUID;
Saritha8a0c36a2017-07-04 15:01:35 +0530128import static org.onosproject.ovsdb.controller.OvsdbConstant.BRIDGE_CONTROLLER;
Saritha1583a6f2017-06-16 14:42:58 +0530129import static org.onosproject.ovsdb.controller.OvsdbConstant.OFPORT_ERROR;
Hyunsun Moon1251e192016-06-07 16:57:05 -0700130
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700131/**
132 * An representation of an ovsdb client.
133 */
Hyunsun Moon1251e192016-06-07 16:57:05 -0700134public class DefaultOvsdbClient implements OvsdbProviderService, OvsdbClientService {
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700135
jaegonkim01d7c912017-01-22 22:03:38 +0900136 private static final int TRANSACTCONFIG_TIMEOUT = 3; //sec
Saritha1583a6f2017-06-16 14:42:58 +0530137 private static final int OFPORT_ERROR_COMPARISON = 0;
jaegonkim01d7c912017-01-22 22:03:38 +0900138
Hyunsun Moon1251e192016-06-07 16:57:05 -0700139 private final Logger log = LoggerFactory.getLogger(DefaultOvsdbClient.class);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700140
141 private Channel channel;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700142 private OvsdbAgent agent;
143 private boolean connected;
144 private OvsdbNodeId nodeId;
145 private Callback monitorCallBack;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700146 private OvsdbStore ovsdbStore = new OvsdbStore();
147
148 private final Map<String, String> requestMethod = Maps.newHashMap();
Hyunsun Moon1251e192016-06-07 16:57:05 -0700149 private final Map<String, SettableFuture<? extends Object>> requestResult = Maps.newHashMap();
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700150 private final Map<String, DatabaseSchema> schema = Maps.newHashMap();
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700151
Pier Ventref5d72362016-07-17 12:02:14 +0200152
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700153 /**
154 * Creates an OvsdbClient.
155 *
156 * @param nodeId ovsdb node id
157 */
158 public DefaultOvsdbClient(OvsdbNodeId nodeId) {
159 this.nodeId = nodeId;
160 }
161
162 @Override
163 public OvsdbNodeId nodeId() {
164 return nodeId;
165 }
166
167 @Override
168 public void setAgent(OvsdbAgent agent) {
169 if (this.agent == null) {
170 this.agent = agent;
171 }
172 }
173
174 @Override
175 public void setChannel(Channel channel) {
176 this.channel = channel;
177 }
178
179 @Override
180 public void setConnection(boolean connected) {
181 this.connected = connected;
182 }
183
184 @Override
185 public boolean isConnected() {
186 return this.connected;
187 }
188
189 @Override
190 public void nodeAdded() {
191 this.agent.addConnectedNode(nodeId, this);
192 }
193
194 @Override
195 public void nodeRemoved() {
196 this.agent.removeConnectedNode(nodeId);
197 channel.disconnect();
198 }
199
200 /**
201 * Gets the ovsdb table store.
202 *
203 * @param dbName the ovsdb database name
204 * @return ovsTableStore, empty if table store is find
205 */
206 private OvsdbTableStore getTableStore(String dbName) {
207 if (ovsdbStore == null) {
208 return null;
209 }
210 return ovsdbStore.getOvsdbTableStore(dbName);
211 }
212
213 /**
214 * Gets the ovsdb row store.
215 *
andreaed976a42015-10-05 14:38:25 -0700216 * @param dbName the ovsdb database name
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700217 * @param tableName the ovsdb table name
Hyunsun Moon6125c612015-10-15 10:54:44 -0700218 * @return ovsRowStore, empty store if no rows exist in the table
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700219 */
220 private OvsdbRowStore getRowStore(String dbName, String tableName) {
221 OvsdbTableStore tableStore = getTableStore(dbName);
222 if (tableStore == null) {
223 return null;
224 }
Hyunsun Moon6125c612015-10-15 10:54:44 -0700225
226 OvsdbRowStore rowStore = tableStore.getRows(tableName);
227 if (rowStore == null) {
228 rowStore = new OvsdbRowStore();
229 }
230 return rowStore;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700231 }
232
233 /**
234 * Gets the ovsdb row.
235 *
andreaed976a42015-10-05 14:38:25 -0700236 * @param dbName the ovsdb database name
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700237 * @param tableName the ovsdb table name
andreaed976a42015-10-05 14:38:25 -0700238 * @param uuid the key of the row
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700239 * @return row, empty if row is find
240 */
241 @Override
242 public Row getRow(String dbName, String tableName, String uuid) {
243 OvsdbTableStore tableStore = getTableStore(dbName);
244 if (tableStore == null) {
245 return null;
246 }
247 OvsdbRowStore rowStore = tableStore.getRows(tableName);
248 if (rowStore == null) {
249 return null;
250 }
251 return rowStore.getRow(uuid);
252 }
253
254 @Override
255 public void removeRow(String dbName, String tableName, String uuid) {
256 OvsdbTableStore tableStore = getTableStore(dbName);
257 if (tableStore == null) {
258 return;
259 }
260 OvsdbRowStore rowStore = tableStore.getRows(tableName);
261 if (rowStore == null) {
262 return;
263 }
264 rowStore.deleteRow(uuid);
265 }
266
267 @Override
268 public void updateOvsdbStore(String dbName, String tableName, String uuid,
269 Row row) {
270 OvsdbTableStore tableStore = ovsdbStore.getOvsdbTableStore(dbName);
271 if (tableStore == null) {
272 tableStore = new OvsdbTableStore();
273 }
274 OvsdbRowStore rowStore = tableStore.getRows(tableName);
275 if (rowStore == null) {
276 rowStore = new OvsdbRowStore();
277 }
278 rowStore.insertRow(uuid, row);
279 tableStore.createOrUpdateTable(tableName, rowStore);
280 ovsdbStore.createOrUpdateOvsdbStore(dbName, tableStore);
281 }
282
Pier Ventref5d72362016-07-17 12:02:14 +0200283 /**
284 * Gets the Mirror uuid.
285 *
286 * @param mirrorName mirror name
287 * @return mirror uuid, empty if no uuid is found
288 */
289 @Override
290 public String getMirrorUuid(String mirrorName) {
291 DatabaseSchema dbSchema = schema.get(DATABASENAME);
292 OvsdbRowStore rowStore = getRowStore(DATABASENAME, MIRROR);
293 if (rowStore == null) {
294 log.warn("The mirror uuid is null");
295 return null;
296 }
297
298 ConcurrentMap<String, Row> mirrorTableRows = rowStore.getRowStore();
299 if (mirrorTableRows == null) {
300 log.warn("The mirror uuid is null");
301 return null;
302 }
303
304 for (String uuid : mirrorTableRows.keySet()) {
305 Mirror mirror = (Mirror) TableGenerator
306 .getTable(dbSchema, mirrorTableRows.get(uuid), OvsdbTable.MIRROR);
307 String name = mirror.getName();
308 if (name.contains(mirrorName)) {
309 return uuid;
310 }
311 }
312 log.warn("Mirroring not found");
313 return null;
314 }
315
316 /**
317 * Gets mirrors of the device.
318 *
319 * @param deviceId target device id
320 * @return set of mirroring; empty if no mirror is found
321 */
322 @Override
323 public Set<MirroringStatistics> getMirroringStatistics(DeviceId deviceId) {
324 Uuid bridgeUuid = getBridgeUuid(deviceId);
325 if (bridgeUuid == null) {
326 log.warn("Couldn't find bridge {} in {}", deviceId, nodeId.getIpAddress());
327 return null;
328 }
329
330 List<MirroringStatistics> mirrorings = getMirrorings(bridgeUuid);
331 if (mirrorings == null) {
332 log.warn("Couldn't find mirrors in {}", nodeId.getIpAddress());
333 return null;
334 }
335 return ImmutableSet.copyOf(mirrorings);
336 }
337
338 /**
339 * Helper method which retrieves mirrorings statistics using bridge uuid.
340 *
341 * @param bridgeUuid the uuid of the bridge
342 * @return the list of the mirrorings statistics.
343 */
344 private List<MirroringStatistics> getMirrorings(Uuid bridgeUuid) {
345 DatabaseSchema dbSchema = schema.get(DATABASENAME);
346 if (dbSchema == null) {
347 log.warn("Unable to retrieve dbSchema {}", DATABASENAME);
348 return null;
349 }
350 OvsdbRowStore rowStore = getRowStore(DATABASENAME, BRIDGE);
351 if (rowStore == null) {
352 log.warn("Unable to retrieve rowStore {} of {}", BRIDGE, DATABASENAME);
353 return null;
354 }
355
356 Row bridgeRow = rowStore.getRow(bridgeUuid.value());
357 Bridge bridge = (Bridge) TableGenerator.
358 getTable(dbSchema, bridgeRow, OvsdbTable.BRIDGE);
359
360 Set<Uuid> mirroringsUuids = (Set<Uuid>) ((OvsdbSet) bridge
361 .getMirrorsColumn().data()).set();
362
363 OvsdbRowStore mirrorRowStore = getRowStore(DATABASENAME, MIRROR);
364 if (mirrorRowStore == null) {
365 log.warn("Unable to retrieve rowStore {} of {}", MIRROR, DATABASENAME);
366 return null;
367 }
368
369 List<MirroringStatistics> mirroringStatistics = new ArrayList<>();
370 ConcurrentMap<String, Row> mirrorTableRows = mirrorRowStore.getRowStore();
371 mirrorTableRows.forEach((key, row) -> {
372 if (!mirroringsUuids.contains(Uuid.uuid(key))) {
373 return;
374 }
375 Mirror mirror = (Mirror) TableGenerator
376 .getTable(dbSchema, row, OvsdbTable.MIRROR);
377 mirroringStatistics.add(MirroringStatistics.mirroringStatistics(mirror.getName(),
378 (Map<String, Integer>) ((OvsdbMap) mirror
379 .getStatisticsColumn().data()).map()));
380 });
381 return ImmutableList.copyOf(mirroringStatistics);
382 }
383
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700384 @Override
385 public String getPortUuid(String portName, String bridgeUuid) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700386 DatabaseSchema dbSchema = schema.get(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700387
Hyunsun Moon1251e192016-06-07 16:57:05 -0700388 Row bridgeRow = getRow(DATABASENAME, BRIDGE, bridgeUuid);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700389 Bridge bridge = (Bridge) TableGenerator.getTable(dbSchema, bridgeRow,
390 OvsdbTable.BRIDGE);
391 if (bridge != null) {
392 OvsdbSet setPorts = (OvsdbSet) bridge.getPortsColumn().data();
393 @SuppressWarnings("unchecked")
Jonathan Hart51539b82015-10-29 09:53:04 -0700394 Set<Uuid> ports = setPorts.set();
Jon Hallcbd1b392017-01-18 20:15:44 -0800395 if (ports == null || ports.isEmpty()) {
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700396 log.warn("The port uuid is null");
397 return null;
398 }
399
Jonathan Hart51539b82015-10-29 09:53:04 -0700400 for (Uuid uuid : ports) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700401 Row portRow = getRow(DATABASENAME, PORT, uuid.value());
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700402 Port port = (Port) TableGenerator.getTable(dbSchema, portRow,
403 OvsdbTable.PORT);
404 if (port != null && portName.equalsIgnoreCase(port.getName())) {
405 return uuid.value();
406 }
407 }
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700408 }
409 return null;
410 }
411
412 @Override
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700413 public String getBridgeUuid(String bridgeName) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700414 DatabaseSchema dbSchema = schema.get(DATABASENAME);
Hyunsun Moon1251e192016-06-07 16:57:05 -0700415 OvsdbRowStore rowStore = getRowStore(DATABASENAME, BRIDGE);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700416 if (rowStore == null) {
417 log.debug("The bridge uuid is null");
418 return null;
419 }
420
421 ConcurrentMap<String, Row> bridgeTableRows = rowStore.getRowStore();
422 if (bridgeTableRows == null) {
423 log.debug("The bridge uuid is null");
424 return null;
425 }
426
427 for (String uuid : bridgeTableRows.keySet()) {
428 Bridge bridge = (Bridge) TableGenerator
Hyunsun Moon1251e192016-06-07 16:57:05 -0700429 .getTable(dbSchema, bridgeTableRows.get(uuid), OvsdbTable.BRIDGE);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700430 if (bridge.getName().equals(bridgeName)) {
431 return uuid;
432 }
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700433 }
434 return null;
435 }
436
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700437 private String getOvsUuid(String dbName) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700438 OvsdbRowStore rowStore = getRowStore(DATABASENAME, DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700439 if (rowStore == null) {
440 log.debug("The bridge uuid is null");
441 return null;
442 }
443 ConcurrentMap<String, Row> ovsTableRows = rowStore.getRowStore();
444 if (ovsTableRows != null) {
445 for (String uuid : ovsTableRows.keySet()) {
446 Row row = ovsTableRows.get(uuid);
447 String tableName = row.tableName();
448 if (tableName.equals(dbName)) {
449 return uuid;
450 }
451 }
452 }
453 return null;
454 }
455
456 @Override
457 public void createPort(String bridgeName, String portName) {
458 String bridgeUuid = getBridgeUuid(bridgeName);
459 if (bridgeUuid == null) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700460 log.error("Can't find bridge {} in {}", bridgeName, nodeId.getIpAddress());
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700461 return;
462 }
463
Hyunsun Moon1251e192016-06-07 16:57:05 -0700464 DatabaseSchema dbSchema = schema.get(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700465 String portUuid = getPortUuid(portName, bridgeUuid);
Hyunsun Moon1251e192016-06-07 16:57:05 -0700466 Port port = (Port) TableGenerator.createTable(dbSchema, OvsdbTable.PORT);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700467 port.setName(portName);
468 if (portUuid == null) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700469 insertConfig(PORT, UUID, BRIDGE, PORTS, bridgeUuid, port.getRow());
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700470 }
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700471 }
472
473 @Override
474 public void dropPort(String bridgeName, String portName) {
475 String bridgeUuid = getBridgeUuid(bridgeName);
476 if (bridgeUuid == null) {
477 log.error("Could not find Bridge {} in {}", bridgeName, nodeId);
478 return;
479 }
480
481 String portUuid = getPortUuid(portName, bridgeUuid);
482 if (portUuid != null) {
483 log.info("Port {} delete", portName);
Frank Wange11a98d2016-10-26 17:04:03 +0800484 deleteConfig(PORT, UUID, portUuid, BRIDGE, PORTS, Uuid.uuid(portUuid));
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700485 }
486 }
487
lishuai6c56f5e2015-11-17 16:38:19 +0800488 @Override
Hyunsun Moon1251e192016-06-07 16:57:05 -0700489 public boolean createBridge(OvsdbBridge ovsdbBridge) {
490 DatabaseSchema dbSchema = schema.get(DATABASENAME);
491 String ovsUuid = getOvsUuid(DATABASENAME);
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700492
493 if (dbSchema == null || ovsUuid == null) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700494 log.error("Can't find database Open_vSwitch");
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700495 return false;
496 }
497
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700498 Bridge bridge = (Bridge) TableGenerator.createTable(dbSchema, OvsdbTable.BRIDGE);
Hyunsun Moon1251e192016-06-07 16:57:05 -0700499 bridge.setOtherConfig(ovsdbBridge.otherConfigs());
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700500
Hyunsun Moon1251e192016-06-07 16:57:05 -0700501 if (ovsdbBridge.failMode().isPresent()) {
502 String failMode = ovsdbBridge.failMode().get().name().toLowerCase();
503 bridge.setFailMode(Sets.newHashSet(failMode));
Bob zhoue9795fd2016-05-12 20:18:45 +0800504 }
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700505
jaegonkim80bee532017-05-15 15:16:38 +0900506 if (ovsdbBridge.datapathType().isPresent()) {
507 String datapathType = ovsdbBridge.datapathType().get();
508 bridge.setDatapathType(datapathType);
509 }
510
Hyunsun Moon1251e192016-06-07 16:57:05 -0700511 String bridgeUuid = getBridgeUuid(ovsdbBridge.name());
Hyunsun Moon98025542016-03-08 04:36:02 -0800512 if (bridgeUuid == null) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700513 bridge.setName(ovsdbBridge.name());
514 bridgeUuid = insertConfig(
515 BRIDGE, UUID, DATABASENAME, BRIDGES,
516 ovsUuid, bridge.getRow());
Hyunsun Moon98025542016-03-08 04:36:02 -0800517 } else {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700518 // update the bridge if it's already existing
519 updateConfig(BRIDGE, UUID, bridgeUuid, bridge.getRow());
Hyunsun Moon98025542016-03-08 04:36:02 -0800520 }
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700521
Hyunsun Moon1251e192016-06-07 16:57:05 -0700522 if (bridgeUuid == null) {
523 log.warn("Failed to create bridge {} on {}", ovsdbBridge.name(), nodeId);
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700524 return false;
525 }
526
Hyunsun Moon1251e192016-06-07 16:57:05 -0700527 createPort(ovsdbBridge.name(), ovsdbBridge.name());
528 setControllersWithUuid(Uuid.uuid(bridgeUuid), ovsdbBridge.controllers());
529
530 log.info("Created bridge {}", ovsdbBridge.name());
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700531 return true;
532 }
533
Hyunsun Moon1251e192016-06-07 16:57:05 -0700534 @Override
535 public ControllerInfo localController() {
536 IpAddress ipAddress = IpAddress.valueOf(((InetSocketAddress)
537 channel.localAddress()).getAddress());
538 return new ControllerInfo(ipAddress, OFPORT, "tcp");
andreaed976a42015-10-05 14:38:25 -0700539 }
540
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700541 private void setControllersWithUuid(Uuid bridgeUuid, List<ControllerInfo> controllers) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700542 DatabaseSchema dbSchema = schema.get(DATABASENAME);
andreaed976a42015-10-05 14:38:25 -0700543 if (dbSchema == null) {
544 log.debug("There is no schema");
545 return;
546 }
547 List<Controller> oldControllers = getControllers(bridgeUuid);
548 if (oldControllers == null) {
549 log.warn("There are no controllers");
550 return;
551 }
552
Jonathan Hart51539b82015-10-29 09:53:04 -0700553 Set<Uuid> newControllerUuids = new HashSet<>();
andreaed976a42015-10-05 14:38:25 -0700554
555 Set<ControllerInfo> newControllers = new HashSet<>(controllers);
556 List<Controller> removeControllers = new ArrayList<>();
557 oldControllers.forEach(controller -> {
558 ControllerInfo controllerInfo = new ControllerInfo((String) controller.getTargetColumn().data());
559 if (newControllers.contains(controllerInfo)) {
560 newControllers.remove(controllerInfo);
561 newControllerUuids.add(controller.getRow().uuid());
562 } else {
563 removeControllers.add(controller);
564 }
565 });
Hyunsun Moon1251e192016-06-07 16:57:05 -0700566 OvsdbRowStore controllerRowStore = getRowStore(DATABASENAME, CONTROLLER);
andreaed976a42015-10-05 14:38:25 -0700567 if (controllerRowStore == null) {
568 log.debug("There is no controller table");
569 return;
570 }
571
Hyunsun Moon1251e192016-06-07 16:57:05 -0700572 removeControllers.forEach(c -> deleteConfig(CONTROLLER, UUID, c.getRow().uuid().value(),
Saritha8a0c36a2017-07-04 15:01:35 +0530573 BRIDGE, BRIDGE_CONTROLLER, c.getRow().uuid()));
andreaed976a42015-10-05 14:38:25 -0700574 newControllers.stream().map(c -> {
575 Controller controller = (Controller) TableGenerator
576 .createTable(dbSchema, OvsdbTable.CONTROLLER);
577 controller.setTarget(c.target());
578 return controller;
579 }).forEach(c -> {
Saritha8a0c36a2017-07-04 15:01:35 +0530580 String uuid = insertConfig(CONTROLLER, UUID, BRIDGE, BRIDGE_CONTROLLER, bridgeUuid.value(),
andreaed976a42015-10-05 14:38:25 -0700581 c.getRow());
Jonathan Hart51539b82015-10-29 09:53:04 -0700582 newControllerUuids.add(Uuid.uuid(uuid));
andreaed976a42015-10-05 14:38:25 -0700583
584 });
585
Hyunsun Moon1251e192016-06-07 16:57:05 -0700586 OvsdbRowStore rowStore = getRowStore(DATABASENAME, BRIDGE);
andreaed976a42015-10-05 14:38:25 -0700587 if (rowStore == null) {
588 log.debug("There is no bridge table");
589 return;
590 }
591
592 Row bridgeRow = rowStore.getRow(bridgeUuid.value());
593 Bridge bridge = (Bridge) TableGenerator.getTable(dbSchema, bridgeRow, OvsdbTable.BRIDGE);
594 bridge.setController(OvsdbSet.ovsdbSet(newControllerUuids));
Hyunsun Moon1251e192016-06-07 16:57:05 -0700595 updateConfig(BRIDGE, UUID, bridgeUuid.value(), bridge.getRow());
andreaed976a42015-10-05 14:38:25 -0700596 }
597
andreaed976a42015-10-05 14:38:25 -0700598 @Override
599 public void setControllersWithDeviceId(DeviceId deviceId, List<ControllerInfo> controllers) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700600 setControllersWithUuid(getBridgeUuid(deviceId), controllers);
andreaed976a42015-10-05 14:38:25 -0700601 }
602
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700603 @Override
604 public void dropBridge(String bridgeName) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700605 String bridgeUuid = getBridgeUuid(bridgeName);
606 if (bridgeUuid == null) {
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700607 log.warn("Could not find bridge in node", nodeId.getIpAddress());
608 return;
609 }
Frank Wange11a98d2016-10-26 17:04:03 +0800610 deleteConfig(BRIDGE, UUID, bridgeUuid, DATABASENAME, BRIDGES, Uuid.uuid(bridgeUuid));
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700611 }
612
Frank Wange11a98d2016-10-26 17:04:03 +0800613 @Override
614 public void applyQos(PortNumber portNumber, String qosName) {
615 DatabaseSchema dbSchema = schema.get(DATABASENAME);
616 OvsdbRowStore portRowStore = getRowStore(DATABASENAME, PORT);
617 if (portRowStore == null) {
618 log.debug("The port uuid is null");
619 return;
620 }
621 OvsdbRowStore qosRowStore = getRowStore(DATABASENAME, QOS);
622 if (qosRowStore == null) {
623 log.debug("The qos uuid is null");
624 return;
625 }
626
627 // Due to Qos Table doesn't have a unique identifier except uuid, unlike
628 // Bridge or Port Table has a name column,in order to make the api more
629 // general, put qos name in external_ids column of Qos Table if this qos
630 // created by onos.
631 ConcurrentMap<String, Row> qosTableRows = qosRowStore.getRowStore();
632 ConcurrentMap<String, Row> portTableRows = portRowStore.getRowStore();
633 Row qosRow = qosTableRows.values().stream().filter(r -> {
634 OvsdbMap ovsdbMap = (OvsdbMap) (r.getColumn(EXTERNAL_ID).data());
635 return qosName.equals(ovsdbMap.map().get(QOS_EXTERNAL_ID_KEY));
636 }).findFirst().orElse(null);
637
638 Row portRow = portTableRows.values().stream()
639 .filter(r -> r.getColumn("name").data().equals(portNumber.name()))
640 .findFirst().orElse(null);
641 if (portRow != null && qosRow != null) {
642 String qosId = qosRow.uuid().value();
643 Uuid portUuid = portRow.uuid();
644 Map<String, Column> columns = new HashMap<>();
645 Row newPortRow = new Row(PORT, portUuid, columns);
646 Port newport = new Port(dbSchema, newPortRow);
647 columns.put(Port.PortColumn.QOS.columnName(), newport.getQosColumn());
648 newport.setQos(Uuid.uuid(qosId));
649 updateConfig(PORT, UUID, portUuid.value(), newport.getRow());
650 }
651 }
652
653 @Override
654 public void removeQos(PortNumber portNumber) {
655 DatabaseSchema dbSchema = schema.get(DATABASENAME);
656 OvsdbRowStore rowStore = getRowStore(DATABASENAME, PORT);
657 if (rowStore == null) {
658 log.debug("The qos uuid is null");
659 return;
660 }
661
662 ConcurrentMap<String, Row> ovsTableRows = rowStore.getRowStore();
663 Row portRow = ovsTableRows.values().stream()
664 .filter(r -> r.getColumn("name").data().equals(portNumber.name()))
665 .findFirst().orElse(null);
666 if (portRow == null) {
667 log.warn("Couldn't find port {} in ovsdb port table.", portNumber.name());
668 return;
669 }
670
671 OvsdbSet ovsdbSet = ((OvsdbSet) portRow.getColumn(PORT_QOS).data());
672 @SuppressWarnings("unchecked")
673 Set<Uuid> qosIdSet = ovsdbSet.set();
674 if (qosIdSet == null || qosIdSet.isEmpty()) {
675 return;
676 }
677 Uuid qosUuid = (Uuid) qosIdSet.toArray()[0];
678 Condition condition = ConditionUtil.isEqual(UUID, portRow.uuid());
679 List<Condition> conditions = Lists.newArrayList(condition);
680 Mutation mutation = MutationUtil.delete(PORT_QOS, qosUuid);
681 List<Mutation> mutations = Lists.newArrayList(mutation);
682
683 ArrayList<Operation> operations = Lists.newArrayList();
684 Mutate mutate = new Mutate(dbSchema.getTableSchema(PORT), conditions, mutations);
685 operations.add(mutate);
686 transactConfig(DATABASENAME, operations);
687 }
688
689 @Override
690 public boolean createQos(OvsdbQos ovsdbQos) {
691 DatabaseSchema dbSchema = schema.get(DATABASENAME);
692 Qos qos = (Qos) TableGenerator.createTable(dbSchema, OvsdbTable.QOS);
693 OvsdbRowStore rowStore = getRowStore(DATABASENAME, QOS);
694 if (rowStore == null) {
695 log.debug("The qos uuid is null");
696 return false;
697 }
698
699 ArrayList<Operation> operations = Lists.newArrayList();
700 Set<String> types = Sets.newHashSet();
701 Map<Long, Uuid> queues = Maps.newHashMap();
702
703 types.add(ovsdbQos.qosType());
704 qos.setOtherConfig(ovsdbQos.otherConfigs());
705 qos.setExternalIds(ovsdbQos.externalIds());
706 qos.setType(types);
707 if (ovsdbQos.qosQueues().isPresent()) {
708 for (Map.Entry<Long, String> entry : ovsdbQos.qosQueues().get().entrySet()) {
709 OvsdbRowStore queueRowStore = getRowStore(DATABASENAME, QUEUE);
710 if (queueRowStore != null) {
711 ConcurrentMap<String, Row> queueTableRows = queueRowStore.getRowStore();
712 Row queueRow = queueTableRows.values().stream().filter(r -> {
713 OvsdbMap ovsdbMap = (OvsdbMap) (r.getColumn(EXTERNAL_ID).data());
714 return entry.getValue().equals(ovsdbMap.map().get(QUEUE_EXTERNAL_ID_KEY));
715 }).findFirst().orElse(null);
716 if (queueRow != null) {
717 queues.put(entry.getKey(), queueRow.uuid());
718 }
719 }
720 }
721 qos.setQueues(queues);
722 }
723
724 Insert qosInsert = new Insert(dbSchema.getTableSchema(QOS), QOS, qos.getRow());
725 operations.add(qosInsert);
726 try {
727 transactConfig(DATABASENAME, operations).get();
728 } catch (InterruptedException | ExecutionException e) {
729 return false;
730 }
731 return true;
732 }
733
734 @Override
735 public void dropQos(QosId qosId) {
736 OvsdbRowStore rowStore = getRowStore(DATABASENAME, QOS);
737 if (rowStore != null) {
738 ConcurrentMap<String, Row> qosTableRows = rowStore.getRowStore();
739 Row qosRow = qosTableRows.values().stream().filter(r -> {
740 OvsdbMap ovsdbMap = (OvsdbMap) (r.getColumn(EXTERNAL_ID).data());
741 return qosId.name().equals(ovsdbMap.map().get(QOS_EXTERNAL_ID_KEY));
742 }).findFirst().orElse(null);
743 if (qosRow != null) {
744 deleteConfig(QOS, UUID, qosRow.uuid().value(), PORT, PORT_QOS, qosRow.uuid());
745 }
746 }
747 }
748 @Override
749 public OvsdbQos getQos(QosId qosId) {
750 Set<OvsdbQos> ovsdbQoses = getQoses();
751 return ovsdbQoses.stream().filter(r ->
752 qosId.name().equals(r.externalIds().get(QOS_EXTERNAL_ID_KEY))).
753 findFirst().orElse(null);
754 }
755
756 @Override
757 public Set<OvsdbQos> getQoses() {
758 Set<OvsdbQos> ovsdbQoses = new HashSet<>();
759 OvsdbRowStore rowStore = getRowStore(DATABASENAME, QOS);
760 if (rowStore == null) {
761 log.debug("The qos uuid is null");
762 return ovsdbQoses;
763 }
764 ConcurrentMap<String, Row> rows = rowStore.getRowStore();
765 for (String uuid : rows.keySet()) {
766 Row row = getRow(DATABASENAME, QOS, uuid);
767 OvsdbQos ovsdbQos = getOvsdbQos(row);
768 if (ovsdbQos != null) {
769 ovsdbQoses.add(ovsdbQos);
770 }
771 }
772 return ovsdbQoses;
773 }
774
775 @Override
776 public boolean createQueue(OvsdbQueue ovsdbQueue) {
777 DatabaseSchema dbSchema = schema.get(DATABASENAME);
778 Queue queue = (Queue) TableGenerator.createTable(dbSchema, OvsdbTable.QUEUE);
779 ArrayList<Operation> operations = Lists.newArrayList();
780 OvsdbRowStore rowStore = getRowStore(DATABASENAME, QUEUE);
781 if (rowStore == null) {
782 log.debug("The queue uuid is null");
783 return false;
784 }
785
786 if (ovsdbQueue.dscp().isPresent()) {
787 queue.setDscp(ImmutableSet.of(ovsdbQueue.dscp().get()));
788 }
789 queue.setOtherConfig(ovsdbQueue.otherConfigs());
790 queue.setExternalIds(ovsdbQueue.externalIds());
791 Insert queueInsert = new Insert(dbSchema.getTableSchema(QUEUE), QUEUE, queue.getRow());
792 operations.add(queueInsert);
793
794 try {
795 transactConfig(DATABASENAME, operations).get();
796 } catch (InterruptedException | ExecutionException e) {
797 log.error("createQueue transactConfig get exception !");
798 }
799 return true;
800 }
801
802 @Override
803 public void dropQueue(QueueId queueId) {
804 OvsdbRowStore queueRowStore = getRowStore(DATABASENAME, QUEUE);
805 if (queueRowStore == null) {
806 return;
807 }
808
809 ConcurrentMap<String, Row> queueTableRows = queueRowStore.getRowStore();
810 Row queueRow = queueTableRows.values().stream().filter(r -> {
811 OvsdbMap ovsdbMap = (OvsdbMap) (r.getColumn(EXTERNAL_ID).data());
812 return queueId.name().equals(ovsdbMap.map().get(QUEUE_EXTERNAL_ID_KEY));
813 }).findFirst().orElse(null);
814 if (queueRow == null) {
815 return;
816 }
817
818 String queueUuid = queueRow.uuid().value();
819 OvsdbRowStore qosRowStore = getRowStore(DATABASENAME, QOS);
820 if (qosRowStore != null) {
821 Map<Long, Uuid> queueMap = new HashMap<>();
822 ConcurrentMap<String, Row> qosTableRows = qosRowStore.getRowStore();
823 qosTableRows.values().stream().filter(r -> {
824 Map<Integer, Uuid> ovsdbMap = ((OvsdbMap) r.getColumn(QUEUES).data()).map();
825 Set<Integer> keySet = ovsdbMap.keySet();
826 for (Integer keyId : keySet) {
827 if (ovsdbMap.get(keyId).equals(Uuid.uuid(queueUuid))) {
828 queueMap.put(keyId.longValue(), Uuid.uuid(queueUuid));
829 return true;
830 }
831 }
832 return false;
833 }).findFirst().orElse(null);
834 deleteConfig(QUEUE, UUID, queueUuid, QOS, QUEUES, OvsdbMap.ovsdbMap(queueMap));
835 } else {
836 deleteConfig(QUEUE, UUID, queueUuid, null, null, null);
837 }
838 }
839 @Override
840 public OvsdbQueue getQueue(QueueId queueId) {
841 Set<OvsdbQueue> ovsdbQueues = getQueues();
842 return ovsdbQueues.stream().filter(r ->
843 queueId.name().equals(r.externalIds().get(QUEUE_EXTERNAL_ID_KEY))).
844 findFirst().orElse(null);
845 }
846
847 @Override
848 public Set<OvsdbQueue> getQueues() {
849 Set<OvsdbQueue> ovsdbqueues = new HashSet<>();
850 OvsdbRowStore rowStore = getRowStore(DATABASENAME, QUEUE);
851 if (rowStore == null) {
852 log.debug("The queue uuid is null");
853 return ovsdbqueues;
854 }
855 ConcurrentMap<String, Row> rows = rowStore.getRowStore();
856 for (String uuid : rows.keySet()) {
857 Row row = getRow(DATABASENAME, QUEUE, uuid);
858 OvsdbQueue ovsdbQueue = getOvsdbQueue(row);
859 if (ovsdbQueue != null) {
860 ovsdbqueues.add(ovsdbQueue);
861 }
862 }
863 return ovsdbqueues;
864 }
Pier Ventref5d72362016-07-17 12:02:14 +0200865 /**
866 * Creates a mirror port. Mirrors the traffic
867 * that goes to selectDstPort or comes from
868 * selectSrcPort or packets containing selectVlan
869 * to mirrorPort or to all ports that trunk mirrorVlan.
870 *
871 * @param mirror the OVSDB mirror description
872 * @return true if mirror creation is successful, false otherwise
873 */
874 @Override
875 public boolean createMirror(String bridgeName, OvsdbMirror mirror) {
876
877 /**
878 * Retrieves bridge's uuid. It is necessary to update
879 * Bridge table.
880 */
881 String bridgeUuid = getBridgeUuid(bridgeName);
882 if (bridgeUuid == null) {
883 log.warn("Couldn't find bridge {} in {}", bridgeName, nodeId.getIpAddress());
884 return false;
885 }
886
887 OvsdbMirror.Builder mirrorBuilder = OvsdbMirror.builder();
888
889 mirrorBuilder.mirroringName(mirror.mirroringName());
890 mirrorBuilder.selectAll(mirror.selectAll());
891
892 /**
893 * Retrieves the uuid of the monitored dst ports.
894 */
895 mirrorBuilder.monitorDstPorts(mirror.monitorDstPorts().parallelStream()
896 .map(dstPort -> {
897 String dstPortUuid = getPortUuid(dstPort.value(), bridgeUuid);
898 if (dstPortUuid != null) {
899 return Uuid.uuid(dstPortUuid);
900 }
901 log.warn("Couldn't find port {} in {}",
902 dstPort.value(), nodeId.getIpAddress());
903 return null;
904 })
905 .filter(Objects::nonNull)
906 .collect(Collectors.toSet())
907 );
908
909 /**
910 * Retrieves the uuid of the monitored src ports.
911 */
912 mirrorBuilder.monitorSrcPorts(mirror.monitorSrcPorts().parallelStream()
913 .map(srcPort -> {
914 String srcPortUuid = getPortUuid(srcPort.value(), bridgeUuid);
915 if (srcPortUuid != null) {
916 return Uuid.uuid(srcPortUuid);
917 }
918 log.warn("Couldn't find port {} in {}",
919 srcPort.value(), nodeId.getIpAddress());
920 return null;
921 }).filter(Objects::nonNull)
922 .collect(Collectors.toSet())
923 );
924
925 mirrorBuilder.monitorVlans(mirror.monitorVlans());
926 mirrorBuilder.mirrorPort(mirror.mirrorPort());
927 mirrorBuilder.mirrorVlan(mirror.mirrorVlan());
928 mirrorBuilder.externalIds(mirror.externalIds());
929 mirror = mirrorBuilder.build();
930
Jon Hallcbd1b392017-01-18 20:15:44 -0800931 if (mirror.monitorDstPorts().isEmpty() &&
932 mirror.monitorSrcPorts().isEmpty() &&
933 mirror.monitorVlans().isEmpty()) {
Pier Ventref5d72362016-07-17 12:02:14 +0200934 log.warn("Invalid monitoring data");
935 return false;
936 }
937
938 DatabaseSchema dbSchema = schema.get(DATABASENAME);
939
940 Mirror mirrorEntry = (Mirror) TableGenerator.createTable(dbSchema, OvsdbTable.MIRROR);
941 mirrorEntry.setName(mirror.mirroringName());
942 mirrorEntry.setSelectDstPort(mirror.monitorDstPorts());
943 mirrorEntry.setSelectSrcPort(mirror.monitorSrcPorts());
944 mirrorEntry.setSelectVlan(mirror.monitorVlans());
945 mirrorEntry.setExternalIds(mirror.externalIds());
946
947 /**
948 * If mirror port, retrieves the uuid of the mirror port.
949 */
950 if (mirror.mirrorPort() != null) {
951
952 String outputPortUuid = getPortUuid(mirror.mirrorPort().value(), bridgeUuid);
953 if (outputPortUuid == null) {
954 log.warn("Couldn't find port {} in {}", mirror.mirrorPort().value(), nodeId.getIpAddress());
955 return false;
956 }
957
958 mirrorEntry.setOutputPort(Uuid.uuid(outputPortUuid));
959
960 } else if (mirror.mirrorVlan() != null) {
961
962 mirrorEntry.setOutputVlan(mirror.mirrorVlan());
963
964 } else {
965 log.warn("Invalid mirror, no mirror port and no mirror vlan");
966 return false;
967 }
968
969 ArrayList<Operation> operations = Lists.newArrayList();
970 Insert mirrorInsert = new Insert(dbSchema.getTableSchema("Mirror"), "Mirror", mirrorEntry.getRow());
971 operations.add(mirrorInsert);
972
973 // update the bridge table
974 Condition condition = ConditionUtil.isEqual(UUID, Uuid.uuid(bridgeUuid));
975 Mutation mutation = MutationUtil.insert(MIRRORS, Uuid.uuid("Mirror"));
976 List<Condition> conditions = Lists.newArrayList(condition);
977 List<Mutation> mutations = Lists.newArrayList(mutation);
978 operations.add(new Mutate(dbSchema.getTableSchema("Bridge"), conditions, mutations));
979
980 transactConfig(DATABASENAME, operations);
981 log.info("Created mirror {}", mirror.mirroringName());
982 return true;
983 }
984
985 /**
986 * Drops the configuration for mirror.
987 *
Ray Milkeyef794342016-11-09 16:20:29 -0800988 * @param mirroringName name of mirror to drop
Pier Ventref5d72362016-07-17 12:02:14 +0200989 */
990 @Override
991 public void dropMirror(MirroringName mirroringName) {
992 String mirrorUuid = getMirrorUuid(mirroringName.name());
993 if (mirrorUuid != null) {
994 log.info("Deleted mirror {}", mirroringName.name());
Frank Wange11a98d2016-10-26 17:04:03 +0800995 deleteConfig(MIRROR, UUID, mirrorUuid, BRIDGE, MIRRORS, Uuid.uuid(mirrorUuid));
Pier Ventref5d72362016-07-17 12:02:14 +0200996 }
997 log.warn("Unable to delete {}", mirroringName.name());
998 return;
999 }
1000
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001001 @Override
Hyunsun Moondd14e8e2016-06-09 16:17:32 -07001002 public boolean createInterface(String bridgeName, OvsdbInterface ovsdbIface) {
Hyunsun Moon646d8c42015-10-08 20:32:44 -07001003 String bridgeUuid = getBridgeUuid(bridgeName);
1004 if (bridgeUuid == null) {
1005 log.warn("Couldn't find bridge {} in {}", bridgeName, nodeId.getIpAddress());
1006 return false;
1007 }
1008
Hyunsun Moondd14e8e2016-06-09 16:17:32 -07001009 if (getPortUuid(ovsdbIface.name(), bridgeUuid) != null) {
1010 log.warn("Interface {} already exists", ovsdbIface.name());
Hyunsun Moon646d8c42015-10-08 20:32:44 -07001011 // remove existing one and re-create?
1012 return false;
1013 }
1014
1015 ArrayList<Operation> operations = Lists.newArrayList();
Hyunsun Moon1251e192016-06-07 16:57:05 -07001016 DatabaseSchema dbSchema = schema.get(DATABASENAME);
Hyunsun Moon646d8c42015-10-08 20:32:44 -07001017
Hyunsun Moon89478662016-06-09 17:52:34 -07001018 // insert a new port with the interface name
Hyunsun Moon646d8c42015-10-08 20:32:44 -07001019 Port port = (Port) TableGenerator.createTable(dbSchema, OvsdbTable.PORT);
Hyunsun Moondd14e8e2016-06-09 16:17:32 -07001020 port.setName(ovsdbIface.name());
1021 Insert portInsert = new Insert(dbSchema.getTableSchema(PORT), PORT, port.getRow());
1022 portInsert.getRow().put(INTERFACES, Uuid.uuid(INTERFACE));
Hyunsun Moon646d8c42015-10-08 20:32:44 -07001023 operations.add(portInsert);
1024
Hyunsun Moon89478662016-06-09 17:52:34 -07001025 // update the bridge table with the new port
Hyunsun Moon1251e192016-06-07 16:57:05 -07001026 Condition condition = ConditionUtil.isEqual(UUID, Uuid.uuid(bridgeUuid));
Hyunsun Moondd14e8e2016-06-09 16:17:32 -07001027 Mutation mutation = MutationUtil.insert(PORTS, Uuid.uuid(PORT));
1028 List<Condition> conditions = Lists.newArrayList(condition);
1029 List<Mutation> mutations = Lists.newArrayList(mutation);
1030 operations.add(new Mutate(dbSchema.getTableSchema(BRIDGE), conditions, mutations));
Hyunsun Moon646d8c42015-10-08 20:32:44 -07001031
Hyunsun Moon89478662016-06-09 17:52:34 -07001032 // insert an interface
Hyunsun Moon646d8c42015-10-08 20:32:44 -07001033 Interface intf = (Interface) TableGenerator.createTable(dbSchema, OvsdbTable.INTERFACE);
Hyunsun Moondd14e8e2016-06-09 16:17:32 -07001034 intf.setName(ovsdbIface.name());
jaegonkim256f2c12017-05-26 00:03:42 +09001035
Hyunsun Moondd14e8e2016-06-09 16:17:32 -07001036 intf.setType(ovsdbIface.typeToString());
jaegonkim256f2c12017-05-26 00:03:42 +09001037
1038 if (ovsdbIface.mtu().isPresent()) {
1039 Set<Long> mtuSet = Sets.newConcurrentHashSet();
1040 mtuSet.add(ovsdbIface.mtu().get());
1041 intf.setMtu(mtuSet);
1042 intf.setMtuRequest(mtuSet);
1043 }
1044
Hyunsun Moondd14e8e2016-06-09 16:17:32 -07001045 intf.setOptions(ovsdbIface.options());
1046 Insert intfInsert = new Insert(dbSchema.getTableSchema(INTERFACE), INTERFACE, intf.getRow());
Hyunsun Moon646d8c42015-10-08 20:32:44 -07001047 operations.add(intfInsert);
1048
Hyunsun Moon1251e192016-06-07 16:57:05 -07001049 transactConfig(DATABASENAME, operations);
Hyunsun Moon89478662016-06-09 17:52:34 -07001050 log.info("Created interface {}", ovsdbIface);
Hyunsun Moon646d8c42015-10-08 20:32:44 -07001051 return true;
1052 }
1053
1054 @Override
Hyunsun Moondd14e8e2016-06-09 16:17:32 -07001055 public boolean dropInterface(String ifaceName) {
1056 OvsdbRowStore rowStore = getRowStore(DATABASENAME, BRIDGE);
1057 if (rowStore == null) {
1058 log.warn("Failed to get BRIDGE table");
1059 return false;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001060 }
1061
Hyunsun Moondd14e8e2016-06-09 16:17:32 -07001062 ConcurrentMap<String, Row> bridgeTableRows = rowStore.getRowStore();
1063 if (bridgeTableRows == null) {
1064 log.warn("Failed to get BRIDGE table rows");
1065 return false;
1066 }
1067
1068 // interface name is unique
1069 Optional<String> bridgeId = bridgeTableRows.keySet().stream()
1070 .filter(uuid -> getPortUuid(ifaceName, uuid) != null)
1071 .findFirst();
1072
1073 if (bridgeId.isPresent()) {
1074 String portId = getPortUuid(ifaceName, bridgeId.get());
Frank Wange11a98d2016-10-26 17:04:03 +08001075 deleteConfig(PORT, UUID, portId, BRIDGE, PORTS, Uuid.uuid(portId));
Hyunsun Moondd14e8e2016-06-09 16:17:32 -07001076 return true;
1077 } else {
1078 log.warn("Unable to find the interface with name {}", ifaceName);
1079 return false;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001080 }
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001081 }
1082
1083 /**
1084 * Delete transact config.
1085 *
andreaed976a42015-10-05 14:38:25 -07001086 * @param childTableName child table name
1087 * @param childColumnName child column name
1088 * @param childUuid child row uuid
1089 * @param parentTableName parent table name
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001090 * @param parentColumnName parent column
Frank Wange11a98d2016-10-26 17:04:03 +08001091 * @param referencedValue referenced value
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001092 */
1093 private void deleteConfig(String childTableName, String childColumnName,
andreaed976a42015-10-05 14:38:25 -07001094 String childUuid, String parentTableName,
Frank Wange11a98d2016-10-26 17:04:03 +08001095 String parentColumnName, Object referencedValue) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001096 DatabaseSchema dbSchema = schema.get(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001097 TableSchema childTableSchema = dbSchema.getTableSchema(childTableName);
1098
1099 ArrayList<Operation> operations = Lists.newArrayList();
Frank Wange11a98d2016-10-26 17:04:03 +08001100 if (parentTableName != null && parentColumnName != null && referencedValue != null) {
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001101 TableSchema parentTableSchema = dbSchema
1102 .getTableSchema(parentTableName);
1103 ColumnSchema parentColumnSchema = parentTableSchema
1104 .getColumnSchema(parentColumnName);
1105 List<Mutation> mutations = Lists.newArrayList();
Frank Wange11a98d2016-10-26 17:04:03 +08001106 Mutation mutation = MutationUtil.delete(parentColumnSchema.name(), referencedValue);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001107 mutations.add(mutation);
1108 List<Condition> conditions = Lists.newArrayList();
Frank Wange11a98d2016-10-26 17:04:03 +08001109 Condition condition = ConditionUtil.includes(parentColumnName, referencedValue);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001110 conditions.add(condition);
1111 Mutate op = new Mutate(parentTableSchema, conditions, mutations);
1112 operations.add(op);
1113 }
1114
1115 List<Condition> conditions = Lists.newArrayList();
Jonathan Hart51539b82015-10-29 09:53:04 -07001116 Condition condition = ConditionUtil.isEqual(childColumnName, Uuid.uuid(childUuid));
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001117 conditions.add(condition);
1118 Delete del = new Delete(childTableSchema, conditions);
1119 operations.add(del);
Hyunsun Moon1251e192016-06-07 16:57:05 -07001120 transactConfig(DATABASENAME, operations);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001121 }
1122
1123 /**
1124 * Update transact config.
1125 *
andreaed976a42015-10-05 14:38:25 -07001126 * @param tableName table name
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001127 * @param columnName column name
andreaed976a42015-10-05 14:38:25 -07001128 * @param uuid uuid
1129 * @param row the config data
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001130 */
1131 private void updateConfig(String tableName, String columnName, String uuid,
andreaed976a42015-10-05 14:38:25 -07001132 Row row) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001133 DatabaseSchema dbSchema = schema.get(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001134 TableSchema tableSchema = dbSchema.getTableSchema(tableName);
1135
1136 List<Condition> conditions = Lists.newArrayList();
Jonathan Hart51539b82015-10-29 09:53:04 -07001137 Condition condition = ConditionUtil.isEqual(columnName, Uuid.uuid(uuid));
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001138 conditions.add(condition);
1139
1140 Update update = new Update(tableSchema, row, conditions);
1141
1142 ArrayList<Operation> operations = Lists.newArrayList();
1143 operations.add(update);
1144
Hyunsun Moon1251e192016-06-07 16:57:05 -07001145 transactConfig(DATABASENAME, operations);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001146 }
1147
1148 /**
1149 * Insert transact config.
1150 *
andreaed976a42015-10-05 14:38:25 -07001151 * @param childTableName child table name
1152 * @param childColumnName child column name
1153 * @param parentTableName parent table name
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001154 * @param parentColumnName parent column
andreaed976a42015-10-05 14:38:25 -07001155 * @param parentUuid parent uuid
1156 * @param row the config data
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001157 * @return uuid, empty if no uuid is find
1158 */
1159 private String insertConfig(String childTableName, String childColumnName,
andreaed976a42015-10-05 14:38:25 -07001160 String parentTableName, String parentColumnName,
1161 String parentUuid, Row row) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001162 DatabaseSchema dbSchema = schema.get(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001163 TableSchema tableSchema = dbSchema.getTableSchema(childTableName);
1164
Sho SHIMIZUff18f8c2016-03-11 14:43:53 -08001165 Insert insert = new Insert(tableSchema, childTableName, row);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001166
1167 ArrayList<Operation> operations = Lists.newArrayList();
1168 operations.add(insert);
1169
1170 if (parentTableName != null && parentColumnName != null) {
1171 TableSchema parentTableSchema = dbSchema
1172 .getTableSchema(parentTableName);
1173 ColumnSchema parentColumnSchema = parentTableSchema
1174 .getColumnSchema(parentColumnName);
1175
1176 List<Mutation> mutations = Lists.newArrayList();
1177 Mutation mutation = MutationUtil.insert(parentColumnSchema.name(),
Sho SHIMIZUff18f8c2016-03-11 14:43:53 -08001178 Uuid.uuid(childTableName));
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001179 mutations.add(mutation);
1180
1181 List<Condition> conditions = Lists.newArrayList();
Hyunsun Moon1251e192016-06-07 16:57:05 -07001182 Condition condition = ConditionUtil.isEqual(UUID, Uuid.uuid(parentUuid));
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001183 conditions.add(condition);
1184
1185 Mutate op = new Mutate(parentTableSchema, conditions, mutations);
1186 operations.add(op);
1187 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001188 if (childTableName.equalsIgnoreCase(PORT)) {
1189 log.debug("Handle port insert");
Hyunsun Moondd14e8e2016-06-09 16:17:32 -07001190 Insert intfInsert = handlePortInsertTable(row);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001191
1192 if (intfInsert != null) {
1193 operations.add(intfInsert);
1194 }
1195
1196 Insert ins = (Insert) operations.get(0);
Hyunsun Moon1251e192016-06-07 16:57:05 -07001197 ins.getRow().put("interfaces", Uuid.uuid(INTERFACE));
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001198 }
1199
1200 List<OperationResult> results;
1201 try {
jaegonkim01d7c912017-01-22 22:03:38 +09001202 results = transactConfig(DATABASENAME, operations)
1203 .get(TRANSACTCONFIG_TIMEOUT, TimeUnit.SECONDS);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001204 return results.get(0).getUuid().value();
jaegonkim01d7c912017-01-22 22:03:38 +09001205 } catch (TimeoutException e) {
1206 log.warn("TimeoutException thrown while to get result");
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001207 } catch (InterruptedException e) {
1208 log.warn("Interrupted while waiting to get result");
1209 Thread.currentThread().interrupt();
1210 } catch (ExecutionException e) {
1211 log.error("Exception thrown while to get result");
1212 }
1213
1214 return null;
1215 }
1216
jaegonkim01d7c912017-01-22 22:03:38 +09001217
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001218 /**
1219 * Handles port insert.
1220 *
andreaed976a42015-10-05 14:38:25 -07001221 * @param portRow row of port
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001222 * @return insert, empty if null
1223 */
Hyunsun Moondd14e8e2016-06-09 16:17:32 -07001224 private Insert handlePortInsertTable(Row portRow) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001225 DatabaseSchema dbSchema = schema.get(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001226
Hyunsun Moon1251e192016-06-07 16:57:05 -07001227 TableSchema portTableSchema = dbSchema.getTableSchema(PORT);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001228 ColumnSchema portColumnSchema = portTableSchema.getColumnSchema("name");
1229
1230 String portName = (String) portRow.getColumn(portColumnSchema.name()).data();
Hyunsun Moon1251e192016-06-07 16:57:05 -07001231 Interface inf = (Interface) TableGenerator.createTable(dbSchema, OvsdbTable.INTERFACE);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001232 inf.setName(portName);
1233
Hyunsun Moon1251e192016-06-07 16:57:05 -07001234 TableSchema intfTableSchema = dbSchema.getTableSchema(INTERFACE);
1235 return new Insert(intfTableSchema, INTERFACE, inf.getRow());
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001236 }
1237
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001238 @Override
1239 public ListenableFuture<DatabaseSchema> getOvsdbSchema(String dbName) {
1240 if (dbName == null) {
1241 return null;
1242 }
1243 DatabaseSchema databaseSchema = schema.get(dbName);
1244 if (databaseSchema == null) {
1245 List<String> dbNames = new ArrayList<String>();
1246 dbNames.add(dbName);
Hyunsun Moon1251e192016-06-07 16:57:05 -07001247 Function<JsonNode, DatabaseSchema> rowFunction = input -> {
1248 log.debug("Get ovsdb database schema {}", dbName);
1249 DatabaseSchema dbSchema = FromJsonUtil.jsonNodeToDbSchema(dbName, input);
1250 if (dbSchema == null) {
1251 log.debug("Get ovsdb database schema error");
1252 return null;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001253 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001254 schema.put(dbName, dbSchema);
1255 return dbSchema;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001256 };
1257
1258 ListenableFuture<JsonNode> input = getSchema(dbNames);
1259 if (input != null) {
1260 return Futures.transform(input, rowFunction);
1261 }
1262 return null;
1263 } else {
1264 return Futures.immediateFuture(databaseSchema);
1265 }
1266 }
1267
1268 @Override
1269 public ListenableFuture<TableUpdates> monitorTables(String dbName, String id) {
1270 if (dbName == null) {
1271 return null;
1272 }
1273 DatabaseSchema dbSchema = schema.get(dbName);
1274 if (dbSchema != null) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001275 Function<JsonNode, TableUpdates> rowFunction = input -> {
1276 log.debug("Get table updates");
1277 TableUpdates updates = FromJsonUtil.jsonNodeToTableUpdates(input, dbSchema);
1278 if (updates == null) {
1279 log.debug("Get table updates error");
1280 return null;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001281 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001282 return updates;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001283 };
1284 return Futures.transform(monitor(dbSchema, id), rowFunction);
1285 }
1286 return null;
1287 }
1288
Hyunsun Moondd14e8e2016-06-09 16:17:32 -07001289 private ListenableFuture<List<OperationResult>> transactConfig(String dbName,
1290 List<Operation> operations) {
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001291 if (dbName == null) {
1292 return null;
1293 }
1294 DatabaseSchema dbSchema = schema.get(dbName);
1295 if (dbSchema != null) {
andreaed976a42015-10-05 14:38:25 -07001296 Function<List<JsonNode>, List<OperationResult>> rowFunction = (input -> {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001297 log.debug("Get ovsdb operation result");
1298 List<OperationResult> result = FromJsonUtil.jsonNodeToOperationResult(input, operations);
andreaed976a42015-10-05 14:38:25 -07001299 if (result == null) {
1300 log.debug("The operation result is null");
1301 return null;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001302 }
andreaed976a42015-10-05 14:38:25 -07001303 return result;
1304 });
Hyunsun Moon1251e192016-06-07 16:57:05 -07001305 return Futures.transform(transact(dbSchema, operations), rowFunction);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001306 }
1307 return null;
1308 }
1309
1310 @Override
1311 public ListenableFuture<JsonNode> getSchema(List<String> dbnames) {
1312 String id = java.util.UUID.randomUUID().toString();
1313 String getSchemaString = JsonRpcWriterUtil.getSchemaStr(id, dbnames);
1314
1315 SettableFuture<JsonNode> sf = SettableFuture.create();
1316 requestResult.put(id, sf);
1317 requestMethod.put(id, "getSchema");
1318
1319 channel.writeAndFlush(getSchemaString);
1320 return sf;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001321 }
1322
1323 @Override
1324 public ListenableFuture<List<String>> echo() {
1325 String id = java.util.UUID.randomUUID().toString();
1326 String echoString = JsonRpcWriterUtil.echoStr(id);
1327
1328 SettableFuture<List<String>> sf = SettableFuture.create();
1329 requestResult.put(id, sf);
1330 requestMethod.put(id, "echo");
1331
1332 channel.writeAndFlush(echoString);
1333 return sf;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001334 }
1335
1336 @Override
1337 public ListenableFuture<JsonNode> monitor(DatabaseSchema dbSchema,
1338 String monitorId) {
1339 String id = java.util.UUID.randomUUID().toString();
1340 String monitorString = JsonRpcWriterUtil.monitorStr(id, monitorId,
1341 dbSchema);
1342
1343 SettableFuture<JsonNode> sf = SettableFuture.create();
1344 requestResult.put(id, sf);
1345 requestMethod.put(id, "monitor");
1346
1347 channel.writeAndFlush(monitorString);
1348 return sf;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001349 }
1350
1351 @Override
1352 public ListenableFuture<List<String>> listDbs() {
1353 String id = java.util.UUID.randomUUID().toString();
1354 String listDbsString = JsonRpcWriterUtil.listDbsStr(id);
1355
1356 SettableFuture<List<String>> sf = SettableFuture.create();
1357 requestResult.put(id, sf);
1358 requestMethod.put(id, "listDbs");
1359
1360 channel.writeAndFlush(listDbsString);
1361 return sf;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001362 }
1363
1364 @Override
1365 public ListenableFuture<List<JsonNode>> transact(DatabaseSchema dbSchema,
1366 List<Operation> operations) {
1367 String id = java.util.UUID.randomUUID().toString();
1368 String transactString = JsonRpcWriterUtil.transactStr(id, dbSchema,
1369 operations);
1370
1371 SettableFuture<List<JsonNode>> sf = SettableFuture.create();
1372 requestResult.put(id, sf);
1373 requestMethod.put(id, "transact");
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001374 channel.writeAndFlush(transactString);
1375 return sf;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001376 }
1377
andreaed976a42015-10-05 14:38:25 -07001378 @SuppressWarnings({"rawtypes", "unchecked"})
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001379 @Override
1380 public void processResult(JsonNode response) {
1381 log.debug("Handle result");
1382 String requestId = response.get("id").asText();
1383 SettableFuture sf = requestResult.get(requestId);
1384 if (sf == null) {
1385 log.debug("No such future to process");
1386 return;
1387 }
1388 String methodName = requestMethod.get(requestId);
Hyunsun Moon1251e192016-06-07 16:57:05 -07001389 sf.set(FromJsonUtil.jsonResultParser(response, methodName));
tanbangcheng1afecce2017-11-12 11:41:28 +08001390
1391 requestResult.remove(requestId);
1392 requestMethod.remove(requestId);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001393 }
1394
1395 @Override
1396 public void processRequest(JsonNode requestJson) {
1397 log.debug("Handle request");
1398 if (requestJson.get("method").asText().equalsIgnoreCase("echo")) {
1399 log.debug("handle echo request");
1400
1401 String replyString = FromJsonUtil.getEchoRequestStr(requestJson);
1402 channel.writeAndFlush(replyString);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001403 } else {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001404 FromJsonUtil.jsonCallbackRequestParser(requestJson, monitorCallBack);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001405 }
1406 }
1407
1408 @Override
1409 public void setCallback(Callback monitorCallback) {
1410 this.monitorCallBack = monitorCallback;
1411 }
1412
1413 @Override
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001414 public Set<OvsdbBridge> getBridges() {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001415 Set<OvsdbBridge> ovsdbBridges = new HashSet<>();
1416 OvsdbTableStore tableStore = getTableStore(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001417 if (tableStore == null) {
MaoJianweidac220d2016-07-04 22:37:52 +08001418 return ovsdbBridges;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001419 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001420 OvsdbRowStore rowStore = tableStore.getRows(BRIDGE);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001421 if (rowStore == null) {
MaoJianweidac220d2016-07-04 22:37:52 +08001422 return ovsdbBridges;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001423 }
1424 ConcurrentMap<String, Row> rows = rowStore.getRowStore();
1425 for (String uuid : rows.keySet()) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001426 Row row = getRow(DATABASENAME, BRIDGE, uuid);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001427 OvsdbBridge ovsdbBridge = getOvsdbBridge(row);
1428 if (ovsdbBridge != null) {
1429 ovsdbBridges.add(ovsdbBridge);
1430 }
1431 }
1432 return ovsdbBridges;
1433 }
1434
1435 @Override
andreaed976a42015-10-05 14:38:25 -07001436 public Set<ControllerInfo> getControllers(DeviceId openflowDeviceId) {
Jonathan Hart51539b82015-10-29 09:53:04 -07001437 Uuid bridgeUuid = getBridgeUuid(openflowDeviceId);
andreaed976a42015-10-05 14:38:25 -07001438 if (bridgeUuid == null) {
1439 log.warn("bad bridge Uuid");
1440 return null;
1441 }
1442 List<Controller> controllers = getControllers(bridgeUuid);
1443 if (controllers == null) {
1444 log.warn("bad list of controllers");
1445 return null;
1446 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001447 return controllers.stream().map(controller -> new ControllerInfo(
1448 (String) controller.getTargetColumn()
1449 .data())).collect(Collectors.toSet());
andreaed976a42015-10-05 14:38:25 -07001450 }
1451
Jonathan Hart51539b82015-10-29 09:53:04 -07001452 private List<Controller> getControllers(Uuid bridgeUuid) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001453 DatabaseSchema dbSchema = schema.get(DATABASENAME);
andreaed976a42015-10-05 14:38:25 -07001454 if (dbSchema == null) {
1455 return null;
1456 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001457 OvsdbRowStore rowStore = getRowStore(DATABASENAME, BRIDGE);
andreaed976a42015-10-05 14:38:25 -07001458 if (rowStore == null) {
1459 log.debug("There is no bridge table");
1460 return null;
1461 }
1462
1463 Row bridgeRow = rowStore.getRow(bridgeUuid.value());
1464 Bridge bridge = (Bridge) TableGenerator.
1465 getTable(dbSchema, bridgeRow, OvsdbTable.BRIDGE);
1466
1467 //FIXME remove log
1468 log.warn("type of controller column", bridge.getControllerColumn()
1469 .data().getClass());
Jonathan Hart51539b82015-10-29 09:53:04 -07001470 Set<Uuid> controllerUuids = (Set<Uuid>) ((OvsdbSet) bridge
andreaed976a42015-10-05 14:38:25 -07001471 .getControllerColumn().data()).set();
andreaed976a42015-10-05 14:38:25 -07001472
Hyunsun Moon1251e192016-06-07 16:57:05 -07001473 OvsdbRowStore controllerRowStore = getRowStore(DATABASENAME, CONTROLLER);
andreaed976a42015-10-05 14:38:25 -07001474 if (controllerRowStore == null) {
1475 log.debug("There is no controller table");
1476 return null;
1477 }
1478
1479 List<Controller> ovsdbControllers = new ArrayList<>();
1480 ConcurrentMap<String, Row> controllerTableRows = controllerRowStore.getRowStore();
1481 controllerTableRows.forEach((key, row) -> {
Jonathan Hart51539b82015-10-29 09:53:04 -07001482 if (!controllerUuids.contains(Uuid.uuid(key))) {
andreaed976a42015-10-05 14:38:25 -07001483 return;
1484 }
1485 Controller controller = (Controller) TableGenerator
1486 .getTable(dbSchema, row, OvsdbTable.CONTROLLER);
1487 ovsdbControllers.add(controller);
1488 });
1489 return ovsdbControllers;
1490 }
1491
1492
Jonathan Hart51539b82015-10-29 09:53:04 -07001493 private Uuid getBridgeUuid(DeviceId openflowDeviceId) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001494 DatabaseSchema dbSchema = schema.get(DATABASENAME);
andreaed976a42015-10-05 14:38:25 -07001495 if (dbSchema == null) {
1496 return null;
1497 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001498 OvsdbRowStore rowStore = getRowStore(DATABASENAME, BRIDGE);
andreaed976a42015-10-05 14:38:25 -07001499 if (rowStore == null) {
1500 log.debug("There is no bridge table");
1501 return null;
1502 }
1503
1504 ConcurrentMap<String, Row> bridgeTableRows = rowStore.getRowStore();
Jonathan Hart51539b82015-10-29 09:53:04 -07001505 final AtomicReference<Uuid> uuid = new AtomicReference<>();
andreaed976a42015-10-05 14:38:25 -07001506 for (Map.Entry<String, Row> entry : bridgeTableRows.entrySet()) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001507 Bridge bridge = (Bridge) TableGenerator.getTable(
1508 dbSchema,
1509 entry.getValue(),
1510 OvsdbTable.BRIDGE);
1511
1512 if (matchesDpid(bridge, openflowDeviceId)) {
Jonathan Hart51539b82015-10-29 09:53:04 -07001513 uuid.set(Uuid.uuid(entry.getKey()));
andreaed976a42015-10-05 14:38:25 -07001514 break;
1515 }
1516 }
1517 if (uuid.get() == null) {
1518 log.debug("There is no bridge for {}", openflowDeviceId);
1519 }
1520 return uuid.get();
andreaed976a42015-10-05 14:38:25 -07001521 }
1522
1523 private static boolean matchesDpid(Bridge b, DeviceId deviceId) {
1524 String ofDpid = deviceId.toString().replace("of:", "");
1525 Set ofDeviceIds = ((OvsdbSet) b.getDatapathIdColumn().data()).set();
1526 //TODO Set<String>
1527 return ofDeviceIds.contains(ofDpid);
1528 }
1529
1530 @Override
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001531 public Set<OvsdbPort> getPorts() {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001532 Set<OvsdbPort> ovsdbPorts = new HashSet<>();
1533 OvsdbTableStore tableStore = getTableStore(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001534 if (tableStore == null) {
1535 return null;
1536 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001537 OvsdbRowStore rowStore = tableStore.getRows(INTERFACE);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001538 if (rowStore == null) {
1539 return null;
1540 }
1541 ConcurrentMap<String, Row> rows = rowStore.getRowStore();
1542 for (String uuid : rows.keySet()) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001543 Row row = getRow(DATABASENAME, INTERFACE, uuid);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001544 OvsdbPort ovsdbPort = getOvsdbPort(row);
1545 if (ovsdbPort != null) {
1546 ovsdbPorts.add(ovsdbPort);
1547 }
1548 }
1549 return ovsdbPorts;
1550 }
1551
1552 @Override
1553 public DatabaseSchema getDatabaseSchema(String dbName) {
1554 return schema.get(dbName);
1555 }
1556
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001557 private OvsdbPort getOvsdbPort(Row row) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001558 DatabaseSchema dbSchema = getDatabaseSchema(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001559 Interface intf = (Interface) TableGenerator
1560 .getTable(dbSchema, row, OvsdbTable.INTERFACE);
1561 if (intf == null) {
1562 return null;
1563 }
1564 long ofPort = getOfPort(intf);
1565 String portName = intf.getName();
1566 if ((ofPort < 0) || (portName == null)) {
1567 return null;
1568 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001569 return new OvsdbPort(new OvsdbPortNumber(ofPort), new OvsdbPortName(portName));
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001570 }
1571
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001572 private OvsdbBridge getOvsdbBridge(Row row) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001573 DatabaseSchema dbSchema = getDatabaseSchema(DATABASENAME);
1574 Bridge bridge = (Bridge) TableGenerator.getTable(dbSchema, row, OvsdbTable.BRIDGE);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001575 if (bridge == null) {
1576 return null;
1577 }
1578
1579 OvsdbSet datapathIdSet = (OvsdbSet) bridge.getDatapathIdColumn().data();
1580 @SuppressWarnings("unchecked")
1581 Set<String> datapathIds = datapathIdSet.set();
Jon Hallcbd1b392017-01-18 20:15:44 -08001582 if (datapathIds == null || datapathIds.isEmpty()) {
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001583 return null;
1584 }
1585 String datapathId = (String) datapathIds.toArray()[0];
1586 String bridgeName = bridge.getName();
1587 if ((datapathId == null) || (bridgeName == null)) {
1588 return null;
1589 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001590 return OvsdbBridge.builder().name(bridgeName).datapathId(datapathId).build();
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001591 }
1592
Frank Wange11a98d2016-10-26 17:04:03 +08001593 private OvsdbQos getOvsdbQos(Row row) {
1594 DatabaseSchema dbSchema = getDatabaseSchema(DATABASENAME);
1595 Qos qos = (Qos) TableGenerator.getTable(dbSchema, row, OvsdbTable.QOS);
1596 if (qos == null) {
1597 return null;
1598 }
1599
1600 String type = (String) qos.getTypeColumn().data();
1601 Map<String, String> otherConfigs;
1602 Map<String, String> externalIds;
1603 Map<Long, String> queues;
1604
1605 otherConfigs = ((OvsdbMap) qos.getOtherConfigColumn().data()).map();
1606 externalIds = ((OvsdbMap) qos.getExternalIdsColumn().data()).map();
1607 queues = ((OvsdbMap) qos.getQueuesColumn().data()).map();
1608 return OvsdbQos.builder().qosType(type).
1609 queues(queues).otherConfigs(otherConfigs).
1610 externalIds(externalIds).build();
1611 }
1612
1613 private OvsdbQueue getOvsdbQueue(Row row) {
1614 DatabaseSchema dbSchema = getDatabaseSchema(DATABASENAME);
1615 Queue queue = (Queue) TableGenerator.getTable(dbSchema, row, OvsdbTable.QUEUE);
1616 if (queue == null) {
1617 return null;
1618 }
1619
1620 OvsdbSet dscpOvsdbSet = ((OvsdbSet) queue.getDscpColumn().data());
1621 @SuppressWarnings("unchecked")
1622 Set<String> dscpSet = dscpOvsdbSet.set();
1623 Long dscp = null;
1624 if (dscpSet != null && !dscpSet.isEmpty()) {
1625 dscp = Long.valueOf((String) dscpSet.toArray()[0]);
1626 }
1627
1628 Map<String, String> otherConfigs;
1629 Map<String, String> externalIds;
1630
1631 otherConfigs = ((OvsdbMap) queue.getOtherConfigColumn().data()).map();
1632 externalIds = ((OvsdbMap) queue.getExternalIdsColumn().data()).map();
1633 return OvsdbQueue.builder().dscp(dscp).
1634 otherConfigs(otherConfigs).externalIds(externalIds).build();
1635 }
1636
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001637 private long getOfPort(Interface intf) {
1638 OvsdbSet ofPortSet = (OvsdbSet) intf.getOpenFlowPortColumn().data();
1639 @SuppressWarnings("unchecked")
1640 Set<Integer> ofPorts = ofPortSet.set();
Jon Hallcbd1b392017-01-18 20:15:44 -08001641 if (ofPorts == null || ofPorts.isEmpty()) {
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001642 log.debug("The ofport is null in {}", intf.getName());
1643 return -1;
1644 }
1645 // return (long) ofPorts.toArray()[0];
1646 Iterator<Integer> it = ofPorts.iterator();
1647 return Long.parseLong(it.next().toString());
1648 }
CNluciusa66c3972015-09-06 20:31:29 +08001649
1650 @Override
1651 public Set<OvsdbPort> getLocalPorts(Iterable<String> ifaceids) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001652 Set<OvsdbPort> ovsdbPorts = new HashSet<>();
1653 OvsdbTableStore tableStore = getTableStore(DATABASENAME);
CNluciusa66c3972015-09-06 20:31:29 +08001654 if (tableStore == null) {
1655 return null;
1656 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001657 OvsdbRowStore rowStore = tableStore.getRows(INTERFACE);
CNluciusa66c3972015-09-06 20:31:29 +08001658 if (rowStore == null) {
1659 return null;
1660 }
1661 ConcurrentMap<String, Row> rows = rowStore.getRowStore();
1662 for (String uuid : rows.keySet()) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001663 Row row = getRow(DATABASENAME, INTERFACE, uuid);
1664 DatabaseSchema dbSchema = getDatabaseSchema(DATABASENAME);
CNluciusa66c3972015-09-06 20:31:29 +08001665 Interface intf = (Interface) TableGenerator
1666 .getTable(dbSchema, row, OvsdbTable.INTERFACE);
1667 if (intf == null || getIfaceid(intf) == null) {
1668 continue;
1669 }
1670 String portName = intf.getName();
Hyunsun Moon1251e192016-06-07 16:57:05 -07001671 if (portName == null) {
1672 continue;
1673 }
CNluciusa66c3972015-09-06 20:31:29 +08001674 Set<String> ifaceidSet = Sets.newHashSet(ifaceids);
Hyunsun Moon1251e192016-06-07 16:57:05 -07001675 if (portName.startsWith(TYPEVXLAN) || !ifaceidSet.contains(getIfaceid(intf))) {
CNluciusa66c3972015-09-06 20:31:29 +08001676 continue;
1677 }
1678 long ofPort = getOfPort(intf);
Hyunsun Moon1251e192016-06-07 16:57:05 -07001679 if (ofPort < 0) {
CNluciusa66c3972015-09-06 20:31:29 +08001680 continue;
1681 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001682 ovsdbPorts.add(new OvsdbPort(new OvsdbPortNumber(ofPort),
1683 new OvsdbPortName(portName)));
CNluciusa66c3972015-09-06 20:31:29 +08001684 }
1685 return ovsdbPorts;
1686 }
1687
1688 private String getIfaceid(Interface intf) {
1689 OvsdbMap ovsdbMap = (OvsdbMap) intf.getExternalIdsColumn().data();
1690 @SuppressWarnings("unchecked")
1691 Map<String, String> externalIds = ovsdbMap.map();
1692 if (externalIds.isEmpty()) {
1693 log.warn("The external_ids is null");
1694 return null;
1695 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001696 String ifaceid = externalIds.get(EXTERNAL_ID_INTERFACE_ID);
CNluciusa66c3972015-09-06 20:31:29 +08001697 if (ifaceid == null) {
1698 log.warn("The ifaceid is null");
1699 return null;
1700 }
1701 return ifaceid;
1702 }
Hyunsun Moon5fb20a52015-09-25 17:02:33 -07001703
1704 @Override
1705 public void disconnect() {
1706 channel.disconnect();
1707 this.agent.removeConnectedNode(nodeId);
1708 }
Saritha1583a6f2017-06-16 14:42:58 +05301709
1710 @Override
1711 public List<OvsdbPortName> getPorts(List<String> portNames, DeviceId deviceId) {
1712 Uuid bridgeUuid = getBridgeUuid(deviceId);
1713 if (bridgeUuid == null) {
1714 log.error("Can't find the bridge for the deviceId {}", deviceId);
1715 return Collections.emptyList();
1716 }
1717 DatabaseSchema dbSchema = schema.get(DATABASENAME);
1718 Row bridgeRow = getRow(DATABASENAME, BRIDGE, bridgeUuid.value());
1719 Bridge bridge = (Bridge) TableGenerator.getTable(dbSchema, bridgeRow, OvsdbTable.BRIDGE);
1720 if (bridge == null) {
1721 return Collections.emptyList();
1722 }
1723 OvsdbSet setPorts = (OvsdbSet) bridge.getPortsColumn().data();
1724 Set<Uuid> portSet = setPorts.set();
1725 if (portSet.isEmpty()) {
1726 return Collections.emptyList();
1727 }
1728
1729 Map<Uuid, Port> portMap = portSet.stream().collect(Collectors.toMap(
1730 java.util.function.Function.identity(), port -> (Port) TableGenerator
1731 .getTable(dbSchema, getRow(DATABASENAME,
1732 PORT, port.value()), OvsdbTable.PORT)));
1733
1734 List<OvsdbPortName> portList = portMap.entrySet().stream().filter(port -> Objects.nonNull(port.getValue())
1735 && portNames.contains(port.getValue().getName())
1736 && Objects.nonNull(getInterfacebyPort(port.getKey().value(), port.getValue().getName())))
1737 .map(port -> new OvsdbPortName(port.getValue().getName())).collect(Collectors.toList());
1738
1739 return Collections.unmodifiableList(portList);
1740 }
1741
1742 @Override
1743 public boolean getPortError(List<OvsdbPortName> portNames, DeviceId bridgeId) {
1744 Uuid bridgeUuid = getBridgeUuid(bridgeId);
1745
1746 List<Interface> interfaceList = portNames.stream().collect(Collectors
1747 .toMap(java.util.function.Function.identity(),
1748 port -> (Interface) getInterfacebyPort(getPortUuid(port.value(),
1749 bridgeUuid.value()), port.value())))
1750 .entrySet().stream().filter(intf -> Objects.nonNull(intf.getValue())
1751 && ((OvsdbSet) intf.getValue().getOpenFlowPortColumn().data()).set()
1752 .stream().findAny().orElse(OFPORT_ERROR_COMPARISON).equals(OFPORT_ERROR))
1753 .map(intf -> intf.getValue()).collect(Collectors.toList());
1754
1755 interfaceList.forEach(intf -> new Consumer<Interface>() {
1756 @Override
1757 public void accept(Interface intf) {
1758 try {
1759 Set<String> setErrors = ((OvsdbSet) intf.getErrorColumn().data()).set();
1760 log.info("Port has errors. ofport value - {}, Interface - {} has error - {} ",
1761 intf.getOpenFlowPortColumn().data(), intf.getName(), setErrors.stream()
1762 .findFirst().get());
1763 } catch (ColumnSchemaNotFoundException | VersionMismatchException e) {
1764 log.debug("Port has errors. ofport value - {}, Interface - {} has error - {} ",
1765 intf.getOpenFlowPortColumn().data(), intf.getName(), e);
1766 }
1767 }
1768 }.accept(intf));
1769
1770 return !interfaceList.isEmpty();
1771 }
1772
1773 private Interface getInterfacebyPort(String portUuid, String portName) {
1774 DatabaseSchema dbSchema = schema.get(DATABASENAME);
1775
1776 Row portRow = getRow(DATABASENAME, PORT, portUuid);
1777 Port port = (Port) TableGenerator.getTable(dbSchema, portRow,
1778 OvsdbTable.PORT);
1779 if (port == null) {
1780 return null;
1781 }
1782
1783 OvsdbSet setInterfaces = (OvsdbSet) port.getInterfacesColumn().data();
1784 Set<Uuid> interfaces = setInterfaces.set();
1785
1786 return interfaces.stream().map(intf -> (Interface) TableGenerator
1787 .getTable(dbSchema, getRow(DATABASENAME,
1788 INTERFACE, intf.value()), OvsdbTable.INTERFACE))
1789 .filter(intf -> Objects.nonNull(intf) && portName.equalsIgnoreCase(intf.getName()))
1790 .findFirst().orElse(null);
1791 }
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001792}