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 | |
Srikanth Vavilapalli | b725851 | 2014-09-29 13:24:11 -0700 | [diff] [blame] | 20 | import java.util.ArrayList; |
Srikanth Vavilapalli | a0e6d58 | 2014-09-28 22:42:34 -0700 | [diff] [blame] | 21 | import java.util.List; |
| 22 | import java.util.concurrent.Future; |
| 23 | import java.util.concurrent.TimeUnit; |
| 24 | |
| 25 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 26 | import net.floodlightcontroller.core.IOFSwitch; |
| 27 | import net.floodlightcontroller.core.annotations.LogMessageDoc; |
| 28 | |
| 29 | import org.projectfloodlight.openflow.protocol.OFFeaturesReply; |
Fahad Naeem Khan | 9ae7fa1 | 2014-10-03 14:30:55 -0700 | [diff] [blame] | 30 | import org.projectfloodlight.openflow.protocol.OFGroupDescStatsEntry; |
| 31 | import org.projectfloodlight.openflow.protocol.OFGroupDescStatsReply; |
Fahad Naeem Khan | 8a8daf2 | 2014-10-06 14:07:43 -0700 | [diff] [blame^] | 32 | import org.projectfloodlight.openflow.protocol.OFGroupStatsEntry; |
| 33 | import org.projectfloodlight.openflow.protocol.OFGroupStatsReply; |
Srikanth Vavilapalli | a0e6d58 | 2014-09-28 22:42:34 -0700 | [diff] [blame] | 34 | import org.projectfloodlight.openflow.protocol.OFMatchV3; |
| 35 | import org.projectfloodlight.openflow.protocol.OFOxmList; |
Srikanth Vavilapalli | b725851 | 2014-09-29 13:24:11 -0700 | [diff] [blame] | 36 | import org.projectfloodlight.openflow.protocol.OFPortStatsEntry; |
| 37 | import org.projectfloodlight.openflow.protocol.OFPortStatsReply; |
| 38 | import org.projectfloodlight.openflow.protocol.OFPortStatsRequest; |
Srikanth Vavilapalli | a0e6d58 | 2014-09-28 22:42:34 -0700 | [diff] [blame] | 39 | import org.projectfloodlight.openflow.protocol.OFStatsReply; |
| 40 | import org.projectfloodlight.openflow.protocol.OFStatsRequest; |
| 41 | import org.projectfloodlight.openflow.protocol.OFStatsType; |
| 42 | import org.projectfloodlight.openflow.types.OFPort; |
| 43 | import org.projectfloodlight.openflow.types.TableId; |
| 44 | import org.projectfloodlight.openflow.util.HexString; |
| 45 | import org.restlet.resource.ResourceException; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 46 | import org.restlet.resource.ServerResource; |
Srikanth Vavilapalli | a0e6d58 | 2014-09-28 22:42:34 -0700 | [diff] [blame] | 47 | import org.slf4j.Logger; |
| 48 | import org.slf4j.LoggerFactory; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Base class for server resources related to switches |
Fahad Naeem Khan | 9ae7fa1 | 2014-10-03 14:30:55 -0700 | [diff] [blame] | 52 | * |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 53 | * @author readams |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 54 | */ |
Ray Milkey | ff73514 | 2014-05-22 19:06:02 -0700 | [diff] [blame] | 55 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 56 | public class SwitchResourceBase extends ServerResource { |
Srikanth Vavilapalli | a0e6d58 | 2014-09-28 22:42:34 -0700 | [diff] [blame] | 57 | protected final static Logger log = LoggerFactory.getLogger(SwitchResourceBase.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 58 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 59 | public enum REQUESTTYPE { |
| 60 | OFSTATS, |
| 61 | OFFEATURES |
| 62 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 63 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 64 | @Override |
| 65 | protected void doInit() throws ResourceException { |
| 66 | super.doInit(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 67 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 68 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 69 | |
| 70 | @LogMessageDoc(level = "ERROR", |
| 71 | message = "Failure retrieving statistics from switch {switch}", |
| 72 | explanation = "An error occurred while retrieving statistics" + |
| 73 | "from the switch", |
Fahad Naeem Khan | 9ae7fa1 | 2014-10-03 14:30:55 -0700 | [diff] [blame] | 74 | recommendation = LogMessageDoc.CHECK_SWITCH + " " + |
| 75 | LogMessageDoc.GENERIC_ACTION) |
| 76 | |
| 77 | protected List<?> getSwitchStatistics(long switchId, |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 78 | OFStatsType statType) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 79 | IFloodlightProviderService floodlightProvider = |
| 80 | (IFloodlightProviderService) getContext().getAttributes(). |
Fahad Naeem Khan | 9ae7fa1 | 2014-10-03 14:30:55 -0700 | [diff] [blame] | 81 | get(IFloodlightProviderService.class.getCanonicalName()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 82 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 83 | IOFSwitch sw = floodlightProvider.getSwitches().get(switchId); |
Srikanth Vavilapalli | a0e6d58 | 2014-09-28 22:42:34 -0700 | [diff] [blame] | 84 | Future<List<OFStatsReply>> future; |
| 85 | List<OFStatsReply> values = null; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 86 | if (sw != null) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 87 | OFStatsRequest<?> req = null; |
Srikanth Vavilapalli | a0e6d58 | 2014-09-28 22:42:34 -0700 | [diff] [blame] | 88 | if (statType == OFStatsType.FLOW) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 89 | log.debug("Switch Flow Stats req sent for switch {}", |
| 90 | sw.getStringId()); |
Srikanth Vavilapalli | a0e6d58 | 2014-09-28 22:42:34 -0700 | [diff] [blame] | 91 | OFMatchV3 match = sw.getFactory().buildMatchV3() |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 92 | .setOxmList(OFOxmList.EMPTY).build(); |
| 93 | req = sw.getFactory() |
| 94 | .buildFlowStatsRequest() |
| 95 | .setMatch(match) |
| 96 | .setOutPort(OFPort.ANY) |
| 97 | .setTableId(TableId.ALL) |
| 98 | .setXid(sw.getNextTransactionId()).build(); |
Fahad Naeem Khan | 9ae7fa1 | 2014-10-03 14:30:55 -0700 | [diff] [blame] | 99 | } |
| 100 | else if (statType == OFStatsType.PORT){ |
| 101 | log.debug("Switch Port Stats: req sent for all " |
| 102 | + "ports in switch {}", sw.getStringId()); |
| 103 | List<OFPortStatsEntryMod> portStats = null; |
| 104 | req = sw.getFactory() |
| 105 | .buildPortStatsRequest() |
| 106 | .setPortNo(OFPort.ANY).setXid |
| 107 | (sw.getNextTransactionId()).build(); |
| 108 | try { |
| 109 | future = sw.getStatistics(req); |
| 110 | values = future.get(10, TimeUnit.SECONDS); |
| 111 | portStats = new ArrayList<OFPortStatsEntryMod>(); |
| 112 | for (OFPortStatsEntry entry : ((OFPortStatsReply)values.get(0)).getEntries()) { |
| 113 | OFPortStatsEntryMod entryMod = new OFPortStatsEntryMod(entry); |
| 114 | portStats.add(entryMod); |
| 115 | } |
| 116 | log.debug("Switch Port Stats Entries from switch {} are {}", |
| 117 | sw.getStringId(), portStats); |
| 118 | } catch (Exception e) { |
| 119 | log.error("Failure retrieving statistics from switch " + sw, e); |
| 120 | } |
| 121 | return portStats; |
| 122 | } |
| 123 | else if (statType == OFStatsType.GROUP){ |
| 124 | log.debug("Switch Group Stats: req sent for all " |
| 125 | + "ports in switch {}", sw.getStringId()); |
| 126 | req = sw.getFactory().buildGroupStatsRequest().setXid |
| 127 | (sw.getNextTransactionId()).build(); |
Fahad Naeem Khan | 8a8daf2 | 2014-10-06 14:07:43 -0700 | [diff] [blame^] | 128 | List<OFGroupStatsEntryMod> groupStats = new ArrayList<OFGroupStatsEntryMod>(); |
| 129 | try { |
| 130 | future = sw.getStatistics(req); |
| 131 | values = future.get(10, TimeUnit.SECONDS); |
| 132 | for (OFGroupStatsEntry entry : ((OFGroupStatsReply)values.get(0)).getEntries()) { |
| 133 | OFGroupStatsEntryMod entryMod = new OFGroupStatsEntryMod(entry); |
| 134 | groupStats.add(entryMod); |
| 135 | } |
| 136 | log.debug("Switch Group Stats Entries from switch {} are {}", |
| 137 | sw.getStringId(), groupStats); |
| 138 | } catch (Exception e) { |
| 139 | log.error("Failure retrieving statistics from switch " + sw, e); |
| 140 | } |
| 141 | return groupStats; |
Fahad Naeem Khan | 9ae7fa1 | 2014-10-03 14:30:55 -0700 | [diff] [blame] | 142 | } |
| 143 | else if (statType == OFStatsType.GROUP_DESC){ |
| 144 | log.debug("Switch Group Desc Stats: req sent for all " |
| 145 | + "ports in switch {}", sw.getStringId()); |
| 146 | List<OFGroupDescStatsEntryMod> GroupDescStats= new ArrayList<OFGroupDescStatsEntryMod>(); |
| 147 | req = sw.getFactory().buildGroupDescStatsRequest().setXid |
| 148 | (sw.getNextTransactionId()).build(); |
| 149 | try { |
| 150 | future = sw.getStatistics(req); |
| 151 | values = future.get(10, TimeUnit.SECONDS); |
| 152 | for (OFGroupDescStatsEntry entry : ((OFGroupDescStatsReply)values.get(0)).getEntries()) { |
| 153 | OFGroupDescStatsEntryMod entryMod = new OFGroupDescStatsEntryMod(entry); |
| 154 | GroupDescStats.add(entryMod); |
| 155 | } |
| 156 | log.debug("Switch Group_Desc Stats Entries from switch {} are {}", |
| 157 | sw.getStringId(), GroupDescStats); |
| 158 | } catch (Exception e) { |
| 159 | log.error("Failure retrieving statistics from switch " + sw, e); |
| 160 | } |
| 161 | return GroupDescStats; |
| 162 | } |
| 163 | /*else if (statType == OFStatisticsType.AGGREGATE) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 164 | OFAggregateStatisticsRequest specificReq = new OFAggregateStatisticsRequest(); |
| 165 | OFMatch match = new OFMatch(); |
| 166 | match.setWildcards(0xffffffff); |
| 167 | specificReq.setMatch(match); |
| 168 | specificReq.setOutPort(OFPort.OFPP_NONE.getValue()); |
| 169 | specificReq.setTableId((byte) 0xff); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 170 | req.setStatistics(Collections.singletonList((OFStatistics) specificReq)); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 171 | requestLength += specificReq.getLength(); |
Fahad Naeem Khan | 9ae7fa1 | 2014-10-03 14:30:55 -0700 | [diff] [blame] | 172 | } /*else if (statType == OFStatisticsType.QUEUE) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 173 | OFQueueStatisticsRequest specificReq = new OFQueueStatisticsRequest(); |
Ray Milkey | ff73514 | 2014-05-22 19:06:02 -0700 | [diff] [blame] | 174 | specificReq.setPortNumber(OFPort.OFPP_ALL.getValue()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 175 | // LOOK! openflowj does not define OFPQ_ALL! pulled this from openflow.h |
| 176 | // note that I haven't seen this work yet though... |
| 177 | specificReq.setQueueId(0xffffffff); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 178 | req.setStatistics(Collections.singletonList((OFStatistics) specificReq)); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 179 | requestLength += specificReq.getLength(); |
Fahad Naeem Khan | 9ae7fa1 | 2014-10-03 14:30:55 -0700 | [diff] [blame] | 180 | } else if (statType == OFStatisticsType.DESC || |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 181 | statType == OFStatisticsType.TABLE) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 182 | // pass - nothing todo besides set the type above |
Fahad Naeem Khan | 9ae7fa1 | 2014-10-03 14:30:55 -0700 | [diff] [blame] | 183 | }*/ |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 184 | // XXX S fix when we fix stats |
Jonathan Hart | c78b8f6 | 2014-08-07 22:31:09 -0700 | [diff] [blame] | 185 | try { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 186 | future = sw.getStatistics(req); |
| 187 | values = future.get(10, TimeUnit.SECONDS); |
| 188 | } catch (Exception e) { |
| 189 | log.error("Failure retrieving statistics from switch " + sw, e); |
Jonathan Hart | c78b8f6 | 2014-08-07 22:31:09 -0700 | [diff] [blame] | 190 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 191 | } |
| 192 | return values; |
| 193 | } |
| 194 | |
Srikanth Vavilapalli | b725851 | 2014-09-29 13:24:11 -0700 | [diff] [blame] | 195 | protected Object getSwitchStatistics(String switchId, OFStatsType statType) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 196 | return getSwitchStatistics(HexString.toLong(switchId), statType); |
| 197 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 198 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 199 | protected OFFeaturesReply getSwitchFeaturesReply(long switchId) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 200 | IFloodlightProviderService floodlightProvider = |
| 201 | (IFloodlightProviderService) getContext().getAttributes(). |
Fahad Naeem Khan | 9ae7fa1 | 2014-10-03 14:30:55 -0700 | [diff] [blame] | 202 | get(IFloodlightProviderService.class.getCanonicalName()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 203 | |
| 204 | IOFSwitch sw = floodlightProvider.getSwitches().get(switchId); |
Fahad Naeem Khan | 9ae7fa1 | 2014-10-03 14:30:55 -0700 | [diff] [blame] | 205 | //uture<OFFeaturesReply> future; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 206 | OFFeaturesReply featuresReply = null; |
| 207 | if (sw != null) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 208 | // XXX S fix when we fix stats |
Jonathan Hart | c78b8f6 | 2014-08-07 22:31:09 -0700 | [diff] [blame] | 209 | try { |
Fahad Naeem Khan | 9ae7fa1 | 2014-10-03 14:30:55 -0700 | [diff] [blame] | 210 | //future = sw.getFeaturesReplyFromSwitch(); |
| 211 | //featuresReply = future.get(10, TimeUnit.SECONDS); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 212 | } catch (Exception e) { |
| 213 | log.error("Failure getting features reply from switch" + sw, e); |
Jonathan Hart | c78b8f6 | 2014-08-07 22:31:09 -0700 | [diff] [blame] | 214 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | return featuresReply; |
| 218 | } |
| 219 | |
| 220 | protected OFFeaturesReply getSwitchFeaturesReply(String switchId) { |
| 221 | return getSwitchFeaturesReply(HexString.toLong(switchId)); |
Srikanth Vavilapalli | a0e6d58 | 2014-09-28 22:42:34 -0700 | [diff] [blame] | 222 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 223 | |
| 224 | } |