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