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