Simon Hunt | 99ee1e2 | 2015-02-13 09:24:43 -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 layer filtering Module. |
| 19 | Provides functionality to visually differentiate between the packet and |
| 20 | optical layers of the topology. |
| 21 | */ |
| 22 | |
| 23 | (function () { |
| 24 | 'use strict'; |
| 25 | |
| 26 | // injected refs |
| 27 | var $log, fs, flash, tps, tts; |
| 28 | |
| 29 | // api to topoForce |
| 30 | var api; |
| 31 | /* |
Simon Hunt | eb0fa05 | 2015-02-17 19:20:28 -0800 | [diff] [blame] | 32 | node() // get ref to D3 selection of nodes |
| 33 | link() // get ref to D3 selection of links |
Simon Hunt | 99ee1e2 | 2015-02-13 09:24:43 -0800 | [diff] [blame] | 34 | */ |
| 35 | |
Simon Hunt | eb0fa05 | 2015-02-17 19:20:28 -0800 | [diff] [blame] | 36 | // which "layer" a particular item "belongs to" |
| 37 | var layerLookup = { |
| 38 | host: { |
| 39 | endstation: 'pkt', // default, if host event does not define type |
| 40 | router: 'pkt', |
| 41 | bgpSpeaker: 'pkt' |
| 42 | }, |
| 43 | device: { |
| 44 | switch: 'pkt', |
| 45 | roadm: 'opt' |
| 46 | }, |
| 47 | link: { |
| 48 | hostLink: 'pkt', |
| 49 | direct: 'pkt', |
| 50 | indirect: '', |
| 51 | tunnel: '', |
| 52 | optical: 'opt' |
| 53 | } |
| 54 | }; |
Simon Hunt | 99ee1e2 | 2015-02-13 09:24:43 -0800 | [diff] [blame] | 55 | |
Simon Hunt | eb0fa05 | 2015-02-17 19:20:28 -0800 | [diff] [blame] | 56 | var idPrefix = 'topo-rb-'; |
| 57 | |
| 58 | var dispatch = { |
| 59 | all: function () { suppressLayers(false); }, |
| 60 | pkt: function () { showLayer('pkt'); }, |
| 61 | opt: function () { showLayer('opt'); } |
| 62 | }, |
| 63 | filterButtons = [ |
| 64 | { text: 'All Layers', id: 'all' }, |
| 65 | { text: 'Packet Only', id: 'pkt' }, |
| 66 | { text: 'Optical Only', id: 'opt' } |
| 67 | ], |
| 68 | btnG, |
| 69 | btnDef = {}, |
| 70 | targetDiv; |
| 71 | |
| 72 | |
| 73 | function injectButtons(div) { |
| 74 | targetDiv = div; |
| 75 | |
| 76 | btnG = div.append('div').attr('id', 'topo-radio-group'); |
| 77 | |
| 78 | filterButtons.forEach(function (btn, i) { |
| 79 | var bid = btn.id, |
| 80 | txt = btn.text, |
| 81 | uid = idPrefix + bid, |
| 82 | button = btnG.append('span') |
| 83 | .attr({ |
| 84 | id: uid, |
| 85 | 'class': 'radio' |
| 86 | }) |
| 87 | .text(txt); |
| 88 | btnDef[uid] = btn; |
| 89 | |
| 90 | if (i === 0) { |
| 91 | button.classed('active', true); |
| 92 | btnG.selected = bid; |
| 93 | } |
| 94 | }); |
| 95 | |
| 96 | btnG.selectAll('span') |
| 97 | .on('click', function () { |
| 98 | var button = d3.select(this), |
| 99 | uid = button.attr('id'), |
| 100 | btn = btnDef[uid], |
| 101 | act = button.classed('active'); |
| 102 | |
| 103 | if (!act) { |
| 104 | btnG.selectAll('span').classed('active', false); |
| 105 | button.classed('active', true); |
| 106 | btnG.selected = btn.id; |
| 107 | clickAction(btn.id); |
| 108 | } |
| 109 | }); |
| 110 | } |
| 111 | |
| 112 | function clickAction(which) { |
| 113 | dispatch[which](); |
| 114 | } |
| 115 | |
| 116 | function selected() { |
| 117 | return btnG ? btnG.selected : ''; |
| 118 | } |
| 119 | |
Simon Hunt | eb0fa05 | 2015-02-17 19:20:28 -0800 | [diff] [blame] | 120 | function inLayer(d, layer) { |
| 121 | var type = d.class === 'link' ? d.type() : d.type, |
| 122 | look = layerLookup[d.class], |
| 123 | lyr = look && look[type]; |
| 124 | return lyr === layer; |
| 125 | } |
| 126 | |
| 127 | function unsuppressLayer(which) { |
| 128 | api.node().each(function (d) { |
| 129 | var node = d.el; |
| 130 | if (inLayer(d, which)) { |
| 131 | node.classed('suppressed', false); |
| 132 | } |
| 133 | }); |
| 134 | |
| 135 | api.link().each(function (d) { |
| 136 | var link = d.el; |
| 137 | if (inLayer(d, which)) { |
| 138 | link.classed('suppressed', false); |
| 139 | } |
| 140 | }); |
| 141 | } |
| 142 | |
| 143 | function suppressLayers(b) { |
| 144 | api.node().classed('suppressed', b); |
| 145 | api.link().classed('suppressed', b); |
| 146 | // d3.selectAll('svg .port').classed('inactive', false); |
| 147 | // d3.selectAll('svg .portText').classed('inactive', false); |
| 148 | } |
| 149 | |
| 150 | function showLayer(which) { |
| 151 | suppressLayers(true); |
| 152 | unsuppressLayer(which); |
| 153 | } |
Simon Hunt | 99ee1e2 | 2015-02-13 09:24:43 -0800 | [diff] [blame] | 154 | |
| 155 | // === ----------------------------------------------------- |
| 156 | // === MODULE DEFINITION === |
| 157 | |
| 158 | angular.module('ovTopo') |
| 159 | .factory('TopoFilterService', |
| 160 | ['$log', 'FnService', |
| 161 | 'FlashService', |
| 162 | 'TopoPanelService', |
| 163 | 'TopoTrafficService', |
| 164 | |
| 165 | function (_$log_, _fs_, _flash_, _tps_, _tts_) { |
| 166 | $log = _$log_; |
| 167 | fs = _fs_; |
| 168 | flash = _flash_; |
| 169 | tps = _tps_; |
| 170 | tts = _tts_; |
| 171 | |
Simon Hunt | eb0fa05 | 2015-02-17 19:20:28 -0800 | [diff] [blame] | 172 | function initFilter(_api_, div) { |
Simon Hunt | 99ee1e2 | 2015-02-13 09:24:43 -0800 | [diff] [blame] | 173 | api = _api_; |
Simon Hunt | eb0fa05 | 2015-02-17 19:20:28 -0800 | [diff] [blame] | 174 | injectButtons(div); |
Simon Hunt | 99ee1e2 | 2015-02-13 09:24:43 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Simon Hunt | eb0fa05 | 2015-02-17 19:20:28 -0800 | [diff] [blame] | 177 | function destroyFilter() { |
| 178 | targetDiv.select('#topo-radio-group').remove(); |
| 179 | btnG = null; |
| 180 | btnDef = {}; |
| 181 | } |
Simon Hunt | 99ee1e2 | 2015-02-13 09:24:43 -0800 | [diff] [blame] | 182 | |
| 183 | return { |
| 184 | initFilter: initFilter, |
Simon Hunt | eb0fa05 | 2015-02-17 19:20:28 -0800 | [diff] [blame] | 185 | destroyFilter: destroyFilter, |
| 186 | |
| 187 | clickAction: clickAction, |
Simon Hunt | c3c5b67 | 2015-02-20 11:32:13 -0800 | [diff] [blame^] | 188 | selected: selected, |
| 189 | inLayer: inLayer |
Simon Hunt | 99ee1e2 | 2015-02-13 09:24:43 -0800 | [diff] [blame] | 190 | }; |
| 191 | }]); |
| 192 | }()); |