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