Skip to content
Snippets Groups Projects
Commit 3d050e05 authored by Alex Reimers's avatar Alex Reimers
Browse files

Added a DeviceManager REST API. You can use this to see what devices the...

Added a DeviceManager REST API. You can use this to see what devices the controller has learned along with their IPs and attachment points.
parent 0c92ecc5
No related branches found
No related tags found
No related merge requests found
Showing with 184 additions and 1 deletion
...@@ -19,6 +19,9 @@ package net.floodlightcontroller.core.web; ...@@ -19,6 +19,9 @@ package net.floodlightcontroller.core.web;
import java.util.List; import java.util.List;
import net.floodlightcontroller.core.web.serializers.DeviceAttachmentPointJSONSerializer;
import net.floodlightcontroller.core.web.serializers.DeviceJSONSerializer;
import net.floodlightcontroller.core.web.serializers.DeviceNetworkAddressJSONSerializer;
import net.floodlightcontroller.core.web.serializers.EventHistoryAttachmentPointJSONSerializer; import net.floodlightcontroller.core.web.serializers.EventHistoryAttachmentPointJSONSerializer;
import net.floodlightcontroller.core.web.serializers.EventHistoryBaseInfoJSONSerializer; import net.floodlightcontroller.core.web.serializers.EventHistoryBaseInfoJSONSerializer;
import net.floodlightcontroller.core.web.serializers.EventHistoryTopologyClusterJSONSerializer; import net.floodlightcontroller.core.web.serializers.EventHistoryTopologyClusterJSONSerializer;
...@@ -73,6 +76,9 @@ public class JacksonCustomConverter extends JacksonConverter { ...@@ -73,6 +76,9 @@ public class JacksonCustomConverter extends JacksonConverter {
jsonModule.addSerializer(new EventHistoryTopologySwitchJSONSerializer()); jsonModule.addSerializer(new EventHistoryTopologySwitchJSONSerializer());
jsonModule.addSerializer(new EventHistoryTopologyLinkJSONSerializer()); jsonModule.addSerializer(new EventHistoryTopologyLinkJSONSerializer());
jsonModule.addSerializer(new EventHistoryTopologyClusterJSONSerializer()); jsonModule.addSerializer(new EventHistoryTopologyClusterJSONSerializer());
jsonModule.addSerializer(new DeviceJSONSerializer());
jsonModule.addSerializer(new DeviceNetworkAddressJSONSerializer());
jsonModule.addSerializer(new DeviceAttachmentPointJSONSerializer());
jsonObjectMapper.registerModule(jsonModule); jsonObjectMapper.registerModule(jsonModule);
} }
......
package net.floodlightcontroller.core.web.serializers;
import java.io.IOException;
import net.floodlightcontroller.devicemanager.DeviceAttachmentPoint;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;
public class DeviceAttachmentPointJSONSerializer extends JsonSerializer<DeviceAttachmentPoint> {
@Override
public void serialize(DeviceAttachmentPoint dap, JsonGenerator jgen, SerializerProvider sp)
throws IOException, JsonProcessingException {
jgen.writeStartObject();
jgen.writeStringField("switch", dap.getSwitchPort().getSw().getStringId());
jgen.writeNumberField("port", dap.getSwitchPort().getPort());
jgen.writeStringField("last-seen", dap.getLastSeen().toString());
jgen.writeEndObject();
}
@Override
public Class<DeviceAttachmentPoint> handledType() {
return DeviceAttachmentPoint.class;
}
}
package net.floodlightcontroller.core.web.serializers;
import java.io.IOException;
import net.floodlightcontroller.devicemanager.Device;
import net.floodlightcontroller.devicemanager.DeviceAttachmentPoint;
import net.floodlightcontroller.devicemanager.DeviceNetworkAddress;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;
public class DeviceJSONSerializer extends JsonSerializer<Device> {
@Override
public void serialize(Device device, JsonGenerator jgen, SerializerProvider sp)
throws IOException, JsonProcessingException {
jgen.writeStartObject();
jgen.writeStringField("last-seen", device.getLastSeen().toString());
if (device.getVlanId() != null)
jgen.writeNumberField("vlan", device.getVlanId());
else
jgen.writeNullField("vlan");
jgen.writeArrayFieldStart("network-addresses");
for (DeviceNetworkAddress dna : device.getNetworkAddresses()) {
sp.defaultSerializeValue(dna, jgen);
}
jgen.writeEndArray();
jgen.writeArrayFieldStart("attachment-points");
for (DeviceAttachmentPoint dap : device.getAttachmentPoints()) {
sp.defaultSerializeValue(dap, jgen);
}
jgen.writeEndArray();
jgen.writeEndObject();
}
@Override
public Class<Device> handledType() {
return Device.class;
}
}
package net.floodlightcontroller.core.web.serializers;
import java.io.IOException;
import net.floodlightcontroller.devicemanager.DeviceNetworkAddress;
import net.floodlightcontroller.packet.IPv4;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;
public class DeviceNetworkAddressJSONSerializer extends JsonSerializer<DeviceNetworkAddress> {
@Override
public void serialize(DeviceNetworkAddress dna, JsonGenerator jgen, SerializerProvider sp)
throws IOException, JsonProcessingException {
jgen.writeStartObject();
jgen.writeStringField("ip", IPv4.fromIPv4Address(dna.getNetworkAddress()));
jgen.writeStringField("last-seen", dna.getLastSeen().toString());
jgen.writeEndObject();
}
@Override
public Class<DeviceNetworkAddress> handledType() {
return DeviceNetworkAddress.class;
}
}
...@@ -62,6 +62,7 @@ import net.floodlightcontroller.linkdiscovery.SwitchPortTuple; ...@@ -62,6 +62,7 @@ import net.floodlightcontroller.linkdiscovery.SwitchPortTuple;
import net.floodlightcontroller.packet.ARP; import net.floodlightcontroller.packet.ARP;
import net.floodlightcontroller.packet.Ethernet; import net.floodlightcontroller.packet.Ethernet;
import net.floodlightcontroller.packet.IPv4; import net.floodlightcontroller.packet.IPv4;
import net.floodlightcontroller.restserver.IRestApiService;
import net.floodlightcontroller.routing.ForwardingBase; import net.floodlightcontroller.routing.ForwardingBase;
import net.floodlightcontroller.storage.IResultSet; import net.floodlightcontroller.storage.IResultSet;
import net.floodlightcontroller.storage.IStorageSourceListener; import net.floodlightcontroller.storage.IStorageSourceListener;
...@@ -631,6 +632,7 @@ public class DeviceManagerImpl implements IDeviceManagerService, IOFMessageListe ...@@ -631,6 +632,7 @@ public class DeviceManagerImpl implements IDeviceManagerService, IOFMessageListe
protected ITopologyService topology; protected ITopologyService topology;
protected IStorageSourceService storageSource; protected IStorageSourceService storageSource;
protected IThreadPoolService threadPool; protected IThreadPoolService threadPool;
protected IRestApiService restApi;
protected Runnable deviceAgingTimer; protected Runnable deviceAgingTimer;
protected SingletonTask deviceUpdateTask; protected SingletonTask deviceUpdateTask;
...@@ -2156,6 +2158,7 @@ public class DeviceManagerImpl implements IDeviceManagerService, IOFMessageListe ...@@ -2156,6 +2158,7 @@ public class DeviceManagerImpl implements IDeviceManagerService, IOFMessageListe
l.add(ILinkDiscoveryService.class); l.add(ILinkDiscoveryService.class);
l.add(IStorageSourceService.class); l.add(IStorageSourceService.class);
l.add(IThreadPoolService.class); l.add(IThreadPoolService.class);
l.add(IRestApiService.class);
return l; return l;
} }
...@@ -2173,6 +2176,8 @@ public class DeviceManagerImpl implements IDeviceManagerService, IOFMessageListe ...@@ -2173,6 +2176,8 @@ public class DeviceManagerImpl implements IDeviceManagerService, IOFMessageListe
context.getServiceImpl(IStorageSourceService.class); context.getServiceImpl(IStorageSourceService.class);
threadPool = threadPool =
context.getServiceImpl(IThreadPoolService.class); context.getServiceImpl(IThreadPoolService.class);
restApi =
context.getServiceImpl(IRestApiService.class);
// We create this here because there is no ordering guarantee // We create this here because there is no ordering guarantee
this.deviceManagerAware = new HashSet<IDeviceManagerAware>(); this.deviceManagerAware = new HashSet<IDeviceManagerAware>();
...@@ -2221,6 +2226,10 @@ public class DeviceManagerImpl implements IDeviceManagerService, IOFMessageListe ...@@ -2221,6 +2226,10 @@ public class DeviceManagerImpl implements IDeviceManagerService, IOFMessageListe
// Register for switch events // Register for switch events
floodlightProvider.addOFSwitchListener(this); floodlightProvider.addOFSwitchListener(this);
floodlightProvider.addInfoProvider("summary", this); floodlightProvider.addInfoProvider("summary", this);
// Register our REST API
restApi.addRestletRoutable(new DeviceManagerWebRoutable());
// Device and storage aging. // Device and storage aging.
enableDeviceAgingTimer(); enableDeviceAgingTimer();
// Read all our device state (MACs, IPs, attachment points) from storage // Read all our device state (MACs, IPs, attachment points) from storage
......
package net.floodlightcontroller.devicemanager.internal;
import org.restlet.Context;
import org.restlet.Restlet;
import org.restlet.routing.Router;
import net.floodlightcontroller.restserver.RestletRoutable;
/**
* Restlet Routable to handle DeviceManager API
* @author alexreimers
*
*/
public class DeviceManagerWebRoutable implements RestletRoutable {
@Override
public Restlet getRestlet(Context context) {
Router router = new Router(context);
router.attach("/device/{device}/json", DeviceResource.class);
return router;
}
@Override
public String basePath() {
return "/wm/devicemanager";
}
}
package net.floodlightcontroller.devicemanager.internal;
import java.util.HashMap;
import java.util.Map;
import net.floodlightcontroller.devicemanager.Device;
import net.floodlightcontroller.devicemanager.IDeviceManagerService;
import org.openflow.util.HexString;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DeviceResource extends ServerResource {
protected static Logger log =
LoggerFactory.getLogger(DeviceResource.class);
@Get("json")
public Map<String, Device> retrieve() {
Map<String, Device> retMap = new HashMap<String, Device>();
IDeviceManagerService dm =
(IDeviceManagerService)getContext().getAttributes().
get(IDeviceManagerService.class.getCanonicalName());
String param = (String) getRequestAttributes().get("device");
if (param.toLowerCase().equals("all")) {
// Get all devices
for (Device d : dm.getDevices()) {
retMap.put(HexString.toHexString(d.getDataLayerAddress()), d);
}
} else {
// Get device by MAC
Device dev = null;
byte[] devMac = HexString.fromHexString(param);
dev = dm.getDeviceByDataLayerAddress(devMac);
if (dev != null) {
retMap.put(HexString.toHexString(dev.getDataLayerAddress()), dev);
}
}
return retMap;
}
}
floodlight.modules = net.floodlightcontroller.staticflowentry.StaticFlowEntryPusher,\ floodlight.modules = net.floodlightcontroller.staticflowentry.StaticFlowEntryPusher,\
net.floodlightcontroller.forwarding.Forwarding,\
net.floodlightcontroller.jython.JythonDebugInterface,\ net.floodlightcontroller.jython.JythonDebugInterface,\
net.floodlightcontroller.counter.CounterStore,\ net.floodlightcontroller.counter.CounterStore,\
net.floodlightcontroller.perfmon.PktInProcessingTime net.floodlightcontroller.perfmon.PktInProcessingTime
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment