blob: e1efe474151b4f8691c9955cee7a6886e57746ad [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
33 var $log, fs, gs;
34
35 // internal state
36 var overlays = {};
37
38 function error(fn, msg) {
39 $log.error(tos + fn + '(): ' + msg);
40 }
41
42 function warn(fn, msg) {
43 $log.warn(tos + fn + '(): ' + msg);
44 }
45
46 function handleGlyph(o) {
47 var gdata = fs.isO(o.glyph),
48 oid,
49 data = {};
50
51 if (!gdata) {
52 o._glyphId = 'unknown';
53 } else {
54 if (gdata.id) {
55 o._glyphId = gdata.id;
56 } else if (gdata.vb && gdata.d) {
57 oid = o.overlayId;
58 data['_' + oid] = gdata.vb;
59 data[oid] = gdata.d;
60 gs.registerGlyphs(data);
61 o._glyphId = oid;
62 $log.debug('registered overlay glyph:', oid);
63 } else {
64 warn('registerGlyph', 'problem with glyph data');
65 }
66 }
67 }
68
69 function register(overlay) {
70 var r = 'register',
71 over = fs.isO(overlay),
72 id = over ? over.overlayId : '';
73
74 if (!id) {
75 return error(r, 'not a recognized overlay');
76 }
77 if (overlays[id]) {
78 return warn(r, 'already registered: "' + id + '"');
79 }
80 overlays[id] = overlay;
81 handleGlyph(overlay);
82 $log.debug(tos + 'registered overlay: ' + id, overlay);
83 }
84
85 // NOTE: unregister needs to be called if an app is ever
86 // deactivated/uninstalled via the applications view
87 function unregister(overlay) {
88 var u = 'unregister',
89 over = fs.isO(overlay),
90 id = over ? over.overlayId : '';
91
92 if (!id) {
93 return error(u, 'not a recognized overlay');
94 }
95 if (!overlays[id]) {
96 return warn(u, 'not registered: "' + id + "'")
97 }
98 delete overlays[id];
99 $log.debug(tos + 'unregistered overlay: ' + id);
100 // TODO: rebuild the toolbar overlay radio button set
101 }
102
103 function list() {
104 return d3.map(overlays).keys();
105 }
106
107 function overlay(id) {
108 return overlays[id];
109 }
110
111 angular.module('ovTopo')
112 .factory('TopoOverlayService',
113 ['$log', 'FnService', 'GlyphService',
114
115 function (_$log_, _fs_, _gs_) {
116 $log = _$log_;
117 fs = _fs_;
118 gs = _gs_;
119
120 return {
121 register: register,
122 unregister: unregister,
123 list: list,
124 overlay: overlay
125 }
126 }]);
127
128}());