Matteo Scandolo | f0446ed | 2017-08-08 13:05:24 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | c46a82d | 2017-03-24 18:37:18 -0700 | [diff] [blame] | 19 | import * as $ from 'jquery'; |
| 20 | import * as _ from 'lodash'; |
| 21 | |
| 22 | export interface IRCordGraphReducer { |
| 23 | setup(): void; |
| 24 | } |
| 25 | |
| 26 | export class RCordGraphReducer implements IRCordGraphReducer { |
| 27 | |
| 28 | static $inject = [ |
| 29 | 'XosServiceGraphExtender' |
| 30 | ]; |
| 31 | |
| 32 | constructor( |
| 33 | private XosServiceGraphExtender: any |
| 34 | ) { |
| 35 | |
| 36 | } |
| 37 | |
| 38 | public setup() { |
Matteo Scandolo | c46a82d | 2017-03-24 18:37:18 -0700 | [diff] [blame] | 39 | this.XosServiceGraphExtender.register('coarse', 'ecord-local', (graph: any): any => { |
| 40 | graph.nodes = this.positionCoarseNodes(graph.nodes); |
| 41 | return { |
| 42 | nodes: graph.nodes, |
| 43 | links: graph.links |
| 44 | }; |
| 45 | }); |
| 46 | } |
| 47 | |
Max Chu | a7e6ede | 2017-06-29 14:52:01 -0700 | [diff] [blame] | 48 | private getSvgDimensions(graph: string): {width: number, height: number} { |
Matteo Scandolo | c46a82d | 2017-03-24 18:37:18 -0700 | [diff] [blame] | 49 | return { |
| 50 | width: $(`${graph} svg`).width(), |
Max Chu | a7e6ede | 2017-06-29 14:52:01 -0700 | [diff] [blame] | 51 | height: $(`${graph} svg`).height() |
Matteo Scandolo | c46a82d | 2017-03-24 18:37:18 -0700 | [diff] [blame] | 52 | }; |
| 53 | } |
| 54 | |
| 55 | private positionCoarseNodes(nodes: any[]): any[] { |
| 56 | // getting distance between nodes |
| 57 | const hStep = this.getSvgDimensions('xos-coarse-tenancy-graph').width / 4; |
Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 58 | const vStep = this.getSvgDimensions('xos-coarse-tenancy-graph').height / 5; |
Matteo Scandolo | c46a82d | 2017-03-24 18:37:18 -0700 | [diff] [blame] | 59 | |
| 60 | const vtr = _.find(nodes, {label: 'vtr'}); |
| 61 | if (vtr) { |
| 62 | vtr.x = hStep * 2; |
| 63 | vtr.y = vStep * 1; |
| 64 | vtr.fixed = true; |
| 65 | } |
| 66 | |
Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 67 | const rcord = _.find(nodes, {label: 'rcord'}); |
| 68 | if (rcord) { |
| 69 | rcord.x = hStep * 0.5; |
| 70 | rcord.y = vStep * 2; |
| 71 | rcord.fixed = true; |
| 72 | } |
| 73 | |
Matteo Scandolo | c46a82d | 2017-03-24 18:37:18 -0700 | [diff] [blame] | 74 | const volt = _.find(nodes, {label: 'volt'}); |
| 75 | if (volt) { |
| 76 | volt.x = hStep * 1; |
| 77 | volt.y = vStep * 2; |
| 78 | volt.fixed = true; |
| 79 | } |
| 80 | |
| 81 | const vsg = _.find(nodes, {label: 'vsg'}); |
| 82 | if (vsg) { |
| 83 | vsg.x = hStep * 2; |
| 84 | vsg.y = vStep * 2; |
| 85 | vsg.fixed = true; |
| 86 | } |
| 87 | |
| 88 | const vrouter = _.find(nodes, {label: 'vrouter'}); |
| 89 | if (vrouter) { |
| 90 | vrouter.x = hStep * 3; |
| 91 | vrouter.y = vStep * 2; |
| 92 | vrouter.fixed = true; |
| 93 | } |
| 94 | |
Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 95 | const addressmanager = _.find(nodes, {label: 'addressmanager'}); |
| 96 | if (addressmanager) { |
| 97 | addressmanager.x = hStep * 2.5; |
| 98 | addressmanager.y = vStep * 1.5; |
| 99 | addressmanager.fixed = true; |
| 100 | } |
| 101 | |
Matteo Scandolo | c46a82d | 2017-03-24 18:37:18 -0700 | [diff] [blame] | 102 | const oc = _.find(nodes, {label: 'ONOS_CORD'}); |
| 103 | if (oc) { |
| 104 | oc.x = hStep + (hStep / 2); |
| 105 | oc.y = vStep * 3; |
| 106 | oc.fixed = true; |
| 107 | } |
| 108 | |
Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 109 | const vtn = _.find(nodes, {label: 'vtn'}); |
| 110 | if (vtn) { |
| 111 | vtn.x = hStep * 1.5; |
| 112 | vtn.y = vStep * 4; |
| 113 | vtn.fixed = true; |
| 114 | } |
| 115 | |
Matteo Scandolo | c46a82d | 2017-03-24 18:37:18 -0700 | [diff] [blame] | 116 | const of = _.find(nodes, {label: 'ONOS_Fabric'}); |
| 117 | if (of) { |
Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 118 | of.x = hStep * 2.5; |
Matteo Scandolo | c46a82d | 2017-03-24 18:37:18 -0700 | [diff] [blame] | 119 | of.y = vStep * 3; |
| 120 | of.fixed = true; |
| 121 | } |
| 122 | |
Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 123 | const fabric = _.find(nodes, {label: 'fabric'}); |
| 124 | if (fabric) { |
| 125 | fabric.x = hStep * 2.5; |
| 126 | fabric.y = vStep * 4; |
| 127 | fabric.fixed = true; |
| 128 | } |
| 129 | |
| 130 | const exampleservice = _.find(nodes, {label: 'exampleservice'}); |
| 131 | if (exampleservice) { |
| 132 | exampleservice.x = hStep * 3.5; |
| 133 | exampleservice.y = vStep * 4.5; |
| 134 | exampleservice.fixed = true; |
| 135 | } |
| 136 | |
Matteo Scandolo | c46a82d | 2017-03-24 18:37:18 -0700 | [diff] [blame] | 137 | return nodes; |
| 138 | } |
Matteo Scandolo | c46a82d | 2017-03-24 18:37:18 -0700 | [diff] [blame] | 139 | } |