blob: 38b0d1e70700e7c83212328169b4017190094b24 [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
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080048 *
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
73 protected List<OFStatsReply> getSwitchStatistics(long switchId,
74 OFStatsType statType) {
Ray Milkey269ffb92014-04-03 14:43:30 -070075 IFloodlightProviderService floodlightProvider =
76 (IFloodlightProviderService) getContext().getAttributes().
77 get(IFloodlightProviderService.class.getCanonicalName());
78
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080079 IOFSwitch sw = floodlightProvider.getSwitches().get(switchId);
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070080 Future<List<OFStatsReply>> future;
81 List<OFStatsReply> values = null;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080082 if (sw != null) {
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -070083 OFStatsRequest<?> req = null;
84 if (statType == OFStatsType.FLOW) {
85 log.debug("Switch Flow Stats req sent for switch {}",
86 sw.getStringId());
87 OFMatchV3 match = sw.getFactory().buildMatchV3()
88 .setOxmList(OFOxmList.EMPTY).build();
89 req = sw.getFactory()
90 .buildFlowStatsRequest()
91 .setMatch(match)
92 .setOutPort(OFPort.ANY)
93 .setTableId(TableId.ALL)
94 .setXid(sw.getNextTransactionId()).build();
95 } /*else if (statType == OFStatisticsType.AGGREGATE) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080096 OFAggregateStatisticsRequest specificReq = new OFAggregateStatisticsRequest();
97 OFMatch match = new OFMatch();
98 match.setWildcards(0xffffffff);
99 specificReq.setMatch(match);
100 specificReq.setOutPort(OFPort.OFPP_NONE.getValue());
101 specificReq.setTableId((byte) 0xff);
Ray Milkey269ffb92014-04-03 14:43:30 -0700102 req.setStatistics(Collections.singletonList((OFStatistics) specificReq));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800103 requestLength += specificReq.getLength();
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -0700104 } /*else if (statType == OFStatisticsType.QUEUE) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800105 OFQueueStatisticsRequest specificReq = new OFQueueStatisticsRequest();
Ray Milkeyff735142014-05-22 19:06:02 -0700106 specificReq.setPortNumber(OFPort.OFPP_ALL.getValue());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800107 // LOOK! openflowj does not define OFPQ_ALL! pulled this from openflow.h
108 // note that I haven't seen this work yet though...
109 specificReq.setQueueId(0xffffffff);
Ray Milkey269ffb92014-04-03 14:43:30 -0700110 req.setStatistics(Collections.singletonList((OFStatistics) specificReq));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800111 requestLength += specificReq.getLength();
112 } else if (statType == OFStatisticsType.DESC ||
Ray Milkey269ffb92014-04-03 14:43:30 -0700113 statType == OFStatisticsType.TABLE) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800114 // pass - nothing todo besides set the type above
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -0700115 }*/
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700116 // XXX S fix when we fix stats
Jonathan Hartc78b8f62014-08-07 22:31:09 -0700117 try {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800118 future = sw.getStatistics(req);
119 values = future.get(10, TimeUnit.SECONDS);
120 } catch (Exception e) {
121 log.error("Failure retrieving statistics from switch " + sw, e);
Jonathan Hartc78b8f62014-08-07 22:31:09 -0700122 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800123 }
124 return values;
125 }
126
Srikanth Vavilapallib7258512014-09-29 13:24:11 -0700127 protected List<OFPortStatsEntryMod> getSwitchPortStatistics(long switchId) {
128 IFloodlightProviderService floodlightProvider =
129 (IFloodlightProviderService) getContext().getAttributes().
130 get(IFloodlightProviderService.class.getCanonicalName());
131
132 IOFSwitch sw = floodlightProvider.getSwitches().get(switchId);
133 Future<List<OFStatsReply>> future;
134 List<OFStatsReply> values = null;
135 List<OFPortStatsEntryMod> portStats = null;
136
137 if (sw != null) {
138 log.debug("Switch Port Stats: req sent for all "
139 + "ports in switch {}", sw.getStringId());
140 OFPortStatsRequest req = sw.getFactory()
141 .buildPortStatsRequest()
142 .setPortNo(OFPort.ANY).setXid
143 (sw.getNextTransactionId()).build();
144
145 try {
146 future = sw.getStatistics(req);
147 values = future.get(10, TimeUnit.SECONDS);
148 portStats = new ArrayList<OFPortStatsEntryMod>();
149 for (OFPortStatsEntry entry : ((OFPortStatsReply)values.get(0)).getEntries()) {
150 OFPortStatsEntryMod entryMod = new OFPortStatsEntryMod(entry);
151 portStats.add(entryMod);
152 }
153 log.debug("Switch Port Stats Entries from switch {} are {}",
154 sw.getStringId(), portStats);
155 } catch (Exception e) {
156 log.error("Failure retrieving statistics from switch " + sw, e);
157 }
158 }
159
160 return portStats;
161 }
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 Vavilapallia0e6d582014-09-28 22:42:34 -0700173 //uture<OFFeaturesReply> future;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800174 OFFeaturesReply featuresReply = null;
175 if (sw != null) {
Jonathan Hartc78b8f62014-08-07 22:31:09 -0700176 // XXX S fix when we fix stats
177 try {
Srikanth Vavilapallia0e6d582014-09-28 22:42:34 -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}