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