Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
| 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 | |
| 18 | package net.floodlightcontroller.topology.web; |
| 19 | |
| 20 | import java.util.ArrayList; |
| 21 | import java.util.HashMap; |
| 22 | import java.util.List; |
| 23 | import java.util.Map; |
| 24 | import java.util.Map.Entry; |
| 25 | |
| 26 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 27 | import net.floodlightcontroller.core.IOFSwitch; |
| 28 | import net.floodlightcontroller.topology.ITopologyService; |
| 29 | |
| 30 | import org.openflow.util.HexString; |
| 31 | import org.restlet.data.Form; |
| 32 | import org.restlet.resource.Get; |
| 33 | import org.restlet.resource.ServerResource; |
| 34 | |
| 35 | /** |
| 36 | * Returns a JSON map of <ClusterId, List<SwitchDpids>> |
| 37 | */ |
| 38 | public 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 | } |