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(); |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame^] | 359 | if (ports == null || ports.isEmpty()) { |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 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 | } |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | @Override |
| 438 | public void dropPort(String bridgeName, String portName) { |
| 439 | String bridgeUuid = getBridgeUuid(bridgeName); |
| 440 | if (bridgeUuid == null) { |
| 441 | log.error("Could not find Bridge {} in {}", bridgeName, nodeId); |
| 442 | return; |
| 443 | } |
| 444 | |
| 445 | String portUuid = getPortUuid(portName, bridgeUuid); |
| 446 | if (portUuid != null) { |
| 447 | log.info("Port {} delete", portName); |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 448 | deleteConfig(PORT, UUID, portUuid, BRIDGE, PORTS); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 452 | @Deprecated |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 453 | @Override |
| 454 | public void createBridge(String bridgeName) { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 455 | OvsdbBridge ovsdbBridge = OvsdbBridge.builder() |
| 456 | .name(bridgeName) |
| 457 | .build(); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 458 | |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 459 | createBridge(ovsdbBridge); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 462 | @Deprecated |
Hyunsun Moon | 646d8c4 | 2015-10-08 20:32:44 -0700 | [diff] [blame] | 463 | @Override |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 464 | public void createBridge(String bridgeName, String dpid, String exPortName) { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 465 | OvsdbBridge ovsdbBridge = OvsdbBridge.builder() |
| 466 | .name(bridgeName) |
| 467 | .failMode(BridgeDescription.FailMode.SECURE) |
| 468 | .datapathId(dpid) |
| 469 | .disableInBand() |
| 470 | .controllers(Lists.newArrayList(localController())) |
| 471 | .build(); |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 472 | |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 473 | createBridge(ovsdbBridge); |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 474 | |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 475 | if (exPortName != null) { |
| 476 | createPort(bridgeName, exPortName); |
| 477 | } |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 478 | } |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 479 | |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 480 | @Deprecated |
| 481 | @Override |
| 482 | public boolean createBridge(String bridgeName, String dpid, List<ControllerInfo> controllers) { |
| 483 | OvsdbBridge ovsdbBridge = OvsdbBridge.builder() |
| 484 | .name(bridgeName) |
| 485 | .failMode(BridgeDescription.FailMode.SECURE) |
| 486 | .datapathId(dpid) |
| 487 | .disableInBand() |
| 488 | .controllers(controllers) |
| 489 | .build(); |
| 490 | |
| 491 | return createBridge(ovsdbBridge); |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | @Override |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 495 | public boolean createBridge(OvsdbBridge ovsdbBridge) { |
| 496 | DatabaseSchema dbSchema = schema.get(DATABASENAME); |
| 497 | String ovsUuid = getOvsUuid(DATABASENAME); |
Hyunsun Moon | 646d8c4 | 2015-10-08 20:32:44 -0700 | [diff] [blame] | 498 | |
| 499 | if (dbSchema == null || ovsUuid == null) { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 500 | log.error("Can't find database Open_vSwitch"); |
Hyunsun Moon | 646d8c4 | 2015-10-08 20:32:44 -0700 | [diff] [blame] | 501 | return false; |
| 502 | } |
| 503 | |
Hyunsun Moon | 646d8c4 | 2015-10-08 20:32:44 -0700 | [diff] [blame] | 504 | Bridge bridge = (Bridge) TableGenerator.createTable(dbSchema, OvsdbTable.BRIDGE); |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 505 | bridge.setOtherConfig(ovsdbBridge.otherConfigs()); |
Hyunsun Moon | 646d8c4 | 2015-10-08 20:32:44 -0700 | [diff] [blame] | 506 | |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 507 | if (ovsdbBridge.failMode().isPresent()) { |
| 508 | String failMode = ovsdbBridge.failMode().get().name().toLowerCase(); |
| 509 | bridge.setFailMode(Sets.newHashSet(failMode)); |
Bob zhou | e9795fd | 2016-05-12 20:18:45 +0800 | [diff] [blame] | 510 | } |
Hyunsun Moon | 646d8c4 | 2015-10-08 20:32:44 -0700 | [diff] [blame] | 511 | |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 512 | String bridgeUuid = getBridgeUuid(ovsdbBridge.name()); |
Hyunsun Moon | 9802554 | 2016-03-08 04:36:02 -0800 | [diff] [blame] | 513 | if (bridgeUuid == null) { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 514 | bridge.setName(ovsdbBridge.name()); |
| 515 | bridgeUuid = insertConfig( |
| 516 | BRIDGE, UUID, DATABASENAME, BRIDGES, |
| 517 | ovsUuid, bridge.getRow()); |
Hyunsun Moon | 9802554 | 2016-03-08 04:36:02 -0800 | [diff] [blame] | 518 | } else { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 519 | // update the bridge if it's already existing |
| 520 | updateConfig(BRIDGE, UUID, bridgeUuid, bridge.getRow()); |
Hyunsun Moon | 9802554 | 2016-03-08 04:36:02 -0800 | [diff] [blame] | 521 | } |
Hyunsun Moon | 646d8c4 | 2015-10-08 20:32:44 -0700 | [diff] [blame] | 522 | |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 523 | if (bridgeUuid == null) { |
| 524 | log.warn("Failed to create bridge {} on {}", ovsdbBridge.name(), nodeId); |
Hyunsun Moon | 646d8c4 | 2015-10-08 20:32:44 -0700 | [diff] [blame] | 525 | return false; |
| 526 | } |
| 527 | |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 528 | createPort(ovsdbBridge.name(), ovsdbBridge.name()); |
| 529 | setControllersWithUuid(Uuid.uuid(bridgeUuid), ovsdbBridge.controllers()); |
| 530 | |
| 531 | log.info("Created bridge {}", ovsdbBridge.name()); |
Hyunsun Moon | 646d8c4 | 2015-10-08 20:32:44 -0700 | [diff] [blame] | 532 | return true; |
| 533 | } |
| 534 | |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 535 | @Override |
| 536 | public ControllerInfo localController() { |
| 537 | IpAddress ipAddress = IpAddress.valueOf(((InetSocketAddress) |
| 538 | channel.localAddress()).getAddress()); |
| 539 | return new ControllerInfo(ipAddress, OFPORT, "tcp"); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 542 | private void setControllersWithUuid(Uuid bridgeUuid, List<ControllerInfo> controllers) { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 543 | DatabaseSchema dbSchema = schema.get(DATABASENAME); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 544 | if (dbSchema == null) { |
| 545 | log.debug("There is no schema"); |
| 546 | return; |
| 547 | } |
| 548 | List<Controller> oldControllers = getControllers(bridgeUuid); |
| 549 | if (oldControllers == null) { |
| 550 | log.warn("There are no controllers"); |
| 551 | return; |
| 552 | } |
| 553 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 554 | Set<Uuid> newControllerUuids = new HashSet<>(); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 555 | |
| 556 | Set<ControllerInfo> newControllers = new HashSet<>(controllers); |
| 557 | List<Controller> removeControllers = new ArrayList<>(); |
| 558 | oldControllers.forEach(controller -> { |
| 559 | ControllerInfo controllerInfo = new ControllerInfo((String) controller.getTargetColumn().data()); |
| 560 | if (newControllers.contains(controllerInfo)) { |
| 561 | newControllers.remove(controllerInfo); |
| 562 | newControllerUuids.add(controller.getRow().uuid()); |
| 563 | } else { |
| 564 | removeControllers.add(controller); |
| 565 | } |
| 566 | }); |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 567 | OvsdbRowStore controllerRowStore = getRowStore(DATABASENAME, CONTROLLER); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 568 | if (controllerRowStore == null) { |
| 569 | log.debug("There is no controller table"); |
| 570 | return; |
| 571 | } |
| 572 | |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 573 | removeControllers.forEach(c -> deleteConfig(CONTROLLER, UUID, c.getRow().uuid().value(), |
| 574 | BRIDGE, "controller")); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 575 | newControllers.stream().map(c -> { |
| 576 | Controller controller = (Controller) TableGenerator |
| 577 | .createTable(dbSchema, OvsdbTable.CONTROLLER); |
| 578 | controller.setTarget(c.target()); |
| 579 | return controller; |
| 580 | }).forEach(c -> { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 581 | String uuid = insertConfig(CONTROLLER, UUID, BRIDGE, "controller", bridgeUuid.value(), |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 582 | c.getRow()); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 583 | newControllerUuids.add(Uuid.uuid(uuid)); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 584 | |
| 585 | }); |
| 586 | |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 587 | OvsdbRowStore rowStore = getRowStore(DATABASENAME, BRIDGE); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 588 | if (rowStore == null) { |
| 589 | log.debug("There is no bridge table"); |
| 590 | return; |
| 591 | } |
| 592 | |
| 593 | Row bridgeRow = rowStore.getRow(bridgeUuid.value()); |
| 594 | Bridge bridge = (Bridge) TableGenerator.getTable(dbSchema, bridgeRow, OvsdbTable.BRIDGE); |
| 595 | bridge.setController(OvsdbSet.ovsdbSet(newControllerUuids)); |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 596 | updateConfig(BRIDGE, UUID, bridgeUuid.value(), bridge.getRow()); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 597 | } |
| 598 | |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 599 | @Override |
| 600 | public void setControllersWithDeviceId(DeviceId deviceId, List<ControllerInfo> controllers) { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 601 | setControllersWithUuid(getBridgeUuid(deviceId), controllers); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 604 | @Override |
| 605 | public void dropBridge(String bridgeName) { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 606 | String bridgeUuid = getBridgeUuid(bridgeName); |
| 607 | if (bridgeUuid == null) { |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 608 | log.warn("Could not find bridge in node", nodeId.getIpAddress()); |
| 609 | return; |
| 610 | } |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 611 | deleteConfig(BRIDGE, UUID, bridgeUuid, DATABASENAME, BRIDGES); |
| 612 | } |
| 613 | |
Pier Ventre | f5d7236 | 2016-07-17 12:02:14 +0200 | [diff] [blame] | 614 | /** |
| 615 | * Creates a mirror port. Mirrors the traffic |
| 616 | * that goes to selectDstPort or comes from |
| 617 | * selectSrcPort or packets containing selectVlan |
| 618 | * to mirrorPort or to all ports that trunk mirrorVlan. |
| 619 | * |
| 620 | * @param mirror the OVSDB mirror description |
| 621 | * @return true if mirror creation is successful, false otherwise |
| 622 | */ |
| 623 | @Override |
| 624 | public boolean createMirror(String bridgeName, OvsdbMirror mirror) { |
| 625 | |
| 626 | /** |
| 627 | * Retrieves bridge's uuid. It is necessary to update |
| 628 | * Bridge table. |
| 629 | */ |
| 630 | String bridgeUuid = getBridgeUuid(bridgeName); |
| 631 | if (bridgeUuid == null) { |
| 632 | log.warn("Couldn't find bridge {} in {}", bridgeName, nodeId.getIpAddress()); |
| 633 | return false; |
| 634 | } |
| 635 | |
| 636 | OvsdbMirror.Builder mirrorBuilder = OvsdbMirror.builder(); |
| 637 | |
| 638 | mirrorBuilder.mirroringName(mirror.mirroringName()); |
| 639 | mirrorBuilder.selectAll(mirror.selectAll()); |
| 640 | |
| 641 | /** |
| 642 | * Retrieves the uuid of the monitored dst ports. |
| 643 | */ |
| 644 | mirrorBuilder.monitorDstPorts(mirror.monitorDstPorts().parallelStream() |
| 645 | .map(dstPort -> { |
| 646 | String dstPortUuid = getPortUuid(dstPort.value(), bridgeUuid); |
| 647 | if (dstPortUuid != null) { |
| 648 | return Uuid.uuid(dstPortUuid); |
| 649 | } |
| 650 | log.warn("Couldn't find port {} in {}", |
| 651 | dstPort.value(), nodeId.getIpAddress()); |
| 652 | return null; |
| 653 | }) |
| 654 | .filter(Objects::nonNull) |
| 655 | .collect(Collectors.toSet()) |
| 656 | ); |
| 657 | |
| 658 | /** |
| 659 | * Retrieves the uuid of the monitored src ports. |
| 660 | */ |
| 661 | mirrorBuilder.monitorSrcPorts(mirror.monitorSrcPorts().parallelStream() |
| 662 | .map(srcPort -> { |
| 663 | String srcPortUuid = getPortUuid(srcPort.value(), bridgeUuid); |
| 664 | if (srcPortUuid != null) { |
| 665 | return Uuid.uuid(srcPortUuid); |
| 666 | } |
| 667 | log.warn("Couldn't find port {} in {}", |
| 668 | srcPort.value(), nodeId.getIpAddress()); |
| 669 | return null; |
| 670 | }).filter(Objects::nonNull) |
| 671 | .collect(Collectors.toSet()) |
| 672 | ); |
| 673 | |
| 674 | mirrorBuilder.monitorVlans(mirror.monitorVlans()); |
| 675 | mirrorBuilder.mirrorPort(mirror.mirrorPort()); |
| 676 | mirrorBuilder.mirrorVlan(mirror.mirrorVlan()); |
| 677 | mirrorBuilder.externalIds(mirror.externalIds()); |
| 678 | mirror = mirrorBuilder.build(); |
| 679 | |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame^] | 680 | if (mirror.monitorDstPorts().isEmpty() && |
| 681 | mirror.monitorSrcPorts().isEmpty() && |
| 682 | mirror.monitorVlans().isEmpty()) { |
Pier Ventre | f5d7236 | 2016-07-17 12:02:14 +0200 | [diff] [blame] | 683 | log.warn("Invalid monitoring data"); |
| 684 | return false; |
| 685 | } |
| 686 | |
| 687 | DatabaseSchema dbSchema = schema.get(DATABASENAME); |
| 688 | |
| 689 | Mirror mirrorEntry = (Mirror) TableGenerator.createTable(dbSchema, OvsdbTable.MIRROR); |
| 690 | mirrorEntry.setName(mirror.mirroringName()); |
| 691 | mirrorEntry.setSelectDstPort(mirror.monitorDstPorts()); |
| 692 | mirrorEntry.setSelectSrcPort(mirror.monitorSrcPorts()); |
| 693 | mirrorEntry.setSelectVlan(mirror.monitorVlans()); |
| 694 | mirrorEntry.setExternalIds(mirror.externalIds()); |
| 695 | |
| 696 | /** |
| 697 | * If mirror port, retrieves the uuid of the mirror port. |
| 698 | */ |
| 699 | if (mirror.mirrorPort() != null) { |
| 700 | |
| 701 | String outputPortUuid = getPortUuid(mirror.mirrorPort().value(), bridgeUuid); |
| 702 | if (outputPortUuid == null) { |
| 703 | log.warn("Couldn't find port {} in {}", mirror.mirrorPort().value(), nodeId.getIpAddress()); |
| 704 | return false; |
| 705 | } |
| 706 | |
| 707 | mirrorEntry.setOutputPort(Uuid.uuid(outputPortUuid)); |
| 708 | |
| 709 | } else if (mirror.mirrorVlan() != null) { |
| 710 | |
| 711 | mirrorEntry.setOutputVlan(mirror.mirrorVlan()); |
| 712 | |
| 713 | } else { |
| 714 | log.warn("Invalid mirror, no mirror port and no mirror vlan"); |
| 715 | return false; |
| 716 | } |
| 717 | |
| 718 | ArrayList<Operation> operations = Lists.newArrayList(); |
| 719 | Insert mirrorInsert = new Insert(dbSchema.getTableSchema("Mirror"), "Mirror", mirrorEntry.getRow()); |
| 720 | operations.add(mirrorInsert); |
| 721 | |
| 722 | // update the bridge table |
| 723 | Condition condition = ConditionUtil.isEqual(UUID, Uuid.uuid(bridgeUuid)); |
| 724 | Mutation mutation = MutationUtil.insert(MIRRORS, Uuid.uuid("Mirror")); |
| 725 | List<Condition> conditions = Lists.newArrayList(condition); |
| 726 | List<Mutation> mutations = Lists.newArrayList(mutation); |
| 727 | operations.add(new Mutate(dbSchema.getTableSchema("Bridge"), conditions, mutations)); |
| 728 | |
| 729 | transactConfig(DATABASENAME, operations); |
| 730 | log.info("Created mirror {}", mirror.mirroringName()); |
| 731 | return true; |
| 732 | } |
| 733 | |
| 734 | /** |
| 735 | * Drops the configuration for mirror. |
| 736 | * |
Ray Milkey | ef79434 | 2016-11-09 16:20:29 -0800 | [diff] [blame] | 737 | * @param mirroringName name of mirror to drop |
Pier Ventre | f5d7236 | 2016-07-17 12:02:14 +0200 | [diff] [blame] | 738 | */ |
| 739 | @Override |
| 740 | public void dropMirror(MirroringName mirroringName) { |
| 741 | String mirrorUuid = getMirrorUuid(mirroringName.name()); |
| 742 | if (mirrorUuid != null) { |
| 743 | log.info("Deleted mirror {}", mirroringName.name()); |
| 744 | deleteConfig(MIRROR, UUID, mirrorUuid, BRIDGE, MIRRORS); |
| 745 | } |
| 746 | log.warn("Unable to delete {}", mirroringName.name()); |
| 747 | return; |
| 748 | } |
| 749 | |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 750 | @Deprecated |
| 751 | @Override |
| 752 | public boolean createTunnel(String bridgeName, String ifaceName, String tunnelType, |
| 753 | Map<String, String> options) { |
| 754 | OvsdbInterface ovsdbIface = OvsdbInterface.builder() |
| 755 | .name(ifaceName) |
| 756 | .type(Type.valueOf(tunnelType)) |
| 757 | .options(options) |
| 758 | .build(); |
| 759 | |
| 760 | return createInterface(bridgeName, ovsdbIface); |
| 761 | } |
| 762 | |
| 763 | @Deprecated |
| 764 | @Override |
| 765 | public void dropTunnel(IpAddress srcIp, IpAddress dstIp) { |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | @Override |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 769 | public boolean createInterface(String bridgeName, OvsdbInterface ovsdbIface) { |
Hyunsun Moon | 646d8c4 | 2015-10-08 20:32:44 -0700 | [diff] [blame] | 770 | String bridgeUuid = getBridgeUuid(bridgeName); |
| 771 | if (bridgeUuid == null) { |
| 772 | log.warn("Couldn't find bridge {} in {}", bridgeName, nodeId.getIpAddress()); |
| 773 | return false; |
| 774 | } |
| 775 | |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 776 | if (getPortUuid(ovsdbIface.name(), bridgeUuid) != null) { |
| 777 | log.warn("Interface {} already exists", ovsdbIface.name()); |
Hyunsun Moon | 646d8c4 | 2015-10-08 20:32:44 -0700 | [diff] [blame] | 778 | // remove existing one and re-create? |
| 779 | return false; |
| 780 | } |
| 781 | |
| 782 | ArrayList<Operation> operations = Lists.newArrayList(); |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 783 | DatabaseSchema dbSchema = schema.get(DATABASENAME); |
Hyunsun Moon | 646d8c4 | 2015-10-08 20:32:44 -0700 | [diff] [blame] | 784 | |
Hyunsun Moon | 8947866 | 2016-06-09 17:52:34 -0700 | [diff] [blame] | 785 | // insert a new port with the interface name |
Hyunsun Moon | 646d8c4 | 2015-10-08 20:32:44 -0700 | [diff] [blame] | 786 | Port port = (Port) TableGenerator.createTable(dbSchema, OvsdbTable.PORT); |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 787 | port.setName(ovsdbIface.name()); |
| 788 | Insert portInsert = new Insert(dbSchema.getTableSchema(PORT), PORT, port.getRow()); |
| 789 | portInsert.getRow().put(INTERFACES, Uuid.uuid(INTERFACE)); |
Hyunsun Moon | 646d8c4 | 2015-10-08 20:32:44 -0700 | [diff] [blame] | 790 | operations.add(portInsert); |
| 791 | |
Hyunsun Moon | 8947866 | 2016-06-09 17:52:34 -0700 | [diff] [blame] | 792 | // update the bridge table with the new port |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 793 | Condition condition = ConditionUtil.isEqual(UUID, Uuid.uuid(bridgeUuid)); |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 794 | Mutation mutation = MutationUtil.insert(PORTS, Uuid.uuid(PORT)); |
| 795 | List<Condition> conditions = Lists.newArrayList(condition); |
| 796 | List<Mutation> mutations = Lists.newArrayList(mutation); |
| 797 | operations.add(new Mutate(dbSchema.getTableSchema(BRIDGE), conditions, mutations)); |
Hyunsun Moon | 646d8c4 | 2015-10-08 20:32:44 -0700 | [diff] [blame] | 798 | |
Hyunsun Moon | 8947866 | 2016-06-09 17:52:34 -0700 | [diff] [blame] | 799 | // insert an interface |
Hyunsun Moon | 646d8c4 | 2015-10-08 20:32:44 -0700 | [diff] [blame] | 800 | Interface intf = (Interface) TableGenerator.createTable(dbSchema, OvsdbTable.INTERFACE); |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 801 | intf.setName(ovsdbIface.name()); |
| 802 | intf.setType(ovsdbIface.typeToString()); |
| 803 | intf.setOptions(ovsdbIface.options()); |
| 804 | Insert intfInsert = new Insert(dbSchema.getTableSchema(INTERFACE), INTERFACE, intf.getRow()); |
Hyunsun Moon | 646d8c4 | 2015-10-08 20:32:44 -0700 | [diff] [blame] | 805 | operations.add(intfInsert); |
| 806 | |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 807 | transactConfig(DATABASENAME, operations); |
Hyunsun Moon | 8947866 | 2016-06-09 17:52:34 -0700 | [diff] [blame] | 808 | log.info("Created interface {}", ovsdbIface); |
Hyunsun Moon | 646d8c4 | 2015-10-08 20:32:44 -0700 | [diff] [blame] | 809 | return true; |
| 810 | } |
| 811 | |
| 812 | @Override |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 813 | public boolean dropInterface(String ifaceName) { |
| 814 | OvsdbRowStore rowStore = getRowStore(DATABASENAME, BRIDGE); |
| 815 | if (rowStore == null) { |
| 816 | log.warn("Failed to get BRIDGE table"); |
| 817 | return false; |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 818 | } |
| 819 | |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 820 | ConcurrentMap<String, Row> bridgeTableRows = rowStore.getRowStore(); |
| 821 | if (bridgeTableRows == null) { |
| 822 | log.warn("Failed to get BRIDGE table rows"); |
| 823 | return false; |
| 824 | } |
| 825 | |
| 826 | // interface name is unique |
| 827 | Optional<String> bridgeId = bridgeTableRows.keySet().stream() |
| 828 | .filter(uuid -> getPortUuid(ifaceName, uuid) != null) |
| 829 | .findFirst(); |
| 830 | |
| 831 | if (bridgeId.isPresent()) { |
| 832 | String portId = getPortUuid(ifaceName, bridgeId.get()); |
| 833 | deleteConfig(PORT, UUID, portId, BRIDGE, PORTS); |
| 834 | return true; |
| 835 | } else { |
| 836 | log.warn("Unable to find the interface with name {}", ifaceName); |
| 837 | return false; |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 838 | } |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | /** |
| 842 | * Delete 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 childUuid child row uuid |
| 847 | * @param parentTableName parent table name |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 848 | * @param parentColumnName parent column |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 849 | */ |
| 850 | private void deleteConfig(String childTableName, String childColumnName, |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 851 | String childUuid, String parentTableName, |
| 852 | String parentColumnName) { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 853 | DatabaseSchema dbSchema = schema.get(DATABASENAME); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 854 | TableSchema childTableSchema = dbSchema.getTableSchema(childTableName); |
| 855 | |
| 856 | ArrayList<Operation> operations = Lists.newArrayList(); |
| 857 | if (parentTableName != null && parentColumnName != null) { |
| 858 | TableSchema parentTableSchema = dbSchema |
| 859 | .getTableSchema(parentTableName); |
| 860 | ColumnSchema parentColumnSchema = parentTableSchema |
| 861 | .getColumnSchema(parentColumnName); |
| 862 | List<Mutation> mutations = Lists.newArrayList(); |
| 863 | Mutation mutation = MutationUtil.delete(parentColumnSchema.name(), |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 864 | Uuid.uuid(childUuid)); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 865 | mutations.add(mutation); |
| 866 | List<Condition> conditions = Lists.newArrayList(); |
| 867 | Condition condition = ConditionUtil.includes(parentColumnName, |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 868 | Uuid.uuid(childUuid)); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 869 | conditions.add(condition); |
| 870 | Mutate op = new Mutate(parentTableSchema, conditions, mutations); |
| 871 | operations.add(op); |
| 872 | } |
| 873 | |
| 874 | List<Condition> conditions = Lists.newArrayList(); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 875 | Condition condition = ConditionUtil.isEqual(childColumnName, Uuid.uuid(childUuid)); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 876 | conditions.add(condition); |
| 877 | Delete del = new Delete(childTableSchema, conditions); |
| 878 | operations.add(del); |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 879 | transactConfig(DATABASENAME, operations); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | /** |
| 883 | * Update transact config. |
| 884 | * |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 885 | * @param tableName table name |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 886 | * @param columnName column name |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 887 | * @param uuid uuid |
| 888 | * @param row the config data |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 889 | */ |
| 890 | private void updateConfig(String tableName, String columnName, String uuid, |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 891 | Row row) { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 892 | DatabaseSchema dbSchema = schema.get(DATABASENAME); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 893 | TableSchema tableSchema = dbSchema.getTableSchema(tableName); |
| 894 | |
| 895 | List<Condition> conditions = Lists.newArrayList(); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 896 | Condition condition = ConditionUtil.isEqual(columnName, Uuid.uuid(uuid)); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 897 | conditions.add(condition); |
| 898 | |
| 899 | Update update = new Update(tableSchema, row, conditions); |
| 900 | |
| 901 | ArrayList<Operation> operations = Lists.newArrayList(); |
| 902 | operations.add(update); |
| 903 | |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 904 | transactConfig(DATABASENAME, operations); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | /** |
| 908 | * Insert transact config. |
| 909 | * |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 910 | * @param childTableName child table name |
| 911 | * @param childColumnName child column name |
| 912 | * @param parentTableName parent table name |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 913 | * @param parentColumnName parent column |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 914 | * @param parentUuid parent uuid |
| 915 | * @param row the config data |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 916 | * @return uuid, empty if no uuid is find |
| 917 | */ |
| 918 | private String insertConfig(String childTableName, String childColumnName, |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 919 | String parentTableName, String parentColumnName, |
| 920 | String parentUuid, Row row) { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 921 | DatabaseSchema dbSchema = schema.get(DATABASENAME); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 922 | TableSchema tableSchema = dbSchema.getTableSchema(childTableName); |
| 923 | |
Sho SHIMIZU | ff18f8c | 2016-03-11 14:43:53 -0800 | [diff] [blame] | 924 | Insert insert = new Insert(tableSchema, childTableName, row); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 925 | |
| 926 | ArrayList<Operation> operations = Lists.newArrayList(); |
| 927 | operations.add(insert); |
| 928 | |
| 929 | if (parentTableName != null && parentColumnName != null) { |
| 930 | TableSchema parentTableSchema = dbSchema |
| 931 | .getTableSchema(parentTableName); |
| 932 | ColumnSchema parentColumnSchema = parentTableSchema |
| 933 | .getColumnSchema(parentColumnName); |
| 934 | |
| 935 | List<Mutation> mutations = Lists.newArrayList(); |
| 936 | Mutation mutation = MutationUtil.insert(parentColumnSchema.name(), |
Sho SHIMIZU | ff18f8c | 2016-03-11 14:43:53 -0800 | [diff] [blame] | 937 | Uuid.uuid(childTableName)); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 938 | mutations.add(mutation); |
| 939 | |
| 940 | List<Condition> conditions = Lists.newArrayList(); |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 941 | Condition condition = ConditionUtil.isEqual(UUID, Uuid.uuid(parentUuid)); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 942 | conditions.add(condition); |
| 943 | |
| 944 | Mutate op = new Mutate(parentTableSchema, conditions, mutations); |
| 945 | operations.add(op); |
| 946 | } |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 947 | if (childTableName.equalsIgnoreCase(PORT)) { |
| 948 | log.debug("Handle port insert"); |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 949 | Insert intfInsert = handlePortInsertTable(row); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 950 | |
| 951 | if (intfInsert != null) { |
| 952 | operations.add(intfInsert); |
| 953 | } |
| 954 | |
| 955 | Insert ins = (Insert) operations.get(0); |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 956 | ins.getRow().put("interfaces", Uuid.uuid(INTERFACE)); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | List<OperationResult> results; |
| 960 | try { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 961 | results = transactConfig(DATABASENAME, operations).get(); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 962 | return results.get(0).getUuid().value(); |
| 963 | } catch (InterruptedException e) { |
| 964 | log.warn("Interrupted while waiting to get result"); |
| 965 | Thread.currentThread().interrupt(); |
| 966 | } catch (ExecutionException e) { |
| 967 | log.error("Exception thrown while to get result"); |
| 968 | } |
| 969 | |
| 970 | return null; |
| 971 | } |
| 972 | |
| 973 | /** |
| 974 | * Handles port insert. |
| 975 | * |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 976 | * @param portRow row of port |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 977 | * @return insert, empty if null |
| 978 | */ |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 979 | private Insert handlePortInsertTable(Row portRow) { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 980 | DatabaseSchema dbSchema = schema.get(DATABASENAME); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 981 | |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 982 | TableSchema portTableSchema = dbSchema.getTableSchema(PORT); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 983 | ColumnSchema portColumnSchema = portTableSchema.getColumnSchema("name"); |
| 984 | |
| 985 | String portName = (String) portRow.getColumn(portColumnSchema.name()).data(); |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 986 | Interface inf = (Interface) TableGenerator.createTable(dbSchema, OvsdbTable.INTERFACE); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 987 | inf.setName(portName); |
| 988 | |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 989 | TableSchema intfTableSchema = dbSchema.getTableSchema(INTERFACE); |
| 990 | return new Insert(intfTableSchema, INTERFACE, inf.getRow()); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 991 | } |
| 992 | |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 993 | @Override |
| 994 | public ListenableFuture<DatabaseSchema> getOvsdbSchema(String dbName) { |
| 995 | if (dbName == null) { |
| 996 | return null; |
| 997 | } |
| 998 | DatabaseSchema databaseSchema = schema.get(dbName); |
| 999 | if (databaseSchema == null) { |
| 1000 | List<String> dbNames = new ArrayList<String>(); |
| 1001 | dbNames.add(dbName); |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 1002 | Function<JsonNode, DatabaseSchema> rowFunction = input -> { |
| 1003 | log.debug("Get ovsdb database schema {}", dbName); |
| 1004 | DatabaseSchema dbSchema = FromJsonUtil.jsonNodeToDbSchema(dbName, input); |
| 1005 | if (dbSchema == null) { |
| 1006 | log.debug("Get ovsdb database schema error"); |
| 1007 | return null; |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1008 | } |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 1009 | schema.put(dbName, dbSchema); |
| 1010 | return dbSchema; |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1011 | }; |
| 1012 | |
| 1013 | ListenableFuture<JsonNode> input = getSchema(dbNames); |
| 1014 | if (input != null) { |
| 1015 | return Futures.transform(input, rowFunction); |
| 1016 | } |
| 1017 | return null; |
| 1018 | } else { |
| 1019 | return Futures.immediateFuture(databaseSchema); |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | @Override |
| 1024 | public ListenableFuture<TableUpdates> monitorTables(String dbName, String id) { |
| 1025 | if (dbName == null) { |
| 1026 | return null; |
| 1027 | } |
| 1028 | DatabaseSchema dbSchema = schema.get(dbName); |
| 1029 | if (dbSchema != null) { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 1030 | Function<JsonNode, TableUpdates> rowFunction = input -> { |
| 1031 | log.debug("Get table updates"); |
| 1032 | TableUpdates updates = FromJsonUtil.jsonNodeToTableUpdates(input, dbSchema); |
| 1033 | if (updates == null) { |
| 1034 | log.debug("Get table updates error"); |
| 1035 | return null; |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1036 | } |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 1037 | return updates; |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1038 | }; |
| 1039 | return Futures.transform(monitor(dbSchema, id), rowFunction); |
| 1040 | } |
| 1041 | return null; |
| 1042 | } |
| 1043 | |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 1044 | private ListenableFuture<List<OperationResult>> transactConfig(String dbName, |
| 1045 | List<Operation> operations) { |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1046 | if (dbName == null) { |
| 1047 | return null; |
| 1048 | } |
| 1049 | DatabaseSchema dbSchema = schema.get(dbName); |
| 1050 | if (dbSchema != null) { |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1051 | Function<List<JsonNode>, List<OperationResult>> rowFunction = (input -> { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 1052 | log.debug("Get ovsdb operation result"); |
| 1053 | List<OperationResult> result = FromJsonUtil.jsonNodeToOperationResult(input, operations); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1054 | if (result == null) { |
| 1055 | log.debug("The operation result is null"); |
| 1056 | return null; |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1057 | } |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1058 | return result; |
| 1059 | }); |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 1060 | return Futures.transform(transact(dbSchema, operations), rowFunction); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1061 | } |
| 1062 | return null; |
| 1063 | } |
| 1064 | |
| 1065 | @Override |
| 1066 | public ListenableFuture<JsonNode> getSchema(List<String> dbnames) { |
| 1067 | String id = java.util.UUID.randomUUID().toString(); |
| 1068 | String getSchemaString = JsonRpcWriterUtil.getSchemaStr(id, dbnames); |
| 1069 | |
| 1070 | SettableFuture<JsonNode> sf = SettableFuture.create(); |
| 1071 | requestResult.put(id, sf); |
| 1072 | requestMethod.put(id, "getSchema"); |
| 1073 | |
| 1074 | channel.writeAndFlush(getSchemaString); |
| 1075 | return sf; |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | @Override |
| 1079 | public ListenableFuture<List<String>> echo() { |
| 1080 | String id = java.util.UUID.randomUUID().toString(); |
| 1081 | String echoString = JsonRpcWriterUtil.echoStr(id); |
| 1082 | |
| 1083 | SettableFuture<List<String>> sf = SettableFuture.create(); |
| 1084 | requestResult.put(id, sf); |
| 1085 | requestMethod.put(id, "echo"); |
| 1086 | |
| 1087 | channel.writeAndFlush(echoString); |
| 1088 | return sf; |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | @Override |
| 1092 | public ListenableFuture<JsonNode> monitor(DatabaseSchema dbSchema, |
| 1093 | String monitorId) { |
| 1094 | String id = java.util.UUID.randomUUID().toString(); |
| 1095 | String monitorString = JsonRpcWriterUtil.monitorStr(id, monitorId, |
| 1096 | dbSchema); |
| 1097 | |
| 1098 | SettableFuture<JsonNode> sf = SettableFuture.create(); |
| 1099 | requestResult.put(id, sf); |
| 1100 | requestMethod.put(id, "monitor"); |
| 1101 | |
| 1102 | channel.writeAndFlush(monitorString); |
| 1103 | return sf; |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | @Override |
| 1107 | public ListenableFuture<List<String>> listDbs() { |
| 1108 | String id = java.util.UUID.randomUUID().toString(); |
| 1109 | String listDbsString = JsonRpcWriterUtil.listDbsStr(id); |
| 1110 | |
| 1111 | SettableFuture<List<String>> sf = SettableFuture.create(); |
| 1112 | requestResult.put(id, sf); |
| 1113 | requestMethod.put(id, "listDbs"); |
| 1114 | |
| 1115 | channel.writeAndFlush(listDbsString); |
| 1116 | return sf; |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | @Override |
| 1120 | public ListenableFuture<List<JsonNode>> transact(DatabaseSchema dbSchema, |
| 1121 | List<Operation> operations) { |
| 1122 | String id = java.util.UUID.randomUUID().toString(); |
| 1123 | String transactString = JsonRpcWriterUtil.transactStr(id, dbSchema, |
| 1124 | operations); |
| 1125 | |
| 1126 | SettableFuture<List<JsonNode>> sf = SettableFuture.create(); |
| 1127 | requestResult.put(id, sf); |
| 1128 | requestMethod.put(id, "transact"); |
| 1129 | |
| 1130 | channel.writeAndFlush(transactString); |
| 1131 | return sf; |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1132 | } |
| 1133 | |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1134 | @SuppressWarnings({"rawtypes", "unchecked"}) |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1135 | @Override |
| 1136 | public void processResult(JsonNode response) { |
| 1137 | log.debug("Handle result"); |
| 1138 | String requestId = response.get("id").asText(); |
| 1139 | SettableFuture sf = requestResult.get(requestId); |
| 1140 | if (sf == null) { |
| 1141 | log.debug("No such future to process"); |
| 1142 | return; |
| 1143 | } |
| 1144 | String methodName = requestMethod.get(requestId); |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 1145 | sf.set(FromJsonUtil.jsonResultParser(response, methodName)); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | @Override |
| 1149 | public void processRequest(JsonNode requestJson) { |
| 1150 | log.debug("Handle request"); |
| 1151 | if (requestJson.get("method").asText().equalsIgnoreCase("echo")) { |
| 1152 | log.debug("handle echo request"); |
| 1153 | |
| 1154 | String replyString = FromJsonUtil.getEchoRequestStr(requestJson); |
| 1155 | channel.writeAndFlush(replyString); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1156 | } else { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 1157 | FromJsonUtil.jsonCallbackRequestParser(requestJson, monitorCallBack); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | @Override |
| 1162 | public void setCallback(Callback monitorCallback) { |
| 1163 | this.monitorCallBack = monitorCallback; |
| 1164 | } |
| 1165 | |
| 1166 | @Override |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1167 | public Set<OvsdbBridge> getBridges() { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 1168 | Set<OvsdbBridge> ovsdbBridges = new HashSet<>(); |
| 1169 | OvsdbTableStore tableStore = getTableStore(DATABASENAME); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1170 | if (tableStore == null) { |
MaoJianwei | dac220d | 2016-07-04 22:37:52 +0800 | [diff] [blame] | 1171 | return ovsdbBridges; |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1172 | } |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 1173 | OvsdbRowStore rowStore = tableStore.getRows(BRIDGE); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1174 | if (rowStore == null) { |
MaoJianwei | dac220d | 2016-07-04 22:37:52 +0800 | [diff] [blame] | 1175 | return ovsdbBridges; |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1176 | } |
| 1177 | ConcurrentMap<String, Row> rows = rowStore.getRowStore(); |
| 1178 | for (String uuid : rows.keySet()) { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 1179 | Row row = getRow(DATABASENAME, BRIDGE, uuid); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1180 | OvsdbBridge ovsdbBridge = getOvsdbBridge(row); |
| 1181 | if (ovsdbBridge != null) { |
| 1182 | ovsdbBridges.add(ovsdbBridge); |
| 1183 | } |
| 1184 | } |
| 1185 | return ovsdbBridges; |
| 1186 | } |
| 1187 | |
| 1188 | @Override |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1189 | public Set<ControllerInfo> getControllers(DeviceId openflowDeviceId) { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 1190 | Uuid bridgeUuid = getBridgeUuid(openflowDeviceId); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1191 | if (bridgeUuid == null) { |
| 1192 | log.warn("bad bridge Uuid"); |
| 1193 | return null; |
| 1194 | } |
| 1195 | List<Controller> controllers = getControllers(bridgeUuid); |
| 1196 | if (controllers == null) { |
| 1197 | log.warn("bad list of controllers"); |
| 1198 | return null; |
| 1199 | } |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 1200 | return controllers.stream().map(controller -> new ControllerInfo( |
| 1201 | (String) controller.getTargetColumn() |
| 1202 | .data())).collect(Collectors.toSet()); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1203 | } |
| 1204 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 1205 | private List<Controller> getControllers(Uuid bridgeUuid) { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 1206 | DatabaseSchema dbSchema = schema.get(DATABASENAME); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1207 | if (dbSchema == null) { |
| 1208 | return null; |
| 1209 | } |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 1210 | OvsdbRowStore rowStore = getRowStore(DATABASENAME, BRIDGE); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1211 | if (rowStore == null) { |
| 1212 | log.debug("There is no bridge table"); |
| 1213 | return null; |
| 1214 | } |
| 1215 | |
| 1216 | Row bridgeRow = rowStore.getRow(bridgeUuid.value()); |
| 1217 | Bridge bridge = (Bridge) TableGenerator. |
| 1218 | getTable(dbSchema, bridgeRow, OvsdbTable.BRIDGE); |
| 1219 | |
| 1220 | //FIXME remove log |
| 1221 | log.warn("type of controller column", bridge.getControllerColumn() |
| 1222 | .data().getClass()); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 1223 | Set<Uuid> controllerUuids = (Set<Uuid>) ((OvsdbSet) bridge |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1224 | .getControllerColumn().data()).set(); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1225 | |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 1226 | OvsdbRowStore controllerRowStore = getRowStore(DATABASENAME, CONTROLLER); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1227 | if (controllerRowStore == null) { |
| 1228 | log.debug("There is no controller table"); |
| 1229 | return null; |
| 1230 | } |
| 1231 | |
| 1232 | List<Controller> ovsdbControllers = new ArrayList<>(); |
| 1233 | ConcurrentMap<String, Row> controllerTableRows = controllerRowStore.getRowStore(); |
| 1234 | controllerTableRows.forEach((key, row) -> { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 1235 | if (!controllerUuids.contains(Uuid.uuid(key))) { |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1236 | return; |
| 1237 | } |
| 1238 | Controller controller = (Controller) TableGenerator |
| 1239 | .getTable(dbSchema, row, OvsdbTable.CONTROLLER); |
| 1240 | ovsdbControllers.add(controller); |
| 1241 | }); |
| 1242 | return ovsdbControllers; |
| 1243 | } |
| 1244 | |
| 1245 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 1246 | private Uuid getBridgeUuid(DeviceId openflowDeviceId) { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 1247 | DatabaseSchema dbSchema = schema.get(DATABASENAME); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1248 | if (dbSchema == null) { |
| 1249 | return null; |
| 1250 | } |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 1251 | OvsdbRowStore rowStore = getRowStore(DATABASENAME, BRIDGE); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1252 | if (rowStore == null) { |
| 1253 | log.debug("There is no bridge table"); |
| 1254 | return null; |
| 1255 | } |
| 1256 | |
| 1257 | ConcurrentMap<String, Row> bridgeTableRows = rowStore.getRowStore(); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 1258 | final AtomicReference<Uuid> uuid = new AtomicReference<>(); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1259 | for (Map.Entry<String, Row> entry : bridgeTableRows.entrySet()) { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 1260 | Bridge bridge = (Bridge) TableGenerator.getTable( |
| 1261 | dbSchema, |
| 1262 | entry.getValue(), |
| 1263 | OvsdbTable.BRIDGE); |
| 1264 | |
| 1265 | if (matchesDpid(bridge, openflowDeviceId)) { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 1266 | uuid.set(Uuid.uuid(entry.getKey())); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1267 | break; |
| 1268 | } |
| 1269 | } |
| 1270 | if (uuid.get() == null) { |
| 1271 | log.debug("There is no bridge for {}", openflowDeviceId); |
| 1272 | } |
| 1273 | return uuid.get(); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | private static boolean matchesDpid(Bridge b, DeviceId deviceId) { |
| 1277 | String ofDpid = deviceId.toString().replace("of:", ""); |
| 1278 | Set ofDeviceIds = ((OvsdbSet) b.getDatapathIdColumn().data()).set(); |
| 1279 | //TODO Set<String> |
| 1280 | return ofDeviceIds.contains(ofDpid); |
| 1281 | } |
| 1282 | |
| 1283 | @Override |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1284 | public Set<OvsdbPort> getPorts() { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 1285 | Set<OvsdbPort> ovsdbPorts = new HashSet<>(); |
| 1286 | OvsdbTableStore tableStore = getTableStore(DATABASENAME); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1287 | if (tableStore == null) { |
| 1288 | return null; |
| 1289 | } |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 1290 | OvsdbRowStore rowStore = tableStore.getRows(INTERFACE); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1291 | if (rowStore == null) { |
| 1292 | return null; |
| 1293 | } |
| 1294 | ConcurrentMap<String, Row> rows = rowStore.getRowStore(); |
| 1295 | for (String uuid : rows.keySet()) { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 1296 | Row row = getRow(DATABASENAME, INTERFACE, uuid); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1297 | OvsdbPort ovsdbPort = getOvsdbPort(row); |
| 1298 | if (ovsdbPort != null) { |
| 1299 | ovsdbPorts.add(ovsdbPort); |
| 1300 | } |
| 1301 | } |
| 1302 | return ovsdbPorts; |
| 1303 | } |
| 1304 | |
| 1305 | @Override |
| 1306 | public DatabaseSchema getDatabaseSchema(String dbName) { |
| 1307 | return schema.get(dbName); |
| 1308 | } |
| 1309 | |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1310 | private OvsdbPort getOvsdbPort(Row row) { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 1311 | DatabaseSchema dbSchema = getDatabaseSchema(DATABASENAME); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1312 | Interface intf = (Interface) TableGenerator |
| 1313 | .getTable(dbSchema, row, OvsdbTable.INTERFACE); |
| 1314 | if (intf == null) { |
| 1315 | return null; |
| 1316 | } |
| 1317 | long ofPort = getOfPort(intf); |
| 1318 | String portName = intf.getName(); |
| 1319 | if ((ofPort < 0) || (portName == null)) { |
| 1320 | return null; |
| 1321 | } |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 1322 | return new OvsdbPort(new OvsdbPortNumber(ofPort), new OvsdbPortName(portName)); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1323 | } |
| 1324 | |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1325 | private OvsdbBridge getOvsdbBridge(Row row) { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 1326 | DatabaseSchema dbSchema = getDatabaseSchema(DATABASENAME); |
| 1327 | Bridge bridge = (Bridge) TableGenerator.getTable(dbSchema, row, OvsdbTable.BRIDGE); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1328 | if (bridge == null) { |
| 1329 | return null; |
| 1330 | } |
| 1331 | |
| 1332 | OvsdbSet datapathIdSet = (OvsdbSet) bridge.getDatapathIdColumn().data(); |
| 1333 | @SuppressWarnings("unchecked") |
| 1334 | Set<String> datapathIds = datapathIdSet.set(); |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame^] | 1335 | if (datapathIds == null || datapathIds.isEmpty()) { |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1336 | return null; |
| 1337 | } |
| 1338 | String datapathId = (String) datapathIds.toArray()[0]; |
| 1339 | String bridgeName = bridge.getName(); |
| 1340 | if ((datapathId == null) || (bridgeName == null)) { |
| 1341 | return null; |
| 1342 | } |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 1343 | return OvsdbBridge.builder().name(bridgeName).datapathId(datapathId).build(); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1344 | } |
| 1345 | |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1346 | private long getOfPort(Interface intf) { |
| 1347 | OvsdbSet ofPortSet = (OvsdbSet) intf.getOpenFlowPortColumn().data(); |
| 1348 | @SuppressWarnings("unchecked") |
| 1349 | Set<Integer> ofPorts = ofPortSet.set(); |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame^] | 1350 | if (ofPorts == null || ofPorts.isEmpty()) { |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1351 | log.debug("The ofport is null in {}", intf.getName()); |
| 1352 | return -1; |
| 1353 | } |
| 1354 | // return (long) ofPorts.toArray()[0]; |
| 1355 | Iterator<Integer> it = ofPorts.iterator(); |
| 1356 | return Long.parseLong(it.next().toString()); |
| 1357 | } |
CNlucius | a66c397 | 2015-09-06 20:31:29 +0800 | [diff] [blame] | 1358 | |
| 1359 | @Override |
| 1360 | public Set<OvsdbPort> getLocalPorts(Iterable<String> ifaceids) { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 1361 | Set<OvsdbPort> ovsdbPorts = new HashSet<>(); |
| 1362 | OvsdbTableStore tableStore = getTableStore(DATABASENAME); |
CNlucius | a66c397 | 2015-09-06 20:31:29 +0800 | [diff] [blame] | 1363 | if (tableStore == null) { |
| 1364 | return null; |
| 1365 | } |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 1366 | OvsdbRowStore rowStore = tableStore.getRows(INTERFACE); |
CNlucius | a66c397 | 2015-09-06 20:31:29 +0800 | [diff] [blame] | 1367 | if (rowStore == null) { |
| 1368 | return null; |
| 1369 | } |
| 1370 | ConcurrentMap<String, Row> rows = rowStore.getRowStore(); |
| 1371 | for (String uuid : rows.keySet()) { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 1372 | Row row = getRow(DATABASENAME, INTERFACE, uuid); |
| 1373 | DatabaseSchema dbSchema = getDatabaseSchema(DATABASENAME); |
CNlucius | a66c397 | 2015-09-06 20:31:29 +0800 | [diff] [blame] | 1374 | Interface intf = (Interface) TableGenerator |
| 1375 | .getTable(dbSchema, row, OvsdbTable.INTERFACE); |
| 1376 | if (intf == null || getIfaceid(intf) == null) { |
| 1377 | continue; |
| 1378 | } |
| 1379 | String portName = intf.getName(); |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 1380 | if (portName == null) { |
| 1381 | continue; |
| 1382 | } |
CNlucius | a66c397 | 2015-09-06 20:31:29 +0800 | [diff] [blame] | 1383 | Set<String> ifaceidSet = Sets.newHashSet(ifaceids); |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 1384 | if (portName.startsWith(TYPEVXLAN) || !ifaceidSet.contains(getIfaceid(intf))) { |
CNlucius | a66c397 | 2015-09-06 20:31:29 +0800 | [diff] [blame] | 1385 | continue; |
| 1386 | } |
| 1387 | long ofPort = getOfPort(intf); |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 1388 | if (ofPort < 0) { |
CNlucius | a66c397 | 2015-09-06 20:31:29 +0800 | [diff] [blame] | 1389 | continue; |
| 1390 | } |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 1391 | ovsdbPorts.add(new OvsdbPort(new OvsdbPortNumber(ofPort), |
| 1392 | new OvsdbPortName(portName))); |
CNlucius | a66c397 | 2015-09-06 20:31:29 +0800 | [diff] [blame] | 1393 | } |
| 1394 | return ovsdbPorts; |
| 1395 | } |
| 1396 | |
| 1397 | private String getIfaceid(Interface intf) { |
| 1398 | OvsdbMap ovsdbMap = (OvsdbMap) intf.getExternalIdsColumn().data(); |
| 1399 | @SuppressWarnings("unchecked") |
| 1400 | Map<String, String> externalIds = ovsdbMap.map(); |
| 1401 | if (externalIds.isEmpty()) { |
| 1402 | log.warn("The external_ids is null"); |
| 1403 | return null; |
| 1404 | } |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 1405 | String ifaceid = externalIds.get(EXTERNAL_ID_INTERFACE_ID); |
CNlucius | a66c397 | 2015-09-06 20:31:29 +0800 | [diff] [blame] | 1406 | if (ifaceid == null) { |
| 1407 | log.warn("The ifaceid is null"); |
| 1408 | return null; |
| 1409 | } |
| 1410 | return ifaceid; |
| 1411 | } |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 1412 | |
| 1413 | @Override |
| 1414 | public void disconnect() { |
| 1415 | channel.disconnect(); |
| 1416 | this.agent.removeConnectedNode(nodeId); |
| 1417 | } |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1418 | } |