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.impl; |
| 17 | |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.google.common.collect.ImmutableList; |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
Jian Li | eaf3103 | 2018-05-03 15:54:03 +0900 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Modified; |
| 24 | import org.apache.felix.scr.annotations.Property; |
| 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.Service; |
| 28 | import org.onlab.packet.IpAddress; |
| 29 | import org.onlab.packet.MacAddress; |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 30 | import org.onlab.packet.TpPort; |
Jian Li | eaf3103 | 2018-05-03 15:54:03 +0900 | [diff] [blame] | 31 | import org.onlab.util.Tools; |
| 32 | import org.onosproject.cfg.ComponentConfigService; |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 33 | import org.onosproject.ovsdb.controller.DefaultEventSubject; |
| 34 | import org.onosproject.ovsdb.controller.EventSubject; |
| 35 | import org.onosproject.ovsdb.controller.OvsdbClientService; |
| 36 | import org.onosproject.ovsdb.controller.OvsdbConstant; |
| 37 | import org.onosproject.ovsdb.controller.OvsdbController; |
| 38 | import org.onosproject.ovsdb.controller.OvsdbDatapathId; |
| 39 | import org.onosproject.ovsdb.controller.OvsdbEvent; |
| 40 | import org.onosproject.ovsdb.controller.OvsdbEvent.Type; |
| 41 | import org.onosproject.ovsdb.controller.OvsdbEventListener; |
| 42 | import org.onosproject.ovsdb.controller.OvsdbIfaceId; |
| 43 | import org.onosproject.ovsdb.controller.OvsdbNodeId; |
| 44 | import org.onosproject.ovsdb.controller.OvsdbNodeListener; |
| 45 | import org.onosproject.ovsdb.controller.OvsdbPortName; |
| 46 | import org.onosproject.ovsdb.controller.OvsdbPortNumber; |
| 47 | import org.onosproject.ovsdb.controller.OvsdbPortType; |
| 48 | import org.onosproject.ovsdb.controller.driver.OvsdbAgent; |
| 49 | import org.onosproject.ovsdb.rfc.jsonrpc.Callback; |
| 50 | import org.onosproject.ovsdb.rfc.message.TableUpdate; |
| 51 | import org.onosproject.ovsdb.rfc.message.TableUpdates; |
| 52 | import org.onosproject.ovsdb.rfc.message.UpdateNotification; |
| 53 | import org.onosproject.ovsdb.rfc.notation.OvsdbMap; |
| 54 | import org.onosproject.ovsdb.rfc.notation.OvsdbSet; |
| 55 | import org.onosproject.ovsdb.rfc.notation.Row; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 56 | import org.onosproject.ovsdb.rfc.notation.Uuid; |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 57 | import org.onosproject.ovsdb.rfc.schema.DatabaseSchema; |
| 58 | import org.onosproject.ovsdb.rfc.table.Bridge; |
| 59 | import org.onosproject.ovsdb.rfc.table.Interface; |
| 60 | import org.onosproject.ovsdb.rfc.table.OvsdbTable; |
| 61 | import org.onosproject.ovsdb.rfc.table.TableGenerator; |
| 62 | import org.onosproject.ovsdb.rfc.utils.FromJsonUtil; |
| 63 | import org.osgi.service.component.ComponentContext; |
| 64 | import org.slf4j.Logger; |
| 65 | import org.slf4j.LoggerFactory; |
| 66 | |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 67 | import java.math.BigInteger; |
Jian Li | eaf3103 | 2018-05-03 15:54:03 +0900 | [diff] [blame] | 68 | import java.util.Dictionary; |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 69 | import java.util.HashSet; |
| 70 | import java.util.Iterator; |
| 71 | import java.util.List; |
| 72 | import java.util.Map; |
| 73 | import java.util.Set; |
| 74 | import java.util.concurrent.ConcurrentHashMap; |
| 75 | import java.util.concurrent.CopyOnWriteArraySet; |
| 76 | import java.util.concurrent.ExecutionException; |
jiangrui | bce8065 | 2017-10-17 14:35:42 +0800 | [diff] [blame] | 77 | import java.util.concurrent.TimeUnit; |
| 78 | import java.util.concurrent.TimeoutException; |
jaegonkim | 1af0ae5 | 2017-01-01 10:46:55 +0900 | [diff] [blame] | 79 | import java.util.function.Consumer; |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 80 | |
| 81 | import static com.google.common.base.Preconditions.checkNotNull; |
Jian Li | eaf3103 | 2018-05-03 15:54:03 +0900 | [diff] [blame] | 82 | import static org.onosproject.ovsdb.controller.OvsdbConstant.SERVER_MODE; |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * The implementation of OvsdbController. |
| 86 | */ |
| 87 | @Component(immediate = true) |
| 88 | @Service |
| 89 | public class OvsdbControllerImpl implements OvsdbController { |
| 90 | |
| 91 | public static final Logger log = LoggerFactory |
| 92 | .getLogger(OvsdbControllerImpl.class); |
jiangrui | bce8065 | 2017-10-17 14:35:42 +0800 | [diff] [blame] | 93 | private static final long DEFAULT_OVSDB_RPC_TIMEOUT = 3000; |
| 94 | private final Controller controller = new Controller(); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 95 | protected ConcurrentHashMap<OvsdbNodeId, OvsdbClientService> ovsdbClients = |
kdarapu | fce5abb | 2018-05-10 19:37:53 +0530 | [diff] [blame] | 96 | new ConcurrentHashMap<>(); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 97 | protected OvsdbAgent agent = new InternalOvsdbNodeAgent(); |
| 98 | protected InternalMonitorCallBack updateCallback = new InternalMonitorCallBack(); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 99 | protected Set<OvsdbNodeListener> ovsdbNodeListener = new CopyOnWriteArraySet<>(); |
| 100 | protected Set<OvsdbEventListener> ovsdbEventListener = new CopyOnWriteArraySet<>(); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 101 | protected ConcurrentHashMap<String, OvsdbClientService> requestNotification = |
kdarapu | fce5abb | 2018-05-10 19:37:53 +0530 | [diff] [blame] | 102 | new ConcurrentHashMap<>(); |
| 103 | protected ConcurrentHashMap<String, String> requestDbName = new ConcurrentHashMap<>(); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 104 | |
Jian Li | eaf3103 | 2018-05-03 15:54:03 +0900 | [diff] [blame] | 105 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 106 | protected ComponentConfigService configService; |
| 107 | |
| 108 | @Property(name = "serverMode", boolValue = SERVER_MODE, |
| 109 | label = "Run as server mode, listen on 6640 port") |
| 110 | private boolean serverMode = SERVER_MODE; |
| 111 | |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 112 | @Activate |
| 113 | public void activate(ComponentContext context) { |
Jian Li | eaf3103 | 2018-05-03 15:54:03 +0900 | [diff] [blame] | 114 | controller.start(agent, updateCallback, serverMode); |
| 115 | |
| 116 | configService.registerProperties(getClass()); |
| 117 | |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 118 | log.info("Started"); |
| 119 | } |
| 120 | |
| 121 | @Deactivate |
| 122 | public void deactivate() { |
| 123 | controller.stop(); |
Jian Li | eaf3103 | 2018-05-03 15:54:03 +0900 | [diff] [blame] | 124 | |
| 125 | configService.unregisterProperties(getClass(), false); |
| 126 | |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 127 | log.info("Stoped"); |
| 128 | } |
| 129 | |
Jian Li | eaf3103 | 2018-05-03 15:54:03 +0900 | [diff] [blame] | 130 | @Modified |
| 131 | protected void modified(ComponentContext context) { |
| 132 | Dictionary<?, ?> properties = context.getProperties(); |
| 133 | Boolean flag; |
| 134 | |
| 135 | flag = Tools.isPropertyEnabled(properties, "serverMode"); |
| 136 | if (flag == null) { |
| 137 | log.info("OVSDB server mode is not configured, " + |
| 138 | "using current value of {}", serverMode); |
| 139 | } else { |
| 140 | serverMode = flag; |
| 141 | log.info("Configured. OVSDB server mode was {}", |
| 142 | serverMode ? "enabled" : "disabled"); |
| 143 | } |
| 144 | |
| 145 | restartController(); |
| 146 | } |
| 147 | |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 148 | @Override |
| 149 | public void addNodeListener(OvsdbNodeListener listener) { |
| 150 | if (!ovsdbNodeListener.contains(listener)) { |
| 151 | this.ovsdbNodeListener.add(listener); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void removeNodeListener(OvsdbNodeListener listener) { |
| 157 | this.ovsdbNodeListener.remove(listener); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public void addOvsdbEventListener(OvsdbEventListener listener) { |
| 162 | if (!ovsdbEventListener.contains(listener)) { |
| 163 | this.ovsdbEventListener.add(listener); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public void removeOvsdbEventListener(OvsdbEventListener listener) { |
| 169 | this.ovsdbEventListener.remove(listener); |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public List<OvsdbNodeId> getNodeIds() { |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 174 | return ImmutableList.copyOf(ovsdbClients.keySet()); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public OvsdbClientService getOvsdbClient(OvsdbNodeId nodeId) { |
| 179 | return ovsdbClients.get(nodeId); |
| 180 | } |
| 181 | |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 182 | @Override |
| 183 | public void connect(IpAddress ip, TpPort port) { |
| 184 | controller.connect(ip, port); |
| 185 | } |
| 186 | |
jaegonkim | 1af0ae5 | 2017-01-01 10:46:55 +0900 | [diff] [blame] | 187 | @Override |
| 188 | public void connect(IpAddress ip, TpPort port, Consumer<Exception> failhandler) { |
| 189 | controller.connect(ip, port, failhandler); |
| 190 | } |
| 191 | |
Jian Li | eaf3103 | 2018-05-03 15:54:03 +0900 | [diff] [blame] | 192 | @Override |
| 193 | public void setServerMode(boolean serverMode) { |
| 194 | this.serverMode = serverMode; |
| 195 | restartController(); |
| 196 | } |
| 197 | |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 198 | /** |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 199 | * Processes table updates. |
| 200 | * |
| 201 | * @param clientService OvsdbClientService instance |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 202 | * @param updates TableUpdates instance |
| 203 | * @param dbName ovsdb database name |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 204 | */ |
| 205 | private void processTableUpdates(OvsdbClientService clientService, |
| 206 | TableUpdates updates, String dbName) |
| 207 | throws InterruptedException { |
| 208 | checkNotNull(clientService, "OvsdbClientService is not null"); |
| 209 | |
| 210 | DatabaseSchema dbSchema = clientService.getDatabaseSchema(dbName); |
| 211 | |
| 212 | for (String tableName : updates.result().keySet()) { |
| 213 | TableUpdate update = updates.result().get(tableName); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 214 | for (Uuid uuid : (Set<Uuid>) update.rows().keySet()) { |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 215 | log.debug("Begin to process table updates uuid: {}, databaseName: {}, tableName: {}", |
| 216 | uuid.value(), dbName, tableName); |
| 217 | |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 218 | Row newRow = update.getNew(uuid); |
| 219 | if (newRow != null) { |
| 220 | clientService.updateOvsdbStore(dbName, tableName, |
| 221 | uuid.value(), newRow); |
| 222 | |
| 223 | if (OvsdbConstant.INTERFACE.equals(tableName)) { |
| 224 | dispatchInterfaceEvent(clientService, |
CNlucius | a66c397 | 2015-09-06 20:31:29 +0800 | [diff] [blame] | 225 | newRow, |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 226 | OvsdbEvent.Type.PORT_ADDED, |
| 227 | dbSchema); |
| 228 | } |
| 229 | } else if (update.getOld(uuid) != null) { |
CNlucius | a66c397 | 2015-09-06 20:31:29 +0800 | [diff] [blame] | 230 | if (OvsdbConstant.INTERFACE.equals(tableName)) { |
| 231 | Row row = clientService.getRow(OvsdbConstant.DATABASENAME, tableName, uuid.value()); |
| 232 | dispatchInterfaceEvent(clientService, |
| 233 | row, |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 234 | OvsdbEvent.Type.PORT_REMOVED, |
| 235 | dbSchema); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 236 | } |
CNlucius | a66c397 | 2015-09-06 20:31:29 +0800 | [diff] [blame] | 237 | clientService.removeRow(dbName, tableName, uuid.value()); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Dispatches event to the north. |
| 245 | * |
| 246 | * @param clientService OvsdbClientService instance |
tanbangcheng | e8d0439 | 2017-11-18 18:12:47 +0800 | [diff] [blame] | 247 | * @param row a new row |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 248 | * @param eventType type of event |
| 249 | * @param dbSchema ovsdb database schema |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 250 | */ |
| 251 | private void dispatchInterfaceEvent(OvsdbClientService clientService, |
CNlucius | a66c397 | 2015-09-06 20:31:29 +0800 | [diff] [blame] | 252 | Row row, |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 253 | Type eventType, |
| 254 | DatabaseSchema dbSchema) { |
| 255 | |
| 256 | long dpid = getDataPathid(clientService, dbSchema); |
| 257 | Interface intf = (Interface) TableGenerator |
CNlucius | a66c397 | 2015-09-06 20:31:29 +0800 | [diff] [blame] | 258 | .getTable(dbSchema, row, OvsdbTable.INTERFACE); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 259 | if (intf == null) { |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | String portType = (String) intf.getTypeColumn().data(); |
| 264 | long localPort = getOfPort(intf); |
| 265 | if (localPort < 0) { |
| 266 | return; |
| 267 | } |
| 268 | String[] macAndIfaceId = getMacAndIfaceid(intf); |
| 269 | if (macAndIfaceId == null) { |
| 270 | return; |
| 271 | } |
| 272 | |
| 273 | EventSubject eventSubject = new DefaultEventSubject(MacAddress.valueOf( |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 274 | macAndIfaceId[0]), |
kdarapu | fce5abb | 2018-05-10 19:37:53 +0530 | [diff] [blame] | 275 | new HashSet<>(), |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 276 | new OvsdbPortName(intf |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 277 | .getName()), |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 278 | new OvsdbPortNumber(localPort), |
| 279 | new OvsdbDatapathId(Long |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 280 | .toString(dpid)), |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 281 | new OvsdbPortType(portType), |
| 282 | new OvsdbIfaceId(macAndIfaceId[1])); |
| 283 | for (OvsdbEventListener listener : ovsdbEventListener) { |
kdarapu | fce5abb | 2018-05-10 19:37:53 +0530 | [diff] [blame] | 284 | listener.handle(new OvsdbEvent<>(eventType, |
| 285 | eventSubject)); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Gets mac and iface from the table Interface. |
| 291 | * |
| 292 | * @param intf Interface instance |
| 293 | * @return attachedMac, ifaceid |
| 294 | */ |
| 295 | private String[] getMacAndIfaceid(Interface intf) { |
| 296 | OvsdbMap ovsdbMap = (OvsdbMap) intf.getExternalIdsColumn().data(); |
| 297 | @SuppressWarnings("unchecked") |
| 298 | Map<String, String> externalIds = ovsdbMap.map(); |
| 299 | if (externalIds == null) { |
| 300 | log.warn("The external_ids is null"); |
| 301 | return null; |
| 302 | } |
| 303 | |
| 304 | String attachedMac = externalIds.get(OvsdbConstant.EXTERNAL_ID_VM_MAC); |
| 305 | if (attachedMac == null) { |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 306 | log.debug("The attachedMac is null"); //FIXME why always null? |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 307 | return null; |
| 308 | } |
| 309 | String ifaceid = externalIds |
| 310 | .get(OvsdbConstant.EXTERNAL_ID_INTERFACE_ID); |
| 311 | if (ifaceid == null) { |
| 312 | log.warn("The ifaceid is null"); |
| 313 | return null; |
| 314 | } |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 315 | return new String[]{attachedMac, ifaceid}; |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Gets ofPorts number from table Interface. |
| 320 | * |
| 321 | * @param intf Interface instance |
| 322 | * @return ofport the ofport number |
| 323 | */ |
| 324 | private long getOfPort(Interface intf) { |
| 325 | OvsdbSet ofPortSet = (OvsdbSet) intf.getOpenFlowPortColumn().data(); |
| 326 | @SuppressWarnings("unchecked") |
| 327 | Set<Integer> ofPorts = ofPortSet.set(); |
Jon Hall | b1f4e0f | 2017-02-22 13:36:16 -0800 | [diff] [blame] | 328 | if (ofPorts == null || ofPorts.isEmpty()) { |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 329 | log.debug("The ofport is null in {}", intf.getName()); |
| 330 | return -1; |
| 331 | } |
| 332 | Iterator<Integer> it = ofPorts.iterator(); |
| 333 | return Long.parseLong(it.next().toString()); |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Gets datapathid from table bridge. |
| 338 | * |
| 339 | * @param clientService OvsdbClientService instance |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 340 | * @param dbSchema ovsdb database schema |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 341 | * @return datapathid the bridge datapathid |
| 342 | */ |
| 343 | private long getDataPathid(OvsdbClientService clientService, |
| 344 | DatabaseSchema dbSchema) { |
| 345 | String bridgeUuid = clientService |
| 346 | .getBridgeUuid(OvsdbConstant.INTEGRATION_BRIDGE); |
| 347 | if (bridgeUuid == null) { |
| 348 | log.debug("Unable to spot bridge uuid for {} in {}", |
| 349 | OvsdbConstant.INTEGRATION_BRIDGE, clientService); |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | Row bridgeRow = clientService.getRow(OvsdbConstant.DATABASENAME, |
| 354 | "Bridge", bridgeUuid); |
| 355 | Bridge bridge = (Bridge) TableGenerator.getTable(dbSchema, bridgeRow, |
| 356 | OvsdbTable.BRIDGE); |
| 357 | OvsdbSet dpidSet = (OvsdbSet) bridge.getDatapathIdColumn().data(); |
| 358 | @SuppressWarnings("unchecked") |
| 359 | Set<String> dpids = dpidSet.set(); |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 360 | if (dpids == null || dpids.isEmpty()) { |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 361 | return 0; |
| 362 | } |
| 363 | return stringToLong((String) dpids.toArray()[0]); |
| 364 | } |
| 365 | |
| 366 | private long stringToLong(String values) { |
| 367 | long value = (new BigInteger(values.replaceAll(":", ""), 16)) |
| 368 | .longValue(); |
| 369 | return value; |
| 370 | } |
| 371 | |
Jian Li | eaf3103 | 2018-05-03 15:54:03 +0900 | [diff] [blame] | 372 | private void restartController() { |
| 373 | controller.stop(); |
| 374 | controller.start(agent, updateCallback, serverMode); |
| 375 | } |
| 376 | |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 377 | /** |
jiangrui | bce8065 | 2017-10-17 14:35:42 +0800 | [diff] [blame] | 378 | * Implementation of an Ovsdb Agent which is responsible for keeping track |
| 379 | * of connected node and the state in which they are. |
| 380 | */ |
| 381 | private class InternalOvsdbNodeAgent implements OvsdbAgent { |
| 382 | @Override |
| 383 | public void addConnectedNode(OvsdbNodeId nodeId, |
| 384 | OvsdbClientService ovsdbClient) { |
| 385 | |
| 386 | if (ovsdbClients.get(nodeId) != null) { |
| 387 | ovsdbClient.disconnect(); |
| 388 | return; |
| 389 | } else { |
| 390 | |
| 391 | try { |
| 392 | List<String> dbNames = ovsdbClient.listDbs().get(DEFAULT_OVSDB_RPC_TIMEOUT, TimeUnit.MILLISECONDS); |
| 393 | for (String dbName : dbNames) { |
| 394 | DatabaseSchema dbSchema; |
| 395 | dbSchema = ovsdbClient.getOvsdbSchema(dbName) |
| 396 | .get(DEFAULT_OVSDB_RPC_TIMEOUT, TimeUnit.MILLISECONDS); |
| 397 | |
| 398 | log.debug("Begin to monitor tables"); |
| 399 | String id = java.util.UUID.randomUUID().toString(); |
| 400 | TableUpdates updates = ovsdbClient |
| 401 | .monitorTables(dbName, id).get(DEFAULT_OVSDB_RPC_TIMEOUT, TimeUnit.MILLISECONDS); |
| 402 | |
| 403 | requestDbName.put(id, dbName); |
| 404 | requestNotification.put(id, ovsdbClient); |
| 405 | |
| 406 | if (updates != null) { |
| 407 | processTableUpdates(ovsdbClient, updates, |
| 408 | dbSchema.name()); |
| 409 | } |
| 410 | } |
| 411 | } catch (InterruptedException e) { |
| 412 | log.warn("Interrupted while waiting to get message from ovsdb"); |
| 413 | Thread.currentThread().interrupt(); |
| 414 | return; |
| 415 | } catch (ExecutionException e) { |
| 416 | log.error("Exception thrown while to get message from ovsdb"); |
| 417 | ovsdbClient.disconnect(); |
| 418 | return; |
| 419 | } catch (TimeoutException e) { |
| 420 | log.error("TimeoutException thrown while to get message from ovsdb"); |
| 421 | ovsdbClient.disconnect(); |
| 422 | return; |
| 423 | } |
| 424 | ovsdbClients.put(nodeId, ovsdbClient); |
| 425 | |
| 426 | log.debug("Add node to north"); |
| 427 | for (OvsdbNodeListener l : ovsdbNodeListener) { |
| 428 | l.nodeAdded(nodeId); |
| 429 | } |
| 430 | return; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | @Override |
| 435 | public void removeConnectedNode(OvsdbNodeId nodeId) { |
tanbangcheng | e8d0439 | 2017-11-18 18:12:47 +0800 | [diff] [blame] | 436 | requestNotification.forEach((k, v) -> { |
| 437 | if (v.nodeId().equals(nodeId)) { |
| 438 | requestNotification.remove(k); |
| 439 | requestDbName.remove(k); |
| 440 | |
| 441 | ovsdbClients.remove(nodeId); |
| 442 | log.debug("Node connection is removed"); |
| 443 | for (OvsdbNodeListener l : ovsdbNodeListener) { |
| 444 | l.nodeRemoved(nodeId); |
| 445 | } |
| 446 | } |
| 447 | }); |
jiangrui | bce8065 | 2017-10-17 14:35:42 +0800 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | |
| 451 | /** |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 452 | * Implementation of an Callback which is responsible for receiving request |
| 453 | * infomation from ovsdb. |
| 454 | */ |
| 455 | private class InternalMonitorCallBack implements Callback { |
| 456 | @Override |
| 457 | public void update(UpdateNotification updateNotification) { |
| 458 | Object key = updateNotification.jsonValue(); |
| 459 | OvsdbClientService ovsdbClient = requestNotification.get(key); |
| 460 | |
| 461 | String dbName = requestDbName.get(key); |
| 462 | JsonNode updatesJson = updateNotification.tbUpdatesJsonNode(); |
| 463 | DatabaseSchema dbSchema = ovsdbClient.getDatabaseSchema(dbName); |
| 464 | TableUpdates updates = FromJsonUtil |
| 465 | .jsonNodeToTableUpdates(updatesJson, dbSchema); |
| 466 | try { |
| 467 | processTableUpdates(ovsdbClient, updates, dbName); |
| 468 | } catch (InterruptedException e) { |
| 469 | log.warn("Interrupted while processing table updates"); |
| 470 | Thread.currentThread().interrupt(); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | @Override |
| 475 | public void locked(List<String> ids) { |
| 476 | // TODO Auto-generated method stub |
| 477 | } |
| 478 | |
| 479 | @Override |
| 480 | public void stolen(List<String> ids) { |
| 481 | // TODO Auto-generated method stub |
| 482 | } |
| 483 | |
| 484 | } |
| 485 | |
| 486 | } |