CNlucius
Committed by Gerrit Code Review

ONOS-2708

Add implementation of getting ovsdb ports or bridges in the ovsdb node.

Change-Id: If31af08ccb90a29bc800a79f332dae2bc497b105
......@@ -16,7 +16,9 @@
package org.onosproject.net.behaviour;
import java.util.Collection;
import java.util.Set;
import org.onosproject.net.PortNumber;
import org.onosproject.net.device.PortDescription;
import org.onosproject.net.driver.HandlerBehaviour;
......@@ -66,4 +68,11 @@ public interface BridgeConfig extends HandlerBehaviour {
* @return collection of port
*/
Collection<PortDescription> getPorts();
/**
* Get a collection of port.
*
* @return portNumbers set of PortNumber
*/
Set<PortNumber> getPortNumbers();
}
......
......@@ -16,6 +16,8 @@
package org.onosproject.driver.ovsdb;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.onlab.packet.IpAddress;
......@@ -46,45 +48,44 @@ public class OvsdbBridgeConfig extends AbstractHandlerBehaviour
@Override
public void addBridge(BridgeName bridgeName) {
DriverHandler handler = handler();
OvsdbClientService ovsdbNode = getOvsdbNode(handler);
ovsdbNode.createBridge(bridgeName.name());
OvsdbClientService clientService = getOvsdbClientService(handler);
clientService.createBridge(bridgeName.name());
}
@Override
public void deleteBridge(BridgeName bridgeName) {
DriverHandler handler = handler();
OvsdbClientService ovsdbNode = getOvsdbNode(handler);
ovsdbNode.dropBridge(bridgeName.name());
OvsdbClientService clientService = getOvsdbClientService(handler);
clientService.dropBridge(bridgeName.name());
}
@Override
public Collection<BridgeDescription> getBridges() {
DriverHandler handler = handler();
DeviceId deviceId = handler.data().deviceId();
OvsdbClientService ovsdbNode = getOvsdbNode(handler);
Set<OvsdbBridge> ovsdbSet = ovsdbNode.getBridges();
OvsdbClientService clientService = getOvsdbClientService(handler);
Set<OvsdbBridge> ovsdbSet = clientService.getBridges();
Collection<BridgeDescription> bridges = Sets.newHashSet();
ovsdbSet.forEach(o -> {
BridgeName bridgeName = BridgeName.bridgeName(o.bridgeName()
.toString());
DeviceId ownDeviceId = DeviceId.deviceId(o.datapathId().toString());
BridgeDescription description = new DefaultBridgeDescription(
bridgeName,
BridgeName bridgeName = BridgeName
.bridgeName(o.bridgeName().value());
DeviceId ownDeviceId = DeviceId.deviceId("of:" + o.datapathId().value());
BridgeDescription description = new DefaultBridgeDescription(bridgeName,
deviceId,
ownDeviceId);
bridges.add(description);
});
return bridges;
return bridges == null ? Collections.emptySet() : bridges;
}
@Override
public void addPort(PortDescription port) {
DriverHandler handler = handler();
OvsdbClientService ovsdbNode = getOvsdbNode(handler);
Set<OvsdbBridge> ovsdbSet = ovsdbNode.getBridges();
OvsdbClientService clientService = getOvsdbClientService(handler);
Set<OvsdbBridge> ovsdbSet = clientService.getBridges();
if (ovsdbSet != null && ovsdbSet.size() > 0) {
OvsdbBridge bridge = ovsdbSet.iterator().next();
ovsdbNode.createPort(bridge.bridgeName().toString(), port
clientService.createPort(bridge.bridgeName().toString(), port
.portNumber().toString());
}
}
......@@ -92,11 +93,11 @@ public class OvsdbBridgeConfig extends AbstractHandlerBehaviour
@Override
public void deletePort(PortDescription port) {
DriverHandler handler = handler();
OvsdbClientService ovsdbNode = getOvsdbNode(handler);
Set<OvsdbBridge> ovsdbSet = ovsdbNode.getBridges();
OvsdbClientService clientService = getOvsdbClientService(handler);
Set<OvsdbBridge> ovsdbSet = clientService.getBridges();
if (ovsdbSet != null && ovsdbSet.size() > 0) {
OvsdbBridge bridge = ovsdbSet.iterator().next();
ovsdbNode.dropPort(bridge.bridgeName().toString(), port
clientService.dropPort(bridge.bridgeName().toString(), port
.portNumber().toString());
}
}
......@@ -104,8 +105,8 @@ public class OvsdbBridgeConfig extends AbstractHandlerBehaviour
@Override
public Collection<PortDescription> getPorts() {
DriverHandler handler = handler();
OvsdbClientService ovsdbNode = getOvsdbNode(handler);
Set<OvsdbPort> ovsdbSet = ovsdbNode.getPorts();
OvsdbClientService clientService = getOvsdbClientService(handler);
Set<OvsdbPort> ovsdbSet = clientService.getPorts();
Collection<PortDescription> ports = Sets.newHashSet();
ovsdbSet.forEach(o -> {
PortNumber port = PortNumber.portNumber(o.portNumber().value());
......@@ -127,10 +128,25 @@ public class OvsdbBridgeConfig extends AbstractHandlerBehaviour
return new OvsdbNodeId(ipAddress, portL);
}
private OvsdbClientService getOvsdbNode(DriverHandler handler) {
// Used for getting OvsdbClientService.
private OvsdbClientService getOvsdbClientService(DriverHandler handler) {
OvsdbController ovsController = handler.get(OvsdbController.class);
DeviceId deviceId = handler.data().deviceId();
OvsdbNodeId nodeId = changeDeviceIdToNodeId(deviceId);
return ovsController.getOvsdbClient(nodeId);
}
@Override
public Set<PortNumber> getPortNumbers() {
Set<PortNumber> ports = new HashSet<>();
DriverHandler handler = handler();
OvsdbClientService clientService = getOvsdbClientService(handler);
Set<OvsdbPort> ovsdbSet = clientService.getPorts();
ovsdbSet.forEach(o -> {
PortNumber port = PortNumber.portNumber(o.portNumber().value(),
o.portName().value());
ports.add(port);
});
return ports;
}
}
......
......@@ -18,18 +18,18 @@
<driver name="default"
manufacturer="ON.Lab" hwVersion="0.0.1" swVersion="0.0.1">
<behaviour api="org.onosproject.net.behaviour.Pipeliner"
impl="org.onosproject.driver.pipeline.DefaultSingleTablePipeline"/>
impl="org.onosproject.driver.pipeline.OpenVSwitchPipeline"/>
<behaviour api="org.onosproject.openflow.controller.driver.OpenFlowSwitchDriver"
impl="org.onosproject.driver.handshaker.DefaultSwitchHandshaker"/>
<behaviour api="org.onosproject.net.behaviour.TunnelConfig"
impl="org.onosproject.driver.ovsdb.OvsdbTunnelConfig"/>
<behaviour api="org.onosproject.net.behaviour.BridgeConfig"
impl="org.onosproject.driver.ovsdb.OvsdbBridgeConfig"/>
</driver>
<driver name="ovs" extends="default"
manufacturer="Nicira, Inc\." hwVersion="Open vSwitch" swVersion="2\..*">
<behaviour api="org.onosproject.openflow.controller.driver.OpenFlowSwitchDriver"
impl="org.onosproject.driver.handshaker.NiciraSwitchHandshaker"/>
<behaviour api="org.onosproject.net.behaviour.TunnelConfig"
impl="org.onosproject.driver.ovsdb.OvsdbTunnelConfig"/>
<behaviour api="org.onosproject.net.behaviour.BridgeConfig"
impl="org.onosproject.driver.ovsdb.OvsdbBridgeConfig"/>
</driver>
<driver name="ovs-corsa" extends="ovs"
manufacturer="Corsa" hwVersion="emulation" swVersion="0.0.0">
......
......@@ -20,6 +20,7 @@ import io.netty.channel.Channel;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
......@@ -28,10 +29,14 @@ import java.util.concurrent.ExecutionException;
import org.onlab.packet.IpAddress;
import org.onosproject.ovsdb.controller.OvsdbBridge;
import org.onosproject.ovsdb.controller.OvsdbBridgeName;
import org.onosproject.ovsdb.controller.OvsdbClientService;
import org.onosproject.ovsdb.controller.OvsdbConstant;
import org.onosproject.ovsdb.controller.OvsdbDatapathId;
import org.onosproject.ovsdb.controller.OvsdbNodeId;
import org.onosproject.ovsdb.controller.OvsdbPort;
import org.onosproject.ovsdb.controller.OvsdbPortName;
import org.onosproject.ovsdb.controller.OvsdbPortNumber;
import org.onosproject.ovsdb.controller.OvsdbRowStore;
import org.onosproject.ovsdb.controller.OvsdbStore;
import org.onosproject.ovsdb.controller.OvsdbTableStore;
......@@ -96,9 +101,7 @@ public class DefaultOvsdbClient
.newHashMap();
private final Map<String, DatabaseSchema> schema = Maps.newHashMap();
private final Set<OvsdbPort> ovsdbPorts = new HashSet<OvsdbPort>();
private final Set<OvsdbTunnel> ovsdbTunnels = new HashSet<OvsdbTunnel>();
private final Set<OvsdbBridge> ovsdbBridges = new HashSet<OvsdbBridge>();
/**
* Creates an OvsdbClient.
......@@ -247,9 +250,6 @@ public class DefaultOvsdbClient
for (UUID uuid : ports) {
Row portRow = getRow(OvsdbConstant.DATABASENAME,
OvsdbConstant.PORT, uuid.value());
if (portRow == null) {
continue;
}
Port port = (Port) TableGenerator.getTable(dbSchema, portRow,
OvsdbTable.PORT);
if (port != null && portName.equalsIgnoreCase(port.getName())) {
......@@ -283,9 +283,6 @@ public class DefaultOvsdbClient
for (UUID uuid : interfaces) {
Row intfRow = getRow(OvsdbConstant.DATABASENAME,
OvsdbConstant.INTERFACE, uuid.value());
if (intfRow == null) {
continue;
}
Interface intf = (Interface) TableGenerator
.getTable(dbSchema, intfRow, OvsdbTable.INTERFACE);
if (intf != null && portName.equalsIgnoreCase(intf.getName())) {
......@@ -583,22 +580,10 @@ public class DefaultOvsdbClient
}
if (interfaceUuid != null) {
OvsdbRowStore rowStore = getRowStore(OvsdbConstant.DATABASENAME,
OvsdbConstant.INTERFACE);
if (rowStore == null) {
log.debug("The bridge uuid is null");
return;
}
ConcurrentMap<String, Row> intfTableRows = rowStore.getRowStore();
if (intfTableRows == null) {
log.debug("The bridge uuid is null");
return;
}
Interface tunInterface = (Interface) TableGenerator
.getTable(dbSchema, intfTableRows.get(interfaceUuid),
OvsdbTable.INTERFACE);
.createTable(dbSchema, OvsdbTable.INTERFACE);
if (tunInterface != null) {
tunInterface.setType(OvsdbConstant.TYPEVXLAN);
......@@ -849,16 +834,9 @@ public class DefaultOvsdbClient
ListenableFuture<JsonNode> input = getSchema(dbNames);
if (input != null) {
try {
log.info("input message: {}", input.get().toString());
} catch (InterruptedException e) {
log.warn("Interrupted while waiting to get message");
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
log.error("Exception thrown while to get message");
}
}
return Futures.transform(input, rowFunction);
}
return null;
} else {
return Futures.immediateFuture(databaseSchema);
}
......@@ -1040,11 +1018,47 @@ public class DefaultOvsdbClient
@Override
public Set<OvsdbBridge> getBridges() {
Set<OvsdbBridge> ovsdbBridges = new HashSet<OvsdbBridge>();
OvsdbTableStore tableStore = getTableStore(OvsdbConstant.DATABASENAME);
if (tableStore == null) {
return null;
}
OvsdbRowStore rowStore = tableStore.getRows(OvsdbConstant.BRIDGE);
if (rowStore == null) {
return null;
}
ConcurrentMap<String, Row> rows = rowStore.getRowStore();
for (String uuid : rows.keySet()) {
Row row = getRow(OvsdbConstant.DATABASENAME, OvsdbConstant.BRIDGE,
uuid);
OvsdbBridge ovsdbBridge = getOvsdbBridge(row);
if (ovsdbBridge != null) {
ovsdbBridges.add(ovsdbBridge);
}
}
return ovsdbBridges;
}
@Override
public Set<OvsdbPort> getPorts() {
Set<OvsdbPort> ovsdbPorts = new HashSet<OvsdbPort>();
OvsdbTableStore tableStore = getTableStore(OvsdbConstant.DATABASENAME);
if (tableStore == null) {
return null;
}
OvsdbRowStore rowStore = tableStore.getRows(OvsdbConstant.INTERFACE);
if (rowStore == null) {
return null;
}
ConcurrentMap<String, Row> rows = rowStore.getRowStore();
for (String uuid : rows.keySet()) {
Row row = getRow(OvsdbConstant.DATABASENAME,
OvsdbConstant.INTERFACE, uuid);
OvsdbPort ovsdbPort = getOvsdbPort(row);
if (ovsdbPort != null) {
ovsdbPorts.add(ovsdbPort);
}
}
return ovsdbPorts;
}
......@@ -1053,4 +1067,62 @@ public class DefaultOvsdbClient
return schema.get(dbName);
}
//Gets ovsdb port.
private OvsdbPort getOvsdbPort(Row row) {
DatabaseSchema dbSchema = getDatabaseSchema(OvsdbConstant.DATABASENAME);
Interface intf = (Interface) TableGenerator
.getTable(dbSchema, row, OvsdbTable.INTERFACE);
if (intf == null) {
return null;
}
long ofPort = getOfPort(intf);
String portName = intf.getName();
if ((ofPort < 0) || (portName == null)) {
return null;
}
OvsdbPort ovsdbPort = new OvsdbPort(new OvsdbPortNumber(ofPort),
new OvsdbPortName(portName));
return ovsdbPort;
}
////Gets ovsdb bridge.
private OvsdbBridge getOvsdbBridge(Row row) {
DatabaseSchema dbSchema = getDatabaseSchema(OvsdbConstant.DATABASENAME);
Bridge bridge = (Bridge) TableGenerator.getTable(dbSchema, row,
OvsdbTable.BRIDGE);
if (bridge == null) {
return null;
}
OvsdbSet datapathIdSet = (OvsdbSet) bridge.getDatapathIdColumn().data();
@SuppressWarnings("unchecked")
Set<String> datapathIds = datapathIdSet.set();
if (datapathIds == null || datapathIds.size() == 0) {
return null;
}
String datapathId = (String) datapathIds.toArray()[0];
String bridgeName = bridge.getName();
if ((datapathId == null) || (bridgeName == null)) {
return null;
}
OvsdbBridge ovsdbBridge = new OvsdbBridge(new OvsdbBridgeName(bridgeName),
new OvsdbDatapathId(datapathId));
return ovsdbBridge;
}
//Gets ofPort in the interface.
private long getOfPort(Interface intf) {
OvsdbSet ofPortSet = (OvsdbSet) intf.getOpenFlowPortColumn().data();
@SuppressWarnings("unchecked")
Set<Integer> ofPorts = ofPortSet.set();
while (ofPorts == null || ofPorts.size() <= 0) {
log.debug("The ofport is null in {}", intf.getName());
return -1;
}
// return (long) ofPorts.toArray()[0];
Iterator<Integer> it = ofPorts.iterator();
return Long.parseLong(it.next().toString());
}
}
......