Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1 | /* |
| 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 Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 18 | ONOS network topology viewer - version 1.1 |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 19 | |
| 20 | @author Simon Hunt |
| 21 | */ |
| 22 | |
| 23 | (function (onos) { |
| 24 | 'use strict'; |
| 25 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 26 | // configuration data |
| 27 | var config = { |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 28 | useLiveData: false, |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 29 | debugOn: false, |
| 30 | debug: { |
| 31 | showNodeXY: false, |
| 32 | showKeyHandler: true |
| 33 | }, |
| 34 | options: { |
| 35 | layering: true, |
| 36 | collisionPrevention: true, |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 37 | showBackground: true |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 38 | }, |
| 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 Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 58 | force: { |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 59 | marginLR: 20, |
| 60 | marginTB: 20, |
| 61 | translate: function() { |
| 62 | return 'translate(' + |
| 63 | config.force.marginLR + ',' + |
| 64 | config.force.marginTB + ')'; |
| 65 | } |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 66 | } |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 67 | }; |
| 68 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 69 | // 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 Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 76 | // state variables |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 77 | var svg, |
| 78 | bgImg, |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 79 | network = {}, |
| 80 | selected = {}, |
| 81 | highlighted = null, |
| 82 | hovered = null, |
| 83 | viewMode = 'showAll', |
| 84 | portLabelsOn = false; |
| 85 | |
| 86 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 87 | // ============================== |
| 88 | // Private functions |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 89 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 90 | // 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 Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 96 | }); |
| 97 | } |
| 98 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 99 | function doRadio(view, id) { |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 100 | showAllLayers(); |
| 101 | if (id === 'showPkt') { |
| 102 | showPacketLayer(); |
| 103 | } else if (id === 'showOpt') { |
| 104 | showOpticalLayer(); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | function showAllLayers() { |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 109 | // 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 Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | function showPacketLayer() { |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 117 | alert('show packet layer'); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | function showOpticalLayer() { |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 121 | alert('show optical layer'); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 124 | // ============================== |
| 125 | // View life-cycle callbacks |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 126 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 127 | 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 Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 132 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 133 | // 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 Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 137 | .attr('transform', config.force.translate()); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 138 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 139 | // load the background image |
| 140 | bgImg = svg.append('svg:image') |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 141 | .attr({ |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 142 | id: idBg, |
| 143 | width: w, |
| 144 | height: h, |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 145 | 'xlink:href': config.backgroundUrl |
| 146 | }) |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 147 | .style({ |
| 148 | visibility: showBg |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 149 | }); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 153 | function load(view, ctx) { |
| 154 | view.setRadio(btnSet, doRadio); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 155 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 158 | function resize(view, ctx) { |
| 159 | setSize(view); |
| 160 | setSize(view, bgImg); |
| 161 | } |
| 162 | |
| 163 | |
| 164 | // ============================== |
| 165 | // View registration |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 166 | |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 167 | onos.ui.addView('topo', { |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 168 | preload: preload, |
| 169 | load: load, |
| 170 | resize: resize |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 171 | }); |
| 172 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 173 | }(ONOS)); |