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.util.HashMap; |
| 21 | import java.util.Iterator; |
| 22 | import java.util.Map; |
| 23 | import java.util.Map.Entry; |
| 24 | |
| 25 | import net.floodlightcontroller.counter.CounterValue; |
| 26 | import net.floodlightcontroller.counter.ICounter; |
| 27 | |
| 28 | import org.restlet.resource.Get; |
| 29 | |
| 30 | public class CounterResource extends CounterResourceBase { |
| 31 | @Get("json") |
| 32 | public Map<String, Object> retrieve() { |
| 33 | String counterTitle = |
| 34 | (String) getRequestAttributes().get("counterTitle"); |
| 35 | Map<String, Object> model = new HashMap<String,Object>(); |
| 36 | CounterValue v; |
| 37 | if (counterTitle.equalsIgnoreCase("all")) { |
| 38 | Map<String, ICounter> counters = this.counterStore.getAll(); |
| 39 | if (counters != null) { |
| 40 | Iterator<Map.Entry<String, ICounter>> it = |
| 41 | counters.entrySet().iterator(); |
| 42 | while (it.hasNext()) { |
| 43 | Entry<String, ICounter> entry = it.next(); |
| 44 | String counterName = entry.getKey(); |
| 45 | v = entry.getValue().getCounterValue(); |
| 46 | |
| 47 | if (CounterValue.CounterType.LONG == v.getType()) { |
| 48 | model.put(counterName, v.getLong()); |
| 49 | } else if (v.getType() == CounterValue.CounterType.DOUBLE) { |
| 50 | model.put(counterName, v.getDouble()); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | } else { |
| 55 | ICounter counter = this.counterStore.getCounter(counterTitle); |
| 56 | if (counter != null) { |
| 57 | v = counter.getCounterValue(); |
| 58 | } else { |
| 59 | v = new CounterValue(CounterValue.CounterType.LONG); |
| 60 | } |
| 61 | |
| 62 | if (CounterValue.CounterType.LONG == v.getType()) { |
| 63 | model.put(counterTitle, v.getLong()); |
| 64 | } else if (v.getType() == CounterValue.CounterType.DOUBLE) { |
| 65 | model.put(counterTitle, v.getDouble()); |
| 66 | } |
| 67 | } |
| 68 | return model; |
| 69 | } |
| 70 | } |