chengfan | 2fff70f | 2015-08-24 18:20:19 -0500 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
chengfan | 2fff70f | 2015-08-24 18:20:19 -0500 | [diff] [blame] | 3 | * |
Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 4 | * 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 |
chengfan | 2fff70f | 2015-08-24 18:20:19 -0500 | [diff] [blame] | 7 | * |
Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 8 | * 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. |
chengfan | 2fff70f | 2015-08-24 18:20:19 -0500 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | package org.onosproject.provider.pcep.tunnel.impl; |
| 18 | |
| 19 | |
| 20 | import org.apache.felix.scr.annotations.Reference; |
| 21 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 22 | import org.jboss.netty.util.HashedWheelTimer; |
| 23 | import org.jboss.netty.util.Timeout; |
| 24 | import org.jboss.netty.util.TimerTask; |
| 25 | import org.onlab.util.Timer; |
| 26 | import org.onosproject.pcep.api.PcepController; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | import java.util.concurrent.TimeUnit; |
| 31 | |
| 32 | /* |
| 33 | * Sends Stats Request and collect the tunnel statistics with a time interval. |
| 34 | */ |
| 35 | public 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 | } |