blob: def6a7d52d56d2397a0d54e575a22598161d2975 [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;
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -070030import org.projectfloodlight.openflow.protocol.OFGroupDescStatsEntry;
31import org.projectfloodlight.openflow.protocol.OFGroupDescStatsReply;
Fahad Naeem Khan8a8daf22014-10-06 14:07:43 -070032import org.projectfloodlight.openflow.protocol.OFGroupStatsEntry;
33import org.projectfloodlight.openflow.protocol.OFGroupStatsReply;
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070034import org.projectfloodlight.openflow.protocol.OFMatchV3;
35import org.projectfloodlight.openflow.protocol.OFOxmList;
Srikanth Vavilapallib7258512014-09-29 13:24:11 -070036import org.projectfloodlight.openflow.protocol.OFPortStatsEntry;
37import org.projectfloodlight.openflow.protocol.OFPortStatsReply;
38import org.projectfloodlight.openflow.protocol.OFPortStatsRequest;
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070039import org.projectfloodlight.openflow.protocol.OFStatsReply;
40import org.projectfloodlight.openflow.protocol.OFStatsRequest;
41import org.projectfloodlight.openflow.protocol.OFStatsType;
42import org.projectfloodlight.openflow.types.OFPort;
43import org.projectfloodlight.openflow.types.TableId;
44import org.projectfloodlight.openflow.util.HexString;
45import org.restlet.resource.ResourceException;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080046import org.restlet.resource.ServerResource;
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070047import org.slf4j.Logger;
48import org.slf4j.LoggerFactory;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080049
50/**
51 * Base class for server resources related to switches
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -070052 *
Ray Milkey269ffb92014-04-03 14:43:30 -070053 * @author readams
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080054 */
Ray Milkeyff735142014-05-22 19:06:02 -070055
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080056public class SwitchResourceBase extends ServerResource {
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070057 protected final static Logger log = LoggerFactory.getLogger(SwitchResourceBase.class);
Ray Milkey269ffb92014-04-03 14:43:30 -070058
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080059 public enum REQUESTTYPE {
60 OFSTATS,
61 OFFEATURES
62 }
Ray Milkey269ffb92014-04-03 14:43:30 -070063
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080064 @Override
65 protected void doInit() throws ResourceException {
66 super.doInit();
Ray Milkey269ffb92014-04-03 14:43:30 -070067
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080068 }
Ray Milkey269ffb92014-04-03 14:43:30 -070069
70 @LogMessageDoc(level = "ERROR",
71 message = "Failure retrieving statistics from switch {switch}",
72 explanation = "An error occurred while retrieving statistics" +
73 "from the switch",
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -070074 recommendation = LogMessageDoc.CHECK_SWITCH + " " +
75 LogMessageDoc.GENERIC_ACTION)
76
77 protected List<?> getSwitchStatistics(long switchId,
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -070078 OFStatsType statType) {
Ray Milkey269ffb92014-04-03 14:43:30 -070079 IFloodlightProviderService floodlightProvider =
80 (IFloodlightProviderService) getContext().getAttributes().
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -070081 get(IFloodlightProviderService.class.getCanonicalName());
Ray Milkey269ffb92014-04-03 14:43:30 -070082
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080083 IOFSwitch sw = floodlightProvider.getSwitches().get(switchId);
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070084 Future<List<OFStatsReply>> future;
85 List<OFStatsReply> values = null;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080086 if (sw != null) {
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -070087 OFStatsRequest<?> req = null;
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070088 if (statType == OFStatsType.FLOW) {
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -070089 log.debug("Switch Flow Stats req sent for switch {}",
90 sw.getStringId());
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070091 OFMatchV3 match = sw.getFactory().buildMatchV3()
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -070092 .setOxmList(OFOxmList.EMPTY).build();
93 req = sw.getFactory()
94 .buildFlowStatsRequest()
95 .setMatch(match)
96 .setOutPort(OFPort.ANY)
97 .setTableId(TableId.ALL)
98 .setXid(sw.getNextTransactionId()).build();
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -070099 }
100 else if (statType == OFStatsType.PORT){
101 log.debug("Switch Port Stats: req sent for all "
102 + "ports in switch {}", sw.getStringId());
103 List<OFPortStatsEntryMod> portStats = null;
104 req = sw.getFactory()
105 .buildPortStatsRequest()
106 .setPortNo(OFPort.ANY).setXid
107 (sw.getNextTransactionId()).build();
108 try {
109 future = sw.getStatistics(req);
110 values = future.get(10, TimeUnit.SECONDS);
111 portStats = new ArrayList<OFPortStatsEntryMod>();
112 for (OFPortStatsEntry entry : ((OFPortStatsReply)values.get(0)).getEntries()) {
113 OFPortStatsEntryMod entryMod = new OFPortStatsEntryMod(entry);
114 portStats.add(entryMod);
115 }
116 log.debug("Switch Port Stats Entries from switch {} are {}",
117 sw.getStringId(), portStats);
118 } catch (Exception e) {
119 log.error("Failure retrieving statistics from switch " + sw, e);
120 }
121 return portStats;
122 }
123 else if (statType == OFStatsType.GROUP){
124 log.debug("Switch Group Stats: req sent for all "
125 + "ports in switch {}", sw.getStringId());
126 req = sw.getFactory().buildGroupStatsRequest().setXid
127 (sw.getNextTransactionId()).build();
Fahad Naeem Khan8a8daf22014-10-06 14:07:43 -0700128 List<OFGroupStatsEntryMod> groupStats = new ArrayList<OFGroupStatsEntryMod>();
129 try {
130 future = sw.getStatistics(req);
131 values = future.get(10, TimeUnit.SECONDS);
132 for (OFGroupStatsEntry entry : ((OFGroupStatsReply)values.get(0)).getEntries()) {
133 OFGroupStatsEntryMod entryMod = new OFGroupStatsEntryMod(entry);
134 groupStats.add(entryMod);
135 }
136 log.debug("Switch Group Stats Entries from switch {} are {}",
137 sw.getStringId(), groupStats);
138 } catch (Exception e) {
139 log.error("Failure retrieving statistics from switch " + sw, e);
140 }
141 return groupStats;
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -0700142 }
143 else if (statType == OFStatsType.GROUP_DESC){
144 log.debug("Switch Group Desc Stats: req sent for all "
145 + "ports in switch {}", sw.getStringId());
146 List<OFGroupDescStatsEntryMod> GroupDescStats= new ArrayList<OFGroupDescStatsEntryMod>();
147 req = sw.getFactory().buildGroupDescStatsRequest().setXid
148 (sw.getNextTransactionId()).build();
149 try {
150 future = sw.getStatistics(req);
151 values = future.get(10, TimeUnit.SECONDS);
152 for (OFGroupDescStatsEntry entry : ((OFGroupDescStatsReply)values.get(0)).getEntries()) {
153 OFGroupDescStatsEntryMod entryMod = new OFGroupDescStatsEntryMod(entry);
154 GroupDescStats.add(entryMod);
155 }
156 log.debug("Switch Group_Desc Stats Entries from switch {} are {}",
157 sw.getStringId(), GroupDescStats);
158 } catch (Exception e) {
159 log.error("Failure retrieving statistics from switch " + sw, e);
160 }
161 return GroupDescStats;
162 }
163 /*else if (statType == OFStatisticsType.AGGREGATE) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800164 OFAggregateStatisticsRequest specificReq = new OFAggregateStatisticsRequest();
165 OFMatch match = new OFMatch();
166 match.setWildcards(0xffffffff);
167 specificReq.setMatch(match);
168 specificReq.setOutPort(OFPort.OFPP_NONE.getValue());
169 specificReq.setTableId((byte) 0xff);
Ray Milkey269ffb92014-04-03 14:43:30 -0700170 req.setStatistics(Collections.singletonList((OFStatistics) specificReq));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800171 requestLength += specificReq.getLength();
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -0700172 } /*else if (statType == OFStatisticsType.QUEUE) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800173 OFQueueStatisticsRequest specificReq = new OFQueueStatisticsRequest();
Ray Milkeyff735142014-05-22 19:06:02 -0700174 specificReq.setPortNumber(OFPort.OFPP_ALL.getValue());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800175 // LOOK! openflowj does not define OFPQ_ALL! pulled this from openflow.h
176 // note that I haven't seen this work yet though...
177 specificReq.setQueueId(0xffffffff);
Ray Milkey269ffb92014-04-03 14:43:30 -0700178 req.setStatistics(Collections.singletonList((OFStatistics) specificReq));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800179 requestLength += specificReq.getLength();
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -0700180 } else if (statType == OFStatisticsType.DESC ||
Ray Milkey269ffb92014-04-03 14:43:30 -0700181 statType == OFStatisticsType.TABLE) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800182 // pass - nothing todo besides set the type above
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -0700183 }*/
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700184 // XXX S fix when we fix stats
Jonathan Hartc78b8f62014-08-07 22:31:09 -0700185 try {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800186 future = sw.getStatistics(req);
187 values = future.get(10, TimeUnit.SECONDS);
188 } catch (Exception e) {
189 log.error("Failure retrieving statistics from switch " + sw, e);
Jonathan Hartc78b8f62014-08-07 22:31:09 -0700190 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800191 }
192 return values;
193 }
194
Srikanth Vavilapallib7258512014-09-29 13:24:11 -0700195 protected Object getSwitchStatistics(String switchId, OFStatsType statType) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800196 return getSwitchStatistics(HexString.toLong(switchId), statType);
197 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700198
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800199 protected OFFeaturesReply getSwitchFeaturesReply(long switchId) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700200 IFloodlightProviderService floodlightProvider =
201 (IFloodlightProviderService) getContext().getAttributes().
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -0700202 get(IFloodlightProviderService.class.getCanonicalName());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800203
204 IOFSwitch sw = floodlightProvider.getSwitches().get(switchId);
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -0700205 //uture<OFFeaturesReply> future;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800206 OFFeaturesReply featuresReply = null;
207 if (sw != null) {
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700208 // XXX S fix when we fix stats
Jonathan Hartc78b8f62014-08-07 22:31:09 -0700209 try {
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -0700210 //future = sw.getFeaturesReplyFromSwitch();
211 //featuresReply = future.get(10, TimeUnit.SECONDS);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800212 } catch (Exception e) {
213 log.error("Failure getting features reply from switch" + sw, e);
Jonathan Hartc78b8f62014-08-07 22:31:09 -0700214 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800215 }
216
217 return featuresReply;
218 }
219
220 protected OFFeaturesReply getSwitchFeaturesReply(String switchId) {
221 return getSwitchFeaturesReply(HexString.toLong(switchId));
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -0700222 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800223
224}