blob: 2b38caaa43cef0084a6dee101f8bb58606cdd758 [file] [log] [blame]
Pavlin Radoslavov42706b92014-02-28 01:22:55 -08001package net.onrc.onos.datagrid.web;
2
3import java.util.ArrayList;
Pavlin Radoslavov42706b92014-02-28 01:22:55 -08004import java.util.SortedMap;
5import java.util.TreeMap;
6
7import net.onrc.onos.intent.Intent;
8import net.onrc.onos.intent.PathIntent;
9import net.onrc.onos.intent.ShortestPathIntent;
10import net.onrc.onos.intent.Intent.IntentState;
11import net.onrc.onos.intent.IntentMap;
12import net.onrc.onos.intent.runtime.IPathCalcRuntimeService;
13
14import net.onrc.onos.ofcontroller.networkgraph.LinkEvent;
15import net.onrc.onos.ofcontroller.networkgraph.Path;
16import net.onrc.onos.ofcontroller.util.CallerId;
17import net.onrc.onos.ofcontroller.util.Dpid;
18import net.onrc.onos.ofcontroller.util.FlowEntry;
19import net.onrc.onos.ofcontroller.util.FlowId;
20import net.onrc.onos.ofcontroller.util.FlowPath;
21import net.onrc.onos.ofcontroller.util.FlowPathType;
22import net.onrc.onos.ofcontroller.util.FlowPathUserState;
23import net.onrc.onos.ofcontroller.util.Port;
24import net.onrc.onos.ofcontroller.util.SwitchPort;
25
26import org.restlet.resource.Get;
27import org.restlet.resource.ServerResource;
28import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
31/**
32 * REST API call to get a summary of Flow Paths.
33 *
34 * NOTE: This REST API call is needed for the ONOS GUI.
35 *
Ray Milkey9c8a2132014-04-02 15:16:42 -070036 * GET /wm/onos/datagrid/get/ng-flows/summary/json
Pavlin Radoslavov42706b92014-02-28 01:22:55 -080037 */
38public class GetNGFlowsSummaryResource extends ServerResource {
39 public static final Logger log = LoggerFactory.getLogger(GetNGFlowsSummaryResource.class);
Ray Milkey9c8a2132014-04-02 15:16:42 -070040
Pavlin Radoslavov42706b92014-02-28 01:22:55 -080041 @Get("json")
42 public ArrayList<FlowPath> retrieve() {
Ray Milkey9c8a2132014-04-02 15:16:42 -070043 ArrayList<FlowPath> result = new ArrayList<>();
44 SortedMap<Long, FlowPath> sortedFlowPaths = new TreeMap<>();
Pavlin Radoslavov42706b92014-02-28 01:22:55 -080045
46 IPathCalcRuntimeService pathRuntime =
Ray Milkey9c8a2132014-04-02 15:16:42 -070047 (IPathCalcRuntimeService) getContext().
48 getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName());
49 log.debug("Get NG Flows Summary");
Pavlin Radoslavov42706b92014-02-28 01:22:55 -080050
51
Ray Milkey9c8a2132014-04-02 15:16:42 -070052 IntentMap parentIntentMap = pathRuntime.getHighLevelIntents();
53 IntentMap intentMap = pathRuntime.getPathIntents();
54 for (Intent parentIntent : parentIntentMap.getAllIntents()) {
55 // Get only installed Shortest Paths
56 if (parentIntent.getState() != IntentState.INST_ACK)
57 continue;
58 if (!(parentIntent instanceof ShortestPathIntent))
59 continue;
60 ShortestPathIntent spIntent = (ShortestPathIntent) parentIntent;
Pavlin Radoslavov42706b92014-02-28 01:22:55 -080061
Ray Milkey9c8a2132014-04-02 15:16:42 -070062 // Get the Path Intent
63 Intent intent = intentMap.getIntent(spIntent.getPathIntentId());
64 if (!(intent instanceof PathIntent))
65 continue;
66 PathIntent pathIntent = (PathIntent) intent;
Pavlin Radoslavov42706b92014-02-28 01:22:55 -080067
Ray Milkey9c8a2132014-04-02 15:16:42 -070068 // Decode the Shortest Path ID
69 String applnIntentId = parentIntent.getId();
70 String intentId = applnIntentId.split(":")[1];
Pavlin Radoslavov42706b92014-02-28 01:22:55 -080071
Ray Milkey9c8a2132014-04-02 15:16:42 -070072 // Create the Flow Path
73 FlowId flowId = new FlowId(intentId);
74 FlowPath flowPath = new FlowPath();
75 flowPath.setFlowId(flowId);
76 sortedFlowPaths.put(flowPath.flowId().value(), flowPath);
Pavlin Radoslavov42706b92014-02-28 01:22:55 -080077
Ray Milkey9c8a2132014-04-02 15:16:42 -070078 flowPath.setInstallerId(new CallerId("E"));
79 flowPath.setFlowEntryActions(null);
80 flowPath.setFlowPathType(FlowPathType.FP_TYPE_SHORTEST_PATH);
81 flowPath.setFlowPathUserState(FlowPathUserState.FP_USER_ADD);
Pavlin Radoslavov42706b92014-02-28 01:22:55 -080082
Ray Milkey9c8a2132014-04-02 15:16:42 -070083 // Setup the Source and Destination DPID and Port
84 SwitchPort srcPort = flowPath.dataPath().srcPort();
85 SwitchPort dstPort = flowPath.dataPath().dstPort();
86 srcPort.setDpid(new Dpid(spIntent.getSrcSwitchDpid()));
87 srcPort.setPort(new Port((short) spIntent.getSrcPortNumber()));
88 dstPort.setDpid(new Dpid(spIntent.getDstSwitchDpid()));
89 dstPort.setPort(new Port((short) spIntent.getDstPortNumber()));
Pavlin Radoslavov42706b92014-02-28 01:22:55 -080090
Ray Milkey9c8a2132014-04-02 15:16:42 -070091 // Extract the Flow Entries
92 Path path = pathIntent.getPath();
93 FlowEntry flowEntry;
94 ArrayList<FlowEntry> flowEntries = new ArrayList<>();
95 for (LinkEvent linkEvent : path) {
96 Dpid dpid = new Dpid(linkEvent.getSrc().getDpid());
97 flowEntry = new FlowEntry();
98 flowEntry.setDpid(dpid);
99 flowEntries.add(flowEntry);
100 }
101 // Add the final Flow Entry
102 flowEntry = new FlowEntry();
103 flowEntry.setDpid(new Dpid(spIntent.getDstSwitchDpid()));
104 flowEntries.add(flowEntry);
105 flowPath.dataPath().setFlowEntries(flowEntries);
106 }
Pavlin Radoslavov42706b92014-02-28 01:22:55 -0800107
Ray Milkey9c8a2132014-04-02 15:16:42 -0700108 // Prepare the return result
109 for (FlowPath flowPath : sortedFlowPaths.values())
110 result.add(flowPath);
Pavlin Radoslavov42706b92014-02-28 01:22:55 -0800111
Ray Milkey9c8a2132014-04-02 15:16:42 -0700112 return result;
Pavlin Radoslavov42706b92014-02-28 01:22:55 -0800113 }
114}