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