blob: 19f8bf50be1bfeda493429bcf3e12e9debb43783 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001package net.floodlightcontroller.learningswitch;
2
3import java.util.ArrayList;
4import java.util.HashMap;
5import java.util.List;
6import java.util.Map;
7import java.util.Map.Entry;
8
9import net.floodlightcontroller.core.IFloodlightProviderService;
10import net.floodlightcontroller.core.IOFSwitch;
11import net.floodlightcontroller.core.types.MacVlanPair;
12
13import org.openflow.util.HexString;
14import org.restlet.data.Status;
15import org.restlet.resource.Get;
16import org.restlet.resource.ServerResource;
17import org.slf4j.Logger;
18import org.slf4j.LoggerFactory;
19
20public class LearningSwitchTable extends ServerResource {
21 protected static Logger log = LoggerFactory.getLogger(LearningSwitchTable.class);
22
23 protected Map<String, Object> formatTableEntry(MacVlanPair key, short port) {
24 Map<String, Object> entry = new HashMap<String, Object>();
25 entry.put("mac", HexString.toHexString(key.mac));
26 entry.put("vlan", key.vlan);
27 entry.put("port", port);
28 return entry;
29 }
30
31 protected List<Map<String, Object>> getOneSwitchTable(Map<MacVlanPair, Short> switchMap) {
32 List<Map<String, Object>> switchTable = new ArrayList<Map<String, Object>>();
33 for (Entry<MacVlanPair, Short> entry : switchMap.entrySet()) {
34 switchTable.add(formatTableEntry(entry.getKey(), entry.getValue()));
35 }
36 return switchTable;
37 }
38
39 @Get("json")
40 public Map<String, List<Map<String, Object>>> getSwitchTableJson() {
41 ILearningSwitchService lsp =
42 (ILearningSwitchService)getContext().getAttributes().
43 get(ILearningSwitchService.class.getCanonicalName());
44
45 Map<IOFSwitch, Map<MacVlanPair,Short>> table = lsp.getTable();
46 Map<String, List<Map<String, Object>>> allSwitchTableJson = new HashMap<String, List<Map<String, Object>>>();
47
48 String switchId = (String) getRequestAttributes().get("switch");
49 if (switchId.toLowerCase().equals("all")) {
50 for (IOFSwitch sw : table.keySet()) {
51 allSwitchTableJson.put(HexString.toHexString(sw.getId()), getOneSwitchTable(table.get(sw)));
52 }
53 } else {
54 try {
55 IFloodlightProviderService floodlightProvider =
56 (IFloodlightProviderService)getContext().getAttributes().
57 get(IFloodlightProviderService.class.getCanonicalName());
58 long dpid = HexString.toLong(switchId);
59 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
60 allSwitchTableJson.put(HexString.toHexString(sw.getId()), getOneSwitchTable(table.get(sw)));
61 } catch (NumberFormatException e) {
62 log.error("Could not decode switch ID = " + switchId);
63 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
64 }
65 }
66
67 return allSwitchTableJson;
68 }
69}