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