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 selectedVNF: string = null;
34 getSelectedVNF(): string {
35 return this.selectedVNF;
38 setSelectedVNF(node): void {
39 if (_.isNil(node) || node.data.type !== 'VF') {
40 this.selectedVNF = null;
42 this.selectedVNF = node.data.vnfStoreKey;
47 * Determines a consistent unique ID for a given right-tree
50 modelUniqueId = (nodeInstance: NodeInstance): string => {
51 return _.isNil(nodeInstance.modelInfo)
53 : (nodeInstance.modelInfo.modelCustomizationId || nodeInstance.modelInfo.modelInvariantId);
56 modelUniqueNameOrId = (instance): string => {
57 if (_.isNil(instance)) {
61 const innerInstance = _.find(instance) || {};
63 return instance.originalName
64 || this.modelUniqueId(instance)
65 || innerInstance.originalName
66 || this.modelUniqueId(innerInstance);
70 * Finds a model inside a full service model
71 * @param serviceModelFromHierarchy
72 * @param modelTypeName "vnfs" | "networks" | "vfModules" | "collectionResources" | ...
73 * @param modelUniqueNameOrId Either an entry name (i.e. "originalName"), modelCustomizationId or modelInvariantId.
74 * Note that modelInvariantId will work only where model lacks a modelCustomizationId.
75 * @param modelName An optional entry name (i.e. "originalName"); will not try to use as id
77 modelByIdentifiers = (serviceModelFromHierarchy, modelTypeName: string, modelUniqueNameOrId: string, modelName?: string): any => {
78 const logErrorAndReturnUndefined = () =>
79 console.info(`modelByIdentifiers: could not find a model matching query`, {
80 modelTypeName, modelUniqueNameOrId, modelName, serviceModelFromHierarchy
83 if (_.isNil(serviceModelFromHierarchy)) return logErrorAndReturnUndefined();
85 const modelsOfType = serviceModelFromHierarchy[modelTypeName];
86 if (_.isNil(modelsOfType)) return logErrorAndReturnUndefined();
88 const modelIfModelIdentifierIsEntryName = modelsOfType[modelUniqueNameOrId];
89 const modelIfModeNameExists = _.isNil(modelName) ? null : modelsOfType[modelName];
91 if (!_.isNil(modelIfModelIdentifierIsEntryName)) {
92 return modelIfModelIdentifierIsEntryName;
93 } else if (!_.isNil(modelIfModeNameExists)) {
94 return modelIfModeNameExists;
96 // try modelUniqueNameOrId as an id
97 return _.find(modelsOfType, o => (o.customizationUuid || o.invariantUuid) === modelUniqueNameOrId) || logErrorAndReturnUndefined()
101 hasMissingData(instance, dynamicInputs: any, isEcompGeneratedNaming: boolean, requiredFields: string[]): boolean {
102 if (!isEcompGeneratedNaming && _.isEmpty(instance.instanceName)) {
106 for (let field of requiredFields) {
107 if (_.isEmpty(instance[field])) {
112 for (let field of dynamicInputs) {
113 if (field.isRequired && !_.isNil(instance.instanceParams) && _.isEmpty(instance.instanceParams[0][field.id])) {
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.type === 'VFmodule') {
126 node['statusProperties'].push({key: 'Model Version: ', value: this.getNodeModelVersion(node), testId: 'modelVersion'});
129 node['statusProperties'].push({key: 'In-maintenance', value: '', testId: 'inMaint'});
134 getNodeModelVersion(node): string | undefined {
135 if(!_.isNil(node.instanceModelInfo) && !_.isNil(node.instanceModelInfo.modelVersion)){
136 return node.instanceModelInfo.modelVersion;
141 /**********************************************
142 * should delete or remove child instance's
143 "new" -> should remove
144 !new" -> should change action status
145 **********************************************/
146 removeDeleteAllChild(node, serviceModelId: string, callback): void {
147 for (let nodeChild of node.children) {
148 if (nodeChild.data.action === ServiceInstanceActions.Create) {
149 if (!_.isNil(nodeChild.data) && !_.isNil(nodeChild.data.menuActions) && !_.isNil(nodeChild.data.menuActions['remove'])) {
150 nodeChild.data.menuActions['remove']['method'](nodeChild, serviceModelId);
153 if (!_.isNil(nodeChild.data) && !_.isNil(nodeChild.data.menuActions) && !_.isNil(nodeChild.data.menuActions['delete'])) {
154 nodeChild.data.menuActions['delete']['method'](nodeChild, serviceModelId);
158 callback(node, serviceModelId);
162 /**********************************************
163 * should undo delete child instance's
164 **********************************************/
165 undoDeleteAllChild(node, serviceModelId: string, callback): void {
166 for (let nodeChild of node.children) {
167 if (!_.isNil(nodeChild.data) && !_.isNil(nodeChild.data.menuActions) && !_.isNil(nodeChild.data.menuActions['undoDelete'])) {
168 nodeChild.data.menuActions['undoDelete']['method'](nodeChild, serviceModelId);
171 callback(node, serviceModelId);
174 /**********************************************
175 * should return true if can delete
176 **********************************************/
177 shouldShowDelete(node, serviceModelId): boolean {
178 return this.shouldShowButtonGeneric(node, "delete", serviceModelId)
181 /**********************************************
182 * should return true if can undo delete
183 **********************************************/
184 shouldShowUndoDelete(node): boolean {
185 const mode = this._store.getState().global.drawingBoardStatus;
186 if (mode === DrawingBoardModes.EDIT && !_.isNil(node.data.action) && !_.isNil(node.data.menuActions['undoDelete'])) {
187 if (node.data.action === ServiceInstanceActions.Create || node.data.action === ServiceInstanceActions.Delete) {
189 } else if (node.data.action.split('_').pop() === 'Delete') {
196 /**********************************************
197 * should return true if can remove or edit
198 * enabled only on edit/design mode and for new instances
199 **********************************************/
200 shouldShowRemoveAndEdit(node): boolean {
201 const mode = this._store.getState().global.drawingBoardStatus;
202 if (!_.isNil(node) && !_.isNil(node.data) && !_.isNil(node.data.action) && node.data.action === ServiceInstanceActions.Create &&
203 mode !== DrawingBoardModes.VIEW && mode !== DrawingBoardModes.RETRY) {
208 /**********************************************
209 * enabled only on edit/design
210 * enabled only if there's a newer version for VNF-M
211 **********************************************/
212 upgradeBottomUp(node,serviceModelId: string): void {
213 this.iterateOverTreeBranchAndRunAction(node, serviceModelId, VNFMethods.UPGRADE);
214 this._store.dispatch(upgradeService(serviceModelId));
217 private iterateOverTreeBranchAndRunAction(node, serviceModelId: string, actionMethod) {
218 while (_.has(node.parent, 'data') && _.has(node.parent.data, 'menuActions')
219 && !_.isNil(node.parent.data.menuActions[actionMethod])) {
221 node.data.menuActions[actionMethod]['method'](node, serviceModelId);
225 shouldShowRemovePause(node) : boolean {
226 if(FeatureFlagsService.getFlagState(Features.FLAG_2008_REMOVE_PAUSE_INSTANTIATION, this._store)){
227 return node.pauseInstantiation === PauseStatus.AFTER_COMPLETION;
232 shouldShowPauseInstantiation(node): boolean {
233 if(FeatureFlagsService.getFlagState(Features.FLAG_2008_REMOVE_PAUSE_INSTANTIATION, this._store)){
234 return (FeatureFlagsService.getFlagState(Features.FLAG_2006_PAUSE_VFMODULE_INSTANTIATION_CREATION, this._store) && node.pauseInstantiation == null);
236 return (FeatureFlagsService.getFlagState(Features.FLAG_2006_PAUSE_VFMODULE_INSTANTIATION_CREATION, this._store));
238 /****************************************************
239 * should return true if customer can upgrade a VFM *
240 ****************************************************/
241 shouldShowUpgrade(node, serviceModelId): boolean {
242 return (this.isVfMoudleCouldBeUpgraded(node, serviceModelId))
243 && this.shouldShowButtonGeneric(node, VNFMethods.UPGRADE, serviceModelId) ;
246 isVfMoudleCouldBeUpgraded(node, serviceModelId): boolean{
247 return (FeatureFlagsService.getFlagState(Features.FLAG_FLASH_REPLACE_VF_MODULE, this._store) &&
248 (this.isThereAnUpdatedLatestVersion(serviceModelId) || this.isVfModuleCustomizationIdNotExistsOnModel(node, serviceModelId)))
251 isVfModuleCustomizationIdNotExistsOnModel(vfModuleNode, serviceModelId) {
254 if (_.isNil(vfModuleNode.data) || _.isNil(vfModuleNode.data.modelCustomizationId)) {
258 let vfModulesHierarchyByGivenModelId = this._store.getState().service.serviceHierarchy[serviceModelId].vfModules;
259 return !_.some(vfModulesHierarchyByGivenModelId, vfmodel => vfmodel.customizationUuid === vfModuleNode.data.modelCustomizationId);
263 isVfmoduleAlmostPartOfModelOnlyCustomizationUuidDiffer(vfModuleNode, serviceModelId) : boolean {
265 for `true`, should all:
266 1. parent vnf found by model-mane
267 2. vfmodule found by invariant
268 3. vfmodule diff by customization
271 if (_.isNil(vfModuleNode.data)) {
275 const vnfHierarchy = this.getParentVnfHierarchy(vfModuleNode, serviceModelId);
276 if (_.isNil(vnfHierarchy)) {
280 const vfModuleHierarchyByInvariantId = this.getVfModuleHFromVnfHierarchyByInvariantId(vfModuleNode, vnfHierarchy);
281 if(_.isNil(vfModuleHierarchyByInvariantId)){
285 return vfModuleHierarchyByInvariantId.customizationUuid
286 && (vfModuleHierarchyByInvariantId.customizationUuid !== vfModuleNode.data.modelCustomizationId);
289 getParentVnfHierarchy(vfModuleNode, serviceModelId) {
290 if (vfModuleNode.parent && vfModuleNode.parent.data) {
291 return this._store.getState().service.serviceHierarchy[serviceModelId].vnfs[vfModuleNode.parent.data.modelName];
297 getVfModuleHFromVnfHierarchyByInvariantId(vfModuleNode, parentVnfHierarchy) {
298 if(vfModuleNode.data.modelInvariantId && parentVnfHierarchy && parentVnfHierarchy.vfModules){
299 return _.find(parentVnfHierarchy.vfModules, o => o.invariantUuid === vfModuleNode.data.modelInvariantId);
305 isThereAnUpdatedLatestVersion(serviceModelId) : boolean{
306 let serviceInstance = this.getServiceInstance(serviceModelId);
307 return !_.isNil(serviceInstance.latestAvailableVersion) && (Number(serviceInstance.modelInfo.modelVersion) < serviceInstance.latestAvailableVersion);
310 private getServiceInstance(serviceModelId): any {
311 return this._store.getState().service.serviceInstance[serviceModelId];
314 shouldShowButtonGeneric(node, method, serviceModelId) {
315 const mode = this._store.getState().global.drawingBoardStatus;
316 const isMacro = !(this.getServiceInstance(serviceModelId).isALaCarte);
318 if (isMacro) { //if macro action allowed only for service level
322 if (!_.isNil(node) && !_.isNil(node.data) && !_.isNil(node.data.action) && !_.isNil(node.data.menuActions[method])) {
323 if (mode !== DrawingBoardModes.EDIT || node.data.action === ServiceInstanceActions.Create) {
326 else if (node.data.action === ServiceInstanceActions.None) {
333 /**********************************************
334 * return boolean according to
335 * current defined action of VFModule node
336 **********************************************/
337 shouldShowUndoUpgrade(node): boolean {
338 const mode = this._store.getState().global.drawingBoardStatus;
339 if (mode === DrawingBoardModes.EDIT && !_.isNil(node.data.action) && !_.isNil(node.data.menuActions[VNFMethods.UNDO_UPGRADE])) {
340 if (node.data.action === ServiceInstanceActions.Upgrade) {
342 } else if (node.data.action.split('_').pop() === ServiceInstanceActions.Upgrade) {
349 /**********************************************
350 * enabled only on edit/design
351 * enabled only if there's a newer version for VNF-M
352 **********************************************/
353 undoUpgradeBottomUp(node,serviceModelId: string): void {
354 this.iterateOverTreeBranchAndRunAction(node, serviceModelId, VNFMethods.UNDO_UPGRADE);
355 this._store.dispatch(undoUpgradeService(serviceModelId));
357 /**********************************************
358 * should return true if can duplicate by mode
359 **********************************************/
360 shouldShowDuplicate(node): boolean {
361 const mode = this._store.getState().global.drawingBoardStatus;
362 return !mode.includes('RETRY');
365 /**********************************************
366 * should return true if can audit info
367 **********************************************/
368 shouldShowAuditInfo(node): boolean {
369 return this.isRetryMode() || (!_.isNil(node.data) && !_.isNil(node.data.action) && node.data.action !== ServiceInstanceActions.Create);
373 isRetryMode(): boolean {
374 const mode = this._store.getState().global.drawingBoardStatus;
375 return mode.includes('RETRY');
379 /**********************************************
380 * should return true if can add node instances
381 **********************************************/
382 shouldShowAddIcon(): boolean{
383 const mode = this._store.getState().global.drawingBoardStatus;
384 return mode === DrawingBoardModes.EDIT || mode=== DrawingBoardModes.CREATE || mode=== DrawingBoardModes.RECREATE;
388 isReachedToMaxInstances(properties, counter, flags): boolean{
389 let maxInstances = Utils.getMaxFirstLevel(properties, flags);
390 if(_.isNil(maxInstances)){
393 return !(maxInstances > counter);
396 /************************************************
397 return number of instances with action Delete
398 @type: vnfs networks, vngGroups (not vfModule)
399 @node : node model from the left tree
400 ************************************************/
401 getExistingInstancesWithDeleteMode(node, serviceModelId: string, type: string): number {
403 const existingInstances = this.getServiceInstance(serviceModelId)[type];
404 const modelUniqueId = node.data.modelUniqueId;
405 if (!_.isNil(existingInstances)) {
406 for (let instanceKey in existingInstances) {
407 if (!_.isNil(existingInstances[instanceKey].action)) {
408 if (existingInstances[instanceKey].modelInfo.modelUniqueId === modelUniqueId && existingInstances[instanceKey].action.split('_').pop() === 'Delete') {
418 isServiceOnDeleteMode(serviceId: string): boolean {
419 return this._store.getState().service.serviceInstance[serviceId].action === ServiceInstanceActions.Delete;
423 openModal(node : any | any[] , serviceModelId : string, cb : Function) : void {
424 let type: string = _.isArray(node) ? 'Service' : node.data.typeName;
425 let messageBoxData: MessageBoxData = new MessageBoxData(
427 `You are about to mark for delete this ${type} this will also mark all its children and remove all new instances just added`,
432 text: "Mark and remove",
434 callback: cb.bind(this, node, serviceModelId),
437 {text: "Don’t Remove", size: "medium", closeModal: true}
440 MessageBoxService.openModal.next(messageBoxData);
443 someChildHasCreateAction(nodes: any | any[]) : boolean {
444 let nodesArr = _.isArray(nodes) ? nodes : [nodes];
445 for(const node of nodesArr){
446 if(node.action === ServiceInstanceActions.Create) {return true;}
448 for (let nodeChild of node.children) {
449 if (nodeChild.action === ServiceInstanceActions.Create) {
452 if(nodeChild.children && nodeChild.children.length > 0){
453 for(let child of nodeChild.children){
454 let hasCreateAction = this.someChildHasCreateAction(child);
455 if(hasCreateAction) {
466 shouldShowDeleteInstanceWithChildrenModal(node : any | any[] , serviceModelId : string, cb : Function) : void {
467 if(this.someChildHasCreateAction(node)){
468 this.openModal(node , serviceModelId, cb);
470 cb(node, serviceModelId)
475 isFailed(node): boolean {
476 return !_.isNil(node.data) ? node.data.isFailed : false;
479 /************************************************
480 in a case the node is failed e.g. not instantiated correctly
481 the function will call to openRetryInstanceAuditInfoModal
482 @node : node model from the left tree
483 @serviceModelId : serviceModelId
485 @instanceType: instanceType
486 @modelInfoService : the model (vnf, vfmodule, network, vnfgroup)object that call to the function (this)
487 ************************************************/
488 openAuditInfoModal(node, serviceModelId, instance, instanceType, modelInfoService : ILevelNodeInfo){
489 AuditInfoModalComponent.openInstanceAuditInfoModal.next({
490 instanceId: serviceModelId,
492 model: modelInfoService.getModel(
493 this.modelByIdentifiers(
494 this._store.getState().service.serviceHierarchy[serviceModelId],
495 modelInfoService.name,
496 this.modelUniqueNameOrId(instance), node.data.modelName
503 getModelVersionEitherFromInstanceOrFromHierarchy(selectedNodeData, model): string | undefined {
504 return this.getNamedFieldFromInstanceOrFromHierarchy(selectedNodeData, "modelVersion", model, "version");
507 getModelCustomizationIdEitherFromInstanceOrFromHierarchy(selectedNodeData, model): string | undefined {
508 return this.getNamedFieldFromInstanceOrFromHierarchy(selectedNodeData, "modelCustomizationId", model, "customizationUuid");
511 getModelInvariantIdEitherFromInstanceOrFromHierarchy(selectedNodeData, model): string | undefined {
512 return this.getNamedFieldFromInstanceOrFromHierarchy(selectedNodeData, "modelInvariantId", model, "invariantUuid");
515 getModelVersionIdEitherFromInstanceOrFromHierarchy(selectedNodeData, model): string | undefined {
516 return this.getNamedFieldFromInstanceOrFromHierarchy (selectedNodeData, "modelVersionId", model, "uuid");
521 getNamedFieldFromInstanceOrFromHierarchy(selectedNodeData, instanceModelInfoFieldName, model, modelFieldName): string | undefined {
522 if (instanceModelInfoFieldName && selectedNodeData && selectedNodeData.instanceModelInfo
523 && selectedNodeData.instanceModelInfo[instanceModelInfoFieldName]) {
524 return selectedNodeData.instanceModelInfo[instanceModelInfoFieldName];
525 } else if (modelFieldName && model && model[modelFieldName]) {
526 return model[modelFieldName];
531 addGeneralInfoItems(modelInfoSpecificItems: ModelInformationItem[], type: ComponentInfoType, model, selectedNodeData):ComponentInfoModel {
532 let modelInfoItems: ModelInformationItem[] = [
533 ModelInformationItem.createInstance("Model version", this.getModelVersionEitherFromInstanceOrFromHierarchy(selectedNodeData, model)),
534 ModelInformationItem.createInstance("Model customization ID", this.getModelCustomizationIdEitherFromInstanceOrFromHierarchy(selectedNodeData, model)),
535 ModelInformationItem.createInstance("Instance ID", selectedNodeData ? selectedNodeData.instanceId : null),
536 ModelInformationItem.createInstance("Instance type", selectedNodeData ? selectedNodeData.instanceType : null),
537 ModelInformationItem.createInstance("In maintenance", selectedNodeData? selectedNodeData.inMaint : null),
539 modelInfoItems = modelInfoItems.concat(modelInfoSpecificItems);
540 return this.getComponentInfoModelByModelInformationItems(modelInfoItems, type, selectedNodeData);
543 getComponentInfoModelByModelInformationItems(modelInfoItems: ModelInformationItem[], type: ComponentInfoType, instance){
544 const modelInfoItemsWithoutEmpty = _.filter(modelInfoItems, function(item){ return !item.values.every(_.isNil)});
545 return new ComponentInfoModel(type, modelInfoItemsWithoutEmpty, [], instance != null);
548 createMaximumToInstantiateModelInformationItem(model): ModelInformationItem {
549 return ModelInformationItem.createInstance(
551 !_.isNil(model.max) ? String(model.max) : Constants.ModelInfo.UNLIMITED_DEFAULT
555 isAddPositionFlagTrue():boolean{
556 return FeatureFlagsService.getFlagState(Features.FLAG_2008_CREATE_VFMODULE_INSTANTIATION_ORDER_NUMBER, this._store);