blob: 5d70cee226bc7f123bfe7860b5b9e03d155cccf4 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
2* 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**/
17
18package net.floodlightcontroller.core.web;
19
20import java.lang.Thread.State;
21import java.util.ArrayList;
22import java.util.HashMap;
23import java.util.List;
24import java.util.Map;
25
26import net.floodlightcontroller.core.IFloodlightProviderService;
Jonathan Harta99ec672014-04-03 11:30:34 -070027
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080028import org.openflow.protocol.OFFeaturesReply;
29import org.openflow.protocol.statistics.OFStatistics;
30import org.openflow.protocol.statistics.OFStatisticsType;
31import org.openflow.util.HexString;
32import org.restlet.resource.Get;
33import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
35
36/**
37 * Return switch statistics information for all switches
38 * @author readams
39 */
40public class AllSwitchStatisticsResource extends SwitchResourceBase {
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070041 protected final static Logger log =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080042 LoggerFactory.getLogger(AllSwitchStatisticsResource.class);
43
44 @Get("json")
45 public Map<String, Object> retrieve() {
46 String statType = (String) getRequestAttributes().get("statType");
47 return retrieveInternal(statType);
48 }
49
50 public Map<String, Object> retrieveInternal(String statType) {
51 HashMap<String, Object> model = new HashMap<String, Object>();
52
53 OFStatisticsType type = null;
54 REQUESTTYPE rType = null;
55
56 if (statType.equals("port")) {
57 type = OFStatisticsType.PORT;
58 rType = REQUESTTYPE.OFSTATS;
59 } else if (statType.equals("queue")) {
60 type = OFStatisticsType.QUEUE;
61 rType = REQUESTTYPE.OFSTATS;
62 } else if (statType.equals("flow")) {
63 type = OFStatisticsType.FLOW;
64 rType = REQUESTTYPE.OFSTATS;
65 } else if (statType.equals("aggregate")) {
66 type = OFStatisticsType.AGGREGATE;
67 rType = REQUESTTYPE.OFSTATS;
68 } else if (statType.equals("desc")) {
69 type = OFStatisticsType.DESC;
70 rType = REQUESTTYPE.OFSTATS;
71 } else if (statType.equals("table")) {
72 type = OFStatisticsType.TABLE;
73 rType = REQUESTTYPE.OFSTATS;
74 } else if (statType.equals("features")) {
75 rType = REQUESTTYPE.OFFEATURES;
76 } else {
77 return model;
78 }
79
80 IFloodlightProviderService floodlightProvider =
81 (IFloodlightProviderService)getContext().getAttributes().
82 get(IFloodlightProviderService.class.getCanonicalName());
83 Long[] switchDpids = floodlightProvider.getSwitches().keySet().toArray(new Long[0]);
84 List<GetConcurrentStatsThread> activeThreads = new ArrayList<GetConcurrentStatsThread>(switchDpids.length);
85 List<GetConcurrentStatsThread> pendingRemovalThreads = new ArrayList<GetConcurrentStatsThread>();
86 GetConcurrentStatsThread t;
87 for (Long l : switchDpids) {
88 t = new GetConcurrentStatsThread(l, rType, type);
89 activeThreads.add(t);
90 t.start();
91 }
92
93 // Join all the threads after the timeout. Set a hard timeout
94 // of 12 seconds for the threads to finish. If the thread has not
95 // finished the switch has not replied yet and therefore we won't
96 // add the switch's stats to the reply.
97 for (int iSleepCycles = 0; iSleepCycles < 12; iSleepCycles++) {
98 for (GetConcurrentStatsThread curThread : activeThreads) {
99 if (curThread.getState() == State.TERMINATED) {
100 if (rType == REQUESTTYPE.OFSTATS) {
101 model.put(HexString.toHexString(curThread.getSwitchId()), curThread.getStatisticsReply());
102 } else if (rType == REQUESTTYPE.OFFEATURES) {
103 model.put(HexString.toHexString(curThread.getSwitchId()), curThread.getFeaturesReply());
104 }
105 pendingRemovalThreads.add(curThread);
106 }
107 }
108
109 // remove the threads that have completed the queries to the switches
110 for (GetConcurrentStatsThread curThread : pendingRemovalThreads) {
111 activeThreads.remove(curThread);
112 }
113 // clear the list so we don't try to double remove them
114 pendingRemovalThreads.clear();
115
116 // if we are done finish early so we don't always get the worst case
117 if (activeThreads.isEmpty()) {
118 break;
119 }
120
121 // sleep for 1 s here
122 try {
123 Thread.sleep(1000);
124 } catch (InterruptedException e) {
125 log.error("Interrupted while waiting for statistics", e);
126 }
127 }
128
129 return model;
130 }
131
132 protected class GetConcurrentStatsThread extends Thread {
133 private List<OFStatistics> switchReply;
134 private long switchId;
135 private OFStatisticsType statType;
136 private REQUESTTYPE requestType;
137 private OFFeaturesReply featuresReply;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800138
139 public GetConcurrentStatsThread(long switchId, REQUESTTYPE requestType, OFStatisticsType statType) {
140 this.switchId = switchId;
141 this.requestType = requestType;
142 this.statType = statType;
143 this.switchReply = null;
144 this.featuresReply = null;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800145 }
146
147 public List<OFStatistics> getStatisticsReply() {
148 return switchReply;
149 }
150
151 public OFFeaturesReply getFeaturesReply() {
152 return featuresReply;
153 }
154
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800155 public long getSwitchId() {
156 return switchId;
157 }
158
159 public void run() {
160 if ((requestType == REQUESTTYPE.OFSTATS) && (statType != null)) {
161 switchReply = getSwitchStatistics(switchId, statType);
162 } else if (requestType == REQUESTTYPE.OFFEATURES) {
163 featuresReply = getSwitchFeaturesReply(switchId);
164 }
165 }
166 }
167}