Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 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 | */ |
| 16 | package org.onosproject.ovsdb.controller.driver; |
| 17 | |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.google.common.base.Function; |
Brian O'Connor | 6ee8aa3 | 2015-10-09 16:07:42 -0700 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableList; |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Lists; |
| 22 | import com.google.common.collect.Maps; |
| 23 | import com.google.common.collect.Sets; |
| 24 | import com.google.common.util.concurrent.Futures; |
| 25 | import com.google.common.util.concurrent.ListenableFuture; |
| 26 | import com.google.common.util.concurrent.SettableFuture; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 27 | |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 28 | import io.netty.channel.Channel; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 29 | |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 30 | import org.onlab.packet.IpAddress; |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 31 | import org.onosproject.net.DeviceId; |
| 32 | import org.onosproject.net.behaviour.ControllerInfo; |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 33 | import org.onosproject.ovsdb.controller.OvsdbBridge; |
| 34 | import org.onosproject.ovsdb.controller.OvsdbBridgeName; |
| 35 | import org.onosproject.ovsdb.controller.OvsdbClientService; |
| 36 | import org.onosproject.ovsdb.controller.OvsdbConstant; |
| 37 | import org.onosproject.ovsdb.controller.OvsdbDatapathId; |
| 38 | import org.onosproject.ovsdb.controller.OvsdbNodeId; |
| 39 | import org.onosproject.ovsdb.controller.OvsdbPort; |
| 40 | import org.onosproject.ovsdb.controller.OvsdbPortName; |
| 41 | import org.onosproject.ovsdb.controller.OvsdbPortNumber; |
| 42 | import org.onosproject.ovsdb.controller.OvsdbRowStore; |
| 43 | import org.onosproject.ovsdb.controller.OvsdbStore; |
| 44 | import org.onosproject.ovsdb.controller.OvsdbTableStore; |
| 45 | import org.onosproject.ovsdb.controller.OvsdbTunnel; |
Satish K | ebe203d | 2015-11-21 15:58:18 +0530 | [diff] [blame] | 46 | import org.onosproject.ovsdb.rfc.exception.BridgeCreateException; |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 47 | import org.onosproject.ovsdb.rfc.jsonrpc.Callback; |
| 48 | import org.onosproject.ovsdb.rfc.message.OperationResult; |
| 49 | import org.onosproject.ovsdb.rfc.message.TableUpdates; |
| 50 | import org.onosproject.ovsdb.rfc.notation.Condition; |
| 51 | import org.onosproject.ovsdb.rfc.notation.Mutation; |
CNlucius | a66c397 | 2015-09-06 20:31:29 +0800 | [diff] [blame] | 52 | import org.onosproject.ovsdb.rfc.notation.OvsdbMap; |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 53 | import org.onosproject.ovsdb.rfc.notation.OvsdbSet; |
| 54 | import org.onosproject.ovsdb.rfc.notation.Row; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 55 | import org.onosproject.ovsdb.rfc.notation.Uuid; |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 56 | import org.onosproject.ovsdb.rfc.operations.Delete; |
| 57 | import org.onosproject.ovsdb.rfc.operations.Insert; |
| 58 | import org.onosproject.ovsdb.rfc.operations.Mutate; |
| 59 | import org.onosproject.ovsdb.rfc.operations.Operation; |
| 60 | import org.onosproject.ovsdb.rfc.operations.Update; |
| 61 | import org.onosproject.ovsdb.rfc.schema.ColumnSchema; |
| 62 | import org.onosproject.ovsdb.rfc.schema.DatabaseSchema; |
| 63 | import org.onosproject.ovsdb.rfc.schema.TableSchema; |
| 64 | import org.onosproject.ovsdb.rfc.table.Bridge; |
| 65 | import org.onosproject.ovsdb.rfc.table.Controller; |
| 66 | import org.onosproject.ovsdb.rfc.table.Interface; |
| 67 | import org.onosproject.ovsdb.rfc.table.OvsdbTable; |
| 68 | import org.onosproject.ovsdb.rfc.table.Port; |
| 69 | import org.onosproject.ovsdb.rfc.table.TableGenerator; |
| 70 | import org.onosproject.ovsdb.rfc.utils.ConditionUtil; |
| 71 | import org.onosproject.ovsdb.rfc.utils.FromJsonUtil; |
| 72 | import org.onosproject.ovsdb.rfc.utils.JsonRpcWriterUtil; |
| 73 | import org.onosproject.ovsdb.rfc.utils.MutationUtil; |
| 74 | import org.slf4j.Logger; |
| 75 | import org.slf4j.LoggerFactory; |
| 76 | |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 77 | import java.net.InetSocketAddress; |
| 78 | import java.util.ArrayList; |
Hyunsun Moon | 646d8c4 | 2015-10-08 20:32:44 -0700 | [diff] [blame] | 79 | import java.util.Arrays; |
| 80 | import java.util.HashMap; |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 81 | import java.util.HashSet; |
| 82 | import java.util.Iterator; |
| 83 | import java.util.List; |
| 84 | import java.util.Map; |
| 85 | import java.util.Set; |
| 86 | import java.util.concurrent.ConcurrentMap; |
| 87 | import java.util.concurrent.ExecutionException; |
| 88 | import java.util.concurrent.atomic.AtomicReference; |
| 89 | import java.util.stream.Collectors; |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 90 | |
| 91 | /** |
| 92 | * An representation of an ovsdb client. |
| 93 | */ |
| 94 | public class DefaultOvsdbClient |
| 95 | implements OvsdbProviderService, OvsdbClientService { |
| 96 | |
| 97 | private final Logger log = LoggerFactory |
| 98 | .getLogger(DefaultOvsdbClient.class); |
| 99 | |
| 100 | private Channel channel; |
| 101 | |
| 102 | private OvsdbAgent agent; |
| 103 | private boolean connected; |
| 104 | private OvsdbNodeId nodeId; |
| 105 | private Callback monitorCallBack; |
| 106 | |
| 107 | private OvsdbStore ovsdbStore = new OvsdbStore(); |
| 108 | |
| 109 | private final Map<String, String> requestMethod = Maps.newHashMap(); |
| 110 | private final Map<String, SettableFuture<? extends Object>> requestResult = Maps |
| 111 | .newHashMap(); |
| 112 | |
| 113 | private final Map<String, DatabaseSchema> schema = Maps.newHashMap(); |
| 114 | private final Set<OvsdbTunnel> ovsdbTunnels = new HashSet<OvsdbTunnel>(); |
| 115 | |
| 116 | /** |
| 117 | * Creates an OvsdbClient. |
| 118 | * |
| 119 | * @param nodeId ovsdb node id |
| 120 | */ |
| 121 | public DefaultOvsdbClient(OvsdbNodeId nodeId) { |
| 122 | this.nodeId = nodeId; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public OvsdbNodeId nodeId() { |
| 127 | return nodeId; |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void setAgent(OvsdbAgent agent) { |
| 132 | if (this.agent == null) { |
| 133 | this.agent = agent; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void setChannel(Channel channel) { |
| 139 | this.channel = channel; |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public void setConnection(boolean connected) { |
| 144 | this.connected = connected; |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public boolean isConnected() { |
| 149 | return this.connected; |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public void nodeAdded() { |
| 154 | this.agent.addConnectedNode(nodeId, this); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public void nodeRemoved() { |
| 159 | this.agent.removeConnectedNode(nodeId); |
| 160 | channel.disconnect(); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Gets the ovsdb table store. |
| 165 | * |
| 166 | * @param dbName the ovsdb database name |
| 167 | * @return ovsTableStore, empty if table store is find |
| 168 | */ |
| 169 | private OvsdbTableStore getTableStore(String dbName) { |
| 170 | if (ovsdbStore == null) { |
| 171 | return null; |
| 172 | } |
| 173 | return ovsdbStore.getOvsdbTableStore(dbName); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Gets the ovsdb row store. |
| 178 | * |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 179 | * @param dbName the ovsdb database name |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 180 | * @param tableName the ovsdb table name |
Hyunsun Moon | 6125c61 | 2015-10-15 10:54:44 -0700 | [diff] [blame] | 181 | * @return ovsRowStore, empty store if no rows exist in the table |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 182 | */ |
| 183 | private OvsdbRowStore getRowStore(String dbName, String tableName) { |
| 184 | OvsdbTableStore tableStore = getTableStore(dbName); |
| 185 | if (tableStore == null) { |
| 186 | return null; |
| 187 | } |
Hyunsun Moon | 6125c61 | 2015-10-15 10:54:44 -0700 | [diff] [blame] | 188 | |
| 189 | OvsdbRowStore rowStore = tableStore.getRows(tableName); |
| 190 | if (rowStore == null) { |
| 191 | rowStore = new OvsdbRowStore(); |
| 192 | } |
| 193 | return rowStore; |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Gets the ovsdb row. |
| 198 | * |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 199 | * @param dbName the ovsdb database name |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 200 | * @param tableName the ovsdb table name |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 201 | * @param uuid the key of the row |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 202 | * @return row, empty if row is find |
| 203 | */ |
| 204 | @Override |
| 205 | public Row getRow(String dbName, String tableName, String uuid) { |
| 206 | OvsdbTableStore tableStore = getTableStore(dbName); |
| 207 | if (tableStore == null) { |
| 208 | return null; |
| 209 | } |
| 210 | OvsdbRowStore rowStore = tableStore.getRows(tableName); |
| 211 | if (rowStore == null) { |
| 212 | return null; |
| 213 | } |
| 214 | return rowStore.getRow(uuid); |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public void removeRow(String dbName, String tableName, String uuid) { |
| 219 | OvsdbTableStore tableStore = getTableStore(dbName); |
| 220 | if (tableStore == null) { |
| 221 | return; |
| 222 | } |
| 223 | OvsdbRowStore rowStore = tableStore.getRows(tableName); |
| 224 | if (rowStore == null) { |
| 225 | return; |
| 226 | } |
| 227 | rowStore.deleteRow(uuid); |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public void updateOvsdbStore(String dbName, String tableName, String uuid, |
| 232 | Row row) { |
| 233 | OvsdbTableStore tableStore = ovsdbStore.getOvsdbTableStore(dbName); |
| 234 | if (tableStore == null) { |
| 235 | tableStore = new OvsdbTableStore(); |
| 236 | } |
| 237 | OvsdbRowStore rowStore = tableStore.getRows(tableName); |
| 238 | if (rowStore == null) { |
| 239 | rowStore = new OvsdbRowStore(); |
| 240 | } |
| 241 | rowStore.insertRow(uuid, row); |
| 242 | tableStore.createOrUpdateTable(tableName, rowStore); |
| 243 | ovsdbStore.createOrUpdateOvsdbStore(dbName, tableStore); |
| 244 | } |
| 245 | |
| 246 | @Override |
| 247 | public String getPortUuid(String portName, String bridgeUuid) { |
| 248 | DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME); |
| 249 | |
| 250 | Row bridgeRow = getRow(OvsdbConstant.DATABASENAME, |
| 251 | OvsdbConstant.BRIDGE, bridgeUuid); |
| 252 | |
| 253 | Bridge bridge = (Bridge) TableGenerator.getTable(dbSchema, bridgeRow, |
| 254 | OvsdbTable.BRIDGE); |
| 255 | if (bridge != null) { |
| 256 | OvsdbSet setPorts = (OvsdbSet) bridge.getPortsColumn().data(); |
| 257 | @SuppressWarnings("unchecked") |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 258 | Set<Uuid> ports = setPorts.set(); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 259 | if (ports == null || ports.size() == 0) { |
| 260 | log.warn("The port uuid is null"); |
| 261 | return null; |
| 262 | } |
| 263 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 264 | for (Uuid uuid : ports) { |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 265 | Row portRow = getRow(OvsdbConstant.DATABASENAME, |
| 266 | OvsdbConstant.PORT, uuid.value()); |
| 267 | Port port = (Port) TableGenerator.getTable(dbSchema, portRow, |
| 268 | OvsdbTable.PORT); |
| 269 | if (port != null && portName.equalsIgnoreCase(port.getName())) { |
| 270 | return uuid.value(); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | } |
| 275 | return null; |
| 276 | } |
| 277 | |
| 278 | @Override |
| 279 | public String getInterfaceUuid(String portUuid, String portName) { |
| 280 | DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME); |
| 281 | |
| 282 | Row portRow = getRow(OvsdbConstant.DATABASENAME, OvsdbConstant.PORT, |
| 283 | portUuid); |
| 284 | Port port = (Port) TableGenerator.getTable(dbSchema, portRow, |
| 285 | OvsdbTable.PORT); |
| 286 | |
| 287 | if (port != null) { |
| 288 | OvsdbSet setInterfaces = (OvsdbSet) port.getInterfacesColumn().data(); |
| 289 | @SuppressWarnings("unchecked") |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 290 | Set<Uuid> interfaces = setInterfaces.set(); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 291 | |
| 292 | if (interfaces == null || interfaces.size() == 0) { |
| 293 | log.warn("The interface uuid is null"); |
| 294 | return null; |
| 295 | } |
| 296 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 297 | for (Uuid uuid : interfaces) { |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 298 | Row intfRow = getRow(OvsdbConstant.DATABASENAME, |
| 299 | OvsdbConstant.INTERFACE, uuid.value()); |
| 300 | Interface intf = (Interface) TableGenerator |
| 301 | .getTable(dbSchema, intfRow, OvsdbTable.INTERFACE); |
| 302 | if (intf != null && portName.equalsIgnoreCase(intf.getName())) { |
| 303 | return uuid.value(); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | } |
| 308 | |
| 309 | return null; |
| 310 | } |
| 311 | |
| 312 | @Override |
| 313 | public String getBridgeUuid(String bridgeName) { |
| 314 | DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME); |
| 315 | |
| 316 | OvsdbRowStore rowStore = getRowStore(OvsdbConstant.DATABASENAME, |
| 317 | OvsdbConstant.BRIDGE); |
| 318 | if (rowStore == null) { |
| 319 | log.debug("The bridge uuid is null"); |
| 320 | return null; |
| 321 | } |
| 322 | |
| 323 | ConcurrentMap<String, Row> bridgeTableRows = rowStore.getRowStore(); |
| 324 | if (bridgeTableRows == null) { |
| 325 | log.debug("The bridge uuid is null"); |
| 326 | return null; |
| 327 | } |
| 328 | |
| 329 | for (String uuid : bridgeTableRows.keySet()) { |
| 330 | Bridge bridge = (Bridge) TableGenerator |
| 331 | .getTable(dbSchema, bridgeTableRows.get(uuid), |
| 332 | OvsdbTable.BRIDGE); |
| 333 | |
| 334 | if (bridge.getName().equals(bridgeName)) { |
| 335 | return uuid; |
| 336 | } |
| 337 | |
| 338 | } |
| 339 | return null; |
| 340 | } |
| 341 | |
| 342 | @Override |
| 343 | public String getControllerUuid(String controllerName, |
| 344 | String controllerTarget) { |
| 345 | DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME); |
| 346 | OvsdbRowStore rowStore = getRowStore(OvsdbConstant.DATABASENAME, |
| 347 | OvsdbConstant.CONTROLLER); |
| 348 | if (rowStore == null) { |
| 349 | log.debug("The controller uuid is null"); |
| 350 | return null; |
| 351 | } |
| 352 | |
| 353 | ConcurrentMap<String, Row> controllerTableRows = rowStore.getRowStore(); |
| 354 | if (controllerTableRows != null) { |
| 355 | for (String uuid : controllerTableRows.keySet()) { |
| 356 | |
| 357 | Controller controller = (Controller) TableGenerator |
| 358 | .getTable(dbSchema, controllerTableRows.get(uuid), |
| 359 | OvsdbTable.CONTROLLER); |
| 360 | String target = (String) controller.getTargetColumn().data(); |
| 361 | if (target.equalsIgnoreCase(controllerTarget)) { |
| 362 | return uuid; |
| 363 | } |
| 364 | |
| 365 | } |
| 366 | } |
| 367 | return null; |
| 368 | } |
| 369 | |
| 370 | @Override |
| 371 | public String getOvsUuid(String dbName) { |
| 372 | OvsdbRowStore rowStore = getRowStore(OvsdbConstant.DATABASENAME, |
| 373 | OvsdbConstant.DATABASENAME); |
| 374 | if (rowStore == null) { |
| 375 | log.debug("The bridge uuid is null"); |
| 376 | return null; |
| 377 | } |
| 378 | ConcurrentMap<String, Row> ovsTableRows = rowStore.getRowStore(); |
| 379 | if (ovsTableRows != null) { |
| 380 | for (String uuid : ovsTableRows.keySet()) { |
| 381 | Row row = ovsTableRows.get(uuid); |
| 382 | String tableName = row.tableName(); |
| 383 | if (tableName.equals(dbName)) { |
| 384 | return uuid; |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | return null; |
| 389 | } |
| 390 | |
| 391 | @Override |
| 392 | public void createPort(String bridgeName, String portName) { |
| 393 | String bridgeUuid = getBridgeUuid(bridgeName); |
| 394 | if (bridgeUuid == null) { |
| 395 | log.error("Can't find bridge {} in {}", bridgeName, |
| 396 | nodeId.getIpAddress()); |
| 397 | return; |
| 398 | } |
| 399 | |
| 400 | DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME); |
| 401 | String portUuid = getPortUuid(portName, bridgeUuid); |
| 402 | |
| 403 | Port port = (Port) TableGenerator |
| 404 | .createTable(dbSchema, OvsdbTable.PORT); |
| 405 | |
| 406 | port.setName(portName); |
| 407 | if (portUuid == null) { |
| 408 | insertConfig(OvsdbConstant.PORT, "_uuid", OvsdbConstant.BRIDGE, |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 409 | "ports", bridgeUuid, port.getRow()); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 410 | } else { |
| 411 | updateConfig(OvsdbConstant.PORT, "_uuid", portUuid, port.getRow()); |
| 412 | } |
| 413 | |
| 414 | return; |
| 415 | } |
| 416 | |
| 417 | @Override |
| 418 | public void dropPort(String bridgeName, String portName) { |
| 419 | String bridgeUuid = getBridgeUuid(bridgeName); |
| 420 | if (bridgeUuid == null) { |
| 421 | log.error("Could not find Bridge {} in {}", bridgeName, nodeId); |
| 422 | return; |
| 423 | } |
| 424 | |
| 425 | String portUuid = getPortUuid(portName, bridgeUuid); |
| 426 | if (portUuid != null) { |
| 427 | log.info("Port {} delete", portName); |
| 428 | deleteConfig(OvsdbConstant.PORT, "_uuid", portUuid, |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 429 | OvsdbConstant.BRIDGE, "ports"); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
| 433 | @Override |
| 434 | public void createBridge(String bridgeName) { |
| 435 | log.debug("create bridge {}", bridgeName); |
| 436 | |
| 437 | DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME); |
| 438 | if (dbSchema == null) { |
| 439 | log.warn("The schema is null"); |
| 440 | return; |
| 441 | } |
| 442 | |
| 443 | Bridge bridge = (Bridge) TableGenerator.createTable(dbSchema, |
| 444 | OvsdbTable.BRIDGE); |
| 445 | if (bridge == null) { |
| 446 | log.debug("Can not create bridge"); |
| 447 | return; |
| 448 | } |
| 449 | |
| 450 | Set<String> failModes = new HashSet<>(); |
| 451 | failModes.add("secure"); |
| 452 | bridge.setFailMode(failModes); |
| 453 | |
| 454 | Set<String> protocols = new HashSet<>(); |
| 455 | protocols.add(OvsdbConstant.OPENFLOW13); |
| 456 | bridge.setProtocols(protocols); |
| 457 | |
| 458 | String ovsUuid = getOvsUuid(OvsdbConstant.DATABASENAME); |
| 459 | if (ovsUuid == null) { |
| 460 | log.warn("The Open_vSwitch is null"); |
| 461 | return; |
| 462 | } |
| 463 | |
| 464 | String bridgeUuid = getBridgeUuid(bridgeName); |
| 465 | if (bridgeUuid == null) { |
| 466 | log.debug("Create a new bridge"); |
| 467 | |
| 468 | bridge.setName(bridgeName); |
| 469 | bridgeUuid = insertConfig(OvsdbConstant.BRIDGE, "_uuid", |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 470 | OvsdbConstant.DATABASENAME, "bridges", |
| 471 | ovsUuid, bridge.getRow()); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 472 | |
| 473 | if (bridgeUuid != null) { |
| 474 | Port port = (Port) TableGenerator.createTable(dbSchema, |
| 475 | OvsdbTable.PORT); |
| 476 | if (port != null) { |
| 477 | log.debug("the port is not null"); |
| 478 | port.setName(bridgeName); |
| 479 | |
| 480 | insertConfig(OvsdbConstant.PORT, "_uuid", "Bridge", "ports", bridgeUuid, |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 481 | port.getRow()); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 482 | } |
Satish K | ebe203d | 2015-11-21 15:58:18 +0530 | [diff] [blame] | 483 | } else { |
| 484 | String message = BridgeCreateException.createMessage(ovsUuid); |
| 485 | throw new BridgeCreateException(message); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | } else { |
| 489 | log.info("Update a bridge"); |
| 490 | updateConfig(OvsdbConstant.BRIDGE, "_uuid", bridgeUuid, bridge.getRow()); |
| 491 | } |
| 492 | |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 493 | setControllerAuto(bridgeUuid); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 494 | log.info("Create bridge success"); |
| 495 | } |
| 496 | |
Hyunsun Moon | 646d8c4 | 2015-10-08 20:32:44 -0700 | [diff] [blame] | 497 | @Override |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 498 | public void createBridge(String bridgeName, String dpid, String exPortName) { |
| 499 | log.debug("create bridge {}", bridgeName); |
| 500 | |
| 501 | DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME); |
| 502 | if (dbSchema == null) { |
| 503 | log.warn("The schema is null"); |
| 504 | return; |
| 505 | } |
| 506 | |
| 507 | Bridge bridge = (Bridge) TableGenerator.createTable(dbSchema, |
| 508 | OvsdbTable.BRIDGE); |
| 509 | if (bridge == null) { |
| 510 | log.debug("Can not create bridge"); |
| 511 | return; |
| 512 | } |
| 513 | |
| 514 | Set<String> failModes = new HashSet<>(); |
| 515 | failModes.add("secure"); |
| 516 | bridge.setFailMode(failModes); |
| 517 | |
| 518 | Set<String> protocols = new HashSet<>(); |
| 519 | protocols.add(OvsdbConstant.OPENFLOW13); |
| 520 | bridge.setProtocols(protocols); |
| 521 | |
| 522 | String ovsUuid = getOvsUuid(OvsdbConstant.DATABASENAME); |
| 523 | if (ovsUuid == null) { |
| 524 | log.warn("The Open_vSwitch is null"); |
| 525 | return; |
| 526 | } |
| 527 | |
| 528 | String bridgeUuid = getBridgeUuid(bridgeName); |
| 529 | if (bridgeUuid == null) { |
| 530 | log.debug("Create a new bridge"); |
| 531 | |
| 532 | bridge.setName(bridgeName); |
| 533 | if (dpid != null) { |
| 534 | Map<String, String> options = new HashMap<>(); |
| 535 | options.put("datapath-id", dpid); |
| 536 | bridge.setOtherConfig(options); |
| 537 | } |
| 538 | bridgeUuid = insertConfig(OvsdbConstant.BRIDGE, "_uuid", |
| 539 | OvsdbConstant.DATABASENAME, "bridges", |
| 540 | ovsUuid, bridge.getRow()); |
| 541 | |
| 542 | if (bridgeUuid != null) { |
| 543 | Port port = (Port) TableGenerator.createTable(dbSchema, |
| 544 | OvsdbTable.PORT); |
| 545 | if (port != null) { |
| 546 | log.debug("the port is not null"); |
| 547 | port.setName(bridgeName); |
| 548 | |
| 549 | insertConfig(OvsdbConstant.PORT, "_uuid", "Bridge", "ports", bridgeUuid, |
| 550 | port.getRow()); |
| 551 | } |
Satish K | ebe203d | 2015-11-21 15:58:18 +0530 | [diff] [blame] | 552 | } else { |
| 553 | String message = BridgeCreateException.createMessage(ovsUuid); |
| 554 | throw new BridgeCreateException(message); |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | } else { |
| 558 | log.info("Update a bridge"); |
| 559 | updateConfig(OvsdbConstant.BRIDGE, "_uuid", bridgeUuid, bridge.getRow()); |
| 560 | } |
| 561 | // Create external port |
| 562 | if (exPortName != null) { |
| 563 | createPort(bridgeName, exPortName); |
| 564 | } |
| 565 | |
| 566 | setControllerAuto(bridgeUuid); |
| 567 | log.info("Create bridge success"); |
| 568 | } |
| 569 | |
| 570 | @Override |
Hyunsun Moon | 646d8c4 | 2015-10-08 20:32:44 -0700 | [diff] [blame] | 571 | public boolean createBridge(String bridgeName, String dpid, List<ControllerInfo> controllers) { |
| 572 | |
| 573 | DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME); |
| 574 | String ovsUuid = getOvsUuid(OvsdbConstant.DATABASENAME); |
| 575 | |
| 576 | if (dbSchema == null || ovsUuid == null) { |
| 577 | log.warn("Couldn't find database Open_vSwitch"); |
| 578 | return false; |
| 579 | } |
| 580 | |
Hyunsun Moon | 646d8c4 | 2015-10-08 20:32:44 -0700 | [diff] [blame] | 581 | Bridge bridge = (Bridge) TableGenerator.createTable(dbSchema, OvsdbTable.BRIDGE); |
Hyunsun Moon | 646d8c4 | 2015-10-08 20:32:44 -0700 | [diff] [blame] | 582 | Set<String> failMode = new HashSet<>(Arrays.asList("secure")); |
| 583 | bridge.setFailMode(failMode); |
| 584 | |
| 585 | Set<String> protocols = new HashSet<>(Arrays.asList(OvsdbConstant.OPENFLOW13)); |
| 586 | bridge.setProtocols(protocols); |
| 587 | |
| 588 | Map<String, String> options = new HashMap<>(); |
| 589 | options.put("datapath-id", dpid); |
| 590 | bridge.setOtherConfig(options); |
| 591 | |
Hyunsun Moon | 9802554 | 2016-03-08 04:36:02 -0800 | [diff] [blame] | 592 | String bridgeUuid = getBridgeUuid(bridgeName); |
| 593 | if (bridgeUuid == null) { |
| 594 | bridge.setName(bridgeName); |
| 595 | bridgeUuid = insertConfig(OvsdbConstant.BRIDGE, "_uuid", |
| 596 | OvsdbConstant.DATABASENAME, "bridges", |
| 597 | ovsUuid, bridge.getRow()); |
| 598 | } else { |
| 599 | updateConfig(OvsdbConstant.BRIDGE, "_uuid", bridgeUuid, bridge.getRow()); |
| 600 | } |
Hyunsun Moon | 646d8c4 | 2015-10-08 20:32:44 -0700 | [diff] [blame] | 601 | |
| 602 | if (bridgeUuid != null) { |
| 603 | createPort(bridgeName, bridgeName); |
| 604 | } else { |
| 605 | log.warn("Failed to create bridge {} on {}", bridgeName, nodeId.toString()); |
| 606 | return false; |
| 607 | } |
| 608 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 609 | setControllersWithUuid(Uuid.uuid(bridgeUuid), controllers); |
Hyunsun Moon | 646d8c4 | 2015-10-08 20:32:44 -0700 | [diff] [blame] | 610 | return true; |
| 611 | } |
| 612 | |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 613 | /** |
Brian O'Connor | 6ee8aa3 | 2015-10-09 16:07:42 -0700 | [diff] [blame] | 614 | * Sets the bridge's controller automatically. |
| 615 | * <p/> |
| 616 | * The connection is a TCP connection to the local ONOS instance's IP |
| 617 | * and the default OpenFlow port. |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 618 | * |
| 619 | * @param bridgeUuid bridge uuid |
| 620 | */ |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 621 | private void setControllerAuto(String bridgeUuid) { |
Brian O'Connor | 6ee8aa3 | 2015-10-09 16:07:42 -0700 | [diff] [blame] | 622 | IpAddress ipAddress = IpAddress.valueOf(((InetSocketAddress) channel.localAddress()).getAddress()); |
| 623 | ControllerInfo controllerInfo = new ControllerInfo(ipAddress, OvsdbConstant.OFPORT, "tcp"); |
| 624 | log.debug("Automatically setting controller for bridge {} to {}", |
| 625 | bridgeUuid, controllerInfo.target()); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 626 | setControllersWithUuid(Uuid.uuid(bridgeUuid), ImmutableList.of(controllerInfo)); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 627 | } |
| 628 | |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 629 | @Override |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 630 | public void setControllersWithUuid(Uuid bridgeUuid, List<ControllerInfo> controllers) { |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 631 | |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 632 | DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 633 | if (dbSchema == null) { |
| 634 | log.debug("There is no schema"); |
| 635 | return; |
| 636 | } |
| 637 | List<Controller> oldControllers = getControllers(bridgeUuid); |
| 638 | if (oldControllers == null) { |
| 639 | log.warn("There are no controllers"); |
| 640 | return; |
| 641 | } |
| 642 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 643 | Set<Uuid> newControllerUuids = new HashSet<>(); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 644 | |
| 645 | Set<ControllerInfo> newControllers = new HashSet<>(controllers); |
| 646 | List<Controller> removeControllers = new ArrayList<>(); |
| 647 | oldControllers.forEach(controller -> { |
| 648 | ControllerInfo controllerInfo = new ControllerInfo((String) controller.getTargetColumn().data()); |
| 649 | if (newControllers.contains(controllerInfo)) { |
| 650 | newControllers.remove(controllerInfo); |
| 651 | newControllerUuids.add(controller.getRow().uuid()); |
| 652 | } else { |
| 653 | removeControllers.add(controller); |
| 654 | } |
| 655 | }); |
| 656 | OvsdbRowStore controllerRowStore = getRowStore(OvsdbConstant.DATABASENAME, |
| 657 | OvsdbConstant.CONTROLLER); |
| 658 | if (controllerRowStore == null) { |
| 659 | log.debug("There is no controller table"); |
| 660 | return; |
| 661 | } |
| 662 | |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 663 | removeControllers.forEach(c -> deleteConfig(OvsdbConstant.CONTROLLER, "_uuid", c.getRow().uuid().value(), |
| 664 | OvsdbConstant.BRIDGE, "controller")); |
| 665 | |
| 666 | newControllers.stream().map(c -> { |
| 667 | Controller controller = (Controller) TableGenerator |
| 668 | .createTable(dbSchema, OvsdbTable.CONTROLLER); |
| 669 | controller.setTarget(c.target()); |
| 670 | return controller; |
| 671 | }).forEach(c -> { |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 672 | String uuid = insertConfig(OvsdbConstant.CONTROLLER, "_uuid", |
| 673 | OvsdbConstant.BRIDGE, "controller", bridgeUuid.value(), |
| 674 | c.getRow()); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 675 | newControllerUuids.add(Uuid.uuid(uuid)); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 676 | |
| 677 | }); |
| 678 | |
| 679 | OvsdbRowStore rowStore = getRowStore(OvsdbConstant.DATABASENAME, |
| 680 | OvsdbConstant.BRIDGE); |
| 681 | if (rowStore == null) { |
| 682 | log.debug("There is no bridge table"); |
| 683 | return; |
| 684 | } |
| 685 | |
| 686 | Row bridgeRow = rowStore.getRow(bridgeUuid.value()); |
| 687 | Bridge bridge = (Bridge) TableGenerator.getTable(dbSchema, bridgeRow, OvsdbTable.BRIDGE); |
| 688 | bridge.setController(OvsdbSet.ovsdbSet(newControllerUuids)); |
| 689 | updateConfig(OvsdbConstant.BRIDGE, "_uuid", bridgeUuid.value(), bridge.getRow()); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 690 | } |
| 691 | |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 692 | @Override |
| 693 | public void setControllersWithDeviceId(DeviceId deviceId, List<ControllerInfo> controllers) { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 694 | setControllersWithUuid(getBridgeUuid(deviceId), controllers); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 695 | } |
| 696 | |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 697 | @Override |
| 698 | public void dropBridge(String bridgeName) { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 699 | String bridgeUuid = getBridgeUuid(bridgeName); |
| 700 | if (bridgeUuid == null) { |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 701 | log.warn("Could not find bridge in node", nodeId.getIpAddress()); |
| 702 | return; |
| 703 | } |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 704 | deleteConfig(OvsdbConstant.BRIDGE, "_uuid", bridgeUuid, |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 705 | OvsdbConstant.DATABASENAME, "bridges"); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | @Override |
Hyunsun Moon | 646d8c4 | 2015-10-08 20:32:44 -0700 | [diff] [blame] | 709 | public boolean createTunnel(String bridgeName, String portName, String tunnelType, Map<String, String> options) { |
| 710 | |
| 711 | String bridgeUuid = getBridgeUuid(bridgeName); |
| 712 | if (bridgeUuid == null) { |
| 713 | log.warn("Couldn't find bridge {} in {}", bridgeName, nodeId.getIpAddress()); |
| 714 | return false; |
| 715 | } |
| 716 | |
| 717 | if (getPortUuid(portName, bridgeUuid) != null) { |
| 718 | log.warn("Port {} already exists", portName); |
| 719 | // remove existing one and re-create? |
| 720 | return false; |
| 721 | } |
| 722 | |
| 723 | ArrayList<Operation> operations = Lists.newArrayList(); |
| 724 | DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME); |
| 725 | |
| 726 | // insert a new port to the port table |
| 727 | Port port = (Port) TableGenerator.createTable(dbSchema, OvsdbTable.PORT); |
| 728 | port.setName(portName); |
| 729 | Insert portInsert = new Insert(dbSchema.getTableSchema("Port"), "Port", port.getRow()); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 730 | portInsert.getRow().put("interfaces", Uuid.uuid("Interface")); |
Hyunsun Moon | 646d8c4 | 2015-10-08 20:32:44 -0700 | [diff] [blame] | 731 | operations.add(portInsert); |
| 732 | |
| 733 | // update the bridge table |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 734 | Condition condition = ConditionUtil.isEqual("_uuid", Uuid.uuid(bridgeUuid)); |
| 735 | Mutation mutation = MutationUtil.insert("ports", Uuid.uuid("Port")); |
Hyunsun Moon | 646d8c4 | 2015-10-08 20:32:44 -0700 | [diff] [blame] | 736 | List<Condition> conditions = new ArrayList<>(Arrays.asList(condition)); |
| 737 | List<Mutation> mutations = new ArrayList<>(Arrays.asList(mutation)); |
| 738 | operations.add(new Mutate(dbSchema.getTableSchema("Bridge"), conditions, mutations)); |
| 739 | |
| 740 | // insert a tunnel interface |
| 741 | Interface intf = (Interface) TableGenerator.createTable(dbSchema, OvsdbTable.INTERFACE); |
| 742 | intf.setName(portName); |
| 743 | intf.setType(tunnelType); |
| 744 | intf.setOptions(options); |
| 745 | Insert intfInsert = new Insert(dbSchema.getTableSchema("Interface"), "Interface", intf.getRow()); |
| 746 | operations.add(intfInsert); |
| 747 | |
| 748 | transactConfig(OvsdbConstant.DATABASENAME, operations); |
| 749 | return true; |
| 750 | } |
| 751 | |
| 752 | @Override |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 753 | public void dropTunnel(IpAddress srcIp, IpAddress dstIp) { |
| 754 | String bridgeName = OvsdbConstant.INTEGRATION_BRIDGE; |
| 755 | String portName = getTunnelName(OvsdbConstant.TYPEVXLAN, dstIp); |
| 756 | String bridgeUuid = getBridgeUuid(OvsdbConstant.INTEGRATION_BRIDGE); |
| 757 | if (bridgeUuid == null) { |
| 758 | log.warn("Could not find bridge {} in {}", bridgeName, |
| 759 | nodeId.getIpAddress()); |
| 760 | return; |
| 761 | } |
| 762 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 763 | String portUuid = getPortUuid(portName, bridgeUuid); |
| 764 | if (portUuid != null) { |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 765 | log.info("Delete tunnel"); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 766 | deleteConfig(OvsdbConstant.PORT, "_uuid", portUuid, |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 767 | OvsdbConstant.BRIDGE, "ports"); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | return; |
| 771 | } |
| 772 | |
| 773 | /** |
| 774 | * Delete transact config. |
| 775 | * |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 776 | * @param childTableName child table name |
| 777 | * @param childColumnName child column name |
| 778 | * @param childUuid child row uuid |
| 779 | * @param parentTableName parent table name |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 780 | * @param parentColumnName parent column |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 781 | */ |
| 782 | private void deleteConfig(String childTableName, String childColumnName, |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 783 | String childUuid, String parentTableName, |
| 784 | String parentColumnName) { |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 785 | DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME); |
| 786 | TableSchema childTableSchema = dbSchema.getTableSchema(childTableName); |
| 787 | |
| 788 | ArrayList<Operation> operations = Lists.newArrayList(); |
| 789 | if (parentTableName != null && parentColumnName != null) { |
| 790 | TableSchema parentTableSchema = dbSchema |
| 791 | .getTableSchema(parentTableName); |
| 792 | ColumnSchema parentColumnSchema = parentTableSchema |
| 793 | .getColumnSchema(parentColumnName); |
| 794 | List<Mutation> mutations = Lists.newArrayList(); |
| 795 | Mutation mutation = MutationUtil.delete(parentColumnSchema.name(), |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 796 | Uuid.uuid(childUuid)); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 797 | mutations.add(mutation); |
| 798 | List<Condition> conditions = Lists.newArrayList(); |
| 799 | Condition condition = ConditionUtil.includes(parentColumnName, |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 800 | Uuid.uuid(childUuid)); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 801 | conditions.add(condition); |
| 802 | Mutate op = new Mutate(parentTableSchema, conditions, mutations); |
| 803 | operations.add(op); |
| 804 | } |
| 805 | |
| 806 | List<Condition> conditions = Lists.newArrayList(); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 807 | Condition condition = ConditionUtil.isEqual(childColumnName, Uuid.uuid(childUuid)); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 808 | conditions.add(condition); |
| 809 | Delete del = new Delete(childTableSchema, conditions); |
| 810 | operations.add(del); |
| 811 | transactConfig(OvsdbConstant.DATABASENAME, operations); |
| 812 | |
| 813 | return; |
| 814 | } |
| 815 | |
| 816 | /** |
| 817 | * Update transact config. |
| 818 | * |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 819 | * @param tableName table name |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 820 | * @param columnName column name |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 821 | * @param uuid uuid |
| 822 | * @param row the config data |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 823 | */ |
| 824 | private void updateConfig(String tableName, String columnName, String uuid, |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 825 | Row row) { |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 826 | DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME); |
| 827 | TableSchema tableSchema = dbSchema.getTableSchema(tableName); |
| 828 | |
| 829 | List<Condition> conditions = Lists.newArrayList(); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 830 | Condition condition = ConditionUtil.isEqual(columnName, Uuid.uuid(uuid)); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 831 | conditions.add(condition); |
| 832 | |
| 833 | Update update = new Update(tableSchema, row, conditions); |
| 834 | |
| 835 | ArrayList<Operation> operations = Lists.newArrayList(); |
| 836 | operations.add(update); |
| 837 | |
| 838 | transactConfig(OvsdbConstant.DATABASENAME, operations); |
| 839 | } |
| 840 | |
| 841 | /** |
| 842 | * Insert transact config. |
| 843 | * |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 844 | * @param childTableName child table name |
| 845 | * @param childColumnName child column name |
| 846 | * @param parentTableName parent table name |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 847 | * @param parentColumnName parent column |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 848 | * @param parentUuid parent uuid |
| 849 | * @param row the config data |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 850 | * @return uuid, empty if no uuid is find |
| 851 | */ |
| 852 | private String insertConfig(String childTableName, String childColumnName, |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 853 | String parentTableName, String parentColumnName, |
| 854 | String parentUuid, Row row) { |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 855 | DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME); |
| 856 | TableSchema tableSchema = dbSchema.getTableSchema(childTableName); |
| 857 | |
Sho SHIMIZU | ff18f8c | 2016-03-11 14:43:53 -0800 | [diff] [blame] | 858 | Insert insert = new Insert(tableSchema, childTableName, row); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 859 | |
| 860 | ArrayList<Operation> operations = Lists.newArrayList(); |
| 861 | operations.add(insert); |
| 862 | |
| 863 | if (parentTableName != null && parentColumnName != null) { |
| 864 | TableSchema parentTableSchema = dbSchema |
| 865 | .getTableSchema(parentTableName); |
| 866 | ColumnSchema parentColumnSchema = parentTableSchema |
| 867 | .getColumnSchema(parentColumnName); |
| 868 | |
| 869 | List<Mutation> mutations = Lists.newArrayList(); |
| 870 | Mutation mutation = MutationUtil.insert(parentColumnSchema.name(), |
Sho SHIMIZU | ff18f8c | 2016-03-11 14:43:53 -0800 | [diff] [blame] | 871 | Uuid.uuid(childTableName)); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 872 | mutations.add(mutation); |
| 873 | |
| 874 | List<Condition> conditions = Lists.newArrayList(); |
Ray Milkey | 676249c | 2015-12-18 09:27:03 -0800 | [diff] [blame] | 875 | Condition condition = ConditionUtil.isEqual("_uuid", |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 876 | Uuid.uuid(parentUuid)); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 877 | conditions.add(condition); |
| 878 | |
| 879 | Mutate op = new Mutate(parentTableSchema, conditions, mutations); |
| 880 | operations.add(op); |
| 881 | } |
| 882 | if (childTableName.equalsIgnoreCase(OvsdbConstant.PORT)) { |
| 883 | log.info("Handle port insert"); |
| 884 | Insert intfInsert = handlePortInsertTable(OvsdbConstant.INTERFACE, |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 885 | row); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 886 | |
| 887 | if (intfInsert != null) { |
| 888 | operations.add(intfInsert); |
| 889 | } |
| 890 | |
| 891 | Insert ins = (Insert) operations.get(0); |
| 892 | ins.getRow().put("interfaces", |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 893 | Uuid.uuid(OvsdbConstant.INTERFACE)); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | List<OperationResult> results; |
| 897 | try { |
| 898 | results = transactConfig(OvsdbConstant.DATABASENAME, operations) |
| 899 | .get(); |
| 900 | |
| 901 | return results.get(0).getUuid().value(); |
| 902 | } catch (InterruptedException e) { |
| 903 | log.warn("Interrupted while waiting to get result"); |
| 904 | Thread.currentThread().interrupt(); |
| 905 | } catch (ExecutionException e) { |
| 906 | log.error("Exception thrown while to get result"); |
| 907 | } |
| 908 | |
| 909 | return null; |
| 910 | } |
| 911 | |
| 912 | /** |
| 913 | * Handles port insert. |
| 914 | * |
| 915 | * @param tableName ovsdb table interface |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 916 | * @param portRow row of port |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 917 | * @return insert, empty if null |
| 918 | */ |
| 919 | private Insert handlePortInsertTable(String tableName, Row portRow) { |
| 920 | DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME); |
| 921 | |
| 922 | TableSchema portTableSchema = dbSchema |
| 923 | .getTableSchema(OvsdbConstant.PORT); |
| 924 | ColumnSchema portColumnSchema = portTableSchema.getColumnSchema("name"); |
| 925 | |
| 926 | String portName = (String) portRow.getColumn(portColumnSchema.name()).data(); |
| 927 | |
| 928 | Interface inf = (Interface) TableGenerator |
| 929 | .createTable(dbSchema, OvsdbTable.INTERFACE); |
| 930 | |
| 931 | inf.setName(portName); |
| 932 | |
| 933 | TableSchema intfTableSchema = dbSchema |
| 934 | .getTableSchema(OvsdbConstant.INTERFACE); |
| 935 | Insert insert = new Insert(intfTableSchema, OvsdbConstant.INTERFACE, |
| 936 | inf.getRow()); |
| 937 | return insert; |
| 938 | } |
| 939 | |
| 940 | /** |
| 941 | * Gets tunnel name. |
| 942 | * |
| 943 | * @param tunnelType |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 944 | * @param dstIp the remote ip address |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 945 | * @return tunnel name |
| 946 | */ |
| 947 | private String getTunnelName(String tunnelType, IpAddress dstIp) { |
| 948 | return tunnelType + "-" + dstIp.toString(); |
| 949 | } |
| 950 | |
| 951 | @Override |
| 952 | public ListenableFuture<DatabaseSchema> getOvsdbSchema(String dbName) { |
| 953 | if (dbName == null) { |
| 954 | return null; |
| 955 | } |
| 956 | DatabaseSchema databaseSchema = schema.get(dbName); |
| 957 | if (databaseSchema == null) { |
| 958 | List<String> dbNames = new ArrayList<String>(); |
| 959 | dbNames.add(dbName); |
| 960 | Function<JsonNode, DatabaseSchema> rowFunction = new Function<JsonNode, DatabaseSchema>() { |
| 961 | @Override |
| 962 | public DatabaseSchema apply(JsonNode input) { |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 963 | log.info("Get ovsdb database schema {}", dbName); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 964 | DatabaseSchema dbSchema = FromJsonUtil |
| 965 | .jsonNodeToDbSchema(dbName, input); |
| 966 | if (dbSchema == null) { |
| 967 | log.debug("Get ovsdb database schema error"); |
| 968 | return null; |
| 969 | } |
| 970 | schema.put(dbName, dbSchema); |
| 971 | |
| 972 | return dbSchema; |
| 973 | } |
| 974 | }; |
| 975 | |
| 976 | ListenableFuture<JsonNode> input = getSchema(dbNames); |
| 977 | if (input != null) { |
| 978 | return Futures.transform(input, rowFunction); |
| 979 | } |
| 980 | return null; |
| 981 | } else { |
| 982 | return Futures.immediateFuture(databaseSchema); |
| 983 | } |
| 984 | } |
| 985 | |
| 986 | @Override |
| 987 | public ListenableFuture<TableUpdates> monitorTables(String dbName, String id) { |
| 988 | if (dbName == null) { |
| 989 | return null; |
| 990 | } |
| 991 | DatabaseSchema dbSchema = schema.get(dbName); |
| 992 | if (dbSchema != null) { |
| 993 | Function<JsonNode, TableUpdates> rowFunction = new Function<JsonNode, TableUpdates>() { |
| 994 | @Override |
| 995 | public TableUpdates apply(JsonNode input) { |
| 996 | log.info("Get table updates"); |
| 997 | TableUpdates updates = FromJsonUtil |
| 998 | .jsonNodeToTableUpdates(input, dbSchema); |
| 999 | if (updates == null) { |
| 1000 | log.debug("Get table updates error"); |
| 1001 | return null; |
| 1002 | } |
| 1003 | return updates; |
| 1004 | } |
| 1005 | }; |
| 1006 | return Futures.transform(monitor(dbSchema, id), rowFunction); |
| 1007 | } |
| 1008 | return null; |
| 1009 | } |
| 1010 | |
| 1011 | @Override |
| 1012 | public ListenableFuture<List<OperationResult>> transactConfig(String dbName, |
| 1013 | List<Operation> operations) { |
| 1014 | if (dbName == null) { |
| 1015 | return null; |
| 1016 | } |
| 1017 | DatabaseSchema dbSchema = schema.get(dbName); |
| 1018 | if (dbSchema != null) { |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1019 | Function<List<JsonNode>, List<OperationResult>> rowFunction = (input -> { |
| 1020 | log.info("Get ovsdb operation result"); |
| 1021 | List<OperationResult> result = FromJsonUtil |
| 1022 | .jsonNodeToOperationResult(input, operations); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1023 | |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1024 | if (result == null) { |
| 1025 | log.debug("The operation result is null"); |
| 1026 | return null; |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1027 | } |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1028 | return result; |
| 1029 | }); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1030 | return Futures.transform(transact(dbSchema, operations), |
| 1031 | rowFunction); |
| 1032 | } |
| 1033 | return null; |
| 1034 | } |
| 1035 | |
| 1036 | @Override |
| 1037 | public ListenableFuture<JsonNode> getSchema(List<String> dbnames) { |
| 1038 | String id = java.util.UUID.randomUUID().toString(); |
| 1039 | String getSchemaString = JsonRpcWriterUtil.getSchemaStr(id, dbnames); |
| 1040 | |
| 1041 | SettableFuture<JsonNode> sf = SettableFuture.create(); |
| 1042 | requestResult.put(id, sf); |
| 1043 | requestMethod.put(id, "getSchema"); |
| 1044 | |
| 1045 | channel.writeAndFlush(getSchemaString); |
| 1046 | return sf; |
| 1047 | |
| 1048 | } |
| 1049 | |
| 1050 | @Override |
| 1051 | public ListenableFuture<List<String>> echo() { |
| 1052 | String id = java.util.UUID.randomUUID().toString(); |
| 1053 | String echoString = JsonRpcWriterUtil.echoStr(id); |
| 1054 | |
| 1055 | SettableFuture<List<String>> sf = SettableFuture.create(); |
| 1056 | requestResult.put(id, sf); |
| 1057 | requestMethod.put(id, "echo"); |
| 1058 | |
| 1059 | channel.writeAndFlush(echoString); |
| 1060 | return sf; |
| 1061 | |
| 1062 | } |
| 1063 | |
| 1064 | @Override |
| 1065 | public ListenableFuture<JsonNode> monitor(DatabaseSchema dbSchema, |
| 1066 | String monitorId) { |
| 1067 | String id = java.util.UUID.randomUUID().toString(); |
| 1068 | String monitorString = JsonRpcWriterUtil.monitorStr(id, monitorId, |
| 1069 | dbSchema); |
| 1070 | |
| 1071 | SettableFuture<JsonNode> sf = SettableFuture.create(); |
| 1072 | requestResult.put(id, sf); |
| 1073 | requestMethod.put(id, "monitor"); |
| 1074 | |
| 1075 | channel.writeAndFlush(monitorString); |
| 1076 | return sf; |
| 1077 | |
| 1078 | } |
| 1079 | |
| 1080 | @Override |
| 1081 | public ListenableFuture<List<String>> listDbs() { |
| 1082 | String id = java.util.UUID.randomUUID().toString(); |
| 1083 | String listDbsString = JsonRpcWriterUtil.listDbsStr(id); |
| 1084 | |
| 1085 | SettableFuture<List<String>> sf = SettableFuture.create(); |
| 1086 | requestResult.put(id, sf); |
| 1087 | requestMethod.put(id, "listDbs"); |
| 1088 | |
| 1089 | channel.writeAndFlush(listDbsString); |
| 1090 | return sf; |
| 1091 | |
| 1092 | } |
| 1093 | |
| 1094 | @Override |
| 1095 | public ListenableFuture<List<JsonNode>> transact(DatabaseSchema dbSchema, |
| 1096 | List<Operation> operations) { |
| 1097 | String id = java.util.UUID.randomUUID().toString(); |
| 1098 | String transactString = JsonRpcWriterUtil.transactStr(id, dbSchema, |
| 1099 | operations); |
| 1100 | |
| 1101 | SettableFuture<List<JsonNode>> sf = SettableFuture.create(); |
| 1102 | requestResult.put(id, sf); |
| 1103 | requestMethod.put(id, "transact"); |
| 1104 | |
| 1105 | channel.writeAndFlush(transactString); |
| 1106 | return sf; |
| 1107 | |
| 1108 | } |
| 1109 | |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1110 | @SuppressWarnings({"rawtypes", "unchecked"}) |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1111 | @Override |
| 1112 | public void processResult(JsonNode response) { |
| 1113 | log.debug("Handle result"); |
| 1114 | String requestId = response.get("id").asText(); |
| 1115 | SettableFuture sf = requestResult.get(requestId); |
| 1116 | if (sf == null) { |
| 1117 | log.debug("No such future to process"); |
| 1118 | return; |
| 1119 | } |
| 1120 | String methodName = requestMethod.get(requestId); |
| 1121 | |
| 1122 | Object result; |
| 1123 | result = FromJsonUtil.jsonResultParser(response, methodName); |
| 1124 | |
| 1125 | sf.set(result); |
| 1126 | return; |
| 1127 | } |
| 1128 | |
| 1129 | @Override |
| 1130 | public void processRequest(JsonNode requestJson) { |
| 1131 | log.debug("Handle request"); |
| 1132 | if (requestJson.get("method").asText().equalsIgnoreCase("echo")) { |
| 1133 | log.debug("handle echo request"); |
| 1134 | |
| 1135 | String replyString = FromJsonUtil.getEchoRequestStr(requestJson); |
| 1136 | channel.writeAndFlush(replyString); |
| 1137 | |
| 1138 | return; |
| 1139 | } else { |
| 1140 | FromJsonUtil |
| 1141 | .jsonCallbackRequestParser(requestJson, monitorCallBack); |
| 1142 | return; |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | @Override |
| 1147 | public void setCallback(Callback monitorCallback) { |
| 1148 | this.monitorCallBack = monitorCallback; |
| 1149 | } |
| 1150 | |
| 1151 | @Override |
| 1152 | public Set<OvsdbTunnel> getTunnels() { |
| 1153 | return ovsdbTunnels; |
| 1154 | } |
| 1155 | |
| 1156 | @Override |
| 1157 | public Set<OvsdbBridge> getBridges() { |
| 1158 | Set<OvsdbBridge> ovsdbBridges = new HashSet<OvsdbBridge>(); |
| 1159 | OvsdbTableStore tableStore = getTableStore(OvsdbConstant.DATABASENAME); |
| 1160 | if (tableStore == null) { |
| 1161 | return null; |
| 1162 | } |
| 1163 | OvsdbRowStore rowStore = tableStore.getRows(OvsdbConstant.BRIDGE); |
| 1164 | if (rowStore == null) { |
| 1165 | return null; |
| 1166 | } |
| 1167 | ConcurrentMap<String, Row> rows = rowStore.getRowStore(); |
| 1168 | for (String uuid : rows.keySet()) { |
| 1169 | Row row = getRow(OvsdbConstant.DATABASENAME, OvsdbConstant.BRIDGE, |
| 1170 | uuid); |
| 1171 | OvsdbBridge ovsdbBridge = getOvsdbBridge(row); |
| 1172 | if (ovsdbBridge != null) { |
| 1173 | ovsdbBridges.add(ovsdbBridge); |
| 1174 | } |
| 1175 | } |
| 1176 | return ovsdbBridges; |
| 1177 | } |
| 1178 | |
| 1179 | @Override |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1180 | public Set<ControllerInfo> getControllers(DeviceId openflowDeviceId) { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 1181 | Uuid bridgeUuid = getBridgeUuid(openflowDeviceId); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1182 | if (bridgeUuid == null) { |
| 1183 | log.warn("bad bridge Uuid"); |
| 1184 | return null; |
| 1185 | } |
| 1186 | List<Controller> controllers = getControllers(bridgeUuid); |
| 1187 | if (controllers == null) { |
| 1188 | log.warn("bad list of controllers"); |
| 1189 | return null; |
| 1190 | } |
| 1191 | return controllers.stream(). |
| 1192 | map(controller -> new ControllerInfo( |
| 1193 | (String) controller.getTargetColumn() |
| 1194 | .data())).collect(Collectors.toSet()); |
| 1195 | } |
| 1196 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 1197 | private List<Controller> getControllers(Uuid bridgeUuid) { |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1198 | DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME); |
| 1199 | if (dbSchema == null) { |
| 1200 | return null; |
| 1201 | } |
| 1202 | OvsdbRowStore rowStore = getRowStore(OvsdbConstant.DATABASENAME, |
| 1203 | OvsdbConstant.BRIDGE); |
| 1204 | if (rowStore == null) { |
| 1205 | log.debug("There is no bridge table"); |
| 1206 | return null; |
| 1207 | } |
| 1208 | |
| 1209 | Row bridgeRow = rowStore.getRow(bridgeUuid.value()); |
| 1210 | Bridge bridge = (Bridge) TableGenerator. |
| 1211 | getTable(dbSchema, bridgeRow, OvsdbTable.BRIDGE); |
| 1212 | |
| 1213 | //FIXME remove log |
| 1214 | log.warn("type of controller column", bridge.getControllerColumn() |
| 1215 | .data().getClass()); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 1216 | Set<Uuid> controllerUuids = (Set<Uuid>) ((OvsdbSet) bridge |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1217 | .getControllerColumn().data()).set(); |
| 1218 | // Set<String> controllerUuidStrings = (Set<String>) bridge.getControllerColumn().data(); |
| 1219 | |
| 1220 | OvsdbRowStore controllerRowStore = getRowStore(OvsdbConstant.DATABASENAME, |
| 1221 | OvsdbConstant.CONTROLLER); |
| 1222 | if (controllerRowStore == null) { |
| 1223 | log.debug("There is no controller table"); |
| 1224 | return null; |
| 1225 | } |
| 1226 | |
| 1227 | List<Controller> ovsdbControllers = new ArrayList<>(); |
| 1228 | ConcurrentMap<String, Row> controllerTableRows = controllerRowStore.getRowStore(); |
| 1229 | controllerTableRows.forEach((key, row) -> { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 1230 | if (!controllerUuids.contains(Uuid.uuid(key))) { |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1231 | return; |
| 1232 | } |
| 1233 | Controller controller = (Controller) TableGenerator |
| 1234 | .getTable(dbSchema, row, OvsdbTable.CONTROLLER); |
| 1235 | ovsdbControllers.add(controller); |
| 1236 | }); |
| 1237 | return ovsdbControllers; |
| 1238 | } |
| 1239 | |
| 1240 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 1241 | private Uuid getBridgeUuid(DeviceId openflowDeviceId) { |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1242 | DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME); |
| 1243 | if (dbSchema == null) { |
| 1244 | return null; |
| 1245 | } |
| 1246 | OvsdbRowStore rowStore = getRowStore(OvsdbConstant.DATABASENAME, |
| 1247 | OvsdbConstant.BRIDGE); |
| 1248 | if (rowStore == null) { |
| 1249 | log.debug("There is no bridge table"); |
| 1250 | return null; |
| 1251 | } |
| 1252 | |
| 1253 | ConcurrentMap<String, Row> bridgeTableRows = rowStore.getRowStore(); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 1254 | final AtomicReference<Uuid> uuid = new AtomicReference<>(); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1255 | for (Map.Entry<String, Row> entry : bridgeTableRows.entrySet()) { |
| 1256 | Bridge b = (Bridge) TableGenerator.getTable(dbSchema, |
| 1257 | entry.getValue(), |
| 1258 | OvsdbTable.BRIDGE); |
| 1259 | if (matchesDpid(b, openflowDeviceId)) { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 1260 | uuid.set(Uuid.uuid(entry.getKey())); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1261 | break; |
| 1262 | } |
| 1263 | } |
| 1264 | if (uuid.get() == null) { |
| 1265 | log.debug("There is no bridge for {}", openflowDeviceId); |
| 1266 | } |
| 1267 | return uuid.get(); |
| 1268 | |
| 1269 | } |
| 1270 | |
| 1271 | private static boolean matchesDpid(Bridge b, DeviceId deviceId) { |
| 1272 | String ofDpid = deviceId.toString().replace("of:", ""); |
| 1273 | Set ofDeviceIds = ((OvsdbSet) b.getDatapathIdColumn().data()).set(); |
| 1274 | //TODO Set<String> |
| 1275 | return ofDeviceIds.contains(ofDpid); |
| 1276 | } |
| 1277 | |
| 1278 | @Override |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1279 | public Set<OvsdbPort> getPorts() { |
| 1280 | Set<OvsdbPort> ovsdbPorts = new HashSet<OvsdbPort>(); |
| 1281 | OvsdbTableStore tableStore = getTableStore(OvsdbConstant.DATABASENAME); |
| 1282 | if (tableStore == null) { |
| 1283 | return null; |
| 1284 | } |
| 1285 | OvsdbRowStore rowStore = tableStore.getRows(OvsdbConstant.INTERFACE); |
| 1286 | if (rowStore == null) { |
| 1287 | return null; |
| 1288 | } |
| 1289 | ConcurrentMap<String, Row> rows = rowStore.getRowStore(); |
| 1290 | for (String uuid : rows.keySet()) { |
| 1291 | Row row = getRow(OvsdbConstant.DATABASENAME, |
| 1292 | OvsdbConstant.INTERFACE, uuid); |
| 1293 | OvsdbPort ovsdbPort = getOvsdbPort(row); |
| 1294 | if (ovsdbPort != null) { |
| 1295 | ovsdbPorts.add(ovsdbPort); |
| 1296 | } |
| 1297 | } |
| 1298 | return ovsdbPorts; |
| 1299 | } |
| 1300 | |
| 1301 | @Override |
| 1302 | public DatabaseSchema getDatabaseSchema(String dbName) { |
| 1303 | return schema.get(dbName); |
| 1304 | } |
| 1305 | |
| 1306 | //Gets ovsdb port. |
| 1307 | private OvsdbPort getOvsdbPort(Row row) { |
| 1308 | DatabaseSchema dbSchema = getDatabaseSchema(OvsdbConstant.DATABASENAME); |
| 1309 | Interface intf = (Interface) TableGenerator |
| 1310 | .getTable(dbSchema, row, OvsdbTable.INTERFACE); |
| 1311 | if (intf == null) { |
| 1312 | return null; |
| 1313 | } |
| 1314 | long ofPort = getOfPort(intf); |
| 1315 | String portName = intf.getName(); |
| 1316 | if ((ofPort < 0) || (portName == null)) { |
| 1317 | return null; |
| 1318 | } |
| 1319 | |
| 1320 | OvsdbPort ovsdbPort = new OvsdbPort(new OvsdbPortNumber(ofPort), |
| 1321 | new OvsdbPortName(portName)); |
| 1322 | return ovsdbPort; |
| 1323 | } |
| 1324 | |
| 1325 | ////Gets ovsdb bridge. |
| 1326 | private OvsdbBridge getOvsdbBridge(Row row) { |
| 1327 | DatabaseSchema dbSchema = getDatabaseSchema(OvsdbConstant.DATABASENAME); |
| 1328 | Bridge bridge = (Bridge) TableGenerator.getTable(dbSchema, row, |
| 1329 | OvsdbTable.BRIDGE); |
| 1330 | if (bridge == null) { |
| 1331 | return null; |
| 1332 | } |
| 1333 | |
| 1334 | OvsdbSet datapathIdSet = (OvsdbSet) bridge.getDatapathIdColumn().data(); |
| 1335 | @SuppressWarnings("unchecked") |
| 1336 | Set<String> datapathIds = datapathIdSet.set(); |
| 1337 | if (datapathIds == null || datapathIds.size() == 0) { |
| 1338 | return null; |
| 1339 | } |
| 1340 | String datapathId = (String) datapathIds.toArray()[0]; |
| 1341 | String bridgeName = bridge.getName(); |
| 1342 | if ((datapathId == null) || (bridgeName == null)) { |
| 1343 | return null; |
| 1344 | } |
| 1345 | |
| 1346 | OvsdbBridge ovsdbBridge = new OvsdbBridge(new OvsdbBridgeName(bridgeName), |
| 1347 | new OvsdbDatapathId(datapathId)); |
| 1348 | return ovsdbBridge; |
| 1349 | } |
| 1350 | |
| 1351 | //Gets ofPort in the interface. |
| 1352 | private long getOfPort(Interface intf) { |
| 1353 | OvsdbSet ofPortSet = (OvsdbSet) intf.getOpenFlowPortColumn().data(); |
| 1354 | @SuppressWarnings("unchecked") |
| 1355 | Set<Integer> ofPorts = ofPortSet.set(); |
| 1356 | while (ofPorts == null || ofPorts.size() <= 0) { |
| 1357 | log.debug("The ofport is null in {}", intf.getName()); |
| 1358 | return -1; |
| 1359 | } |
| 1360 | // return (long) ofPorts.toArray()[0]; |
| 1361 | Iterator<Integer> it = ofPorts.iterator(); |
| 1362 | return Long.parseLong(it.next().toString()); |
| 1363 | } |
CNlucius | a66c397 | 2015-09-06 20:31:29 +0800 | [diff] [blame] | 1364 | |
| 1365 | @Override |
| 1366 | public Set<OvsdbPort> getLocalPorts(Iterable<String> ifaceids) { |
| 1367 | Set<OvsdbPort> ovsdbPorts = new HashSet<OvsdbPort>(); |
| 1368 | OvsdbTableStore tableStore = getTableStore(OvsdbConstant.DATABASENAME); |
| 1369 | if (tableStore == null) { |
| 1370 | return null; |
| 1371 | } |
| 1372 | OvsdbRowStore rowStore = tableStore.getRows(OvsdbConstant.INTERFACE); |
| 1373 | if (rowStore == null) { |
| 1374 | return null; |
| 1375 | } |
| 1376 | ConcurrentMap<String, Row> rows = rowStore.getRowStore(); |
| 1377 | for (String uuid : rows.keySet()) { |
| 1378 | Row row = getRow(OvsdbConstant.DATABASENAME, |
| 1379 | OvsdbConstant.INTERFACE, uuid); |
| 1380 | DatabaseSchema dbSchema = getDatabaseSchema(OvsdbConstant.DATABASENAME); |
| 1381 | Interface intf = (Interface) TableGenerator |
| 1382 | .getTable(dbSchema, row, OvsdbTable.INTERFACE); |
| 1383 | if (intf == null || getIfaceid(intf) == null) { |
| 1384 | continue; |
| 1385 | } |
| 1386 | String portName = intf.getName(); |
| 1387 | Set<String> ifaceidSet = Sets.newHashSet(ifaceids); |
| 1388 | if (portName.startsWith("vxlan") |
| 1389 | || !ifaceidSet.contains(getIfaceid(intf))) { |
| 1390 | continue; |
| 1391 | } |
| 1392 | long ofPort = getOfPort(intf); |
| 1393 | if ((ofPort < 0) || (portName == null)) { |
| 1394 | continue; |
| 1395 | } |
| 1396 | |
| 1397 | OvsdbPort ovsdbPort = new OvsdbPort(new OvsdbPortNumber(ofPort), |
| 1398 | new OvsdbPortName(portName)); |
| 1399 | if (ovsdbPort != null) { |
| 1400 | ovsdbPorts.add(ovsdbPort); |
| 1401 | } |
| 1402 | } |
| 1403 | return ovsdbPorts; |
| 1404 | } |
| 1405 | |
| 1406 | private String getIfaceid(Interface intf) { |
| 1407 | OvsdbMap ovsdbMap = (OvsdbMap) intf.getExternalIdsColumn().data(); |
| 1408 | @SuppressWarnings("unchecked") |
| 1409 | Map<String, String> externalIds = ovsdbMap.map(); |
| 1410 | if (externalIds.isEmpty()) { |
| 1411 | log.warn("The external_ids is null"); |
| 1412 | return null; |
| 1413 | } |
| 1414 | String ifaceid = externalIds |
| 1415 | .get(OvsdbConstant.EXTERNAL_ID_INTERFACE_ID); |
| 1416 | if (ifaceid == null) { |
| 1417 | log.warn("The ifaceid is null"); |
| 1418 | return null; |
| 1419 | } |
| 1420 | return ifaceid; |
| 1421 | } |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 1422 | |
| 1423 | @Override |
| 1424 | public void disconnect() { |
| 1425 | channel.disconnect(); |
| 1426 | this.agent.removeConnectedNode(nodeId); |
| 1427 | } |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1428 | } |