Fahad Naeem Khan | d1ac370 | 2014-10-22 15:59:31 -0700 | [diff] [blame] | 1 | package net.onrc.onos.apps.segmentrouting.web; |
| 2 | |
| 3 | |
| 4 | |
| 5 | import static net.onrc.onos.core.topology.web.TopologyResource.eval; |
| 6 | |
| 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 | |
| 14 | import net.onrc.onos.apps.segmentrouting.ISegmentRoutingService; |
| 15 | import net.onrc.onos.core.topology.ITopologyService; |
| 16 | import net.onrc.onos.core.topology.MutableTopology; |
| 17 | import net.onrc.onos.core.topology.Port; |
| 18 | import net.onrc.onos.core.topology.Switch; |
| 19 | import net.onrc.onos.core.util.Dpid; |
| 20 | import net.sf.json.JSONArray; |
| 21 | import net.sf.json.JSONObject; |
| 22 | |
| 23 | import org.projectfloodlight.openflow.util.HexString; |
Fahad Naeem Khan | d1ac370 | 2014-10-22 15:59:31 -0700 | [diff] [blame] | 24 | import org.restlet.resource.Get; |
| 25 | import org.restlet.resource.ServerResource; |
| 26 | /** |
| 27 | * Base class for return router statistics |
| 28 | * |
| 29 | */ |
| 30 | public class SegmentRouterResource extends ServerResource { |
| 31 | /** |
| 32 | * Gets the switches/routers and ports information from the network topology. |
| 33 | * |
| 34 | * @return a Representation of a Collection of switches/routers from the network |
| 35 | * topology. Each switch contains the switch ports. |
| 36 | */ |
| 37 | @Get("json") |
| 38 | public Object retrieve() { |
| 39 | String routerId = (String) getRequestAttributes().get("routerId"); |
| 40 | String statsType = (String) getRequestAttributes().get("statsType"); |
| 41 | ITopologyService topologyService = |
| 42 | (ITopologyService) getContext().getAttributes() |
| 43 | .get(ITopologyService.class.getCanonicalName()); |
| 44 | |
| 45 | MutableTopology mutableTopology = topologyService.getTopology(); |
| 46 | mutableTopology.acquireReadLock(); |
| 47 | try { |
| 48 | if (routerId == null && statsType == null){ |
| 49 | return eval(toRepresentation(mutableTopology.getSwitches(), null)); |
Srikanth Vavilapalli | 204d298 | 2014-10-23 14:58:49 -0700 | [diff] [blame] | 50 | } |
Fahad Naeem Khan | d1ac370 | 2014-10-22 15:59:31 -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 | ISegmentRoutingService segmentRoutingService = |
| 59 | (ISegmentRoutingService) getContext().getAttributes(). |
| 60 | get(ISegmentRoutingService.class.getCanonicalName()); |
| 61 | Map <String, List<SegmentRouterPortInfo>> result = new HashMap <String, List<SegmentRouterPortInfo>>(); |
| 62 | List<SegmentRouterPortInfo> listPortInfo = new ArrayList<SegmentRouterPortInfo>(); |
| 63 | Collection<Port> portList =sw.getPorts(); |
| 64 | String subnets = null; |
| 65 | if (sw.getAllStringAttributes().containsKey("subnets")){ |
| 66 | subnets = sw.getAllStringAttributes().get("subnets"); |
| 67 | JSONArray subnetArray = JSONArray.fromObject(subnets); |
| 68 | Iterator<Port> pI = portList.iterator(); |
| 69 | int nodeSid = Integer.parseInt(sw.getStringAttribute("nodeSid")); |
| 70 | HashMap<Integer, List<Integer>> adjPortInfo = segmentRoutingService.getAdjacencyInfo(nodeSid); |
| 71 | while(pI.hasNext()){ |
| 72 | Port p = pI.next(); |
| 73 | Iterator<Integer> keyIt = adjPortInfo.keySet().iterator(); |
| 74 | Iterator<?> sI = subnetArray.iterator(); |
| 75 | List<Integer> adjacency = new ArrayList<Integer>(); |
| 76 | while(keyIt.hasNext()){ |
| 77 | Integer adj = keyIt.next(); |
| 78 | List<Integer> adjPortList = adjPortInfo.get(adj); |
| 79 | if(adjPortList.contains(Integer.valueOf(p.getNumber().shortValue()))){ |
| 80 | adjacency.add(adj); |
| 81 | } |
| 82 | } |
| 83 | String subnet = null; |
| 84 | while(sI.hasNext()){ |
| 85 | JSONObject portSubnetIp = (JSONObject) sI.next(); |
| 86 | subnet = null; |
| 87 | if(portSubnetIp.getString("portNo").equals(p.getNumber().toString())){ |
| 88 | subnet = portSubnetIp.getString("subnetIp"); |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | listPortInfo.add( new SegmentRouterPortInfo(subnet,p, adjacency)); |
| 93 | } |
| 94 | result.put(routerId, listPortInfo); |
| 95 | return eval(toRepresentation(result,null)); |
| 96 | } |
| 97 | else{ |
| 98 | Iterator<Port> pI = portList.iterator(); |
| 99 | int nodeSid = Integer.parseInt(sw.getStringAttribute("nodeSid")); |
| 100 | HashMap<Integer, List<Integer>> adjPortInfo = segmentRoutingService.getAdjacencyInfo(nodeSid); |
| 101 | while(pI.hasNext()){ |
| 102 | Port p = pI.next(); |
| 103 | String subnet = null; |
| 104 | Iterator<Integer> keyIt = adjPortInfo.keySet().iterator(); |
| 105 | List<Integer> adjacency = new ArrayList<Integer>(); |
| 106 | while(keyIt.hasNext()){ |
| 107 | Integer adj = keyIt.next(); |
| 108 | List<Integer> adjPortList = adjPortInfo.get(adj); |
| 109 | if(adjPortList.contains(Integer.valueOf(p.getNumber().shortValue()))){ |
| 110 | adjacency.add(adj); |
| 111 | } |
| 112 | } |
| 113 | listPortInfo.add( new SegmentRouterPortInfo(subnet,p, adjacency)); |
| 114 | } |
| 115 | result.put(routerId, listPortInfo); |
| 116 | return eval(toRepresentation(result,null)); |
| 117 | } |
| 118 | } |
| 119 | else if(routerId != null && statsType.equals("adjacency")){ |
| 120 | ISegmentRoutingService segmentRoutingService = |
| 121 | (ISegmentRoutingService) getContext().getAttributes(). |
| 122 | get(ISegmentRoutingService.class.getCanonicalName()); |
| 123 | Switch sw = mutableTopology |
| 124 | .getSwitch(new Dpid(HexString.toLong(routerId))); |
| 125 | if(sw ==null){ |
| 126 | //TODO: Add exception |
| 127 | return null; |
| 128 | } |
| 129 | int nodeSid = Integer.parseInt(sw.getStringAttribute("nodeSid")); |
| 130 | HashMap<Integer, List<Integer>> adjPortInfo = segmentRoutingService.getAdjacencyInfo(nodeSid); |
| 131 | Iterator<Integer> aPIt = adjPortInfo.keySet().iterator(); |
| 132 | List<SegmentRouterAdjacencyInfo> result= new ArrayList<SegmentRouterAdjacencyInfo>(); |
| 133 | while(aPIt.hasNext()){ |
| 134 | Integer adj = aPIt.next(); |
| 135 | result.add( new SegmentRouterAdjacencyInfo(adj, |
| 136 | adjPortInfo.get(adj))); |
| 137 | } |
Fahad Naeem Khan | d1ac370 | 2014-10-22 15:59:31 -0700 | [diff] [blame] | 138 | return result; |
| 139 | } |
| 140 | } finally { |
| 141 | mutableTopology.releaseReadLock(); |
| 142 | } |
| 143 | //Should Never get to this point. |
| 144 | return null; |
| 145 | } |
| 146 | } |