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);
57 hasMissingData(instance, dynamicInputs: any, isEcompGeneratedNaming: boolean, requiredFields: string[]): boolean {
58 if (!isEcompGeneratedNaming && _.isEmpty(instance.instanceName)) {
62 for (let field of requiredFields) {
63 if (_.isEmpty(instance[field])) {
68 for (let field of dynamicInputs) {
69 if (field.isRequired && !_.isNil(instance.instanceParams) && _.isEmpty(instance.instanceParams[0][field.id])) {
77 addingStatusProperty(node) {
78 node['statusProperties'] = [];
79 node['statusProperties'].push({key: 'Prov Status:', value: node.provStatus, testId: 'provStatus'});
80 node['statusProperties'].push({key: 'Orch Status:', value: node.orchStatus, testId: 'orchStatus'});
82 node['statusProperties'].push({key: 'In-maintenance', value: '', testId: 'inMaint'});
87 /**********************************************
88 * should delete or remove child instance's
89 "new" -> should remove
90 !new" -> should change action status
91 **********************************************/
92 removeDeleteAllChild(node, serviceModelId: string, callback): void {
93 for (let nodeChild of node.children) {
94 if (nodeChild.data.action === ServiceInstanceActions.Create) {
95 if (!_.isNil(nodeChild.data) && !_.isNil(nodeChild.data.menuActions) && !_.isNil(nodeChild.data.menuActions['remove'])) {
96 nodeChild.data.menuActions['remove']['method'](nodeChild, serviceModelId);
99 if (!_.isNil(nodeChild.data) && !_.isNil(nodeChild.data.menuActions) && !_.isNil(nodeChild.data.menuActions['delete'])) {
100 nodeChild.data.menuActions['delete']['method'](nodeChild, serviceModelId);
104 callback(node, serviceModelId);
108 /**********************************************
109 * should undo delete child instance's
110 **********************************************/
111 undoDeleteAllChild(node, serviceModelId: string, callback): void {
112 for (let nodeChild of node.children) {
113 if (!_.isNil(nodeChild.data) && !_.isNil(nodeChild.data.menuActions) && !_.isNil(nodeChild.data.menuActions['undoDelete'])) {
114 nodeChild.data.menuActions['undoDelete']['method'](nodeChild, serviceModelId);
117 callback(node, serviceModelId);
120 /**********************************************
121 * should return true if can delete
122 **********************************************/
123 shouldShowDelete(node): boolean {
124 return this.shouldShowButtonGeneric(node, "delete")
127 /**********************************************
128 * should return true if can undo delete
129 **********************************************/
130 shouldShowUndoDelete(node): boolean {
131 const mode = this._store.getState().global.drawingBoardStatus;
132 if (mode === DrawingBoardModes.EDIT && !_.isNil(node.data.action) && !_.isNil(node.data.menuActions['undoDelete'])) {
133 if (node.data.action === ServiceInstanceActions.Create || node.data.action === ServiceInstanceActions.Delete) {
135 } else if (node.data.action.split('_').pop() === 'Delete') {
142 /**********************************************
143 * should return true if can remove or edit
144 * enabled only on edit/design mode and for new instances
145 **********************************************/
146 shouldShowRemoveAndEdit(node): boolean {
147 const mode = this._store.getState().global.drawingBoardStatus;
148 if (!_.isNil(node) && !_.isNil(node.data) && !_.isNil(node.data.action) && node.data.action === ServiceInstanceActions.Create &&
149 mode !== DrawingBoardModes.VIEW && mode !== DrawingBoardModes.RETRY) {
154 /**********************************************
155 * enabled only on edit/design
156 * enabled only if there's a newer version for VNF-M
157 **********************************************/
158 upgradeBottomUp(node,serviceModelId: string): void {
159 this.iterateOverTreeBranchAndRunAction(node, serviceModelId, VNFMethods.UPGRADE);
160 this._store.dispatch(upgradeService(serviceModelId));
163 private iterateOverTreeBranchAndRunAction(node, serviceModelId: string, actionMethod) {
164 while (_.has(node.parent, 'data') && _.has(node.parent.data, 'menuActions')
165 && !_.isNil(node.parent.data.menuActions[actionMethod])) {
167 node.data.menuActions[actionMethod]['method'](node, serviceModelId);
171 /****************************************************
172 * should return true if customer can upgrade a VFM *
173 ****************************************************/
174 shouldShowUpgrade(node, serviceModelId): boolean {
175 if (FeatureFlagsService.getFlagState(Features.FLAG_FLASH_REPLACE_VF_MODULE, this._store) &&
176 this.isThereAnUpdatedLatestVersion(serviceModelId)) {
177 return this.shouldShowButtonGeneric(node, VNFMethods.UPGRADE);
184 private isThereAnUpdatedLatestVersion(serviceModelId) : boolean{
185 let serviceInstance = this._store.getState().service.serviceInstance[serviceModelId];
186 return !_.isNil(serviceInstance.latestAvailableVersion) && (Number(serviceInstance.modelInfo.modelVersion) < serviceInstance.latestAvailableVersion);
189 private shouldShowButtonGeneric(node, method) {
190 const mode = this._store.getState().global.drawingBoardStatus;
191 if (!_.isNil(node) && !_.isNil(node.data) && !_.isNil(node.data.action) && !_.isNil(node.data.menuActions[method])) {
192 if (mode !== DrawingBoardModes.EDIT || node.data.action === ServiceInstanceActions.Create) {
195 else if (node.data.action === ServiceInstanceActions.None) {
202 /**********************************************
203 * return boolean according to
204 * current defined action of VFModule node
205 **********************************************/
206 shouldShowUndoUpgrade(node): boolean {
207 const mode = this._store.getState().global.drawingBoardStatus;
208 if (mode === DrawingBoardModes.EDIT && !_.isNil(node.data.action) && !_.isNil(node.data.menuActions[VNFMethods.UNDO_UPGRADE])) {
209 if (node.data.action === ServiceInstanceActions.Upgrade) {
211 } else if (node.data.action.split('_').pop() === ServiceInstanceActions.Upgrade) {
218 /**********************************************
219 * enabled only on edit/design
220 * enabled only if there's a newer version for VNF-M
221 **********************************************/
222 undoUpgradeBottomUp(node,serviceModelId: string): void {
223 this.iterateOverTreeBranchAndRunAction(node, serviceModelId, VNFMethods.UNDO_UPGRADE);
224 this._store.dispatch(undoUpgradeService(serviceModelId));
226 /**********************************************
227 * should return true if can duplicate by mode
228 **********************************************/
229 shouldShowDuplicate(node): boolean {
230 const mode = this._store.getState().global.drawingBoardStatus;
231 return !mode.includes('RETRY');
234 /**********************************************
235 * should return true if can audit info
236 **********************************************/
237 shouldShowAuditInfo(node): boolean {
238 return this.isRetryMode() || (!_.isNil(node.data) && !_.isNil(node.data.action) && node.data.action !== ServiceInstanceActions.Create);
242 isRetryMode(): boolean {
243 const mode = this._store.getState().global.drawingBoardStatus;
244 return mode.includes('RETRY');
248 /**********************************************
249 * should return true if can add node instances
250 **********************************************/
251 shouldShowAddIcon(): boolean{
252 const mode = this._store.getState().global.drawingBoardStatus;
253 return mode === DrawingBoardModes.EDIT || mode=== DrawingBoardModes.CREATE || mode=== DrawingBoardModes.RECREATE;
257 isReachedToMaxInstances(properties, counter, flags): boolean{
258 let maxInstances = Utils.getMaxFirstLevel(properties, flags);
259 if(_.isNil(maxInstances)){
262 return !(maxInstances > counter);
265 /************************************************
266 return number of instances with action Delete
267 @type: vnfs networks, vngGroups (not vfModule)
268 @node : node model from the left tree
269 ************************************************/
270 getExistingInstancesWithDeleteMode(node, serviceModelId: string, type: string): number {
272 const existingInstances = this._store.getState().service.serviceInstance[serviceModelId][type];
273 const modelUniqueId = node.data.modelUniqueId;
274 if (!_.isNil(existingInstances)) {
275 for (let instanceKey in existingInstances) {
276 if (!_.isNil(existingInstances[instanceKey].action)) {
277 if (existingInstances[instanceKey].modelInfo.modelUniqueId === modelUniqueId && existingInstances[instanceKey].action.split('_').pop() === 'Delete') {
287 isServiceOnDeleteMode(serviceId: string): boolean {
288 return this._store.getState().service.serviceInstance[serviceId].action === ServiceInstanceActions.Delete;
292 openModal(node : any | any[] , serviceModelId : string, cb : Function) : void {
293 let type: string = _.isArray(node) ? 'Service' : node.data.typeName;
294 let messageBoxData: MessageBoxData = new MessageBoxData(
296 `You are about to mark for delete this ${type} this will also mark all its children and remove all new instances just added`,
301 text: "Mark and remove",
303 callback: cb.bind(this, node, serviceModelId),
306 {text: "Don’t Remove", size: "medium", closeModal: true}
309 MessageBoxService.openModal.next(messageBoxData);
312 someChildHasCreateAction(nodes: any | any[]) : boolean {
313 let nodesArr = _.isArray(nodes) ? nodes : [nodes];
314 for(const node of nodesArr){
315 if(node.action === ServiceInstanceActions.Create) {return true;}
317 for (let nodeChild of node.children) {
318 if (nodeChild.action === ServiceInstanceActions.Create) {
321 if(nodeChild.children && nodeChild.children.length > 0){
322 for(let child of nodeChild.children){
323 let hasCreateAction = this.someChildHasCreateAction(child);
324 if(hasCreateAction) {
335 shouldShowDeleteInstanceWithChildrenModal(node : any | any[] , serviceModelId : string, cb : Function) : void {
336 if(this.someChildHasCreateAction(node)){
337 this.openModal(node , serviceModelId, cb);
339 cb(node, serviceModelId)
344 isFailed(node): boolean {
345 return !_.isNil(node.data) ? node.data.isFailed : false;
348 /************************************************
349 in a case the node is failed e.g. not instantiated correctly
350 the function will call to openRetryInstanceAuditInfoModal
351 @node : node model from the left tree
352 @serviceModelId : serviceModelId
354 @instanceType: instanceType
355 @modelInfoService : the model (vnf, vfmodule, network, vnfgroup)object that call to the function (this)
356 ************************************************/
357 openAuditInfoModal(node, serviceModelId, instance, instanceType, modelInfoService : ILevelNodeInfo){
358 AuditInfoModalComponent.openInstanceAuditInfoModal.next({
359 instanceId: serviceModelId,
361 model: modelInfoService.getModel(node.data.modelName, instance, this._store.getState().service.serviceHierarchy[serviceModelId]),
367 addGeneralInfoItems(modelInfoSpecificItems: ModelInformationItem[], type: ComponentInfoType, model, instance):ComponentInfoModel {
368 let modelInfoItems: ModelInformationItem[] = [
369 ModelInformationItem.createInstance("Model version", model ? model.version : null),
370 ModelInformationItem.createInstance("Model customization ID", model ? model.customizationUuid : null),
371 ModelInformationItem.createInstance("Instance ID", instance ? instance.instanceId : null),
372 ModelInformationItem.createInstance("Instance type", instance ? instance.instanceType : null),
373 ModelInformationItem.createInstance("In maintenance", instance? instance.inMaint : null),
375 modelInfoItems = modelInfoItems.concat(modelInfoSpecificItems);
376 return this.getComponentInfoModelByModelInformationItems(modelInfoItems, type, instance);
379 getComponentInfoModelByModelInformationItems(modelInfoItems: ModelInformationItem[], type: ComponentInfoType, instance){
380 const modelInfoItemsWithoutEmpty = _.filter(modelInfoItems, function(item){ return !item.values.every(_.isNil)});
381 return new ComponentInfoModel(type, modelInfoItemsWithoutEmpty, [], instance != null);
384 createMaximumToInstantiateModelInformationItem(model): ModelInformationItem {
385 return ModelInformationItem.createInstance(
387 !_.isNil(model.max) ? String(model.max) : Constants.ModelInfo.UNLIMITED_DEFAULT