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 |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 55 | if (parentIntent.getState() != IntentState.INST_ACK) { |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 56 | continue; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 57 | } |
| 58 | if (!(parentIntent instanceof ShortestPathIntent)) { |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 59 | continue; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 60 | } |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 61 | ShortestPathIntent spIntent = (ShortestPathIntent) parentIntent; |
Pavlin Radoslavov | 42706b9 | 2014-02-28 01:22:55 -0800 | [diff] [blame] | 62 | |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 63 | // Get the Path Intent |
| 64 | Intent intent = intentMap.getIntent(spIntent.getPathIntentId()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 65 | if (!(intent instanceof PathIntent)) { |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 66 | continue; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 67 | } |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 68 | PathIntent pathIntent = (PathIntent) intent; |
Pavlin Radoslavov | 42706b9 | 2014-02-28 01:22:55 -0800 | [diff] [blame] | 69 | |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 70 | // Decode the Shortest Path ID |
| 71 | String applnIntentId = parentIntent.getId(); |
| 72 | String intentId = applnIntentId.split(":")[1]; |
Pavlin Radoslavov | 42706b9 | 2014-02-28 01:22:55 -0800 | [diff] [blame] | 73 | |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 74 | // Create the Flow Path |
| 75 | FlowId flowId = new FlowId(intentId); |
| 76 | FlowPath flowPath = new FlowPath(); |
| 77 | flowPath.setFlowId(flowId); |
| 78 | sortedFlowPaths.put(flowPath.flowId().value(), flowPath); |
Pavlin Radoslavov | 42706b9 | 2014-02-28 01:22:55 -0800 | [diff] [blame] | 79 | |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 80 | flowPath.setInstallerId(new CallerId("E")); |
| 81 | flowPath.setFlowEntryActions(null); |
| 82 | flowPath.setFlowPathType(FlowPathType.FP_TYPE_SHORTEST_PATH); |
| 83 | flowPath.setFlowPathUserState(FlowPathUserState.FP_USER_ADD); |
Pavlin Radoslavov | 42706b9 | 2014-02-28 01:22:55 -0800 | [diff] [blame] | 84 | |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 85 | // Setup the Source and Destination DPID and Port |
Pavlin Radoslavov | 29a2a88 | 2014-04-08 17:40:54 -0700 | [diff] [blame] | 86 | Dpid srcDpid = new Dpid(spIntent.getSrcSwitchDpid()); |
| 87 | Port srcPort = new Port((short) spIntent.getSrcPortNumber()); |
| 88 | Dpid dstDpid = new Dpid(spIntent.getDstSwitchDpid()); |
| 89 | Port dstPort = new Port((short) spIntent.getDstPortNumber()); |
| 90 | SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort); |
| 91 | SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort); |
| 92 | flowPath.dataPath().setSrcPort(srcSwitchPort); |
| 93 | flowPath.dataPath().setDstPort(dstSwitchPort); |
Pavlin Radoslavov | 42706b9 | 2014-02-28 01:22:55 -0800 | [diff] [blame] | 94 | |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 95 | // Extract the Flow Entries |
| 96 | Path path = pathIntent.getPath(); |
| 97 | FlowEntry flowEntry; |
| 98 | ArrayList<FlowEntry> flowEntries = new ArrayList<>(); |
| 99 | for (LinkEvent linkEvent : path) { |
| 100 | Dpid dpid = new Dpid(linkEvent.getSrc().getDpid()); |
| 101 | flowEntry = new FlowEntry(); |
| 102 | flowEntry.setDpid(dpid); |
| 103 | flowEntries.add(flowEntry); |
| 104 | } |
| 105 | // Add the final Flow Entry |
| 106 | flowEntry = new FlowEntry(); |
| 107 | flowEntry.setDpid(new Dpid(spIntent.getDstSwitchDpid())); |
| 108 | flowEntries.add(flowEntry); |
| 109 | flowPath.dataPath().setFlowEntries(flowEntries); |
| 110 | } |
Pavlin Radoslavov | 42706b9 | 2014-02-28 01:22:55 -0800 | [diff] [blame] | 111 | |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 112 | // Prepare the return result |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 113 | for (FlowPath flowPath : sortedFlowPaths.values()) { |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 114 | result.add(flowPath); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 115 | } |
Pavlin Radoslavov | 42706b9 | 2014-02-28 01:22:55 -0800 | [diff] [blame] | 116 | |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 117 | return result; |
Pavlin Radoslavov | 42706b9 | 2014-02-28 01:22:55 -0800 | [diff] [blame] | 118 | } |
| 119 | } |