Pavlin Radoslavov | da7ef61 | 2013-10-30 16:12:14 -0700 | [diff] [blame] | 1 | package net.onrc.onos.datagrid.web; |
| 2 | |
| 3 | import java.util.Collection; |
| 4 | |
| 5 | import net.onrc.onos.datagrid.IDatagridService; |
| 6 | import net.onrc.onos.ofcontroller.topology.TopologyElement; |
| 7 | import net.onrc.onos.ofcontroller.util.FlowEntry; |
| 8 | import net.onrc.onos.ofcontroller.util.FlowPath; |
| 9 | |
| 10 | import org.restlet.resource.Get; |
| 11 | import org.restlet.resource.ServerResource; |
| 12 | import org.slf4j.Logger; |
| 13 | import org.slf4j.LoggerFactory; |
| 14 | |
| 15 | /** |
| 16 | * Datagrid REST API implementation: Get the state of a map. |
| 17 | * |
| 18 | * Valid map names: |
| 19 | * - "all" : Get all maps |
| 20 | * - "flow" : Get the Flows |
| 21 | * - "flow-entry" : Get the Flow Entries |
| 22 | * - "topology" : Get the Topology |
| 23 | * |
| 24 | * GET /wm/datagrid/get/map/{map-name}/json |
| 25 | */ |
| 26 | public class GetMapResource extends ServerResource { |
| 27 | protected final static Logger log = LoggerFactory.getLogger(GetMapResource.class); |
| 28 | |
| 29 | /** |
| 30 | * Implement the API. |
| 31 | * |
| 32 | * @return a string with the state of the map(s). |
| 33 | */ |
| 34 | @Get("json") |
| 35 | public String retrieve() { |
| 36 | String result = ""; |
| 37 | |
| 38 | IDatagridService datagridService = |
| 39 | (IDatagridService)getContext().getAttributes(). |
| 40 | get(IDatagridService.class.getCanonicalName()); |
| 41 | |
| 42 | if (datagridService == null) { |
| 43 | log.debug("ONOS Datagrid Service not found"); |
| 44 | return result; |
| 45 | } |
| 46 | |
| 47 | // Extract the arguments |
| 48 | String mapNameStr = (String)getRequestAttributes().get("map-name"); |
| 49 | |
| 50 | log.debug("Get Datagrid Map: " + mapNameStr); |
| 51 | |
| 52 | // |
| 53 | // Get the Flows |
| 54 | // |
| 55 | if (mapNameStr.equals("flow") || mapNameStr.equals("all")) { |
| 56 | Collection<FlowPath> flowPaths = datagridService.getAllFlows(); |
| 57 | result += "Flows:\n"; |
| 58 | for (FlowPath flowPath : flowPaths) { |
| 59 | result += flowPath.toString() + "\n"; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // |
| 64 | // Get the Flow Entries |
| 65 | // |
| 66 | if (mapNameStr.equals("flow-entry") || mapNameStr.equals("all")) { |
| 67 | Collection<FlowEntry> flowEntries = datagridService.getAllFlowEntries(); |
| 68 | result += "Flow Entries:\n"; |
| 69 | for (FlowEntry flowEntry : flowEntries) { |
| 70 | result += flowEntry.toString() + "\n"; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | if (mapNameStr.equals("topology") || mapNameStr.equals("all")) { |
| 75 | Collection<TopologyElement> topologyElements = datagridService.getAllTopologyElements(); |
| 76 | result += "Topology:\n"; |
| 77 | for (TopologyElement topologyElement : topologyElements) { |
Pavlin Radoslavov | c599a8c | 2013-10-31 15:46:32 -0700 | [diff] [blame] | 78 | result += topologyElement.toString() + "\n"; |
Pavlin Radoslavov | da7ef61 | 2013-10-30 16:12:14 -0700 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | return result; |
| 83 | } |
| 84 | } |