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 {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 instance.originalName ? instance.originalName : this.modelUniqueId(instance);
60 * Finds a model inside a full service model
61 * @param serviceModelFromHierarchy
62 * @param modelTypeName "vnfs" | "networks" | "vfModules" | "collectionResources" | ...
63 * @param modelUniqueNameOrId Either an entry name (i.e. "originalName"), modelCustomizationId or modelInvariantId.
64 * Note that modelInvariantId will work only where model lacks a modelCustomizationId.
65 * @param modelName An optional entry name (i.e. "originalName"); will not try to use as id
67 modelByIdentifiers = (serviceModelFromHierarchy, modelTypeName: string, modelUniqueNameOrId: string, modelName?: string): any => {
68 const logErrorAndReturnUndefined = () =>
69 console.info(`modelByIdentifiers: could not find a model matching query`, {
70 modelTypeName, modelUniqueNameOrId, modelName, serviceModelFromHierarchy
73 if (_.isNil(serviceModelFromHierarchy)) return logErrorAndReturnUndefined();
75 const modelsOfType = serviceModelFromHierarchy[modelTypeName];
76 if (_.isNil(modelsOfType)) return logErrorAndReturnUndefined();
78 const modelIfModelIdentifierIsEntryName = modelsOfType[modelUniqueNameOrId];
79 const modelIfModeNameExists = _.isNil(modelName) ? null : modelsOfType[modelName];
81 if (!_.isNil(modelIfModelIdentifierIsEntryName)) {
82 return modelIfModelIdentifierIsEntryName;
83 } else if (!_.isNil(modelIfModeNameExists)) {
84 return modelIfModeNameExists;
86 // try modelUniqueNameOrId as an id
87 return _.find(modelsOfType, o => (o.customizationUuid || o.invariantUuid) === modelUniqueNameOrId) || logErrorAndReturnUndefined()
91 hasMissingData(instance, dynamicInputs: any, isEcompGeneratedNaming: boolean, requiredFields: string[]): boolean {
92 if (!isEcompGeneratedNaming && _.isEmpty(instance.instanceName)) {
96 for (let field of requiredFields) {
97 if (_.isEmpty(instance[field])) {
102 for (let field of dynamicInputs) {
103 if (field.isRequired && !_.isNil(instance.instanceParams) && _.isEmpty(instance.instanceParams[0][field.id])) {
111 addingStatusProperty(node) {
112 node['statusProperties'] = [];
113 node['statusProperties'].push({key: 'Prov Status:', value: node.provStatus, testId: 'provStatus'});
114 node['statusProperties'].push({key: 'Orch Status:', value: node.orchStatus, testId: 'orchStatus'});
116 node['statusProperties'].push({key: 'In-maintenance', value: '', testId: 'inMaint'});
121 /**********************************************
122 * should delete or remove child instance's
123 "new" -> should remove
124 !new" -> should change action status
125 **********************************************/
126 removeDeleteAllChild(node, serviceModelId: string, callback): void {
127 for (let nodeChild of node.children) {
128 if (nodeChild.data.action === ServiceInstanceActions.Create) {
129 if (!_.isNil(nodeChild.data) && !_.isNil(nodeChild.data.menuActions) && !_.isNil(nodeChild.data.menuActions['remove'])) {
130 nodeChild.data.menuActions['remove']['method'](nodeChild, serviceModelId);
133 if (!_.isNil(nodeChild.data) && !_.isNil(nodeChild.data.menuActions) && !_.isNil(nodeChild.data.menuActions['delete'])) {
134 nodeChild.data.menuActions['delete']['method'](nodeChild, serviceModelId);
138 callback(node, serviceModelId);
142 /**********************************************
143 * should undo delete child instance's
144 **********************************************/
145 undoDeleteAllChild(node, serviceModelId: string, callback): void {
146 for (let nodeChild of node.children) {
147 if (!_.isNil(nodeChild.data) && !_.isNil(nodeChild.data.menuActions) && !_.isNil(nodeChild.data.menuActions['undoDelete'])) {
148 nodeChild.data.menuActions['undoDelete']['method'](nodeChild, serviceModelId);
151 callback(node, serviceModelId);
154 /**********************************************
155 * should return true if can delete
156 **********************************************/
157 shouldShowDelete(node, serviceModelId): boolean {
158 return this.shouldShowButtonGeneric(node, "delete", serviceModelId)
161 /**********************************************
162 * should return true if can undo delete
163 **********************************************/
164 shouldShowUndoDelete(node): boolean {
165 const mode = this._store.getState().global.drawingBoardStatus;
166 if (mode === DrawingBoardModes.EDIT && !_.isNil(node.data.action) && !_.isNil(node.data.menuActions['undoDelete'])) {
167 if (node.data.action === ServiceInstanceActions.Create || node.data.action === ServiceInstanceActions.Delete) {
169 } else if (node.data.action.split('_').pop() === 'Delete') {
176 /**********************************************
177 * should return true if can remove or edit
178 * enabled only on edit/design mode and for new instances
179 **********************************************/
180 shouldShowRemoveAndEdit(node): boolean {
181 const mode = this._store.getState().global.drawingBoardStatus;
182 if (!_.isNil(node) && !_.isNil(node.data) && !_.isNil(node.data.action) && node.data.action === ServiceInstanceActions.Create &&
183 mode !== DrawingBoardModes.VIEW && mode !== DrawingBoardModes.RETRY) {
188 /**********************************************
189 * enabled only on edit/design
190 * enabled only if there's a newer version for VNF-M
191 **********************************************/
192 upgradeBottomUp(node,serviceModelId: string): void {
193 this.iterateOverTreeBranchAndRunAction(node, serviceModelId, VNFMethods.UPGRADE);
194 this._store.dispatch(upgradeService(serviceModelId));
197 private iterateOverTreeBranchAndRunAction(node, serviceModelId: string, actionMethod) {
198 while (_.has(node.parent, 'data') && _.has(node.parent.data, 'menuActions')
199 && !_.isNil(node.parent.data.menuActions[actionMethod])) {
201 node.data.menuActions[actionMethod]['method'](node, serviceModelId);
205 /****************************************************
206 * should return true if customer can upgrade a VFM *
207 ****************************************************/
208 shouldShowUpgrade(node, serviceModelId): boolean {
209 if (FeatureFlagsService.getFlagState(Features.FLAG_FLASH_REPLACE_VF_MODULE, this._store) &&
210 (this.isThereAnUpdatedLatestVersion(serviceModelId)) || this.isDiffCustomizationUuid(node, serviceModelId)) {
211 return this.shouldShowButtonGeneric(node, VNFMethods.UPGRADE, serviceModelId);
219 isDiffCustomizationUuid(node, serviceModelId) : boolean {
220 const vfModuleServiceHierarchy = this.getVfModuleHierarchyThroughParentModelName(node, serviceModelId);
221 if(_.isNil(vfModuleServiceHierarchy)){
224 return node.data && !_.isNil(vfModuleServiceHierarchy) && vfModuleServiceHierarchy.customizationUuid && (vfModuleServiceHierarchy.customizationUuid !== node.data.modelCustomizationId);
227 getVfModuleHierarchyThroughParentModelName(node, serviceModelId) {
228 if(node.parent && node.parent.data && node.data){
229 const vnfHierarchy = this._store.getState().service.serviceHierarchy[serviceModelId].vnfs[node.parent.data.modelName];
230 return vnfHierarchy ? vnfHierarchy.vfModules[node.data.modelName] : null;
236 isThereAnUpdatedLatestVersion(serviceModelId) : boolean{
237 let serviceInstance = this.getServiceInstance(serviceModelId);
238 return !_.isNil(serviceInstance.latestAvailableVersion) && (Number(serviceInstance.modelInfo.modelVersion) < serviceInstance.latestAvailableVersion);
241 private getServiceInstance(serviceModelId): any {
242 return this._store.getState().service.serviceInstance[serviceModelId];
245 shouldShowButtonGeneric(node, method, serviceModelId) {
246 const mode = this._store.getState().global.drawingBoardStatus;
247 const isMacro = !(this.getServiceInstance(serviceModelId).isALaCarte);
249 if (isMacro) { //if macro action allowed only for service level
253 if (!_.isNil(node) && !_.isNil(node.data) && !_.isNil(node.data.action) && !_.isNil(node.data.menuActions[method])) {
254 if (mode !== DrawingBoardModes.EDIT || node.data.action === ServiceInstanceActions.Create) {
257 else if (node.data.action === ServiceInstanceActions.None) {
264 /**********************************************
265 * return boolean according to
266 * current defined action of VFModule node
267 **********************************************/
268 shouldShowUndoUpgrade(node): boolean {
269 const mode = this._store.getState().global.drawingBoardStatus;
270 if (mode === DrawingBoardModes.EDIT && !_.isNil(node.data.action) && !_.isNil(node.data.menuActions[VNFMethods.UNDO_UPGRADE])) {
271 if (node.data.action === ServiceInstanceActions.Upgrade) {
273 } else if (node.data.action.split('_').pop() === ServiceInstanceActions.Upgrade) {
280 /**********************************************
281 * enabled only on edit/design
282 * enabled only if there's a newer version for VNF-M
283 **********************************************/
284 undoUpgradeBottomUp(node,serviceModelId: string): void {
285 this.iterateOverTreeBranchAndRunAction(node, serviceModelId, VNFMethods.UNDO_UPGRADE);
286 this._store.dispatch(undoUpgradeService(serviceModelId));
288 /**********************************************
289 * should return true if can duplicate by mode
290 **********************************************/
291 shouldShowDuplicate(node): boolean {
292 const mode = this._store.getState().global.drawingBoardStatus;
293 return !mode.includes('RETRY');
296 /**********************************************
297 * should return true if can audit info
298 **********************************************/
299 shouldShowAuditInfo(node): boolean {
300 return this.isRetryMode() || (!_.isNil(node.data) && !_.isNil(node.data.action) && node.data.action !== ServiceInstanceActions.Create);
304 isRetryMode(): boolean {
305 const mode = this._store.getState().global.drawingBoardStatus;
306 return mode.includes('RETRY');
310 /**********************************************
311 * should return true if can add node instances
312 **********************************************/
313 shouldShowAddIcon(): boolean{
314 const mode = this._store.getState().global.drawingBoardStatus;
315 return mode === DrawingBoardModes.EDIT || mode=== DrawingBoardModes.CREATE || mode=== DrawingBoardModes.RECREATE;
319 isReachedToMaxInstances(properties, counter, flags): boolean{
320 let maxInstances = Utils.getMaxFirstLevel(properties, flags);
321 if(_.isNil(maxInstances)){
324 return !(maxInstances > counter);
327 /************************************************
328 return number of instances with action Delete
329 @type: vnfs networks, vngGroups (not vfModule)
330 @node : node model from the left tree
331 ************************************************/
332 getExistingInstancesWithDeleteMode(node, serviceModelId: string, type: string): number {
334 const existingInstances = this.getServiceInstance(serviceModelId)[type];
335 const modelUniqueId = node.data.modelUniqueId;
336 if (!_.isNil(existingInstances)) {
337 for (let instanceKey in existingInstances) {
338 if (!_.isNil(existingInstances[instanceKey].action)) {
339 if (existingInstances[instanceKey].modelInfo.modelUniqueId === modelUniqueId && existingInstances[instanceKey].action.split('_').pop() === 'Delete') {
349 isServiceOnDeleteMode(serviceId: string): boolean {
350 return this._store.getState().service.serviceInstance[serviceId].action === ServiceInstanceActions.Delete;
354 openModal(node : any | any[] , serviceModelId : string, cb : Function) : void {
355 let type: string = _.isArray(node) ? 'Service' : node.data.typeName;
356 let messageBoxData: MessageBoxData = new MessageBoxData(
358 `You are about to mark for delete this ${type} this will also mark all its children and remove all new instances just added`,
363 text: "Mark and remove",
365 callback: cb.bind(this, node, serviceModelId),
368 {text: "Don’t Remove", size: "medium", closeModal: true}
371 MessageBoxService.openModal.next(messageBoxData);
374 someChildHasCreateAction(nodes: any | any[]) : boolean {
375 let nodesArr = _.isArray(nodes) ? nodes : [nodes];
376 for(const node of nodesArr){
377 if(node.action === ServiceInstanceActions.Create) {return true;}
379 for (let nodeChild of node.children) {
380 if (nodeChild.action === ServiceInstanceActions.Create) {
383 if(nodeChild.children && nodeChild.children.length > 0){
384 for(let child of nodeChild.children){
385 let hasCreateAction = this.someChildHasCreateAction(child);
386 if(hasCreateAction) {
397 shouldShowDeleteInstanceWithChildrenModal(node : any | any[] , serviceModelId : string, cb : Function) : void {
398 if(this.someChildHasCreateAction(node)){
399 this.openModal(node , serviceModelId, cb);
401 cb(node, serviceModelId)
406 isFailed(node): boolean {
407 return !_.isNil(node.data) ? node.data.isFailed : false;
410 /************************************************
411 in a case the node is failed e.g. not instantiated correctly
412 the function will call to openRetryInstanceAuditInfoModal
413 @node : node model from the left tree
414 @serviceModelId : serviceModelId
416 @instanceType: instanceType
417 @modelInfoService : the model (vnf, vfmodule, network, vnfgroup)object that call to the function (this)
418 ************************************************/
419 openAuditInfoModal(node, serviceModelId, instance, instanceType, modelInfoService : ILevelNodeInfo){
420 AuditInfoModalComponent.openInstanceAuditInfoModal.next({
421 instanceId: serviceModelId,
423 model: modelInfoService.getModel(
424 this.modelByIdentifiers(
425 this._store.getState().service.serviceHierarchy[serviceModelId],
426 modelInfoService.name,
427 this.modelUniqueNameOrId(instance), node.data.modelName
435 addGeneralInfoItems(modelInfoSpecificItems: ModelInformationItem[], type: ComponentInfoType, model, instance):ComponentInfoModel {
436 let modelInfoItems: ModelInformationItem[] = [
437 ModelInformationItem.createInstance("Model version", model ? model.version : null),
438 ModelInformationItem.createInstance("Model customization ID", model ? model.customizationUuid : null),
439 ModelInformationItem.createInstance("Instance ID", instance ? instance.instanceId : null),
440 ModelInformationItem.createInstance("Instance type", instance ? instance.instanceType : null),
441 ModelInformationItem.createInstance("In maintenance", instance? instance.inMaint : null),
443 modelInfoItems = modelInfoItems.concat(modelInfoSpecificItems);
444 return this.getComponentInfoModelByModelInformationItems(modelInfoItems, type, instance);
447 getComponentInfoModelByModelInformationItems(modelInfoItems: ModelInformationItem[], type: ComponentInfoType, instance){
448 const modelInfoItemsWithoutEmpty = _.filter(modelInfoItems, function(item){ return !item.values.every(_.isNil)});
449 return new ComponentInfoModel(type, modelInfoItemsWithoutEmpty, [], instance != null);
452 createMaximumToInstantiateModelInformationItem(model): ModelInformationItem {
453 return ModelInformationItem.createInstance(
455 !_.isNil(model.max) ? String(model.max) : Constants.ModelInfo.UNLIMITED_DEFAULT