Rift.IO OSM R1 Initial Submission
[osm/UI.git] / skyquake / plugins / composer / src / src / libraries / graph / GraphRecordServicePath.js
1 /**
2 * Created by onvelocity on 2/10/16.
3 */
4
5 import math from './math'
6 import PathBuilder from './PathBuilder'
7 import DescriptorModelFactory from '../model/DescriptorModelFactory'
8
9 import '../../styles/GraphRecordServicePaths.scss'
10
11 export default class GraphRecordServicePath {
12
13 constructor(graph, props) {
14 this.graph = graph;
15 this.props = props;
16 this.containers = [];
17 this.networkMap = new WeakMap();
18 this.networkMap.set(this, {networkMap: {}});
19
20 }
21
22 getNetwork(key) {
23 return this.networkMap.get(this).networkMap[key];
24 }
25
26 putNetwork(item) {
27 this.networkMap.get(this).networkMap[item.key + item.type] = item;
28 }
29
30 addContainers(containers) {
31
32 containers.filter(d => {
33 if (DescriptorModelFactory.isRecordServicePath(d)) {
34 return true;
35 }
36 if (DescriptorModelFactory.isRspConnectionPointRef(d)) {
37 return false;
38 }
39 if (DescriptorModelFactory.isConnectionPoint(d)) {
40 this.putNetwork(d);
41 }
42 if (DescriptorModelFactory.isVnfdConnectionPointRef(d)) {
43 this.putNetwork(d)
44 }
45 }).forEach(d => this.containers.push(d));
46
47 }
48
49 render() {
50
51 const pathsGroup = this.graph.forwardingGraphPaths;
52
53 const path = pathsGroup.selectAll('.rsp-path').data(this.containers, DescriptorModelFactory.containerIdentity);
54
55 path.enter().append('path');
56
57 path.attr({
58 'class': 'rsp-path path',
59 'stroke': rsp => rsp.parent.colors.primary,
60 'data-id': d => d.id,
61 'stroke-width': 1.5,
62 fill: 'transparent',
63 'shape-rendering': 'geometricPrecision',
64 d: (rsp, rspIndex) => {
65
66 if (!rsp.uiState.showPath) {
67 return '';
68 }
69
70 const cpList = rsp.connectionPoints.filter(d => d);
71 const cpCount = rsp.connectionPoints.length - 1;
72
73 const path = new PathBuilder();
74
75 const pathOffset = 20 * rspIndex;
76
77 return rsp.connectionPoints.reduce((r, cpRef, cpRefIndex) => {
78
79 const cp = this.getNetwork(cpRef.key + 'connection-point');
80 const point = cp.position.centerPoint();
81 const vnfdCpRef = this.getNetwork(cp.key + 'vnfd-connection-point-ref');
82
83 if (path.length === 0) {
84 path.M(point.x, point.y, {container: cp});
85 } else if (cpRefIndex === 1) {
86
87 const last = path.last();
88 //
89 //const previousContainer = path.findPrevious(d => d.info.container);
90 //
91 //let distance = 200;
92
93 //if (previousContainer) {
94 // distance = math.distanceBetweenPositions(previousContainer.info.container.position, cp.position);
95 //}
96
97 if (last.cmd === 'M') {
98 path.C(last.x + 100 + pathOffset, last.y - 100 - pathOffset, point.x - 100 - pathOffset, point.y - 100 - pathOffset, point.x, point.y);
99 } else {
100 path.S(point.x - 100, point.y - 100, point.x, point.y);
101 }
102
103 } else {
104
105 //const last = path.last();
106 //
107 //const previousContainer = path.findPrevious(d => d.info.container);
108 //
109 //let distance = 200;
110
111 //if (previousContainer) {
112 // distance = math.distanceBetweenPositions(previousContainer.info.container.position, cp.position);
113 //}
114
115 path.S(point.x - 100 - pathOffset, point.y - 100 - pathOffset, point.x, point.y);
116
117 }
118
119
120 if (cpRefIndex < cpCount && vnfdCpRef) {
121
122 const nextVnfdCpRef = this.getNetwork(cpList[cpRefIndex + 1].key + 'vnfd-connection-point-ref');
123
124 if (nextVnfdCpRef) {
125
126 const cpsOnSameVnfd = nextVnfdCpRef && nextVnfdCpRef.key === vnfdCpRef.key;
127 const cpsOnSameVld = nextVnfdCpRef.parent.key === vnfdCpRef.parent.key;
128
129 if (cpsOnSameVld) {
130
131 const vldCenter = vnfdCpRef.parent.position.centerPoint();
132
133 const last = path.last();
134
135 if (last.cmd === 'M') {
136 path.C(last.x, last.y + 40 + pathOffset, vldCenter.x, vldCenter.y - 40 - pathOffset, vldCenter.x, vldCenter.y);
137 } else {
138 path.S(vldCenter.x, vldCenter.y - 40 - pathOffset, vldCenter.x, vldCenter.y);
139 }
140
141 //const distance = math.distanceBetweenPositions(cp.position, vnfdCpRef.parent.position);
142
143 } else if (cpsOnSameVnfd) {
144
145 const last = path.last();
146
147 const vldCenter = vnfdCpRef.parent.position.centerPoint();
148
149 if (last.cmd === 'M') {
150 path.C(last.x, last.y + 40, vldCenter.x, vldCenter.y - 40, vldCenter.x, vldCenter.y);
151 } else {
152 path.S(vldCenter.x, vldCenter.y - 40, vldCenter.x, vldCenter.y);
153 }
154
155 }
156
157 }
158
159 }
160
161 return path.toString();
162
163 }, '')
164 }
165
166 });
167
168 path.exit().remove();
169
170 }
171
172 }