-
Notifications
You must be signed in to change notification settings - Fork 64
/
canvas.component.ts
2438 lines (2288 loc) · 108 KB
/
canvas.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/********************************************************************************
* Copyright (c) 2017-2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache Software License 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
********************************************************************************/
import {
AfterViewInit, Component, ElementRef, HostListener, Input, KeyValueDiffers, NgZone, OnChanges, OnDestroy, OnInit, QueryList, Renderer2, SimpleChanges,
ViewChild, ViewChildren
} from '@angular/core';
import { JsPlumbService } from '../services/jsPlumb.service';
import { EntityType, TNodeTemplate, TRelationshipTemplate, VisualEntityType } from '../models/ttopology-template';
import { LayoutDirective } from '../layout/layout.directive';
import { WineryActions } from '../redux/actions/winery.actions';
import { NgRedux } from '@angular-redux/store';
import { IWineryState } from '../redux/store/winery.store';
import { TopologyRendererActions } from '../redux/actions/topologyRenderer.actions';
import { NodeComponent } from '../node/node.component';
import { Hotkey, HotkeysService } from 'angular2-hotkeys';
import { ModalDirective } from 'ngx-bootstrap';
import { GridTemplate } from '../models/gridTemplate';
import { Subscription } from 'rxjs';
import { CapabilitiesModalData } from '../models/capabilitiesModalData';
import { RequirementsModalData } from '../models/requirementsModalData';
import { NodeIdAndFocusModel } from '../models/nodeIdAndFocusModel';
import { ToggleModalDataModel } from '../models/toggleModalDataModel';
import { ToastrService } from 'ngx-toastr';
import { BackendService } from '../services/backend.service';
import { CapabilityModel } from '../models/capabilityModel';
import { isNullOrUndefined } from 'util';
import { RequirementModel } from '../models/requirementModel';
import { EntityTypesModel } from '../models/entityTypesModel';
import { ExistsService } from '../services/exists.service';
import { ModalVariant, ModalVariantAndState } from './entities-modal/modal-model';
import { align, toggleModalType } from '../models/enums';
import { QName } from '../models/qname';
import { ImportTopologyModalData } from '../models/importTopologyModalData';
import { ImportTopologyService } from '../services/import-topology.service';
import { SplitMatchTopologyService } from '../services/split-match-topology.service';
import { PlaceComponentsService } from '../services/placement.service';
import { DifferenceStates, VersionUtils } from '../models/ToscaDiff';
import { ErrorHandlerService } from '../services/error-handler.service';
import { DragSource } from '../models/DragSource';
import { TopologyRendererState } from '../redux/reducers/topologyRenderer.reducer';
import { ThreatModelingModalData } from '../models/threatModelingModalData';
import { ThreatCreation } from '../models/threatCreation';
import { TopologyTemplateUtil } from '../models/topologyTemplateUtil';
import { ReqCapRelationshipService } from '../services/req-cap-relationship.service';
import { TPolicy } from '../models/policiesModalData';
import { WineryRepositoryConfigurationService } from '../../../../tosca-management/src/app/wineryFeatureToggleModule/WineryRepositoryConfiguration.service';
import { RequirementDefinitionModel } from '../models/requirementDefinitonModel';
import { CapabilityDefinitionModel } from '../models/capabilityDefinitionModel';
import { WineryRowData } from '../../../../tosca-management/src/app/wineryTableModule/wineryTable.component';
import { InheritanceUtils } from '../models/InheritanceUtils';
@Component({
selector: 'winery-canvas',
providers: [LayoutDirective],
templateUrl: './canvas.component.html',
styleUrls: ['./canvas.component.css']
})
export class CanvasComponent implements OnInit, OnDestroy, OnChanges, AfterViewInit {
@ViewChildren(NodeComponent) nodeComponentChildren: QueryList<NodeComponent>;
@ViewChildren('KVTextareas') KVTextareas: QueryList<any>;
@ViewChildren('XMLTextareas') xmlTextareas: QueryList<any>;
@ViewChildren('YamlPolicyProperties') yamlPolicyProperties: QueryList<any>;
@ViewChild('nodes') child: ElementRef;
@ViewChild('selection') selection: ElementRef;
@ViewChild('capabilitiesModal') capabilitiesModal: ModalDirective;
@ViewChild('requirementsModal') requirementsModal: ModalDirective;
@ViewChild('importTopologyModal') importTopologyModal: ModalDirective;
@ViewChild('threatModelingModal') threatModelingModal: ModalDirective;
@ViewChild('manageYamlPoliciesModal') manageYamlPoliciesModal: ModalDirective;
@ViewChild('addYamlPolicyModal') addYamlPolicyModal: ModalDirective;
@Input() readonly: boolean;
@Input() entityTypes: EntityTypesModel;
@Input() diffMode = false;
@Input() sidebarDeleteButtonClickEvent: any;
readonly draggingThreshold = 300;
readonly newNodePositionOffsetX = 108;
readonly newNodePositionOffsetY = 30;
allNodeTemplates: Array<TNodeTemplate> = [];
allRelationshipTemplates: Array<TRelationshipTemplate> = [];
topologyRendererState: TopologyRendererState;
selectedNodes: Array<TNodeTemplate> = [];
// current data emitted from a node
currentModalData: any;
dragSourceActive = false;
event: any;
selectedRelationshipType: EntityType;
nodeChildrenIdArray: Array<string>;
nodeChildrenArray: Array<NodeComponent>;
jsPlumbBindConnection = false;
newNode: TNodeTemplate;
paletteOpened: boolean;
newJsPlumbInstance: any;
gridTemplate: GridTemplate;
allNodesIds: Array<string> = [];
dragSourceInfos: DragSource;
longPress: boolean;
startTime: number;
endTime: number;
subscriptions: Array<Subscription> = [];
// unbind mouse move and up functions
unbindMouseActions: Array<Function> = [];
// variables which hold their corresponding modal data
capabilities: CapabilitiesModalData;
requirements: RequirementsModalData;
importTopologyData: ImportTopologyModalData;
threatModelingData: ThreatModelingModalData;
// threatmodeling accordion state
threatModalTab = 'create';
indexOfNewNode: number;
targetNodes: Array<string> = [];
// scroll offset
scrollOffset = 0;
// modalVariantAndState is passed to the entities-modal component and tells it which modal to render
modalData: ModalVariantAndState = {
modalVisible: true,
modalVariant: ModalVariant.None,
modalTitle: 'none'
};
// used to display the correct modal sort
showCurrentRequirement: boolean;
showCurrentCapability: boolean;
showDefaultProperties: boolean;
hideNavBarAndPalette = false;
// holds all Id's of the topology template
allIds: Array<String> = [];
// determines if a warning in the modal of having duplicate Id's is shown
duplicateId = false;
private longPressing: boolean;
// Manage YAML Policies Modal
selectedNewPolicyType: string;
yamlPoliciesColumns = [
{ title: 'Name', name: 'name' },
{ title: 'Type', name: 'typeHref' }];
selectedYamlPolicy: TPolicy;
/**
* Used to change the policy type fields into clickable <a> elements.
* Must be populated on every show of the modal!
*/
copyOfYamlPolicies: {
name: string,
policyType: string,
typeHref: string,
properties?: any,
targets?: string[]
}[];
constructor(private jsPlumbService: JsPlumbService,
private eref: ElementRef,
private layoutDirective: LayoutDirective,
private ngRedux: NgRedux<IWineryState>,
private actions: WineryActions,
private topologyRendererActions: TopologyRendererActions,
private zone: NgZone,
private hotkeysService: HotkeysService,
private renderer: Renderer2,
private alert: ToastrService,
private differs: KeyValueDiffers,
private backendService: BackendService,
private importTopologyService: ImportTopologyService,
private existsService: ExistsService,
private splitMatchService: SplitMatchTopologyService,
private placementService: PlaceComponentsService,
private errorHandler: ErrorHandlerService,
private reqCapRelationshipService: ReqCapRelationshipService,
private notify: ToastrService,
private configuration: WineryRepositoryConfigurationService) {
this.newJsPlumbInstance = this.jsPlumbService.getJsPlumbInstance();
this.newJsPlumbInstance.setContainer('container');
this.subscriptions.push(this.ngRedux.select(state => state.wineryState.currentJsonTopology.policies)
.subscribe(policies => this.handleUpdatedYamlPolicies(policies)));
this.subscriptions.push(this.ngRedux.select(state => state.wineryState.currentJsonTopology.nodeTemplates)
.subscribe(currentNodes => this.updateNodes(currentNodes)));
this.subscriptions.push(this.ngRedux.select(state => state.wineryState.currentJsonTopology.relationshipTemplates)
.subscribe(currentRelationships => this.updateRelationships(currentRelationships)));
this.subscriptions.push(this.ngRedux.select(state => state.topologyRendererState)
.subscribe(currentButtonsState => this.setRendererState(currentButtonsState)));
this.subscriptions.push(this.ngRedux.select(state => state.wineryState.currentNodeData)
.subscribe(currentNodeData => this.toggleMarkNode(currentNodeData)));
this.gridTemplate = new GridTemplate(100, false, false, 30);
this.subscriptions.push(this.ngRedux.select(state => state.wineryState.currentPaletteOpenedState)
.subscribe(currentPaletteOpened => this.setPaletteState(currentPaletteOpened)));
this.hotkeysService.add(new Hotkey('mod+a', (event: KeyboardEvent): boolean => {
event.stopPropagation();
this.allNodeTemplates.forEach(node => this.enhanceDragSelection(node.id));
return false; // Prevent bubbling
}, undefined, 'Select all Node Templates'));
this.hotkeysService.add(new Hotkey('del', (event: KeyboardEvent): boolean => {
this.handleDeleteKeyEvent();
return false;
}, undefined, 'Delete an element.'));
this.capabilities = new CapabilitiesModalData();
this.requirements = new RequirementsModalData();
this.importTopologyData = new ImportTopologyModalData();
this.threatModelingData = new ThreatModelingModalData();
}
ngOnChanges(changes: SimpleChanges) {
if (changes) {
const buttonClickEvent = changes.sidebarDeleteButtonClickEvent;
this.handleDeleteKeyEvent();
}
}
/**
* Needed for the optimal user experience when dragging a selection box.
* Upon detecting a long mouse down the navbar and the palette fade out for maximum dragging space.
* Resets the values.
*/
@HostListener('mouseup')
onMouseUp() {
this.longPressing = false;
}
/**
* Needed for the optimal user experience when dragging a selection box.
* Upon detecting a long mouse down the navbar and the palette fade out for maximum dragging space.
* Sets the values upon detecting a long mouse down press.
*/
@HostListener('mousedown', ['$event'])
onMouseDown(event: MouseEvent) {
// don't do right/middle clicks
if (event.button === 0) {
this.longPressing = false;
setTimeout(() => this.longPressing = true, 250);
}
}
/**
* Gets called if nodes get deleted, created, or node attributes are updated and calls the
* correct handler.
* @param currentNodes List of all displayed nodes.
*/
updateNodes(currentNodes: Array<TNodeTemplate>): void {
const storeNodesLength = currentNodes.length;
const localCopyNodesLength = this.allNodeTemplates.length;
if (storeNodesLength !== localCopyNodesLength) {
const difference = currentNodes.length - this.allNodeTemplates.length;
if (difference === 1 && this.paletteOpened) {
this.handleNewNode(currentNodes);
} else if (difference < 0) {
this.handleDeletedNodes(currentNodes);
} else if (difference === 1 && !this.paletteOpened) {
this.allNodeTemplates.push(currentNodes[currentNodes.length - 1]);
this.revalidateContainer();
} else {
this.allNodeTemplates = currentNodes;
}
} else if (storeNodesLength !== 0 && localCopyNodesLength !== 0) {
this.updateNodeAttributes(currentNodes);
}
this.allNodesIds = this.allNodeTemplates.map(node => node.id);
}
/**
* Executed when a node is short clicked triggering the sidebar, focusing on the name input field and
* upon unfocusing the input field blurs away
* @param currentNodeData - holds the node id and a focus boolean value which determines the marking or unmarking
* of the node
*/
toggleMarkNode(currentNodeData: NodeIdAndFocusModel) {
if (this.nodeChildrenArray) {
this.nodeChildrenArray.forEach(node => {
if (node.nodeTemplate.id === currentNodeData.id) {
if (currentNodeData.focus === true) {
node.makeSelectionVisible = true;
} else {
node.makeSelectionVisible = false;
}
}
});
}
}
/**
* Setter for PaletteState, triggered by a redux store change and getting latest value
* @param currentPaletteOpened
*/
setPaletteState(currentPaletteOpened: boolean): void {
this.paletteOpened = currentPaletteOpened;
if (this.paletteOpened) {
this.gridTemplate.marginLeft = 300;
} else {
this.gridTemplate.marginLeft = 30;
}
}
/**
* This modal handler gets triggered by the node component
* @param currentNodeData - this holds the corresponding node template information and the information which modal
* to show
*/
public toggleModalHandler(currentNodeData: ToggleModalDataModel) {
this.currentModalData = currentNodeData;
this.modalData.modalVisible = true;
this.duplicateId = false;
this.getAllIds();
switch (currentNodeData.currentNodePart) {
case toggleModalType.DeploymentArtifacts:
this.modalData.modalVariant = ModalVariant.DeploymentArtifacts;
this.modalData.modalTitle = 'Deployment Artifact';
break;
case toggleModalType.Policies:
this.modalData.modalVariant = ModalVariant.Policies;
this.modalData.modalTitle = 'Policy';
break;
case toggleModalType.Requirements:
this.modalData.modalVariant = ModalVariant.Other;
this.modalData.modalVisible = false;
this.resetRequirements();
this.requirements.requirements = currentNodeData.requirements;
this.requirements.nodeId = currentNodeData.id;
// if a requirement in the table is clicked show the data in the modal
if (!isNullOrUndefined(currentNodeData.currentRequirement)) {
this.showCurrentRequirement = true;
this.requirements.reqId = currentNodeData.currentRequirement.id;
this.requirements.oldReqId = currentNodeData.currentRequirement.id;
this.requirements.reqDefinitionName = currentNodeData.currentRequirement.name;
this.requirements.reqQName = currentNodeData.currentRequirement.type;
this.requirements.reqQNameLocalName = new QName(currentNodeData.currentRequirement.type).localName;
// check which propertyType is defined by checking with the defined requirement type property types
// from the repository
this.entityTypes.requirementTypes.some(reqType => {
if (currentNodeData.currentRequirement.type === reqType.qName) {
// if any is defined with at least one element it's a KV property, sets default values if
// there aren't any in the node template
if (reqType.full.serviceTemplateOrNodeTypeOrNodeTypeImplementation[0].any.length > 0) {
this.requirements.propertyType = 'KV';
if (currentNodeData.currentRequirement.properties) {
if (currentNodeData.currentRequirement.properties.kvproperties) {
this.requirements.properties = currentNodeData.currentRequirement.properties.kvproperties;
return true;
} else {
this.requirements.properties = TopologyTemplateUtil.getKVProperties(reqType);
this.setDefaultReqKVProperties();
return true;
}
} else {
this.requirements.properties = TopologyTemplateUtil.getKVProperties(reqType);
this.setDefaultReqKVProperties();
return true;
}
// if propertiesDefinition is defined it's a XML property
} else if (reqType.full.serviceTemplateOrNodeTypeOrNodeTypeImplementation[0].propertiesDefinition) {
if (reqType.full.serviceTemplateOrNodeTypeOrNodeTypeImplementation[0].propertiesDefinition.element) {
this.requirements.propertyType = 'XML';
const defaultXML = reqType.full.serviceTemplateOrNodeTypeOrNodeTypeImplementation[0].propertiesDefinition.element;
if (currentNodeData.currentRequirement.properties) {
if (currentNodeData.currentRequirement.properties.any) {
this.requirements.properties = currentNodeData.currentRequirement.properties.any;
return true;
} else {
this.requirements.properties = defaultXML;
this.setDefaultReqXMLProperties();
return true;
}
} else {
this.requirements.properties = defaultXML;
this.setDefaultReqXMLProperties();
return true;
}
}
} else {
// else no properties
this.requirements.propertyType = '';
return true;
}
}
});
} else {
this.showCurrentRequirement = false;
try {
// request all valid requirement types for that node type for display as name select options in
// the modal
const data = InheritanceUtils.getRequirementDefinitionsOfNodeType(currentNodeData.type, this.entityTypes);
this.requirements.reqDefinitionNames = [];
this.requirements.reqDefinitionName = '';
for (const reqType of data) {
const qNameOfType = new QName(reqType.requirementType);
this.requirements.reqDefinitionNames.push(qNameOfType.localName);
}
} catch (e) {
this.requirements.requirements = '';
}
}
this.requirementsModal.show();
break;
case toggleModalType.Capabilities:
this.modalData.modalVariant = ModalVariant.Other;
this.modalData.modalVisible = false;
this.resetCapabilities();
this.capabilities.capabilities = currentNodeData.capabilities;
this.capabilities.nodeId = currentNodeData.id;
// if a capability in the table is clicked show the data in the modal
if (!isNullOrUndefined(currentNodeData.currentCapability)) {
this.showCurrentCapability = true;
this.capabilities.capId = currentNodeData.currentCapability.id;
this.capabilities.oldCapId = currentNodeData.currentCapability.id;
this.capabilities.capDefinitionName = currentNodeData.currentCapability.name;
this.capabilities.capQName = currentNodeData.currentCapability.type;
this.capabilities.capQNameLocalName = new QName(currentNodeData.currentCapability.type).localName;
// check which propertyType is defined by checking with the defined capability type property types
// from the repository
this.entityTypes.capabilityTypes.some(capType => {
if (currentNodeData.currentCapability.type === capType.qName) {
// if any is defined with at least one element it's a KV property, sets default values if
// there aren't any in the node template
if (capType.full.serviceTemplateOrNodeTypeOrNodeTypeImplementation[0].any.length > 0) {
this.capabilities.propertyType = 'KV';
if (currentNodeData.currentCapability.properties) {
if (currentNodeData.currentCapability.properties.kvproperties) {
this.capabilities.properties = currentNodeData.currentCapability.properties.kvproperties;
return true;
} else {
this.capabilities.properties = TopologyTemplateUtil.getKVProperties(capType);
this.setDefaultCapKVProperties();
return true;
}
} else {
this.capabilities.properties = TopologyTemplateUtil.getKVProperties(capType);
this.setDefaultCapKVProperties();
return true;
}
// if propertiesDefinition is defined it's a XML property
} else if (capType.full.serviceTemplateOrNodeTypeOrNodeTypeImplementation[0].propertiesDefinition) {
if (capType.full.serviceTemplateOrNodeTypeOrNodeTypeImplementation[0].propertiesDefinition.element) {
this.capabilities.propertyType = 'XML';
const defaultXML = capType.full.serviceTemplateOrNodeTypeOrNodeTypeImplementation[0].propertiesDefinition.element;
if (currentNodeData.currentCapability.properties) {
if (currentNodeData.currentCapability.properties.any) {
this.capabilities.properties = currentNodeData.currentCapability.properties.any;
return true;
} else {
this.capabilities.properties = defaultXML;
this.setDefaultCapXMLProperties();
return true;
}
} else {
this.capabilities.properties = defaultXML;
this.setDefaultCapXMLProperties();
return true;
}
}
} else {
// else no properties
this.capabilities.propertyType = '';
return true;
}
}
});
} else {
this.showCurrentCapability = false;
try {
// request all valid capability types for that node type for display as name select options in
// the modal
const data = InheritanceUtils.getCapabilityDefinitionsOfNodeType(currentNodeData.type, this.entityTypes);
this.capabilities.capDefinitionNames = [];
this.capabilities.capDefinitionName = '';
for (const capType of data) {
const qNameOfType = new QName(capType.capabilityType);
this.capabilities.capDefinitionNames.push(qNameOfType.localName);
}
} catch (e) {
this.capabilities.capabilities = '';
}
}
this.capabilitiesModal.show();
break;
}
}
/**
* This function sets the capability default KV properties
*/
setDefaultCapKVProperties(): void {
this.capabilities.capabilities.capability.some(cap => {
if (cap.id === this.currentModalData.currentCapability.id) {
cap.properties = {
kvproperties:
this.capabilities.properties
};
}
});
}
/**
* This function sets the requirement default KV properties
*/
setDefaultReqKVProperties(): void {
this.requirements.requirements.requirement.some(req => {
if (req.id === this.currentModalData.currentRequirement.id) {
req.properties = {
kvproperties:
this.requirements.properties
};
}
});
}
/**
* This function sets the capability default XML properties
*/
setDefaultCapXMLProperties(): void {
this.capabilities.capabilities.capability.some(cap => {
if (cap.id === this.currentModalData.currentCapability.id) {
cap.properties = {
any: this.capabilities.properties
};
}
});
}
/**
* This function sets the requirement default XML properties
*/
setDefaultReqXMLProperties(): void {
this.requirements.requirements.requirement.some(req => {
if (req.id === this.currentModalData.currentCapability.id) {
req.properties = {
any: this.requirements.properties
};
}
});
}
/**
* Gets called from the modal to update all the capability data
*/
updateCaps(): void {
let currentIndex;
// search for the kv property index within the requirement object of the requirements array of the current
// requirement
this.capabilities.capabilities.capability.some((cap, index) => {
if (cap.id === this.capabilities.oldCapId) {
currentIndex = index;
return true;
}
});
if (this.capabilities.propertyType === 'KV') {
this.KVTextareas.forEach(txtArea => {
const keyOfChangedTextArea = txtArea.nativeElement.parentElement.innerText.replace(/\s/g, '');
this.capabilities.capabilities.capability[currentIndex].properties.kvproperties[keyOfChangedTextArea] = txtArea.nativeElement.value;
});
} else if (this.capabilities.propertyType === 'XML') {
this.xmlTextareas.forEach(xmlTextArea => {
this.capabilities.capabilities.capability[currentIndex].properties.any = xmlTextArea.nativeElement.value;
});
}
this.capabilities.capabilities.capability[currentIndex].id = this.capabilities.capId;
const newCapabilityData = this.capabilities.capabilities;
newCapabilityData.nodeId = this.capabilities.nodeId;
this.ngRedux.dispatch(this.actions.setCapability(newCapabilityData));
this.resetCapabilities();
this.capabilitiesModal.hide();
}
/**
* Gets called from the modal to update all the requirement data
*/
updateReqs(): void {
let currentIndex;
// search for the kv property index within the requirement object of the requirements array of the current
// requirement
this.requirements.requirements.requirement.some((req, index) => {
if (req.id === this.requirements.oldReqId) {
currentIndex = index;
return true;
}
});
if (this.requirements.propertyType === 'KV') {
this.KVTextareas.forEach(txtArea => {
const keyOfChangedTextArea = txtArea.nativeElement.parentElement.innerText.replace(/\s/g, '');
this.requirements.requirements.requirement[currentIndex].properties.kvproperties[keyOfChangedTextArea] = txtArea.nativeElement.value;
});
} else if (this.requirements.propertyType === 'XML') {
this.xmlTextareas.forEach(xmlTextArea => {
this.requirements.requirements.requirement[currentIndex].properties.any = xmlTextArea.nativeElement.value;
});
}
this.requirements.requirements.requirement[currentIndex].id = this.requirements.reqId;
const newRequirementData = this.requirements.requirements;
newRequirementData.nodeId = this.requirements.nodeId;
this.ngRedux.dispatch(this.actions.setRequirement(newRequirementData));
this.resetRequirements();
this.requirementsModal.hide();
}
/**
* Saves a capability template to the model and gets pushed into the Redux store of the application
*/
saveCapabilityToModel(): void {
const newCapability = new CapabilityModel();
newCapability.any = [];
newCapability.documentation = [];
newCapability.id = this.capabilities.capId;
newCapability.name = this.capabilities.capQName.substring(this.capabilities.capQName.indexOf('}') + 1);
newCapability.otherAttributes = {};
newCapability.type = this.capabilities.capQName;
// case that a capability type with KV properties was chosen in the model and the default KV properties are
// shown and modified by the user
if (this.capabilities.propertyType === 'KV') {
// get all values from the KV property textareas
this.KVTextareas.forEach(txtArea => {
const keyOfChangedTextArea = txtArea.nativeElement.parentElement.innerText.replace(/\s/g, '');
this.capabilities.properties[keyOfChangedTextArea] = txtArea.nativeElement.value;
});
newCapability.properties = {
kvproperties: this.capabilities.properties
};
// case that a capability type with XML properties was chosen in the model and the default XML properties
// are shown and modified by the user
} else if (this.capabilities.propertyType === 'XML') {
this.xmlTextareas.forEach(xmlTextArea => {
this.capabilities.properties = xmlTextArea.nativeElement.value;
});
newCapability.properties = {
any: this.capabilities.properties
};
}
// case when there are no capabilities on the node template
if (!this.capabilities.capabilities || !this.capabilities.capabilities.capability) {
const capabilityArray: Array<CapabilityModel> = [];
this.capabilities.capabilities = {
capability: capabilityArray
};
}
this.capabilities.capabilities.capability.push(newCapability);
const newCapabilityData = this.capabilities.capabilities;
newCapabilityData.nodeId = this.capabilities.nodeId;
this.ngRedux.dispatch(this.actions.setCapability(newCapabilityData));
this.resetCapabilities();
this.capabilitiesModal.hide();
}
/**
* Auto-completes other capability relevant values when a capability name has been selected in the modal
*/
onChangeCapDefinitionName(capName: string) {
this.entityTypes.capabilityTypes.some(cap => {
if (cap.name === capName) {
this.capabilities.capType = cap.namespace;
this.capabilities.capQName = cap.qName;
this.capabilities.capQNameLocalName = new QName(cap.qName).localName;
// check which propertyType is defined by checking with the defined capability types from the
// repository
// if any is defined with at least one element it's a KV property, sets default values if there aren't
// any in the node template
if (cap.full.serviceTemplateOrNodeTypeOrNodeTypeImplementation[0].any.length > 0) {
this.capabilities.propertyType = 'KV';
this.showDefaultProperties = true;
this.capabilities.properties = TopologyTemplateUtil.getKVProperties(cap);
// if propertiesDefinition is defined it's a XML property
} else if (cap.full.serviceTemplateOrNodeTypeOrNodeTypeImplementation[0].propertiesDefinition) {
if (cap.full.serviceTemplateOrNodeTypeOrNodeTypeImplementation[0].propertiesDefinition.element) {
this.capabilities.propertyType = 'XML';
this.showDefaultProperties = true;
this.capabilities.properties = cap.full.serviceTemplateOrNodeTypeOrNodeTypeImplementation[0].propertiesDefinition.element;
}
} else {
// else no properties
this.capabilities.propertyType = '';
this.showDefaultProperties = false;
}
return true;
}
});
}
/**
* saves the typed in capability id from the modal
*/
onChangeCapId(capId: string) {
if (!this.allIds.find(id => id === capId)) {
this.capabilities.capId = capId;
this.duplicateId = false;
} else {
this.duplicateId = true;
}
}
/**
* Deletes a capability from the winery store
*/
deleteCapability() {
const capabilities = {
nodeId: this.currentModalData.id,
capability: this.currentModalData.capabilities.capability.filter(req => req.id !== this.currentModalData.currentCapability.id)
};
this.ngRedux.dispatch(this.actions.setCapability(capabilities));
this.resetCapabilities();
this.capabilitiesModal.hide();
}
/**
* Saves a requirement template to the model and gets pushed into the Redux store of the application
*/
saveRequirementsToModel(): void {
const newRequirement = new RequirementModel();
newRequirement.any = [];
newRequirement.documentation = [];
newRequirement.id = this.requirements.reqId;
newRequirement.name = this.requirements.reqQName.substring(this.requirements.reqQName.indexOf('}') + 1);
newRequirement.otherAttributes = {};
newRequirement.type = this.requirements.reqQName;
// case that a requirement type with KV properties was chosen in the model and the default KV properties are
// shown and modified by the user
if (this.requirements.propertyType === 'KV') {
// get all values from the KV property textareas
this.KVTextareas.forEach(txtArea => {
const keyOfChangedTextArea = txtArea.nativeElement.parentElement.innerText.replace(/\s/g, '');
this.requirements.properties[keyOfChangedTextArea] = txtArea.nativeElement.value;
});
newRequirement.properties = {
kvproperties: this.requirements.properties
};
// case that a requirement type with XML properties was chosen in the model and the default XML properties
// are shown and modified by the user
} else if (this.requirements.propertyType === 'XML') {
this.xmlTextareas.forEach(xmlTextArea => {
this.requirements.properties = xmlTextArea.nativeElement.value;
});
newRequirement.properties = {
any: this.requirements.properties
};
}
// case when there are no requirements on the node template
if (!this.requirements.requirements || !this.requirements.requirements.requirement) {
const requirementsArray: Array<RequirementModel> = [];
this.requirements.requirements = {
requirement: requirementsArray
};
}
this.requirements.requirements.requirement.push(newRequirement);
const newRequirementData = this.requirements.requirements;
newRequirementData.nodeId = this.requirements.nodeId;
this.ngRedux.dispatch(this.actions.setRequirement(newRequirementData));
this.resetRequirements();
this.requirementsModal.hide();
}
/**
* Auto-completes other requirement relevant values when a requirement name has been selected in the modal
*/
onChangeReqDefinitionName(reqName: string): void {
this.entityTypes.requirementTypes.some(req => {
if (req.name === reqName) {
// this.requirements.reqId = req.id;
this.requirements.reqType = req.namespace;
this.requirements.reqQName = req.qName;
this.requirements.reqQNameLocalName = new QName(req.qName).localName;
// check which propertyType is defined by checking with the defined requirement types from the
// repository if any is defined with at least one element it's a KV property, sets default values if
// there aren't any in the node template
if (req.full.serviceTemplateOrNodeTypeOrNodeTypeImplementation[0].any.length > 0) {
this.requirements.propertyType = 'KV';
this.showDefaultProperties = true;
this.requirements.properties = TopologyTemplateUtil.getKVProperties(req);
return true;
// if propertiesDefinition is defined it's a XML property
} else if (req.full.serviceTemplateOrNodeTypeOrNodeTypeImplementation[0].propertiesDefinition) {
if (req.full.serviceTemplateOrNodeTypeOrNodeTypeImplementation[0].propertiesDefinition.element) {
this.requirements.propertyType = 'XML';
this.showDefaultProperties = true;
this.requirements.properties = req.full.serviceTemplateOrNodeTypeOrNodeTypeImplementation[0].propertiesDefinition.element;
}
} else {
// else no properties
this.requirements.propertyType = '';
this.showDefaultProperties = false;
}
return true;
}
});
}
/**
* saves the typed in requirement id from the modal
*/
onChangeReqId(reqId: string) {
if (!this.allIds.find(id => id === reqId)) {
this.requirements.reqId = reqId;
this.duplicateId = false;
} else {
this.duplicateId = true;
}
}
/**
* Deletes a requirement from the winery store
*/
deleteRequirement() {
const requirements = {
nodeId: this.currentModalData.id,
requirement: this.currentModalData.requirements.requirement.filter(req => req.id !== this.currentModalData.currentRequirement.id)
};
this.ngRedux.dispatch(this.actions.setRequirement(requirements));
this.resetRequirements();
this.requirementsModal.hide();
}
/**
* Resets the requirements modal data
*/
resetRequirements(): void {
this.requirements.reqId = '';
this.requirements.oldReqId = '';
this.requirements.reqDefinitionName = '';
this.requirements.reqDefinitionNames = [];
this.requirements.reqType = '';
this.requirements.reqQName = '';
this.requirements.reqQNameLocalName = '';
this.requirements.nodeId = '';
this.requirements.propertyType = '';
this.requirements.requirements = null;
this.requirements.properties = null;
}
/**
* Closes the requirements modal
*/
closeAndResetRequirements(): void {
this.requirementsModal.hide();
this.resetRequirements();
}
/**
* Resets the capabilities modal data
*/
resetCapabilities(): void {
this.capabilities.capId = '';
this.capabilities.oldCapId = '';
this.capabilities.capDefinitionName = '';
this.capabilities.capDefinitionNames = [];
this.capabilities.capType = '';
this.capabilities.capQNameLocalName = '';
this.capabilities.capQName = '';
this.capabilities.nodeId = '';
this.capabilities.propertyType = '';
this.capabilities.capabilities = null;
this.capabilities.properties = null;
}
/**
* Closes the capabilities modal
*/
closeAndResetCapabilities(): void {
this.capabilitiesModal.hide();
this.resetCapabilities();
}
/**
* New nodes can be dragged directly from the palette,
* adds the node to the internal representation
* @param event The html event.
*/
moveNewNode(event): void {
const x = event.clientX - this.newNodePositionOffsetX;
const y = event.clientY - this.newNodePositionOffsetY + this.scrollOffset;
this.allNodeTemplates[this.indexOfNewNode].x = x;
this.allNodeTemplates[this.indexOfNewNode].y = y;
}
/**
* Repositions the new node and repaints the screen
* @param $event The html event.
*/
positionNewNode(): void {
this.updateSelectedNodes();
this.unbindAll();
this.revalidateContainer();
}
/**
* Gets called if relationships get created, loaded from the server/ a JSON, deleted or updated and calls the
* correct handler.
* @param currentRelationships List of all displayed relationships.
*/
updateRelationships(currentRelationships: Array<TRelationshipTemplate>): void {
// workaround for a jsPlumb connection bug, where upon loading node templates without relationships no
// creation of relationships possible; delete the dummy relationship upon creating a new one
if (this.newJsPlumbInstance.getAllConnections().length === 2 && this.allRelationshipTemplates.length === 0) {
this.newJsPlumbInstance.deleteConnection(this.newJsPlumbInstance.getAllConnections()[0]);
}
const localRelationshipsCopyLength = this.allRelationshipTemplates.length;
const storeRelationshipsLength = currentRelationships.length;
if (storeRelationshipsLength !== localRelationshipsCopyLength) {
const difference = storeRelationshipsLength - localRelationshipsCopyLength;
if (difference === 1) {
this.handleNewRelationship(currentRelationships);
} else if (difference > 0 || difference < 0) {
if (this.configuration.isYaml() && difference < 0) {
// a relationship is deleted. reset the associated source requirement
const deletedRels = this.allRelationshipTemplates.filter(rel => currentRelationships.every(curRel => curRel.id !== rel.id));
deletedRels.forEach(deletedRel => {
const reqId = deletedRel.sourceElement.ref;
const sourceNodeTemplate = this.allNodeTemplates
.find(nt =>
nt.requirements &&
nt.requirements.requirement
&& nt.requirements.requirement.some(req => req.id === reqId));
const requirementModel: RequirementModel = sourceNodeTemplate.requirements.requirement
.find(req => req.id === reqId);
const requirementDefinition: RequirementDefinitionModel = InheritanceUtils
.getRequirementDefinitionsOfNodeType(sourceNodeTemplate.type, this.entityTypes)
.find(reqDef => reqDef.name === requirementModel.name);
requirementModel.capability = requirementDefinition.capability;
requirementModel.relationship = requirementDefinition.relationship;
requirementModel.node = requirementDefinition.node;
});
}
this.allRelationshipTemplates = currentRelationships;
this.allRelationshipTemplates.forEach(relTemplate => this.manageRelationships(relTemplate));
}
} else if (storeRelationshipsLength !== 0 && localRelationshipsCopyLength !== 0) {
this.updateRelName(currentRelationships);
}
}
/**
* Handler for new relations, adds it to the internal representation
* @param currentRelationships List of all displayed relations.
*/
handleNewRelationship(currentRelationships: Array<TRelationshipTemplate>): void {
const newRel = currentRelationships[currentRelationships.length - 1];
this.allRelationshipTemplates.push(newRel);
this.manageRelationships(newRel);
}
/**
* Implements some checks if name of relation gets updated
* @param currentRelationships List of all displayed relations.
*/
updateRelName(currentRelationships: Array<TRelationshipTemplate>): void {
this.allRelationshipTemplates.some(rel => {
const conn = currentRelationships.find(el => el.id === rel.id);
if (conn) {
if (rel.name !== conn.name) {
rel.name = conn.name;
return true;
}
}
});
}
/**
* Handler for the layout buttons.
* @param rendererState Representation of all possible buttons.
*/
setRendererState(rendererState: TopologyRendererState): void {
if (rendererState) {
this.topologyRendererState = rendererState;
this.revalidateContainer();
let leaveNodesAsSelectedAfterLayouting;
if (this.topologyRendererState.buttonsState.layoutButton) {
this.layoutDirective.layoutNodes(this.nodeChildrenArray, this.allRelationshipTemplates)
.then(done => {
leaveNodesAsSelectedAfterLayouting = false;
// This call might seem confusing as we are calling it again right after executing,
// but this just toggles the button state back to false, so layout can be called again.
// TODO: change this behavior to simple events without a boolean flag
this.ngRedux.dispatch(this.topologyRendererActions.executeLayout());
});
} else if (this.topologyRendererState.buttonsState.alignHButton
|| this.topologyRendererState.buttonsState.alignVButton) {
const selectionActive = (this.selectedNodes.length >= 1);
const nodesToBeAligned = selectionActive ? this.selectedNodes : this.allNodeTemplates;
leaveNodesAsSelectedAfterLayouting = selectionActive;
const alignmentMode = this.topologyRendererState.buttonsState.alignHButton ? align.Horizontal : align.Vertical;
this.layoutDirective.align(this.nodeChildrenArray, nodesToBeAligned, alignmentMode)