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