blob: 58e79e49432764453b57af4dd7d1d61145c1bcc2 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
2* Copyright 2012, 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.devicemanager.web;
19
20import java.util.Iterator;
21
22import net.floodlightcontroller.devicemanager.IDevice;
23import net.floodlightcontroller.devicemanager.IDeviceService;
24import net.floodlightcontroller.devicemanager.SwitchPort;
25import net.floodlightcontroller.devicemanager.internal.Device;
26import net.floodlightcontroller.packet.IPv4;
27import net.floodlightcontroller.util.FilterIterator;
28
29import org.openflow.util.HexString;
30import org.restlet.data.Form;
31import org.restlet.data.Status;
32import org.restlet.resource.ServerResource;
33
34/**
35 * Resource for querying and displaying devices that exist in the system
36 */
37public abstract class AbstractDeviceResource extends ServerResource {
38 public static final String MAC_ERROR =
39 "Invalid MAC address: must be a 48-bit quantity, " +
40 "expressed in hex as AA:BB:CC:DD:EE:FF";
41 public static final String VLAN_ERROR =
42 "Invalid VLAN: must be an integer in the range 0-4095";
43 public static final String IPV4_ERROR =
44 "Invalid IPv4 address: must be in dotted decimal format, " +
45 "234.0.59.1";
46 public static final String DPID_ERROR =
47 "Invalid Switch DPID: must be a 64-bit quantity, expressed in " +
48 "hex as AA:BB:CC:DD:EE:FF:00:11";
49 public static final String PORT_ERROR =
50 "Invalid Port: must be a positive integer";
51
52 public Iterator<? extends IDevice> getDevices() {
53 IDeviceService deviceManager =
54 (IDeviceService)getContext().getAttributes().
55 get(IDeviceService.class.getCanonicalName());
56
57 Long macAddress = null;
58 Short vlan = null;
59 Integer ipv4Address = null;
60 Long switchDPID = null;
61 Integer switchPort = null;
62
63 Form form = getQuery();
64 String macAddrStr = form.getFirstValue("mac", true);
65 String vlanStr = form.getFirstValue("vlan", true);
66 String ipv4Str = form.getFirstValue("ipv4", true);
67 String dpid = form.getFirstValue("dpid", true);
68 String port = form.getFirstValue("port", true);
69
70 if (macAddrStr != null) {
71 try {
72 macAddress = HexString.toLong(macAddrStr);
73 } catch (Exception e) {
74 setStatus(Status.CLIENT_ERROR_BAD_REQUEST, MAC_ERROR);
75 return null;
76 }
77 }
78 if (vlanStr != null) {
79 try {
80 vlan = Short.parseShort(vlanStr);
81 if (vlan > 4095 || vlan < 0) {
82 setStatus(Status.CLIENT_ERROR_BAD_REQUEST, VLAN_ERROR);
83 return null;
84 }
85 } catch (Exception e) {
86 setStatus(Status.CLIENT_ERROR_BAD_REQUEST, VLAN_ERROR);
87 return null;
88 }
89 }
90 if (ipv4Str != null) {
91 try {
92 ipv4Address = IPv4.toIPv4Address(ipv4Str);
93 } catch (Exception e) {
94 setStatus(Status.CLIENT_ERROR_BAD_REQUEST, IPV4_ERROR);
95 return null;
96 }
97 }
98 if (dpid != null) {
99 try {
100 switchDPID = HexString.toLong(dpid);
101 } catch (Exception e) {
102 setStatus(Status.CLIENT_ERROR_BAD_REQUEST, DPID_ERROR);
103 return null;
104 }
105 }
106 if (port != null) {
107 try {
108 switchPort = Integer.parseInt(port);
109 if (switchPort < 0) {
110 setStatus(Status.CLIENT_ERROR_BAD_REQUEST, PORT_ERROR);
111 return null;
112 }
113 } catch (Exception e) {
114 setStatus(Status.CLIENT_ERROR_BAD_REQUEST, PORT_ERROR);
115 return null;
116 }
117 }
118
119 @SuppressWarnings("unchecked")
120 Iterator<Device> diter = (Iterator<Device>)
121 deviceManager.queryDevices(macAddress,
122 vlan,
123 ipv4Address,
124 switchDPID,
125 switchPort);
126
127 final String macStartsWith =
128 form.getFirstValue("mac__startswith", true);
129 final String vlanStartsWith =
130 form.getFirstValue("vlan__startswith", true);
131 final String ipv4StartsWith =
132 form.getFirstValue("ipv4__startswith", true);
133 final String dpidStartsWith =
134 form.getFirstValue("dpid__startswith", true);
135 final String portStartsWith =
136 form.getFirstValue("port__startswith", true);
137
138 return new FilterIterator<Device>(diter) {
139 @Override
140 protected boolean matches(Device value) {
141 if (macStartsWith != null) {
142 if (!value.getMACAddressString().startsWith(macStartsWith))
143 return false;
144 }
145 if (vlanStartsWith != null) {
146 boolean match = false;
147 for (Short v : value.getVlanId()) {
148 if (v != null &&
149 v.toString().startsWith(vlanStartsWith)) {
150 match = true;
151 break;
152 }
153 }
154 if (!match) return false;
155 }
156 if (ipv4StartsWith != null) {
157 boolean match = false;
158 for (Integer v : value.getIPv4Addresses()) {
159 String str = IPv4.fromIPv4Address(v);
160 if (v != null &&
161 str.startsWith(ipv4StartsWith)) {
162 match = true;
163 break;
164 }
165 }
166 if (!match) return false;
167 }
168 if (dpidStartsWith != null) {
169 boolean match = false;
170 for (SwitchPort v : value.getAttachmentPoints(true)) {
171 String str =
172 HexString.toHexString(v.getSwitchDPID(), 8);
173 if (v != null &&
174 str.startsWith(dpidStartsWith)) {
175 match = true;
176 break;
177 }
178 }
179 if (!match) return false;
180 }
181 if (portStartsWith != null) {
182 boolean match = false;
183 for (SwitchPort v : value.getAttachmentPoints(true)) {
184 String str = Integer.toString(v.getPort());
185 if (v != null &&
186 str.startsWith(portStartsWith)) {
187 match = true;
188 break;
189 }
190 }
191 if (!match) return false;
192 }
193 return true;
194 }
195 };
196 }
197}