blob: 1f5cb122aae9b14031795b788a0ad99e9a247dc1 [file] [log] [blame]
Fahad Naeem Khan5b558f22014-10-16 10:35:20 -07001package net.onrc.onos.apps.segmentrouting.web;
2
Fahad Naeem Khan5b558f22014-10-16 10:35:20 -07003
Fahad Naeem Khan6ead4622014-10-16 12:33:29 -07004
Srikanth Vavilapalli06335d12014-10-19 20:53:25 -07005import static net.onrc.onos.core.topology.web.TopologyResource.eval;
6
Fahad Naeem Khanee5c15a2014-10-17 19:13:51 -07007import java.util.ArrayList;
8import java.util.Collection;
9import java.util.HashMap;
10import java.util.Iterator;
11import java.util.List;
12import java.util.Map;
13
Fahad Naeem Khan5b558f22014-10-16 10:35:20 -070014import net.onrc.onos.core.topology.ITopologyService;
15import net.onrc.onos.core.topology.MutableTopology;
Fahad Naeem Khanee5c15a2014-10-17 19:13:51 -070016import net.onrc.onos.core.topology.Port;
17import net.onrc.onos.core.topology.Switch;
Fahad Naeem Khan6ead4622014-10-16 12:33:29 -070018import net.onrc.onos.core.util.Dpid;
Fahad Naeem Khanee5c15a2014-10-17 19:13:51 -070019import net.sf.json.JSONArray;
20import net.sf.json.JSONObject;
Fahad Naeem Khan5b558f22014-10-16 10:35:20 -070021
Fahad Naeem Khan6ead4622014-10-16 12:33:29 -070022import org.projectfloodlight.openflow.util.HexString;
Fahad Naeem Khan5b558f22014-10-16 10:35:20 -070023import org.restlet.representation.Representation;
24import org.restlet.resource.Get;
Srikanth Vavilapalli06335d12014-10-19 20:53:25 -070025import org.restlet.resource.ServerResource;
Fahad Naeem Khan5b558f22014-10-16 10:35:20 -070026/**
27 * Base class for return router statistics
28 *
29 */
30public class RouterStatisticsResource extends ServerResource {
31 /**
Fahad Naeem Khan6ead4622014-10-16 12:33:29 -070032 * Gets the switches/routers and ports information from the network topology.
Fahad Naeem Khan5b558f22014-10-16 10:35:20 -070033 *
Fahad Naeem Khan6ead4622014-10-16 12:33:29 -070034 * @return a Representation of a Collection of switches/routers from the network
Fahad Naeem Khan5b558f22014-10-16 10:35:20 -070035 * topology. Each switch contains the switch ports.
36 */
37 @Get("json")
38 public Representation retrieve() {
Fahad Naeem Khan6ead4622014-10-16 12:33:29 -070039 String routerId = (String) getRequestAttributes().get("routerId");
40 String statsType = (String) getRequestAttributes().get("statsType");
Fahad Naeem Khan5b558f22014-10-16 10:35:20 -070041 ITopologyService topologyService =
Fahad Naeem Khan6ead4622014-10-16 12:33:29 -070042 (ITopologyService) getContext().getAttributes()
Fahad Naeem Khan5b558f22014-10-16 10:35:20 -070043 .get(ITopologyService.class.getCanonicalName());
44
45 MutableTopology mutableTopology = topologyService.getTopology();
46 mutableTopology.acquireReadLock();
47 try {
Fahad Naeem Khan6ead4622014-10-16 12:33:29 -070048 if (routerId == null && statsType == null){
49 return eval(toRepresentation(mutableTopology.getSwitches(), null));
50 }
Fahad Naeem Khanee5c15a2014-10-17 19:13:51 -070051 else if(routerId != null && statsType.equals("port")){
52 Switch sw = mutableTopology
53 .getSwitch(new Dpid(HexString.toLong(routerId)));
54 if(sw ==null){
55 //TODO: Add exception
56 return null;
57 }
58 Map <String, List<SegmentRouterPortInfo>> result = new HashMap <String, List<SegmentRouterPortInfo>>();
59 List<SegmentRouterPortInfo> listPortInfo = new ArrayList<SegmentRouterPortInfo>();
60 Collection<Port> portList =sw.getPorts();
61 String subnets = null;
62 if (sw.getAllStringAttributes().containsKey("subnets")){
63 subnets = sw.getAllStringAttributes().get("subnets");
64 JSONArray subnetArray = JSONArray.fromObject(subnets);
Srikanth Vavilapalli06335d12014-10-19 20:53:25 -070065 Iterator<Port> pI = portList.iterator();
Fahad Naeem Khanee5c15a2014-10-17 19:13:51 -070066 while(pI.hasNext()){
67 Port p = (Port) pI.next();
68 Iterator sI = subnetArray.iterator();
69 String subnet = null;
70 while(sI.hasNext()){
71 JSONObject portSubnetIp = (JSONObject) sI.next();
72 subnet = null;
73 if(portSubnetIp.getString("portNo").equals(p.getNumber().toString())){
74 subnet = portSubnetIp.getString("subnetIp");
75 break;
76 }
77 }
78 listPortInfo.add( new SegmentRouterPortInfo(subnet,p));
79 }
80 result.put(routerId, listPortInfo);
81 return eval(toRepresentation(result,null));
82 }
83 else{
Srikanth Vavilapalli06335d12014-10-19 20:53:25 -070084 Iterator<Port> pI = portList.iterator();
Fahad Naeem Khanee5c15a2014-10-17 19:13:51 -070085 while(pI.hasNext()){
86 Port p = (Port) pI.next();
87 String subnet = null;
88 listPortInfo.add( new SegmentRouterPortInfo(subnet,p));
89 }
90 result.put(routerId, listPortInfo);
91 return eval(toRepresentation(result,null));
92 }
Fahad Naeem Khan6ead4622014-10-16 12:33:29 -070093 }
Fahad Naeem Khan5b558f22014-10-16 10:35:20 -070094 } finally {
95 mutableTopology.releaseReadLock();
96 }
Fahad Naeem Khanee5c15a2014-10-17 19:13:51 -070097 //Should Never get to this point.
Fahad Naeem Khan6ead4622014-10-16 12:33:29 -070098 return null;
Fahad Naeem Khan5b558f22014-10-16 10:35:20 -070099 }
Fahad Naeem Khan5b558f22014-10-16 10:35:20 -0700100}