Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | package net.floodlightcontroller.learningswitch; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.HashMap; |
| 5 | import java.util.List; |
| 6 | import java.util.Map; |
| 7 | import java.util.Map.Entry; |
| 8 | |
| 9 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 10 | import net.floodlightcontroller.core.IOFSwitch; |
| 11 | import net.floodlightcontroller.core.types.MacVlanPair; |
| 12 | |
| 13 | import org.openflow.util.HexString; |
| 14 | import org.restlet.data.Status; |
| 15 | import org.restlet.resource.Get; |
| 16 | import org.restlet.resource.ServerResource; |
| 17 | import org.slf4j.Logger; |
| 18 | import org.slf4j.LoggerFactory; |
| 19 | |
| 20 | public 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 | } |