blob: 39249c5c6e80a1696a5f515126e489d17c2a86fc [file] [log] [blame]
chengfan2fff70f2015-08-24 18:20:19 -05001/*
2 *
3 * * Copyright 2014-2015 Open Networking Laboratory
4 * *
5 * * Licensed under the Apache License, Version 2.0 (the "License");
6 * * you may not use this file except in compliance with the License.
7 * * You may obtain 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,
13 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * * See the License for the specific language governing permissions and
15 * * limitations under the License.
16 *
17 */
18
19package org.onosproject.provider.pcep.tunnel.impl;
20
21
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.jboss.netty.util.HashedWheelTimer;
25import org.jboss.netty.util.Timeout;
26import org.jboss.netty.util.TimerTask;
27import org.onlab.util.Timer;
28import org.onosproject.pcep.api.PcepController;
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32import java.util.concurrent.TimeUnit;
33
34/*
35 * Sends Stats Request and collect the tunnel statistics with a time interval.
36 */
37public class TunnelStatsCollector implements TimerTask {
38 private final Logger log = LoggerFactory.getLogger(getClass());
39
40 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
41 protected PcepController controller;
42
43 private int refreshInterval;
44 private final HashedWheelTimer timer = Timer.getTimer();
45
46 private String pcepTunnelId;
47 private Timeout timeout;
48 private volatile boolean stopped;
49
50
51 /**
52 * Greate a tunnel status collector object.
53 *
54 * @param id tunnel whose status data will be collected
55 * @param refreshInterval time interval for collecting statistic
56 */
57 public TunnelStatsCollector(String id, int refreshInterval) {
58 this.pcepTunnelId = id;
59 this.refreshInterval = refreshInterval;
60 }
61
62 @Override
63 public void run(Timeout timeout) throws Exception {
64 if (stopped || timeout.isCancelled()) {
65 return;
66 }
67 log.trace("Collecting stats for {}", pcepTunnelId);
68
69 sendTunnelStatistic();
70 if (!stopped && !timeout.isCancelled()) {
71 log.trace("Scheduling stats collection in {} seconds for {}",
72 this.refreshInterval, pcepTunnelId);
73 timeout.getTimer().newTimeout(this, refreshInterval, TimeUnit.SECONDS);
74 }
75
76 }
77
78 private void sendTunnelStatistic() {
79 controller.getTunnelStatistics(pcepTunnelId);
80
81 }
82
83 synchronized void adjustPollInterval(int pollInterval) {
84 this.refreshInterval = pollInterval;
85 }
86
87 /**
88 * Starts the collector.
89 */
90 public synchronized void start() {
91 log.info("Starting Tunnel Stats collection thread for {}", pcepTunnelId);
92 stopped = false;
93 timeout = timer.newTimeout(this, 1, TimeUnit.SECONDS);
94 }
95
96 /**
97 * Stops the collector.
98 */
99 public synchronized void stop() {
100 log.info("Stopping Tunnel Stats collection thread for {}", pcepTunnelId);
101 stopped = true;
102 timeout.cancel();
103 }
104}