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