blob: 8e4208c0608fa19c1f6399555ab2665a1b4f6cc2 [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 Khand89448d2014-10-06 18:40:45 -070030import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry;
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -070031import org.projectfloodlight.openflow.protocol.OFGroupDescStatsEntry;
32import org.projectfloodlight.openflow.protocol.OFGroupDescStatsReply;
Fahad Naeem Khan8a8daf22014-10-06 14:07:43 -070033import org.projectfloodlight.openflow.protocol.OFGroupStatsEntry;
34import org.projectfloodlight.openflow.protocol.OFGroupStatsReply;
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070035import org.projectfloodlight.openflow.protocol.OFMatchV3;
36import org.projectfloodlight.openflow.protocol.OFOxmList;
Srikanth Vavilapallib7258512014-09-29 13:24:11 -070037import org.projectfloodlight.openflow.protocol.OFPortStatsEntry;
Fahad Naeem Khand89448d2014-10-06 18:40:45 -070038import org.projectfloodlight.openflow.protocol.OFFlowStatsReply;
Srikanth Vavilapallib7258512014-09-29 13:24:11 -070039import org.projectfloodlight.openflow.protocol.OFPortStatsReply;
40import org.projectfloodlight.openflow.protocol.OFPortStatsRequest;
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070041import org.projectfloodlight.openflow.protocol.OFStatsReply;
42import org.projectfloodlight.openflow.protocol.OFStatsRequest;
43import org.projectfloodlight.openflow.protocol.OFStatsType;
44import org.projectfloodlight.openflow.types.OFPort;
45import org.projectfloodlight.openflow.types.TableId;
46import org.projectfloodlight.openflow.util.HexString;
47import org.restlet.resource.ResourceException;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080048import org.restlet.resource.ServerResource;
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070049import org.slf4j.Logger;
50import org.slf4j.LoggerFactory;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080051
52/**
53 * Base class for server resources related to switches
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -070054 *
Ray Milkey269ffb92014-04-03 14:43:30 -070055 * @author readams
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080056 */
Ray Milkeyff735142014-05-22 19:06:02 -070057
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080058public class SwitchResourceBase extends ServerResource {
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070059 protected final static Logger log = LoggerFactory.getLogger(SwitchResourceBase.class);
Ray Milkey269ffb92014-04-03 14:43:30 -070060
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080061 public enum REQUESTTYPE {
62 OFSTATS,
63 OFFEATURES
64 }
Ray Milkey269ffb92014-04-03 14:43:30 -070065
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080066 @Override
67 protected void doInit() throws ResourceException {
68 super.doInit();
Ray Milkey269ffb92014-04-03 14:43:30 -070069
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080070 }
Ray Milkey269ffb92014-04-03 14:43:30 -070071
72 @LogMessageDoc(level = "ERROR",
73 message = "Failure retrieving statistics from switch {switch}",
74 explanation = "An error occurred while retrieving statistics" +
75 "from the switch",
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -070076 recommendation = LogMessageDoc.CHECK_SWITCH + " " +
77 LogMessageDoc.GENERIC_ACTION)
78
79 protected List<?> getSwitchStatistics(long switchId,
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -070080 OFStatsType statType) {
Ray Milkey269ffb92014-04-03 14:43:30 -070081 IFloodlightProviderService floodlightProvider =
82 (IFloodlightProviderService) getContext().getAttributes().
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -070083 get(IFloodlightProviderService.class.getCanonicalName());
Ray Milkey269ffb92014-04-03 14:43:30 -070084
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080085 IOFSwitch sw = floodlightProvider.getSwitches().get(switchId);
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070086 Future<List<OFStatsReply>> future;
87 List<OFStatsReply> values = null;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080088 if (sw != null) {
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -070089 OFStatsRequest<?> req = null;
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070090 if (statType == OFStatsType.FLOW) {
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -070091 log.debug("Switch Flow Stats req sent for switch {}",
92 sw.getStringId());
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070093 OFMatchV3 match = sw.getFactory().buildMatchV3()
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -070094 .setOxmList(OFOxmList.EMPTY).build();
95 req = sw.getFactory()
96 .buildFlowStatsRequest()
97 .setMatch(match)
98 .setOutPort(OFPort.ANY)
99 .setTableId(TableId.ALL)
100 .setXid(sw.getNextTransactionId()).build();
Fahad Naeem Khand89448d2014-10-06 18:40:45 -0700101 List<OFFlowStatsEntryMod> flowStats = new ArrayList<OFFlowStatsEntryMod>();
102 try {
103 future = sw.getStatistics(req);
104 values = future.get(10, TimeUnit.SECONDS);
105 System.out.println("value\n\n\n"+ values);
106 for (OFFlowStatsEntry entry : ((OFFlowStatsReply)values.get(0)).getEntries()) {
107 OFFlowStatsEntryMod entryMod = new OFFlowStatsEntryMod(entry);
108 flowStats.add(entryMod);
109 }
110 log.debug("Switch flow Stats Entries from switch {} are {}",
111 sw.getStringId(), flowStats);
112 } catch (Exception e) {
113 log.error("Failure retrieving statistics from switch " + sw, e);
114 }
115 return flowStats;
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -0700116 }
117 else if (statType == OFStatsType.PORT){
118 log.debug("Switch Port Stats: req sent for all "
119 + "ports in switch {}", sw.getStringId());
120 List<OFPortStatsEntryMod> portStats = null;
121 req = sw.getFactory()
122 .buildPortStatsRequest()
123 .setPortNo(OFPort.ANY).setXid
124 (sw.getNextTransactionId()).build();
125 try {
126 future = sw.getStatistics(req);
127 values = future.get(10, TimeUnit.SECONDS);
128 portStats = new ArrayList<OFPortStatsEntryMod>();
129 for (OFPortStatsEntry entry : ((OFPortStatsReply)values.get(0)).getEntries()) {
130 OFPortStatsEntryMod entryMod = new OFPortStatsEntryMod(entry);
131 portStats.add(entryMod);
132 }
133 log.debug("Switch Port Stats Entries from switch {} are {}",
134 sw.getStringId(), portStats);
135 } catch (Exception e) {
136 log.error("Failure retrieving statistics from switch " + sw, e);
137 }
138 return portStats;
139 }
140 else if (statType == OFStatsType.GROUP){
141 log.debug("Switch Group Stats: req sent for all "
142 + "ports in switch {}", sw.getStringId());
143 req = sw.getFactory().buildGroupStatsRequest().setXid
144 (sw.getNextTransactionId()).build();
Fahad Naeem Khan8a8daf22014-10-06 14:07:43 -0700145 List<OFGroupStatsEntryMod> groupStats = new ArrayList<OFGroupStatsEntryMod>();
146 try {
147 future = sw.getStatistics(req);
148 values = future.get(10, TimeUnit.SECONDS);
149 for (OFGroupStatsEntry entry : ((OFGroupStatsReply)values.get(0)).getEntries()) {
150 OFGroupStatsEntryMod entryMod = new OFGroupStatsEntryMod(entry);
151 groupStats.add(entryMod);
152 }
153 log.debug("Switch Group Stats Entries from switch {} are {}",
154 sw.getStringId(), groupStats);
155 } catch (Exception e) {
156 log.error("Failure retrieving statistics from switch " + sw, e);
157 }
158 return groupStats;
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -0700159 }
160 else if (statType == OFStatsType.GROUP_DESC){
161 log.debug("Switch Group Desc Stats: req sent for all "
162 + "ports in switch {}", sw.getStringId());
163 List<OFGroupDescStatsEntryMod> GroupDescStats= new ArrayList<OFGroupDescStatsEntryMod>();
164 req = sw.getFactory().buildGroupDescStatsRequest().setXid
165 (sw.getNextTransactionId()).build();
166 try {
167 future = sw.getStatistics(req);
168 values = future.get(10, TimeUnit.SECONDS);
169 for (OFGroupDescStatsEntry entry : ((OFGroupDescStatsReply)values.get(0)).getEntries()) {
170 OFGroupDescStatsEntryMod entryMod = new OFGroupDescStatsEntryMod(entry);
171 GroupDescStats.add(entryMod);
172 }
173 log.debug("Switch Group_Desc Stats Entries from switch {} are {}",
174 sw.getStringId(), GroupDescStats);
175 } catch (Exception e) {
176 log.error("Failure retrieving statistics from switch " + sw, e);
177 }
178 return GroupDescStats;
179 }
180 /*else if (statType == OFStatisticsType.AGGREGATE) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800181 OFAggregateStatisticsRequest specificReq = new OFAggregateStatisticsRequest();
182 OFMatch match = new OFMatch();
183 match.setWildcards(0xffffffff);
184 specificReq.setMatch(match);
185 specificReq.setOutPort(OFPort.OFPP_NONE.getValue());
186 specificReq.setTableId((byte) 0xff);
Ray Milkey269ffb92014-04-03 14:43:30 -0700187 req.setStatistics(Collections.singletonList((OFStatistics) specificReq));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800188 requestLength += specificReq.getLength();
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -0700189 } /*else if (statType == OFStatisticsType.QUEUE) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800190 OFQueueStatisticsRequest specificReq = new OFQueueStatisticsRequest();
Ray Milkeyff735142014-05-22 19:06:02 -0700191 specificReq.setPortNumber(OFPort.OFPP_ALL.getValue());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800192 // LOOK! openflowj does not define OFPQ_ALL! pulled this from openflow.h
193 // note that I haven't seen this work yet though...
194 specificReq.setQueueId(0xffffffff);
Ray Milkey269ffb92014-04-03 14:43:30 -0700195 req.setStatistics(Collections.singletonList((OFStatistics) specificReq));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800196 requestLength += specificReq.getLength();
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -0700197 } else if (statType == OFStatisticsType.DESC ||
Ray Milkey269ffb92014-04-03 14:43:30 -0700198 statType == OFStatisticsType.TABLE) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800199 // pass - nothing todo besides set the type above
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -0700200 }*/
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700201 // XXX S fix when we fix stats
Jonathan Hartc78b8f62014-08-07 22:31:09 -0700202 try {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800203 future = sw.getStatistics(req);
204 values = future.get(10, TimeUnit.SECONDS);
205 } catch (Exception e) {
206 log.error("Failure retrieving statistics from switch " + sw, e);
Jonathan Hartc78b8f62014-08-07 22:31:09 -0700207 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800208 }
209 return values;
210 }
211
Srikanth Vavilapallib7258512014-09-29 13:24:11 -0700212 protected Object getSwitchStatistics(String switchId, OFStatsType statType) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800213 return getSwitchStatistics(HexString.toLong(switchId), statType);
214 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700215
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800216 protected OFFeaturesReply getSwitchFeaturesReply(long switchId) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700217 IFloodlightProviderService floodlightProvider =
218 (IFloodlightProviderService) getContext().getAttributes().
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -0700219 get(IFloodlightProviderService.class.getCanonicalName());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800220
221 IOFSwitch sw = floodlightProvider.getSwitches().get(switchId);
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -0700222 //uture<OFFeaturesReply> future;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800223 OFFeaturesReply featuresReply = null;
224 if (sw != null) {
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700225 // XXX S fix when we fix stats
Jonathan Hartc78b8f62014-08-07 22:31:09 -0700226 try {
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -0700227 //future = sw.getFeaturesReplyFromSwitch();
228 //featuresReply = future.get(10, TimeUnit.SECONDS);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800229 } catch (Exception e) {
230 log.error("Failure getting features reply from switch" + sw, e);
Jonathan Hartc78b8f62014-08-07 22:31:09 -0700231 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800232 }
233
234 return featuresReply;
235 }
236
237 protected OFFeaturesReply getSwitchFeaturesReply(String switchId) {
238 return getSwitchFeaturesReply(HexString.toLong(switchId));
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -0700239 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800240
241}