blob: 1c138f61908d26cbcb5aad89a4bb0e39eae8b833 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
2* 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**/
17
18package net.floodlightcontroller.core.web;
19
20import java.util.Collections;
21import 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.openflow.protocol.OFFeaturesReply;
30import org.openflow.protocol.OFMatch;
31import org.openflow.protocol.OFPort;
32import org.openflow.protocol.OFStatisticsRequest;
33import org.openflow.protocol.statistics.OFAggregateStatisticsRequest;
34import org.openflow.protocol.statistics.OFFlowStatisticsRequest;
35import org.openflow.protocol.statistics.OFPortStatisticsRequest;
36import org.openflow.protocol.statistics.OFQueueStatisticsRequest;
37import org.openflow.protocol.statistics.OFStatistics;
38import org.openflow.protocol.statistics.OFStatisticsType;
39import org.openflow.util.HexString;
40import org.restlet.resource.ResourceException;
41import org.restlet.resource.ServerResource;
42import org.slf4j.Logger;
43import org.slf4j.LoggerFactory;
44
45/**
46 * Base class for server resources related to switches
47 * @author readams
48 *
49 */
50public class SwitchResourceBase extends ServerResource {
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070051 protected final static Logger log = LoggerFactory.getLogger(SwitchResourceBase.class);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080052
53 public enum REQUESTTYPE {
54 OFSTATS,
55 OFFEATURES
56 }
57
58 @Override
59 protected void doInit() throws ResourceException {
60 super.doInit();
61
62 }
63
64 @LogMessageDoc(level="ERROR",
65 message="Failure retrieving statistics from switch {switch}",
66 explanation="An error occurred while retrieving statistics" +
67 "from the switch",
68 recommendation=LogMessageDoc.CHECK_SWITCH + " " +
69 LogMessageDoc.GENERIC_ACTION)
70 protected List<OFStatistics> getSwitchStatistics(long switchId,
71 OFStatisticsType statType) {
72 IFloodlightProviderService floodlightProvider =
73 (IFloodlightProviderService)getContext().getAttributes().
74 get(IFloodlightProviderService.class.getCanonicalName());
75
76 IOFSwitch sw = floodlightProvider.getSwitches().get(switchId);
77 Future<List<OFStatistics>> future;
78 List<OFStatistics> values = null;
79 if (sw != null) {
80 OFStatisticsRequest req = new OFStatisticsRequest();
81 req.setStatisticType(statType);
82 int requestLength = req.getLengthU();
83 if (statType == OFStatisticsType.FLOW) {
84 OFFlowStatisticsRequest specificReq = new OFFlowStatisticsRequest();
85 OFMatch match = new OFMatch();
86 match.setWildcards(0xffffffff);
87 specificReq.setMatch(match);
88 specificReq.setOutPort(OFPort.OFPP_NONE.getValue());
89 specificReq.setTableId((byte) 0xff);
90 req.setStatistics(Collections.singletonList((OFStatistics)specificReq));
91 requestLength += specificReq.getLength();
92 } else if (statType == OFStatisticsType.AGGREGATE) {
93 OFAggregateStatisticsRequest specificReq = new OFAggregateStatisticsRequest();
94 OFMatch match = new OFMatch();
95 match.setWildcards(0xffffffff);
96 specificReq.setMatch(match);
97 specificReq.setOutPort(OFPort.OFPP_NONE.getValue());
98 specificReq.setTableId((byte) 0xff);
99 req.setStatistics(Collections.singletonList((OFStatistics)specificReq));
100 requestLength += specificReq.getLength();
101 } else if (statType == OFStatisticsType.PORT) {
102 OFPortStatisticsRequest specificReq = new OFPortStatisticsRequest();
103 specificReq.setPortNumber((short)OFPort.OFPP_NONE.getValue());
104 req.setStatistics(Collections.singletonList((OFStatistics)specificReq));
105 requestLength += specificReq.getLength();
106 } else if (statType == OFStatisticsType.QUEUE) {
107 OFQueueStatisticsRequest specificReq = new OFQueueStatisticsRequest();
108 specificReq.setPortNumber((short)OFPort.OFPP_ALL.getValue());
109 // LOOK! openflowj does not define OFPQ_ALL! pulled this from openflow.h
110 // note that I haven't seen this work yet though...
111 specificReq.setQueueId(0xffffffff);
112 req.setStatistics(Collections.singletonList((OFStatistics)specificReq));
113 requestLength += specificReq.getLength();
114 } else if (statType == OFStatisticsType.DESC ||
115 statType == OFStatisticsType.TABLE) {
116 // pass - nothing todo besides set the type above
117 }
118 req.setLengthU(requestLength);
119 try {
120 future = sw.getStatistics(req);
121 values = future.get(10, TimeUnit.SECONDS);
122 } catch (Exception e) {
123 log.error("Failure retrieving statistics from switch " + sw, e);
124 }
125 }
126 return values;
127 }
128
129 protected List<OFStatistics> getSwitchStatistics(String switchId, OFStatisticsType statType) {
130 return getSwitchStatistics(HexString.toLong(switchId), statType);
131 }
132
133 protected OFFeaturesReply getSwitchFeaturesReply(long switchId) {
134 IFloodlightProviderService floodlightProvider =
135 (IFloodlightProviderService)getContext().getAttributes().
136 get(IFloodlightProviderService.class.getCanonicalName());
137
138 IOFSwitch sw = floodlightProvider.getSwitches().get(switchId);
139 Future<OFFeaturesReply> future;
140 OFFeaturesReply featuresReply = null;
141 if (sw != null) {
142 try {
143 future = sw.getFeaturesReplyFromSwitch();
144 featuresReply = future.get(10, TimeUnit.SECONDS);
145 } catch (Exception e) {
146 log.error("Failure getting features reply from switch" + sw, e);
147 }
148 }
149
150 return featuresReply;
151 }
152
153 protected OFFeaturesReply getSwitchFeaturesReply(String switchId) {
154 return getSwitchFeaturesReply(HexString.toLong(switchId));
155 }
156
157}