blob: 454f566cf78fbf5fcf1ec73c705a59e99a0d83d4 [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.Iterator;
22
23import net.floodlightcontroller.core.IFloodlightProviderService;
24import net.floodlightcontroller.core.IOFSwitch;
25import net.floodlightcontroller.util.FilterIterator;
26
27import org.openflow.util.HexString;
28import org.restlet.data.Form;
29import org.restlet.data.Status;
30import org.restlet.resource.Get;
31import org.restlet.resource.ServerResource;
32
33/**
34 * Get a list of switches connected to the controller
35 * @author readams
36 */
37public class ControllerSwitchesResource extends ServerResource {
38 public static final String DPID_ERROR =
39 "Invalid Switch DPID: must be a 64-bit quantity, expressed in " +
40 "hex as AA:BB:CC:DD:EE:FF:00:11";
41
42 @Get("json")
43 public Iterator<IOFSwitch> retrieve() {
44 IFloodlightProviderService floodlightProvider =
45 (IFloodlightProviderService)getContext().getAttributes().
46 get(IFloodlightProviderService.class.getCanonicalName());
47
48 Long switchDPID = null;
49
50 Form form = getQuery();
51 String dpid = form.getFirstValue("dpid", true);
52 if (dpid != null) {
53 try {
54 switchDPID = HexString.toLong(dpid);
55 } catch (Exception e) {
56 setStatus(Status.CLIENT_ERROR_BAD_REQUEST, DPID_ERROR);
57 return null;
58 }
59 }
60 if (switchDPID != null) {
61 IOFSwitch sw =
62 floodlightProvider.getSwitches().get(switchDPID);
63 if (sw != null)
64 return Collections.singleton(sw).iterator();
65 return Collections.<IOFSwitch>emptySet().iterator();
66 }
67 final String dpidStartsWith =
68 form.getFirstValue("dpid__startswith", true);
69 Iterator<IOFSwitch> switer =
70 floodlightProvider.getSwitches().values().iterator();
71 if (dpidStartsWith != null) {
72 return new FilterIterator<IOFSwitch>(switer) {
73 @Override
74 protected boolean matches(IOFSwitch value) {
75 return value.getStringId().startsWith(dpidStartsWith);
76 }
77 };
78 }
79 return switer;
80 }
81}