blob: 9e7a0211e9cf76a103d0a4390ae53d21218a97f4 [file] [log] [blame]
Ittay Stern6f900cc2018-08-29 17:01:32 +03001import {Injectable} from "@angular/core";
2import {NgRedux} from "@angular-redux/store";
3import {AppState} from "../../../shared/store/reducers";
4import {ServiceInstanceActions} from "../../../shared/models/serviceInstanceActions";
5import {MessageBoxData} from "../../../shared/components/messageBox/messageBox.data";
6import {MessageBoxService} from "../../../shared/components/messageBox/messageBox.service";
7import * as _ from "lodash";
8import {DrawingBoardModes} from "../drawing-board.modes";
9import {AuditInfoModalComponent} from "../../../shared/components/auditInfoModal/auditInfoModal.component";
Ittay Stern6f900cc2018-08-29 17:01:32 +030010import {ILevelNodeInfo} from "./models/basic.model.info";
Ittay Sternf7926712019-07-07 19:23:03 +030011import {ComponentInfoModel, ComponentInfoType} from "../component-info/component-info-model";
12import {ModelInformationItem} from "../../../shared/components/model-information/model-information.component";
Einat Vinouzee1f79742019-08-27 16:01:01 +030013import {undoUpgradeService, upgradeService} from "../../../shared/storeUtil/utils/service/service.actions";
14import {VNFMethods} from "../../../shared/storeUtil/utils/vnf/vnf.actions";
15import {FeatureFlagsService, Features} from "../../../shared/services/featureFlag/feature-flags.service";
Yoav Schneiderman1f2a2942019-12-09 16:42:21 +020016import {Utils} from "../../../shared/utils/utils";
Eylon Malin0a268262019-12-15 10:03:03 +020017import {Constants} from "../../../shared/utils/constants";
Ittay Stern18c3ce82019-12-26 15:21:17 +020018import {NodeInstance} from "../../../shared/models/nodeInstance";
Ittay Stern6f900cc2018-08-29 17:01:32 +030019
20@Injectable()
21export class SharedTreeService {
Ittay Stern6f900cc2018-08-29 17:01:32 +030022 constructor(private _store: NgRedux<AppState>) {
23 }
24
25 /***********************************************************
26 * return if instance has missing data
27 * @param instance - vnf instance
28 * @param dynamicInputs - from the instance
29 * @param isEcompGeneratedNaming
30 ************************************************************/
31 selectedVNF: string = null;
32
33
34 getSelectedVNF(): string {
35 return this.selectedVNF;
36 }
37
38 setSelectedVNF(node): void {
39 if (_.isNil(node) || node.data.type !== 'VF') {
40 this.selectedVNF = null;
41 } else {
42 this.selectedVNF = node.data.vnfStoreKey;
43 }
44 }
45
Ittay Stern18c3ce82019-12-26 15:21:17 +020046 /**
47 * Determines a consistent unique ID for a given right-tree
48 * node instance.
49 */
50 modelUniqueId = (nodeInstance: NodeInstance): string => {
51 return _.isNil(nodeInstance.modelInfo)
52 ? null
53 : (nodeInstance.modelInfo.modelCustomizationId || nodeInstance.modelInfo.modelInvariantId);
54 };
55
Ittay Sternf84164f2020-02-04 14:13:24 +020056 modelUniqueNameOrId = (instance): string => {
57 if (_.isNil(instance)) {
58 return null;
59 }
60
61 const innerInstance = _.find(instance) || {};
62
63 return instance.originalName
64 || this.modelUniqueId(instance)
65 || innerInstance.originalName
66 || this.modelUniqueId(innerInstance);
67 };
Ittay Stern74e62712020-01-29 18:20:23 +020068
Ittay Stern6b1e53d2020-01-21 12:20:37 +020069 /**
70 * Finds a model inside a full service model
71 * @param serviceModelFromHierarchy
72 * @param modelTypeName "vnfs" | "networks" | "vfModules" | "collectionResources" | ...
Ittay Stern74e62712020-01-29 18:20:23 +020073 * @param modelUniqueNameOrId Either an entry name (i.e. "originalName"), modelCustomizationId or modelInvariantId.
Ittay Stern6b1e53d2020-01-21 12:20:37 +020074 * Note that modelInvariantId will work only where model lacks a modelCustomizationId.
Ittay Stern08142382020-02-02 18:09:40 +020075 * @param modelName An optional entry name (i.e. "originalName"); will not try to use as id
Ittay Stern6b1e53d2020-01-21 12:20:37 +020076 */
Ittay Stern08142382020-02-02 18:09:40 +020077 modelByIdentifiers = (serviceModelFromHierarchy, modelTypeName: string, modelUniqueNameOrId: string, modelName?: string): any => {
Ittay Stern74e62712020-01-29 18:20:23 +020078 const logErrorAndReturnUndefined = () =>
79 console.info(`modelByIdentifiers: could not find a model matching query`, {
Ittay Stern08142382020-02-02 18:09:40 +020080 modelTypeName, modelUniqueNameOrId, modelName, serviceModelFromHierarchy
Ittay Stern74e62712020-01-29 18:20:23 +020081 });
82
83 if (_.isNil(serviceModelFromHierarchy)) return logErrorAndReturnUndefined();
Ittay Stern6b1e53d2020-01-21 12:20:37 +020084
85 const modelsOfType = serviceModelFromHierarchy[modelTypeName];
Ittay Stern74e62712020-01-29 18:20:23 +020086 if (_.isNil(modelsOfType)) return logErrorAndReturnUndefined();
Ittay Stern6b1e53d2020-01-21 12:20:37 +020087
Ittay Stern74e62712020-01-29 18:20:23 +020088 const modelIfModelIdentifierIsEntryName = modelsOfType[modelUniqueNameOrId];
Ittay Stern08142382020-02-02 18:09:40 +020089 const modelIfModeNameExists = _.isNil(modelName) ? null : modelsOfType[modelName];
Ittay Stern74e62712020-01-29 18:20:23 +020090
91 if (!_.isNil(modelIfModelIdentifierIsEntryName)) {
92 return modelIfModelIdentifierIsEntryName;
93 } else if (!_.isNil(modelIfModeNameExists)) {
94 return modelIfModeNameExists;
95 } else {
96 // try modelUniqueNameOrId as an id
97 return _.find(modelsOfType, o => (o.customizationUuid || o.invariantUuid) === modelUniqueNameOrId) || logErrorAndReturnUndefined()
98 }
Ittay Stern6b1e53d2020-01-21 12:20:37 +020099 };
100
Ittay Stern6f900cc2018-08-29 17:01:32 +0300101 hasMissingData(instance, dynamicInputs: any, isEcompGeneratedNaming: boolean, requiredFields: string[]): boolean {
102 if (!isEcompGeneratedNaming && _.isEmpty(instance.instanceName)) {
103 return true;
104 }
105
106 for (let field of requiredFields) {
107 if (_.isEmpty(instance[field])) {
108 return true;
109 }
110 }
111
112 for (let field of dynamicInputs) {
113 if (field.isRequired && !_.isNil(instance.instanceParams) && _.isEmpty(instance.instanceParams[0][field.id])) {
114 return true;
115 }
116 }
117 return false;
118 }
119
120
121 addingStatusProperty(node) {
122 node['statusProperties'] = [];
123 node['statusProperties'].push({key: 'Prov Status:', value: node.provStatus, testId: 'provStatus'});
124 node['statusProperties'].push({key: 'Orch Status:', value: node.orchStatus, testId: 'orchStatus'});
125 if (node.inMaint) {
126 node['statusProperties'].push({key: 'In-maintenance', value: '', testId: 'inMaint'});
127 }
128 return node;
129 }
130
131 /**********************************************
132 * should delete or remove child instance's
133 "new" -> should remove
134 !new" -> should change action status
135 **********************************************/
136 removeDeleteAllChild(node, serviceModelId: string, callback): void {
137 for (let nodeChild of node.children) {
138 if (nodeChild.data.action === ServiceInstanceActions.Create) {
139 if (!_.isNil(nodeChild.data) && !_.isNil(nodeChild.data.menuActions) && !_.isNil(nodeChild.data.menuActions['remove'])) {
140 nodeChild.data.menuActions['remove']['method'](nodeChild, serviceModelId);
141 }
142 } else {
143 if (!_.isNil(nodeChild.data) && !_.isNil(nodeChild.data.menuActions) && !_.isNil(nodeChild.data.menuActions['delete'])) {
144 nodeChild.data.menuActions['delete']['method'](nodeChild, serviceModelId);
145 }
146 }
147 }
148 callback(node, serviceModelId);
149 }
150
151
152 /**********************************************
153 * should undo delete child instance's
154 **********************************************/
155 undoDeleteAllChild(node, serviceModelId: string, callback): void {
156 for (let nodeChild of node.children) {
157 if (!_.isNil(nodeChild.data) && !_.isNil(nodeChild.data.menuActions) && !_.isNil(nodeChild.data.menuActions['undoDelete'])) {
158 nodeChild.data.menuActions['undoDelete']['method'](nodeChild, serviceModelId);
159 }
160 }
161 callback(node, serviceModelId);
162 }
163
164 /**********************************************
165 * should return true if can delete
166 **********************************************/
Eylon Malinaefc6a02020-01-29 15:11:47 +0200167 shouldShowDelete(node, serviceModelId): boolean {
168 return this.shouldShowButtonGeneric(node, "delete", serviceModelId)
Ittay Stern6f900cc2018-08-29 17:01:32 +0300169 }
170
171 /**********************************************
172 * should return true if can undo delete
173 **********************************************/
174 shouldShowUndoDelete(node): boolean {
175 const mode = this._store.getState().global.drawingBoardStatus;
176 if (mode === DrawingBoardModes.EDIT && !_.isNil(node.data.action) && !_.isNil(node.data.menuActions['undoDelete'])) {
177 if (node.data.action === ServiceInstanceActions.Create || node.data.action === ServiceInstanceActions.Delete) {
178 return false;
179 } else if (node.data.action.split('_').pop() === 'Delete') {
180 return true
181 }
182 return false;
183 }
184 return false;
185 }
186 /**********************************************
187 * should return true if can remove or edit
188 * enabled only on edit/design mode and for new instances
189 **********************************************/
190 shouldShowRemoveAndEdit(node): boolean {
191 const mode = this._store.getState().global.drawingBoardStatus;
192 if (!_.isNil(node) && !_.isNil(node.data) && !_.isNil(node.data.action) && node.data.action === ServiceInstanceActions.Create &&
193 mode !== DrawingBoardModes.VIEW && mode !== DrawingBoardModes.RETRY) {
194 return true;
195 }
196 return false;
197 }
198 /**********************************************
Einat Vinouzee1f79742019-08-27 16:01:01 +0300199 * enabled only on edit/design
200 * enabled only if there's a newer version for VNF-M
201 **********************************************/
202 upgradeBottomUp(node,serviceModelId: string): void {
203 this.iterateOverTreeBranchAndRunAction(node, serviceModelId, VNFMethods.UPGRADE);
204 this._store.dispatch(upgradeService(serviceModelId));
205 }
206
207 private iterateOverTreeBranchAndRunAction(node, serviceModelId: string, actionMethod) {
208 while (_.has(node.parent, 'data') && _.has(node.parent.data, 'menuActions')
209 && !_.isNil(node.parent.data.menuActions[actionMethod])) {
210 node = node.parent;
211 node.data.menuActions[actionMethod]['method'](node, serviceModelId);
212 }
213 }
214
215 /****************************************************
216 * should return true if customer can upgrade a VFM *
217 ****************************************************/
218 shouldShowUpgrade(node, serviceModelId): boolean {
219 if (FeatureFlagsService.getFlagState(Features.FLAG_FLASH_REPLACE_VF_MODULE, this._store) &&
Ittay Sternf84164f2020-02-04 14:13:24 +0200220 (this.isThereAnUpdatedLatestVersion(serviceModelId)) || this.isVfmoduleAlmostPartOfModelOnlyCustomizationUuidDiffer(node, serviceModelId)) {
Eylon Malinaefc6a02020-01-29 15:11:47 +0200221 return this.shouldShowButtonGeneric(node, VNFMethods.UPGRADE, serviceModelId);
Einat Vinouzee1f79742019-08-27 16:01:01 +0300222 }
223 else {
224 return false
225 }
226 }
227
Yoav Schneidermana3e54c32020-02-02 20:00:00 +0200228
Ittay Sternf84164f2020-02-04 14:13:24 +0200229 isVfmoduleAlmostPartOfModelOnlyCustomizationUuidDiffer(vfModuleNode, serviceModelId) : boolean {
230 /*
231 for `true`, should all:
232 1. parent vnf found by model-mane
233 2. vfmodule found by invariant
234 3. vfmodule diff by customization
235 */
236
237 if (_.isNil(vfModuleNode.data)) {
238 return false;
Yoav Schneidermana3e54c32020-02-02 20:00:00 +0200239 }
Ittay Sternf84164f2020-02-04 14:13:24 +0200240
241 const vnfHierarchy = this.getParentVnfHierarchy(vfModuleNode, serviceModelId);
242 if (_.isNil(vnfHierarchy)) {
243 return false;
244 }
245
246 const vfModuleHierarchyByInvariantId = this.getVfModuleHFromVnfHierarchyByInvariantId(vfModuleNode, vnfHierarchy);
247 if(_.isNil(vfModuleHierarchyByInvariantId)){
248 return false;
249 }
250
251 return vfModuleHierarchyByInvariantId.customizationUuid
252 && (vfModuleHierarchyByInvariantId.customizationUuid !== vfModuleNode.data.modelCustomizationId);
Yoav Schneidermana3e54c32020-02-02 20:00:00 +0200253 }
254
Ittay Sternf84164f2020-02-04 14:13:24 +0200255 getParentVnfHierarchy(vfModuleNode, serviceModelId) {
256 if (vfModuleNode.parent && vfModuleNode.parent.data) {
257 return this._store.getState().service.serviceHierarchy[serviceModelId].vnfs[vfModuleNode.parent.data.modelName];
258 } else {
259 return null;
260 }
261 }
262
263 getVfModuleHFromVnfHierarchyByInvariantId(vfModuleNode, parentVnfHierarchy) {
264 if(vfModuleNode.data.modelInvariantId && parentVnfHierarchy && parentVnfHierarchy.vfModules){
265 return _.find(parentVnfHierarchy.vfModules, o => o.invariantUuid === vfModuleNode.data.modelInvariantId);
Yoav Schneidermana3e54c32020-02-02 20:00:00 +0200266 }
267 return null;
268 }
269
270
271 isThereAnUpdatedLatestVersion(serviceModelId) : boolean{
Eylon Malinaefc6a02020-01-29 15:11:47 +0200272 let serviceInstance = this.getServiceInstance(serviceModelId);
Einat Vinouzee1f79742019-08-27 16:01:01 +0300273 return !_.isNil(serviceInstance.latestAvailableVersion) && (Number(serviceInstance.modelInfo.modelVersion) < serviceInstance.latestAvailableVersion);
274 }
275
Eylon Malinaefc6a02020-01-29 15:11:47 +0200276 private getServiceInstance(serviceModelId): any {
277 return this._store.getState().service.serviceInstance[serviceModelId];
278 }
279
280 shouldShowButtonGeneric(node, method, serviceModelId) {
Einat Vinouzee1f79742019-08-27 16:01:01 +0300281 const mode = this._store.getState().global.drawingBoardStatus;
Eylon Malinaefc6a02020-01-29 15:11:47 +0200282 const isMacro = !(this.getServiceInstance(serviceModelId).isALaCarte);
283
284 if (isMacro) { //if macro action allowed only for service level
285 return false;
286 }
287
Einat Vinouzee1f79742019-08-27 16:01:01 +0300288 if (!_.isNil(node) && !_.isNil(node.data) && !_.isNil(node.data.action) && !_.isNil(node.data.menuActions[method])) {
289 if (mode !== DrawingBoardModes.EDIT || node.data.action === ServiceInstanceActions.Create) {
290 return false;
291 }
292 else if (node.data.action === ServiceInstanceActions.None) {
293 return true
294 }
295 }
296 return false;
297 }
298
299 /**********************************************
300 * return boolean according to
301 * current defined action of VFModule node
302 **********************************************/
303 shouldShowUndoUpgrade(node): boolean {
304 const mode = this._store.getState().global.drawingBoardStatus;
305 if (mode === DrawingBoardModes.EDIT && !_.isNil(node.data.action) && !_.isNil(node.data.menuActions[VNFMethods.UNDO_UPGRADE])) {
306 if (node.data.action === ServiceInstanceActions.Upgrade) {
307 return false;
308 } else if (node.data.action.split('_').pop() === ServiceInstanceActions.Upgrade) {
309 return true
310 }
311 return false;
312 }
313 return false;
314 }
315 /**********************************************
316 * enabled only on edit/design
317 * enabled only if there's a newer version for VNF-M
318 **********************************************/
319 undoUpgradeBottomUp(node,serviceModelId: string): void {
320 this.iterateOverTreeBranchAndRunAction(node, serviceModelId, VNFMethods.UNDO_UPGRADE);
321 this._store.dispatch(undoUpgradeService(serviceModelId));
322 }
323 /**********************************************
Ittay Stern6f900cc2018-08-29 17:01:32 +0300324 * should return true if can duplicate by mode
325 **********************************************/
326 shouldShowDuplicate(node): boolean {
327 const mode = this._store.getState().global.drawingBoardStatus;
328 return !mode.includes('RETRY');
329 }
330
331 /**********************************************
332 * should return true if can audit info
333 **********************************************/
334 shouldShowAuditInfo(node): boolean {
335 return this.isRetryMode() || (!_.isNil(node.data) && !_.isNil(node.data.action) && node.data.action !== ServiceInstanceActions.Create);
336 }
337
338
339 isRetryMode(): boolean {
340 const mode = this._store.getState().global.drawingBoardStatus;
341 return mode.includes('RETRY');
342 }
343
344
345 /**********************************************
346 * should return true if can add node instances
347 **********************************************/
348 shouldShowAddIcon(): boolean{
349 const mode = this._store.getState().global.drawingBoardStatus;
Ittay Stern18c3ce82019-12-26 15:21:17 +0200350 return mode === DrawingBoardModes.EDIT || mode=== DrawingBoardModes.CREATE || mode=== DrawingBoardModes.RECREATE;
Ittay Stern6f900cc2018-08-29 17:01:32 +0300351 }
Yoav Schneiderman1f2a2942019-12-09 16:42:21 +0200352
353
354 isReachedToMaxInstances(properties, counter, flags): boolean{
355 let maxInstances = Utils.getMaxFirstLevel(properties, flags);
356 if(_.isNil(maxInstances)){
357 return false;
358 }else {
359 return !(maxInstances > counter);
360 }
361 }
Ittay Stern6f900cc2018-08-29 17:01:32 +0300362 /************************************************
363 return number of instances with action Delete
364 @type: vnfs networks, vngGroups (not vfModule)
365 @node : node model from the left tree
366 ************************************************/
367 getExistingInstancesWithDeleteMode(node, serviceModelId: string, type: string): number {
368 let counter = 0;
Eylon Malinaefc6a02020-01-29 15:11:47 +0200369 const existingInstances = this.getServiceInstance(serviceModelId)[type];
Ittay Stern6f900cc2018-08-29 17:01:32 +0300370 const modelUniqueId = node.data.modelUniqueId;
371 if (!_.isNil(existingInstances)) {
372 for (let instanceKey in existingInstances) {
373 if (!_.isNil(existingInstances[instanceKey].action)) {
374 if (existingInstances[instanceKey].modelInfo.modelUniqueId === modelUniqueId && existingInstances[instanceKey].action.split('_').pop() === 'Delete') {
375 counter++;
376 }
377 }
378 }
379 }
380 return counter;
381 }
382
383
384 isServiceOnDeleteMode(serviceId: string): boolean {
385 return this._store.getState().service.serviceInstance[serviceId].action === ServiceInstanceActions.Delete;
386 }
387
388
389 openModal(node : any | any[] , serviceModelId : string, cb : Function) : void {
390 let type: string = _.isArray(node) ? 'Service' : node.data.typeName;
391 let messageBoxData: MessageBoxData = new MessageBoxData(
392 "Mark for Delete",
393 `You are about to mark for delete this ${type} this will also mark all its children and remove all new instances just added`,
394 <any>"warning",
395 <any>"md",
396 [
397 {
398 text: "Mark and remove",
399 size: "large",
400 callback: cb.bind(this, node, serviceModelId),
401 closeModal: true
402 },
403 {text: "Don’t Remove", size: "medium", closeModal: true}
404 ]);
405
406 MessageBoxService.openModal.next(messageBoxData);
407 }
408
409 someChildHasCreateAction(nodes: any | any[]) : boolean {
410 let nodesArr = _.isArray(nodes) ? nodes : [nodes];
411 for(const node of nodesArr){
412 if(node.action === ServiceInstanceActions.Create) {return true;}
413 if(node.children){
414 for (let nodeChild of node.children) {
415 if (nodeChild.action === ServiceInstanceActions.Create) {
416 return true;
417 }
418 if(nodeChild.children && nodeChild.children.length > 0){
419 for(let child of nodeChild.children){
420 let hasCreateAction = this.someChildHasCreateAction(child);
421 if(hasCreateAction) {
422 return true;
423 }
424 }
425 }
426 }
427 }
428 }
429 return false;
430 }
431
432 shouldShowDeleteInstanceWithChildrenModal(node : any | any[] , serviceModelId : string, cb : Function) : void {
433 if(this.someChildHasCreateAction(node)){
434 this.openModal(node , serviceModelId, cb);
435 }else {
436 cb(node, serviceModelId)
437 }
438 }
439
440
441 isFailed(node): boolean {
442 return !_.isNil(node.data) ? node.data.isFailed : false;
443 }
444
445 /************************************************
446 in a case the node is failed e.g. not instantiated correctly
447 the function will call to openRetryInstanceAuditInfoModal
448 @node : node model from the left tree
449 @serviceModelId : serviceModelId
450 @instance : instance
451 @instanceType: instanceType
452 @modelInfoService : the model (vnf, vfmodule, network, vnfgroup)object that call to the function (this)
453 ************************************************/
454 openAuditInfoModal(node, serviceModelId, instance, instanceType, modelInfoService : ILevelNodeInfo){
Ittay Sternf7926712019-07-07 19:23:03 +0300455 AuditInfoModalComponent.openInstanceAuditInfoModal.next({
456 instanceId: serviceModelId,
457 type: instanceType,
Ittay Stern08142382020-02-02 18:09:40 +0200458 model: modelInfoService.getModel(
459 this.modelByIdentifiers(
460 this._store.getState().service.serviceHierarchy[serviceModelId],
461 modelInfoService.name,
462 this.modelUniqueNameOrId(instance), node.data.modelName
463 )
464 ),
Ittay Sternf7926712019-07-07 19:23:03 +0300465 instance
466 });
467 }
468
469
470 addGeneralInfoItems(modelInfoSpecificItems: ModelInformationItem[], type: ComponentInfoType, model, instance):ComponentInfoModel {
471 let modelInfoItems: ModelInformationItem[] = [
472 ModelInformationItem.createInstance("Model version", model ? model.version : null),
473 ModelInformationItem.createInstance("Model customization ID", model ? model.customizationUuid : null),
474 ModelInformationItem.createInstance("Instance ID", instance ? instance.instanceId : null),
475 ModelInformationItem.createInstance("Instance type", instance ? instance.instanceType : null),
476 ModelInformationItem.createInstance("In maintenance", instance? instance.inMaint : null),
477 ];
478 modelInfoItems = modelInfoItems.concat(modelInfoSpecificItems);
479 return this.getComponentInfoModelByModelInformationItems(modelInfoItems, type, instance);
480 }
481
482 getComponentInfoModelByModelInformationItems(modelInfoItems: ModelInformationItem[], type: ComponentInfoType, instance){
483 const modelInfoItemsWithoutEmpty = _.filter(modelInfoItems, function(item){ return !item.values.every(_.isNil)});
484 return new ComponentInfoModel(type, modelInfoItemsWithoutEmpty, [], instance != null);
485 }
Eylon Malin0a268262019-12-15 10:03:03 +0200486
487 createMaximumToInstantiateModelInformationItem(model): ModelInformationItem {
488 return ModelInformationItem.createInstance(
489 "Max instances",
490 !_.isNil(model.max) ? String(model.max) : Constants.ModelInfo.UNLIMITED_DEFAULT
491 );
492 }
Ittay Stern6f900cc2018-08-29 17:01:32 +0300493}