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 static com.google.common.base.Preconditions.checkNotNull;
20 import java.math.BigInteger;
21 import java.util.HashSet;
22 import java.util.Iterator;
23 import java.util.List;
26 import java.util.concurrent.ConcurrentHashMap;
27 import java.util.concurrent.CopyOnWriteArraySet;
28 import java.util.concurrent.ExecutionException;
30 import org.apache.felix.scr.annotations.Activate;
31 import org.apache.felix.scr.annotations.Component;
32 import org.apache.felix.scr.annotations.Deactivate;
33 import org.apache.felix.scr.annotations.Service;
34 import org.onlab.packet.IpAddress;
35 import org.onlab.packet.MacAddress;
36 import org.onosproject.ovsdb.controller.DefaultEventSubject;
37 import org.onosproject.ovsdb.controller.EventSubject;
38 import org.onosproject.ovsdb.controller.OvsdbClientService;
39 import org.onosproject.ovsdb.controller.OvsdbConstant;
40 import org.onosproject.ovsdb.controller.OvsdbController;
41 import org.onosproject.ovsdb.controller.OvsdbDatapathId;
42 import org.onosproject.ovsdb.controller.OvsdbEvent;
43 import org.onosproject.ovsdb.controller.OvsdbEvent.Type;
44 import org.onosproject.ovsdb.controller.OvsdbEventListener;
45 import org.onosproject.ovsdb.controller.OvsdbIfaceId;
46 import org.onosproject.ovsdb.controller.OvsdbNodeId;
47 import org.onosproject.ovsdb.controller.OvsdbNodeListener;
48 import org.onosproject.ovsdb.controller.OvsdbPortName;
49 import org.onosproject.ovsdb.controller.OvsdbPortNumber;
50 import org.onosproject.ovsdb.controller.OvsdbPortType;
51 import org.onosproject.ovsdb.controller.driver.OvsdbAgent;
52 import org.onosproject.ovsdb.rfc.jsonrpc.Callback;
53 import org.onosproject.ovsdb.rfc.message.TableUpdate;
54 import org.onosproject.ovsdb.rfc.message.TableUpdates;
55 import org.onosproject.ovsdb.rfc.message.UpdateNotification;
56 import org.onosproject.ovsdb.rfc.notation.OvsdbMap;
57 import org.onosproject.ovsdb.rfc.notation.OvsdbSet;
58 import org.onosproject.ovsdb.rfc.notation.Row;
59 import org.onosproject.ovsdb.rfc.notation.UUID;
60 import org.onosproject.ovsdb.rfc.schema.DatabaseSchema;
61 import org.onosproject.ovsdb.rfc.table.Bridge;
62 import org.onosproject.ovsdb.rfc.table.Interface;
63 import org.onosproject.ovsdb.rfc.table.OvsdbTable;
64 import org.onosproject.ovsdb.rfc.table.TableGenerator;
65 import org.onosproject.ovsdb.rfc.utils.FromJsonUtil;
66 import org.osgi.service.component.ComponentContext;
67 import org.slf4j.Logger;
68 import org.slf4j.LoggerFactory;
70 import com.fasterxml.jackson.databind.JsonNode;
73 * The implementation of OvsdbController.
75 @Component(immediate = true)
77 public class OvsdbControllerImpl implements OvsdbController {
79 public static final Logger log = LoggerFactory
80 .getLogger(OvsdbControllerImpl.class);
82 protected ConcurrentHashMap<OvsdbNodeId, OvsdbClientService> ovsdbClients =
83 new ConcurrentHashMap<OvsdbNodeId, OvsdbClientService>();
85 protected OvsdbAgent agent = new InternalOvsdbNodeAgent();
86 protected InternalMonitorCallBack updateCallback = new InternalMonitorCallBack();
88 protected Set<OvsdbNodeListener> ovsdbNodeListener = new CopyOnWriteArraySet<>();
89 protected Set<OvsdbEventListener> ovsdbEventListener = new CopyOnWriteArraySet<>();
91 protected ConcurrentHashMap<String, OvsdbClientService> requestNotification =
92 new ConcurrentHashMap<String, OvsdbClientService>();
94 protected ConcurrentHashMap<String, String> requestDbName = new ConcurrentHashMap<String, String>();
96 private final Controller controller = new Controller();
99 public void activate(ComponentContext context) {
100 controller.start(agent, updateCallback);
105 public void deactivate() {
111 public void addNodeListener(OvsdbNodeListener listener) {
112 if (!ovsdbNodeListener.contains(listener)) {
113 this.ovsdbNodeListener.add(listener);
118 public void removeNodeListener(OvsdbNodeListener listener) {
119 this.ovsdbNodeListener.remove(listener);
123 public void addOvsdbEventListener(OvsdbEventListener listener) {
124 if (!ovsdbEventListener.contains(listener)) {
125 this.ovsdbEventListener.add(listener);
130 public void removeOvsdbEventListener(OvsdbEventListener listener) {
131 this.ovsdbEventListener.remove(listener);
135 public List<OvsdbNodeId> getNodeIds() {
136 // TODO Auto-generated method stub
141 public OvsdbClientService getOvsdbClient(OvsdbNodeId nodeId) {
142 return ovsdbClients.get(nodeId);
146 * Implementation of an Ovsdb Agent which is responsible for keeping track
147 * of connected node and the state in which they are.
149 private class InternalOvsdbNodeAgent implements OvsdbAgent {
151 public void addConnectedNode(OvsdbNodeId nodeId,
152 OvsdbClientService ovsdbClient) {
154 if (ovsdbClients.get(nodeId) != null) {
157 ovsdbClients.put(nodeId, ovsdbClient);
160 List<String> dbNames = ovsdbClient.listDbs().get();
161 for (String dbName : dbNames) {
162 DatabaseSchema dbSchema;
163 dbSchema = ovsdbClient.getOvsdbSchema(dbName).get();
165 log.debug("Begin to monitor tables");
166 String id = java.util.UUID.randomUUID().toString();
167 TableUpdates updates = ovsdbClient
168 .monitorTables(dbName, id).get();
170 requestDbName.put(id, dbName);
171 requestNotification.put(id, ovsdbClient);
173 if (updates != null) {
174 processTableUpdates(ovsdbClient, updates,
178 } catch (InterruptedException e) {
179 log.warn("Interrupted while waiting to get message from ovsdb");
180 Thread.currentThread().interrupt();
181 } catch (ExecutionException e) {
182 log.error("Exception thrown while to get message from ovsdb");
185 log.debug("Add node to north");
186 for (OvsdbNodeListener l : ovsdbNodeListener) {
194 public void removeConnectedNode(OvsdbNodeId nodeId) {
195 ovsdbClients.remove(nodeId);
196 log.debug("Node connection is removed");
197 for (OvsdbNodeListener l : ovsdbNodeListener) {
198 l.nodeRemoved(nodeId);
204 * Processes table updates.
206 * @param clientService OvsdbClientService instance
207 * @param updates TableUpdates instance
208 * @param dbName ovsdb database name
210 private void processTableUpdates(OvsdbClientService clientService,
211 TableUpdates updates, String dbName)
212 throws InterruptedException {
213 checkNotNull(clientService, "OvsdbClientService is not null");
215 DatabaseSchema dbSchema = clientService.getDatabaseSchema(dbName);
217 for (String tableName : updates.result().keySet()) {
218 TableUpdate update = updates.result().get(tableName);
219 for (UUID uuid : (Set<UUID>) update.rows().keySet()) {
220 log.debug("Begin to process table updates uuid: {}, databaseName: {}, tableName: {}",
221 uuid.value(), dbName, tableName);
223 Row newRow = update.getNew(uuid);
224 if (newRow != null) {
225 clientService.updateOvsdbStore(dbName, tableName,
226 uuid.value(), newRow);
228 if (OvsdbConstant.INTERFACE.equals(tableName)) {
229 dispatchInterfaceEvent(clientService,
231 OvsdbEvent.Type.PORT_ADDED,
234 } else if (update.getOld(uuid) != null) {
235 if (OvsdbConstant.INTERFACE.equals(tableName)) {
236 Row row = clientService.getRow(OvsdbConstant.DATABASENAME, tableName, uuid.value());
237 dispatchInterfaceEvent(clientService,
239 OvsdbEvent.Type.PORT_REMOVED,
242 clientService.removeRow(dbName, tableName, uuid.value());
249 * Dispatches event to the north.
251 * @param clientService OvsdbClientService instance
252 * @param newRow a new row
253 * @param oldRow an old row
254 * @param eventType type of event
255 * @param dbSchema ovsdb database schema
257 private void dispatchInterfaceEvent(OvsdbClientService clientService,
260 DatabaseSchema dbSchema) {
262 long dpid = getDataPathid(clientService, dbSchema);
263 Interface intf = (Interface) TableGenerator
264 .getTable(dbSchema, row, OvsdbTable.INTERFACE);
269 String portType = (String) intf.getTypeColumn().data();
270 long localPort = getOfPort(intf);
274 String[] macAndIfaceId = getMacAndIfaceid(intf);
275 if (macAndIfaceId == null) {
279 EventSubject eventSubject = new DefaultEventSubject(MacAddress.valueOf(
281 new HashSet<IpAddress>(),
282 new OvsdbPortName(intf
284 new OvsdbPortNumber(localPort),
285 new OvsdbDatapathId(Long
287 new OvsdbPortType(portType),
288 new OvsdbIfaceId(macAndIfaceId[1]));
289 for (OvsdbEventListener listener : ovsdbEventListener) {
290 listener.handle(new OvsdbEvent<EventSubject>(eventType,
296 * Gets mac and iface from the table Interface.
298 * @param intf Interface instance
299 * @return attachedMac, ifaceid
301 private String[] getMacAndIfaceid(Interface intf) {
302 OvsdbMap ovsdbMap = (OvsdbMap) intf.getExternalIdsColumn().data();
303 @SuppressWarnings("unchecked")
304 Map<String, String> externalIds = ovsdbMap.map();
305 if (externalIds == null) {
306 log.warn("The external_ids is null");
310 String attachedMac = externalIds.get(OvsdbConstant.EXTERNAL_ID_VM_MAC);
311 if (attachedMac == null) {
312 log.warn("The attachedMac is null");
315 String ifaceid = externalIds
316 .get(OvsdbConstant.EXTERNAL_ID_INTERFACE_ID);
317 if (ifaceid == null) {
318 log.warn("The ifaceid is null");
321 return new String[] {attachedMac, ifaceid};
325 * Gets ofPorts number from table Interface.
327 * @param intf Interface instance
328 * @return ofport the ofport number
330 private long getOfPort(Interface intf) {
331 OvsdbSet ofPortSet = (OvsdbSet) intf.getOpenFlowPortColumn().data();
332 @SuppressWarnings("unchecked")
333 Set<Integer> ofPorts = ofPortSet.set();
334 while (ofPorts == null || ofPorts.size() <= 0) {
335 log.debug("The ofport is null in {}", intf.getName());
338 Iterator<Integer> it = ofPorts.iterator();
339 return Long.parseLong(it.next().toString());
343 * Gets datapathid from table bridge.
345 * @param clientService OvsdbClientService instance
346 * @param dbSchema ovsdb database schema
347 * @return datapathid the bridge datapathid
349 private long getDataPathid(OvsdbClientService clientService,
350 DatabaseSchema dbSchema) {
351 String bridgeUuid = clientService
352 .getBridgeUuid(OvsdbConstant.INTEGRATION_BRIDGE);
353 if (bridgeUuid == null) {
354 log.debug("Unable to spot bridge uuid for {} in {}",
355 OvsdbConstant.INTEGRATION_BRIDGE, clientService);
359 Row bridgeRow = clientService.getRow(OvsdbConstant.DATABASENAME,
360 "Bridge", bridgeUuid);
361 Bridge bridge = (Bridge) TableGenerator.getTable(dbSchema, bridgeRow,
363 OvsdbSet dpidSet = (OvsdbSet) bridge.getDatapathIdColumn().data();
364 @SuppressWarnings("unchecked")
365 Set<String> dpids = dpidSet.set();
366 if (dpids == null || dpids.size() == 0) {
369 return stringToLong((String) dpids.toArray()[0]);
372 private long stringToLong(String values) {
373 long value = (new BigInteger(values.replaceAll(":", ""), 16))
379 * Implementation of an Callback which is responsible for receiving request
380 * infomation from ovsdb.
382 private class InternalMonitorCallBack implements Callback {
384 public void update(UpdateNotification updateNotification) {
385 Object key = updateNotification.jsonValue();
386 OvsdbClientService ovsdbClient = requestNotification.get(key);
388 String dbName = requestDbName.get(key);
389 JsonNode updatesJson = updateNotification.tbUpdatesJsonNode();
390 DatabaseSchema dbSchema = ovsdbClient.getDatabaseSchema(dbName);
391 TableUpdates updates = FromJsonUtil
392 .jsonNodeToTableUpdates(updatesJson, dbSchema);
394 processTableUpdates(ovsdbClient, updates, dbName);
395 } catch (InterruptedException e) {
396 log.warn("Interrupted while processing table updates");
397 Thread.currentThread().interrupt();
402 public void locked(List<String> ids) {
403 // TODO Auto-generated method stub
407 public void stolen(List<String> ids) {
408 // TODO Auto-generated method stub