blob: f2b81f55834ed9de88ee4457e4eee0c9ca8bdc0e [file] [log] [blame]
Simon Hunt72e44bf2015-07-21 21:34:20 -07001/*
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 Hunt3a0598f2015-08-04 19:59:04 -070033 var $log, fs, gs, wss, ns;
Simon Hunt72e44bf2015-07-21 21:34:20 -070034
35 // internal state
Simon Hunte05cae42015-07-23 17:35:24 -070036 var overlays = {},
37 current = null;
Simon Hunt72e44bf2015-07-21 21:34:20 -070038
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 Huntfb940112015-07-29 18:36:35 -070047 function mkGlyphId(oid, gid) {
48 return (gid[0] === '*') ? oid + '-' + gid.slice(1) : gid;
49 }
Simon Hunt72e44bf2015-07-21 21:34:20 -070050
Simon Huntfb940112015-07-29 18:36:35 -070051 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 Hunt72e44bf2015-07-21 21:34:20 -070077 }
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 Huntfb940112015-07-29 18:36:35 -070092 handleGlyphs(overlay);
Simon Hunt72e44bf2015-07-21 21:34:20 -070093 $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 Hunte05cae42015-07-23 17:35:24 -0700122 // 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 Hunt0af1ec32015-07-24 12:17:55 -0700139
140 // TODO: refactor to emit "flush on overlay change" messages
141 wss.sendEvent('requestSummary');
Simon Hunte05cae42015-07-23 17:35:24 -0700142 }
143 }
144
Simon Hunt3a0598f2015-08-04 19:59:04 -0700145 var coreButtonPath = {
146 showDeviceView: 'device',
147 showFlowView: 'flow',
148 showPortView: 'port',
149 showGroupView: 'group'
150 };
Simon Huntfb940112015-07-29 18:36:35 -0700151
Simon Hunt3a0598f2015-08-04 19:59:04 -0700152 // 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 Huntfb940112015-07-29 18:36:35 -0700179
180 }
181
Simon Hunt72e44bf2015-07-21 21:34:20 -0700182 angular.module('ovTopo')
183 .factory('TopoOverlayService',
Simon Hunt3a0598f2015-08-04 19:59:04 -0700184 ['$log', 'FnService', 'GlyphService', 'WebSocketService', 'NavService',
Simon Hunt72e44bf2015-07-21 21:34:20 -0700185
Simon Hunt3a0598f2015-08-04 19:59:04 -0700186 function (_$log_, _fs_, _gs_, _wss_, _ns_) {
Simon Hunt72e44bf2015-07-21 21:34:20 -0700187 $log = _$log_;
188 fs = _fs_;
189 gs = _gs_;
Simon Hunte05cae42015-07-23 17:35:24 -0700190 wss = _wss_;
Simon Hunt3a0598f2015-08-04 19:59:04 -0700191 ns = _ns_;
Simon Hunt72e44bf2015-07-21 21:34:20 -0700192
193 return {
194 register: register,
195 unregister: unregister,
196 list: list,
Simon Hunte05cae42015-07-23 17:35:24 -0700197 overlay: overlay,
Simon Huntfb940112015-07-29 18:36:35 -0700198 tbSelection: tbSelection,
199 installButtons: installButtons
Simon Hunt72e44bf2015-07-21 21:34:20 -0700200 }
201 }]);
202
203}());