blob: cf02a120a242b7448b4891ad89c309283e536df4 [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];
Brian O'Connor274fabf2014-02-28 02:04:17 -080072 // A hack for Flow IDs that are not routable
73 intentId = intentId.replace("F", "");
Pavlin Radoslavov42706b92014-02-28 01:22:55 -080074
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}