Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | ONOS GUI -- Topology Traffic Module. |
| 19 | Defines behavior for viewing different traffic modes. |
| 20 | */ |
| 21 | |
| 22 | (function () { |
| 23 | 'use strict'; |
| 24 | |
| 25 | // injected refs |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 26 | var $log, fs, flash, wss; |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 27 | |
| 28 | // api to topoForce |
| 29 | var api; |
| 30 | /* |
| 31 | clearLinkTrafficStyle() |
| 32 | removeLinkLabels() |
| 33 | updateLinks() |
| 34 | findLinkById( id ) |
| 35 | hovered() |
| 36 | validateSelectionContext() |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 37 | */ |
| 38 | |
| 39 | // constants |
| 40 | var hoverModeNone = 0, |
| 41 | hoverModeAll = 1, |
| 42 | hoverModeFlows = 2, |
| 43 | hoverModeIntents = 3; |
| 44 | |
| 45 | // internal state |
| 46 | var hoverMode = hoverModeNone; |
| 47 | |
| 48 | |
| 49 | // === ----------------------------------------------------- |
| 50 | // Event Handlers |
| 51 | |
| 52 | function showTraffic(data) { |
| 53 | var paths = data.paths; |
| 54 | |
| 55 | api.clearLinkTrafficStyle(); |
| 56 | api.removeLinkLabels(); |
| 57 | |
| 58 | // Now highlight all links in the paths payload, and attach |
| 59 | // labels to them, if they are defined. |
| 60 | paths.forEach(function (p) { |
| 61 | var n = p.links.length, |
Simon Hunt | 9664137 | 2015-06-02 15:53:25 -0700 | [diff] [blame] | 62 | i, ldata, lab, units, magnitude, portcls; |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 63 | |
| 64 | for (i=0; i<n; i++) { |
| 65 | ldata = api.findLinkById(p.links[i]); |
Simon Hunt | 9664137 | 2015-06-02 15:53:25 -0700 | [diff] [blame] | 66 | lab = p.labels[i]; |
| 67 | |
| 68 | if (ldata && !ldata.el.empty()) { |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 69 | ldata.el.classed(p.class, true); |
Simon Hunt | 9664137 | 2015-06-02 15:53:25 -0700 | [diff] [blame] | 70 | ldata.label = lab; |
| 71 | |
Srikanth Vavilapalli | 8c1ccca | 2015-06-08 19:23:24 -0700 | [diff] [blame] | 72 | if (fs.endsWith(lab, 'bps')) { |
Simon Hunt | 9664137 | 2015-06-02 15:53:25 -0700 | [diff] [blame] | 73 | // inject additional styling for port-based traffic |
| 74 | units = lab.substring(lab.length-4); |
| 75 | portcls = 'port-traffic-' + units; |
| 76 | |
| 77 | // for GBps |
| 78 | if (units.substring(0,1) === 'G') { |
| 79 | magnitude = fs.parseBitRate(lab); |
| 80 | if (magnitude >= 9) { |
| 81 | portcls += '-choked' |
| 82 | } |
| 83 | } |
| 84 | ldata.el.classed(portcls, true); |
| 85 | } |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | }); |
| 89 | |
| 90 | api.updateLinks(); |
| 91 | } |
| 92 | |
| 93 | // === ----------------------------------------------------- |
| 94 | // Helper functions |
| 95 | |
| 96 | function requestDeviceLinkFlows() { |
| 97 | var hov = api.hovered(); |
| 98 | |
| 99 | function hoverValid() { |
| 100 | return hoverMode === hoverModeFlows && |
| 101 | hov && (hov.class === 'device'); |
| 102 | } |
| 103 | |
| 104 | if (api.validateSelectionContext()) { |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 105 | wss.sendEvent('requestDeviceLinkFlows', { |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 106 | ids: api.selectOrder(), |
| 107 | hover: hoverValid() ? hov.id : '' |
| 108 | }); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | function requestRelatedIntents() { |
| 113 | var hov = api.hovered(); |
| 114 | |
| 115 | function hoverValid() { |
| 116 | return hoverMode === hoverModeIntents && |
| 117 | hov && (hov.class === 'host' || hov.class === 'device'); |
| 118 | } |
| 119 | |
| 120 | if (api.validateSelectionContext()) { |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 121 | wss.sendEvent('requestRelatedIntents', { |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 122 | ids: api.selectOrder(), |
| 123 | hover: hoverValid() ? hov.id : '' |
| 124 | }); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | |
| 129 | // === ----------------------------------------------------- |
| 130 | // Traffic requests |
| 131 | |
| 132 | function cancelTraffic() { |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 133 | wss.sendEvent('cancelTraffic'); |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | // invoked in response to change in selection and/or mouseover/out: |
| 137 | function requestTrafficForMode() { |
| 138 | if (hoverMode === hoverModeFlows) { |
| 139 | requestDeviceLinkFlows(); |
| 140 | } else if (hoverMode === hoverModeIntents) { |
| 141 | requestRelatedIntents(); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // === ----------------------------- |
| 146 | // keystroke commands |
| 147 | |
| 148 | // keystroke-right-arrow (see topo.js) |
| 149 | function showNextIntentAction() { |
| 150 | hoverMode = hoverModeNone; |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 151 | wss.sendEvent('requestNextRelatedIntent'); |
Bri Prebilic Cole | 9cf1a8d | 2015-04-21 13:15:29 -0700 | [diff] [blame] | 152 | flash.flash('Next related intent'); |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | // keystroke-left-arrow (see topo.js) |
| 156 | function showPrevIntentAction() { |
| 157 | hoverMode = hoverModeNone; |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 158 | wss.sendEvent('requestPrevRelatedIntent'); |
Bri Prebilic Cole | 9cf1a8d | 2015-04-21 13:15:29 -0700 | [diff] [blame] | 159 | flash.flash('Previous related intent'); |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // keystroke-W (see topo.js) |
| 163 | function showSelectedIntentTrafficAction() { |
| 164 | hoverMode = hoverModeNone; |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 165 | wss.sendEvent('requestSelectedIntentTraffic'); |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 166 | flash.flash('Traffic on Selected Path'); |
| 167 | } |
| 168 | |
| 169 | // keystroke-A (see topo.js) |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 170 | function showAllFlowTrafficAction() { |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 171 | hoverMode = hoverModeAll; |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 172 | wss.sendEvent('requestAllFlowTraffic'); |
| 173 | flash.flash('All Flow Traffic'); |
| 174 | } |
| 175 | |
| 176 | // keystroke-A (see topo.js) |
| 177 | function showAllPortTrafficAction() { |
| 178 | hoverMode = hoverModeAll; |
| 179 | wss.sendEvent('requestAllPortTraffic'); |
| 180 | flash.flash('All Port Traffic'); |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | // === ----------------------------- |
| 184 | // action buttons on detail panel |
| 185 | |
| 186 | // also, keystroke-V (see topo.js) |
| 187 | function showRelatedIntentsAction () { |
| 188 | hoverMode = hoverModeIntents; |
| 189 | requestRelatedIntents(); |
| 190 | flash.flash('Related Paths'); |
| 191 | } |
| 192 | |
| 193 | function addHostIntentAction () { |
| 194 | var so = api.selectOrder(); |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 195 | wss.sendEvent('addHostIntent', { |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 196 | one: so[0], |
| 197 | two: so[1], |
| 198 | ids: so |
| 199 | }); |
| 200 | flash.flash('Host-to-Host flow added'); |
| 201 | } |
| 202 | |
| 203 | function addMultiSourceIntentAction () { |
| 204 | var so = api.selectOrder(); |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 205 | wss.sendEvent('addMultiSourceIntent', { |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 206 | src: so.slice(0, so.length - 1), |
| 207 | dst: so[so.length - 1], |
| 208 | ids: so |
| 209 | }); |
| 210 | flash.flash('Multi-Source flow added'); |
| 211 | } |
| 212 | |
| 213 | // also, keystroke-F (see topo.js) |
| 214 | function showDeviceLinkFlowsAction () { |
| 215 | hoverMode = hoverModeFlows; |
| 216 | requestDeviceLinkFlows(); |
| 217 | flash.flash('Device Flows'); |
| 218 | } |
| 219 | |
| 220 | |
| 221 | // === ----------------------------------------------------- |
| 222 | // === MODULE DEFINITION === |
| 223 | |
| 224 | angular.module('ovTopo') |
Simon Hunt | 75ec969 | 2015-02-11 16:40:36 -0800 | [diff] [blame] | 225 | .factory('TopoTrafficService', |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 226 | ['$log', 'FnService', 'FlashService', 'WebSocketService', |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 227 | |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 228 | function (_$log_, _fs_, _flash_, _wss_) { |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 229 | $log = _$log_; |
| 230 | fs = _fs_; |
| 231 | flash = _flash_; |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 232 | wss = _wss_; |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 233 | |
| 234 | function initTraffic(_api_) { |
| 235 | api = _api_; |
| 236 | } |
| 237 | |
| 238 | function destroyTraffic() { } |
| 239 | |
| 240 | return { |
| 241 | initTraffic: initTraffic, |
| 242 | destroyTraffic: destroyTraffic, |
| 243 | |
| 244 | showTraffic: showTraffic, |
| 245 | |
| 246 | cancelTraffic: cancelTraffic, |
| 247 | requestTrafficForMode: requestTrafficForMode, |
| 248 | showRelatedIntentsAction: showRelatedIntentsAction, |
| 249 | addHostIntentAction: addHostIntentAction, |
| 250 | addMultiSourceIntentAction: addMultiSourceIntentAction, |
| 251 | showDeviceLinkFlowsAction: showDeviceLinkFlowsAction, |
| 252 | showNextIntentAction: showNextIntentAction, |
| 253 | showPrevIntentAction: showPrevIntentAction, |
| 254 | showSelectedIntentTrafficAction: showSelectedIntentTrafficAction, |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 255 | showAllFlowTrafficAction: showAllFlowTrafficAction, |
| 256 | showAllPortTrafficAction: showAllPortTrafficAction |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 257 | }; |
| 258 | }]); |
| 259 | }()); |