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