Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | (function () { |
| 19 | 'use strict'; |
| 20 | |
| 21 | // injected refs |
| 22 | var $log, fs, flash, wss, api; |
| 23 | |
| 24 | // configuration |
| 25 | var allTrafficTypes = [ |
| 26 | 'flowStatsBytes', |
| 27 | 'portStatsBitSec', |
| 28 | 'portStatsPktSec' |
| 29 | ], |
| 30 | allTrafficMsgs = [ |
| 31 | 'Flow Stats (bytes)', |
| 32 | 'Port Stats (bits / second)', |
| 33 | 'Port Stats (packets / second)' |
| 34 | ]; |
| 35 | |
| 36 | // internal state |
| 37 | var mode = null, |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame^] | 38 | currentIndex = 0, |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 39 | allIndex = 0; |
| 40 | |
| 41 | // === ----------------------------------------------------- |
| 42 | |
| 43 | function cancelTraffic() { |
| 44 | $log.debug('Topo2Traffic: Cancel Traffic'); |
| 45 | |
| 46 | if (!mode) { |
| 47 | return false; |
| 48 | } |
| 49 | mode = null; |
| 50 | wss.sendEvent('topo2CancelTraffic'); |
| 51 | flash.flash('Traffic monitoring canceled'); |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | function showAllTraffic() { |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 56 | $log.debug('Topo2Traffic: Show All Traffic:', allTrafficTypes[allIndex]); |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 57 | |
| 58 | mode = 'allFlowPort'; |
| 59 | wss.sendEvent('topo2RequestAllTraffic', { |
| 60 | trafficType: allTrafficTypes[allIndex] |
| 61 | }); |
| 62 | flash.flash(allTrafficMsgs[allIndex]); |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame^] | 63 | currentIndex = allIndex; |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 64 | allIndex = (allIndex + 1) % 3; |
| 65 | } |
| 66 | |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame^] | 67 | function selectedTrafficOverlay() { |
| 68 | return allTrafficTypes[currentIndex]; |
| 69 | } |
| 70 | |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 71 | // === ----------------------------------------------------- |
| 72 | |
| 73 | angular.module('ovTopo2') |
| 74 | .factory('Topo2TrafficService', [ |
| 75 | '$log', 'FnService', 'FlashService', 'WebSocketService', |
| 76 | |
| 77 | function (_$log_, _fs_, _flash_, _wss_) { |
| 78 | $log = _$log_; |
| 79 | fs = _fs_; |
| 80 | flash = _flash_; |
| 81 | wss = _wss_; |
| 82 | |
| 83 | return { |
| 84 | initTraffic: function (_api_) { api = _api_; }, |
| 85 | destroyTraffic: function () {}, |
| 86 | |
| 87 | // invoked from toolbar overlay buttons or keystrokes |
| 88 | cancelTraffic: cancelTraffic, |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame^] | 89 | showAllTraffic: showAllTraffic, |
| 90 | selectedTrafficOverlay: selectedTrafficOverlay |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | ]); |
| 94 | }()); |