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";
18 export class SharedTreeService {
19 private _sharedTreeService: SharedTreeService;
20 constructor(private _store: NgRedux<AppState>) {
23 /***********************************************************
24 * return if instance has missing data
25 * @param instance - vnf instance
26 * @param dynamicInputs - from the instance
27 * @param isEcompGeneratedNaming
28 ************************************************************/
29 selectedVNF: string = null;
32 getSelectedVNF(): string {
33 return this.selectedVNF;
36 setSelectedVNF(node): void {
37 if (_.isNil(node) || node.data.type !== 'VF') {
38 this.selectedVNF = null;
40 this.selectedVNF = node.data.vnfStoreKey;
44 hasMissingData(instance, dynamicInputs: any, isEcompGeneratedNaming: boolean, requiredFields: string[]): boolean {
45 if (!isEcompGeneratedNaming && _.isEmpty(instance.instanceName)) {
49 for (let field of requiredFields) {
50 if (_.isEmpty(instance[field])) {
55 for (let field of dynamicInputs) {
56 if (field.isRequired && !_.isNil(instance.instanceParams) && _.isEmpty(instance.instanceParams[0][field.id])) {
64 addingStatusProperty(node) {
65 node['statusProperties'] = [];
66 node['statusProperties'].push({key: 'Prov Status:', value: node.provStatus, testId: 'provStatus'});
67 node['statusProperties'].push({key: 'Orch Status:', value: node.orchStatus, testId: 'orchStatus'});
69 node['statusProperties'].push({key: 'In-maintenance', value: '', testId: 'inMaint'});
74 /**********************************************
75 * should delete or remove child instance's
76 "new" -> should remove
77 !new" -> should change action status
78 **********************************************/
79 removeDeleteAllChild(node, serviceModelId: string, callback): void {
80 for (let nodeChild of node.children) {
81 if (nodeChild.data.action === ServiceInstanceActions.Create) {
82 if (!_.isNil(nodeChild.data) && !_.isNil(nodeChild.data.menuActions) && !_.isNil(nodeChild.data.menuActions['remove'])) {
83 nodeChild.data.menuActions['remove']['method'](nodeChild, serviceModelId);
86 if (!_.isNil(nodeChild.data) && !_.isNil(nodeChild.data.menuActions) && !_.isNil(nodeChild.data.menuActions['delete'])) {
87 nodeChild.data.menuActions['delete']['method'](nodeChild, serviceModelId);
91 callback(node, serviceModelId);
95 /**********************************************
96 * should undo delete child instance's
97 **********************************************/
98 undoDeleteAllChild(node, serviceModelId: string, callback): void {
99 for (let nodeChild of node.children) {
100 if (!_.isNil(nodeChild.data) && !_.isNil(nodeChild.data.menuActions) && !_.isNil(nodeChild.data.menuActions['undoDelete'])) {
101 nodeChild.data.menuActions['undoDelete']['method'](nodeChild, serviceModelId);
104 callback(node, serviceModelId);
107 /**********************************************
108 * should return true if can delete
109 **********************************************/
110 shouldShowDelete(node): boolean {
111 return this.shouldShowButtonGeneric(node, "delete")
114 /**********************************************
115 * should return true if can undo delete
116 **********************************************/
117 shouldShowUndoDelete(node): boolean {
118 const mode = this._store.getState().global.drawingBoardStatus;
119 if (mode === DrawingBoardModes.EDIT && !_.isNil(node.data.action) && !_.isNil(node.data.menuActions['undoDelete'])) {
120 if (node.data.action === ServiceInstanceActions.Create || node.data.action === ServiceInstanceActions.Delete) {
122 } else if (node.data.action.split('_').pop() === 'Delete') {
129 /**********************************************
130 * should return true if can remove or edit
131 * enabled only on edit/design mode and for new instances
132 **********************************************/
133 shouldShowRemoveAndEdit(node): boolean {
134 const mode = this._store.getState().global.drawingBoardStatus;
135 if (!_.isNil(node) && !_.isNil(node.data) && !_.isNil(node.data.action) && node.data.action === ServiceInstanceActions.Create &&
136 mode !== DrawingBoardModes.VIEW && mode !== DrawingBoardModes.RETRY) {
141 /**********************************************
142 * enabled only on edit/design
143 * enabled only if there's a newer version for VNF-M
144 **********************************************/
145 upgradeBottomUp(node,serviceModelId: string): void {
146 this.iterateOverTreeBranchAndRunAction(node, serviceModelId, VNFMethods.UPGRADE);
147 this._store.dispatch(upgradeService(serviceModelId));
150 private iterateOverTreeBranchAndRunAction(node, serviceModelId: string, actionMethod) {
151 while (_.has(node.parent, 'data') && _.has(node.parent.data, 'menuActions')
152 && !_.isNil(node.parent.data.menuActions[actionMethod])) {
154 node.data.menuActions[actionMethod]['method'](node, serviceModelId);
158 /****************************************************
159 * should return true if customer can upgrade a VFM *
160 ****************************************************/
161 shouldShowUpgrade(node, serviceModelId): boolean {
162 if (FeatureFlagsService.getFlagState(Features.FLAG_FLASH_REPLACE_VF_MODULE, this._store) &&
163 this.isThereAnUpdatedLatestVersion(serviceModelId)) {
164 return this.shouldShowButtonGeneric(node, VNFMethods.UPGRADE);
171 private isThereAnUpdatedLatestVersion(serviceModelId) : boolean{
172 let serviceInstance = this._store.getState().service.serviceInstance[serviceModelId];
173 return !_.isNil(serviceInstance.latestAvailableVersion) && (Number(serviceInstance.modelInfo.modelVersion) < serviceInstance.latestAvailableVersion);
176 private shouldShowButtonGeneric(node, method) {
177 const mode = this._store.getState().global.drawingBoardStatus;
178 if (!_.isNil(node) && !_.isNil(node.data) && !_.isNil(node.data.action) && !_.isNil(node.data.menuActions[method])) {
179 if (mode !== DrawingBoardModes.EDIT || node.data.action === ServiceInstanceActions.Create) {
182 else if (node.data.action === ServiceInstanceActions.None) {
189 /**********************************************
190 * return boolean according to
191 * current defined action of VFModule node
192 **********************************************/
193 shouldShowUndoUpgrade(node): boolean {
194 const mode = this._store.getState().global.drawingBoardStatus;
195 if (mode === DrawingBoardModes.EDIT && !_.isNil(node.data.action) && !_.isNil(node.data.menuActions[VNFMethods.UNDO_UPGRADE])) {
196 if (node.data.action === ServiceInstanceActions.Upgrade) {
198 } else if (node.data.action.split('_').pop() === ServiceInstanceActions.Upgrade) {
205 /**********************************************
206 * enabled only on edit/design
207 * enabled only if there's a newer version for VNF-M
208 **********************************************/
209 undoUpgradeBottomUp(node,serviceModelId: string): void {
210 this.iterateOverTreeBranchAndRunAction(node, serviceModelId, VNFMethods.UNDO_UPGRADE);
211 this._store.dispatch(undoUpgradeService(serviceModelId));
213 /**********************************************
214 * should return true if can duplicate by mode
215 **********************************************/
216 shouldShowDuplicate(node): boolean {
217 const mode = this._store.getState().global.drawingBoardStatus;
218 return !mode.includes('RETRY');
221 /**********************************************
222 * should return true if can audit info
223 **********************************************/
224 shouldShowAuditInfo(node): boolean {
225 return this.isRetryMode() || (!_.isNil(node.data) && !_.isNil(node.data.action) && node.data.action !== ServiceInstanceActions.Create);
229 isRetryMode(): boolean {
230 const mode = this._store.getState().global.drawingBoardStatus;
231 return mode.includes('RETRY');
235 /**********************************************
236 * should return true if can add node instances
237 **********************************************/
238 shouldShowAddIcon(): boolean{
239 const mode = this._store.getState().global.drawingBoardStatus;
240 return mode === DrawingBoardModes.EDIT || mode=== DrawingBoardModes.CREATE;
242 /************************************************
243 return number of instances with action Delete
244 @type: vnfs networks, vngGroups (not vfModule)
245 @node : node model from the left tree
246 ************************************************/
247 getExistingInstancesWithDeleteMode(node, serviceModelId: string, type: string): number {
249 const existingInstances = this._store.getState().service.serviceInstance[serviceModelId][type];
250 const modelUniqueId = node.data.modelUniqueId;
251 if (!_.isNil(existingInstances)) {
252 for (let instanceKey in existingInstances) {
253 if (!_.isNil(existingInstances[instanceKey].action)) {
254 if (existingInstances[instanceKey].modelInfo.modelUniqueId === modelUniqueId && existingInstances[instanceKey].action.split('_').pop() === 'Delete') {
264 isServiceOnDeleteMode(serviceId: string): boolean {
265 return this._store.getState().service.serviceInstance[serviceId].action === ServiceInstanceActions.Delete;
269 openModal(node : any | any[] , serviceModelId : string, cb : Function) : void {
270 let type: string = _.isArray(node) ? 'Service' : node.data.typeName;
271 let messageBoxData: MessageBoxData = new MessageBoxData(
273 `You are about to mark for delete this ${type} this will also mark all its children and remove all new instances just added`,
278 text: "Mark and remove",
280 callback: cb.bind(this, node, serviceModelId),
283 {text: "Don’t Remove", size: "medium", closeModal: true}
286 MessageBoxService.openModal.next(messageBoxData);
289 someChildHasCreateAction(nodes: any | any[]) : boolean {
290 let nodesArr = _.isArray(nodes) ? nodes : [nodes];
291 for(const node of nodesArr){
292 if(node.action === ServiceInstanceActions.Create) {return true;}
294 for (let nodeChild of node.children) {
295 if (nodeChild.action === ServiceInstanceActions.Create) {
298 if(nodeChild.children && nodeChild.children.length > 0){
299 for(let child of nodeChild.children){
300 let hasCreateAction = this.someChildHasCreateAction(child);
301 if(hasCreateAction) {
312 shouldShowDeleteInstanceWithChildrenModal(node : any | any[] , serviceModelId : string, cb : Function) : void {
313 if(this.someChildHasCreateAction(node)){
314 this.openModal(node , serviceModelId, cb);
316 cb(node, serviceModelId)
321 isFailed(node): boolean {
322 return !_.isNil(node.data) ? node.data.isFailed : false;
325 /************************************************
326 in a case the node is failed e.g. not instantiated correctly
327 the function will call to openRetryInstanceAuditInfoModal
328 @node : node model from the left tree
329 @serviceModelId : serviceModelId
331 @instanceType: instanceType
332 @modelInfoService : the model (vnf, vfmodule, network, vnfgroup)object that call to the function (this)
333 ************************************************/
334 openAuditInfoModal(node, serviceModelId, instance, instanceType, modelInfoService : ILevelNodeInfo){
335 AuditInfoModalComponent.openInstanceAuditInfoModal.next({
336 instanceId: serviceModelId,
338 model: modelInfoService.getModel(node.data.modelName, instance, this._store.getState().service.serviceHierarchy[serviceModelId]),
344 addGeneralInfoItems(modelInfoSpecificItems: ModelInformationItem[], type: ComponentInfoType, model, instance):ComponentInfoModel {
345 let modelInfoItems: ModelInformationItem[] = [
346 ModelInformationItem.createInstance("Model version", model ? model.version : null),
347 ModelInformationItem.createInstance("Model customization ID", model ? model.customizationUuid : null),
348 ModelInformationItem.createInstance("Instance ID", instance ? instance.instanceId : null),
349 ModelInformationItem.createInstance("Instance type", instance ? instance.instanceType : null),
350 ModelInformationItem.createInstance("In maintenance", instance? instance.inMaint : null),
352 modelInfoItems = modelInfoItems.concat(modelInfoSpecificItems);
353 return this.getComponentInfoModelByModelInformationItems(modelInfoItems, type, instance);
356 getComponentInfoModelByModelInformationItems(modelInfoItems: ModelInformationItem[], type: ComponentInfoType, instance){
357 const modelInfoItemsWithoutEmpty = _.filter(modelInfoItems, function(item){ return !item.values.every(_.isNil)});
358 return new ComponentInfoModel(type, modelInfoItemsWithoutEmpty, [], instance != null);