tom

Added tools for converting dpid to uri and vice versa.

Modified device and link provider to use the new tools.
......@@ -189,8 +189,7 @@ public class SimpleTopologyManager
TopologyEvent event = store.updateTopology(provider().id(),
topoDescription, reasons);
if (event != null) {
log.info("Topology {} changed due to: {}", event.subject(),
reasons == null ? "initial compute" : reasons);
log.info("Topology {} changed", event.subject());
eventDispatcher.post(event);
}
}
......
......@@ -2,11 +2,20 @@ package org.onlab.onos.of.controller;
import org.projectfloodlight.openflow.util.HexString;
import java.net.URI;
import java.net.URISyntaxException;
import static com.google.common.base.Preconditions.checkArgument;
import static org.onlab.util.Tools.fromHex;
import static org.onlab.util.Tools.toHex;
/**
* The class representing a network switch DPID.
* This class is immutable.
*/
public final class Dpid {
private static final String SCHEME = "of";
private static final long UNKNOWN = 0;
private final long value;
......@@ -71,4 +80,40 @@ public final class Dpid {
hash += 31 * hash + (int) (value ^ value >>> 32);
return hash;
}
/**
* Returns DPID created from the given device URI.
*
* @param uri device URI
* @return dpid
*/
public static Dpid dpid(URI uri) {
checkArgument(uri.getScheme().equals(SCHEME), "Unsupported URI scheme");
return new Dpid(fromHex(uri.getSchemeSpecificPart()));
}
/**
* Produces device URI from the given DPID.
*
* @param dpid device dpid
* @return device URI
*/
public static URI uri(Dpid dpid) {
return uri(dpid.value);
}
/**
* Produces device URI from the given DPID long.
*
* @param value device dpid as long
* @return device URI
*/
public static URI uri(long value) {
try {
return new URI(SCHEME, toHex(value), null);
} catch (URISyntaxException e) {
return null;
}
}
}
......
package org.onlab.onos.provider.of.device.impl;
import static org.onlab.onos.net.DeviceId.deviceId;
import static org.slf4j.LoggerFactory.getLogger;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
......@@ -37,6 +29,14 @@ import org.projectfloodlight.openflow.protocol.OFPortState;
import org.projectfloodlight.openflow.protocol.OFPortStatus;
import org.slf4j.Logger;
import java.util.ArrayList;
import java.util.List;
import static org.onlab.onos.net.DeviceId.deviceId;
import static org.onlab.onos.of.controller.Dpid.dpid;
import static org.onlab.onos.of.controller.Dpid.uri;
import static org.slf4j.LoggerFactory.getLogger;
/**
* Provider which uses an OpenFlow controller to detect network
* infrastructure devices.
......@@ -76,8 +76,7 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr
@Deactivate
public void deactivate() {
for (OpenFlowSwitch sw : controller.getSwitches()) {
providerService.deviceDisconnected(DeviceId.deviceId("of:"
+ Long.toHexString(sw.getId())));
providerService.deviceDisconnected(DeviceId.deviceId(uri(sw.getId())));
}
providerRegistry.unregister(this);
controller.removeListener(listener);
......@@ -95,16 +94,13 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr
public void roleChanged(Device device, MastershipRole newRole) {
switch (newRole) {
case MASTER:
controller.setRole(new Dpid(device.id().uri().getSchemeSpecificPart()),
RoleState.MASTER);
controller.setRole(dpid(device.id().uri()), RoleState.MASTER);
break;
case STANDBY:
controller.setRole(new Dpid(device.id().uri().getSchemeSpecificPart()),
RoleState.EQUAL);
controller.setRole(dpid(device.id().uri()), RoleState.EQUAL);
break;
case NONE:
controller.setRole(new Dpid(device.id().uri().getSchemeSpecificPart()),
RoleState.SLAVE);
controller.setRole(dpid(device.id().uri()), RoleState.SLAVE);
break;
default:
LOG.error("Unknown Mastership state : {}", newRole);
......@@ -119,17 +115,17 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr
if (providerService == null) {
return;
}
URI uri = buildURI(dpid);
DeviceId did = deviceId(uri(dpid));
OpenFlowSwitch sw = controller.getSwitch(dpid);
DeviceDescription description =
new DefaultDeviceDescription(buildURI(dpid), Device.Type.SWITCH,
new DefaultDeviceDescription(did.uri(), Device.Type.SWITCH,
sw.manfacturerDescription(),
sw.hardwareDescription(),
sw.softwareDescription(),
sw.serialNumber());
providerService.deviceConnected(deviceId(uri), description);
providerService.updatePorts(deviceId(uri), buildPortDescriptions(sw.getPorts()));
providerService.deviceConnected(did, description);
providerService.updatePorts(did, buildPortDescriptions(sw.getPorts()));
}
@Override
......@@ -137,31 +133,13 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr
if (providerService == null) {
return;
}
URI uri = buildURI(dpid);
providerService.deviceDisconnected(deviceId(uri));
providerService.deviceDisconnected(deviceId(uri(dpid)));
}
@Override
public void portChanged(Dpid dpid, OFPortStatus status) {
final PortDescription portDescription = buildPortDescription(status.getDesc());
final URI uri = buildURI(dpid);
providerService.portStatusChanged(deviceId(uri), portDescription);
}
/**
* Given a dpid builds a URI for the device.
*
* @param dpid the dpid to build the uri from
* @return returns a uri of the form of:<dpidHexForm>
*/
private URI buildURI(Dpid dpid) {
URI uri = null;
try {
uri = new URI("of", Long.toHexString(dpid.value()), null);
} catch (URISyntaxException e) {
LOG.warn("URI construction for device {} failed.", dpid);
}
return uri;
PortDescription portDescription = buildPortDescription(status.getDesc());
providerService.portStatusChanged(deviceId(uri(dpid)), portDescription);
}
/**
......@@ -172,7 +150,7 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr
*/
private List<PortDescription> buildPortDescriptions(
List<OFPortDesc> ports) {
final List<PortDescription> portDescs = new ArrayList<PortDescription>();
final List<PortDescription> portDescs = new ArrayList<>();
for (OFPortDesc port : ports) {
portDescs.add(buildPortDescription(port));
}
......
......@@ -15,6 +15,7 @@
******************************************************************************/
package org.onlab.onos.provider.of.link.impl;
import static org.onlab.onos.of.controller.Dpid.uri;
import static org.slf4j.LoggerFactory.getLogger;
import java.util.Collections;
......@@ -188,7 +189,7 @@ public class LinkDiscovery implements TimerTask {
}
}
ConnectPoint cp = new ConnectPoint(
DeviceId.deviceId("of:" + Long.toHexString(sw.getId())),
DeviceId.deviceId(uri(sw.getId())),
PortNumber.portNumber(port.getPortNo().getPortNumber()));
linkProvider.linksVanished(cp);
......@@ -228,7 +229,6 @@ public class LinkDiscovery implements TimerTask {
*
* @param port the port
* @return Packet_out message with LLDP data
* @throws PortMappingException
*/
private OFPacketOut createLLDPPacketOut(final OFPortDesc port) {
OFPacketOut.Builder packetOut = this.ofFactory.buildPacketOut();
......@@ -249,7 +249,6 @@ public class LinkDiscovery implements TimerTask {
*
* @param port the port
* @return Packet_out message with LLDP data
* @throws PortMappingException
*/
private OFPacketOut createBDDPPacketOut(final OFPortDesc port) {
OFPacketOut.Builder packetOut = sw.factory().buildPacketOut();
......@@ -295,11 +294,11 @@ public class LinkDiscovery implements TimerTask {
}
this.ackProbe(srcPort);
ConnectPoint src = new ConnectPoint(
DeviceId.deviceId("of:" + Long.toHexString(srcSwitch.getId())),
DeviceId.deviceId(uri(srcSwitch.getId())),
PortNumber.portNumber(srcPort));
ConnectPoint dst = new ConnectPoint(
DeviceId.deviceId("of:" + Long.toHexString(sw.getId())),
DeviceId.deviceId(uri(sw.getId())),
PortNumber.portNumber(dstPort));
LinkDescription ld;
if (ethType == Ethernet.TYPE_BSN) {
......@@ -357,7 +356,7 @@ public class LinkDiscovery implements TimerTask {
final OFPortDesc srcPort = port;
ConnectPoint cp = new ConnectPoint(
DeviceId.deviceId("of:" + Long.toHexString(sw.getId())),
DeviceId.deviceId(uri(sw.getId())),
PortNumber.portNumber(srcPort.getPortNo().getPortNumber()));
linkProvider.linksVanished(cp);
}
......
package org.onlab.util;
import com.google.common.base.Strings;
import com.google.common.primitives.UnsignedLongs;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.util.concurrent.ThreadFactory;
......@@ -20,4 +22,34 @@ public abstract class Tools {
return new ThreadFactoryBuilder().setNameFormat(pattern).build();
}
/**
* Converts a string from hex to long.
*
* @param string hex number in string form; sans 0x
* @return long value
*/
public static long fromHex(String string) {
return UnsignedLongs.parseUnsignedLong(string, 16);
}
/**
* Converts a long value to hex string; 16 wide and sans 0x.
*
* @param value long value
* @return hex string
*/
public static String toHex(long value) {
return Strings.padStart(UnsignedLongs.toString(value, 16), 16, '0');
}
/**
* Converts a long value to hex string; 16 wide and sans 0x.
*
* @param value long value
* @param width string width; zero padded
* @return hex string
*/
public static String toHex(long value, int width) {
return Strings.padStart(UnsignedLongs.toString(value, 16), width, '0');
}
}
......
package org.onlab.util;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Test of the miscellaneous tools.
*/
public class ToolsTest {
@Test
public void fromHex() throws Exception {
assertEquals(15, Tools.fromHex("0f"));
assertEquals(16, Tools.fromHex("10"));
assertEquals(65535, Tools.fromHex("ffff"));
assertEquals(4096, Tools.fromHex("1000"));
assertEquals(0xffffffffffffffffL, Tools.fromHex("ffffffffffffffff"));
}
@Test
public void toHex() throws Exception {
assertEquals("0f", Tools.toHex(15, 2));
assertEquals("ffff", Tools.toHex(65535, 4));
assertEquals("1000", Tools.toHex(4096, 4));
assertEquals("000000000000000f", Tools.toHex(15));
assertEquals("ffffffffffffffff", Tools.toHex(0xffffffffffffffffL));
}
}