blob: f52d27a0735e936fc8425c4a30e58a3b43517d21 [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.topology.web;
19
20import java.util.ArrayList;
21import java.util.HashMap;
22import java.util.List;
23import java.util.Map;
24import java.util.Map.Entry;
25
26import net.floodlightcontroller.core.IFloodlightProviderService;
27import net.floodlightcontroller.core.IOFSwitch;
28import net.floodlightcontroller.topology.ITopologyService;
29
30import org.openflow.util.HexString;
31import org.restlet.data.Form;
32import org.restlet.resource.Get;
33import org.restlet.resource.ServerResource;
34
35/**
36 * Returns a JSON map of <ClusterId, List<SwitchDpids>>
37 */
38public class SwitchClustersResource extends ServerResource {
39 @Get("json")
40 public Map<String, List<String>> retrieve() {
41 IFloodlightProviderService floodlightProvider =
42 (IFloodlightProviderService)getContext().getAttributes().
43 get(IFloodlightProviderService.class.getCanonicalName());
44 ITopologyService topology =
45 (ITopologyService)getContext().getAttributes().
46 get(ITopologyService.class.getCanonicalName());
47
48 Form form = getQuery();
49 String queryType = form.getFirstValue("type", true);
50 boolean openflowDomain = true;
51 if (queryType != null && "l2".equals(queryType)) {
52 openflowDomain = false;
53 }
54
55 Map<String, List<String>> switchClusterMap = new HashMap<String, List<String>>();
56 for (Entry<Long, IOFSwitch> entry : floodlightProvider.getSwitches().entrySet()) {
57 Long clusterDpid =
58 (openflowDomain
59 ? topology.getOpenflowDomainId(entry.getValue().getId())
60 :topology.getL2DomainId(entry.getValue().getId()));
61 List<String> switchesInCluster = switchClusterMap.get(HexString.toHexString(clusterDpid));
62 if (switchesInCluster != null) {
63 switchesInCluster.add(HexString.toHexString(entry.getKey()));
64 } else {
65 List<String> l = new ArrayList<String>();
66 l.add(HexString.toHexString(entry.getKey()));
67 switchClusterMap.put(HexString.toHexString(clusterDpid), l);
68 }
69 }
70 return switchClusterMap;
71 }
72}