Matteo Scandolo | c46a82d | 2017-03-24 18:37:18 -0700 | [diff] [blame] | 1 | import * as $ from 'jquery'; |
| 2 | import * as _ from 'lodash'; |
| 3 | |
| 4 | export interface IRCordGraphReducer { |
| 5 | setup(): void; |
| 6 | } |
| 7 | |
| 8 | export class RCordGraphReducer implements IRCordGraphReducer { |
| 9 | |
| 10 | static $inject = [ |
| 11 | 'XosServiceGraphExtender' |
| 12 | ]; |
| 13 | |
| 14 | constructor( |
| 15 | private XosServiceGraphExtender: any |
| 16 | ) { |
| 17 | |
| 18 | } |
| 19 | |
| 20 | public setup() { |
| 21 | this.XosServiceGraphExtender.register('finegrained', 'ecord-local', (graph): any => { |
| 22 | graph = this.positionFineGrainedNodes(graph); |
| 23 | return graph; |
| 24 | }); |
| 25 | |
| 26 | this.XosServiceGraphExtender.register('coarse', 'ecord-local', (graph: any): any => { |
| 27 | graph.nodes = this.positionCoarseNodes(graph.nodes); |
| 28 | return { |
| 29 | nodes: graph.nodes, |
| 30 | links: graph.links |
| 31 | }; |
| 32 | }); |
| 33 | } |
| 34 | |
Max Chu | a7e6ede | 2017-06-29 14:52:01 -0700 | [diff] [blame] | 35 | private getSvgDimensions(graph: string): {width: number, height: number} { |
Matteo Scandolo | c46a82d | 2017-03-24 18:37:18 -0700 | [diff] [blame] | 36 | return { |
| 37 | width: $(`${graph} svg`).width(), |
Max Chu | a7e6ede | 2017-06-29 14:52:01 -0700 | [diff] [blame] | 38 | height: $(`${graph} svg`).height() |
Matteo Scandolo | c46a82d | 2017-03-24 18:37:18 -0700 | [diff] [blame] | 39 | }; |
| 40 | } |
| 41 | |
| 42 | private positionCoarseNodes(nodes: any[]): any[] { |
| 43 | // getting distance between nodes |
| 44 | const hStep = this.getSvgDimensions('xos-coarse-tenancy-graph').width / 4; |
Max Chu | a7e6ede | 2017-06-29 14:52:01 -0700 | [diff] [blame] | 45 | const vStep = this.getSvgDimensions('xos-coarse-tenancy-graph').height / 4; |
Matteo Scandolo | c46a82d | 2017-03-24 18:37:18 -0700 | [diff] [blame] | 46 | |
| 47 | const vtr = _.find(nodes, {label: 'vtr'}); |
| 48 | if (vtr) { |
| 49 | vtr.x = hStep * 2; |
| 50 | vtr.y = vStep * 1; |
| 51 | vtr.fixed = true; |
| 52 | } |
| 53 | |
| 54 | const volt = _.find(nodes, {label: 'volt'}); |
| 55 | if (volt) { |
| 56 | volt.x = hStep * 1; |
| 57 | volt.y = vStep * 2; |
| 58 | volt.fixed = true; |
| 59 | } |
| 60 | |
| 61 | const vsg = _.find(nodes, {label: 'vsg'}); |
| 62 | if (vsg) { |
| 63 | vsg.x = hStep * 2; |
| 64 | vsg.y = vStep * 2; |
| 65 | vsg.fixed = true; |
| 66 | } |
| 67 | |
| 68 | const vrouter = _.find(nodes, {label: 'vrouter'}); |
| 69 | if (vrouter) { |
| 70 | vrouter.x = hStep * 3; |
| 71 | vrouter.y = vStep * 2; |
| 72 | vrouter.fixed = true; |
| 73 | } |
| 74 | |
| 75 | const oc = _.find(nodes, {label: 'ONOS_CORD'}); |
| 76 | if (oc) { |
| 77 | oc.x = hStep + (hStep / 2); |
| 78 | oc.y = vStep * 3; |
| 79 | oc.fixed = true; |
| 80 | } |
| 81 | |
| 82 | const of = _.find(nodes, {label: 'ONOS_Fabric'}); |
| 83 | if (of) { |
| 84 | of.x = (hStep * 2) + (hStep / 2); |
| 85 | of.y = vStep * 3; |
| 86 | of.fixed = true; |
| 87 | } |
| 88 | |
| 89 | return nodes; |
| 90 | } |
| 91 | |
| 92 | private positionFineGrainedNodes(graph: any): any[] { |
| 93 | |
| 94 | let subscriberPosition = 0; |
| 95 | let networkPosition = 0; |
| 96 | |
| 97 | const positionSubscriberNode = (node: any, hStep: number, vStep: number): any => { |
| 98 | subscriberPosition = subscriberPosition + 1; |
| 99 | node.x = hStep; |
| 100 | node.y = vStep * (3 + subscriberPosition ); |
| 101 | node.fixed = true; |
| 102 | return node; |
| 103 | }; |
| 104 | |
| 105 | const positionServiceNode = (node: any, hStep: number, vStep: number, vLength: number): any => { |
| 106 | if (node.label === 'ONOS_Fabric') { |
| 107 | node.x = hStep * 4; |
| 108 | node.y = vStep; |
| 109 | } |
| 110 | if (node.label === 'volt' || node.label === 'vsg' || node.label === 'vrouter') { |
| 111 | node.y = vStep * 3; |
| 112 | } |
| 113 | if (node.label === 'volt') { |
| 114 | node.x = hStep * 2; |
| 115 | } |
| 116 | if (node.label === 'vsg') { |
| 117 | node.x = hStep * 3; |
| 118 | } |
| 119 | if (node.label === 'vrouter') { |
| 120 | node.x = hStep * 4; |
| 121 | } |
| 122 | if (node.label === 'ONOS_CORD' || node.label === 'vtr') { |
| 123 | node.y = vStep * (vLength -1); |
| 124 | } |
| 125 | if (node.label === 'ONOS_CORD') { |
| 126 | node.x = hStep * 2; |
| 127 | } |
| 128 | if (node.label === 'vtr') { |
| 129 | node.x = hStep * 3; |
| 130 | } |
| 131 | |
| 132 | node.fixed = true; |
| 133 | return node; |
| 134 | }; |
| 135 | |
| 136 | const positionNetworkNode = (node: any, hStep: number, vStep: number): any => { |
| 137 | networkPosition = networkPosition + 1; |
| 138 | node.x = hStep * 5; |
| 139 | node.y = vStep * (3 + networkPosition ); |
| 140 | node.fixed = true; |
| 141 | return node; |
| 142 | }; |
| 143 | |
| 144 | const findSubscriberElementY = (nodes: any[], node: any): any => { |
| 145 | if (node.model.subscriber_root_id) { |
| 146 | console.log(node.model.subscriber_root_id); |
| 147 | const subscriber = _.find(nodes, n => { |
| 148 | return n.id === `tenantroot~${node.model.subscriber_root_id}` |
| 149 | }); |
| 150 | debugger; |
| 151 | // console.log(subscriber.y); |
| 152 | return subscriber.y; |
| 153 | } |
| 154 | }; |
| 155 | |
| 156 | const positionTenantNode = (nodes: any[], node: any, hStep: number, vStep: number): any => { |
| 157 | if (node.model.kind === 'vOLT') { |
| 158 | node.x = hStep * 2; |
| 159 | } |
| 160 | if (node.model.kind === 'vCPE') { |
| 161 | node.x = hStep * 3; |
| 162 | } |
| 163 | if (node.model.kind === 'vROUTER') { |
| 164 | node.x = hStep * 4; |
| 165 | } |
| 166 | |
| 167 | return node; |
| 168 | }; |
| 169 | |
| 170 | let subscribers = _.filter(graph.nodes, n => n.type === 'subscriber' || n.type === 'tenantroot'); |
| 171 | |
| 172 | const vLength = 5 + subscribers.length; |
| 173 | |
| 174 | const hStep = this.getSvgDimensions('xos-fine-grained-tenancy-graph').width / 6; |
Max Chu | a7e6ede | 2017-06-29 14:52:01 -0700 | [diff] [blame] | 175 | const vStep = this.getSvgDimensions('xos-fine-grained-tenancy-graph').height / vLength; |
Matteo Scandolo | c46a82d | 2017-03-24 18:37:18 -0700 | [diff] [blame] | 176 | |
| 177 | graph.nodes = _.map(graph.nodes, n => { |
| 178 | if (n.type === 'subscriber' || n.type === 'tenantroot') { |
| 179 | n = positionSubscriberNode(n, hStep, vStep); |
| 180 | } |
| 181 | if (n.type === 'service') { |
| 182 | n = positionServiceNode(n, hStep, vStep, vLength); |
| 183 | } |
| 184 | if (n.type === 'network') { |
| 185 | n = positionNetworkNode(n, hStep, vStep); |
| 186 | } |
| 187 | if (n.type === 'tenant') { |
| 188 | n = positionTenantNode(graph.nodes, n, hStep, vStep); |
| 189 | } |
| 190 | // n.fixed = true; |
| 191 | return n; |
| 192 | }); |
| 193 | return graph; |
| 194 | } |
| 195 | } |