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