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