1 import {Injectable} from "@angular/core";
2 import {NgRedux} from "@angular-redux/store";
3 import {AppState} from "../../../shared/store/reducers";
4 import {ServiceInstanceActions} from "../../../shared/models/serviceInstanceActions";
5 import {MessageBoxData} from "../../../shared/components/messageBox/messageBox.data";
6 import {MessageBoxService} from "../../../shared/components/messageBox/messageBox.service";
7 import * as _ from "lodash";
8 import {DrawingBoardModes} from "../drawing-board.modes";
9 import {AuditInfoModalComponent} from "../../../shared/components/auditInfoModal/auditInfoModal.component";
10 import {VnfModelInfo} from "./models/vnf/vnf.model.info";
11 import {ILevelNodeInfo} from "./models/basic.model.info";
14 export class SharedTreeService {
15 private _sharedTreeService: SharedTreeService;
16 constructor(private _store: NgRedux<AppState>) {
19 /***********************************************************
20 * return if instance has missing data
21 * @param instance - vnf instance
22 * @param dynamicInputs - from the instance
23 * @param isEcompGeneratedNaming
24 ************************************************************/
25 selectedVNF: string = null;
28 getSelectedVNF(): string {
29 return this.selectedVNF;
32 setSelectedVNF(node): void {
33 if (_.isNil(node) || node.data.type !== 'VF') {
34 this.selectedVNF = null;
36 this.selectedVNF = node.data.vnfStoreKey;
40 hasMissingData(instance, dynamicInputs: any, isEcompGeneratedNaming: boolean, requiredFields: string[]): boolean {
41 if (!isEcompGeneratedNaming && _.isEmpty(instance.instanceName)) {
45 for (let field of requiredFields) {
46 if (_.isEmpty(instance[field])) {
51 for (let field of dynamicInputs) {
52 if (field.isRequired && !_.isNil(instance.instanceParams) && _.isEmpty(instance.instanceParams[0][field.id])) {
60 addingStatusProperty(node) {
61 node['statusProperties'] = [];
62 node['statusProperties'].push({key: 'Prov Status:', value: node.provStatus, testId: 'provStatus'});
63 node['statusProperties'].push({key: 'Orch Status:', value: node.orchStatus, testId: 'orchStatus'});
65 node['statusProperties'].push({key: 'In-maintenance', value: '', testId: 'inMaint'});
70 /**********************************************
71 * should delete or remove child instance's
72 "new" -> should remove
73 !new" -> should change action status
74 **********************************************/
75 removeDeleteAllChild(node, serviceModelId: string, callback): void {
76 for (let nodeChild of node.children) {
77 if (nodeChild.data.action === ServiceInstanceActions.Create) {
78 if (!_.isNil(nodeChild.data) && !_.isNil(nodeChild.data.menuActions) && !_.isNil(nodeChild.data.menuActions['remove'])) {
79 nodeChild.data.menuActions['remove']['method'](nodeChild, serviceModelId);
82 if (!_.isNil(nodeChild.data) && !_.isNil(nodeChild.data.menuActions) && !_.isNil(nodeChild.data.menuActions['delete'])) {
83 nodeChild.data.menuActions['delete']['method'](nodeChild, serviceModelId);
87 callback(node, serviceModelId);
91 /**********************************************
92 * should undo delete child instance's
93 **********************************************/
94 undoDeleteAllChild(node, serviceModelId: string, callback): void {
95 for (let nodeChild of node.children) {
96 if (!_.isNil(nodeChild.data) && !_.isNil(nodeChild.data.menuActions) && !_.isNil(nodeChild.data.menuActions['undoDelete'])) {
97 nodeChild.data.menuActions['undoDelete']['method'](nodeChild, serviceModelId);
100 callback(node, serviceModelId);
103 /**********************************************
104 * should return true if can delete
105 **********************************************/
106 shouldShowDelete(node): boolean {
107 const mode = this._store.getState().global.drawingBoardStatus;
108 if (!_.isNil(node) && !_.isNil(node.data) && !_.isNil(node.data.action) && !_.isNil(node.data.menuActions['delete'])) {
109 if (mode !== DrawingBoardModes.EDIT || node.data.action === ServiceInstanceActions.Create) {
111 } else if (node.data.action === ServiceInstanceActions.None) {
119 /**********************************************
120 * should return true if can undo delete
121 **********************************************/
122 shouldShowUndoDelete(node): boolean {
123 const mode = this._store.getState().global.drawingBoardStatus;
124 if (mode === DrawingBoardModes.EDIT && !_.isNil(node.data.action) && !_.isNil(node.data.menuActions['undoDelete'])) {
125 if (node.data.action === ServiceInstanceActions.Create || node.data.action === ServiceInstanceActions.Delete) {
127 } else if (node.data.action.split('_').pop() === 'Delete') {
134 /**********************************************
135 * should return true if can remove or edit
136 * enabled only on edit/design mode and for new instances
137 **********************************************/
138 shouldShowRemoveAndEdit(node): boolean {
139 const mode = this._store.getState().global.drawingBoardStatus;
140 if (!_.isNil(node) && !_.isNil(node.data) && !_.isNil(node.data.action) && node.data.action === ServiceInstanceActions.Create &&
141 mode !== DrawingBoardModes.VIEW && mode !== DrawingBoardModes.RETRY) {
146 /**********************************************
147 * should return true if can duplicate by mode
148 **********************************************/
149 shouldShowDuplicate(node): boolean {
150 const mode = this._store.getState().global.drawingBoardStatus;
151 return !mode.includes('RETRY');
154 /**********************************************
155 * should return true if can audit info
156 **********************************************/
157 shouldShowAuditInfo(node): boolean {
158 return this.isRetryMode() || (!_.isNil(node.data) && !_.isNil(node.data.action) && node.data.action !== ServiceInstanceActions.Create);
162 isRetryMode(): boolean {
163 const mode = this._store.getState().global.drawingBoardStatus;
164 return mode.includes('RETRY');
168 /**********************************************
169 * should return true if can add node instances
170 **********************************************/
171 shouldShowAddIcon(): boolean{
172 const mode = this._store.getState().global.drawingBoardStatus;
173 return mode === DrawingBoardModes.EDIT || mode=== DrawingBoardModes.CREATE;
175 /************************************************
176 return number of instances with action Delete
177 @type: vnfs networks, vngGroups (not vfModule)
178 @node : node model from the left tree
179 ************************************************/
180 getExistingInstancesWithDeleteMode(node, serviceModelId: string, type: string): number {
182 const existingInstances = this._store.getState().service.serviceInstance[serviceModelId][type];
183 const modelUniqueId = node.data.modelUniqueId;
184 if (!_.isNil(existingInstances)) {
185 for (let instanceKey in existingInstances) {
186 if (!_.isNil(existingInstances[instanceKey].action)) {
187 if (existingInstances[instanceKey].modelInfo.modelUniqueId === modelUniqueId && existingInstances[instanceKey].action.split('_').pop() === 'Delete') {
197 isServiceOnDeleteMode(serviceId: string): boolean {
198 return this._store.getState().service.serviceInstance[serviceId].action === ServiceInstanceActions.Delete;
202 openModal(node : any | any[] , serviceModelId : string, cb : Function) : void {
203 let type: string = _.isArray(node) ? 'Service' : node.data.typeName;
204 let messageBoxData: MessageBoxData = new MessageBoxData(
206 `You are about to mark for delete this ${type} this will also mark all its children and remove all new instances just added`,
211 text: "Mark and remove",
213 callback: cb.bind(this, node, serviceModelId),
216 {text: "Don’t Remove", size: "medium", closeModal: true}
219 MessageBoxService.openModal.next(messageBoxData);
222 someChildHasCreateAction(nodes: any | any[]) : boolean {
223 let nodesArr = _.isArray(nodes) ? nodes : [nodes];
224 for(const node of nodesArr){
225 if(node.action === ServiceInstanceActions.Create) {return true;}
227 for (let nodeChild of node.children) {
228 if (nodeChild.action === ServiceInstanceActions.Create) {
231 if(nodeChild.children && nodeChild.children.length > 0){
232 for(let child of nodeChild.children){
233 let hasCreateAction = this.someChildHasCreateAction(child);
234 if(hasCreateAction) {
245 shouldShowDeleteInstanceWithChildrenModal(node : any | any[] , serviceModelId : string, cb : Function) : void {
246 if(this.someChildHasCreateAction(node)){
247 this.openModal(node , serviceModelId, cb);
249 cb(node, serviceModelId)
254 isFailed(node): boolean {
255 return !_.isNil(node.data) ? node.data.isFailed : false;
258 /************************************************
259 in a case the node is failed e.g. not instantiated correctly
260 the function will call to openRetryInstanceAuditInfoModal
261 @node : node model from the left tree
262 @serviceModelId : serviceModelId
264 @instanceType: instanceType
265 @modelInfoService : the model (vnf, vfmodule, network, vnfgroup)object that call to the function (this)
266 ************************************************/
267 openAuditInfoModal(node, serviceModelId, instance, instanceType, modelInfoService : ILevelNodeInfo){
268 let isInstanceFailed = this.isFailed(node);
269 AuditInfoModalComponent.openInstanceAuditInfoModal.next({
270 instanceId: serviceModelId,
272 model: modelInfoService.getModel(node.data.modelName, instance, this._store.getState().service.serviceHierarchy[serviceModelId]),
275 trackById: instance.trackById