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