Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.datagrid.web; |
Pavlin Radoslavov | 42706b9 | 2014-02-28 01:22:55 -0800 | [diff] [blame] | 2 | |
| 3 | import java.util.ArrayList; |
Pavlin Radoslavov | 42706b9 | 2014-02-28 01:22:55 -0800 | [diff] [blame] | 4 | import java.util.SortedMap; |
| 5 | import java.util.TreeMap; |
| 6 | |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 7 | import net.onrc.onos.core.intent.Intent; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 8 | import net.onrc.onos.core.intent.Intent.IntentState; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 9 | import net.onrc.onos.core.intent.IntentMap; |
| 10 | import net.onrc.onos.core.intent.PathIntent; |
| 11 | import net.onrc.onos.core.intent.ShortestPathIntent; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 12 | import net.onrc.onos.core.intent.runtime.IPathCalcRuntimeService; |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 13 | import net.onrc.onos.core.topology.LinkEvent; |
| 14 | import net.onrc.onos.core.topology.Path; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 15 | import net.onrc.onos.core.util.CallerId; |
| 16 | import net.onrc.onos.core.util.Dpid; |
| 17 | import net.onrc.onos.core.util.FlowEntry; |
| 18 | import net.onrc.onos.core.util.FlowId; |
| 19 | import net.onrc.onos.core.util.FlowPath; |
| 20 | import net.onrc.onos.core.util.FlowPathType; |
| 21 | import net.onrc.onos.core.util.FlowPathUserState; |
| 22 | import net.onrc.onos.core.util.Port; |
| 23 | import net.onrc.onos.core.util.SwitchPort; |
Pavlin Radoslavov | 42706b9 | 2014-02-28 01:22:55 -0800 | [diff] [blame] | 24 | |
| 25 | import org.restlet.resource.Get; |
| 26 | import org.restlet.resource.ServerResource; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | /** |
| 31 | * REST API call to get a summary of Flow Paths. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 32 | * <p/> |
Pavlin Radoslavov | 42706b9 | 2014-02-28 01:22:55 -0800 | [diff] [blame] | 33 | * NOTE: This REST API call is needed for the ONOS GUI. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 34 | * <p/> |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 35 | * GET /wm/onos/datagrid/get/ng-flows/summary/json |
Pavlin Radoslavov | 42706b9 | 2014-02-28 01:22:55 -0800 | [diff] [blame] | 36 | */ |
| 37 | public class GetNGFlowsSummaryResource extends ServerResource { |
| 38 | public static final Logger log = LoggerFactory.getLogger(GetNGFlowsSummaryResource.class); |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 39 | |
Pavlin Radoslavov | 42706b9 | 2014-02-28 01:22:55 -0800 | [diff] [blame] | 40 | @Get("json") |
| 41 | public ArrayList<FlowPath> retrieve() { |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 42 | ArrayList<FlowPath> result = new ArrayList<>(); |
| 43 | SortedMap<Long, FlowPath> sortedFlowPaths = new TreeMap<>(); |
Pavlin Radoslavov | 42706b9 | 2014-02-28 01:22:55 -0800 | [diff] [blame] | 44 | |
| 45 | IPathCalcRuntimeService pathRuntime = |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 46 | (IPathCalcRuntimeService) getContext(). |
| 47 | getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName()); |
| 48 | log.debug("Get NG Flows Summary"); |
Pavlin Radoslavov | 42706b9 | 2014-02-28 01:22:55 -0800 | [diff] [blame] | 49 | |
| 50 | |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 51 | IntentMap parentIntentMap = pathRuntime.getHighLevelIntents(); |
| 52 | IntentMap intentMap = pathRuntime.getPathIntents(); |
| 53 | for (Intent parentIntent : parentIntentMap.getAllIntents()) { |
| 54 | // Get only installed Shortest Paths |
| 55 | if (parentIntent.getState() != IntentState.INST_ACK) |
| 56 | continue; |
| 57 | if (!(parentIntent instanceof ShortestPathIntent)) |
| 58 | continue; |
| 59 | ShortestPathIntent spIntent = (ShortestPathIntent) parentIntent; |
Pavlin Radoslavov | 42706b9 | 2014-02-28 01:22:55 -0800 | [diff] [blame] | 60 | |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 61 | // Get the Path Intent |
| 62 | Intent intent = intentMap.getIntent(spIntent.getPathIntentId()); |
| 63 | if (!(intent instanceof PathIntent)) |
| 64 | continue; |
| 65 | PathIntent pathIntent = (PathIntent) intent; |
Pavlin Radoslavov | 42706b9 | 2014-02-28 01:22:55 -0800 | [diff] [blame] | 66 | |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 67 | // Decode the Shortest Path ID |
| 68 | String applnIntentId = parentIntent.getId(); |
| 69 | String intentId = applnIntentId.split(":")[1]; |
Pavlin Radoslavov | 42706b9 | 2014-02-28 01:22:55 -0800 | [diff] [blame] | 70 | |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 71 | // Create the Flow Path |
| 72 | FlowId flowId = new FlowId(intentId); |
| 73 | FlowPath flowPath = new FlowPath(); |
| 74 | flowPath.setFlowId(flowId); |
| 75 | sortedFlowPaths.put(flowPath.flowId().value(), flowPath); |
Pavlin Radoslavov | 42706b9 | 2014-02-28 01:22:55 -0800 | [diff] [blame] | 76 | |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 77 | flowPath.setInstallerId(new CallerId("E")); |
| 78 | flowPath.setFlowEntryActions(null); |
| 79 | flowPath.setFlowPathType(FlowPathType.FP_TYPE_SHORTEST_PATH); |
| 80 | flowPath.setFlowPathUserState(FlowPathUserState.FP_USER_ADD); |
Pavlin Radoslavov | 42706b9 | 2014-02-28 01:22:55 -0800 | [diff] [blame] | 81 | |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 82 | // Setup the Source and Destination DPID and Port |
| 83 | SwitchPort srcPort = flowPath.dataPath().srcPort(); |
| 84 | SwitchPort dstPort = flowPath.dataPath().dstPort(); |
| 85 | srcPort.setDpid(new Dpid(spIntent.getSrcSwitchDpid())); |
| 86 | srcPort.setPort(new Port((short) spIntent.getSrcPortNumber())); |
| 87 | dstPort.setDpid(new Dpid(spIntent.getDstSwitchDpid())); |
| 88 | dstPort.setPort(new Port((short) spIntent.getDstPortNumber())); |
Pavlin Radoslavov | 42706b9 | 2014-02-28 01:22:55 -0800 | [diff] [blame] | 89 | |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 90 | // Extract the Flow Entries |
| 91 | Path path = pathIntent.getPath(); |
| 92 | FlowEntry flowEntry; |
| 93 | ArrayList<FlowEntry> flowEntries = new ArrayList<>(); |
| 94 | for (LinkEvent linkEvent : path) { |
| 95 | Dpid dpid = new Dpid(linkEvent.getSrc().getDpid()); |
| 96 | flowEntry = new FlowEntry(); |
| 97 | flowEntry.setDpid(dpid); |
| 98 | flowEntries.add(flowEntry); |
| 99 | } |
| 100 | // Add the final Flow Entry |
| 101 | flowEntry = new FlowEntry(); |
| 102 | flowEntry.setDpid(new Dpid(spIntent.getDstSwitchDpid())); |
| 103 | flowEntries.add(flowEntry); |
| 104 | flowPath.dataPath().setFlowEntries(flowEntries); |
| 105 | } |
Pavlin Radoslavov | 42706b9 | 2014-02-28 01:22:55 -0800 | [diff] [blame] | 106 | |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 107 | // Prepare the return result |
| 108 | for (FlowPath flowPath : sortedFlowPaths.values()) |
| 109 | result.add(flowPath); |
Pavlin Radoslavov | 42706b9 | 2014-02-28 01:22:55 -0800 | [diff] [blame] | 110 | |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 111 | return result; |
Pavlin Radoslavov | 42706b9 | 2014-02-28 01:22:55 -0800 | [diff] [blame] | 112 | } |
| 113 | } |