Matteo Scandolo | 8cf33a3 | 2017-11-14 15:52:29 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | |
| 18 | import * as _ from 'lodash'; |
| 19 | import {IXosResourceService} from '../../datasources/rest/model.rest'; |
| 20 | import {IXosSgNode} from '../interfaces'; |
Matteo Scandolo | 35fdf24 | 2017-11-30 12:29:45 -0800 | [diff] [blame] | 21 | import {IXosConfirm} from '../../core/confirm/confirm.service'; |
Matteo Scandolo | 8cf33a3 | 2017-11-14 15:52:29 -0800 | [diff] [blame] | 22 | |
| 23 | export interface IXosNodePositioner { |
| 24 | positionNodes(svg: {width: number, height: number}, nodes: any[]): ng.IPromise<IXosSgNode[]>; |
| 25 | } |
| 26 | |
| 27 | export class XosNodePositioner implements IXosNodePositioner { |
| 28 | static $inject = [ |
| 29 | '$log', |
| 30 | '$q', |
Matteo Scandolo | 35fdf24 | 2017-11-30 12:29:45 -0800 | [diff] [blame] | 31 | 'ModelRest', |
| 32 | 'XosConfirm' |
Matteo Scandolo | 8cf33a3 | 2017-11-14 15:52:29 -0800 | [diff] [blame] | 33 | ]; |
| 34 | |
| 35 | constructor ( |
| 36 | private $log: ng.ILogService, |
| 37 | private $q: ng.IQService, |
| 38 | private ModelRest: IXosResourceService, |
Matteo Scandolo | 35fdf24 | 2017-11-30 12:29:45 -0800 | [diff] [blame] | 39 | private XosConfirm: IXosConfirm |
Matteo Scandolo | 8cf33a3 | 2017-11-14 15:52:29 -0800 | [diff] [blame] | 40 | ) { |
| 41 | this.$log.info('[XosNodePositioner] Setup'); |
| 42 | } |
| 43 | |
Matteo Scandolo | 35fdf24 | 2017-11-30 12:29:45 -0800 | [diff] [blame] | 44 | public positionNodes(svg: {width: number, height: number}, nodes: IXosSgNode[]): ng.IPromise<IXosSgNode[]> { |
Matteo Scandolo | 8cf33a3 | 2017-11-14 15:52:29 -0800 | [diff] [blame] | 45 | |
Matteo Scandolo | 8cf33a3 | 2017-11-14 15:52:29 -0800 | [diff] [blame] | 46 | const d = this.$q.defer(); |
| 47 | |
| 48 | this.getConstraints() |
| 49 | .then(constraints => { |
| 50 | const hStep = this.getHorizontalStep(svg.width, constraints); |
Matteo Scandolo | 35fdf24 | 2017-11-30 12:29:45 -0800 | [diff] [blame] | 51 | const positionConstraints = _.reduce(constraints, (all: any, horizontalConstraint: string | string[], i: number) => { |
Matteo Scandolo | 8cf33a3 | 2017-11-14 15:52:29 -0800 | [diff] [blame] | 52 | // NOTE it's a single element, leave it in the middle |
Matteo Scandolo | 35fdf24 | 2017-11-30 12:29:45 -0800 | [diff] [blame] | 53 | if (angular.isString(horizontalConstraint)) { |
| 54 | all[horizontalConstraint] = { |
| 55 | x: (i + 1) * hStep, |
| 56 | y: svg.height / 2, |
| 57 | fixed: true |
| 58 | }; |
Matteo Scandolo | 8cf33a3 | 2017-11-14 15:52:29 -0800 | [diff] [blame] | 59 | } |
| 60 | else { |
Matteo Scandolo | 35fdf24 | 2017-11-30 12:29:45 -0800 | [diff] [blame] | 61 | const verticalConstraints = horizontalConstraint; |
Matteo Scandolo | 8cf33a3 | 2017-11-14 15:52:29 -0800 | [diff] [blame] | 62 | const vStep = this.getVerticalStep(svg.height, verticalConstraints); |
Matteo Scandolo | 35fdf24 | 2017-11-30 12:29:45 -0800 | [diff] [blame] | 63 | _.forEach(verticalConstraints, (verticalConstraint: string, v: number) => { |
| 64 | if (angular.isString(verticalConstraint)) { |
| 65 | all[verticalConstraint] = { |
| 66 | x: (i + 1) * hStep, |
| 67 | y: (v + 1) * vStep, |
| 68 | fixed: true |
| 69 | }; |
Matteo Scandolo | 8cf33a3 | 2017-11-14 15:52:29 -0800 | [diff] [blame] | 70 | } |
| 71 | }); |
| 72 | } |
| 73 | return all; |
| 74 | }, {}); |
| 75 | |
Matteo Scandolo | 35fdf24 | 2017-11-30 12:29:45 -0800 | [diff] [blame] | 76 | // find the nodes that don't have a position defined and put them at the top |
| 77 | const allNodes = _.reduce(nodes, (all: string[], n: IXosSgNode) => { |
| 78 | if (n.type === 'service') { |
| 79 | all.push(n.data.name); |
| 80 | } |
| 81 | return all; |
| 82 | }, []); |
| 83 | const positionedNodes = Object.keys(positionConstraints); |
| 84 | const unpositionedNodes = _.difference(allNodes, positionedNodes); |
| 85 | |
| 86 | _.forEach(unpositionedNodes, (node: string, i: number) => { |
| 87 | const hStep = this.getHorizontalStep(svg.width, unpositionedNodes); |
| 88 | positionConstraints[node] = { |
| 89 | x: (i + 1) * hStep, |
| 90 | y: svg.height - 50, |
| 91 | fixed: true |
| 92 | }; |
| 93 | }); |
| 94 | |
Matteo Scandolo | 8cf33a3 | 2017-11-14 15:52:29 -0800 | [diff] [blame] | 95 | d.resolve(_.map(nodes, n => { |
| 96 | return angular.merge(n, positionConstraints[n.data.name]); |
| 97 | })); |
| 98 | }) |
| 99 | .catch(e => { |
| 100 | this.$log.error(`[XosNodePositioner] Error retrieving constraints`, e); |
| 101 | }); |
| 102 | |
| 103 | return d.promise; |
| 104 | } |
| 105 | |
| 106 | private getConstraints(): ng.IPromise<any[]> { |
| 107 | const d = this.$q.defer(); |
| 108 | this.ModelRest.getResource('/core/servicegraphconstraints').query().$promise |
| 109 | .then(res => { |
| 110 | d.resolve(JSON.parse(res[0].constraints)); |
| 111 | }) |
| 112 | .catch(e => { |
Matteo Scandolo | 35fdf24 | 2017-11-30 12:29:45 -0800 | [diff] [blame] | 113 | this.XosConfirm.open({ |
| 114 | header: 'Error in graph constraints config', |
| 115 | text: ` |
| 116 | There was an error in the settings you provided as graph constraints. |
| 117 | Please check the declaration of the <code>"Graph Constraints"</code> model. <br/> |
| 118 | The error was: <br/><br/> |
| 119 | <code>${e}</code> |
| 120 | <br/><br/> |
| 121 | Please fix it to see the graph.`, |
| 122 | actions: [] |
| 123 | }); |
Matteo Scandolo | 8cf33a3 | 2017-11-14 15:52:29 -0800 | [diff] [blame] | 124 | d.reject(e); |
| 125 | }); |
| 126 | return d.promise; |
| 127 | } |
| 128 | |
| 129 | private getHorizontalStep(svgWidth: number, constraints: any[]) { |
| 130 | return svgWidth / (constraints.length + 1); |
| 131 | } |
| 132 | |
| 133 | private getVerticalStep(svgHeight: number, verticalConstraints: string[]) { |
| 134 | // NOTE verticalConstraints represent the vertical part (the nested array) |
| 135 | return svgHeight / (verticalConstraints.length + 1); |
| 136 | } |
| 137 | } |