blob: e64daa46a3d732ffe7087ad56e27c12f89f3d600 [file] [log] [blame]
chengfan2fff70f2015-08-24 18:20:19 -05001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present 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;
chengfan2fff70f2015-08-24 18:20:19 -050022import org.onlab.util.Timer;
23import org.onosproject.pcep.api.PcepController;
24import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
26
Yuta HIGUCHI19afc032017-05-20 23:44:17 -070027import io.netty.util.Timeout;
28import io.netty.util.TimerTask;
29
chengfan2fff70f2015-08-24 18:20:19 -050030import 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;
chengfan2fff70f2015-08-24 18:20:19 -050042
43 private String pcepTunnelId;
44 private Timeout timeout;
45 private volatile boolean stopped;
46
47
48 /**
Yuta HIGUCHI19afc032017-05-20 23:44:17 -070049 * Create a tunnel status collector object.
chengfan2fff70f2015-08-24 18:20:19 -050050 *
51 * @param id tunnel whose status data will be collected
52 * @param refreshInterval time interval for collecting statistic
53 */
54 public TunnelStatsCollector(String id, int refreshInterval) {
55 this.pcepTunnelId = id;
56 this.refreshInterval = refreshInterval;
57 }
58
59 @Override
60 public void run(Timeout timeout) throws Exception {
61 if (stopped || timeout.isCancelled()) {
62 return;
63 }
64 log.trace("Collecting stats for {}", pcepTunnelId);
65
66 sendTunnelStatistic();
67 if (!stopped && !timeout.isCancelled()) {
68 log.trace("Scheduling stats collection in {} seconds for {}",
69 this.refreshInterval, pcepTunnelId);
Yuta HIGUCHI19afc032017-05-20 23:44:17 -070070 timeout.timer().newTimeout(this, refreshInterval, TimeUnit.SECONDS);
chengfan2fff70f2015-08-24 18:20:19 -050071 }
72
73 }
74
75 private void sendTunnelStatistic() {
76 controller.getTunnelStatistics(pcepTunnelId);
77
78 }
79
80 synchronized void adjustPollInterval(int pollInterval) {
81 this.refreshInterval = pollInterval;
82 }
83
84 /**
85 * Starts the collector.
86 */
87 public synchronized void start() {
88 log.info("Starting Tunnel Stats collection thread for {}", pcepTunnelId);
89 stopped = false;
Yuta HIGUCHI19afc032017-05-20 23:44:17 -070090 timeout = Timer.newTimeout(this, 1, TimeUnit.SECONDS);
chengfan2fff70f2015-08-24 18:20:19 -050091 }
92
93 /**
94 * Stops the collector.
95 */
96 public synchronized void stop() {
97 log.info("Stopping Tunnel Stats collection thread for {}", pcepTunnelId);
98 stopped = true;
99 timeout.cancel();
100 }
101}