1 import {Injectable} from "@angular/core";
2 import {NgRedux} from "@angular-redux/store";
3 import {AppState} from "../../../shared/store/reducers";
4 import {PauseStatus, 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 {ILevelNodeInfo} from "./models/basic.model.info";
11 import {ComponentInfoModel, ComponentInfoType} from "../component-info/component-info-model";
12 import {ModelInformationItem} from "../../../shared/components/model-information/model-information.component";
13 import {undoUpgradeService, upgradeService} from "../../../shared/storeUtil/utils/service/service.actions";
14 import {VNFMethods} from "../../../shared/storeUtil/utils/vnf/vnf.actions";
15 import {FeatureFlagsService, Features} from "../../../shared/services/featureFlag/feature-flags.service";
16 import {Utils} from "../../../shared/utils/utils";
17 import {Constants} from "../../../shared/utils/constants";
18 import {NodeInstance} from "../../../shared/models/nodeInstance";
21 export class SharedTreeService {
22 constructor(private _store: NgRedux<AppState>) {
25 /***********************************************************
26 * return if instance has missing data
27 * @param instance - vnf instance
28 * @param dynamicInputs - from the instance
29 * @param isEcompGeneratedNaming
30 ************************************************************/
31 selectedNF: string = null;
33 getSelectedNF(): string {
34 return this.selectedNF;
37 setSelectedNF(node): void {
38 if (_.isNil(node) || node.data.type !== 'VF') {
39 this.selectedNF = null;
40 } else if (node.data.type === 'VF'){
41 this.selectedNF = node.data.vnfStoreKey;
42 } else if (node.data.type === 'PNF'){
43 this.selectedNF = node.data.pnfStoreKey;
49 * Determines a consistent unique ID for a given right-tree
52 modelUniqueId = (nodeInstance: NodeInstance): string => {
53 return _.isNil(nodeInstance.modelInfo)
55 : (nodeInstance.modelInfo.modelCustomizationId || nodeInstance.modelInfo.modelInvariantId);
58 modelUniqueNameOrId = (instance): string => {
59 if (_.isNil(instance)) {
63 const innerInstance = _.find(instance) || {};
65 return instance.originalName
66 || this.modelUniqueId(instance)
67 || innerInstance.originalName
68 || this.modelUniqueId(innerInstance);
72 * Finds a model inside a full service model
73 * @param serviceModelFromHierarchy
74 * @param modelTypeName "vnfs" | "networks" | "vfModules" | "collectionResources" | ...
75 * @param modelUniqueNameOrId Either an entry name (i.e. "originalName"), modelCustomizationId or modelInvariantId.
76 * Note that modelInvariantId will work only where model lacks a modelCustomizationId.
77 * @param modelName An optional entry name (i.e. "originalName"); will not try to use as id
79 modelByIdentifiers = (serviceModelFromHierarchy, modelTypeName: string, modelUniqueNameOrId: string, modelName?: string): any => {
80 const logErrorAndReturnUndefined = () =>
81 console.info(`modelByIdentifiers: could not find a model matching query`, {
82 modelTypeName, modelUniqueNameOrId, modelName, serviceModelFromHierarchy
85 if (_.isNil(serviceModelFromHierarchy)) return logErrorAndReturnUndefined();
87 const modelsOfType = serviceModelFromHierarchy[modelTypeName];
88 if (_.isNil(modelsOfType)) return logErrorAndReturnUndefined();
90 const modelIfModelIdentifierIsEntryName = modelsOfType[modelUniqueNameOrId];
91 const modelIfModeNameExists = _.isNil(modelName) ? null : modelsOfType[modelName];
93 if (!_.isNil(modelIfModelIdentifierIsEntryName)) {
94 return modelIfModelIdentifierIsEntryName;
95 } else if (!_.isNil(modelIfModeNameExists)) {
96 return modelIfModeNameExists;
98 // try modelUniqueNameOrId as an id
99 return _.find(modelsOfType, o => (o.customizationUuid || o.invariantUuid) === modelUniqueNameOrId) || logErrorAndReturnUndefined()
103 hasMissingData(instance, dynamicInputs: any, isEcompGeneratedNaming: boolean, requiredFields: string[]): boolean {
104 if (!isEcompGeneratedNaming && _.isEmpty(instance.instanceName)) {
108 for (let field of requiredFields) {
109 if (_.isEmpty(instance[field])) {
114 for (let field of dynamicInputs) {
115 if (field.isRequired && !_.isNil(instance.instanceParams) && _.isEmpty(instance.instanceParams[0][field.id])) {
123 addingStatusProperty(node) {
124 node['statusProperties'] = [];
125 node['statusProperties'].push({key: 'Prov Status: ', value: node.provStatus, testId: 'provStatus'});
126 node['statusProperties'].push({key: 'Orch Status: ', value: node.orchStatus, testId: 'orchStatus'});
127 if(node.type === 'VFmodule') {
128 node['statusProperties'].push({key: 'Model Version: ', value: this.getNodeModelVersion(node), testId: 'modelVersion'});
131 node['statusProperties'].push({key: 'In-maintenance', value: '', testId: 'inMaint'});
136 getNodeModelVersion(node): string | undefined {
137 if(!_.isNil(node.instanceModelInfo) && !_.isNil(node.instanceModelInfo.modelVersion)){
138 return node.instanceModelInfo.modelVersion;
143 /**********************************************
144 * should delete or remove child instance's
145 "new" -> should remove
146 !new" -> should change action status
147 **********************************************/
148 removeDeleteAllChild(node, serviceModelId: string, callback): void {
149 for (let nodeChild of node.children) {
150 if (nodeChild.data.action === ServiceInstanceActions.Create) {
151 if (!_.isNil(nodeChild.data) && !_.isNil(nodeChild.data.menuActions) && !_.isNil(nodeChild.data.menuActions['remove'])) {
152 nodeChild.data.menuActions['remove']['method'](nodeChild, serviceModelId);
155 if (!_.isNil(nodeChild.data) && !_.isNil(nodeChild.data.menuActions) && !_.isNil(nodeChild.data.menuActions['delete'])) {
156 nodeChild.data.menuActions['delete']['method'](nodeChild, serviceModelId);
160 callback(node, serviceModelId);
164 /**********************************************
165 * should undo delete child instance's
166 **********************************************/
167 undoDeleteAllChild(node, serviceModelId: string, callback): void {
168 for (let nodeChild of node.children) {
169 if (!_.isNil(nodeChild.data) && !_.isNil(nodeChild.data.menuActions) && !_.isNil(nodeChild.data.menuActions['undoDelete'])) {
170 nodeChild.data.menuActions['undoDelete']['method'](nodeChild, serviceModelId);
173 callback(node, serviceModelId);
176 /**********************************************
177 * should return true if can delete
178 **********************************************/
179 shouldShowDelete(node, serviceModelId): boolean {
180 return this.shouldShowButtonGeneric(node, "delete", serviceModelId)
183 /**********************************************
184 * should return true if can undo delete
185 **********************************************/
186 shouldShowUndoDelete(node): boolean {
187 const mode = this._store.getState().global.drawingBoardStatus;
188 if (mode === DrawingBoardModes.EDIT && !_.isNil(node.data.action) && !_.isNil(node.data.menuActions['undoDelete'])) {
189 if (node.data.action === ServiceInstanceActions.Create || node.data.action === ServiceInstanceActions.Delete) {
191 } else if (node.data.action.split('_').pop() === 'Delete') {
198 /**********************************************
199 * should return true if can remove or edit
200 * enabled only on edit/design mode and for new instances
201 **********************************************/
202 shouldShowRemoveAndEdit(node): boolean {
203 const mode = this._store.getState().global.drawingBoardStatus;
204 if (!_.isNil(node) && !_.isNil(node.data) && !_.isNil(node.data.action) && node.data.action === ServiceInstanceActions.Create &&
205 mode !== DrawingBoardModes.VIEW && mode !== DrawingBoardModes.RETRY) {
210 /**********************************************
211 * enabled only on edit/design
212 * enabled only if there's a newer version for VNF-M
213 **********************************************/
214 upgradeBottomUp(node,serviceModelId: string): void {
215 this.iterateOverTreeBranchAndRunAction(node, serviceModelId, VNFMethods.UPGRADE);
216 this._store.dispatch(upgradeService(serviceModelId));
219 private iterateOverTreeBranchAndRunAction(node, serviceModelId: string, actionMethod) {
220 while (_.has(node.parent, 'data') && _.has(node.parent.data, 'menuActions')
221 && !_.isNil(node.parent.data.menuActions[actionMethod])) {
223 node.data.menuActions[actionMethod]['method'](node, serviceModelId);
227 shouldShowRemovePause(node) : boolean {
228 if(FeatureFlagsService.getFlagState(Features.FLAG_2008_REMOVE_PAUSE_INSTANTIATION, this._store)){
229 return node.pauseInstantiation === PauseStatus.AFTER_COMPLETION;
234 showPauseWithOrchStatus(node): boolean {
235 if(node.orchStatus == "Active"){
241 shouldShowPauseInstantiation(node): boolean {
242 if(FeatureFlagsService.getFlagState(Features.FLAG_2008_REMOVE_PAUSE_INSTANTIATION, this._store)){
243 return (FeatureFlagsService.getFlagState(Features.FLAG_2006_PAUSE_VFMODULE_INSTANTIATION_CREATION, this._store) && node.pauseInstantiation == null);
245 return (FeatureFlagsService.getFlagState(Features.FLAG_2006_PAUSE_VFMODULE_INSTANTIATION_CREATION, this._store));
247 /****************************************************
248 * should return true if customer can upgrade a VFM *
249 ****************************************************/
250 shouldShowUpgrade(node, serviceModelId): boolean {
251 return (this.isVfMoudleCouldBeUpgraded(node, serviceModelId))
252 && this.shouldShowButtonGeneric(node, VNFMethods.UPGRADE, serviceModelId) ;
255 isVfMoudleCouldBeUpgraded(node, serviceModelId): boolean{
256 return (FeatureFlagsService.getFlagState(Features.FLAG_FLASH_REPLACE_VF_MODULE, this._store) &&
257 (this.isThereAnUpdatedLatestVersion(serviceModelId) || this.isVfModuleCustomizationIdNotExistsOnModel(node, serviceModelId)))
260 isVfModuleCustomizationIdNotExistsOnModel(vfModuleNode, serviceModelId) {
263 if (_.isNil(vfModuleNode.data) || _.isNil(vfModuleNode.data.modelCustomizationId)) {
267 let vfModulesHierarchyByGivenModelId = this._store.getState().service.serviceHierarchy[serviceModelId].vfModules;
268 return !_.some(vfModulesHierarchyByGivenModelId, vfmodel => vfmodel.customizationUuid === vfModuleNode.data.modelCustomizationId);
272 isVfmoduleAlmostPartOfModelOnlyCustomizationUuidDiffer(vfModuleNode, serviceModelId) : boolean {
274 for `true`, should all:
275 1. parent vnf found by model-mane
276 2. vfmodule found by invariant
277 3. vfmodule diff by customization
280 if (_.isNil(vfModuleNode.data)) {
284 const vnfHierarchy = this.getParentVnfHierarchy(vfModuleNode, serviceModelId);
285 if (_.isNil(vnfHierarchy)) {
289 const vfModuleHierarchyByInvariantId = this.getVfModuleHFromVnfHierarchyByInvariantId(vfModuleNode, vnfHierarchy);
290 if(_.isNil(vfModuleHierarchyByInvariantId)){
294 return vfModuleHierarchyByInvariantId.customizationUuid
295 && (vfModuleHierarchyByInvariantId.customizationUuid !== vfModuleNode.data.modelCustomizationId);
298 getParentVnfHierarchy(vfModuleNode, serviceModelId) {
299 if (vfModuleNode.parent && vfModuleNode.parent.data) {
300 return this._store.getState().service.serviceHierarchy[serviceModelId].vnfs[vfModuleNode.parent.data.modelName];
306 getVfModuleHFromVnfHierarchyByInvariantId(vfModuleNode, parentVnfHierarchy) {
307 if(vfModuleNode.data.modelInvariantId && parentVnfHierarchy && parentVnfHierarchy.vfModules){
308 return _.find(parentVnfHierarchy.vfModules, o => o.invariantUuid === vfModuleNode.data.modelInvariantId);
314 isThereAnUpdatedLatestVersion(serviceModelId) : boolean{
315 let serviceInstance = this.getServiceInstance(serviceModelId);
316 return !_.isNil(serviceInstance.latestAvailableVersion) && (Number(serviceInstance.modelInfo.modelVersion) < serviceInstance.latestAvailableVersion);
319 private getServiceInstance(serviceModelId): any {
320 return this._store.getState().service.serviceInstance[serviceModelId];
323 shouldShowButtonGeneric(node, method, serviceModelId) {
324 const mode = this._store.getState().global.drawingBoardStatus;
325 const isMacro = !(this.getServiceInstance(serviceModelId).isALaCarte);
327 if (isMacro) { //if macro action allowed only for service level
331 if (!_.isNil(node) && !_.isNil(node.data) && !_.isNil(node.data.action) && !_.isNil(node.data.menuActions[method])) {
332 if (mode !== DrawingBoardModes.EDIT || node.data.action === ServiceInstanceActions.Create) {
335 else if (node.data.action === ServiceInstanceActions.None) {
342 /**********************************************
343 * return boolean according to
344 * current defined action of VFModule node
345 **********************************************/
346 shouldShowUndoUpgrade(node): boolean {
347 const mode = this._store.getState().global.drawingBoardStatus;
348 if (mode === DrawingBoardModes.EDIT && !_.isNil(node.data.action) && !_.isNil(node.data.menuActions[VNFMethods.UNDO_UPGRADE])) {
349 if (node.data.action === ServiceInstanceActions.Upgrade) {
351 } else if (node.data.action.split('_').pop() === ServiceInstanceActions.Upgrade) {
358 /**********************************************
359 * enabled only on edit/design
360 * enabled only if there's a newer version for VNF-M
361 **********************************************/
362 undoUpgradeBottomUp(node,serviceModelId: string): void {
363 this.iterateOverTreeBranchAndRunAction(node, serviceModelId, VNFMethods.UNDO_UPGRADE);
364 this._store.dispatch(undoUpgradeService(serviceModelId));
366 /**********************************************
367 * should return true if can duplicate by mode
368 **********************************************/
369 shouldShowDuplicate(node): boolean {
370 const mode = this._store.getState().global.drawingBoardStatus;
371 return !mode.includes('RETRY');
374 /**********************************************
375 * should return true if can audit info
376 **********************************************/
377 shouldShowAuditInfo(node): boolean {
378 return this.isRetryMode() || (!_.isNil(node.data) && !_.isNil(node.data.action) && node.data.action !== ServiceInstanceActions.Create);
382 isRetryMode(): boolean {
383 const mode = this._store.getState().global.drawingBoardStatus;
384 return mode.includes('RETRY');
388 /**********************************************
389 * should return true if can add node instances
390 **********************************************/
391 shouldShowAddIcon(): boolean{
392 const mode = this._store.getState().global.drawingBoardStatus;
393 return mode === DrawingBoardModes.EDIT || mode=== DrawingBoardModes.CREATE || mode=== DrawingBoardModes.RECREATE;
397 isReachedToMaxInstances(properties, counter, flags): boolean{
398 let maxInstances = Utils.getMaxFirstLevel(properties, flags);
399 if(_.isNil(maxInstances)){
402 return !(maxInstances > counter);
405 /************************************************
406 return number of instances with action Delete
407 @type: vnfs networks, vngGroups (not vfModule)
408 @node : node model from the left tree
409 ************************************************/
410 getExistingInstancesWithDeleteMode(node, serviceModelId: string, type: string): number {
412 const existingInstances = this.getServiceInstance(serviceModelId)[type];
413 const modelUniqueId = node.data.modelUniqueId;
414 if (!_.isNil(existingInstances)) {
415 for (let instanceKey in existingInstances) {
416 if (!_.isNil(existingInstances[instanceKey].action)) {
417 if (existingInstances[instanceKey].modelInfo.modelUniqueId === modelUniqueId && existingInstances[instanceKey].action.split('_').pop() === 'Delete') {
427 isServiceOnDeleteMode(serviceId: string): boolean {
428 return this._store.getState().service.serviceInstance[serviceId].action === ServiceInstanceActions.Delete;
432 openModal(node : any | any[] , serviceModelId : string, cb : Function) : void {
433 let type: string = _.isArray(node) ? 'Service' : node.data.typeName;
434 let messageBoxData: MessageBoxData = new MessageBoxData(
436 `You are about to mark for delete this ${type} this will also mark all its children and remove all new instances just added`,
441 text: "Mark and remove",
443 callback: cb.bind(this, node, serviceModelId),
446 {text: "Don’t Remove", size: "medium", closeModal: true}
449 MessageBoxService.openModal.next(messageBoxData);
452 someChildHasCreateAction(nodes: any | any[]) : boolean {
453 let nodesArr = _.isArray(nodes) ? nodes : [nodes];
454 for(const node of nodesArr){
455 if(node.action === ServiceInstanceActions.Create) {return true;}
457 for (let nodeChild of node.children) {
458 if (nodeChild.action === ServiceInstanceActions.Create) {
461 if(nodeChild.children && nodeChild.children.length > 0){
462 for(let child of nodeChild.children){
463 let hasCreateAction = this.someChildHasCreateAction(child);
464 if(hasCreateAction) {
475 shouldShowDeleteInstanceWithChildrenModal(node : any | any[] , serviceModelId : string, cb : Function) : void {
476 if(this.someChildHasCreateAction(node)){
477 this.openModal(node , serviceModelId, cb);
479 cb(node, serviceModelId)
484 isFailed(node): boolean {
485 return !_.isNil(node.data) ? node.data.isFailed : false;
488 /************************************************
489 in a case the node is failed e.g. not instantiated correctly
490 the function will call to openRetryInstanceAuditInfoModal
491 @node : node model from the left tree
492 @serviceModelId : serviceModelId
494 @instanceType: instanceType
495 @modelInfoService : the model (vnf, vfmodule, network, vnfgroup)object that call to the function (this)
496 ************************************************/
497 openAuditInfoModal(node, serviceModelId, instance, instanceType, modelInfoService : ILevelNodeInfo){
498 AuditInfoModalComponent.openInstanceAuditInfoModal.next({
499 instanceId: serviceModelId,
501 model: modelInfoService.getModel(
502 this.modelByIdentifiers(
503 this._store.getState().service.serviceHierarchy[serviceModelId],
504 modelInfoService.name,
505 this.modelUniqueNameOrId(instance), node.data.modelName
512 getModelVersionEitherFromInstanceOrFromHierarchy(selectedNodeData, model): string | undefined {
513 return this.getNamedFieldFromInstanceOrFromHierarchy(selectedNodeData, "modelVersion", model, "version");
516 getModelCustomizationIdEitherFromInstanceOrFromHierarchy(selectedNodeData, model): string | undefined {
517 return this.getNamedFieldFromInstanceOrFromHierarchy(selectedNodeData, "modelCustomizationId", model, "customizationUuid");
520 getModelInvariantIdEitherFromInstanceOrFromHierarchy(selectedNodeData, model): string | undefined {
521 return this.getNamedFieldFromInstanceOrFromHierarchy(selectedNodeData, "modelInvariantId", model, "invariantUuid");
524 getModelVersionIdEitherFromInstanceOrFromHierarchy(selectedNodeData, model): string | undefined {
525 return this.getNamedFieldFromInstanceOrFromHierarchy (selectedNodeData, "modelVersionId", model, "uuid");
530 getNamedFieldFromInstanceOrFromHierarchy(selectedNodeData, instanceModelInfoFieldName, model, modelFieldName): string | undefined {
531 if (instanceModelInfoFieldName && selectedNodeData && selectedNodeData.instanceModelInfo
532 && selectedNodeData.instanceModelInfo[instanceModelInfoFieldName]) {
533 return selectedNodeData.instanceModelInfo[instanceModelInfoFieldName];
534 } else if (modelFieldName && model && model[modelFieldName]) {
535 return model[modelFieldName];
540 addGeneralInfoItems(modelInfoSpecificItems: ModelInformationItem[], type: ComponentInfoType, model, selectedNodeData):ComponentInfoModel {
541 let modelInfoItems: ModelInformationItem[] = [
542 ModelInformationItem.createInstance("Model version", this.getModelVersionEitherFromInstanceOrFromHierarchy(selectedNodeData, model)),
543 ModelInformationItem.createInstance("Model customization ID", this.getModelCustomizationIdEitherFromInstanceOrFromHierarchy(selectedNodeData, model)),
544 ModelInformationItem.createInstance("Instance ID", selectedNodeData ? selectedNodeData.instanceId : null),
545 ModelInformationItem.createInstance("Instance type", selectedNodeData ? selectedNodeData.instanceType : null),
546 ModelInformationItem.createInstance("In maintenance", selectedNodeData? selectedNodeData.inMaint : null),
548 modelInfoItems = modelInfoItems.concat(modelInfoSpecificItems);
549 return this.getComponentInfoModelByModelInformationItems(modelInfoItems, type, selectedNodeData);
552 getComponentInfoModelByModelInformationItems(modelInfoItems: ModelInformationItem[], type: ComponentInfoType, instance){
553 const modelInfoItemsWithoutEmpty = _.filter(modelInfoItems, function(item){ return !item.values.every(_.isNil)});
554 return new ComponentInfoModel(type, modelInfoItemsWithoutEmpty, [], instance != null);
557 createMaximumToInstantiateModelInformationItem(model): ModelInformationItem {
558 return ModelInformationItem.createInstance(
560 !_.isNil(model.max) ? String(model.max) : Constants.ModelInfo.UNLIMITED_DEFAULT
564 isAddPositionFlagTrue():boolean{
565 return FeatureFlagsService.getFlagState(Features.FLAG_2008_CREATE_VFMODULE_INSTANTIATION_ORDER_NUMBER, this._store);