2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.ovsdb.controller.impl;
18 import com.fasterxml.jackson.databind.JsonNode;
19 import com.google.common.collect.ImmutableList;
20 import org.apache.felix.scr.annotations.Activate;
21 import org.apache.felix.scr.annotations.Component;
22 import org.apache.felix.scr.annotations.Deactivate;
23 import org.apache.felix.scr.annotations.Service;
24 import org.onlab.packet.IpAddress;
25 import org.onlab.packet.MacAddress;
26 import org.onlab.packet.TpPort;
27 import org.onosproject.ovsdb.controller.DefaultEventSubject;
28 import org.onosproject.ovsdb.controller.EventSubject;
29 import org.onosproject.ovsdb.controller.OvsdbClientService;
30 import org.onosproject.ovsdb.controller.OvsdbConstant;
31 import org.onosproject.ovsdb.controller.OvsdbController;
32 import org.onosproject.ovsdb.controller.OvsdbDatapathId;
33 import org.onosproject.ovsdb.controller.OvsdbEvent;
34 import org.onosproject.ovsdb.controller.OvsdbEvent.Type;
35 import org.onosproject.ovsdb.controller.OvsdbEventListener;
36 import org.onosproject.ovsdb.controller.OvsdbIfaceId;
37 import org.onosproject.ovsdb.controller.OvsdbNodeId;
38 import org.onosproject.ovsdb.controller.OvsdbNodeListener;
39 import org.onosproject.ovsdb.controller.OvsdbPortName;
40 import org.onosproject.ovsdb.controller.OvsdbPortNumber;
41 import org.onosproject.ovsdb.controller.OvsdbPortType;
42 import org.onosproject.ovsdb.controller.driver.OvsdbAgent;
43 import org.onosproject.ovsdb.rfc.jsonrpc.Callback;
44 import org.onosproject.ovsdb.rfc.message.TableUpdate;
45 import org.onosproject.ovsdb.rfc.message.TableUpdates;
46 import org.onosproject.ovsdb.rfc.message.UpdateNotification;
47 import org.onosproject.ovsdb.rfc.notation.OvsdbMap;
48 import org.onosproject.ovsdb.rfc.notation.OvsdbSet;
49 import org.onosproject.ovsdb.rfc.notation.Row;
50 import org.onosproject.ovsdb.rfc.notation.UUID;
51 import org.onosproject.ovsdb.rfc.schema.DatabaseSchema;
52 import org.onosproject.ovsdb.rfc.table.Bridge;
53 import org.onosproject.ovsdb.rfc.table.Interface;
54 import org.onosproject.ovsdb.rfc.table.OvsdbTable;
55 import org.onosproject.ovsdb.rfc.table.TableGenerator;
56 import org.onosproject.ovsdb.rfc.utils.FromJsonUtil;
57 import org.osgi.service.component.ComponentContext;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
61 import java.math.BigInteger;
62 import java.util.HashSet;
63 import java.util.Iterator;
64 import java.util.List;
67 import java.util.concurrent.ConcurrentHashMap;
68 import java.util.concurrent.CopyOnWriteArraySet;
69 import java.util.concurrent.ExecutionException;
71 import static com.google.common.base.Preconditions.checkNotNull;
74 * The implementation of OvsdbController.
76 @Component(immediate = true)
78 public class OvsdbControllerImpl implements OvsdbController {
80 public static final Logger log = LoggerFactory
81 .getLogger(OvsdbControllerImpl.class);
83 protected ConcurrentHashMap<OvsdbNodeId, OvsdbClientService> ovsdbClients =
84 new ConcurrentHashMap<OvsdbNodeId, OvsdbClientService>();
86 protected OvsdbAgent agent = new InternalOvsdbNodeAgent();
87 protected InternalMonitorCallBack updateCallback = new InternalMonitorCallBack();
89 protected Set<OvsdbNodeListener> ovsdbNodeListener = new CopyOnWriteArraySet<>();
90 protected Set<OvsdbEventListener> ovsdbEventListener = new CopyOnWriteArraySet<>();
92 protected ConcurrentHashMap<String, OvsdbClientService> requestNotification =
93 new ConcurrentHashMap<String, OvsdbClientService>();
95 protected ConcurrentHashMap<String, String> requestDbName = new ConcurrentHashMap<String, String>();
97 private final Controller controller = new Controller();
100 public void activate(ComponentContext context) {
101 controller.start(agent, updateCallback);
106 public void deactivate() {
112 public void addNodeListener(OvsdbNodeListener listener) {
113 if (!ovsdbNodeListener.contains(listener)) {
114 this.ovsdbNodeListener.add(listener);
119 public void removeNodeListener(OvsdbNodeListener listener) {
120 this.ovsdbNodeListener.remove(listener);
124 public void addOvsdbEventListener(OvsdbEventListener listener) {
125 if (!ovsdbEventListener.contains(listener)) {
126 this.ovsdbEventListener.add(listener);
131 public void removeOvsdbEventListener(OvsdbEventListener listener) {
132 this.ovsdbEventListener.remove(listener);
136 public List<OvsdbNodeId> getNodeIds() {
137 return ImmutableList.copyOf(ovsdbClients.keySet());
141 public OvsdbClientService getOvsdbClient(OvsdbNodeId nodeId) {
142 return ovsdbClients.get(nodeId);
146 public void connect(IpAddress ip, TpPort port) {
147 controller.connect(ip, port);
151 * Implementation of an Ovsdb Agent which is responsible for keeping track
152 * of connected node and the state in which they are.
154 private class InternalOvsdbNodeAgent implements OvsdbAgent {
156 public void addConnectedNode(OvsdbNodeId nodeId,
157 OvsdbClientService ovsdbClient) {
159 if (ovsdbClients.get(nodeId) != null) {
162 ovsdbClients.put(nodeId, ovsdbClient);
165 List<String> dbNames = ovsdbClient.listDbs().get();
166 for (String dbName : dbNames) {
167 DatabaseSchema dbSchema;
168 dbSchema = ovsdbClient.getOvsdbSchema(dbName).get();
170 log.debug("Begin to monitor tables");
171 String id = java.util.UUID.randomUUID().toString();
172 TableUpdates updates = ovsdbClient
173 .monitorTables(dbName, id).get();
175 requestDbName.put(id, dbName);
176 requestNotification.put(id, ovsdbClient);
178 if (updates != null) {
179 processTableUpdates(ovsdbClient, updates,
183 } catch (InterruptedException e) {
184 log.warn("Interrupted while waiting to get message from ovsdb");
185 Thread.currentThread().interrupt();
186 } catch (ExecutionException e) {
187 log.error("Exception thrown while to get message from ovsdb");
190 log.debug("Add node to north");
191 for (OvsdbNodeListener l : ovsdbNodeListener) {
199 public void removeConnectedNode(OvsdbNodeId nodeId) {
200 ovsdbClients.remove(nodeId);
201 log.debug("Node connection is removed");
202 for (OvsdbNodeListener l : ovsdbNodeListener) {
203 l.nodeRemoved(nodeId);
209 * Processes table updates.
211 * @param clientService OvsdbClientService instance
212 * @param updates TableUpdates instance
213 * @param dbName ovsdb database name
215 private void processTableUpdates(OvsdbClientService clientService,
216 TableUpdates updates, String dbName)
217 throws InterruptedException {
218 checkNotNull(clientService, "OvsdbClientService is not null");
220 DatabaseSchema dbSchema = clientService.getDatabaseSchema(dbName);
222 for (String tableName : updates.result().keySet()) {
223 TableUpdate update = updates.result().get(tableName);
224 for (UUID uuid : (Set<UUID>) update.rows().keySet()) {
225 log.debug("Begin to process table updates uuid: {}, databaseName: {}, tableName: {}",
226 uuid.value(), dbName, tableName);
228 Row newRow = update.getNew(uuid);
229 if (newRow != null) {
230 clientService.updateOvsdbStore(dbName, tableName,
231 uuid.value(), newRow);
233 if (OvsdbConstant.INTERFACE.equals(tableName)) {
234 dispatchInterfaceEvent(clientService,
236 OvsdbEvent.Type.PORT_ADDED,
239 } else if (update.getOld(uuid) != null) {
240 if (OvsdbConstant.INTERFACE.equals(tableName)) {
241 Row row = clientService.getRow(OvsdbConstant.DATABASENAME, tableName, uuid.value());
242 dispatchInterfaceEvent(clientService,
244 OvsdbEvent.Type.PORT_REMOVED,
247 clientService.removeRow(dbName, tableName, uuid.value());
254 * Dispatches event to the north.
256 * @param clientService OvsdbClientService instance
257 * @param newRow a new row
258 * @param oldRow an old row
259 * @param eventType type of event
260 * @param dbSchema ovsdb database schema
262 private void dispatchInterfaceEvent(OvsdbClientService clientService,
265 DatabaseSchema dbSchema) {
267 long dpid = getDataPathid(clientService, dbSchema);
268 Interface intf = (Interface) TableGenerator
269 .getTable(dbSchema, row, OvsdbTable.INTERFACE);
274 String portType = (String) intf.getTypeColumn().data();
275 long localPort = getOfPort(intf);
279 String[] macAndIfaceId = getMacAndIfaceid(intf);
280 if (macAndIfaceId == null) {
284 EventSubject eventSubject = new DefaultEventSubject(MacAddress.valueOf(
286 new HashSet<IpAddress>(),
287 new OvsdbPortName(intf
289 new OvsdbPortNumber(localPort),
290 new OvsdbDatapathId(Long
292 new OvsdbPortType(portType),
293 new OvsdbIfaceId(macAndIfaceId[1]));
294 for (OvsdbEventListener listener : ovsdbEventListener) {
295 listener.handle(new OvsdbEvent<EventSubject>(eventType,
301 * Gets mac and iface from the table Interface.
303 * @param intf Interface instance
304 * @return attachedMac, ifaceid
306 private String[] getMacAndIfaceid(Interface intf) {
307 OvsdbMap ovsdbMap = (OvsdbMap) intf.getExternalIdsColumn().data();
308 @SuppressWarnings("unchecked")
309 Map<String, String> externalIds = ovsdbMap.map();
310 if (externalIds == null) {
311 log.warn("The external_ids is null");
315 String attachedMac = externalIds.get(OvsdbConstant.EXTERNAL_ID_VM_MAC);
316 if (attachedMac == null) {
317 log.debug("The attachedMac is null"); //FIXME why always null?
320 String ifaceid = externalIds
321 .get(OvsdbConstant.EXTERNAL_ID_INTERFACE_ID);
322 if (ifaceid == null) {
323 log.warn("The ifaceid is null");
326 return new String[]{attachedMac, ifaceid};
330 * Gets ofPorts number from table Interface.
332 * @param intf Interface instance
333 * @return ofport the ofport number
335 private long getOfPort(Interface intf) {
336 OvsdbSet ofPortSet = (OvsdbSet) intf.getOpenFlowPortColumn().data();
337 @SuppressWarnings("unchecked")
338 Set<Integer> ofPorts = ofPortSet.set();
339 while (ofPorts == null || ofPorts.size() <= 0) {
340 log.debug("The ofport is null in {}", intf.getName());
343 Iterator<Integer> it = ofPorts.iterator();
344 return Long.parseLong(it.next().toString());
348 * Gets datapathid from table bridge.
350 * @param clientService OvsdbClientService instance
351 * @param dbSchema ovsdb database schema
352 * @return datapathid the bridge datapathid
354 private long getDataPathid(OvsdbClientService clientService,
355 DatabaseSchema dbSchema) {
356 String bridgeUuid = clientService
357 .getBridgeUuid(OvsdbConstant.INTEGRATION_BRIDGE);
358 if (bridgeUuid == null) {
359 log.debug("Unable to spot bridge uuid for {} in {}",
360 OvsdbConstant.INTEGRATION_BRIDGE, clientService);
364 Row bridgeRow = clientService.getRow(OvsdbConstant.DATABASENAME,
365 "Bridge", bridgeUuid);
366 Bridge bridge = (Bridge) TableGenerator.getTable(dbSchema, bridgeRow,
368 OvsdbSet dpidSet = (OvsdbSet) bridge.getDatapathIdColumn().data();
369 @SuppressWarnings("unchecked")
370 Set<String> dpids = dpidSet.set();
371 if (dpids == null || dpids.size() == 0) {
374 return stringToLong((String) dpids.toArray()[0]);
377 private long stringToLong(String values) {
378 long value = (new BigInteger(values.replaceAll(":", ""), 16))
384 * Implementation of an Callback which is responsible for receiving request
385 * infomation from ovsdb.
387 private class InternalMonitorCallBack implements Callback {
389 public void update(UpdateNotification updateNotification) {
390 Object key = updateNotification.jsonValue();
391 OvsdbClientService ovsdbClient = requestNotification.get(key);
393 String dbName = requestDbName.get(key);
394 JsonNode updatesJson = updateNotification.tbUpdatesJsonNode();
395 DatabaseSchema dbSchema = ovsdbClient.getDatabaseSchema(dbName);
396 TableUpdates updates = FromJsonUtil
397 .jsonNodeToTableUpdates(updatesJson, dbSchema);
399 processTableUpdates(ovsdbClient, updates, dbName);
400 } catch (InterruptedException e) {
401 log.warn("Interrupted while processing table updates");
402 Thread.currentThread().interrupt();
407 public void locked(List<String> ids) {
408 // TODO Auto-generated method stub
412 public void stolen(List<String> ids) {
413 // TODO Auto-generated method stub