blob: 499afd0cf894bff234a6ccd683b15fe53fb4b4ec [file] [log] [blame]
Matteo Scandolo8cf33a32017-11-14 15:52:29 -08001/*
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
18import * as _ from 'lodash';
19import {IXosResourceService} from '../../datasources/rest/model.rest';
20import {IXosSgNode} from '../interfaces';
21
22export interface IXosNodePositioner {
23 positionNodes(svg: {width: number, height: number}, nodes: any[]): ng.IPromise<IXosSgNode[]>;
24}
25
26export class XosNodePositioner implements IXosNodePositioner {
27 static $inject = [
28 '$log',
29 '$q',
30 'ModelRest'
31 ];
32
33 constructor (
34 private $log: ng.ILogService,
35 private $q: ng.IQService,
36 private ModelRest: IXosResourceService,
37 ) {
38 this.$log.info('[XosNodePositioner] Setup');
39 }
40
41 public positionNodes(svg: {width: number, height: number}, nodes: any[]): ng.IPromise<IXosSgNode[]> {
42
43 // TODO refactor naming in this loop to make it clearer
44 const d = this.$q.defer();
45
46 this.getConstraints()
47 .then(constraints => {
48 const hStep = this.getHorizontalStep(svg.width, constraints);
49 const positionConstraints = _.reduce(constraints, (all: any, c: string | string[], i: number) => {
50 let pos: {x: number, y: number, fixed: boolean} = {
51 x: svg.width / 2,
52 y: svg.height / 2,
53 fixed: true
54 };
55 // NOTE it's a single element, leave it in the middle
56 if (angular.isString(c)) {
57 pos.x = (i + 1) * hStep;
58 all[c] = pos;
59 }
60 else {
61 const verticalConstraints = c;
62 const vStep = this.getVerticalStep(svg.height, verticalConstraints);
63 _.forEach(verticalConstraints, (c: string, v: number) => {
64 if (angular.isString(c)) {
65 let p = angular.copy(pos);
66 p.x = (i + 1) * hStep;
67 p.y = (v + 1) * vStep;
68 all[c] = p;
69 }
70 });
71 }
72 return all;
73 }, {});
74
75 d.resolve(_.map(nodes, n => {
76 return angular.merge(n, positionConstraints[n.data.name]);
77 }));
78 })
79 .catch(e => {
80 this.$log.error(`[XosNodePositioner] Error retrieving constraints`, e);
81 });
82
83 return d.promise;
84 }
85
86 private getConstraints(): ng.IPromise<any[]> {
87 const d = this.$q.defer();
88 this.ModelRest.getResource('/core/servicegraphconstraints').query().$promise
89 .then(res => {
90 d.resolve(JSON.parse(res[0].constraints));
91 })
92 .catch(e => {
93 d.reject(e);
94 });
95 return d.promise;
96 }
97
98 private getHorizontalStep(svgWidth: number, constraints: any[]) {
99 return svgWidth / (constraints.length + 1);
100 }
101
102 private getVerticalStep(svgHeight: number, verticalConstraints: string[]) {
103 // NOTE verticalConstraints represent the vertical part (the nested array)
104 return svgHeight / (verticalConstraints.length + 1);
105 }
106}