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 private _sharedTreeService: SharedTreeService;
23 constructor(private _store: NgRedux<AppState>) {
26 /***********************************************************
27 * return if instance has missing data
28 * @param instance - vnf instance
29 * @param dynamicInputs - from the instance
30 * @param isEcompGeneratedNaming
31 ************************************************************/
32 selectedVNF: string = null;
35 getSelectedVNF(): string {
36 return this.selectedVNF;
39 setSelectedVNF(node): void {
40 if (_.isNil(node) || node.data.type !== 'VF') {
41 this.selectedVNF = null;
43 this.selectedVNF = node.data.vnfStoreKey;
48 * Determines a consistent unique ID for a given right-tree
51 modelUniqueId = (nodeInstance: NodeInstance): string => {
52 return _.isNil(nodeInstance.modelInfo)
54 : (nodeInstance.modelInfo.modelCustomizationId || nodeInstance.modelInfo.modelInvariantId);
58 * Finds a model inside a full service model
59 * @param serviceModelFromHierarchy
60 * @param modelTypeName "vnfs" | "networks" | "vfModules" | "collectionResources" | ...
61 * @param modelUniqueIdOrName Either an entry name (i.e. "originalName"), modelCustomizationId or modelInvariantId.
62 * Note that modelInvariantId will work only where model lacks a modelCustomizationId.
64 modelByIdentifier = (serviceModelFromHierarchy, modelTypeName: string, modelUniqueIdOrName: string): any => {
65 if (_.isNil(serviceModelFromHierarchy)) return undefined;
67 const modelsOfType = serviceModelFromHierarchy[modelTypeName];
68 if (_.isNil(modelsOfType)) return undefined;
70 const modelIfModelIdentifierIsEntryName = modelsOfType[modelUniqueIdOrName];
71 return _.isNil(modelIfModelIdentifierIsEntryName)
72 ? _.find(modelsOfType, o => (o.customizationUuid || o.invariantUuid) === modelUniqueIdOrName)
73 : modelIfModelIdentifierIsEntryName;
76 hasMissingData(instance, dynamicInputs: any, isEcompGeneratedNaming: boolean, requiredFields: string[]): boolean {
77 if (!isEcompGeneratedNaming && _.isEmpty(instance.instanceName)) {
81 for (let field of requiredFields) {
82 if (_.isEmpty(instance[field])) {
87 for (let field of dynamicInputs) {
88 if (field.isRequired && !_.isNil(instance.instanceParams) && _.isEmpty(instance.instanceParams[0][field.id])) {
96 addingStatusProperty(node) {
97 node['statusProperties'] = [];
98 node['statusProperties'].push({key: 'Prov Status:', value: node.provStatus, testId: 'provStatus'});
99 node['statusProperties'].push({key: 'Orch Status:', value: node.orchStatus, testId: 'orchStatus'});
101 node['statusProperties'].push({key: 'In-maintenance', value: '', testId: 'inMaint'});
106 /**********************************************
107 * should delete or remove child instance's
108 "new" -> should remove
109 !new" -> should change action status
110 **********************************************/
111 removeDeleteAllChild(node, serviceModelId: string, callback): void {
112 for (let nodeChild of node.children) {
113 if (nodeChild.data.action === ServiceInstanceActions.Create) {
114 if (!_.isNil(nodeChild.data) && !_.isNil(nodeChild.data.menuActions) && !_.isNil(nodeChild.data.menuActions['remove'])) {
115 nodeChild.data.menuActions['remove']['method'](nodeChild, serviceModelId);
118 if (!_.isNil(nodeChild.data) && !_.isNil(nodeChild.data.menuActions) && !_.isNil(nodeChild.data.menuActions['delete'])) {
119 nodeChild.data.menuActions['delete']['method'](nodeChild, serviceModelId);
123 callback(node, serviceModelId);
127 /**********************************************
128 * should undo delete child instance's
129 **********************************************/
130 undoDeleteAllChild(node, serviceModelId: string, callback): void {
131 for (let nodeChild of node.children) {
132 if (!_.isNil(nodeChild.data) && !_.isNil(nodeChild.data.menuActions) && !_.isNil(nodeChild.data.menuActions['undoDelete'])) {
133 nodeChild.data.menuActions['undoDelete']['method'](nodeChild, serviceModelId);
136 callback(node, serviceModelId);
139 /**********************************************
140 * should return true if can delete
141 **********************************************/
142 shouldShowDelete(node): boolean {
143 return this.shouldShowButtonGeneric(node, "delete")
146 /**********************************************
147 * should return true if can undo delete
148 **********************************************/
149 shouldShowUndoDelete(node): boolean {
150 const mode = this._store.getState().global.drawingBoardStatus;
151 if (mode === DrawingBoardModes.EDIT && !_.isNil(node.data.action) && !_.isNil(node.data.menuActions['undoDelete'])) {
152 if (node.data.action === ServiceInstanceActions.Create || node.data.action === ServiceInstanceActions.Delete) {
154 } else if (node.data.action.split('_').pop() === 'Delete') {
161 /**********************************************
162 * should return true if can remove or edit
163 * enabled only on edit/design mode and for new instances
164 **********************************************/
165 shouldShowRemoveAndEdit(node): boolean {
166 const mode = this._store.getState().global.drawingBoardStatus;
167 if (!_.isNil(node) && !_.isNil(node.data) && !_.isNil(node.data.action) && node.data.action === ServiceInstanceActions.Create &&
168 mode !== DrawingBoardModes.VIEW && mode !== DrawingBoardModes.RETRY) {
173 /**********************************************
174 * enabled only on edit/design
175 * enabled only if there's a newer version for VNF-M
176 **********************************************/
177 upgradeBottomUp(node,serviceModelId: string): void {
178 this.iterateOverTreeBranchAndRunAction(node, serviceModelId, VNFMethods.UPGRADE);
179 this._store.dispatch(upgradeService(serviceModelId));
182 private iterateOverTreeBranchAndRunAction(node, serviceModelId: string, actionMethod) {
183 while (_.has(node.parent, 'data') && _.has(node.parent.data, 'menuActions')
184 && !_.isNil(node.parent.data.menuActions[actionMethod])) {
186 node.data.menuActions[actionMethod]['method'](node, serviceModelId);
190 /****************************************************
191 * should return true if customer can upgrade a VFM *
192 ****************************************************/
193 shouldShowUpgrade(node, serviceModelId): boolean {
194 if (FeatureFlagsService.getFlagState(Features.FLAG_FLASH_REPLACE_VF_MODULE, this._store) &&
195 this.isThereAnUpdatedLatestVersion(serviceModelId)) {
196 return this.shouldShowButtonGeneric(node, VNFMethods.UPGRADE);
203 private isThereAnUpdatedLatestVersion(serviceModelId) : boolean{
204 let serviceInstance = this._store.getState().service.serviceInstance[serviceModelId];
205 return !_.isNil(serviceInstance.latestAvailableVersion) && (Number(serviceInstance.modelInfo.modelVersion) < serviceInstance.latestAvailableVersion);
208 private shouldShowButtonGeneric(node, method) {
209 const mode = this._store.getState().global.drawingBoardStatus;
210 if (!_.isNil(node) && !_.isNil(node.data) && !_.isNil(node.data.action) && !_.isNil(node.data.menuActions[method])) {
211 if (mode !== DrawingBoardModes.EDIT || node.data.action === ServiceInstanceActions.Create) {
214 else if (node.data.action === ServiceInstanceActions.None) {
221 /**********************************************
222 * return boolean according to
223 * current defined action of VFModule node
224 **********************************************/
225 shouldShowUndoUpgrade(node): boolean {
226 const mode = this._store.getState().global.drawingBoardStatus;
227 if (mode === DrawingBoardModes.EDIT && !_.isNil(node.data.action) && !_.isNil(node.data.menuActions[VNFMethods.UNDO_UPGRADE])) {
228 if (node.data.action === ServiceInstanceActions.Upgrade) {
230 } else if (node.data.action.split('_').pop() === ServiceInstanceActions.Upgrade) {
237 /**********************************************
238 * enabled only on edit/design
239 * enabled only if there's a newer version for VNF-M
240 **********************************************/
241 undoUpgradeBottomUp(node,serviceModelId: string): void {
242 this.iterateOverTreeBranchAndRunAction(node, serviceModelId, VNFMethods.UNDO_UPGRADE);
243 this._store.dispatch(undoUpgradeService(serviceModelId));
245 /**********************************************
246 * should return true if can duplicate by mode
247 **********************************************/
248 shouldShowDuplicate(node): boolean {
249 const mode = this._store.getState().global.drawingBoardStatus;
250 return !mode.includes('RETRY');
253 /**********************************************
254 * should return true if can audit info
255 **********************************************/
256 shouldShowAuditInfo(node): boolean {
257 return this.isRetryMode() || (!_.isNil(node.data) && !_.isNil(node.data.action) && node.data.action !== ServiceInstanceActions.Create);
261 isRetryMode(): boolean {
262 const mode = this._store.getState().global.drawingBoardStatus;
263 return mode.includes('RETRY');
267 /**********************************************
268 * should return true if can add node instances
269 **********************************************/
270 shouldShowAddIcon(): boolean{
271 const mode = this._store.getState().global.drawingBoardStatus;
272 return mode === DrawingBoardModes.EDIT || mode=== DrawingBoardModes.CREATE || mode=== DrawingBoardModes.RECREATE;
276 isReachedToMaxInstances(properties, counter, flags): boolean{
277 let maxInstances = Utils.getMaxFirstLevel(properties, flags);
278 if(_.isNil(maxInstances)){
281 return !(maxInstances > counter);
284 /************************************************
285 return number of instances with action Delete
286 @type: vnfs networks, vngGroups (not vfModule)
287 @node : node model from the left tree
288 ************************************************/
289 getExistingInstancesWithDeleteMode(node, serviceModelId: string, type: string): number {
291 const existingInstances = this._store.getState().service.serviceInstance[serviceModelId][type];
292 const modelUniqueId = node.data.modelUniqueId;
293 if (!_.isNil(existingInstances)) {
294 for (let instanceKey in existingInstances) {
295 if (!_.isNil(existingInstances[instanceKey].action)) {
296 if (existingInstances[instanceKey].modelInfo.modelUniqueId === modelUniqueId && existingInstances[instanceKey].action.split('_').pop() === 'Delete') {
306 isServiceOnDeleteMode(serviceId: string): boolean {
307 return this._store.getState().service.serviceInstance[serviceId].action === ServiceInstanceActions.Delete;
311 openModal(node : any | any[] , serviceModelId : string, cb : Function) : void {
312 let type: string = _.isArray(node) ? 'Service' : node.data.typeName;
313 let messageBoxData: MessageBoxData = new MessageBoxData(
315 `You are about to mark for delete this ${type} this will also mark all its children and remove all new instances just added`,
320 text: "Mark and remove",
322 callback: cb.bind(this, node, serviceModelId),
325 {text: "Don’t Remove", size: "medium", closeModal: true}
328 MessageBoxService.openModal.next(messageBoxData);
331 someChildHasCreateAction(nodes: any | any[]) : boolean {
332 let nodesArr = _.isArray(nodes) ? nodes : [nodes];
333 for(const node of nodesArr){
334 if(node.action === ServiceInstanceActions.Create) {return true;}
336 for (let nodeChild of node.children) {
337 if (nodeChild.action === ServiceInstanceActions.Create) {
340 if(nodeChild.children && nodeChild.children.length > 0){
341 for(let child of nodeChild.children){
342 let hasCreateAction = this.someChildHasCreateAction(child);
343 if(hasCreateAction) {
354 shouldShowDeleteInstanceWithChildrenModal(node : any | any[] , serviceModelId : string, cb : Function) : void {
355 if(this.someChildHasCreateAction(node)){
356 this.openModal(node , serviceModelId, cb);
358 cb(node, serviceModelId)
363 isFailed(node): boolean {
364 return !_.isNil(node.data) ? node.data.isFailed : false;
367 /************************************************
368 in a case the node is failed e.g. not instantiated correctly
369 the function will call to openRetryInstanceAuditInfoModal
370 @node : node model from the left tree
371 @serviceModelId : serviceModelId
373 @instanceType: instanceType
374 @modelInfoService : the model (vnf, vfmodule, network, vnfgroup)object that call to the function (this)
375 ************************************************/
376 openAuditInfoModal(node, serviceModelId, instance, instanceType, modelInfoService : ILevelNodeInfo){
377 AuditInfoModalComponent.openInstanceAuditInfoModal.next({
378 instanceId: serviceModelId,
380 model: modelInfoService.getModel(node.data.modelName, instance, this._store.getState().service.serviceHierarchy[serviceModelId]),
386 addGeneralInfoItems(modelInfoSpecificItems: ModelInformationItem[], type: ComponentInfoType, model, instance):ComponentInfoModel {
387 let modelInfoItems: ModelInformationItem[] = [
388 ModelInformationItem.createInstance("Model version", model ? model.version : null),
389 ModelInformationItem.createInstance("Model customization ID", model ? model.customizationUuid : null),
390 ModelInformationItem.createInstance("Instance ID", instance ? instance.instanceId : null),
391 ModelInformationItem.createInstance("Instance type", instance ? instance.instanceType : null),
392 ModelInformationItem.createInstance("In maintenance", instance? instance.inMaint : null),
394 modelInfoItems = modelInfoItems.concat(modelInfoSpecificItems);
395 return this.getComponentInfoModelByModelInformationItems(modelInfoItems, type, instance);
398 getComponentInfoModelByModelInformationItems(modelInfoItems: ModelInformationItem[], type: ComponentInfoType, instance){
399 const modelInfoItemsWithoutEmpty = _.filter(modelInfoItems, function(item){ return !item.values.every(_.isNil)});
400 return new ComponentInfoModel(type, modelInfoItemsWithoutEmpty, [], instance != null);
403 createMaximumToInstantiateModelInformationItem(model): ModelInformationItem {
404 return ModelInformationItem.createInstance(
406 !_.isNil(model.max) ? String(model.max) : Constants.ModelInfo.UNLIMITED_DEFAULT