blob: 0c2188a858a16ecc2b9a8709d57073d9c24d1ea2 [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
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070020import java.util.List;
21import java.util.concurrent.Future;
22import java.util.concurrent.TimeUnit;
23
24import net.floodlightcontroller.core.IFloodlightProviderService;
25import net.floodlightcontroller.core.IOFSwitch;
26import net.floodlightcontroller.core.annotations.LogMessageDoc;
27
28import org.projectfloodlight.openflow.protocol.OFFeaturesReply;
29import org.projectfloodlight.openflow.protocol.OFMatchV3;
30import org.projectfloodlight.openflow.protocol.OFOxmList;
31import org.projectfloodlight.openflow.protocol.OFStatsReply;
32import org.projectfloodlight.openflow.protocol.OFStatsRequest;
33import org.projectfloodlight.openflow.protocol.OFStatsType;
34import org.projectfloodlight.openflow.types.OFPort;
35import org.projectfloodlight.openflow.types.TableId;
36import org.projectfloodlight.openflow.util.HexString;
37import org.restlet.resource.ResourceException;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080038import org.restlet.resource.ServerResource;
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070039import org.slf4j.Logger;
40import org.slf4j.LoggerFactory;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080041
42/**
43 * Base class for server resources related to switches
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080044 *
Ray Milkey269ffb92014-04-03 14:43:30 -070045 * @author readams
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080046 */
Ray Milkeyff735142014-05-22 19:06:02 -070047
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080048public class SwitchResourceBase extends ServerResource {
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070049 protected final static Logger log = LoggerFactory.getLogger(SwitchResourceBase.class);
Ray Milkey269ffb92014-04-03 14:43:30 -070050
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080051 public enum REQUESTTYPE {
52 OFSTATS,
53 OFFEATURES
54 }
Ray Milkey269ffb92014-04-03 14:43:30 -070055
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080056 @Override
57 protected void doInit() throws ResourceException {
58 super.doInit();
Ray Milkey269ffb92014-04-03 14:43:30 -070059
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080060 }
Ray Milkey269ffb92014-04-03 14:43:30 -070061
62 @LogMessageDoc(level = "ERROR",
63 message = "Failure retrieving statistics from switch {switch}",
64 explanation = "An error occurred while retrieving statistics" +
65 "from the switch",
66 recommendation = LogMessageDoc.CHECK_SWITCH + " " +
67 LogMessageDoc.GENERIC_ACTION)
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070068
69 protected List<OFStatsReply> getSwitchStatistics(long switchId,
70 OFStatsType statType) {
Ray Milkey269ffb92014-04-03 14:43:30 -070071 IFloodlightProviderService floodlightProvider =
72 (IFloodlightProviderService) getContext().getAttributes().
73 get(IFloodlightProviderService.class.getCanonicalName());
74
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080075 IOFSwitch sw = floodlightProvider.getSwitches().get(switchId);
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070076 Future<List<OFStatsReply>> future;
77 List<OFStatsReply> values = null;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080078 if (sw != null) {
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070079 OFStatsRequest<?> req = null;
80 if (statType == OFStatsType.FLOW) {
81 log.debug("Switch Flow Stats req sent for switch {}",
82 sw.getStringId());
83 OFMatchV3 match = sw.getFactory().buildMatchV3()
84 .setOxmList(OFOxmList.EMPTY).build();
85 req = sw.getFactory()
86 .buildFlowStatsRequest()
87 .setMatch(match)
88 .setOutPort(OFPort.ANY)
89 .setTableId(TableId.ALL)
90 .setXid(sw.getNextTransactionId()).build();
91 } /*else if (statType == OFStatisticsType.AGGREGATE) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080092 OFAggregateStatisticsRequest specificReq = new OFAggregateStatisticsRequest();
93 OFMatch match = new OFMatch();
94 match.setWildcards(0xffffffff);
95 specificReq.setMatch(match);
96 specificReq.setOutPort(OFPort.OFPP_NONE.getValue());
97 specificReq.setTableId((byte) 0xff);
Ray Milkey269ffb92014-04-03 14:43:30 -070098 req.setStatistics(Collections.singletonList((OFStatistics) specificReq));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080099 requestLength += specificReq.getLength();
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -0700100 } */else if (statType == OFStatsType.PORT) {
101 log.debug("Switch Port Stats: req sent for all "
102 + "ports in switch {}", sw.getStringId());
103 req = sw.getFactory()
104 .buildPortStatsRequest()
105 .setPortNo(OFPort.ANY).setXid
106 (sw.getNextTransactionId()).build();
107 } /*else if (statType == OFStatisticsType.QUEUE) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800108 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
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -0700118 }*/
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700119 // XXX S fix when we fix stats
Jonathan Hartc78b8f62014-08-07 22:31:09 -0700120 try {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800121 future = sw.getStatistics(req);
122 values = future.get(10, TimeUnit.SECONDS);
123 } catch (Exception e) {
124 log.error("Failure retrieving statistics from switch " + sw, e);
Jonathan Hartc78b8f62014-08-07 22:31:09 -0700125 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800126 }
127 return values;
128 }
129
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -0700130 protected List<OFStatsReply> getSwitchStatistics(String switchId, OFStatsType statType) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800131 return getSwitchStatistics(HexString.toLong(switchId), statType);
132 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700133
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800134 protected OFFeaturesReply getSwitchFeaturesReply(long switchId) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700135 IFloodlightProviderService floodlightProvider =
136 (IFloodlightProviderService) getContext().getAttributes().
137 get(IFloodlightProviderService.class.getCanonicalName());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800138
139 IOFSwitch sw = floodlightProvider.getSwitches().get(switchId);
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -0700140 //uture<OFFeaturesReply> future;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800141 OFFeaturesReply featuresReply = null;
142 if (sw != null) {
Jonathan Hartc78b8f62014-08-07 22:31:09 -0700143 // XXX S fix when we fix stats
144 try {
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -0700145 //future = sw.getFeaturesReplyFromSwitch();
146 //featuresReply = future.get(10, TimeUnit.SECONDS);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800147 } catch (Exception e) {
148 log.error("Failure getting features reply from switch" + sw, e);
Jonathan Hartc78b8f62014-08-07 22:31:09 -0700149 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800150 }
151
152 return featuresReply;
153 }
154
155 protected OFFeaturesReply getSwitchFeaturesReply(String switchId) {
156 return getSwitchFeaturesReply(HexString.toLong(switchId));
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -0700157 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800158
159}