blob: 1e115ac1497324d4ade27bf211b26459a11653e7 [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
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
Jonathan Hartc78b8f62014-08-07 22:31:09 -070027import org.projectfloodlight.openflow.util.HexString;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080028import 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
Ray Milkey269ffb92014-04-03 14:43:30 -070035 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080036 * @author readams
37 */
38public class ControllerSwitchesResource extends ServerResource {
Ray Milkey269ffb92014-04-03 14:43:30 -070039 public static final String DPID_ERROR =
40 "Invalid Switch DPID: must be a 64-bit quantity, expressed in " +
41 "hex as AA:BB:CC:DD:EE:FF:00:11";
42
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080043 @Get("json")
44 public Iterator<IOFSwitch> retrieve() {
Ray Milkey269ffb92014-04-03 14:43:30 -070045 IFloodlightProviderService floodlightProvider =
46 (IFloodlightProviderService) getContext().getAttributes().
47 get(IFloodlightProviderService.class.getCanonicalName());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080048
49 Long switchDPID = null;
Ray Milkey269ffb92014-04-03 14:43:30 -070050
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080051 Form form = getQuery();
52 String dpid = form.getFirstValue("dpid", true);
53 if (dpid != null) {
54 try {
55 switchDPID = HexString.toLong(dpid);
56 } catch (Exception e) {
57 setStatus(Status.CLIENT_ERROR_BAD_REQUEST, DPID_ERROR);
58 return null;
59 }
60 }
61 if (switchDPID != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -070062 IOFSwitch sw =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080063 floodlightProvider.getSwitches().get(switchDPID);
64 if (sw != null)
65 return Collections.singleton(sw).iterator();
66 return Collections.<IOFSwitch>emptySet().iterator();
67 }
Ray Milkey269ffb92014-04-03 14:43:30 -070068 final String dpidStartsWith =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080069 form.getFirstValue("dpid__startswith", true);
Ray Milkey269ffb92014-04-03 14:43:30 -070070 Iterator<IOFSwitch> switer =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080071 floodlightProvider.getSwitches().values().iterator();
72 if (dpidStartsWith != null) {
73 return new FilterIterator<IOFSwitch>(switer) {
74 @Override
75 protected boolean matches(IOFSwitch value) {
76 return value.getStringId().startsWith(dpidStartsWith);
77 }
78 };
Ray Milkey269ffb92014-04-03 14:43:30 -070079 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080080 return switer;
81 }
82}