blob: b312ca0beb4d99eeb0daa3f58271a8e4c26ba946 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
Ray Milkey269ffb92014-04-03 14:43:30 -07002 * Copyright 2011, Big Switch Networks, Inc.
3 * Originally created by David Erickson, Stanford University
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
6 * not use this file except in compliance with the License. You may obtain
7 * a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations
15 * under the License.
16 **/
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080017
18package net.floodlightcontroller.core.web;
19
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080020
21/**
22 * Return switch statistics information for all switches
Ray Milkey269ffb92014-04-03 14:43:30 -070023 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080024 * @author readams
25 */
26public class AllSwitchStatisticsResource extends SwitchResourceBase {
Jonathan Hartc78b8f62014-08-07 22:31:09 -070027 /*protected final static Logger log =
Ray Milkey269ffb92014-04-03 14:43:30 -070028 LoggerFactory.getLogger(AllSwitchStatisticsResource.class);
29
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080030 @Get("json")
Ray Milkey269ffb92014-04-03 14:43:30 -070031 public Map<String, Object> retrieve() {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080032 String statType = (String) getRequestAttributes().get("statType");
33 return retrieveInternal(statType);
34 }
Ray Milkey269ffb92014-04-03 14:43:30 -070035
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080036 public Map<String, Object> retrieveInternal(String statType) {
37 HashMap<String, Object> model = new HashMap<String, Object>();
38
39 OFStatisticsType type = null;
40 REQUESTTYPE rType = null;
Ray Milkey269ffb92014-04-03 14:43:30 -070041
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080042 if (statType.equals("port")) {
43 type = OFStatisticsType.PORT;
44 rType = REQUESTTYPE.OFSTATS;
45 } else if (statType.equals("queue")) {
46 type = OFStatisticsType.QUEUE;
47 rType = REQUESTTYPE.OFSTATS;
48 } else if (statType.equals("flow")) {
49 type = OFStatisticsType.FLOW;
50 rType = REQUESTTYPE.OFSTATS;
51 } else if (statType.equals("aggregate")) {
52 type = OFStatisticsType.AGGREGATE;
53 rType = REQUESTTYPE.OFSTATS;
54 } else if (statType.equals("desc")) {
55 type = OFStatisticsType.DESC;
56 rType = REQUESTTYPE.OFSTATS;
57 } else if (statType.equals("table")) {
58 type = OFStatisticsType.TABLE;
59 rType = REQUESTTYPE.OFSTATS;
60 } else if (statType.equals("features")) {
61 rType = REQUESTTYPE.OFFEATURES;
62 } else {
63 return model;
64 }
Ray Milkey269ffb92014-04-03 14:43:30 -070065
66 IFloodlightProviderService floodlightProvider =
67 (IFloodlightProviderService) getContext().getAttributes().
68 get(IFloodlightProviderService.class.getCanonicalName());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080069 Long[] switchDpids = floodlightProvider.getSwitches().keySet().toArray(new Long[0]);
70 List<GetConcurrentStatsThread> activeThreads = new ArrayList<GetConcurrentStatsThread>(switchDpids.length);
71 List<GetConcurrentStatsThread> pendingRemovalThreads = new ArrayList<GetConcurrentStatsThread>();
72 GetConcurrentStatsThread t;
73 for (Long l : switchDpids) {
74 t = new GetConcurrentStatsThread(l, rType, type);
75 activeThreads.add(t);
76 t.start();
77 }
Ray Milkey269ffb92014-04-03 14:43:30 -070078
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080079 // Join all the threads after the timeout. Set a hard timeout
80 // of 12 seconds for the threads to finish. If the thread has not
Jonathan Hartc78b8f62014-08-07 22:31:09 -070081 // finished the switch has not replied yet and therefore we won't
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080082 // add the switch's stats to the reply.
83 for (int iSleepCycles = 0; iSleepCycles < 12; iSleepCycles++) {
84 for (GetConcurrentStatsThread curThread : activeThreads) {
85 if (curThread.getState() == State.TERMINATED) {
86 if (rType == REQUESTTYPE.OFSTATS) {
87 model.put(HexString.toHexString(curThread.getSwitchId()), curThread.getStatisticsReply());
88 } else if (rType == REQUESTTYPE.OFFEATURES) {
89 model.put(HexString.toHexString(curThread.getSwitchId()), curThread.getFeaturesReply());
90 }
91 pendingRemovalThreads.add(curThread);
92 }
93 }
Ray Milkey269ffb92014-04-03 14:43:30 -070094
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080095 // remove the threads that have completed the queries to the switches
96 for (GetConcurrentStatsThread curThread : pendingRemovalThreads) {
97 activeThreads.remove(curThread);
98 }
99 // clear the list so we don't try to double remove them
100 pendingRemovalThreads.clear();
Ray Milkey269ffb92014-04-03 14:43:30 -0700101
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800102 // if we are done finish early so we don't always get the worst case
103 if (activeThreads.isEmpty()) {
104 break;
105 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700106
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800107 // sleep for 1 s here
108 try {
109 Thread.sleep(1000);
110 } catch (InterruptedException e) {
111 log.error("Interrupted while waiting for statistics", e);
112 }
113 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700114
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800115 return model;
116 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700117
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800118 protected class GetConcurrentStatsThread extends Thread {
119 private List<OFStatistics> switchReply;
120 private long switchId;
121 private OFStatisticsType statType;
122 private REQUESTTYPE requestType;
123 private OFFeaturesReply featuresReply;
Ray Milkey269ffb92014-04-03 14:43:30 -0700124
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800125 public GetConcurrentStatsThread(long switchId, REQUESTTYPE requestType, OFStatisticsType statType) {
126 this.switchId = switchId;
127 this.requestType = requestType;
128 this.statType = statType;
129 this.switchReply = null;
130 this.featuresReply = null;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800131 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700132
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800133 public List<OFStatistics> getStatisticsReply() {
134 return switchReply;
135 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700136
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800137 public OFFeaturesReply getFeaturesReply() {
138 return featuresReply;
139 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700140
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800141 public long getSwitchId() {
142 return switchId;
143 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700144
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800145 public void run() {
146 if ((requestType == REQUESTTYPE.OFSTATS) && (statType != null)) {
147 switchReply = getSwitchStatistics(switchId, statType);
148 } else if (requestType == REQUESTTYPE.OFFEATURES) {
149 featuresReply = getSwitchFeaturesReply(switchId);
150 }
151 }
Jonathan Hartc78b8f62014-08-07 22:31:09 -0700152 }*/
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800153}