blob: c7dd69a06530d7f62afb0772f04d611c08440f79 [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;
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070032import org.projectfloodlight.openflow.protocol.OFMatchV3;
33import org.projectfloodlight.openflow.protocol.OFOxmList;
Srikanth Vavilapallib7258512014-09-29 13:24:11 -070034import org.projectfloodlight.openflow.protocol.OFPortStatsEntry;
35import org.projectfloodlight.openflow.protocol.OFPortStatsReply;
36import org.projectfloodlight.openflow.protocol.OFPortStatsRequest;
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070037import org.projectfloodlight.openflow.protocol.OFStatsReply;
38import org.projectfloodlight.openflow.protocol.OFStatsRequest;
39import org.projectfloodlight.openflow.protocol.OFStatsType;
40import org.projectfloodlight.openflow.types.OFPort;
41import org.projectfloodlight.openflow.types.TableId;
42import org.projectfloodlight.openflow.util.HexString;
43import org.restlet.resource.ResourceException;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080044import org.restlet.resource.ServerResource;
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070045import org.slf4j.Logger;
46import org.slf4j.LoggerFactory;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080047
48/**
49 * Base class for server resources related to switches
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -070050 *
Ray Milkey269ffb92014-04-03 14:43:30 -070051 * @author readams
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080052 */
Ray Milkeyff735142014-05-22 19:06:02 -070053
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080054public class SwitchResourceBase extends ServerResource {
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070055 protected final static Logger log = LoggerFactory.getLogger(SwitchResourceBase.class);
Ray Milkey269ffb92014-04-03 14:43:30 -070056
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080057 public enum REQUESTTYPE {
58 OFSTATS,
59 OFFEATURES
60 }
Ray Milkey269ffb92014-04-03 14:43:30 -070061
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080062 @Override
63 protected void doInit() throws ResourceException {
64 super.doInit();
Ray Milkey269ffb92014-04-03 14:43:30 -070065
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080066 }
Ray Milkey269ffb92014-04-03 14:43:30 -070067
68 @LogMessageDoc(level = "ERROR",
69 message = "Failure retrieving statistics from switch {switch}",
70 explanation = "An error occurred while retrieving statistics" +
71 "from the switch",
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -070072 recommendation = LogMessageDoc.CHECK_SWITCH + " " +
73 LogMessageDoc.GENERIC_ACTION)
74
75 protected List<?> getSwitchStatistics(long switchId,
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -070076 OFStatsType statType) {
Ray Milkey269ffb92014-04-03 14:43:30 -070077 IFloodlightProviderService floodlightProvider =
78 (IFloodlightProviderService) getContext().getAttributes().
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -070079 get(IFloodlightProviderService.class.getCanonicalName());
Ray Milkey269ffb92014-04-03 14:43:30 -070080
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080081 IOFSwitch sw = floodlightProvider.getSwitches().get(switchId);
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070082 Future<List<OFStatsReply>> future;
83 List<OFStatsReply> values = null;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080084 if (sw != null) {
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -070085 OFStatsRequest<?> req = null;
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070086 if (statType == OFStatsType.FLOW) {
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -070087 log.debug("Switch Flow Stats req sent for switch {}",
88 sw.getStringId());
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070089 OFMatchV3 match = sw.getFactory().buildMatchV3()
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -070090 .setOxmList(OFOxmList.EMPTY).build();
91 req = sw.getFactory()
92 .buildFlowStatsRequest()
93 .setMatch(match)
94 .setOutPort(OFPort.ANY)
95 .setTableId(TableId.ALL)
96 .setXid(sw.getNextTransactionId()).build();
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -070097 }
98 else if (statType == OFStatsType.PORT){
99 log.debug("Switch Port Stats: req sent for all "
100 + "ports in switch {}", sw.getStringId());
101 List<OFPortStatsEntryMod> portStats = null;
102 req = sw.getFactory()
103 .buildPortStatsRequest()
104 .setPortNo(OFPort.ANY).setXid
105 (sw.getNextTransactionId()).build();
106 try {
107 future = sw.getStatistics(req);
108 values = future.get(10, TimeUnit.SECONDS);
109 portStats = new ArrayList<OFPortStatsEntryMod>();
110 for (OFPortStatsEntry entry : ((OFPortStatsReply)values.get(0)).getEntries()) {
111 OFPortStatsEntryMod entryMod = new OFPortStatsEntryMod(entry);
112 portStats.add(entryMod);
113 }
114 log.debug("Switch Port Stats Entries from switch {} are {}",
115 sw.getStringId(), portStats);
116 } catch (Exception e) {
117 log.error("Failure retrieving statistics from switch " + sw, e);
118 }
119 return portStats;
120 }
121 else if (statType == OFStatsType.GROUP){
122 log.debug("Switch Group Stats: req sent for all "
123 + "ports in switch {}", sw.getStringId());
124 req = sw.getFactory().buildGroupStatsRequest().setXid
125 (sw.getNextTransactionId()).build();
126 }
127 else if (statType == OFStatsType.GROUP_DESC){
128 log.debug("Switch Group Desc Stats: req sent for all "
129 + "ports in switch {}", sw.getStringId());
130 List<OFGroupDescStatsEntryMod> GroupDescStats= new ArrayList<OFGroupDescStatsEntryMod>();
131 req = sw.getFactory().buildGroupDescStatsRequest().setXid
132 (sw.getNextTransactionId()).build();
133 try {
134 future = sw.getStatistics(req);
135 values = future.get(10, TimeUnit.SECONDS);
136 for (OFGroupDescStatsEntry entry : ((OFGroupDescStatsReply)values.get(0)).getEntries()) {
137 OFGroupDescStatsEntryMod entryMod = new OFGroupDescStatsEntryMod(entry);
138 GroupDescStats.add(entryMod);
139 }
140 log.debug("Switch Group_Desc Stats Entries from switch {} are {}",
141 sw.getStringId(), GroupDescStats);
142 } catch (Exception e) {
143 log.error("Failure retrieving statistics from switch " + sw, e);
144 }
145 return GroupDescStats;
146 }
147 /*else if (statType == OFStatisticsType.AGGREGATE) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800148 OFAggregateStatisticsRequest specificReq = new OFAggregateStatisticsRequest();
149 OFMatch match = new OFMatch();
150 match.setWildcards(0xffffffff);
151 specificReq.setMatch(match);
152 specificReq.setOutPort(OFPort.OFPP_NONE.getValue());
153 specificReq.setTableId((byte) 0xff);
Ray Milkey269ffb92014-04-03 14:43:30 -0700154 req.setStatistics(Collections.singletonList((OFStatistics) specificReq));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800155 requestLength += specificReq.getLength();
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -0700156 } /*else if (statType == OFStatisticsType.QUEUE) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800157 OFQueueStatisticsRequest specificReq = new OFQueueStatisticsRequest();
Ray Milkeyff735142014-05-22 19:06:02 -0700158 specificReq.setPortNumber(OFPort.OFPP_ALL.getValue());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800159 // LOOK! openflowj does not define OFPQ_ALL! pulled this from openflow.h
160 // note that I haven't seen this work yet though...
161 specificReq.setQueueId(0xffffffff);
Ray Milkey269ffb92014-04-03 14:43:30 -0700162 req.setStatistics(Collections.singletonList((OFStatistics) specificReq));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800163 requestLength += specificReq.getLength();
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -0700164 } else if (statType == OFStatisticsType.DESC ||
Ray Milkey269ffb92014-04-03 14:43:30 -0700165 statType == OFStatisticsType.TABLE) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800166 // pass - nothing todo besides set the type above
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -0700167 }*/
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700168 // XXX S fix when we fix stats
Jonathan Hartc78b8f62014-08-07 22:31:09 -0700169 try {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800170 future = sw.getStatistics(req);
171 values = future.get(10, TimeUnit.SECONDS);
172 } catch (Exception e) {
173 log.error("Failure retrieving statistics from switch " + sw, e);
Jonathan Hartc78b8f62014-08-07 22:31:09 -0700174 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800175 }
176 return values;
177 }
178
Srikanth Vavilapallib7258512014-09-29 13:24:11 -0700179 protected Object getSwitchStatistics(String switchId, OFStatsType statType) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800180 return getSwitchStatistics(HexString.toLong(switchId), statType);
181 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700182
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800183 protected OFFeaturesReply getSwitchFeaturesReply(long switchId) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700184 IFloodlightProviderService floodlightProvider =
185 (IFloodlightProviderService) getContext().getAttributes().
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -0700186 get(IFloodlightProviderService.class.getCanonicalName());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800187
188 IOFSwitch sw = floodlightProvider.getSwitches().get(switchId);
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -0700189 //uture<OFFeaturesReply> future;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800190 OFFeaturesReply featuresReply = null;
191 if (sw != null) {
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700192 // XXX S fix when we fix stats
Jonathan Hartc78b8f62014-08-07 22:31:09 -0700193 try {
Fahad Naeem Khan9ae7fa12014-10-03 14:30:55 -0700194 //future = sw.getFeaturesReplyFromSwitch();
195 //featuresReply = future.get(10, TimeUnit.SECONDS);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800196 } catch (Exception e) {
197 log.error("Failure getting features reply from switch" + sw, e);
Jonathan Hartc78b8f62014-08-07 22:31:09 -0700198 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800199 }
200
201 return featuresReply;
202 }
203
204 protected OFFeaturesReply getSwitchFeaturesReply(String switchId) {
205 return getSwitchFeaturesReply(HexString.toLong(switchId));
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -0700206 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800207
208}