Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 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 | **/ |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 17 | |
| 18 | package net.floodlightcontroller.core.web; |
| 19 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 20 | |
| 21 | /** |
| 22 | * Return switch statistics information for all switches |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 23 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 24 | * @author readams |
| 25 | */ |
| 26 | public class AllSwitchStatisticsResource extends SwitchResourceBase { |
Jonathan Hart | c78b8f6 | 2014-08-07 22:31:09 -0700 | [diff] [blame] | 27 | /*protected final static Logger log = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 28 | LoggerFactory.getLogger(AllSwitchStatisticsResource.class); |
| 29 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 30 | @Get("json") |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 31 | public Map<String, Object> retrieve() { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 32 | String statType = (String) getRequestAttributes().get("statType"); |
| 33 | return retrieveInternal(statType); |
| 34 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 35 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 36 | 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 41 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 42 | 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 65 | |
| 66 | IFloodlightProviderService floodlightProvider = |
| 67 | (IFloodlightProviderService) getContext().getAttributes(). |
| 68 | get(IFloodlightProviderService.class.getCanonicalName()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 69 | 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 78 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 79 | // 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 Hart | c78b8f6 | 2014-08-07 22:31:09 -0700 | [diff] [blame] | 81 | // finished the switch has not replied yet and therefore we won't |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 82 | // 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 94 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 95 | // 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 101 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 102 | // if we are done finish early so we don't always get the worst case |
| 103 | if (activeThreads.isEmpty()) { |
| 104 | break; |
| 105 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 106 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 107 | // 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 114 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 115 | return model; |
| 116 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 117 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 118 | 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 124 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 125 | 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 Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 131 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 132 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 133 | public List<OFStatistics> getStatisticsReply() { |
| 134 | return switchReply; |
| 135 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 136 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 137 | public OFFeaturesReply getFeaturesReply() { |
| 138 | return featuresReply; |
| 139 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 140 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 141 | public long getSwitchId() { |
| 142 | return switchId; |
| 143 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 144 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 145 | 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 Hart | c78b8f6 | 2014-08-07 22:31:09 -0700 | [diff] [blame] | 152 | }*/ |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 153 | } |