blob: 176e357f83812dc9dc17ecdae3c84ee897ae4de2 [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
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080020import org.restlet.resource.ServerResource;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080021
22/**
23 * Base class for server resources related to switches
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080024 *
Ray Milkey269ffb92014-04-03 14:43:30 -070025 * @author readams
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080026 */
Ray Milkeyff735142014-05-22 19:06:02 -070027
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080028public class SwitchResourceBase extends ServerResource {
Jonathan Hartc78b8f62014-08-07 22:31:09 -070029 /*protected final static Logger log = LoggerFactory.getLogger(SwitchResourceBase.class);
Ray Milkey269ffb92014-04-03 14:43:30 -070030
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080031 public enum REQUESTTYPE {
32 OFSTATS,
33 OFFEATURES
34 }
Ray Milkey269ffb92014-04-03 14:43:30 -070035
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080036 @Override
37 protected void doInit() throws ResourceException {
38 super.doInit();
Ray Milkey269ffb92014-04-03 14:43:30 -070039
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080040 }
Ray Milkey269ffb92014-04-03 14:43:30 -070041
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 Krishnaswamy345ee992012-12-13 20:29:48 -080049 OFStatisticsType statType) {
Ray Milkey269ffb92014-04-03 14:43:30 -070050 IFloodlightProviderService floodlightProvider =
51 (IFloodlightProviderService) getContext().getAttributes().
52 get(IFloodlightProviderService.class.getCanonicalName());
53
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080054 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 Milkey269ffb92014-04-03 14:43:30 -070068 req.setStatistics(Collections.singletonList((OFStatistics) specificReq));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080069 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 Milkey269ffb92014-04-03 14:43:30 -070077 req.setStatistics(Collections.singletonList((OFStatistics) specificReq));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080078 requestLength += specificReq.getLength();
79 } else if (statType == OFStatisticsType.PORT) {
80 OFPortStatisticsRequest specificReq = new OFPortStatisticsRequest();
Ray Milkeyff735142014-05-22 19:06:02 -070081 specificReq.setPortNumber(OFPort.OFPP_NONE.getValue());
Ray Milkey269ffb92014-04-03 14:43:30 -070082 req.setStatistics(Collections.singletonList((OFStatistics) specificReq));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080083 requestLength += specificReq.getLength();
84 } else if (statType == OFStatisticsType.QUEUE) {
85 OFQueueStatisticsRequest specificReq = new OFQueueStatisticsRequest();
Ray Milkeyff735142014-05-22 19:06:02 -070086 specificReq.setPortNumber(OFPort.OFPP_ALL.getValue());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080087 // 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 Milkey269ffb92014-04-03 14:43:30 -070090 req.setStatistics(Collections.singletonList((OFStatistics) specificReq));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080091 requestLength += specificReq.getLength();
92 } else if (statType == OFStatisticsType.DESC ||
Ray Milkey269ffb92014-04-03 14:43:30 -070093 statType == OFStatisticsType.TABLE) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080094 // pass - nothing todo besides set the type above
95 }
96 req.setLengthU(requestLength);
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070097 // XXX S fix when we fix stats
Jonathan Hartc78b8f62014-08-07 22:31:09 -070098 try {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080099 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 Hartc78b8f62014-08-07 22:31:09 -0700103 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800104 }
105 return values;
106 }
107
108 protected List<OFStatistics> getSwitchStatistics(String switchId, OFStatisticsType statType) {
109 return getSwitchStatistics(HexString.toLong(switchId), statType);
110 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700111
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800112 protected OFFeaturesReply getSwitchFeaturesReply(long switchId) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700113 IFloodlightProviderService floodlightProvider =
114 (IFloodlightProviderService) getContext().getAttributes().
115 get(IFloodlightProviderService.class.getCanonicalName());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800116
117 IOFSwitch sw = floodlightProvider.getSwitches().get(switchId);
118 Future<OFFeaturesReply> future;
119 OFFeaturesReply featuresReply = null;
120 if (sw != null) {
Jonathan Hartc78b8f62014-08-07 22:31:09 -0700121 // XXX S fix when we fix stats
122 try {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800123 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 Hartc78b8f62014-08-07 22:31:09 -0700127 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800128 }
129
130 return featuresReply;
131 }
132
133 protected OFFeaturesReply getSwitchFeaturesReply(String switchId) {
134 return getSwitchFeaturesReply(HexString.toLong(switchId));
Jonathan Hartc78b8f62014-08-07 22:31:09 -0700135 }*/
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800136
137}