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 ...@@ -189,8 +189,7 @@ public class SimpleTopologyManager
189 TopologyEvent event = store.updateTopology(provider().id(), 189 TopologyEvent event = store.updateTopology(provider().id(),
190 topoDescription, reasons); 190 topoDescription, reasons);
191 if (event != null) { 191 if (event != null) {
192 - log.info("Topology {} changed due to: {}", event.subject(), 192 + log.info("Topology {} changed", event.subject());
193 - reasons == null ? "initial compute" : reasons);
194 eventDispatcher.post(event); 193 eventDispatcher.post(event);
195 } 194 }
196 } 195 }
......
...@@ -2,11 +2,20 @@ package org.onlab.onos.of.controller; ...@@ -2,11 +2,20 @@ package org.onlab.onos.of.controller;
2 2
3 import org.projectfloodlight.openflow.util.HexString; 3 import org.projectfloodlight.openflow.util.HexString;
4 4
5 +import java.net.URI;
6 +import java.net.URISyntaxException;
7 +
8 +import static com.google.common.base.Preconditions.checkArgument;
9 +import static org.onlab.util.Tools.fromHex;
10 +import static org.onlab.util.Tools.toHex;
11 +
5 /** 12 /**
6 * The class representing a network switch DPID. 13 * The class representing a network switch DPID.
7 * This class is immutable. 14 * This class is immutable.
8 */ 15 */
9 public final class Dpid { 16 public final class Dpid {
17 +
18 + private static final String SCHEME = "of";
10 private static final long UNKNOWN = 0; 19 private static final long UNKNOWN = 0;
11 private final long value; 20 private final long value;
12 21
...@@ -71,4 +80,40 @@ public final class Dpid { ...@@ -71,4 +80,40 @@ public final class Dpid {
71 hash += 31 * hash + (int) (value ^ value >>> 32); 80 hash += 31 * hash + (int) (value ^ value >>> 32);
72 return hash; 81 return hash;
73 } 82 }
83 +
84 + /**
85 + * Returns DPID created from the given device URI.
86 + *
87 + * @param uri device URI
88 + * @return dpid
89 + */
90 + public static Dpid dpid(URI uri) {
91 + checkArgument(uri.getScheme().equals(SCHEME), "Unsupported URI scheme");
92 + return new Dpid(fromHex(uri.getSchemeSpecificPart()));
93 + }
94 +
95 + /**
96 + * Produces device URI from the given DPID.
97 + *
98 + * @param dpid device dpid
99 + * @return device URI
100 + */
101 + public static URI uri(Dpid dpid) {
102 + return uri(dpid.value);
103 + }
104 +
105 + /**
106 + * Produces device URI from the given DPID long.
107 + *
108 + * @param value device dpid as long
109 + * @return device URI
110 + */
111 + public static URI uri(long value) {
112 + try {
113 + return new URI(SCHEME, toHex(value), null);
114 + } catch (URISyntaxException e) {
115 + return null;
116 + }
117 + }
118 +
74 } 119 }
......
1 package org.onlab.onos.provider.of.device.impl; 1 package org.onlab.onos.provider.of.device.impl;
2 2
3 -import static org.onlab.onos.net.DeviceId.deviceId;
4 -import static org.slf4j.LoggerFactory.getLogger;
5 -
6 -import java.net.URI;
7 -import java.net.URISyntaxException;
8 -import java.util.ArrayList;
9 -import java.util.List;
10 -
11 import org.apache.felix.scr.annotations.Activate; 3 import org.apache.felix.scr.annotations.Activate;
12 import org.apache.felix.scr.annotations.Component; 4 import org.apache.felix.scr.annotations.Component;
13 import org.apache.felix.scr.annotations.Deactivate; 5 import org.apache.felix.scr.annotations.Deactivate;
...@@ -37,6 +29,14 @@ import org.projectfloodlight.openflow.protocol.OFPortState; ...@@ -37,6 +29,14 @@ import org.projectfloodlight.openflow.protocol.OFPortState;
37 import org.projectfloodlight.openflow.protocol.OFPortStatus; 29 import org.projectfloodlight.openflow.protocol.OFPortStatus;
38 import org.slf4j.Logger; 30 import org.slf4j.Logger;
39 31
32 +import java.util.ArrayList;
33 +import java.util.List;
34 +
35 +import static org.onlab.onos.net.DeviceId.deviceId;
36 +import static org.onlab.onos.of.controller.Dpid.dpid;
37 +import static org.onlab.onos.of.controller.Dpid.uri;
38 +import static org.slf4j.LoggerFactory.getLogger;
39 +
40 /** 40 /**
41 * Provider which uses an OpenFlow controller to detect network 41 * Provider which uses an OpenFlow controller to detect network
42 * infrastructure devices. 42 * infrastructure devices.
...@@ -76,8 +76,7 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr ...@@ -76,8 +76,7 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr
76 @Deactivate 76 @Deactivate
77 public void deactivate() { 77 public void deactivate() {
78 for (OpenFlowSwitch sw : controller.getSwitches()) { 78 for (OpenFlowSwitch sw : controller.getSwitches()) {
79 - providerService.deviceDisconnected(DeviceId.deviceId("of:" 79 + providerService.deviceDisconnected(DeviceId.deviceId(uri(sw.getId())));
80 - + Long.toHexString(sw.getId())));
81 } 80 }
82 providerRegistry.unregister(this); 81 providerRegistry.unregister(this);
83 controller.removeListener(listener); 82 controller.removeListener(listener);
...@@ -94,20 +93,17 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr ...@@ -94,20 +93,17 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr
94 @Override 93 @Override
95 public void roleChanged(Device device, MastershipRole newRole) { 94 public void roleChanged(Device device, MastershipRole newRole) {
96 switch (newRole) { 95 switch (newRole) {
97 - case MASTER: 96 + case MASTER:
98 - controller.setRole(new Dpid(device.id().uri().getSchemeSpecificPart()), 97 + controller.setRole(dpid(device.id().uri()), RoleState.MASTER);
99 - RoleState.MASTER); 98 + break;
100 - break; 99 + case STANDBY:
101 - case STANDBY: 100 + controller.setRole(dpid(device.id().uri()), RoleState.EQUAL);
102 - controller.setRole(new Dpid(device.id().uri().getSchemeSpecificPart()), 101 + break;
103 - RoleState.EQUAL); 102 + case NONE:
104 - break; 103 + controller.setRole(dpid(device.id().uri()), RoleState.SLAVE);
105 - case NONE: 104 + break;
106 - controller.setRole(new Dpid(device.id().uri().getSchemeSpecificPart()), 105 + default:
107 - RoleState.SLAVE); 106 + LOG.error("Unknown Mastership state : {}", newRole);
108 - break;
109 - default:
110 - LOG.error("Unknown Mastership state : {}", newRole);
111 107
112 } 108 }
113 LOG.info("Accepting mastership role change for device {}", device.id()); 109 LOG.info("Accepting mastership role change for device {}", device.id());
...@@ -119,17 +115,17 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr ...@@ -119,17 +115,17 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr
119 if (providerService == null) { 115 if (providerService == null) {
120 return; 116 return;
121 } 117 }
122 - URI uri = buildURI(dpid); 118 + DeviceId did = deviceId(uri(dpid));
123 OpenFlowSwitch sw = controller.getSwitch(dpid); 119 OpenFlowSwitch sw = controller.getSwitch(dpid);
124 120
125 DeviceDescription description = 121 DeviceDescription description =
126 - new DefaultDeviceDescription(buildURI(dpid), Device.Type.SWITCH, 122 + new DefaultDeviceDescription(did.uri(), Device.Type.SWITCH,
127 - sw.manfacturerDescription(), 123 + sw.manfacturerDescription(),
128 - sw.hardwareDescription(), 124 + sw.hardwareDescription(),
129 - sw.softwareDescription(), 125 + sw.softwareDescription(),
130 - sw.serialNumber()); 126 + sw.serialNumber());
131 - providerService.deviceConnected(deviceId(uri), description); 127 + providerService.deviceConnected(did, description);
132 - providerService.updatePorts(deviceId(uri), buildPortDescriptions(sw.getPorts())); 128 + providerService.updatePorts(did, buildPortDescriptions(sw.getPorts()));
133 } 129 }
134 130
135 @Override 131 @Override
...@@ -137,31 +133,13 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr ...@@ -137,31 +133,13 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr
137 if (providerService == null) { 133 if (providerService == null) {
138 return; 134 return;
139 } 135 }
140 - URI uri = buildURI(dpid); 136 + providerService.deviceDisconnected(deviceId(uri(dpid)));
141 - providerService.deviceDisconnected(deviceId(uri));
142 } 137 }
143 138
144 @Override 139 @Override
145 public void portChanged(Dpid dpid, OFPortStatus status) { 140 public void portChanged(Dpid dpid, OFPortStatus status) {
146 - final PortDescription portDescription = buildPortDescription(status.getDesc()); 141 + PortDescription portDescription = buildPortDescription(status.getDesc());
147 - final URI uri = buildURI(dpid); 142 + providerService.portStatusChanged(deviceId(uri(dpid)), portDescription);
148 - providerService.portStatusChanged(deviceId(uri), portDescription);
149 - }
150 -
151 - /**
152 - * Given a dpid builds a URI for the device.
153 - *
154 - * @param dpid the dpid to build the uri from
155 - * @return returns a uri of the form of:<dpidHexForm>
156 - */
157 - private URI buildURI(Dpid dpid) {
158 - URI uri = null;
159 - try {
160 - uri = new URI("of", Long.toHexString(dpid.value()), null);
161 - } catch (URISyntaxException e) {
162 - LOG.warn("URI construction for device {} failed.", dpid);
163 - }
164 - return uri;
165 } 143 }
166 144
167 /** 145 /**
...@@ -172,7 +150,7 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr ...@@ -172,7 +150,7 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr
172 */ 150 */
173 private List<PortDescription> buildPortDescriptions( 151 private List<PortDescription> buildPortDescriptions(
174 List<OFPortDesc> ports) { 152 List<OFPortDesc> ports) {
175 - final List<PortDescription> portDescs = new ArrayList<PortDescription>(); 153 + final List<PortDescription> portDescs = new ArrayList<>();
176 for (OFPortDesc port : ports) { 154 for (OFPortDesc port : ports) {
177 portDescs.add(buildPortDescription(port)); 155 portDescs.add(buildPortDescription(port));
178 } 156 }
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
15 ******************************************************************************/ 15 ******************************************************************************/
16 package org.onlab.onos.provider.of.link.impl; 16 package org.onlab.onos.provider.of.link.impl;
17 17
18 +import static org.onlab.onos.of.controller.Dpid.uri;
18 import static org.slf4j.LoggerFactory.getLogger; 19 import static org.slf4j.LoggerFactory.getLogger;
19 20
20 import java.util.Collections; 21 import java.util.Collections;
...@@ -188,7 +189,7 @@ public class LinkDiscovery implements TimerTask { ...@@ -188,7 +189,7 @@ public class LinkDiscovery implements TimerTask {
188 } 189 }
189 } 190 }
190 ConnectPoint cp = new ConnectPoint( 191 ConnectPoint cp = new ConnectPoint(
191 - DeviceId.deviceId("of:" + Long.toHexString(sw.getId())), 192 + DeviceId.deviceId(uri(sw.getId())),
192 PortNumber.portNumber(port.getPortNo().getPortNumber())); 193 PortNumber.portNumber(port.getPortNo().getPortNumber()));
193 linkProvider.linksVanished(cp); 194 linkProvider.linksVanished(cp);
194 195
...@@ -228,7 +229,6 @@ public class LinkDiscovery implements TimerTask { ...@@ -228,7 +229,6 @@ public class LinkDiscovery implements TimerTask {
228 * 229 *
229 * @param port the port 230 * @param port the port
230 * @return Packet_out message with LLDP data 231 * @return Packet_out message with LLDP data
231 - * @throws PortMappingException
232 */ 232 */
233 private OFPacketOut createLLDPPacketOut(final OFPortDesc port) { 233 private OFPacketOut createLLDPPacketOut(final OFPortDesc port) {
234 OFPacketOut.Builder packetOut = this.ofFactory.buildPacketOut(); 234 OFPacketOut.Builder packetOut = this.ofFactory.buildPacketOut();
...@@ -249,7 +249,6 @@ public class LinkDiscovery implements TimerTask { ...@@ -249,7 +249,6 @@ public class LinkDiscovery implements TimerTask {
249 * 249 *
250 * @param port the port 250 * @param port the port
251 * @return Packet_out message with LLDP data 251 * @return Packet_out message with LLDP data
252 - * @throws PortMappingException
253 */ 252 */
254 private OFPacketOut createBDDPPacketOut(final OFPortDesc port) { 253 private OFPacketOut createBDDPPacketOut(final OFPortDesc port) {
255 OFPacketOut.Builder packetOut = sw.factory().buildPacketOut(); 254 OFPacketOut.Builder packetOut = sw.factory().buildPacketOut();
...@@ -295,11 +294,11 @@ public class LinkDiscovery implements TimerTask { ...@@ -295,11 +294,11 @@ public class LinkDiscovery implements TimerTask {
295 } 294 }
296 this.ackProbe(srcPort); 295 this.ackProbe(srcPort);
297 ConnectPoint src = new ConnectPoint( 296 ConnectPoint src = new ConnectPoint(
298 - DeviceId.deviceId("of:" + Long.toHexString(srcSwitch.getId())), 297 + DeviceId.deviceId(uri(srcSwitch.getId())),
299 PortNumber.portNumber(srcPort)); 298 PortNumber.portNumber(srcPort));
300 299
301 ConnectPoint dst = new ConnectPoint( 300 ConnectPoint dst = new ConnectPoint(
302 - DeviceId.deviceId("of:" + Long.toHexString(sw.getId())), 301 + DeviceId.deviceId(uri(sw.getId())),
303 PortNumber.portNumber(dstPort)); 302 PortNumber.portNumber(dstPort));
304 LinkDescription ld; 303 LinkDescription ld;
305 if (ethType == Ethernet.TYPE_BSN) { 304 if (ethType == Ethernet.TYPE_BSN) {
...@@ -357,7 +356,7 @@ public class LinkDiscovery implements TimerTask { ...@@ -357,7 +356,7 @@ public class LinkDiscovery implements TimerTask {
357 final OFPortDesc srcPort = port; 356 final OFPortDesc srcPort = port;
358 357
359 ConnectPoint cp = new ConnectPoint( 358 ConnectPoint cp = new ConnectPoint(
360 - DeviceId.deviceId("of:" + Long.toHexString(sw.getId())), 359 + DeviceId.deviceId(uri(sw.getId())),
361 PortNumber.portNumber(srcPort.getPortNo().getPortNumber())); 360 PortNumber.portNumber(srcPort.getPortNo().getPortNumber()));
362 linkProvider.linksVanished(cp); 361 linkProvider.linksVanished(cp);
363 } 362 }
......
1 package org.onlab.util; 1 package org.onlab.util;
2 2
3 +import com.google.common.base.Strings;
4 +import com.google.common.primitives.UnsignedLongs;
3 import com.google.common.util.concurrent.ThreadFactoryBuilder; 5 import com.google.common.util.concurrent.ThreadFactoryBuilder;
4 6
5 import java.util.concurrent.ThreadFactory; 7 import java.util.concurrent.ThreadFactory;
...@@ -20,4 +22,34 @@ public abstract class Tools { ...@@ -20,4 +22,34 @@ public abstract class Tools {
20 return new ThreadFactoryBuilder().setNameFormat(pattern).build(); 22 return new ThreadFactoryBuilder().setNameFormat(pattern).build();
21 } 23 }
22 24
25 + /**
26 + * Converts a string from hex to long.
27 + *
28 + * @param string hex number in string form; sans 0x
29 + * @return long value
30 + */
31 + public static long fromHex(String string) {
32 + return UnsignedLongs.parseUnsignedLong(string, 16);
33 + }
34 +
35 + /**
36 + * Converts a long value to hex string; 16 wide and sans 0x.
37 + *
38 + * @param value long value
39 + * @return hex string
40 + */
41 + public static String toHex(long value) {
42 + return Strings.padStart(UnsignedLongs.toString(value, 16), 16, '0');
43 + }
44 +
45 + /**
46 + * Converts a long value to hex string; 16 wide and sans 0x.
47 + *
48 + * @param value long value
49 + * @param width string width; zero padded
50 + * @return hex string
51 + */
52 + public static String toHex(long value, int width) {
53 + return Strings.padStart(UnsignedLongs.toString(value, 16), width, '0');
54 + }
23 } 55 }
......
1 +package org.onlab.util;
2 +
3 +import org.junit.Test;
4 +
5 +import static org.junit.Assert.*;
6 +
7 +/**
8 + * Test of the miscellaneous tools.
9 + */
10 +public class ToolsTest {
11 +
12 + @Test
13 + public void fromHex() throws Exception {
14 + assertEquals(15, Tools.fromHex("0f"));
15 + assertEquals(16, Tools.fromHex("10"));
16 + assertEquals(65535, Tools.fromHex("ffff"));
17 + assertEquals(4096, Tools.fromHex("1000"));
18 + assertEquals(0xffffffffffffffffL, Tools.fromHex("ffffffffffffffff"));
19 + }
20 +
21 + @Test
22 + public void toHex() throws Exception {
23 + assertEquals("0f", Tools.toHex(15, 2));
24 + assertEquals("ffff", Tools.toHex(65535, 4));
25 + assertEquals("1000", Tools.toHex(4096, 4));
26 + assertEquals("000000000000000f", Tools.toHex(15));
27 + assertEquals("ffffffffffffffff", Tools.toHex(0xffffffffffffffffL));
28 +
29 + }
30 +}