blob: d5b534d0b1bfb4271352b59ab6354ec76eb5f766 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
Ray Milkey269ffb92014-04-03 14:43:30 -07002 * 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 Krishnaswamy345ee992012-12-13 20:29:48 -080017
18package net.floodlightcontroller.core.web;
19
20import java.util.Collections;
21import java.util.List;
22import java.util.concurrent.Future;
23import java.util.concurrent.TimeUnit;
24
25import net.floodlightcontroller.core.IFloodlightProviderService;
26import net.floodlightcontroller.core.IOFSwitch;
27import net.floodlightcontroller.core.annotations.LogMessageDoc;
28
29import org.openflow.protocol.OFFeaturesReply;
30import org.openflow.protocol.OFMatch;
31import org.openflow.protocol.OFPort;
32import org.openflow.protocol.OFStatisticsRequest;
33import org.openflow.protocol.statistics.OFAggregateStatisticsRequest;
34import org.openflow.protocol.statistics.OFFlowStatisticsRequest;
35import org.openflow.protocol.statistics.OFPortStatisticsRequest;
36import org.openflow.protocol.statistics.OFQueueStatisticsRequest;
37import org.openflow.protocol.statistics.OFStatistics;
38import org.openflow.protocol.statistics.OFStatisticsType;
39import org.openflow.util.HexString;
40import org.restlet.resource.ResourceException;
41import org.restlet.resource.ServerResource;
42import org.slf4j.Logger;
43import org.slf4j.LoggerFactory;
44
45/**
46 * Base class for server resources related to switches
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080047 *
Ray Milkey269ffb92014-04-03 14:43:30 -070048 * @author readams
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080049 */
Ray Milkeyff735142014-05-22 19:06:02 -070050
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080051public class SwitchResourceBase extends ServerResource {
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070052 protected final static Logger log = LoggerFactory.getLogger(SwitchResourceBase.class);
Ray Milkey269ffb92014-04-03 14:43:30 -070053
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080054 public enum REQUESTTYPE {
55 OFSTATS,
56 OFFEATURES
57 }
Ray Milkey269ffb92014-04-03 14:43:30 -070058
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080059 @Override
60 protected void doInit() throws ResourceException {
61 super.doInit();
Ray Milkey269ffb92014-04-03 14:43:30 -070062
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080063 }
Ray Milkey269ffb92014-04-03 14:43:30 -070064
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 Krishnaswamy345ee992012-12-13 20:29:48 -080072 OFStatisticsType statType) {
Ray Milkey269ffb92014-04-03 14:43:30 -070073 IFloodlightProviderService floodlightProvider =
74 (IFloodlightProviderService) getContext().getAttributes().
75 get(IFloodlightProviderService.class.getCanonicalName());
76
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080077 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 Milkey269ffb92014-04-03 14:43:30 -070091 req.setStatistics(Collections.singletonList((OFStatistics) specificReq));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080092 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 Milkey269ffb92014-04-03 14:43:30 -0700100 req.setStatistics(Collections.singletonList((OFStatistics) specificReq));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800101 requestLength += specificReq.getLength();
102 } else if (statType == OFStatisticsType.PORT) {
103 OFPortStatisticsRequest specificReq = new OFPortStatisticsRequest();
Ray Milkeyff735142014-05-22 19:06:02 -0700104 specificReq.setPortNumber(OFPort.OFPP_NONE.getValue());
Ray Milkey269ffb92014-04-03 14:43:30 -0700105 req.setStatistics(Collections.singletonList((OFStatistics) specificReq));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800106 requestLength += specificReq.getLength();
107 } else if (statType == OFStatisticsType.QUEUE) {
108 OFQueueStatisticsRequest specificReq = new OFQueueStatisticsRequest();
Ray Milkeyff735142014-05-22 19:06:02 -0700109 specificReq.setPortNumber(OFPort.OFPP_ALL.getValue());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800110 // 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 Milkey269ffb92014-04-03 14:43:30 -0700113 req.setStatistics(Collections.singletonList((OFStatistics) specificReq));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800114 requestLength += specificReq.getLength();
115 } else if (statType == OFStatisticsType.DESC ||
Ray Milkey269ffb92014-04-03 14:43:30 -0700116 statType == OFStatisticsType.TABLE) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800117 // pass - nothing todo besides set the type above
118 }
119 req.setLengthU(requestLength);
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700120 // XXX S fix when we fix stats
121 /*try {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800122 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'Connorc67f9fa2014-08-07 18:17:46 -0700126 }*/
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800127 }
128 return values;
129 }
130
131 protected List<OFStatistics> getSwitchStatistics(String switchId, OFStatisticsType statType) {
132 return getSwitchStatistics(HexString.toLong(switchId), statType);
133 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700134
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800135 protected OFFeaturesReply getSwitchFeaturesReply(long switchId) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700136 IFloodlightProviderService floodlightProvider =
137 (IFloodlightProviderService) getContext().getAttributes().
138 get(IFloodlightProviderService.class.getCanonicalName());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800139
140 IOFSwitch sw = floodlightProvider.getSwitches().get(switchId);
141 Future<OFFeaturesReply> future;
142 OFFeaturesReply featuresReply = null;
143 if (sw != null) {
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700144 // XXX S fix when we fix stats
145 /*try {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800146 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'Connorc67f9fa2014-08-07 18:17:46 -0700150 }*/
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800151 }
152
153 return featuresReply;
154 }
155
156 protected OFFeaturesReply getSwitchFeaturesReply(String switchId) {
157 return getSwitchFeaturesReply(HexString.toLong(switchId));
158 }
159
160}