blob: fcc4bb49c32e3d005288cdeb3acbd555fafd5ab1 [file] [log] [blame]
Simon Hunt195cb382014-11-03 17:50:51 -08001/*
2 * Copyright 2014 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/*
Simon Hunt142d0032014-11-04 20:13:09 -080018 ONOS network topology viewer - version 1.1
Simon Hunt195cb382014-11-03 17:50:51 -080019
20 @author Simon Hunt
21 */
22
23(function (onos) {
24 'use strict';
25
Simon Hunt195cb382014-11-03 17:50:51 -080026 // configuration data
27 var config = {
Simon Hunt142d0032014-11-04 20:13:09 -080028 useLiveData: false,
Simon Hunt195cb382014-11-03 17:50:51 -080029 debugOn: false,
30 debug: {
31 showNodeXY: false,
32 showKeyHandler: true
33 },
34 options: {
35 layering: true,
36 collisionPrevention: true,
Simon Hunt142d0032014-11-04 20:13:09 -080037 showBackground: true
Simon Hunt195cb382014-11-03 17:50:51 -080038 },
39 backgroundUrl: 'img/us-map.png',
40 data: {
41 live: {
42 jsonUrl: 'rs/topology/graph',
43 detailPrefix: 'rs/topology/graph/',
44 detailSuffix: ''
45 },
46 fake: {
47 jsonUrl: 'json/network2.json',
48 detailPrefix: 'json/',
49 detailSuffix: '.json'
50 }
51 },
52 iconUrl: {
53 device: 'img/device.png',
54 host: 'img/host.png',
55 pkt: 'img/pkt.png',
56 opt: 'img/opt.png'
57 },
Simon Hunt195cb382014-11-03 17:50:51 -080058 force: {
Simon Hunt195cb382014-11-03 17:50:51 -080059 marginLR: 20,
60 marginTB: 20,
61 translate: function() {
62 return 'translate(' +
63 config.force.marginLR + ',' +
64 config.force.marginTB + ')';
65 }
Simon Hunt142d0032014-11-04 20:13:09 -080066 }
Simon Hunt195cb382014-11-03 17:50:51 -080067 };
68
Simon Hunt142d0032014-11-04 20:13:09 -080069 // radio buttons
70 var btnSet = [
71 { id: 'showAll', text: 'All Layers' },
72 { id: 'showPkt', text: 'Packet Only' },
73 { id: 'showOpt', text: 'Optical Only' }
74 ];
75
Simon Hunt195cb382014-11-03 17:50:51 -080076 // state variables
Simon Hunt142d0032014-11-04 20:13:09 -080077 var svg,
78 bgImg,
Simon Hunt195cb382014-11-03 17:50:51 -080079 network = {},
80 selected = {},
81 highlighted = null,
82 hovered = null,
83 viewMode = 'showAll',
84 portLabelsOn = false;
85
86
Simon Hunt142d0032014-11-04 20:13:09 -080087 // ==============================
88 // Private functions
Simon Hunt195cb382014-11-03 17:50:51 -080089
Simon Hunt142d0032014-11-04 20:13:09 -080090 // set the size of the SVG layer (or other element) to that of the view
91 function setSize(view, el) {
92 var thing = el || svg;
93 thing.attr({
94 width: view.width(),
95 height: view.height()
Simon Hunt195cb382014-11-03 17:50:51 -080096 });
97 }
98
Simon Hunt142d0032014-11-04 20:13:09 -080099 function doRadio(view, id) {
Simon Hunt195cb382014-11-03 17:50:51 -0800100 showAllLayers();
101 if (id === 'showPkt') {
102 showPacketLayer();
103 } else if (id === 'showOpt') {
104 showOpticalLayer();
105 }
106 }
107
108 function showAllLayers() {
Simon Hunt142d0032014-11-04 20:13:09 -0800109// network.node.classed('inactive', false);
110// network.link.classed('inactive', false);
111// d3.selectAll('svg .port').classed('inactive', false);
112// d3.selectAll('svg .portText').classed('inactive', false);
113 alert('show all layers');
Simon Hunt195cb382014-11-03 17:50:51 -0800114 }
115
116 function showPacketLayer() {
Simon Hunt142d0032014-11-04 20:13:09 -0800117 alert('show packet layer');
Simon Hunt195cb382014-11-03 17:50:51 -0800118 }
119
120 function showOpticalLayer() {
Simon Hunt142d0032014-11-04 20:13:09 -0800121 alert('show optical layer');
Simon Hunt195cb382014-11-03 17:50:51 -0800122 }
123
Simon Hunt142d0032014-11-04 20:13:09 -0800124 // ==============================
125 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -0800126
Simon Hunt142d0032014-11-04 20:13:09 -0800127 function preload(view, ctx) {
128 var w = view.width(),
129 h = view.height(),
130 idBg = view.uid('bg'),
131 showBg = config.options.showBackground ? 'visible' : 'hidden';
Simon Hunt195cb382014-11-03 17:50:51 -0800132
Simon Hunt142d0032014-11-04 20:13:09 -0800133 // NOTE: view.$div is a D3 selection of the view's div
134 svg = view.$div.append('svg');
135 setSize(view);
136 svg.append('g')
Simon Hunt195cb382014-11-03 17:50:51 -0800137 .attr('transform', config.force.translate());
Simon Hunt195cb382014-11-03 17:50:51 -0800138
Simon Hunt142d0032014-11-04 20:13:09 -0800139 // load the background image
140 bgImg = svg.append('svg:image')
Simon Hunt195cb382014-11-03 17:50:51 -0800141 .attr({
Simon Hunt142d0032014-11-04 20:13:09 -0800142 id: idBg,
143 width: w,
144 height: h,
Simon Hunt195cb382014-11-03 17:50:51 -0800145 'xlink:href': config.backgroundUrl
146 })
Simon Hunt142d0032014-11-04 20:13:09 -0800147 .style({
148 visibility: showBg
Simon Hunt195cb382014-11-03 17:50:51 -0800149 });
Simon Hunt195cb382014-11-03 17:50:51 -0800150 }
151
152
Simon Hunt142d0032014-11-04 20:13:09 -0800153 function load(view, ctx) {
154 view.setRadio(btnSet, doRadio);
Simon Hunt195cb382014-11-03 17:50:51 -0800155
Simon Hunt195cb382014-11-03 17:50:51 -0800156 }
157
Simon Hunt142d0032014-11-04 20:13:09 -0800158 function resize(view, ctx) {
159 setSize(view);
160 setSize(view, bgImg);
161 }
162
163
164 // ==============================
165 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -0800166
Simon Hunt25248912014-11-04 11:25:48 -0800167 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -0800168 preload: preload,
169 load: load,
170 resize: resize
Simon Hunt195cb382014-11-03 17:50:51 -0800171 });
172
Simon Hunt195cb382014-11-03 17:50:51 -0800173}(ONOS));