3 * * Copyright 2014-2015 Open Networking Laboratory
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
9 * * http://www.apache.org/licenses/LICENSE-2.0
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.
19 package org.onosproject.provider.pcep.tunnel.impl;
22 import org.apache.felix.scr.annotations.Reference;
23 import org.apache.felix.scr.annotations.ReferenceCardinality;
24 import org.jboss.netty.util.HashedWheelTimer;
25 import org.jboss.netty.util.Timeout;
26 import org.jboss.netty.util.TimerTask;
27 import org.onlab.util.Timer;
28 import org.onosproject.pcep.api.PcepController;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import java.util.concurrent.TimeUnit;
35 * Sends Stats Request and collect the tunnel statistics with a time interval.
37 public class TunnelStatsCollector implements TimerTask {
38 private final Logger log = LoggerFactory.getLogger(getClass());
40 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
41 protected PcepController controller;
43 private int refreshInterval;
44 private final HashedWheelTimer timer = Timer.getTimer();
46 private String pcepTunnelId;
47 private Timeout timeout;
48 private volatile boolean stopped;
52 * Greate a tunnel status collector object.
54 * @param id tunnel whose status data will be collected
55 * @param refreshInterval time interval for collecting statistic
57 public TunnelStatsCollector(String id, int refreshInterval) {
58 this.pcepTunnelId = id;
59 this.refreshInterval = refreshInterval;
63 public void run(Timeout timeout) throws Exception {
64 if (stopped || timeout.isCancelled()) {
67 log.trace("Collecting stats for {}", pcepTunnelId);
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);
78 private void sendTunnelStatistic() {
79 controller.getTunnelStatistics(pcepTunnelId);
83 synchronized void adjustPollInterval(int pollInterval) {
84 this.refreshInterval = pollInterval;
88 * Starts the collector.
90 public synchronized void start() {
91 log.info("Starting Tunnel Stats collection thread for {}", pcepTunnelId);
93 timeout = timer.newTimeout(this, 1, TimeUnit.SECONDS);
97 * Stops the collector.
99 public synchronized void stop() {
100 log.info("Stopping Tunnel Stats collection thread for {}", pcepTunnelId);