Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
| 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 | **/ |
| 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 |
| 47 | * @author readams |
| 48 | * |
| 49 | */ |
| 50 | public class SwitchResourceBase extends ServerResource { |
| 51 | protected static Logger log = LoggerFactory.getLogger(SwitchResourceBase.class); |
| 52 | |
| 53 | public enum REQUESTTYPE { |
| 54 | OFSTATS, |
| 55 | OFFEATURES |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | protected void doInit() throws ResourceException { |
| 60 | super.doInit(); |
| 61 | |
| 62 | } |
| 63 | |
| 64 | @LogMessageDoc(level="ERROR", |
| 65 | message="Failure retrieving statistics from switch {switch}", |
| 66 | explanation="An error occurred while retrieving statistics" + |
| 67 | "from the switch", |
| 68 | recommendation=LogMessageDoc.CHECK_SWITCH + " " + |
| 69 | LogMessageDoc.GENERIC_ACTION) |
| 70 | protected List<OFStatistics> getSwitchStatistics(long switchId, |
| 71 | OFStatisticsType statType) { |
| 72 | IFloodlightProviderService floodlightProvider = |
| 73 | (IFloodlightProviderService)getContext().getAttributes(). |
| 74 | get(IFloodlightProviderService.class.getCanonicalName()); |
| 75 | |
| 76 | IOFSwitch sw = floodlightProvider.getSwitches().get(switchId); |
| 77 | Future<List<OFStatistics>> future; |
| 78 | List<OFStatistics> values = null; |
| 79 | if (sw != null) { |
| 80 | OFStatisticsRequest req = new OFStatisticsRequest(); |
| 81 | req.setStatisticType(statType); |
| 82 | int requestLength = req.getLengthU(); |
| 83 | if (statType == OFStatisticsType.FLOW) { |
| 84 | OFFlowStatisticsRequest specificReq = new OFFlowStatisticsRequest(); |
| 85 | OFMatch match = new OFMatch(); |
| 86 | match.setWildcards(0xffffffff); |
| 87 | specificReq.setMatch(match); |
| 88 | specificReq.setOutPort(OFPort.OFPP_NONE.getValue()); |
| 89 | specificReq.setTableId((byte) 0xff); |
| 90 | req.setStatistics(Collections.singletonList((OFStatistics)specificReq)); |
| 91 | requestLength += specificReq.getLength(); |
| 92 | } else if (statType == OFStatisticsType.AGGREGATE) { |
| 93 | OFAggregateStatisticsRequest specificReq = new OFAggregateStatisticsRequest(); |
| 94 | OFMatch match = new OFMatch(); |
| 95 | match.setWildcards(0xffffffff); |
| 96 | specificReq.setMatch(match); |
| 97 | specificReq.setOutPort(OFPort.OFPP_NONE.getValue()); |
| 98 | specificReq.setTableId((byte) 0xff); |
| 99 | req.setStatistics(Collections.singletonList((OFStatistics)specificReq)); |
| 100 | requestLength += specificReq.getLength(); |
| 101 | } else if (statType == OFStatisticsType.PORT) { |
| 102 | OFPortStatisticsRequest specificReq = new OFPortStatisticsRequest(); |
| 103 | specificReq.setPortNumber((short)OFPort.OFPP_NONE.getValue()); |
| 104 | req.setStatistics(Collections.singletonList((OFStatistics)specificReq)); |
| 105 | requestLength += specificReq.getLength(); |
| 106 | } else if (statType == OFStatisticsType.QUEUE) { |
| 107 | OFQueueStatisticsRequest specificReq = new OFQueueStatisticsRequest(); |
| 108 | specificReq.setPortNumber((short)OFPort.OFPP_ALL.getValue()); |
| 109 | // LOOK! openflowj does not define OFPQ_ALL! pulled this from openflow.h |
| 110 | // note that I haven't seen this work yet though... |
| 111 | specificReq.setQueueId(0xffffffff); |
| 112 | req.setStatistics(Collections.singletonList((OFStatistics)specificReq)); |
| 113 | requestLength += specificReq.getLength(); |
| 114 | } else if (statType == OFStatisticsType.DESC || |
| 115 | statType == OFStatisticsType.TABLE) { |
| 116 | // pass - nothing todo besides set the type above |
| 117 | } |
| 118 | req.setLengthU(requestLength); |
| 119 | try { |
| 120 | future = sw.getStatistics(req); |
| 121 | values = future.get(10, TimeUnit.SECONDS); |
| 122 | } catch (Exception e) { |
| 123 | log.error("Failure retrieving statistics from switch " + sw, e); |
| 124 | } |
| 125 | } |
| 126 | return values; |
| 127 | } |
| 128 | |
| 129 | protected List<OFStatistics> getSwitchStatistics(String switchId, OFStatisticsType statType) { |
| 130 | return getSwitchStatistics(HexString.toLong(switchId), statType); |
| 131 | } |
| 132 | |
| 133 | protected OFFeaturesReply getSwitchFeaturesReply(long switchId) { |
| 134 | IFloodlightProviderService floodlightProvider = |
| 135 | (IFloodlightProviderService)getContext().getAttributes(). |
| 136 | get(IFloodlightProviderService.class.getCanonicalName()); |
| 137 | |
| 138 | IOFSwitch sw = floodlightProvider.getSwitches().get(switchId); |
| 139 | Future<OFFeaturesReply> future; |
| 140 | OFFeaturesReply featuresReply = null; |
| 141 | if (sw != null) { |
| 142 | try { |
| 143 | future = sw.getFeaturesReplyFromSwitch(); |
| 144 | featuresReply = future.get(10, TimeUnit.SECONDS); |
| 145 | } catch (Exception e) { |
| 146 | log.error("Failure getting features reply from switch" + sw, e); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | return featuresReply; |
| 151 | } |
| 152 | |
| 153 | protected OFFeaturesReply getSwitchFeaturesReply(String switchId) { |
| 154 | return getSwitchFeaturesReply(HexString.toLong(switchId)); |
| 155 | } |
| 156 | |
| 157 | } |