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