Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [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 | /* |
| 19 | ONOS GUI -- Topology Overlay Module. |
| 20 | |
| 21 | Provides overlay capabilities, allowing ONOS apps to provide additional |
| 22 | custom data/behavior for the topology view. |
| 23 | |
| 24 | */ |
| 25 | |
| 26 | (function () { |
| 27 | 'use strict'; |
| 28 | |
| 29 | // constants |
| 30 | var tos = 'TopoOverlayService: '; |
| 31 | |
| 32 | // injected refs |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame^] | 33 | var $log, fs, gs, wss, ns; |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame] | 34 | |
| 35 | // internal state |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 36 | var overlays = {}, |
| 37 | current = null; |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame] | 38 | |
| 39 | function error(fn, msg) { |
| 40 | $log.error(tos + fn + '(): ' + msg); |
| 41 | } |
| 42 | |
| 43 | function warn(fn, msg) { |
| 44 | $log.warn(tos + fn + '(): ' + msg); |
| 45 | } |
| 46 | |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 47 | function mkGlyphId(oid, gid) { |
| 48 | return (gid[0] === '*') ? oid + '-' + gid.slice(1) : gid; |
| 49 | } |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame] | 50 | |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 51 | function handleGlyphs(o) { |
| 52 | var gdata = fs.isO(o.glyphs), |
| 53 | oid = o.overlayId, |
| 54 | gid = o.glyphId || 'unknown', |
| 55 | data = {}, |
| 56 | note = []; |
| 57 | |
| 58 | o._glyphId = mkGlyphId(oid, gid); |
| 59 | |
| 60 | o.mkGid = function (g) { |
| 61 | return mkGlyphId(oid, g); |
| 62 | }; |
| 63 | o.mkId = function (s) { |
| 64 | return oid + '-' + s; |
| 65 | }; |
| 66 | |
| 67 | // process glyphs if defined |
| 68 | if (gdata) { |
| 69 | angular.forEach(gdata, function (value, key) { |
| 70 | var fullkey = oid + '-' + key; |
| 71 | data['_' + fullkey] = value.vb; |
| 72 | data[fullkey] = value.d; |
| 73 | note.push('*' + key); |
| 74 | }); |
| 75 | gs.registerGlyphs(data); |
| 76 | $log.debug('registered overlay glyphs:', oid, note); |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | function register(overlay) { |
| 81 | var r = 'register', |
| 82 | over = fs.isO(overlay), |
| 83 | id = over ? over.overlayId : ''; |
| 84 | |
| 85 | if (!id) { |
| 86 | return error(r, 'not a recognized overlay'); |
| 87 | } |
| 88 | if (overlays[id]) { |
| 89 | return warn(r, 'already registered: "' + id + '"'); |
| 90 | } |
| 91 | overlays[id] = overlay; |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 92 | handleGlyphs(overlay); |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame] | 93 | $log.debug(tos + 'registered overlay: ' + id, overlay); |
| 94 | } |
| 95 | |
| 96 | // NOTE: unregister needs to be called if an app is ever |
| 97 | // deactivated/uninstalled via the applications view |
| 98 | function unregister(overlay) { |
| 99 | var u = 'unregister', |
| 100 | over = fs.isO(overlay), |
| 101 | id = over ? over.overlayId : ''; |
| 102 | |
| 103 | if (!id) { |
| 104 | return error(u, 'not a recognized overlay'); |
| 105 | } |
| 106 | if (!overlays[id]) { |
| 107 | return warn(u, 'not registered: "' + id + "'") |
| 108 | } |
| 109 | delete overlays[id]; |
| 110 | $log.debug(tos + 'unregistered overlay: ' + id); |
| 111 | // TODO: rebuild the toolbar overlay radio button set |
| 112 | } |
| 113 | |
| 114 | function list() { |
| 115 | return d3.map(overlays).keys(); |
| 116 | } |
| 117 | |
| 118 | function overlay(id) { |
| 119 | return overlays[id]; |
| 120 | } |
| 121 | |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 122 | // an overlay was selected via toolbar radio button press from user |
| 123 | function tbSelection(id) { |
| 124 | var same = current && current.overlayId === id, |
| 125 | payload = {}; |
| 126 | |
| 127 | function doop(op) { |
| 128 | var oid = current.overlayId; |
| 129 | $log.debug('Overlay:', op, oid); |
| 130 | current[op](); |
| 131 | payload[op] = oid; |
| 132 | } |
| 133 | |
| 134 | if (!same) { |
| 135 | current && doop('deactivate'); |
| 136 | current = overlay(id); |
| 137 | current && doop('activate'); |
| 138 | wss.sendEvent('topoSelectOverlay', payload); |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 139 | |
| 140 | // TODO: refactor to emit "flush on overlay change" messages |
| 141 | wss.sendEvent('requestSummary'); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame^] | 145 | var coreButtonPath = { |
| 146 | showDeviceView: 'device', |
| 147 | showFlowView: 'flow', |
| 148 | showPortView: 'port', |
| 149 | showGroupView: 'group' |
| 150 | }; |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 151 | |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame^] | 152 | // install core buttons, and include any additional from the current overlay |
| 153 | function installButtons(buttons, addFn, data, devId) { |
| 154 | |
| 155 | angular.forEach(buttons, function (btn) { |
| 156 | var path = coreButtonPath[btn.id], |
| 157 | _id, |
| 158 | _gid, |
| 159 | _cb, |
| 160 | action; |
| 161 | |
| 162 | if (path) { |
| 163 | // core callback function |
| 164 | _id = btn.id; |
| 165 | _gid = btn.gid; |
| 166 | action = function () { |
| 167 | ns.navTo(path, { devId: devId }); |
| 168 | }; |
| 169 | } else if (current) { |
| 170 | _id = current.mkId(btn.id); |
| 171 | _gid = current.mkGid(btn.gid); |
| 172 | action = current.buttonActions[btn.id] || function () {}; |
| 173 | } |
| 174 | |
| 175 | _cb = function () { action(data); }; |
| 176 | |
| 177 | addFn({ id: _id, gid: _gid, cb: _cb, tt: btn.tt}); |
| 178 | }); |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 179 | |
| 180 | } |
| 181 | |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame] | 182 | angular.module('ovTopo') |
| 183 | .factory('TopoOverlayService', |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame^] | 184 | ['$log', 'FnService', 'GlyphService', 'WebSocketService', 'NavService', |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame] | 185 | |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame^] | 186 | function (_$log_, _fs_, _gs_, _wss_, _ns_) { |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame] | 187 | $log = _$log_; |
| 188 | fs = _fs_; |
| 189 | gs = _gs_; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 190 | wss = _wss_; |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame^] | 191 | ns = _ns_; |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame] | 192 | |
| 193 | return { |
| 194 | register: register, |
| 195 | unregister: unregister, |
| 196 | list: list, |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 197 | overlay: overlay, |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 198 | tbSelection: tbSelection, |
| 199 | installButtons: installButtons |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame] | 200 | } |
| 201 | }]); |
| 202 | |
| 203 | }()); |