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