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 | |
| 20 | import java.util.Collections; |
| 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.openflow.protocol.OFFeaturesReply; |
| 30 | import org.openflow.protocol.OFMatch; |
| 31 | import org.openflow.protocol.OFPort; |
| 32 | import org.openflow.protocol.OFStatisticsRequest; |
| 33 | import org.openflow.protocol.statistics.OFAggregateStatisticsRequest; |
| 34 | import org.openflow.protocol.statistics.OFFlowStatisticsRequest; |
| 35 | import org.openflow.protocol.statistics.OFPortStatisticsRequest; |
| 36 | import org.openflow.protocol.statistics.OFQueueStatisticsRequest; |
| 37 | import org.openflow.protocol.statistics.OFStatistics; |
| 38 | import org.openflow.protocol.statistics.OFStatisticsType; |
| 39 | import org.openflow.util.HexString; |
| 40 | import org.restlet.resource.ResourceException; |
| 41 | import org.restlet.resource.ServerResource; |
| 42 | import org.slf4j.Logger; |
| 43 | import org.slf4j.LoggerFactory; |
| 44 | |
| 45 | /** |
| 46 | * Base class for server resources related to switches |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 47 | * |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 48 | * @author readams |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 49 | */ |
Ray Milkey | ff73514 | 2014-05-22 19:06:02 -0700 | [diff] [blame] | 50 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 51 | public class SwitchResourceBase extends ServerResource { |
Yuta HIGUCHI | 6ac8d18 | 2013-10-22 15:24:56 -0700 | [diff] [blame] | 52 | protected final static Logger log = LoggerFactory.getLogger(SwitchResourceBase.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 53 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 54 | public enum REQUESTTYPE { |
| 55 | OFSTATS, |
| 56 | OFFEATURES |
| 57 | } |
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 | @Override |
| 60 | protected void doInit() throws ResourceException { |
| 61 | super.doInit(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 62 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 63 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 64 | |
| 65 | @LogMessageDoc(level = "ERROR", |
| 66 | message = "Failure retrieving statistics from switch {switch}", |
| 67 | explanation = "An error occurred while retrieving statistics" + |
| 68 | "from the switch", |
| 69 | recommendation = LogMessageDoc.CHECK_SWITCH + " " + |
| 70 | LogMessageDoc.GENERIC_ACTION) |
| 71 | protected List<OFStatistics> getSwitchStatistics(long switchId, |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 72 | OFStatisticsType statType) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 73 | IFloodlightProviderService floodlightProvider = |
| 74 | (IFloodlightProviderService) getContext().getAttributes(). |
| 75 | get(IFloodlightProviderService.class.getCanonicalName()); |
| 76 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 77 | IOFSwitch sw = floodlightProvider.getSwitches().get(switchId); |
| 78 | Future<List<OFStatistics>> future; |
| 79 | List<OFStatistics> values = null; |
| 80 | if (sw != null) { |
| 81 | OFStatisticsRequest req = new OFStatisticsRequest(); |
| 82 | req.setStatisticType(statType); |
| 83 | int requestLength = req.getLengthU(); |
| 84 | if (statType == OFStatisticsType.FLOW) { |
| 85 | OFFlowStatisticsRequest specificReq = new OFFlowStatisticsRequest(); |
| 86 | OFMatch match = new OFMatch(); |
| 87 | match.setWildcards(0xffffffff); |
| 88 | specificReq.setMatch(match); |
| 89 | specificReq.setOutPort(OFPort.OFPP_NONE.getValue()); |
| 90 | specificReq.setTableId((byte) 0xff); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 91 | req.setStatistics(Collections.singletonList((OFStatistics) specificReq)); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 92 | requestLength += specificReq.getLength(); |
| 93 | } else if (statType == OFStatisticsType.AGGREGATE) { |
| 94 | OFAggregateStatisticsRequest specificReq = new OFAggregateStatisticsRequest(); |
| 95 | OFMatch match = new OFMatch(); |
| 96 | match.setWildcards(0xffffffff); |
| 97 | specificReq.setMatch(match); |
| 98 | specificReq.setOutPort(OFPort.OFPP_NONE.getValue()); |
| 99 | specificReq.setTableId((byte) 0xff); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 100 | req.setStatistics(Collections.singletonList((OFStatistics) specificReq)); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 101 | requestLength += specificReq.getLength(); |
| 102 | } else if (statType == OFStatisticsType.PORT) { |
| 103 | OFPortStatisticsRequest specificReq = new OFPortStatisticsRequest(); |
Ray Milkey | ff73514 | 2014-05-22 19:06:02 -0700 | [diff] [blame] | 104 | specificReq.setPortNumber(OFPort.OFPP_NONE.getValue()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 105 | req.setStatistics(Collections.singletonList((OFStatistics) specificReq)); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 106 | requestLength += specificReq.getLength(); |
| 107 | } else if (statType == OFStatisticsType.QUEUE) { |
| 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 |
| 118 | } |
| 119 | req.setLengthU(requestLength); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 120 | // XXX S fix when we fix stats |
| 121 | /*try { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 122 | future = sw.getStatistics(req); |
| 123 | values = future.get(10, TimeUnit.SECONDS); |
| 124 | } catch (Exception e) { |
| 125 | log.error("Failure retrieving statistics from switch " + sw, e); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 126 | }*/ |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 127 | } |
| 128 | return values; |
| 129 | } |
| 130 | |
| 131 | protected List<OFStatistics> getSwitchStatistics(String switchId, OFStatisticsType statType) { |
| 132 | return getSwitchStatistics(HexString.toLong(switchId), statType); |
| 133 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 134 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 135 | protected OFFeaturesReply getSwitchFeaturesReply(long switchId) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 136 | IFloodlightProviderService floodlightProvider = |
| 137 | (IFloodlightProviderService) getContext().getAttributes(). |
| 138 | get(IFloodlightProviderService.class.getCanonicalName()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 139 | |
| 140 | IOFSwitch sw = floodlightProvider.getSwitches().get(switchId); |
| 141 | Future<OFFeaturesReply> future; |
| 142 | OFFeaturesReply featuresReply = null; |
| 143 | if (sw != null) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 144 | // XXX S fix when we fix stats |
| 145 | /*try { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 146 | future = sw.getFeaturesReplyFromSwitch(); |
| 147 | featuresReply = future.get(10, TimeUnit.SECONDS); |
| 148 | } catch (Exception e) { |
| 149 | log.error("Failure getting features reply from switch" + sw, e); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 150 | }*/ |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | return featuresReply; |
| 154 | } |
| 155 | |
| 156 | protected OFFeaturesReply getSwitchFeaturesReply(String switchId) { |
| 157 | return getSwitchFeaturesReply(HexString.toLong(switchId)); |
| 158 | } |
| 159 | |
| 160 | } |