blob: 5f7b94dfa09b0d25cccb79fed2881f904bc807ea [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 Vavilapallib7258512014-09-29 13:24:11 -070020import java.util.ArrayList;
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070021import 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.projectfloodlight.openflow.protocol.OFFeaturesReply;
30import org.projectfloodlight.openflow.protocol.OFMatchV3;
31import org.projectfloodlight.openflow.protocol.OFOxmList;
Srikanth Vavilapallib7258512014-09-29 13:24:11 -070032import org.projectfloodlight.openflow.protocol.OFPortStatsEntry;
33import org.projectfloodlight.openflow.protocol.OFPortStatsReply;
34import org.projectfloodlight.openflow.protocol.OFPortStatsRequest;
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070035import org.projectfloodlight.openflow.protocol.OFStatsReply;
36import org.projectfloodlight.openflow.protocol.OFStatsRequest;
37import org.projectfloodlight.openflow.protocol.OFStatsType;
38import org.projectfloodlight.openflow.types.OFPort;
39import org.projectfloodlight.openflow.types.TableId;
40import org.projectfloodlight.openflow.util.HexString;
41import org.restlet.resource.ResourceException;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080042import org.restlet.resource.ServerResource;
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070043import org.slf4j.Logger;
44import org.slf4j.LoggerFactory;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080045
46/**
47 * Base class for server resources related to switches
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -070048 *
Ray Milkey269ffb92014-04-03 14:43:30 -070049 * @author readams
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080050 */
Ray Milkeyff735142014-05-22 19:06:02 -070051
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080052public class SwitchResourceBase extends ServerResource {
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070053 protected final static Logger log = LoggerFactory.getLogger(SwitchResourceBase.class);
Ray Milkey269ffb92014-04-03 14:43:30 -070054
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080055 public enum REQUESTTYPE {
56 OFSTATS,
57 OFFEATURES
58 }
Ray Milkey269ffb92014-04-03 14:43:30 -070059
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080060 @Override
61 protected void doInit() throws ResourceException {
62 super.doInit();
Ray Milkey269ffb92014-04-03 14:43:30 -070063
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080064 }
Ray Milkey269ffb92014-04-03 14:43:30 -070065
66 @LogMessageDoc(level = "ERROR",
67 message = "Failure retrieving statistics from switch {switch}",
68 explanation = "An error occurred while retrieving statistics" +
69 "from the switch",
70 recommendation = LogMessageDoc.CHECK_SWITCH + " " +
71 LogMessageDoc.GENERIC_ACTION)
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070072 protected List<OFStatsReply> getSwitchStatistics(long switchId,
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -070073 OFStatsType statType) {
Ray Milkey269ffb92014-04-03 14:43:30 -070074 IFloodlightProviderService floodlightProvider =
75 (IFloodlightProviderService) getContext().getAttributes().
76 get(IFloodlightProviderService.class.getCanonicalName());
77
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080078 IOFSwitch sw = floodlightProvider.getSwitches().get(switchId);
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070079 Future<List<OFStatsReply>> future;
80 List<OFStatsReply> values = null;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080081 if (sw != null) {
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -070082 OFStatsRequest<?> req = null;
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070083 if (statType == OFStatsType.FLOW) {
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -070084 log.debug("Switch Flow Stats req sent for switch {}",
85 sw.getStringId());
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070086 OFMatchV3 match = sw.getFactory().buildMatchV3()
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -070087 .setOxmList(OFOxmList.EMPTY).build();
88 req = sw.getFactory()
89 .buildFlowStatsRequest()
90 .setMatch(match)
91 .setOutPort(OFPort.ANY)
92 .setTableId(TableId.ALL)
93 .setXid(sw.getNextTransactionId()).build();
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070094 } /*else if (statType == OFStatisticsType.AGGREGATE) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080095 OFAggregateStatisticsRequest specificReq = new OFAggregateStatisticsRequest();
96 OFMatch match = new OFMatch();
97 match.setWildcards(0xffffffff);
98 specificReq.setMatch(match);
99 specificReq.setOutPort(OFPort.OFPP_NONE.getValue());
100 specificReq.setTableId((byte) 0xff);
Ray Milkey269ffb92014-04-03 14:43:30 -0700101 req.setStatistics(Collections.singletonList((OFStatistics) specificReq));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800102 requestLength += specificReq.getLength();
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700103 } /*else if (statType == OFStatisticsType.QUEUE) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800104 OFQueueStatisticsRequest specificReq = new OFQueueStatisticsRequest();
Ray Milkeyff735142014-05-22 19:06:02 -0700105 specificReq.setPortNumber(OFPort.OFPP_ALL.getValue());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800106 // LOOK! openflowj does not define OFPQ_ALL! pulled this from openflow.h
107 // note that I haven't seen this work yet though...
108 specificReq.setQueueId(0xffffffff);
Ray Milkey269ffb92014-04-03 14:43:30 -0700109 req.setStatistics(Collections.singletonList((OFStatistics) specificReq));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800110 requestLength += specificReq.getLength();
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700111 } else if (statType == OFStatisticsType.DESC ||
Ray Milkey269ffb92014-04-03 14:43:30 -0700112 statType == OFStatisticsType.TABLE) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800113 // pass - nothing todo besides set the type above
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700114 }*/
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700115 // XXX S fix when we fix stats
Jonathan Hartc78b8f62014-08-07 22:31:09 -0700116 try {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800117 future = sw.getStatistics(req);
118 values = future.get(10, TimeUnit.SECONDS);
119 } catch (Exception e) {
120 log.error("Failure retrieving statistics from switch " + sw, e);
Jonathan Hartc78b8f62014-08-07 22:31:09 -0700121 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800122 }
123 return values;
124 }
125
Srikanth Vavilapallib7258512014-09-29 13:24:11 -0700126 protected List<OFPortStatsEntryMod> getSwitchPortStatistics(long switchId) {
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700127 IFloodlightProviderService floodlightProvider =
128 (IFloodlightProviderService) getContext().getAttributes().
129 get(IFloodlightProviderService.class.getCanonicalName());
Srikanth Vavilapallib7258512014-09-29 13:24:11 -0700130
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700131 IOFSwitch sw = floodlightProvider.getSwitches().get(switchId);
132 Future<List<OFStatsReply>> future;
133 List<OFStatsReply> values = null;
134 List<OFPortStatsEntryMod> portStats = null;
Srikanth Vavilapallib7258512014-09-29 13:24:11 -0700135
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700136 if (sw != null) {
137 log.debug("Switch Port Stats: req sent for all "
138 + "ports in switch {}", sw.getStringId());
139 OFPortStatsRequest req = sw.getFactory()
140 .buildPortStatsRequest()
141 .setPortNo(OFPort.ANY).setXid
142 (sw.getNextTransactionId()).build();
Srikanth Vavilapallib7258512014-09-29 13:24:11 -0700143
144 try {
145 future = sw.getStatistics(req);
146 values = future.get(10, TimeUnit.SECONDS);
147 portStats = new ArrayList<OFPortStatsEntryMod>();
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700148 for (OFPortStatsEntry entry : ((OFPortStatsReply) values.get(0))
149 .getEntries()) {
Srikanth Vavilapallib7258512014-09-29 13:24:11 -0700150 OFPortStatsEntryMod entryMod = new OFPortStatsEntryMod(entry);
151 portStats.add(entryMod);
152 }
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700153 log.debug("Switch Port Stats Entries from switch {} are {}",
154 sw.getStringId(), portStats);
Srikanth Vavilapallib7258512014-09-29 13:24:11 -0700155 } catch (Exception e) {
156 log.error("Failure retrieving statistics from switch " + sw, e);
157 }
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700158 }
Srikanth Vavilapallib7258512014-09-29 13:24:11 -0700159
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700160 return portStats;
Srikanth Vavilapallib7258512014-09-29 13:24:11 -0700161 }
162
163 protected Object getSwitchStatistics(String switchId, OFStatsType statType) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800164 return getSwitchStatistics(HexString.toLong(switchId), statType);
165 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700166
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800167 protected OFFeaturesReply getSwitchFeaturesReply(long switchId) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700168 IFloodlightProviderService floodlightProvider =
169 (IFloodlightProviderService) getContext().getAttributes().
170 get(IFloodlightProviderService.class.getCanonicalName());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800171
172 IOFSwitch sw = floodlightProvider.getSwitches().get(switchId);
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700173 // uture<OFFeaturesReply> future;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800174 OFFeaturesReply featuresReply = null;
175 if (sw != null) {
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700176 // XXX S fix when we fix stats
Jonathan Hartc78b8f62014-08-07 22:31:09 -0700177 try {
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700178 // future = sw.getFeaturesReplyFromSwitch();
179 // featuresReply = future.get(10, TimeUnit.SECONDS);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800180 } catch (Exception e) {
181 log.error("Failure getting features reply from switch" + sw, e);
Jonathan Hartc78b8f62014-08-07 22:31:09 -0700182 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800183 }
184
185 return featuresReply;
186 }
187
188 protected OFFeaturesReply getSwitchFeaturesReply(String switchId) {
189 return getSwitchFeaturesReply(HexString.toLong(switchId));
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -0700190 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800191
192}