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.core.web; |
| 19 | |
| 20 | import java.io.UnsupportedEncodingException; |
| 21 | import java.net.URLDecoder; |
| 22 | import java.util.HashMap; |
| 23 | import java.util.List; |
| 24 | import java.util.Map; |
| 25 | |
| 26 | import org.openflow.util.HexString; |
| 27 | import org.restlet.resource.Get; |
| 28 | |
| 29 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 30 | import net.floodlightcontroller.counter.CounterStore.NetworkLayer; |
| 31 | import net.floodlightcontroller.counter.ICounterStoreService; |
| 32 | |
| 33 | /** |
| 34 | * Get the counter categories for a particular switch |
| 35 | * @author readams |
| 36 | */ |
| 37 | public class SwitchCounterCategoriesResource extends CounterResourceBase { |
| 38 | @Get("json") |
| 39 | public Map<String, Object> retrieve() { |
| 40 | IFloodlightProviderService floodlightProvider = |
| 41 | (IFloodlightProviderService)getContext().getAttributes(). |
| 42 | get(IFloodlightProviderService.class.getCanonicalName()); |
| 43 | HashMap<String,Object> model = new HashMap<String,Object>(); |
| 44 | |
| 45 | String switchID = (String) getRequestAttributes().get("switchId"); |
| 46 | String counterName = (String) getRequestAttributes().get("counterName"); |
| 47 | String layer = (String) getRequestAttributes().get("layer"); |
| 48 | |
| 49 | Long[] switchDpids; |
| 50 | if (switchID.equalsIgnoreCase("all")) { |
| 51 | switchDpids = floodlightProvider.getSwitches().keySet().toArray(new Long[0]); |
| 52 | for (Long dpid : switchDpids) { |
| 53 | switchID = HexString.toHexString(dpid); |
| 54 | |
| 55 | getOneSwitchCounterCategoriesJson(model, switchID, counterName, layer); |
| 56 | } |
| 57 | } else { |
| 58 | getOneSwitchCounterCategoriesJson(model, switchID, counterName, layer); |
| 59 | } |
| 60 | |
| 61 | return model; |
| 62 | } |
| 63 | |
| 64 | protected void getOneSwitchCounterCategoriesJson(Map<String, Object> model, |
| 65 | String switchID, |
| 66 | String counterName, |
| 67 | String layer) { |
| 68 | String fullCounterName = ""; |
| 69 | NetworkLayer nl = NetworkLayer.L3; |
| 70 | |
| 71 | try { |
| 72 | counterName = URLDecoder.decode(counterName, "UTF-8"); |
| 73 | layer = URLDecoder.decode(layer, "UTF-8"); |
| 74 | fullCounterName = switchID + ICounterStoreService.TitleDelimitor + counterName; |
| 75 | } catch (UnsupportedEncodingException e) { |
| 76 | //Just leave counterTitle undecoded if there is an issue - fail silently |
| 77 | } |
| 78 | |
| 79 | if (layer.compareToIgnoreCase("4") == 0) { |
| 80 | nl = NetworkLayer.L4; |
| 81 | } |
| 82 | List<String> categories = this.counterStore.getAllCategories(fullCounterName, nl); |
| 83 | if (categories != null) { |
| 84 | model.put(fullCounterName + "." + layer, categories); |
| 85 | } |
| 86 | } |
| 87 | } |