blob: 768addf970a8a1c25141969f7c5308dd864d54aa [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
Jonathan Hart6523e122017-11-14 09:23:49 -080018import com.fasterxml.jackson.databind.JsonNode;
19import com.google.common.base.Function;
20import com.google.common.collect.ImmutableList;
21import com.google.common.collect.ImmutableSet;
22import 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;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070028import io.netty.channel.Channel;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070029import org.onlab.packet.IpAddress;
andreaed976a42015-10-05 14:38:25 -070030import org.onosproject.net.DeviceId;
Frank Wange11a98d2016-10-26 17:04:03 +080031import org.onosproject.net.PortNumber;
rohitsharana127ba82018-01-16 02:17:30 +053032import org.onosproject.net.behaviour.ControlProtocolVersion;
andreaed976a42015-10-05 14:38:25 -070033import org.onosproject.net.behaviour.ControllerInfo;
Pier Ventref5d72362016-07-17 12:02:14 +020034import org.onosproject.net.behaviour.MirroringName;
Ray Milkeyb1250322017-06-05 17:18:17 -070035import org.onosproject.net.behaviour.MirroringStatistics;
Frank Wange11a98d2016-10-26 17:04:03 +080036import org.onosproject.net.behaviour.QosId;
tanbangchengc944c282017-11-12 20:42:59 +080037import org.onosproject.net.behaviour.QueueDescription;
Frank Wange11a98d2016-10-26 17:04:03 +080038import org.onosproject.net.behaviour.QueueId;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070039import org.onosproject.ovsdb.controller.OvsdbBridge;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070040import org.onosproject.ovsdb.controller.OvsdbClientService;
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070041import org.onosproject.ovsdb.controller.OvsdbInterface;
Pier Ventref5d72362016-07-17 12:02:14 +020042import org.onosproject.ovsdb.controller.OvsdbMirror;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070043import org.onosproject.ovsdb.controller.OvsdbNodeId;
44import org.onosproject.ovsdb.controller.OvsdbPort;
45import org.onosproject.ovsdb.controller.OvsdbPortName;
46import org.onosproject.ovsdb.controller.OvsdbPortNumber;
Frank Wange11a98d2016-10-26 17:04:03 +080047import org.onosproject.ovsdb.controller.OvsdbQos;
48import org.onosproject.ovsdb.controller.OvsdbQueue;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070049import org.onosproject.ovsdb.controller.OvsdbRowStore;
50import org.onosproject.ovsdb.controller.OvsdbStore;
51import org.onosproject.ovsdb.controller.OvsdbTableStore;
Saritha1583a6f2017-06-16 14:42:58 +053052import org.onosproject.ovsdb.rfc.exception.ColumnSchemaNotFoundException;
53import org.onosproject.ovsdb.rfc.exception.VersionMismatchException;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070054import org.onosproject.ovsdb.rfc.jsonrpc.Callback;
55import org.onosproject.ovsdb.rfc.message.OperationResult;
56import org.onosproject.ovsdb.rfc.message.TableUpdates;
Frank Wange11a98d2016-10-26 17:04:03 +080057import org.onosproject.ovsdb.rfc.notation.Column;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070058import org.onosproject.ovsdb.rfc.notation.Condition;
59import org.onosproject.ovsdb.rfc.notation.Mutation;
CNluciusa66c3972015-09-06 20:31:29 +080060import org.onosproject.ovsdb.rfc.notation.OvsdbMap;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070061import org.onosproject.ovsdb.rfc.notation.OvsdbSet;
62import org.onosproject.ovsdb.rfc.notation.Row;
Jonathan Hart51539b82015-10-29 09:53:04 -070063import org.onosproject.ovsdb.rfc.notation.Uuid;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070064import org.onosproject.ovsdb.rfc.operations.Delete;
65import org.onosproject.ovsdb.rfc.operations.Insert;
66import org.onosproject.ovsdb.rfc.operations.Mutate;
67import org.onosproject.ovsdb.rfc.operations.Operation;
68import org.onosproject.ovsdb.rfc.operations.Update;
69import org.onosproject.ovsdb.rfc.schema.ColumnSchema;
70import org.onosproject.ovsdb.rfc.schema.DatabaseSchema;
71import org.onosproject.ovsdb.rfc.schema.TableSchema;
72import org.onosproject.ovsdb.rfc.table.Bridge;
73import org.onosproject.ovsdb.rfc.table.Controller;
74import org.onosproject.ovsdb.rfc.table.Interface;
Pier Ventref5d72362016-07-17 12:02:14 +020075import org.onosproject.ovsdb.rfc.table.Mirror;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070076import org.onosproject.ovsdb.rfc.table.OvsdbTable;
77import org.onosproject.ovsdb.rfc.table.Port;
Frank Wange11a98d2016-10-26 17:04:03 +080078import org.onosproject.ovsdb.rfc.table.Qos;
79import org.onosproject.ovsdb.rfc.table.Queue;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070080import org.onosproject.ovsdb.rfc.table.TableGenerator;
81import org.onosproject.ovsdb.rfc.utils.ConditionUtil;
82import org.onosproject.ovsdb.rfc.utils.FromJsonUtil;
83import org.onosproject.ovsdb.rfc.utils.JsonRpcWriterUtil;
84import org.onosproject.ovsdb.rfc.utils.MutationUtil;
85import org.slf4j.Logger;
86import org.slf4j.LoggerFactory;
87
Jonathan Hart6523e122017-11-14 09:23:49 -080088import java.net.InetSocketAddress;
89import java.util.ArrayList;
90import java.util.Collections;
91import java.util.HashMap;
92import java.util.HashSet;
93import java.util.Iterator;
94import java.util.List;
95import java.util.Map;
96import java.util.Objects;
97import java.util.Optional;
98import java.util.Set;
99import java.util.concurrent.ConcurrentMap;
100import java.util.concurrent.ExecutionException;
101import java.util.concurrent.TimeUnit;
102import java.util.concurrent.TimeoutException;
103import java.util.concurrent.atomic.AtomicReference;
104import java.util.function.Consumer;
105import java.util.stream.Collectors;
Pier Ventref5d72362016-07-17 12:02:14 +0200106
Jonathan Hart6523e122017-11-14 09:23:49 -0800107import static org.onosproject.ovsdb.controller.OvsdbConstant.*;
Hyunsun Moon1251e192016-06-07 16:57:05 -0700108
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700109/**
110 * An representation of an ovsdb client.
111 */
Hyunsun Moon1251e192016-06-07 16:57:05 -0700112public class DefaultOvsdbClient implements OvsdbProviderService, OvsdbClientService {
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700113
jaegonkim01d7c912017-01-22 22:03:38 +0900114 private static final int TRANSACTCONFIG_TIMEOUT = 3; //sec
Saritha1583a6f2017-06-16 14:42:58 +0530115 private static final int OFPORT_ERROR_COMPARISON = 0;
jaegonkim01d7c912017-01-22 22:03:38 +0900116
Hyunsun Moon1251e192016-06-07 16:57:05 -0700117 private final Logger log = LoggerFactory.getLogger(DefaultOvsdbClient.class);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700118
119 private Channel channel;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700120 private OvsdbAgent agent;
121 private boolean connected;
122 private OvsdbNodeId nodeId;
123 private Callback monitorCallBack;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700124 private OvsdbStore ovsdbStore = new OvsdbStore();
125
126 private final Map<String, String> requestMethod = Maps.newHashMap();
Hyunsun Moon1251e192016-06-07 16:57:05 -0700127 private final Map<String, SettableFuture<? extends Object>> requestResult = Maps.newHashMap();
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700128 private final Map<String, DatabaseSchema> schema = Maps.newHashMap();
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700129
Pier Ventref5d72362016-07-17 12:02:14 +0200130
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700131 /**
132 * Creates an OvsdbClient.
133 *
134 * @param nodeId ovsdb node id
135 */
136 public DefaultOvsdbClient(OvsdbNodeId nodeId) {
137 this.nodeId = nodeId;
138 }
139
140 @Override
141 public OvsdbNodeId nodeId() {
142 return nodeId;
143 }
144
145 @Override
146 public void setAgent(OvsdbAgent agent) {
147 if (this.agent == null) {
148 this.agent = agent;
149 }
150 }
151
152 @Override
153 public void setChannel(Channel channel) {
154 this.channel = channel;
155 }
156
157 @Override
158 public void setConnection(boolean connected) {
159 this.connected = connected;
160 }
161
162 @Override
163 public boolean isConnected() {
164 return this.connected;
165 }
166
167 @Override
168 public void nodeAdded() {
169 this.agent.addConnectedNode(nodeId, this);
170 }
171
172 @Override
173 public void nodeRemoved() {
174 this.agent.removeConnectedNode(nodeId);
175 channel.disconnect();
176 }
177
178 /**
179 * Gets the ovsdb table store.
180 *
181 * @param dbName the ovsdb database name
182 * @return ovsTableStore, empty if table store is find
183 */
184 private OvsdbTableStore getTableStore(String dbName) {
185 if (ovsdbStore == null) {
186 return null;
187 }
188 return ovsdbStore.getOvsdbTableStore(dbName);
189 }
190
191 /**
192 * Gets the ovsdb row store.
193 *
andreaed976a42015-10-05 14:38:25 -0700194 * @param dbName the ovsdb database name
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700195 * @param tableName the ovsdb table name
Hyunsun Moon6125c612015-10-15 10:54:44 -0700196 * @return ovsRowStore, empty store if no rows exist in the table
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700197 */
198 private OvsdbRowStore getRowStore(String dbName, String tableName) {
199 OvsdbTableStore tableStore = getTableStore(dbName);
200 if (tableStore == null) {
201 return null;
202 }
Hyunsun Moon6125c612015-10-15 10:54:44 -0700203
204 OvsdbRowStore rowStore = tableStore.getRows(tableName);
205 if (rowStore == null) {
206 rowStore = new OvsdbRowStore();
207 }
208 return rowStore;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700209 }
210
211 /**
212 * Gets the ovsdb row.
213 *
andreaed976a42015-10-05 14:38:25 -0700214 * @param dbName the ovsdb database name
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700215 * @param tableName the ovsdb table name
andreaed976a42015-10-05 14:38:25 -0700216 * @param uuid the key of the row
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700217 * @return row, empty if row is find
218 */
219 @Override
220 public Row getRow(String dbName, String tableName, String uuid) {
221 OvsdbTableStore tableStore = getTableStore(dbName);
222 if (tableStore == null) {
223 return null;
224 }
225 OvsdbRowStore rowStore = tableStore.getRows(tableName);
226 if (rowStore == null) {
227 return null;
228 }
229 return rowStore.getRow(uuid);
230 }
231
232 @Override
233 public void removeRow(String dbName, String tableName, String uuid) {
234 OvsdbTableStore tableStore = getTableStore(dbName);
235 if (tableStore == null) {
236 return;
237 }
238 OvsdbRowStore rowStore = tableStore.getRows(tableName);
239 if (rowStore == null) {
240 return;
241 }
242 rowStore.deleteRow(uuid);
243 }
244
245 @Override
246 public void updateOvsdbStore(String dbName, String tableName, String uuid,
247 Row row) {
248 OvsdbTableStore tableStore = ovsdbStore.getOvsdbTableStore(dbName);
249 if (tableStore == null) {
250 tableStore = new OvsdbTableStore();
251 }
252 OvsdbRowStore rowStore = tableStore.getRows(tableName);
253 if (rowStore == null) {
254 rowStore = new OvsdbRowStore();
255 }
256 rowStore.insertRow(uuid, row);
257 tableStore.createOrUpdateTable(tableName, rowStore);
258 ovsdbStore.createOrUpdateOvsdbStore(dbName, tableStore);
259 }
260
Pier Ventref5d72362016-07-17 12:02:14 +0200261 /**
262 * Gets the Mirror uuid.
263 *
264 * @param mirrorName mirror name
265 * @return mirror uuid, empty if no uuid is found
266 */
267 @Override
268 public String getMirrorUuid(String mirrorName) {
269 DatabaseSchema dbSchema = schema.get(DATABASENAME);
270 OvsdbRowStore rowStore = getRowStore(DATABASENAME, MIRROR);
271 if (rowStore == null) {
272 log.warn("The mirror uuid is null");
273 return null;
274 }
275
276 ConcurrentMap<String, Row> mirrorTableRows = rowStore.getRowStore();
277 if (mirrorTableRows == null) {
278 log.warn("The mirror uuid is null");
279 return null;
280 }
281
282 for (String uuid : mirrorTableRows.keySet()) {
283 Mirror mirror = (Mirror) TableGenerator
284 .getTable(dbSchema, mirrorTableRows.get(uuid), OvsdbTable.MIRROR);
285 String name = mirror.getName();
286 if (name.contains(mirrorName)) {
287 return uuid;
288 }
289 }
290 log.warn("Mirroring not found");
291 return null;
292 }
293
294 /**
295 * Gets mirrors of the device.
296 *
297 * @param deviceId target device id
298 * @return set of mirroring; empty if no mirror is found
299 */
300 @Override
301 public Set<MirroringStatistics> getMirroringStatistics(DeviceId deviceId) {
302 Uuid bridgeUuid = getBridgeUuid(deviceId);
303 if (bridgeUuid == null) {
304 log.warn("Couldn't find bridge {} in {}", deviceId, nodeId.getIpAddress());
305 return null;
306 }
307
308 List<MirroringStatistics> mirrorings = getMirrorings(bridgeUuid);
309 if (mirrorings == null) {
310 log.warn("Couldn't find mirrors in {}", nodeId.getIpAddress());
311 return null;
312 }
313 return ImmutableSet.copyOf(mirrorings);
314 }
315
316 /**
317 * Helper method which retrieves mirrorings statistics using bridge uuid.
318 *
319 * @param bridgeUuid the uuid of the bridge
320 * @return the list of the mirrorings statistics.
321 */
322 private List<MirroringStatistics> getMirrorings(Uuid bridgeUuid) {
323 DatabaseSchema dbSchema = schema.get(DATABASENAME);
324 if (dbSchema == null) {
325 log.warn("Unable to retrieve dbSchema {}", DATABASENAME);
326 return null;
327 }
328 OvsdbRowStore rowStore = getRowStore(DATABASENAME, BRIDGE);
329 if (rowStore == null) {
330 log.warn("Unable to retrieve rowStore {} of {}", BRIDGE, DATABASENAME);
331 return null;
332 }
333
334 Row bridgeRow = rowStore.getRow(bridgeUuid.value());
335 Bridge bridge = (Bridge) TableGenerator.
336 getTable(dbSchema, bridgeRow, OvsdbTable.BRIDGE);
337
338 Set<Uuid> mirroringsUuids = (Set<Uuid>) ((OvsdbSet) bridge
339 .getMirrorsColumn().data()).set();
340
341 OvsdbRowStore mirrorRowStore = getRowStore(DATABASENAME, MIRROR);
342 if (mirrorRowStore == null) {
343 log.warn("Unable to retrieve rowStore {} of {}", MIRROR, DATABASENAME);
344 return null;
345 }
346
347 List<MirroringStatistics> mirroringStatistics = new ArrayList<>();
348 ConcurrentMap<String, Row> mirrorTableRows = mirrorRowStore.getRowStore();
349 mirrorTableRows.forEach((key, row) -> {
350 if (!mirroringsUuids.contains(Uuid.uuid(key))) {
351 return;
352 }
353 Mirror mirror = (Mirror) TableGenerator
354 .getTable(dbSchema, row, OvsdbTable.MIRROR);
355 mirroringStatistics.add(MirroringStatistics.mirroringStatistics(mirror.getName(),
356 (Map<String, Integer>) ((OvsdbMap) mirror
357 .getStatisticsColumn().data()).map()));
358 });
359 return ImmutableList.copyOf(mirroringStatistics);
360 }
361
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700362 @Override
363 public String getPortUuid(String portName, String bridgeUuid) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700364 DatabaseSchema dbSchema = schema.get(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700365
Hyunsun Moon1251e192016-06-07 16:57:05 -0700366 Row bridgeRow = getRow(DATABASENAME, BRIDGE, bridgeUuid);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700367 Bridge bridge = (Bridge) TableGenerator.getTable(dbSchema, bridgeRow,
368 OvsdbTable.BRIDGE);
369 if (bridge != null) {
370 OvsdbSet setPorts = (OvsdbSet) bridge.getPortsColumn().data();
371 @SuppressWarnings("unchecked")
Jonathan Hart51539b82015-10-29 09:53:04 -0700372 Set<Uuid> ports = setPorts.set();
Jon Hallcbd1b392017-01-18 20:15:44 -0800373 if (ports == null || ports.isEmpty()) {
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700374 log.warn("The port uuid is null");
375 return null;
376 }
377
Jonathan Hart51539b82015-10-29 09:53:04 -0700378 for (Uuid uuid : ports) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700379 Row portRow = getRow(DATABASENAME, PORT, uuid.value());
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700380 Port port = (Port) TableGenerator.getTable(dbSchema, portRow,
381 OvsdbTable.PORT);
382 if (port != null && portName.equalsIgnoreCase(port.getName())) {
383 return uuid.value();
384 }
385 }
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700386 }
387 return null;
388 }
389
390 @Override
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700391 public String getBridgeUuid(String bridgeName) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700392 DatabaseSchema dbSchema = schema.get(DATABASENAME);
Hyunsun Moon1251e192016-06-07 16:57:05 -0700393 OvsdbRowStore rowStore = getRowStore(DATABASENAME, BRIDGE);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700394 if (rowStore == null) {
395 log.debug("The bridge uuid is null");
396 return null;
397 }
398
399 ConcurrentMap<String, Row> bridgeTableRows = rowStore.getRowStore();
400 if (bridgeTableRows == null) {
401 log.debug("The bridge uuid is null");
402 return null;
403 }
404
405 for (String uuid : bridgeTableRows.keySet()) {
406 Bridge bridge = (Bridge) TableGenerator
Hyunsun Moon1251e192016-06-07 16:57:05 -0700407 .getTable(dbSchema, bridgeTableRows.get(uuid), OvsdbTable.BRIDGE);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700408 if (bridge.getName().equals(bridgeName)) {
409 return uuid;
410 }
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700411 }
412 return null;
413 }
414
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700415 private String getOvsUuid(String dbName) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700416 OvsdbRowStore rowStore = getRowStore(DATABASENAME, DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700417 if (rowStore == null) {
418 log.debug("The bridge uuid is null");
419 return null;
420 }
421 ConcurrentMap<String, Row> ovsTableRows = rowStore.getRowStore();
422 if (ovsTableRows != null) {
423 for (String uuid : ovsTableRows.keySet()) {
424 Row row = ovsTableRows.get(uuid);
425 String tableName = row.tableName();
426 if (tableName.equals(dbName)) {
427 return uuid;
428 }
429 }
430 }
431 return null;
432 }
433
434 @Override
435 public void createPort(String bridgeName, String portName) {
436 String bridgeUuid = getBridgeUuid(bridgeName);
437 if (bridgeUuid == null) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700438 log.error("Can't find bridge {} in {}", bridgeName, nodeId.getIpAddress());
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700439 return;
440 }
441
Hyunsun Moon1251e192016-06-07 16:57:05 -0700442 DatabaseSchema dbSchema = schema.get(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700443 String portUuid = getPortUuid(portName, bridgeUuid);
Hyunsun Moon1251e192016-06-07 16:57:05 -0700444 Port port = (Port) TableGenerator.createTable(dbSchema, OvsdbTable.PORT);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700445 port.setName(portName);
446 if (portUuid == null) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700447 insertConfig(PORT, UUID, BRIDGE, PORTS, bridgeUuid, port.getRow());
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700448 }
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700449 }
450
451 @Override
452 public void dropPort(String bridgeName, String portName) {
453 String bridgeUuid = getBridgeUuid(bridgeName);
454 if (bridgeUuid == null) {
455 log.error("Could not find Bridge {} in {}", bridgeName, nodeId);
456 return;
457 }
458
459 String portUuid = getPortUuid(portName, bridgeUuid);
460 if (portUuid != null) {
461 log.info("Port {} delete", portName);
Frank Wange11a98d2016-10-26 17:04:03 +0800462 deleteConfig(PORT, UUID, portUuid, BRIDGE, PORTS, Uuid.uuid(portUuid));
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700463 }
464 }
465
lishuai6c56f5e2015-11-17 16:38:19 +0800466 @Override
Hyunsun Moon1251e192016-06-07 16:57:05 -0700467 public boolean createBridge(OvsdbBridge ovsdbBridge) {
468 DatabaseSchema dbSchema = schema.get(DATABASENAME);
469 String ovsUuid = getOvsUuid(DATABASENAME);
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700470
471 if (dbSchema == null || ovsUuid == null) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700472 log.error("Can't find database Open_vSwitch");
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700473 return false;
474 }
475
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700476 Bridge bridge = (Bridge) TableGenerator.createTable(dbSchema, OvsdbTable.BRIDGE);
Hyunsun Moon1251e192016-06-07 16:57:05 -0700477 bridge.setOtherConfig(ovsdbBridge.otherConfigs());
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700478
Hyunsun Moon1251e192016-06-07 16:57:05 -0700479 if (ovsdbBridge.failMode().isPresent()) {
480 String failMode = ovsdbBridge.failMode().get().name().toLowerCase();
481 bridge.setFailMode(Sets.newHashSet(failMode));
Bob zhoue9795fd2016-05-12 20:18:45 +0800482 }
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700483
jaegonkim80bee532017-05-15 15:16:38 +0900484 if (ovsdbBridge.datapathType().isPresent()) {
485 String datapathType = ovsdbBridge.datapathType().get();
486 bridge.setDatapathType(datapathType);
487 }
488
rohitsharana127ba82018-01-16 02:17:30 +0530489 if (ovsdbBridge.controlProtocols().isPresent()) {
490 bridge.setProtocols(ovsdbBridge.controlProtocols().get().stream()
491 .map(ControlProtocolVersion::toString)
492 .collect(Collectors.toCollection(HashSet::new)));
493 }
494
Hyunsun Moon1251e192016-06-07 16:57:05 -0700495 String bridgeUuid = getBridgeUuid(ovsdbBridge.name());
Hyunsun Moon98025542016-03-08 04:36:02 -0800496 if (bridgeUuid == null) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700497 bridge.setName(ovsdbBridge.name());
498 bridgeUuid = insertConfig(
499 BRIDGE, UUID, DATABASENAME, BRIDGES,
500 ovsUuid, bridge.getRow());
Hyunsun Moon98025542016-03-08 04:36:02 -0800501 } else {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700502 // update the bridge if it's already existing
503 updateConfig(BRIDGE, UUID, bridgeUuid, bridge.getRow());
Hyunsun Moon98025542016-03-08 04:36:02 -0800504 }
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700505
Hyunsun Moon1251e192016-06-07 16:57:05 -0700506 if (bridgeUuid == null) {
507 log.warn("Failed to create bridge {} on {}", ovsdbBridge.name(), nodeId);
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700508 return false;
509 }
510
Hyunsun Moon1251e192016-06-07 16:57:05 -0700511 createPort(ovsdbBridge.name(), ovsdbBridge.name());
512 setControllersWithUuid(Uuid.uuid(bridgeUuid), ovsdbBridge.controllers());
513
514 log.info("Created bridge {}", ovsdbBridge.name());
Hyunsun Moon646d8c42015-10-08 20:32:44 -0700515 return true;
516 }
517
Hyunsun Moon1251e192016-06-07 16:57:05 -0700518 @Override
519 public ControllerInfo localController() {
520 IpAddress ipAddress = IpAddress.valueOf(((InetSocketAddress)
521 channel.localAddress()).getAddress());
522 return new ControllerInfo(ipAddress, OFPORT, "tcp");
andreaed976a42015-10-05 14:38:25 -0700523 }
524
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700525 private void setControllersWithUuid(Uuid bridgeUuid, List<ControllerInfo> controllers) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700526 DatabaseSchema dbSchema = schema.get(DATABASENAME);
andreaed976a42015-10-05 14:38:25 -0700527 if (dbSchema == null) {
528 log.debug("There is no schema");
529 return;
530 }
531 List<Controller> oldControllers = getControllers(bridgeUuid);
532 if (oldControllers == null) {
533 log.warn("There are no controllers");
534 return;
535 }
536
andreaed976a42015-10-05 14:38:25 -0700537 Set<ControllerInfo> newControllers = new HashSet<>(controllers);
538 List<Controller> removeControllers = new ArrayList<>();
539 oldControllers.forEach(controller -> {
540 ControllerInfo controllerInfo = new ControllerInfo((String) controller.getTargetColumn().data());
541 if (newControllers.contains(controllerInfo)) {
542 newControllers.remove(controllerInfo);
andreaed976a42015-10-05 14:38:25 -0700543 } else {
544 removeControllers.add(controller);
545 }
546 });
Hyunsun Moon1251e192016-06-07 16:57:05 -0700547 OvsdbRowStore controllerRowStore = getRowStore(DATABASENAME, CONTROLLER);
andreaed976a42015-10-05 14:38:25 -0700548 if (controllerRowStore == null) {
549 log.debug("There is no controller table");
550 return;
551 }
552
Hyunsun Moon1251e192016-06-07 16:57:05 -0700553 removeControllers.forEach(c -> deleteConfig(CONTROLLER, UUID, c.getRow().uuid().value(),
Saritha8a0c36a2017-07-04 15:01:35 +0530554 BRIDGE, BRIDGE_CONTROLLER, c.getRow().uuid()));
andreaed976a42015-10-05 14:38:25 -0700555 newControllers.stream().map(c -> {
556 Controller controller = (Controller) TableGenerator
557 .createTable(dbSchema, OvsdbTable.CONTROLLER);
558 controller.setTarget(c.target());
559 return controller;
tanbangcheng62cd4492017-11-24 21:57:05 +0800560 }).forEach(c -> insertConfig(CONTROLLER, UUID, BRIDGE, BRIDGE_CONTROLLER,
561 bridgeUuid.value(),
562 c.getRow()));
andreaed976a42015-10-05 14:38:25 -0700563
andreaed976a42015-10-05 14:38:25 -0700564 }
565
andreaed976a42015-10-05 14:38:25 -0700566 @Override
567 public void setControllersWithDeviceId(DeviceId deviceId, List<ControllerInfo> controllers) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700568 setControllersWithUuid(getBridgeUuid(deviceId), controllers);
andreaed976a42015-10-05 14:38:25 -0700569 }
570
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700571 @Override
572 public void dropBridge(String bridgeName) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700573 String bridgeUuid = getBridgeUuid(bridgeName);
574 if (bridgeUuid == null) {
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700575 log.warn("Could not find bridge in node", nodeId.getIpAddress());
576 return;
577 }
Frank Wange11a98d2016-10-26 17:04:03 +0800578 deleteConfig(BRIDGE, UUID, bridgeUuid, DATABASENAME, BRIDGES, Uuid.uuid(bridgeUuid));
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700579 }
580
Frank Wange11a98d2016-10-26 17:04:03 +0800581 @Override
582 public void applyQos(PortNumber portNumber, String qosName) {
583 DatabaseSchema dbSchema = schema.get(DATABASENAME);
584 OvsdbRowStore portRowStore = getRowStore(DATABASENAME, PORT);
585 if (portRowStore == null) {
586 log.debug("The port uuid is null");
587 return;
588 }
589 OvsdbRowStore qosRowStore = getRowStore(DATABASENAME, QOS);
590 if (qosRowStore == null) {
591 log.debug("The qos uuid is null");
592 return;
593 }
594
595 // Due to Qos Table doesn't have a unique identifier except uuid, unlike
596 // Bridge or Port Table has a name column,in order to make the api more
597 // general, put qos name in external_ids column of Qos Table if this qos
598 // created by onos.
599 ConcurrentMap<String, Row> qosTableRows = qosRowStore.getRowStore();
600 ConcurrentMap<String, Row> portTableRows = portRowStore.getRowStore();
601 Row qosRow = qosTableRows.values().stream().filter(r -> {
602 OvsdbMap ovsdbMap = (OvsdbMap) (r.getColumn(EXTERNAL_ID).data());
603 return qosName.equals(ovsdbMap.map().get(QOS_EXTERNAL_ID_KEY));
604 }).findFirst().orElse(null);
605
606 Row portRow = portTableRows.values().stream()
607 .filter(r -> r.getColumn("name").data().equals(portNumber.name()))
608 .findFirst().orElse(null);
609 if (portRow != null && qosRow != null) {
610 String qosId = qosRow.uuid().value();
611 Uuid portUuid = portRow.uuid();
612 Map<String, Column> columns = new HashMap<>();
613 Row newPortRow = new Row(PORT, portUuid, columns);
614 Port newport = new Port(dbSchema, newPortRow);
615 columns.put(Port.PortColumn.QOS.columnName(), newport.getQosColumn());
616 newport.setQos(Uuid.uuid(qosId));
617 updateConfig(PORT, UUID, portUuid.value(), newport.getRow());
618 }
619 }
620
621 @Override
622 public void removeQos(PortNumber portNumber) {
623 DatabaseSchema dbSchema = schema.get(DATABASENAME);
624 OvsdbRowStore rowStore = getRowStore(DATABASENAME, PORT);
625 if (rowStore == null) {
626 log.debug("The qos uuid is null");
627 return;
628 }
629
630 ConcurrentMap<String, Row> ovsTableRows = rowStore.getRowStore();
631 Row portRow = ovsTableRows.values().stream()
632 .filter(r -> r.getColumn("name").data().equals(portNumber.name()))
633 .findFirst().orElse(null);
634 if (portRow == null) {
635 log.warn("Couldn't find port {} in ovsdb port table.", portNumber.name());
636 return;
637 }
638
639 OvsdbSet ovsdbSet = ((OvsdbSet) portRow.getColumn(PORT_QOS).data());
640 @SuppressWarnings("unchecked")
641 Set<Uuid> qosIdSet = ovsdbSet.set();
642 if (qosIdSet == null || qosIdSet.isEmpty()) {
643 return;
644 }
645 Uuid qosUuid = (Uuid) qosIdSet.toArray()[0];
646 Condition condition = ConditionUtil.isEqual(UUID, portRow.uuid());
647 List<Condition> conditions = Lists.newArrayList(condition);
648 Mutation mutation = MutationUtil.delete(PORT_QOS, qosUuid);
649 List<Mutation> mutations = Lists.newArrayList(mutation);
650
651 ArrayList<Operation> operations = Lists.newArrayList();
652 Mutate mutate = new Mutate(dbSchema.getTableSchema(PORT), conditions, mutations);
653 operations.add(mutate);
654 transactConfig(DATABASENAME, operations);
655 }
656
657 @Override
658 public boolean createQos(OvsdbQos ovsdbQos) {
659 DatabaseSchema dbSchema = schema.get(DATABASENAME);
660 Qos qos = (Qos) TableGenerator.createTable(dbSchema, OvsdbTable.QOS);
661 OvsdbRowStore rowStore = getRowStore(DATABASENAME, QOS);
662 if (rowStore == null) {
663 log.debug("The qos uuid is null");
664 return false;
665 }
666
667 ArrayList<Operation> operations = Lists.newArrayList();
668 Set<String> types = Sets.newHashSet();
669 Map<Long, Uuid> queues = Maps.newHashMap();
670
671 types.add(ovsdbQos.qosType());
672 qos.setOtherConfig(ovsdbQos.otherConfigs());
673 qos.setExternalIds(ovsdbQos.externalIds());
674 qos.setType(types);
675 if (ovsdbQos.qosQueues().isPresent()) {
676 for (Map.Entry<Long, String> entry : ovsdbQos.qosQueues().get().entrySet()) {
677 OvsdbRowStore queueRowStore = getRowStore(DATABASENAME, QUEUE);
678 if (queueRowStore != null) {
679 ConcurrentMap<String, Row> queueTableRows = queueRowStore.getRowStore();
680 Row queueRow = queueTableRows.values().stream().filter(r -> {
681 OvsdbMap ovsdbMap = (OvsdbMap) (r.getColumn(EXTERNAL_ID).data());
682 return entry.getValue().equals(ovsdbMap.map().get(QUEUE_EXTERNAL_ID_KEY));
683 }).findFirst().orElse(null);
684 if (queueRow != null) {
685 queues.put(entry.getKey(), queueRow.uuid());
686 }
687 }
688 }
689 qos.setQueues(queues);
690 }
691
692 Insert qosInsert = new Insert(dbSchema.getTableSchema(QOS), QOS, qos.getRow());
693 operations.add(qosInsert);
694 try {
695 transactConfig(DATABASENAME, operations).get();
696 } catch (InterruptedException | ExecutionException e) {
697 return false;
698 }
699 return true;
700 }
701
702 @Override
703 public void dropQos(QosId qosId) {
704 OvsdbRowStore rowStore = getRowStore(DATABASENAME, QOS);
705 if (rowStore != null) {
706 ConcurrentMap<String, Row> qosTableRows = rowStore.getRowStore();
707 Row qosRow = qosTableRows.values().stream().filter(r -> {
708 OvsdbMap ovsdbMap = (OvsdbMap) (r.getColumn(EXTERNAL_ID).data());
709 return qosId.name().equals(ovsdbMap.map().get(QOS_EXTERNAL_ID_KEY));
710 }).findFirst().orElse(null);
711 if (qosRow != null) {
712 deleteConfig(QOS, UUID, qosRow.uuid().value(), PORT, PORT_QOS, qosRow.uuid());
713 }
714 }
715 }
716 @Override
717 public OvsdbQos getQos(QosId qosId) {
718 Set<OvsdbQos> ovsdbQoses = getQoses();
719 return ovsdbQoses.stream().filter(r ->
720 qosId.name().equals(r.externalIds().get(QOS_EXTERNAL_ID_KEY))).
721 findFirst().orElse(null);
722 }
723
724 @Override
725 public Set<OvsdbQos> getQoses() {
726 Set<OvsdbQos> ovsdbQoses = new HashSet<>();
727 OvsdbRowStore rowStore = getRowStore(DATABASENAME, QOS);
728 if (rowStore == null) {
729 log.debug("The qos uuid is null");
730 return ovsdbQoses;
731 }
732 ConcurrentMap<String, Row> rows = rowStore.getRowStore();
kdarapufce5abb2018-05-10 19:37:53 +0530733 ovsdbQoses = rows.keySet().stream()
734 .map(uuid -> getRow(DATABASENAME, QOS, uuid))
735 .map(this::getOvsdbQos)
736 .filter(Objects::nonNull)
737 .collect(Collectors.toSet());
Frank Wange11a98d2016-10-26 17:04:03 +0800738 return ovsdbQoses;
739 }
740
741 @Override
tanbangchengc944c282017-11-12 20:42:59 +0800742 public void bindQueues(QosId qosId, Map<Long, QueueDescription> queues) {
743 DatabaseSchema dbSchema = schema.get(DATABASENAME);
744 OvsdbRowStore qosRowStore = getRowStore(DATABASENAME, QOS);
745 if (qosRowStore == null) {
746 log.debug("The qos uuid is null");
747 return;
748 }
749 OvsdbRowStore queueRowStore = getRowStore(DATABASENAME, QUEUE);
750 if (queueRowStore == null) {
751 log.debug("The queue uuid is null");
752 return;
753 }
754
755 ConcurrentMap<String, Row> qosTableRows = qosRowStore.getRowStore();
756 ConcurrentMap<String, Row> queueTableRows = queueRowStore.getRowStore();
757
758 Row qosRow = qosTableRows.values().stream().filter(r -> {
759 OvsdbMap ovsdbMap = (OvsdbMap) (r.getColumn(EXTERNAL_ID).data());
760 return qosId.name().equals(ovsdbMap.map().get(QOS_EXTERNAL_ID_KEY));
761 }).findFirst().orElse(null);
762
763 if (qosRow == null) {
764 log.warn("Can't find QoS {}", qosId);
765 return;
766 }
767
768 Uuid qosUuid = qosRow.uuid();
769
kdarapufce5abb2018-05-10 19:37:53 +0530770 Map<Long, Uuid> newQueues = new HashMap<>();
tanbangchengc944c282017-11-12 20:42:59 +0800771 for (Map.Entry<Long, QueueDescription> entry : queues.entrySet()) {
772 Row queueRow = queueTableRows.values().stream().filter(r -> {
773 OvsdbMap ovsdbMap = (OvsdbMap) (r.getColumn(EXTERNAL_ID).data());
774 return entry.getValue().queueId().name().equals(ovsdbMap.map().get(QUEUE_EXTERNAL_ID_KEY));
775 }).findFirst().orElse(null);
776 if (queueRow != null) {
777 newQueues.put(entry.getKey(), queueRow.uuid());
778 }
779 }
780
781 // update the qos table
782 ArrayList<Operation> operations = Lists.newArrayList();
783 Condition condition = ConditionUtil.isEqual(UUID, qosUuid);
784 Mutation mutation = MutationUtil.insert(QUEUES, newQueues);
785 List<Condition> conditions = Collections.singletonList(condition);
786 List<Mutation> mutations = Collections.singletonList(mutation);
787 operations.add(new Mutate(dbSchema.getTableSchema(QOS), conditions, mutations));
788
789 transactConfig(DATABASENAME, operations);
790 }
791
792
793 @SuppressWarnings("unchecked")
794 @Override
795 public void unbindQueues(QosId qosId, List<Long> queueKeys) {
796 DatabaseSchema dbSchema = schema.get(DATABASENAME);
797 OvsdbRowStore qosRowStore = getRowStore(DATABASENAME, QOS);
798 if (qosRowStore == null) {
799 return;
800 }
801
802 ConcurrentMap<String, Row> qosTableRows = qosRowStore.getRowStore();
803
804 Row qosRow = qosTableRows.values().stream().filter(r -> {
805 OvsdbMap ovsdbMap = (OvsdbMap) (r.getColumn(EXTERNAL_ID).data());
806 return qosId.name().equals(ovsdbMap.map().get(QOS_EXTERNAL_ID_KEY));
807 }).findFirst().orElse(null);
808
809 if (qosRow == null) {
810 log.warn("Can't find QoS {}", qosId);
811 return;
812 }
813
kdarapufce5abb2018-05-10 19:37:53 +0530814 Map<Long, Uuid> deleteQueuesMap;
tanbangchengc944c282017-11-12 20:42:59 +0800815 Map<Integer, Uuid> queuesMap = ((OvsdbMap) qosRow.getColumn(QUEUES).data()).map();
816
kdarapufce5abb2018-05-10 19:37:53 +0530817 deleteQueuesMap = queueKeys.stream()
818 .filter(key -> queuesMap.containsKey(key.intValue()))
819 .collect(Collectors.toMap(key -> key, key -> queuesMap.get(key.intValue()), (a, b) -> b));
tanbangchengc944c282017-11-12 20:42:59 +0800820
821 if (deleteQueuesMap.size() != 0) {
822 TableSchema parentTableSchema = dbSchema
823 .getTableSchema(QOS);
824 ColumnSchema parentColumnSchema = parentTableSchema
825 .getColumnSchema(QUEUES);
826
827 Mutation mutation = MutationUtil.delete(parentColumnSchema.name(), OvsdbMap.ovsdbMap(deleteQueuesMap));
828 List<Mutation> mutations = Collections.singletonList(mutation);
829
830 Condition condition = ConditionUtil.isEqual(UUID, qosRow.uuid());
831 List<Condition> conditionList = Collections.singletonList(condition);
832 List<Operation> operations = Collections.singletonList(
833 new Mutate(parentTableSchema, conditionList, mutations));
834
835 transactConfig(DATABASENAME, operations);
836 }
837 }
838
839
840 @Override
Frank Wange11a98d2016-10-26 17:04:03 +0800841 public boolean createQueue(OvsdbQueue ovsdbQueue) {
842 DatabaseSchema dbSchema = schema.get(DATABASENAME);
843 Queue queue = (Queue) TableGenerator.createTable(dbSchema, OvsdbTable.QUEUE);
844 ArrayList<Operation> operations = Lists.newArrayList();
845 OvsdbRowStore rowStore = getRowStore(DATABASENAME, QUEUE);
846 if (rowStore == null) {
847 log.debug("The queue uuid is null");
848 return false;
849 }
850
851 if (ovsdbQueue.dscp().isPresent()) {
852 queue.setDscp(ImmutableSet.of(ovsdbQueue.dscp().get()));
853 }
854 queue.setOtherConfig(ovsdbQueue.otherConfigs());
855 queue.setExternalIds(ovsdbQueue.externalIds());
856 Insert queueInsert = new Insert(dbSchema.getTableSchema(QUEUE), QUEUE, queue.getRow());
857 operations.add(queueInsert);
858
859 try {
860 transactConfig(DATABASENAME, operations).get();
861 } catch (InterruptedException | ExecutionException e) {
862 log.error("createQueue transactConfig get exception !");
863 }
864 return true;
865 }
866
867 @Override
868 public void dropQueue(QueueId queueId) {
869 OvsdbRowStore queueRowStore = getRowStore(DATABASENAME, QUEUE);
870 if (queueRowStore == null) {
871 return;
872 }
873
874 ConcurrentMap<String, Row> queueTableRows = queueRowStore.getRowStore();
875 Row queueRow = queueTableRows.values().stream().filter(r -> {
876 OvsdbMap ovsdbMap = (OvsdbMap) (r.getColumn(EXTERNAL_ID).data());
877 return queueId.name().equals(ovsdbMap.map().get(QUEUE_EXTERNAL_ID_KEY));
878 }).findFirst().orElse(null);
879 if (queueRow == null) {
880 return;
881 }
882
883 String queueUuid = queueRow.uuid().value();
884 OvsdbRowStore qosRowStore = getRowStore(DATABASENAME, QOS);
885 if (qosRowStore != null) {
886 Map<Long, Uuid> queueMap = new HashMap<>();
887 ConcurrentMap<String, Row> qosTableRows = qosRowStore.getRowStore();
888 qosTableRows.values().stream().filter(r -> {
889 Map<Integer, Uuid> ovsdbMap = ((OvsdbMap) r.getColumn(QUEUES).data()).map();
890 Set<Integer> keySet = ovsdbMap.keySet();
891 for (Integer keyId : keySet) {
892 if (ovsdbMap.get(keyId).equals(Uuid.uuid(queueUuid))) {
893 queueMap.put(keyId.longValue(), Uuid.uuid(queueUuid));
894 return true;
895 }
896 }
897 return false;
898 }).findFirst().orElse(null);
899 deleteConfig(QUEUE, UUID, queueUuid, QOS, QUEUES, OvsdbMap.ovsdbMap(queueMap));
900 } else {
901 deleteConfig(QUEUE, UUID, queueUuid, null, null, null);
902 }
903 }
904 @Override
905 public OvsdbQueue getQueue(QueueId queueId) {
906 Set<OvsdbQueue> ovsdbQueues = getQueues();
907 return ovsdbQueues.stream().filter(r ->
908 queueId.name().equals(r.externalIds().get(QUEUE_EXTERNAL_ID_KEY))).
909 findFirst().orElse(null);
910 }
911
912 @Override
913 public Set<OvsdbQueue> getQueues() {
914 Set<OvsdbQueue> ovsdbqueues = new HashSet<>();
915 OvsdbRowStore rowStore = getRowStore(DATABASENAME, QUEUE);
916 if (rowStore == null) {
917 log.debug("The queue uuid is null");
918 return ovsdbqueues;
919 }
920 ConcurrentMap<String, Row> rows = rowStore.getRowStore();
kdarapufce5abb2018-05-10 19:37:53 +0530921 ovsdbqueues = rows.keySet()
922 .stream()
923 .map(uuid -> getRow(DATABASENAME, QUEUE, uuid))
924 .map(this::getOvsdbQueue)
925 .filter(Objects::nonNull)
926 .collect(Collectors.toSet());
Frank Wange11a98d2016-10-26 17:04:03 +0800927 return ovsdbqueues;
928 }
Pier Ventref5d72362016-07-17 12:02:14 +0200929 /**
930 * Creates a mirror port. Mirrors the traffic
931 * that goes to selectDstPort or comes from
932 * selectSrcPort or packets containing selectVlan
933 * to mirrorPort or to all ports that trunk mirrorVlan.
934 *
935 * @param mirror the OVSDB mirror description
936 * @return true if mirror creation is successful, false otherwise
937 */
938 @Override
939 public boolean createMirror(String bridgeName, OvsdbMirror mirror) {
940
941 /**
942 * Retrieves bridge's uuid. It is necessary to update
943 * Bridge table.
944 */
945 String bridgeUuid = getBridgeUuid(bridgeName);
946 if (bridgeUuid == null) {
947 log.warn("Couldn't find bridge {} in {}", bridgeName, nodeId.getIpAddress());
948 return false;
949 }
950
951 OvsdbMirror.Builder mirrorBuilder = OvsdbMirror.builder();
952
953 mirrorBuilder.mirroringName(mirror.mirroringName());
954 mirrorBuilder.selectAll(mirror.selectAll());
955
956 /**
957 * Retrieves the uuid of the monitored dst ports.
958 */
959 mirrorBuilder.monitorDstPorts(mirror.monitorDstPorts().parallelStream()
960 .map(dstPort -> {
961 String dstPortUuid = getPortUuid(dstPort.value(), bridgeUuid);
962 if (dstPortUuid != null) {
963 return Uuid.uuid(dstPortUuid);
964 }
965 log.warn("Couldn't find port {} in {}",
966 dstPort.value(), nodeId.getIpAddress());
967 return null;
968 })
969 .filter(Objects::nonNull)
970 .collect(Collectors.toSet())
971 );
972
973 /**
974 * Retrieves the uuid of the monitored src ports.
975 */
976 mirrorBuilder.monitorSrcPorts(mirror.monitorSrcPorts().parallelStream()
977 .map(srcPort -> {
978 String srcPortUuid = getPortUuid(srcPort.value(), bridgeUuid);
979 if (srcPortUuid != null) {
980 return Uuid.uuid(srcPortUuid);
981 }
982 log.warn("Couldn't find port {} in {}",
983 srcPort.value(), nodeId.getIpAddress());
984 return null;
985 }).filter(Objects::nonNull)
986 .collect(Collectors.toSet())
987 );
988
989 mirrorBuilder.monitorVlans(mirror.monitorVlans());
990 mirrorBuilder.mirrorPort(mirror.mirrorPort());
991 mirrorBuilder.mirrorVlan(mirror.mirrorVlan());
992 mirrorBuilder.externalIds(mirror.externalIds());
993 mirror = mirrorBuilder.build();
994
Jon Hallcbd1b392017-01-18 20:15:44 -0800995 if (mirror.monitorDstPorts().isEmpty() &&
996 mirror.monitorSrcPorts().isEmpty() &&
997 mirror.monitorVlans().isEmpty()) {
Pier Ventref5d72362016-07-17 12:02:14 +0200998 log.warn("Invalid monitoring data");
999 return false;
1000 }
1001
1002 DatabaseSchema dbSchema = schema.get(DATABASENAME);
1003
1004 Mirror mirrorEntry = (Mirror) TableGenerator.createTable(dbSchema, OvsdbTable.MIRROR);
1005 mirrorEntry.setName(mirror.mirroringName());
1006 mirrorEntry.setSelectDstPort(mirror.monitorDstPorts());
1007 mirrorEntry.setSelectSrcPort(mirror.monitorSrcPorts());
1008 mirrorEntry.setSelectVlan(mirror.monitorVlans());
1009 mirrorEntry.setExternalIds(mirror.externalIds());
1010
1011 /**
1012 * If mirror port, retrieves the uuid of the mirror port.
1013 */
1014 if (mirror.mirrorPort() != null) {
1015
1016 String outputPortUuid = getPortUuid(mirror.mirrorPort().value(), bridgeUuid);
1017 if (outputPortUuid == null) {
1018 log.warn("Couldn't find port {} in {}", mirror.mirrorPort().value(), nodeId.getIpAddress());
1019 return false;
1020 }
1021
1022 mirrorEntry.setOutputPort(Uuid.uuid(outputPortUuid));
1023
1024 } else if (mirror.mirrorVlan() != null) {
1025
1026 mirrorEntry.setOutputVlan(mirror.mirrorVlan());
1027
1028 } else {
1029 log.warn("Invalid mirror, no mirror port and no mirror vlan");
1030 return false;
1031 }
1032
1033 ArrayList<Operation> operations = Lists.newArrayList();
1034 Insert mirrorInsert = new Insert(dbSchema.getTableSchema("Mirror"), "Mirror", mirrorEntry.getRow());
1035 operations.add(mirrorInsert);
1036
1037 // update the bridge table
1038 Condition condition = ConditionUtil.isEqual(UUID, Uuid.uuid(bridgeUuid));
1039 Mutation mutation = MutationUtil.insert(MIRRORS, Uuid.uuid("Mirror"));
1040 List<Condition> conditions = Lists.newArrayList(condition);
1041 List<Mutation> mutations = Lists.newArrayList(mutation);
1042 operations.add(new Mutate(dbSchema.getTableSchema("Bridge"), conditions, mutations));
1043
1044 transactConfig(DATABASENAME, operations);
1045 log.info("Created mirror {}", mirror.mirroringName());
1046 return true;
1047 }
1048
1049 /**
1050 * Drops the configuration for mirror.
1051 *
Ray Milkeyef794342016-11-09 16:20:29 -08001052 * @param mirroringName name of mirror to drop
Pier Ventref5d72362016-07-17 12:02:14 +02001053 */
1054 @Override
1055 public void dropMirror(MirroringName mirroringName) {
1056 String mirrorUuid = getMirrorUuid(mirroringName.name());
1057 if (mirrorUuid != null) {
1058 log.info("Deleted mirror {}", mirroringName.name());
Frank Wange11a98d2016-10-26 17:04:03 +08001059 deleteConfig(MIRROR, UUID, mirrorUuid, BRIDGE, MIRRORS, Uuid.uuid(mirrorUuid));
Pier Ventref5d72362016-07-17 12:02:14 +02001060 }
1061 log.warn("Unable to delete {}", mirroringName.name());
1062 return;
1063 }
1064
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001065 @Override
Hyunsun Moondd14e8e2016-06-09 16:17:32 -07001066 public boolean createInterface(String bridgeName, OvsdbInterface ovsdbIface) {
Hyunsun Moon646d8c42015-10-08 20:32:44 -07001067 String bridgeUuid = getBridgeUuid(bridgeName);
1068 if (bridgeUuid == null) {
1069 log.warn("Couldn't find bridge {} in {}", bridgeName, nodeId.getIpAddress());
1070 return false;
1071 }
1072
Hyunsun Moondd14e8e2016-06-09 16:17:32 -07001073 if (getPortUuid(ovsdbIface.name(), bridgeUuid) != null) {
1074 log.warn("Interface {} already exists", ovsdbIface.name());
Hyunsun Moon646d8c42015-10-08 20:32:44 -07001075 // remove existing one and re-create?
1076 return false;
1077 }
1078
1079 ArrayList<Operation> operations = Lists.newArrayList();
Hyunsun Moon1251e192016-06-07 16:57:05 -07001080 DatabaseSchema dbSchema = schema.get(DATABASENAME);
Hyunsun Moon646d8c42015-10-08 20:32:44 -07001081
Hyunsun Moon89478662016-06-09 17:52:34 -07001082 // insert a new port with the interface name
Hyunsun Moon646d8c42015-10-08 20:32:44 -07001083 Port port = (Port) TableGenerator.createTable(dbSchema, OvsdbTable.PORT);
Hyunsun Moondd14e8e2016-06-09 16:17:32 -07001084 port.setName(ovsdbIface.name());
1085 Insert portInsert = new Insert(dbSchema.getTableSchema(PORT), PORT, port.getRow());
1086 portInsert.getRow().put(INTERFACES, Uuid.uuid(INTERFACE));
Hyunsun Moon646d8c42015-10-08 20:32:44 -07001087 operations.add(portInsert);
1088
Hyunsun Moon89478662016-06-09 17:52:34 -07001089 // update the bridge table with the new port
Hyunsun Moon1251e192016-06-07 16:57:05 -07001090 Condition condition = ConditionUtil.isEqual(UUID, Uuid.uuid(bridgeUuid));
Hyunsun Moondd14e8e2016-06-09 16:17:32 -07001091 Mutation mutation = MutationUtil.insert(PORTS, Uuid.uuid(PORT));
1092 List<Condition> conditions = Lists.newArrayList(condition);
1093 List<Mutation> mutations = Lists.newArrayList(mutation);
1094 operations.add(new Mutate(dbSchema.getTableSchema(BRIDGE), conditions, mutations));
Hyunsun Moon646d8c42015-10-08 20:32:44 -07001095
Hyunsun Moon89478662016-06-09 17:52:34 -07001096 // insert an interface
Hyunsun Moon646d8c42015-10-08 20:32:44 -07001097 Interface intf = (Interface) TableGenerator.createTable(dbSchema, OvsdbTable.INTERFACE);
Hyunsun Moondd14e8e2016-06-09 16:17:32 -07001098 intf.setName(ovsdbIface.name());
jaegonkim256f2c12017-05-26 00:03:42 +09001099
Hyunsun Moondd14e8e2016-06-09 16:17:32 -07001100 intf.setType(ovsdbIface.typeToString());
jaegonkim256f2c12017-05-26 00:03:42 +09001101
1102 if (ovsdbIface.mtu().isPresent()) {
1103 Set<Long> mtuSet = Sets.newConcurrentHashSet();
1104 mtuSet.add(ovsdbIface.mtu().get());
1105 intf.setMtu(mtuSet);
1106 intf.setMtuRequest(mtuSet);
1107 }
1108
Hyunsun Moondd14e8e2016-06-09 16:17:32 -07001109 intf.setOptions(ovsdbIface.options());
1110 Insert intfInsert = new Insert(dbSchema.getTableSchema(INTERFACE), INTERFACE, intf.getRow());
Hyunsun Moon646d8c42015-10-08 20:32:44 -07001111 operations.add(intfInsert);
1112
Hyunsun Moon1251e192016-06-07 16:57:05 -07001113 transactConfig(DATABASENAME, operations);
Hyunsun Moon89478662016-06-09 17:52:34 -07001114 log.info("Created interface {}", ovsdbIface);
Hyunsun Moon646d8c42015-10-08 20:32:44 -07001115 return true;
1116 }
1117
1118 @Override
Hyunsun Moondd14e8e2016-06-09 16:17:32 -07001119 public boolean dropInterface(String ifaceName) {
1120 OvsdbRowStore rowStore = getRowStore(DATABASENAME, BRIDGE);
1121 if (rowStore == null) {
1122 log.warn("Failed to get BRIDGE table");
1123 return false;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001124 }
1125
Hyunsun Moondd14e8e2016-06-09 16:17:32 -07001126 ConcurrentMap<String, Row> bridgeTableRows = rowStore.getRowStore();
1127 if (bridgeTableRows == null) {
1128 log.warn("Failed to get BRIDGE table rows");
1129 return false;
1130 }
1131
1132 // interface name is unique
1133 Optional<String> bridgeId = bridgeTableRows.keySet().stream()
1134 .filter(uuid -> getPortUuid(ifaceName, uuid) != null)
1135 .findFirst();
1136
1137 if (bridgeId.isPresent()) {
1138 String portId = getPortUuid(ifaceName, bridgeId.get());
Frank Wange11a98d2016-10-26 17:04:03 +08001139 deleteConfig(PORT, UUID, portId, BRIDGE, PORTS, Uuid.uuid(portId));
Hyunsun Moondd14e8e2016-06-09 16:17:32 -07001140 return true;
1141 } else {
1142 log.warn("Unable to find the interface with name {}", ifaceName);
1143 return false;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001144 }
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001145 }
1146
1147 /**
1148 * Delete transact config.
1149 *
andreaed976a42015-10-05 14:38:25 -07001150 * @param childTableName child table name
1151 * @param childColumnName child column name
1152 * @param childUuid child row uuid
1153 * @param parentTableName parent table name
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001154 * @param parentColumnName parent column
Frank Wange11a98d2016-10-26 17:04:03 +08001155 * @param referencedValue referenced value
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001156 */
1157 private void deleteConfig(String childTableName, String childColumnName,
andreaed976a42015-10-05 14:38:25 -07001158 String childUuid, String parentTableName,
Frank Wange11a98d2016-10-26 17:04:03 +08001159 String parentColumnName, Object referencedValue) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001160 DatabaseSchema dbSchema = schema.get(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001161 TableSchema childTableSchema = dbSchema.getTableSchema(childTableName);
1162
1163 ArrayList<Operation> operations = Lists.newArrayList();
Frank Wange11a98d2016-10-26 17:04:03 +08001164 if (parentTableName != null && parentColumnName != null && referencedValue != null) {
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001165 TableSchema parentTableSchema = dbSchema
1166 .getTableSchema(parentTableName);
1167 ColumnSchema parentColumnSchema = parentTableSchema
1168 .getColumnSchema(parentColumnName);
1169 List<Mutation> mutations = Lists.newArrayList();
Frank Wange11a98d2016-10-26 17:04:03 +08001170 Mutation mutation = MutationUtil.delete(parentColumnSchema.name(), referencedValue);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001171 mutations.add(mutation);
1172 List<Condition> conditions = Lists.newArrayList();
Frank Wange11a98d2016-10-26 17:04:03 +08001173 Condition condition = ConditionUtil.includes(parentColumnName, referencedValue);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001174 conditions.add(condition);
1175 Mutate op = new Mutate(parentTableSchema, conditions, mutations);
1176 operations.add(op);
1177 }
1178
1179 List<Condition> conditions = Lists.newArrayList();
Jonathan Hart51539b82015-10-29 09:53:04 -07001180 Condition condition = ConditionUtil.isEqual(childColumnName, Uuid.uuid(childUuid));
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001181 conditions.add(condition);
1182 Delete del = new Delete(childTableSchema, conditions);
1183 operations.add(del);
Hyunsun Moon1251e192016-06-07 16:57:05 -07001184 transactConfig(DATABASENAME, operations);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001185 }
1186
1187 /**
1188 * Update transact config.
1189 *
andreaed976a42015-10-05 14:38:25 -07001190 * @param tableName table name
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001191 * @param columnName column name
andreaed976a42015-10-05 14:38:25 -07001192 * @param uuid uuid
1193 * @param row the config data
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001194 */
1195 private void updateConfig(String tableName, String columnName, String uuid,
andreaed976a42015-10-05 14:38:25 -07001196 Row row) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001197 DatabaseSchema dbSchema = schema.get(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001198 TableSchema tableSchema = dbSchema.getTableSchema(tableName);
1199
1200 List<Condition> conditions = Lists.newArrayList();
Jonathan Hart51539b82015-10-29 09:53:04 -07001201 Condition condition = ConditionUtil.isEqual(columnName, Uuid.uuid(uuid));
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001202 conditions.add(condition);
1203
1204 Update update = new Update(tableSchema, row, conditions);
1205
1206 ArrayList<Operation> operations = Lists.newArrayList();
1207 operations.add(update);
1208
Hyunsun Moon1251e192016-06-07 16:57:05 -07001209 transactConfig(DATABASENAME, operations);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001210 }
1211
1212 /**
1213 * Insert transact config.
1214 *
andreaed976a42015-10-05 14:38:25 -07001215 * @param childTableName child table name
1216 * @param childColumnName child column name
1217 * @param parentTableName parent table name
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001218 * @param parentColumnName parent column
andreaed976a42015-10-05 14:38:25 -07001219 * @param parentUuid parent uuid
1220 * @param row the config data
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001221 * @return uuid, empty if no uuid is find
1222 */
1223 private String insertConfig(String childTableName, String childColumnName,
andreaed976a42015-10-05 14:38:25 -07001224 String parentTableName, String parentColumnName,
1225 String parentUuid, Row row) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001226 DatabaseSchema dbSchema = schema.get(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001227 TableSchema tableSchema = dbSchema.getTableSchema(childTableName);
1228
Sho SHIMIZUff18f8c2016-03-11 14:43:53 -08001229 Insert insert = new Insert(tableSchema, childTableName, row);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001230
1231 ArrayList<Operation> operations = Lists.newArrayList();
1232 operations.add(insert);
1233
1234 if (parentTableName != null && parentColumnName != null) {
1235 TableSchema parentTableSchema = dbSchema
1236 .getTableSchema(parentTableName);
1237 ColumnSchema parentColumnSchema = parentTableSchema
1238 .getColumnSchema(parentColumnName);
1239
1240 List<Mutation> mutations = Lists.newArrayList();
1241 Mutation mutation = MutationUtil.insert(parentColumnSchema.name(),
Sho SHIMIZUff18f8c2016-03-11 14:43:53 -08001242 Uuid.uuid(childTableName));
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001243 mutations.add(mutation);
1244
1245 List<Condition> conditions = Lists.newArrayList();
Hyunsun Moon1251e192016-06-07 16:57:05 -07001246 Condition condition = ConditionUtil.isEqual(UUID, Uuid.uuid(parentUuid));
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001247 conditions.add(condition);
1248
1249 Mutate op = new Mutate(parentTableSchema, conditions, mutations);
1250 operations.add(op);
1251 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001252 if (childTableName.equalsIgnoreCase(PORT)) {
1253 log.debug("Handle port insert");
Hyunsun Moondd14e8e2016-06-09 16:17:32 -07001254 Insert intfInsert = handlePortInsertTable(row);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001255
1256 if (intfInsert != null) {
1257 operations.add(intfInsert);
1258 }
1259
1260 Insert ins = (Insert) operations.get(0);
Hyunsun Moon1251e192016-06-07 16:57:05 -07001261 ins.getRow().put("interfaces", Uuid.uuid(INTERFACE));
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001262 }
1263
1264 List<OperationResult> results;
1265 try {
jaegonkim01d7c912017-01-22 22:03:38 +09001266 results = transactConfig(DATABASENAME, operations)
1267 .get(TRANSACTCONFIG_TIMEOUT, TimeUnit.SECONDS);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001268 return results.get(0).getUuid().value();
jaegonkim01d7c912017-01-22 22:03:38 +09001269 } catch (TimeoutException e) {
1270 log.warn("TimeoutException thrown while to get result");
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001271 } catch (InterruptedException e) {
1272 log.warn("Interrupted while waiting to get result");
1273 Thread.currentThread().interrupt();
1274 } catch (ExecutionException e) {
1275 log.error("Exception thrown while to get result");
1276 }
1277
1278 return null;
1279 }
1280
jaegonkim01d7c912017-01-22 22:03:38 +09001281
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001282 /**
1283 * Handles port insert.
1284 *
andreaed976a42015-10-05 14:38:25 -07001285 * @param portRow row of port
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001286 * @return insert, empty if null
1287 */
Hyunsun Moondd14e8e2016-06-09 16:17:32 -07001288 private Insert handlePortInsertTable(Row portRow) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001289 DatabaseSchema dbSchema = schema.get(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001290
Hyunsun Moon1251e192016-06-07 16:57:05 -07001291 TableSchema portTableSchema = dbSchema.getTableSchema(PORT);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001292 ColumnSchema portColumnSchema = portTableSchema.getColumnSchema("name");
1293
1294 String portName = (String) portRow.getColumn(portColumnSchema.name()).data();
Hyunsun Moon1251e192016-06-07 16:57:05 -07001295 Interface inf = (Interface) TableGenerator.createTable(dbSchema, OvsdbTable.INTERFACE);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001296 inf.setName(portName);
1297
Hyunsun Moon1251e192016-06-07 16:57:05 -07001298 TableSchema intfTableSchema = dbSchema.getTableSchema(INTERFACE);
1299 return new Insert(intfTableSchema, INTERFACE, inf.getRow());
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001300 }
1301
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001302 @Override
1303 public ListenableFuture<DatabaseSchema> getOvsdbSchema(String dbName) {
1304 if (dbName == null) {
1305 return null;
1306 }
1307 DatabaseSchema databaseSchema = schema.get(dbName);
1308 if (databaseSchema == null) {
kdarapufce5abb2018-05-10 19:37:53 +05301309 List<String> dbNames = new ArrayList<>();
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001310 dbNames.add(dbName);
Hyunsun Moon1251e192016-06-07 16:57:05 -07001311 Function<JsonNode, DatabaseSchema> rowFunction = input -> {
1312 log.debug("Get ovsdb database schema {}", dbName);
1313 DatabaseSchema dbSchema = FromJsonUtil.jsonNodeToDbSchema(dbName, input);
1314 if (dbSchema == null) {
1315 log.debug("Get ovsdb database schema error");
1316 return null;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001317 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001318 schema.put(dbName, dbSchema);
1319 return dbSchema;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001320 };
1321
1322 ListenableFuture<JsonNode> input = getSchema(dbNames);
1323 if (input != null) {
1324 return Futures.transform(input, rowFunction);
1325 }
1326 return null;
1327 } else {
1328 return Futures.immediateFuture(databaseSchema);
1329 }
1330 }
1331
1332 @Override
1333 public ListenableFuture<TableUpdates> monitorTables(String dbName, String id) {
1334 if (dbName == null) {
1335 return null;
1336 }
1337 DatabaseSchema dbSchema = schema.get(dbName);
1338 if (dbSchema != null) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001339 Function<JsonNode, TableUpdates> rowFunction = input -> {
1340 log.debug("Get table updates");
1341 TableUpdates updates = FromJsonUtil.jsonNodeToTableUpdates(input, dbSchema);
1342 if (updates == null) {
1343 log.debug("Get table updates error");
1344 return null;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001345 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001346 return updates;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001347 };
1348 return Futures.transform(monitor(dbSchema, id), rowFunction);
1349 }
1350 return null;
1351 }
1352
Hyunsun Moondd14e8e2016-06-09 16:17:32 -07001353 private ListenableFuture<List<OperationResult>> transactConfig(String dbName,
1354 List<Operation> operations) {
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001355 if (dbName == null) {
1356 return null;
1357 }
1358 DatabaseSchema dbSchema = schema.get(dbName);
1359 if (dbSchema != null) {
andreaed976a42015-10-05 14:38:25 -07001360 Function<List<JsonNode>, List<OperationResult>> rowFunction = (input -> {
Jonathan Hart6523e122017-11-14 09:23:49 -08001361 try {
1362 log.debug("Get ovsdb operation result");
1363 List<OperationResult> result = FromJsonUtil.jsonNodeToOperationResult(input, operations);
1364 if (result == null) {
1365 log.debug("The operation result is null");
1366 return null;
1367 }
1368 return result;
1369 } catch (Exception e) {
1370 log.error("Exception while parsing result", e);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001371 }
Jonathan Hart6523e122017-11-14 09:23:49 -08001372 return null;
andreaed976a42015-10-05 14:38:25 -07001373 });
Hyunsun Moon1251e192016-06-07 16:57:05 -07001374 return Futures.transform(transact(dbSchema, operations), rowFunction);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001375 }
1376 return null;
1377 }
1378
1379 @Override
1380 public ListenableFuture<JsonNode> getSchema(List<String> dbnames) {
1381 String id = java.util.UUID.randomUUID().toString();
1382 String getSchemaString = JsonRpcWriterUtil.getSchemaStr(id, dbnames);
1383
1384 SettableFuture<JsonNode> sf = SettableFuture.create();
1385 requestResult.put(id, sf);
1386 requestMethod.put(id, "getSchema");
1387
1388 channel.writeAndFlush(getSchemaString);
1389 return sf;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001390 }
1391
1392 @Override
1393 public ListenableFuture<List<String>> echo() {
1394 String id = java.util.UUID.randomUUID().toString();
1395 String echoString = JsonRpcWriterUtil.echoStr(id);
1396
1397 SettableFuture<List<String>> sf = SettableFuture.create();
1398 requestResult.put(id, sf);
1399 requestMethod.put(id, "echo");
1400
1401 channel.writeAndFlush(echoString);
1402 return sf;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001403 }
1404
1405 @Override
1406 public ListenableFuture<JsonNode> monitor(DatabaseSchema dbSchema,
1407 String monitorId) {
1408 String id = java.util.UUID.randomUUID().toString();
1409 String monitorString = JsonRpcWriterUtil.monitorStr(id, monitorId,
1410 dbSchema);
1411
1412 SettableFuture<JsonNode> sf = SettableFuture.create();
1413 requestResult.put(id, sf);
1414 requestMethod.put(id, "monitor");
1415
1416 channel.writeAndFlush(monitorString);
1417 return sf;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001418 }
1419
1420 @Override
1421 public ListenableFuture<List<String>> listDbs() {
1422 String id = java.util.UUID.randomUUID().toString();
1423 String listDbsString = JsonRpcWriterUtil.listDbsStr(id);
1424
1425 SettableFuture<List<String>> sf = SettableFuture.create();
1426 requestResult.put(id, sf);
1427 requestMethod.put(id, "listDbs");
1428
1429 channel.writeAndFlush(listDbsString);
1430 return sf;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001431 }
1432
1433 @Override
1434 public ListenableFuture<List<JsonNode>> transact(DatabaseSchema dbSchema,
1435 List<Operation> operations) {
1436 String id = java.util.UUID.randomUUID().toString();
1437 String transactString = JsonRpcWriterUtil.transactStr(id, dbSchema,
1438 operations);
1439
1440 SettableFuture<List<JsonNode>> sf = SettableFuture.create();
1441 requestResult.put(id, sf);
1442 requestMethod.put(id, "transact");
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001443 channel.writeAndFlush(transactString);
1444 return sf;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001445 }
1446
andreaed976a42015-10-05 14:38:25 -07001447 @SuppressWarnings({"rawtypes", "unchecked"})
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001448 @Override
1449 public void processResult(JsonNode response) {
1450 log.debug("Handle result");
1451 String requestId = response.get("id").asText();
1452 SettableFuture sf = requestResult.get(requestId);
1453 if (sf == null) {
1454 log.debug("No such future to process");
1455 return;
1456 }
1457 String methodName = requestMethod.get(requestId);
Hyunsun Moon1251e192016-06-07 16:57:05 -07001458 sf.set(FromJsonUtil.jsonResultParser(response, methodName));
tanbangcheng1afecce2017-11-12 11:41:28 +08001459
1460 requestResult.remove(requestId);
1461 requestMethod.remove(requestId);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001462 }
1463
1464 @Override
1465 public void processRequest(JsonNode requestJson) {
1466 log.debug("Handle request");
1467 if (requestJson.get("method").asText().equalsIgnoreCase("echo")) {
1468 log.debug("handle echo request");
1469
1470 String replyString = FromJsonUtil.getEchoRequestStr(requestJson);
1471 channel.writeAndFlush(replyString);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001472 } else {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001473 FromJsonUtil.jsonCallbackRequestParser(requestJson, monitorCallBack);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001474 }
1475 }
1476
1477 @Override
1478 public void setCallback(Callback monitorCallback) {
1479 this.monitorCallBack = monitorCallback;
1480 }
1481
1482 @Override
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001483 public Set<OvsdbBridge> getBridges() {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001484 Set<OvsdbBridge> ovsdbBridges = new HashSet<>();
1485 OvsdbTableStore tableStore = getTableStore(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001486 if (tableStore == null) {
MaoJianweidac220d2016-07-04 22:37:52 +08001487 return ovsdbBridges;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001488 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001489 OvsdbRowStore rowStore = tableStore.getRows(BRIDGE);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001490 if (rowStore == null) {
MaoJianweidac220d2016-07-04 22:37:52 +08001491 return ovsdbBridges;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001492 }
1493 ConcurrentMap<String, Row> rows = rowStore.getRowStore();
1494 for (String uuid : rows.keySet()) {
jaegonkim7b77f712018-01-14 17:18:27 +09001495 Row bridgeRow = getRow(DATABASENAME, BRIDGE, uuid);
1496 OvsdbBridge ovsdbBridge = getOvsdbBridge(bridgeRow, Uuid.uuid(uuid));
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001497 if (ovsdbBridge != null) {
1498 ovsdbBridges.add(ovsdbBridge);
1499 }
1500 }
1501 return ovsdbBridges;
1502 }
1503
1504 @Override
andreaed976a42015-10-05 14:38:25 -07001505 public Set<ControllerInfo> getControllers(DeviceId openflowDeviceId) {
Jonathan Hart51539b82015-10-29 09:53:04 -07001506 Uuid bridgeUuid = getBridgeUuid(openflowDeviceId);
andreaed976a42015-10-05 14:38:25 -07001507 if (bridgeUuid == null) {
1508 log.warn("bad bridge Uuid");
1509 return null;
1510 }
1511 List<Controller> controllers = getControllers(bridgeUuid);
1512 if (controllers == null) {
1513 log.warn("bad list of controllers");
1514 return null;
1515 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001516 return controllers.stream().map(controller -> new ControllerInfo(
1517 (String) controller.getTargetColumn()
1518 .data())).collect(Collectors.toSet());
andreaed976a42015-10-05 14:38:25 -07001519 }
1520
Jonathan Hart51539b82015-10-29 09:53:04 -07001521 private List<Controller> getControllers(Uuid bridgeUuid) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001522 DatabaseSchema dbSchema = schema.get(DATABASENAME);
andreaed976a42015-10-05 14:38:25 -07001523 if (dbSchema == null) {
1524 return null;
1525 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001526 OvsdbRowStore rowStore = getRowStore(DATABASENAME, BRIDGE);
andreaed976a42015-10-05 14:38:25 -07001527 if (rowStore == null) {
1528 log.debug("There is no bridge table");
1529 return null;
1530 }
1531
1532 Row bridgeRow = rowStore.getRow(bridgeUuid.value());
1533 Bridge bridge = (Bridge) TableGenerator.
1534 getTable(dbSchema, bridgeRow, OvsdbTable.BRIDGE);
1535
1536 //FIXME remove log
1537 log.warn("type of controller column", bridge.getControllerColumn()
1538 .data().getClass());
Jonathan Hart51539b82015-10-29 09:53:04 -07001539 Set<Uuid> controllerUuids = (Set<Uuid>) ((OvsdbSet) bridge
andreaed976a42015-10-05 14:38:25 -07001540 .getControllerColumn().data()).set();
andreaed976a42015-10-05 14:38:25 -07001541
Hyunsun Moon1251e192016-06-07 16:57:05 -07001542 OvsdbRowStore controllerRowStore = getRowStore(DATABASENAME, CONTROLLER);
andreaed976a42015-10-05 14:38:25 -07001543 if (controllerRowStore == null) {
1544 log.debug("There is no controller table");
1545 return null;
1546 }
1547
1548 List<Controller> ovsdbControllers = new ArrayList<>();
1549 ConcurrentMap<String, Row> controllerTableRows = controllerRowStore.getRowStore();
1550 controllerTableRows.forEach((key, row) -> {
Jonathan Hart51539b82015-10-29 09:53:04 -07001551 if (!controllerUuids.contains(Uuid.uuid(key))) {
andreaed976a42015-10-05 14:38:25 -07001552 return;
1553 }
1554 Controller controller = (Controller) TableGenerator
1555 .getTable(dbSchema, row, OvsdbTable.CONTROLLER);
1556 ovsdbControllers.add(controller);
1557 });
1558 return ovsdbControllers;
1559 }
1560
1561
Jonathan Hart51539b82015-10-29 09:53:04 -07001562 private Uuid getBridgeUuid(DeviceId openflowDeviceId) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001563 DatabaseSchema dbSchema = schema.get(DATABASENAME);
andreaed976a42015-10-05 14:38:25 -07001564 if (dbSchema == null) {
1565 return null;
1566 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001567 OvsdbRowStore rowStore = getRowStore(DATABASENAME, BRIDGE);
andreaed976a42015-10-05 14:38:25 -07001568 if (rowStore == null) {
1569 log.debug("There is no bridge table");
1570 return null;
1571 }
1572
1573 ConcurrentMap<String, Row> bridgeTableRows = rowStore.getRowStore();
Jonathan Hart51539b82015-10-29 09:53:04 -07001574 final AtomicReference<Uuid> uuid = new AtomicReference<>();
andreaed976a42015-10-05 14:38:25 -07001575 for (Map.Entry<String, Row> entry : bridgeTableRows.entrySet()) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001576 Bridge bridge = (Bridge) TableGenerator.getTable(
1577 dbSchema,
1578 entry.getValue(),
1579 OvsdbTable.BRIDGE);
1580
1581 if (matchesDpid(bridge, openflowDeviceId)) {
Jonathan Hart51539b82015-10-29 09:53:04 -07001582 uuid.set(Uuid.uuid(entry.getKey()));
andreaed976a42015-10-05 14:38:25 -07001583 break;
1584 }
1585 }
1586 if (uuid.get() == null) {
1587 log.debug("There is no bridge for {}", openflowDeviceId);
1588 }
1589 return uuid.get();
andreaed976a42015-10-05 14:38:25 -07001590 }
1591
1592 private static boolean matchesDpid(Bridge b, DeviceId deviceId) {
1593 String ofDpid = deviceId.toString().replace("of:", "");
1594 Set ofDeviceIds = ((OvsdbSet) b.getDatapathIdColumn().data()).set();
1595 //TODO Set<String>
1596 return ofDeviceIds.contains(ofDpid);
1597 }
1598
1599 @Override
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001600 public Set<OvsdbPort> getPorts() {
kdarapufce5abb2018-05-10 19:37:53 +05301601 Set<OvsdbPort> ovsdbPorts;
Hyunsun Moon1251e192016-06-07 16:57:05 -07001602 OvsdbTableStore tableStore = getTableStore(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001603 if (tableStore == null) {
1604 return null;
1605 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001606 OvsdbRowStore rowStore = tableStore.getRows(INTERFACE);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001607 if (rowStore == null) {
1608 return null;
1609 }
1610 ConcurrentMap<String, Row> rows = rowStore.getRowStore();
kdarapufce5abb2018-05-10 19:37:53 +05301611 ovsdbPorts = rows.keySet()
1612 .stream()
1613 .map(uuid -> getRow(DATABASENAME, INTERFACE, uuid))
1614 .map(this::getOvsdbPort)
1615 .filter(Objects::nonNull)
1616 .collect(Collectors.toSet());
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001617 return ovsdbPorts;
1618 }
1619
1620 @Override
1621 public DatabaseSchema getDatabaseSchema(String dbName) {
1622 return schema.get(dbName);
1623 }
1624
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001625 private OvsdbPort getOvsdbPort(Row row) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001626 DatabaseSchema dbSchema = getDatabaseSchema(DATABASENAME);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001627 Interface intf = (Interface) TableGenerator
1628 .getTable(dbSchema, row, OvsdbTable.INTERFACE);
1629 if (intf == null) {
1630 return null;
1631 }
1632 long ofPort = getOfPort(intf);
1633 String portName = intf.getName();
1634 if ((ofPort < 0) || (portName == null)) {
1635 return null;
1636 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001637 return new OvsdbPort(new OvsdbPortNumber(ofPort), new OvsdbPortName(portName));
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001638 }
1639
jaegonkim7b77f712018-01-14 17:18:27 +09001640 private OvsdbBridge getOvsdbBridge(Row row, Uuid bridgeUuid) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001641 DatabaseSchema dbSchema = getDatabaseSchema(DATABASENAME);
1642 Bridge bridge = (Bridge) TableGenerator.getTable(dbSchema, row, OvsdbTable.BRIDGE);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001643 if (bridge == null) {
1644 return null;
1645 }
1646
1647 OvsdbSet datapathIdSet = (OvsdbSet) bridge.getDatapathIdColumn().data();
1648 @SuppressWarnings("unchecked")
1649 Set<String> datapathIds = datapathIdSet.set();
Jon Hallcbd1b392017-01-18 20:15:44 -08001650 if (datapathIds == null || datapathIds.isEmpty()) {
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001651 return null;
1652 }
1653 String datapathId = (String) datapathIds.toArray()[0];
1654 String bridgeName = bridge.getName();
1655 if ((datapathId == null) || (bridgeName == null)) {
1656 return null;
1657 }
jaegonkim7b77f712018-01-14 17:18:27 +09001658
1659 List<Controller> controllers = getControllers(bridgeUuid);
1660
1661 if (controllers != null) {
1662 List<ControllerInfo> controllerInfos = controllers.stream().map(
1663 controller -> new ControllerInfo(
1664 (String) controller.getTargetColumn()
1665 .data())).collect(Collectors.toList());
1666
1667 return OvsdbBridge.builder()
1668 .name(bridgeName)
1669 .datapathId(datapathId)
1670 .controllers(controllerInfos)
1671 .build();
1672 } else {
1673 return OvsdbBridge.builder()
1674 .name(bridgeName)
1675 .datapathId(datapathId)
1676 .build();
1677 }
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001678 }
1679
Frank Wange11a98d2016-10-26 17:04:03 +08001680 private OvsdbQos getOvsdbQos(Row row) {
1681 DatabaseSchema dbSchema = getDatabaseSchema(DATABASENAME);
1682 Qos qos = (Qos) TableGenerator.getTable(dbSchema, row, OvsdbTable.QOS);
1683 if (qos == null) {
1684 return null;
1685 }
1686
1687 String type = (String) qos.getTypeColumn().data();
1688 Map<String, String> otherConfigs;
1689 Map<String, String> externalIds;
1690 Map<Long, String> queues;
1691
1692 otherConfigs = ((OvsdbMap) qos.getOtherConfigColumn().data()).map();
1693 externalIds = ((OvsdbMap) qos.getExternalIdsColumn().data()).map();
1694 queues = ((OvsdbMap) qos.getQueuesColumn().data()).map();
1695 return OvsdbQos.builder().qosType(type).
1696 queues(queues).otherConfigs(otherConfigs).
1697 externalIds(externalIds).build();
1698 }
1699
1700 private OvsdbQueue getOvsdbQueue(Row row) {
1701 DatabaseSchema dbSchema = getDatabaseSchema(DATABASENAME);
1702 Queue queue = (Queue) TableGenerator.getTable(dbSchema, row, OvsdbTable.QUEUE);
1703 if (queue == null) {
1704 return null;
1705 }
1706
1707 OvsdbSet dscpOvsdbSet = ((OvsdbSet) queue.getDscpColumn().data());
1708 @SuppressWarnings("unchecked")
1709 Set<String> dscpSet = dscpOvsdbSet.set();
1710 Long dscp = null;
1711 if (dscpSet != null && !dscpSet.isEmpty()) {
1712 dscp = Long.valueOf((String) dscpSet.toArray()[0]);
1713 }
1714
1715 Map<String, String> otherConfigs;
1716 Map<String, String> externalIds;
1717
1718 otherConfigs = ((OvsdbMap) queue.getOtherConfigColumn().data()).map();
1719 externalIds = ((OvsdbMap) queue.getExternalIdsColumn().data()).map();
1720 return OvsdbQueue.builder().dscp(dscp).
1721 otherConfigs(otherConfigs).externalIds(externalIds).build();
1722 }
1723
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001724 private long getOfPort(Interface intf) {
1725 OvsdbSet ofPortSet = (OvsdbSet) intf.getOpenFlowPortColumn().data();
1726 @SuppressWarnings("unchecked")
1727 Set<Integer> ofPorts = ofPortSet.set();
Jon Hallcbd1b392017-01-18 20:15:44 -08001728 if (ofPorts == null || ofPorts.isEmpty()) {
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001729 log.debug("The ofport is null in {}", intf.getName());
1730 return -1;
1731 }
1732 // return (long) ofPorts.toArray()[0];
1733 Iterator<Integer> it = ofPorts.iterator();
1734 return Long.parseLong(it.next().toString());
1735 }
CNluciusa66c3972015-09-06 20:31:29 +08001736
1737 @Override
1738 public Set<OvsdbPort> getLocalPorts(Iterable<String> ifaceids) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001739 Set<OvsdbPort> ovsdbPorts = new HashSet<>();
1740 OvsdbTableStore tableStore = getTableStore(DATABASENAME);
CNluciusa66c3972015-09-06 20:31:29 +08001741 if (tableStore == null) {
1742 return null;
1743 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001744 OvsdbRowStore rowStore = tableStore.getRows(INTERFACE);
CNluciusa66c3972015-09-06 20:31:29 +08001745 if (rowStore == null) {
1746 return null;
1747 }
1748 ConcurrentMap<String, Row> rows = rowStore.getRowStore();
1749 for (String uuid : rows.keySet()) {
Hyunsun Moon1251e192016-06-07 16:57:05 -07001750 Row row = getRow(DATABASENAME, INTERFACE, uuid);
1751 DatabaseSchema dbSchema = getDatabaseSchema(DATABASENAME);
CNluciusa66c3972015-09-06 20:31:29 +08001752 Interface intf = (Interface) TableGenerator
1753 .getTable(dbSchema, row, OvsdbTable.INTERFACE);
1754 if (intf == null || getIfaceid(intf) == null) {
1755 continue;
1756 }
1757 String portName = intf.getName();
Hyunsun Moon1251e192016-06-07 16:57:05 -07001758 if (portName == null) {
1759 continue;
1760 }
CNluciusa66c3972015-09-06 20:31:29 +08001761 Set<String> ifaceidSet = Sets.newHashSet(ifaceids);
Hyunsun Moon1251e192016-06-07 16:57:05 -07001762 if (portName.startsWith(TYPEVXLAN) || !ifaceidSet.contains(getIfaceid(intf))) {
CNluciusa66c3972015-09-06 20:31:29 +08001763 continue;
1764 }
1765 long ofPort = getOfPort(intf);
Hyunsun Moon1251e192016-06-07 16:57:05 -07001766 if (ofPort < 0) {
CNluciusa66c3972015-09-06 20:31:29 +08001767 continue;
1768 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001769 ovsdbPorts.add(new OvsdbPort(new OvsdbPortNumber(ofPort),
1770 new OvsdbPortName(portName)));
CNluciusa66c3972015-09-06 20:31:29 +08001771 }
1772 return ovsdbPorts;
1773 }
1774
1775 private String getIfaceid(Interface intf) {
1776 OvsdbMap ovsdbMap = (OvsdbMap) intf.getExternalIdsColumn().data();
1777 @SuppressWarnings("unchecked")
1778 Map<String, String> externalIds = ovsdbMap.map();
1779 if (externalIds.isEmpty()) {
1780 log.warn("The external_ids is null");
1781 return null;
1782 }
Hyunsun Moon1251e192016-06-07 16:57:05 -07001783 String ifaceid = externalIds.get(EXTERNAL_ID_INTERFACE_ID);
CNluciusa66c3972015-09-06 20:31:29 +08001784 if (ifaceid == null) {
1785 log.warn("The ifaceid is null");
1786 return null;
1787 }
1788 return ifaceid;
1789 }
Hyunsun Moon5fb20a52015-09-25 17:02:33 -07001790
1791 @Override
1792 public void disconnect() {
1793 channel.disconnect();
Hyunsun Moon5fb20a52015-09-25 17:02:33 -07001794 }
Saritha1583a6f2017-06-16 14:42:58 +05301795
1796 @Override
1797 public List<OvsdbPortName> getPorts(List<String> portNames, DeviceId deviceId) {
1798 Uuid bridgeUuid = getBridgeUuid(deviceId);
1799 if (bridgeUuid == null) {
1800 log.error("Can't find the bridge for the deviceId {}", deviceId);
1801 return Collections.emptyList();
1802 }
1803 DatabaseSchema dbSchema = schema.get(DATABASENAME);
1804 Row bridgeRow = getRow(DATABASENAME, BRIDGE, bridgeUuid.value());
1805 Bridge bridge = (Bridge) TableGenerator.getTable(dbSchema, bridgeRow, OvsdbTable.BRIDGE);
1806 if (bridge == null) {
1807 return Collections.emptyList();
1808 }
1809 OvsdbSet setPorts = (OvsdbSet) bridge.getPortsColumn().data();
1810 Set<Uuid> portSet = setPorts.set();
1811 if (portSet.isEmpty()) {
1812 return Collections.emptyList();
1813 }
1814
1815 Map<Uuid, Port> portMap = portSet.stream().collect(Collectors.toMap(
1816 java.util.function.Function.identity(), port -> (Port) TableGenerator
1817 .getTable(dbSchema, getRow(DATABASENAME,
1818 PORT, port.value()), OvsdbTable.PORT)));
1819
1820 List<OvsdbPortName> portList = portMap.entrySet().stream().filter(port -> Objects.nonNull(port.getValue())
1821 && portNames.contains(port.getValue().getName())
1822 && Objects.nonNull(getInterfacebyPort(port.getKey().value(), port.getValue().getName())))
1823 .map(port -> new OvsdbPortName(port.getValue().getName())).collect(Collectors.toList());
1824
1825 return Collections.unmodifiableList(portList);
1826 }
1827
1828 @Override
1829 public boolean getPortError(List<OvsdbPortName> portNames, DeviceId bridgeId) {
1830 Uuid bridgeUuid = getBridgeUuid(bridgeId);
1831
1832 List<Interface> interfaceList = portNames.stream().collect(Collectors
1833 .toMap(java.util.function.Function.identity(),
1834 port -> (Interface) getInterfacebyPort(getPortUuid(port.value(),
1835 bridgeUuid.value()), port.value())))
1836 .entrySet().stream().filter(intf -> Objects.nonNull(intf.getValue())
1837 && ((OvsdbSet) intf.getValue().getOpenFlowPortColumn().data()).set()
1838 .stream().findAny().orElse(OFPORT_ERROR_COMPARISON).equals(OFPORT_ERROR))
kdarapufce5abb2018-05-10 19:37:53 +05301839 .map(Map.Entry::getValue).collect(Collectors.toList());
Saritha1583a6f2017-06-16 14:42:58 +05301840
kdarapufce5abb2018-05-10 19:37:53 +05301841 interfaceList.forEach(intf -> ((Consumer<Interface>) intf1 -> {
1842 try {
1843 Set<String> setErrors = ((OvsdbSet) intf1.getErrorColumn().data()).set();
1844 log.info("Port has errors. ofport value - {}, Interface - {} has error - {} ",
1845 intf1.getOpenFlowPortColumn().data(), intf1.getName(), setErrors.stream()
1846 .findFirst().get());
1847 } catch (ColumnSchemaNotFoundException | VersionMismatchException e) {
1848 log.debug("Port has errors. ofport value - {}, Interface - {} has error - {} ",
1849 intf1.getOpenFlowPortColumn().data(), intf1.getName(), e);
Saritha1583a6f2017-06-16 14:42:58 +05301850 }
kdarapufce5abb2018-05-10 19:37:53 +05301851 }).accept(intf));
Saritha1583a6f2017-06-16 14:42:58 +05301852
1853 return !interfaceList.isEmpty();
1854 }
1855
1856 private Interface getInterfacebyPort(String portUuid, String portName) {
1857 DatabaseSchema dbSchema = schema.get(DATABASENAME);
1858
1859 Row portRow = getRow(DATABASENAME, PORT, portUuid);
1860 Port port = (Port) TableGenerator.getTable(dbSchema, portRow,
1861 OvsdbTable.PORT);
1862 if (port == null) {
1863 return null;
1864 }
1865
1866 OvsdbSet setInterfaces = (OvsdbSet) port.getInterfacesColumn().data();
1867 Set<Uuid> interfaces = setInterfaces.set();
1868
1869 return interfaces.stream().map(intf -> (Interface) TableGenerator
1870 .getTable(dbSchema, getRow(DATABASENAME,
1871 INTERFACE, intf.value()), OvsdbTable.INTERFACE))
1872 .filter(intf -> Objects.nonNull(intf) && portName.equalsIgnoreCase(intf.getName()))
1873 .findFirst().orElse(null);
1874 }
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001875}