blob: 01a042845f85b1607776c709f7a94726a0ba1796 [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
18/**
19 *
20 */
21package net.floodlightcontroller.counter;
22
23import java.util.Date;
24
25
26
27/**
28 * This is a simple counter implementation that doesn't support data series.
29 * The idea is that floodlight only keeps the realtime value for each counter,
30 * statd, a statistics collection daemon, samples counters at a user-defined interval
31 * and pushes the values to a database, which keeps time-based data series.
32 * @author Kanzhe
33 *
34 */
35public class SimpleCounter implements ICounter {
36
37 protected CounterValue counter;
38 protected Date samplingTime;
39 protected Date startDate;
40
41 /**
42 * Factory method to create a new counter instance.
43 *
44 * @param startDate
45 * @return
46 */
47 public static ICounter createCounter(Date startDate, CounterValue.CounterType type) {
48 SimpleCounter cc = new SimpleCounter(startDate, type);
49 return cc;
50 }
51
52 /**
53 * Factory method to create a copy of a counter instance.
54 *
55 * @param startDate
56 * @return
57 */
58 public static ICounter createCounter(ICounter copy) {
59 if (copy == null ||
60 copy.getCounterDate() == null ||
61 copy.getCounterValue() == null) {
62 return null;
63 }
64
65 SimpleCounter cc = new SimpleCounter(copy.getCounterDate(),
66 copy.getCounterValue().getType());
67 cc.setCounter(copy.getCounterDate(), copy.getCounterValue());
68 return cc;
69 }
70
71 /**
72 * Protected constructor - use createCounter factory method instead
73 * @param startDate
74 */
75 protected SimpleCounter(Date startDate, CounterValue.CounterType type) {
76 init(startDate, type);
77 }
78
79 protected void init(Date startDate, CounterValue.CounterType type) {
80 this.startDate = startDate;
81 this.samplingTime = new Date();
82 this.counter = new CounterValue(type);
83 }
84
85 /**
86 * This is the key method that has to be both fast and very thread-safe.
87 */
88 @Override
89 synchronized public void increment() {
90 this.increment(new Date(), (long)1);
91 }
92
93 @Override
94 synchronized public void increment(Date d, long delta) {
95 this.samplingTime = d;
96 this.counter.increment(delta);
97 }
98
99 synchronized public void setCounter(Date d, CounterValue value) {
100 this.samplingTime = d;
101 this.counter = value;
102 }
103
104 /**
105 * This is the method to retrieve the current value.
106 */
107 @Override
108 synchronized public CounterValue getCounterValue() {
109 return this.counter;
110 }
111
112 /**
113 * This is the method to retrieve the last sampling time.
114 */
115 @Override
116 synchronized public Date getCounterDate() {
117 return this.samplingTime;
118 }
119
120 /**
121 * Reset value.
122 */
123 @Override
124 synchronized public void reset(Date startDate) {
125 init(startDate, this.counter.getType());
126 }
127
128 @Override
129 /**
130 * This method only returns the real-time value.
131 */
132 synchronized public CountSeries snapshot(DateSpan dateSpan) {
133 long[] values = new long[1];
134 values[0] = this.counter.getLong();
135 return new CountSeries(this.samplingTime, DateSpan.DAYS, values);
136 }
137}