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)) &&
258 (this.upgradeAllowedForBm(node,serviceModelId)))
263 isVfModuleCustomizationIdNotExistsOnModel(vfModuleNode, serviceModelId) {
266 if (_.isNil(vfModuleNode.data) || _.isNil(vfModuleNode.data.modelCustomizationId)) {
270 let vfModulesHierarchyByGivenModelId = this._store.getState().service.serviceHierarchy[serviceModelId].vfModules;
271 return !_.some(vfModulesHierarchyByGivenModelId, vfmodel => vfmodel.customizationUuid === vfModuleNode.data.modelCustomizationId);
275 isVfmoduleAlmostPartOfModelOnlyCustomizationUuidDiffer(vfModuleNode, serviceModelId) : boolean {
277 for `true`, should all:
278 1. parent vnf found by model-mane
279 2. vfmodule found by invariant
280 3. vfmodule diff by customization
283 if (_.isNil(vfModuleNode.data)) {
287 const vnfHierarchy = this.getParentVnfHierarchy(vfModuleNode, serviceModelId);
288 if (_.isNil(vnfHierarchy)) {
292 const vfModuleHierarchyByInvariantId = this.getVfModuleHFromVnfHierarchyByInvariantId(vfModuleNode, vnfHierarchy);
293 if(_.isNil(vfModuleHierarchyByInvariantId)){
297 return vfModuleHierarchyByInvariantId.customizationUuid
298 && (vfModuleHierarchyByInvariantId.customizationUuid !== vfModuleNode.data.modelCustomizationId);
301 getParentVnfHierarchy(vfModuleNode, serviceModelId) {
302 if (vfModuleNode.parent && vfModuleNode.parent.data) {
303 return this._store.getState().service.serviceHierarchy[serviceModelId].vnfs[vfModuleNode.parent.data.modelName];
309 getVfModuleHFromVnfHierarchyByInvariantId(vfModuleNode, parentVnfHierarchy) {
310 if(vfModuleNode.data.modelInvariantId && parentVnfHierarchy && parentVnfHierarchy.vfModules){
311 return _.find(parentVnfHierarchy.vfModules, o => o.invariantUuid === vfModuleNode.data.modelInvariantId);
317 isThereAnUpdatedLatestVersion(serviceModelId) : boolean{
318 let serviceInstance = this.getServiceInstance(serviceModelId);
319 return !_.isNil(serviceInstance.latestAvailableVersion) && (Number(serviceInstance.modelInfo.modelVersion) < serviceInstance.latestAvailableVersion);
322 private getServiceInstance(serviceModelId): any {
323 return this._store.getState().service.serviceInstance[serviceModelId];
326 shouldShowButtonGeneric(node, method, serviceModelId) {
327 const mode = this._store.getState().global.drawingBoardStatus;
328 const isMacro = !(this.getServiceInstance(serviceModelId).isALaCarte);
330 if (isMacro) { //if macro action allowed only for service level
334 if (!_.isNil(node) && !_.isNil(node.data) && !_.isNil(node.data.action) && !_.isNil(node.data.menuActions[method])) {
335 if (mode !== DrawingBoardModes.EDIT || node.data.action === ServiceInstanceActions.Create) {
338 else if (node.data.action === ServiceInstanceActions.None) {
345 /**********************************************
346 * return boolean according to
347 * current defined action of VFModule node
348 **********************************************/
349 shouldShowUndoUpgrade(node): boolean {
350 const mode = this._store.getState().global.drawingBoardStatus;
351 if (mode === DrawingBoardModes.EDIT && !_.isNil(node.data.action) && !_.isNil(node.data.menuActions[VNFMethods.UNDO_UPGRADE])) {
352 if (node.data.action === ServiceInstanceActions.Upgrade) {
354 } else if (node.data.action.split('_').pop() === ServiceInstanceActions.Upgrade) {
361 /**********************************************
362 * enabled only on edit/design
363 * enabled only if there's a newer version for VNF-M
364 **********************************************/
365 undoUpgradeBottomUp(node,serviceModelId: string): void {
366 this.iterateOverTreeBranchAndRunAction(node, serviceModelId, VNFMethods.UNDO_UPGRADE);
367 this._store.dispatch(undoUpgradeService(serviceModelId));
369 /**********************************************
370 * should return true if can duplicate by mode
371 **********************************************/
372 shouldShowDuplicate(node): boolean {
373 const mode = this._store.getState().global.drawingBoardStatus;
374 return !mode.includes('RETRY');
377 /**********************************************
378 * should return true if can audit info
379 **********************************************/
380 shouldShowAuditInfo(node): boolean {
381 return this.isRetryMode() || (!_.isNil(node.data) && !_.isNil(node.data.action) && node.data.action !== ServiceInstanceActions.Create);
385 isRetryMode(): boolean {
386 const mode = this._store.getState().global.drawingBoardStatus;
387 return mode.includes('RETRY');
391 /**********************************************
392 * should return true if can add node instances
393 **********************************************/
394 shouldShowAddIcon(): boolean{
395 const mode = this._store.getState().global.drawingBoardStatus;
396 return mode === DrawingBoardModes.EDIT || mode=== DrawingBoardModes.CREATE || mode=== DrawingBoardModes.RECREATE;
400 isReachedToMaxInstances(properties, counter, flags): boolean{
401 let maxInstances = Utils.getMaxFirstLevel(properties, flags);
402 if(_.isNil(maxInstances)){
405 return !(maxInstances > counter);
408 /************************************************
409 return number of instances with action Delete
410 @type: vnfs networks, vngGroups (not vfModule)
411 @node : node model from the left tree
412 ************************************************/
413 getExistingInstancesWithDeleteMode(node, serviceModelId: string, type: string): number {
415 const existingInstances = this.getServiceInstance(serviceModelId)[type];
416 const modelUniqueId = node.data.modelUniqueId;
417 if (!_.isNil(existingInstances)) {
418 for (let instanceKey in existingInstances) {
419 if (!_.isNil(existingInstances[instanceKey].action)) {
420 if (existingInstances[instanceKey].modelInfo.modelUniqueId === modelUniqueId && existingInstances[instanceKey].action.split('_').pop() === 'Delete') {
430 isServiceOnDeleteMode(serviceId: string): boolean {
431 return this._store.getState().service.serviceInstance[serviceId].action === ServiceInstanceActions.Delete;
435 openModal(node : any | any[] , serviceModelId : string, cb : Function) : void {
436 let type: string = _.isArray(node) ? 'Service' : node.data.typeName;
437 let messageBoxData: MessageBoxData = new MessageBoxData(
439 `You are about to mark for delete this ${type} this will also mark all its children and remove all new instances just added`,
444 text: "Mark and remove",
446 callback: cb.bind(this, node, serviceModelId),
449 {text: "Don’t Remove", size: "medium", closeModal: true}
452 MessageBoxService.openModal.next(messageBoxData);
455 someChildHasCreateAction(nodes: any | any[]) : boolean {
456 let nodesArr = _.isArray(nodes) ? nodes : [nodes];
457 for(const node of nodesArr){
458 if(node.action === ServiceInstanceActions.Create) {return true;}
460 for (let nodeChild of node.children) {
461 if (nodeChild.action === ServiceInstanceActions.Create) {
464 if(nodeChild.children && nodeChild.children.length > 0){
465 for(let child of nodeChild.children){
466 let hasCreateAction = this.someChildHasCreateAction(child);
467 if(hasCreateAction) {
478 shouldShowDeleteInstanceWithChildrenModal(node : any | any[] , serviceModelId : string, cb : Function) : void {
479 if(this.someChildHasCreateAction(node)){
480 this.openModal(node , serviceModelId, cb);
482 cb(node, serviceModelId)
487 isFailed(node): boolean {
488 return !_.isNil(node.data) ? node.data.isFailed : false;
491 /************************************************
492 in a case the node is failed e.g. not instantiated correctly
493 the function will call to openRetryInstanceAuditInfoModal
494 @node : node model from the left tree
495 @serviceModelId : serviceModelId
497 @instanceType: instanceType
498 @modelInfoService : the model (vnf, vfmodule, network, vnfgroup)object that call to the function (this)
499 ************************************************/
500 openAuditInfoModal(node, serviceModelId, instance, instanceType, modelInfoService : ILevelNodeInfo){
501 AuditInfoModalComponent.openInstanceAuditInfoModal.next({
502 instanceId: serviceModelId,
504 model: modelInfoService.getModel(
505 this.modelByIdentifiers(
506 this._store.getState().service.serviceHierarchy[serviceModelId],
507 modelInfoService.name,
508 this.modelUniqueNameOrId(instance), node.data.modelName
515 getModelVersionEitherFromInstanceOrFromHierarchy(selectedNodeData, model): string | undefined {
516 return this.getNamedFieldFromInstanceOrFromHierarchy(selectedNodeData, "modelVersion", model, "version");
519 getModelCustomizationIdEitherFromInstanceOrFromHierarchy(selectedNodeData, model): string | undefined {
520 return this.getNamedFieldFromInstanceOrFromHierarchy(selectedNodeData, "modelCustomizationId", model, "customizationUuid");
523 getModelInvariantIdEitherFromInstanceOrFromHierarchy(selectedNodeData, model): string | undefined {
524 return this.getNamedFieldFromInstanceOrFromHierarchy(selectedNodeData, "modelInvariantId", model, "invariantUuid");
527 getModelVersionIdEitherFromInstanceOrFromHierarchy(selectedNodeData, model): string | undefined {
528 return this.getNamedFieldFromInstanceOrFromHierarchy (selectedNodeData, "modelVersionId", model, "uuid");
533 getNamedFieldFromInstanceOrFromHierarchy(selectedNodeData, instanceModelInfoFieldName, model, modelFieldName): string | undefined {
534 if (instanceModelInfoFieldName && selectedNodeData && selectedNodeData.instanceModelInfo
535 && selectedNodeData.instanceModelInfo[instanceModelInfoFieldName]) {
536 return selectedNodeData.instanceModelInfo[instanceModelInfoFieldName];
537 } else if (modelFieldName && model && model[modelFieldName]) {
538 return model[modelFieldName];
543 addGeneralInfoItems(modelInfoSpecificItems: ModelInformationItem[], type: ComponentInfoType, model, selectedNodeData):ComponentInfoModel {
544 let modelInfoItems: ModelInformationItem[] = [
545 ModelInformationItem.createInstance("Model version", this.getModelVersionEitherFromInstanceOrFromHierarchy(selectedNodeData, model)),
546 ModelInformationItem.createInstance("Model customization ID", this.getModelCustomizationIdEitherFromInstanceOrFromHierarchy(selectedNodeData, model)),
547 ModelInformationItem.createInstance("Instance ID", selectedNodeData ? selectedNodeData.instanceId : null),
548 ModelInformationItem.createInstance("Instance type", selectedNodeData ? selectedNodeData.instanceType : null),
549 ModelInformationItem.createInstance("In maintenance", selectedNodeData? selectedNodeData.inMaint : null),
551 modelInfoItems = modelInfoItems.concat(modelInfoSpecificItems);
552 return this.getComponentInfoModelByModelInformationItems(modelInfoItems, type, selectedNodeData);
555 getComponentInfoModelByModelInformationItems(modelInfoItems: ModelInformationItem[], type: ComponentInfoType, instance){
556 const modelInfoItemsWithoutEmpty = _.filter(modelInfoItems, function(item){ return !item.values.every(_.isNil)});
557 return new ComponentInfoModel(type, modelInfoItemsWithoutEmpty, [], instance != null);
560 createMaximumToInstantiateModelInformationItem(model): ModelInformationItem {
561 return ModelInformationItem.createInstance(
563 !_.isNil(model.max) ? String(model.max) : Constants.ModelInfo.UNLIMITED_DEFAULT
567 isAddPositionFlagTrue():boolean{
568 return FeatureFlagsService.getFlagState(Features.FLAG_2008_CREATE_VFMODULE_INSTANTIATION_ORDER_NUMBER, this._store);
571 upgradeAllowedForBm(node, serviceModelId):boolean {
573 if(FeatureFlagsService.getFlagState(Features.FLAG_2012_UPGRADE_BASE_MODULE_FLAG, this._store)){
574 let returnValue = false;
575 if(node.data.type == 'VFmodule') {
576 let serviceHierarchy = this._store.getState().service.serviceHierarchy[serviceModelId];
577 let vnf = node.parent.data.modelName;
578 let baseModuleFlag= (serviceHierarchy.vnfs[vnf].vfModules[node.data.modelName].properties.baseModule) ? true: false;
581 if(node.parent.children.length >1) {