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, |
| 62 | i, ldata; |
| 63 | |
| 64 | for (i=0; i<n; i++) { |
| 65 | ldata = api.findLinkById(p.links[i]); |
| 66 | if (ldata && ldata.el) { |
| 67 | ldata.el.classed(p.class, true); |
| 68 | ldata.label = p.labels[i]; |
| 69 | } |
| 70 | } |
| 71 | }); |
| 72 | |
| 73 | api.updateLinks(); |
| 74 | } |
| 75 | |
| 76 | // === ----------------------------------------------------- |
| 77 | // Helper functions |
| 78 | |
| 79 | function requestDeviceLinkFlows() { |
| 80 | var hov = api.hovered(); |
| 81 | |
| 82 | function hoverValid() { |
| 83 | return hoverMode === hoverModeFlows && |
| 84 | hov && (hov.class === 'device'); |
| 85 | } |
| 86 | |
| 87 | if (api.validateSelectionContext()) { |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 88 | wss.sendEvent('requestDeviceLinkFlows', { |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 89 | ids: api.selectOrder(), |
| 90 | hover: hoverValid() ? hov.id : '' |
| 91 | }); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | function requestRelatedIntents() { |
| 96 | var hov = api.hovered(); |
| 97 | |
| 98 | function hoverValid() { |
| 99 | return hoverMode === hoverModeIntents && |
| 100 | hov && (hov.class === 'host' || hov.class === 'device'); |
| 101 | } |
| 102 | |
| 103 | if (api.validateSelectionContext()) { |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 104 | wss.sendEvent('requestRelatedIntents', { |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 105 | ids: api.selectOrder(), |
| 106 | hover: hoverValid() ? hov.id : '' |
| 107 | }); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | |
| 112 | // === ----------------------------------------------------- |
| 113 | // Traffic requests |
| 114 | |
| 115 | function cancelTraffic() { |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 116 | wss.sendEvent('cancelTraffic'); |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // invoked in response to change in selection and/or mouseover/out: |
| 120 | function requestTrafficForMode() { |
| 121 | if (hoverMode === hoverModeFlows) { |
| 122 | requestDeviceLinkFlows(); |
| 123 | } else if (hoverMode === hoverModeIntents) { |
| 124 | requestRelatedIntents(); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // === ----------------------------- |
| 129 | // keystroke commands |
| 130 | |
| 131 | // keystroke-right-arrow (see topo.js) |
| 132 | function showNextIntentAction() { |
| 133 | hoverMode = hoverModeNone; |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 134 | wss.sendEvent('requestNextRelatedIntent'); |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 135 | flash.flash('>'); |
| 136 | } |
| 137 | |
| 138 | // keystroke-left-arrow (see topo.js) |
| 139 | function showPrevIntentAction() { |
| 140 | hoverMode = hoverModeNone; |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 141 | wss.sendEvent('requestPrevRelatedIntent'); |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 142 | flash.flash('<'); |
| 143 | } |
| 144 | |
| 145 | // keystroke-W (see topo.js) |
| 146 | function showSelectedIntentTrafficAction() { |
| 147 | hoverMode = hoverModeNone; |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 148 | wss.sendEvent('requestSelectedIntentTraffic'); |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 149 | flash.flash('Traffic on Selected Path'); |
| 150 | } |
| 151 | |
| 152 | // keystroke-A (see topo.js) |
| 153 | function showAllTrafficAction() { |
| 154 | hoverMode = hoverModeAll; |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 155 | wss.sendEvent('requestAllTraffic'); |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 156 | flash.flash('All Traffic'); |
| 157 | } |
| 158 | |
| 159 | // === ----------------------------- |
| 160 | // action buttons on detail panel |
| 161 | |
| 162 | // also, keystroke-V (see topo.js) |
| 163 | function showRelatedIntentsAction () { |
| 164 | hoverMode = hoverModeIntents; |
| 165 | requestRelatedIntents(); |
| 166 | flash.flash('Related Paths'); |
| 167 | } |
| 168 | |
| 169 | function addHostIntentAction () { |
| 170 | var so = api.selectOrder(); |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 171 | wss.sendEvent('addHostIntent', { |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 172 | one: so[0], |
| 173 | two: so[1], |
| 174 | ids: so |
| 175 | }); |
| 176 | flash.flash('Host-to-Host flow added'); |
| 177 | } |
| 178 | |
| 179 | function addMultiSourceIntentAction () { |
| 180 | var so = api.selectOrder(); |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 181 | wss.sendEvent('addMultiSourceIntent', { |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 182 | src: so.slice(0, so.length - 1), |
| 183 | dst: so[so.length - 1], |
| 184 | ids: so |
| 185 | }); |
| 186 | flash.flash('Multi-Source flow added'); |
| 187 | } |
| 188 | |
| 189 | // also, keystroke-F (see topo.js) |
| 190 | function showDeviceLinkFlowsAction () { |
| 191 | hoverMode = hoverModeFlows; |
| 192 | requestDeviceLinkFlows(); |
| 193 | flash.flash('Device Flows'); |
| 194 | } |
| 195 | |
| 196 | |
| 197 | // === ----------------------------------------------------- |
| 198 | // === MODULE DEFINITION === |
| 199 | |
| 200 | angular.module('ovTopo') |
Simon Hunt | 75ec969 | 2015-02-11 16:40:36 -0800 | [diff] [blame] | 201 | .factory('TopoTrafficService', |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 202 | ['$log', 'FnService', 'FlashService', 'WebSocketService', |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 203 | |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 204 | function (_$log_, _fs_, _flash_, _wss_) { |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 205 | $log = _$log_; |
| 206 | fs = _fs_; |
| 207 | flash = _flash_; |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 208 | wss = _wss_; |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 209 | |
| 210 | function initTraffic(_api_) { |
| 211 | api = _api_; |
| 212 | } |
| 213 | |
| 214 | function destroyTraffic() { } |
| 215 | |
| 216 | return { |
| 217 | initTraffic: initTraffic, |
| 218 | destroyTraffic: destroyTraffic, |
| 219 | |
| 220 | showTraffic: showTraffic, |
| 221 | |
| 222 | cancelTraffic: cancelTraffic, |
| 223 | requestTrafficForMode: requestTrafficForMode, |
| 224 | showRelatedIntentsAction: showRelatedIntentsAction, |
| 225 | addHostIntentAction: addHostIntentAction, |
| 226 | addMultiSourceIntentAction: addMultiSourceIntentAction, |
| 227 | showDeviceLinkFlowsAction: showDeviceLinkFlowsAction, |
| 228 | showNextIntentAction: showNextIntentAction, |
| 229 | showPrevIntentAction: showPrevIntentAction, |
| 230 | showSelectedIntentTrafficAction: showSelectedIntentTrafficAction, |
| 231 | showAllTrafficAction: showAllTrafficAction |
| 232 | }; |
| 233 | }]); |
| 234 | }()); |