Fahad Naeem Khan | 5b558f2 | 2014-10-16 10:35:20 -0700 | [diff] [blame] | 1 | package net.onrc.onos.apps.segmentrouting.web; |
| 2 | |
Fahad Naeem Khan | 5b558f2 | 2014-10-16 10:35:20 -0700 | [diff] [blame] | 3 | |
Fahad Naeem Khan | 6ead462 | 2014-10-16 12:33:29 -0700 | [diff] [blame] | 4 | |
Srikanth Vavilapalli | 06335d1 | 2014-10-19 20:53:25 -0700 | [diff] [blame] | 5 | import static net.onrc.onos.core.topology.web.TopologyResource.eval; |
| 6 | |
Fahad Naeem Khan | ee5c15a | 2014-10-17 19:13:51 -0700 | [diff] [blame] | 7 | import java.util.ArrayList; |
| 8 | import java.util.Collection; |
| 9 | import java.util.HashMap; |
| 10 | import java.util.Iterator; |
| 11 | import java.util.List; |
| 12 | import java.util.Map; |
| 13 | |
Fahad Naeem Khan | 5b558f2 | 2014-10-16 10:35:20 -0700 | [diff] [blame] | 14 | import net.onrc.onos.core.topology.ITopologyService; |
| 15 | import net.onrc.onos.core.topology.MutableTopology; |
Fahad Naeem Khan | ee5c15a | 2014-10-17 19:13:51 -0700 | [diff] [blame] | 16 | import net.onrc.onos.core.topology.Port; |
| 17 | import net.onrc.onos.core.topology.Switch; |
Fahad Naeem Khan | 6ead462 | 2014-10-16 12:33:29 -0700 | [diff] [blame] | 18 | import net.onrc.onos.core.util.Dpid; |
Fahad Naeem Khan | ee5c15a | 2014-10-17 19:13:51 -0700 | [diff] [blame] | 19 | import net.sf.json.JSONArray; |
| 20 | import net.sf.json.JSONObject; |
Fahad Naeem Khan | 5b558f2 | 2014-10-16 10:35:20 -0700 | [diff] [blame] | 21 | |
Fahad Naeem Khan | 6ead462 | 2014-10-16 12:33:29 -0700 | [diff] [blame] | 22 | import org.projectfloodlight.openflow.util.HexString; |
Fahad Naeem Khan | 5b558f2 | 2014-10-16 10:35:20 -0700 | [diff] [blame] | 23 | import org.restlet.representation.Representation; |
| 24 | import org.restlet.resource.Get; |
Srikanth Vavilapalli | 06335d1 | 2014-10-19 20:53:25 -0700 | [diff] [blame] | 25 | import org.restlet.resource.ServerResource; |
Fahad Naeem Khan | 5b558f2 | 2014-10-16 10:35:20 -0700 | [diff] [blame] | 26 | /** |
| 27 | * Base class for return router statistics |
| 28 | * |
| 29 | */ |
| 30 | public class RouterStatisticsResource extends ServerResource { |
| 31 | /** |
Fahad Naeem Khan | 6ead462 | 2014-10-16 12:33:29 -0700 | [diff] [blame] | 32 | * Gets the switches/routers and ports information from the network topology. |
Fahad Naeem Khan | 5b558f2 | 2014-10-16 10:35:20 -0700 | [diff] [blame] | 33 | * |
Fahad Naeem Khan | 6ead462 | 2014-10-16 12:33:29 -0700 | [diff] [blame] | 34 | * @return a Representation of a Collection of switches/routers from the network |
Fahad Naeem Khan | 5b558f2 | 2014-10-16 10:35:20 -0700 | [diff] [blame] | 35 | * topology. Each switch contains the switch ports. |
| 36 | */ |
| 37 | @Get("json") |
| 38 | public Representation retrieve() { |
Fahad Naeem Khan | 6ead462 | 2014-10-16 12:33:29 -0700 | [diff] [blame] | 39 | String routerId = (String) getRequestAttributes().get("routerId"); |
| 40 | String statsType = (String) getRequestAttributes().get("statsType"); |
Fahad Naeem Khan | 5b558f2 | 2014-10-16 10:35:20 -0700 | [diff] [blame] | 41 | ITopologyService topologyService = |
Fahad Naeem Khan | 6ead462 | 2014-10-16 12:33:29 -0700 | [diff] [blame] | 42 | (ITopologyService) getContext().getAttributes() |
Fahad Naeem Khan | 5b558f2 | 2014-10-16 10:35:20 -0700 | [diff] [blame] | 43 | .get(ITopologyService.class.getCanonicalName()); |
| 44 | |
| 45 | MutableTopology mutableTopology = topologyService.getTopology(); |
| 46 | mutableTopology.acquireReadLock(); |
| 47 | try { |
Fahad Naeem Khan | 6ead462 | 2014-10-16 12:33:29 -0700 | [diff] [blame] | 48 | if (routerId == null && statsType == null){ |
| 49 | return eval(toRepresentation(mutableTopology.getSwitches(), null)); |
| 50 | } |
Fahad Naeem Khan | ee5c15a | 2014-10-17 19:13:51 -0700 | [diff] [blame] | 51 | 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 Vavilapalli | 06335d1 | 2014-10-19 20:53:25 -0700 | [diff] [blame] | 65 | Iterator<Port> pI = portList.iterator(); |
Fahad Naeem Khan | ee5c15a | 2014-10-17 19:13:51 -0700 | [diff] [blame] | 66 | 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 Vavilapalli | 06335d1 | 2014-10-19 20:53:25 -0700 | [diff] [blame] | 84 | Iterator<Port> pI = portList.iterator(); |
Fahad Naeem Khan | ee5c15a | 2014-10-17 19:13:51 -0700 | [diff] [blame] | 85 | 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 Khan | 6ead462 | 2014-10-16 12:33:29 -0700 | [diff] [blame] | 93 | } |
Fahad Naeem Khan | 5b558f2 | 2014-10-16 10:35:20 -0700 | [diff] [blame] | 94 | } finally { |
| 95 | mutableTopology.releaseReadLock(); |
| 96 | } |
Fahad Naeem Khan | ee5c15a | 2014-10-17 19:13:51 -0700 | [diff] [blame] | 97 | //Should Never get to this point. |
Fahad Naeem Khan | 6ead462 | 2014-10-16 12:33:29 -0700 | [diff] [blame] | 98 | return null; |
Fahad Naeem Khan | 5b558f2 | 2014-10-16 10:35:20 -0700 | [diff] [blame] | 99 | } |
Fahad Naeem Khan | 5b558f2 | 2014-10-16 10:35:20 -0700 | [diff] [blame] | 100 | } |