blob: 5879f3b17610570206ce77e32726c5057ea915a5 [file] [log] [blame]
Pavlin Radoslavov42706b92014-02-28 01:22:55 -08001package net.onrc.onos.datagrid.web;
2
3import java.util.ArrayList;
4import java.util.Collection;
5import java.util.SortedMap;
6import java.util.TreeMap;
7
8import net.onrc.onos.intent.Intent;
9import net.onrc.onos.intent.PathIntent;
10import net.onrc.onos.intent.ShortestPathIntent;
11import net.onrc.onos.intent.Intent.IntentState;
12import net.onrc.onos.intent.IntentMap;
13import net.onrc.onos.intent.runtime.IPathCalcRuntimeService;
14
15import net.onrc.onos.ofcontroller.networkgraph.LinkEvent;
16import net.onrc.onos.ofcontroller.networkgraph.Path;
17import net.onrc.onos.ofcontroller.util.CallerId;
18import net.onrc.onos.ofcontroller.util.Dpid;
19import net.onrc.onos.ofcontroller.util.FlowEntry;
20import net.onrc.onos.ofcontroller.util.FlowId;
21import net.onrc.onos.ofcontroller.util.FlowPath;
22import net.onrc.onos.ofcontroller.util.FlowPathType;
23import net.onrc.onos.ofcontroller.util.FlowPathUserState;
24import net.onrc.onos.ofcontroller.util.Port;
25import net.onrc.onos.ofcontroller.util.SwitchPort;
26
27import org.restlet.resource.Get;
28import org.restlet.resource.ServerResource;
29import org.slf4j.Logger;
30import 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 */
39public 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];
72
73 // Create the Flow Path
74 FlowId flowId = new FlowId(intentId);
75 FlowPath flowPath = new FlowPath();
76 flowPath.setFlowId(flowId);
77 sortedFlowPaths.put(flowPath.flowId().value(), flowPath);
78
79 flowPath.setInstallerId(new CallerId("E"));
80 flowPath.setFlowEntryActions(null);
81 flowPath.setFlowPathType(FlowPathType.FP_TYPE_SHORTEST_PATH);
82 flowPath.setFlowPathUserState(FlowPathUserState.FP_USER_ADD);
83
84 // Setup the Source and Destination DPID and Port
85 SwitchPort srcPort = flowPath.dataPath().srcPort();
86 SwitchPort dstPort = flowPath.dataPath().dstPort();
87 srcPort.setDpid(new Dpid(spIntent.getSrcSwitchDpid()));
88 srcPort.setPort(new Port((short)spIntent.getSrcPortNumber()));
89 dstPort.setDpid(new Dpid(spIntent.getDstSwitchDpid()));
90 dstPort.setPort(new Port((short)spIntent.getDstPortNumber()));
91
92 // Extract the Flow Entries
93 Path path = pathIntent.getPath();
94 FlowEntry flowEntry;
95 ArrayList<FlowEntry> flowEntries = new ArrayList<>();
96 for (LinkEvent linkEvent : path) {
97 Dpid dpid = new Dpid(linkEvent.getSrc().getDpid());
98 flowEntry = new FlowEntry();
99 flowEntry.setDpid(dpid);
100 flowEntries.add(flowEntry);
101 }
102 // Add the final Flow Entry
103 flowEntry = new FlowEntry();
104 flowEntry.setDpid(new Dpid(spIntent.getDstSwitchDpid()));
105 flowEntries.add(flowEntry);
106 flowPath.dataPath().setFlowEntries(flowEntries);
107 }
108
109 // Prepare the return result
110 for (FlowPath flowPath : sortedFlowPaths.values())
111 result.add(flowPath);
112
113 return result;
114 }
115}