merge from ecomp a88f0072 - Modern UI
[vid.git] / vid-webpack-master / src / app / drawingBoard / service-planning / available-models-tree / available-models-tree.component.ts
1 import {Component, EventEmitter, Output, ViewChild} from '@angular/core';
2 import {ITreeOptions, TreeComponent} from 'angular-tree-component';
3 import {IDType, ITreeNode} from 'angular-tree-component/dist/defs/api';
4 import {DialogService} from 'ng2-bootstrap-modal';
5 import {AvailableModelsTreeService} from './available-models-tree.service';
6 import {NgRedux} from "@angular-redux/store";
7 import {ActivatedRoute} from '@angular/router';
8 import {AppState} from '../../../shared/store/reducers';
9 import {AaiService} from '../../../shared/services/aaiService/aai.service';
10 import {ServiceNodeTypes} from '../../../shared/models/ServiceNodeTypes';
11 import {IframeService} from "../../../shared/utils/iframe.service";
12 import {DefaultDataGeneratorService} from "../../../shared/services/defaultDataServiceGenerator/default.data.generator.service";
13 import {VfModulePopuopService} from "../../../shared/components/genericFormPopup/genericFormServices/vfModule/vfModule.popuop.service";
14 import {NetworkPopupService} from "../../../shared/components/genericFormPopup/genericFormServices/network/network.popup.service";
15 import {createVFModuleInstance} from "../../../shared/storeUtil/utils/vfModule/vfModule.actions";
16 import {VnfPopupService} from "../../../shared/components/genericFormPopup/genericFormServices/vnf/vnf.popup.service";
17 import {DrawingBoardModes} from "../drawing-board.modes";
18 import {DrawingBoardTreeService} from "../drawing-board-tree/drawing-board-tree.service";
19 import {ObjectToModelTreeService} from "../objectsToTree/objectToModelTree/objectToModelTree.service";
20 import {VnfGroupPopupService} from "../../../shared/components/genericFormPopup/genericFormServices/vnfGroup/vnfGroup.popup.service";
21 import {SharedTreeService} from "../objectsToTree/shared.tree.service";
22 import {changeInstanceCounter} from "../../../shared/storeUtil/utils/general/general.actions";
23 import {createVnfGroupInstance} from "../../../shared/storeUtil/utils/vnfGroup/vnfGroup.actions";
24 import {VnfGroupControlGenerator} from "../../../shared/components/genericForm/formControlsServices/vnfGroupGenerator/vnfGroup.control.generator";
25 import {HighlightPipe} from "../../../shared/pipes/highlight/highlight-filter.pipe";
26 import * as _ from 'lodash';
27 import {DrawingBoardTreeComponent} from "../drawing-board-tree/drawing-board-tree.component";
28
29
30 @Component({
31   selector: 'available-models-tree',
32   templateUrl: './available-models-tree.component.html',
33   styleUrls: ['./available-models-tree.component.scss'],
34   providers : [HighlightPipe]
35 })
36
37 export class AvailableModelsTreeComponent {
38   filterValue : string = '';
39   serviceModelId: string;
40   serviceHierarchy;
41   parentElementClassName = 'content';
42   _store: NgRedux<AppState>;
43   isNewObject: boolean;
44   availableModelsTreeService: AvailableModelsTreeService;
45   drawingBoardTreeService: DrawingBoardTreeService;
46
47   constructor(private _iframeService: IframeService,
48               private _aaiService: AaiService,
49               private route: ActivatedRoute,
50               private dialogService: DialogService,
51               private _availableModelsTreeService: AvailableModelsTreeService,
52               private _drawingBoardTreeService: DrawingBoardTreeService,
53               private _defaultDataGeneratorService: DefaultDataGeneratorService,
54               private _vnfGroupControlGenerator: VnfGroupControlGenerator,
55               private _vfModulePopuopService: VfModulePopuopService,
56               private _vnfGroupPopupService: VnfGroupPopupService,
57               private _vnfPopupService: VnfPopupService,
58               private _networkPopupService: NetworkPopupService,
59               private  store: NgRedux<AppState>,
60               private _objectToModelTreeService : ObjectToModelTreeService,
61               private  _sharedTreeService : SharedTreeService,
62               private _highlightPipe : HighlightPipe) {
63     this.availableModelsTreeService = _availableModelsTreeService;
64     this.drawingBoardTreeService = _drawingBoardTreeService;
65
66     this._store = store;
67     this.route
68       .queryParams
69       .subscribe(params => {
70         this.serviceModelId = params['serviceModelId'];
71         this._aaiService.getServiceModelById(this.serviceModelId).subscribe(
72           value => {
73             this.serviceHierarchy = value;
74             this.nodes = this._objectToModelTreeService.convertServiceHierarchyModelToTreeNodes(this.serviceHierarchy);
75           },
76           error => {
77             console.log('error is ', error)
78           }
79         );
80       });
81
82   }
83
84   @Output()
85   highlightInstances: EventEmitter<number> = new EventEmitter<number>();
86   @ViewChild('tree') tree: TreeComponent;
87
88   nodes = [];
89   service = {name: ''};
90
91   options: ITreeOptions = {
92     nodeHeight: 36,
93     dropSlotHeight: 0,
94     nodeClass: (node: ITreeNode) => {
95       if (node.data.type === ServiceNodeTypes.VFmodule && ! node.parent.data['getNodeCount'](node.parent, this.serviceModelId) && this.store.getState().global.drawingBoardStatus !== DrawingBoardModes.VIEW) {
96         node.data.disabled = true;
97         return 'tree-node tree-node-disabled';
98       }
99       node.data.disabled = false;
100       return 'tree-node';
101     }
102   };
103
104
105   getNodeName(node : ITreeNode, filter : string) {
106     return this._highlightPipe.transform(node.data.name ,filter ? filter : '');
107   }
108
109   expandParentByNodeId(id: IDType): void {
110     this.tree.treeModel.getNodeById(id).parent.expand();
111   }
112
113   updateNodes(updateData : {nodes : any, filterValue : string}) : void {
114     this.nodes = updateData.nodes;
115     this.filterValue = updateData.filterValue;
116   }
117
118   selectNode(node: ITreeNode): void {
119     node.expand();
120     this._sharedTreeService.setSelectedVNF(null);
121     this.highlightInstances.emit(node.data.modelUniqueId);
122   }
123
124
125
126   onClickAdd(node: ITreeNode, serviceId: string ,  isNewObject: boolean = false): void {
127     this.isNewObject = isNewObject;
128     let data = node.data;
129     let dynamicInputs = data.dynamicInputs;
130     let isAlaCarte: boolean = this.serviceHierarchy.service.instantiationType == "A-La-Carte";
131     let isEcompGeneratedNaming: boolean = data.isEcompGeneratedNaming;
132     let type: string = data.type;
133     if (!this.store.getState().global.flags['FLAG_SETTING_DEFAULTS_IN_DRAWING_BOARD'] || node.data.type === ServiceNodeTypes.VF ||
134       this._availableModelsTreeService.shouldOpenDialog(type, dynamicInputs, isEcompGeneratedNaming)) {
135       this._iframeService.addClassOpenModal(this.parentElementClassName);
136       node.data.onAddClick(node, serviceId);
137     } else {
138       if (node.data.type === ServiceNodeTypes.VnfGroup)  {
139         let instanceName = this._vnfGroupControlGenerator.getDefaultInstanceName(null, serviceId, node.data.name);
140         let vnfGroup = this._defaultDataGeneratorService.generateVnfGroupInstance(this.serviceHierarchy.vnfGroups[node.data.name], isEcompGeneratedNaming, isAlaCarte, instanceName);
141         this._store.dispatch(changeInstanceCounter(node.data.modelUniqueId, serviceId, 1 , <any> {data: {type: 'VnfGroup'}}));
142         this._store.dispatch(createVnfGroupInstance(vnfGroup, node.data.name, serviceId, node.data.name));
143         DrawingBoardTreeComponent.triggerreCalculateIsDirty.next(this.serviceModelId);
144       } else {
145         let vfModule = this._defaultDataGeneratorService.generateVFModule(this.serviceHierarchy.vnfs[node.parent.data.name].vfModules[node.data.name], dynamicInputs, isEcompGeneratedNaming, isAlaCarte);
146         if (this._sharedTreeService.selectedVNF) {
147           this.store.dispatch(createVFModuleInstance(vfModule, node.data.name, this.serviceModelId, null, this._sharedTreeService.selectedVNF));
148           DrawingBoardTreeComponent.triggerreCalculateIsDirty.next(this.serviceModelId);
149         } else if (this._availableModelsTreeService.getOptionalVNFs(this.serviceModelId, node.parent.data.name).length === 1) {
150           let existVnf = this._store.getState().service.serviceInstance[this.serviceModelId].vnfs;
151           if(!_.isNil(existVnf)){
152             for(let vnfKey in existVnf){
153               if(existVnf[vnfKey]['modelInfo'].modelUniqueId === node.parent.data.id){
154                 this.store.dispatch(createVFModuleInstance(vfModule, node.data.name, this.serviceModelId, null, vnfKey));
155                 DrawingBoardTreeComponent.triggerreCalculateIsDirty.next(this.serviceModelId);
156               }
157             }
158           }
159
160
161         } else {
162           this._availableModelsTreeService.addingAlertAddingNewVfModuleModal();
163         }
164       }
165     }
166   }
167 }