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