blob: fd704a63aa6e37437263747df0d9613ee623e62e [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 Hunte05cae42015-07-23 17:35:24 -070033 var $log, fs, gs, wss;
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
47 function handleGlyph(o) {
48 var gdata = fs.isO(o.glyph),
49 oid,
50 data = {};
51
52 if (!gdata) {
53 o._glyphId = 'unknown';
54 } else {
55 if (gdata.id) {
56 o._glyphId = gdata.id;
57 } else if (gdata.vb && gdata.d) {
58 oid = o.overlayId;
59 data['_' + oid] = gdata.vb;
60 data[oid] = gdata.d;
61 gs.registerGlyphs(data);
62 o._glyphId = oid;
63 $log.debug('registered overlay glyph:', oid);
64 } else {
65 warn('registerGlyph', 'problem with glyph data');
66 }
67 }
68 }
69
70 function register(overlay) {
71 var r = 'register',
72 over = fs.isO(overlay),
73 id = over ? over.overlayId : '';
74
75 if (!id) {
76 return error(r, 'not a recognized overlay');
77 }
78 if (overlays[id]) {
79 return warn(r, 'already registered: "' + id + '"');
80 }
81 overlays[id] = overlay;
82 handleGlyph(overlay);
83 $log.debug(tos + 'registered overlay: ' + id, overlay);
84 }
85
86 // NOTE: unregister needs to be called if an app is ever
87 // deactivated/uninstalled via the applications view
88 function unregister(overlay) {
89 var u = 'unregister',
90 over = fs.isO(overlay),
91 id = over ? over.overlayId : '';
92
93 if (!id) {
94 return error(u, 'not a recognized overlay');
95 }
96 if (!overlays[id]) {
97 return warn(u, 'not registered: "' + id + "'")
98 }
99 delete overlays[id];
100 $log.debug(tos + 'unregistered overlay: ' + id);
101 // TODO: rebuild the toolbar overlay radio button set
102 }
103
104 function list() {
105 return d3.map(overlays).keys();
106 }
107
108 function overlay(id) {
109 return overlays[id];
110 }
111
Simon Hunte05cae42015-07-23 17:35:24 -0700112 // an overlay was selected via toolbar radio button press from user
113 function tbSelection(id) {
114 var same = current && current.overlayId === id,
115 payload = {};
116
117 function doop(op) {
118 var oid = current.overlayId;
119 $log.debug('Overlay:', op, oid);
120 current[op]();
121 payload[op] = oid;
122 }
123
124 if (!same) {
125 current && doop('deactivate');
126 current = overlay(id);
127 current && doop('activate');
128 wss.sendEvent('topoSelectOverlay', payload);
129 }
130 }
131
Simon Hunt72e44bf2015-07-21 21:34:20 -0700132 angular.module('ovTopo')
133 .factory('TopoOverlayService',
Simon Hunte05cae42015-07-23 17:35:24 -0700134 ['$log', 'FnService', 'GlyphService', 'WebSocketService',
Simon Hunt72e44bf2015-07-21 21:34:20 -0700135
Simon Hunte05cae42015-07-23 17:35:24 -0700136 function (_$log_, _fs_, _gs_, _wss_) {
Simon Hunt72e44bf2015-07-21 21:34:20 -0700137 $log = _$log_;
138 fs = _fs_;
139 gs = _gs_;
Simon Hunte05cae42015-07-23 17:35:24 -0700140 wss = _wss_;
Simon Hunt72e44bf2015-07-21 21:34:20 -0700141
142 return {
143 register: register,
144 unregister: unregister,
145 list: list,
Simon Hunte05cae42015-07-23 17:35:24 -0700146 overlay: overlay,
147 tbSelection: tbSelection
Simon Hunt72e44bf2015-07-21 21:34:20 -0700148 }
149 }]);
150
151}());