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";
20 export class SharedTreeService {
21 private _sharedTreeService: 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;
46 hasMissingData(instance, dynamicInputs: any, isEcompGeneratedNaming: boolean, requiredFields: string[]): boolean {
47 if (!isEcompGeneratedNaming && _.isEmpty(instance.instanceName)) {
51 for (let field of requiredFields) {
52 if (_.isEmpty(instance[field])) {
57 for (let field of dynamicInputs) {
58 if (field.isRequired && !_.isNil(instance.instanceParams) && _.isEmpty(instance.instanceParams[0][field.id])) {
66 addingStatusProperty(node) {
67 node['statusProperties'] = [];
68 node['statusProperties'].push({key: 'Prov Status:', value: node.provStatus, testId: 'provStatus'});
69 node['statusProperties'].push({key: 'Orch Status:', value: node.orchStatus, testId: 'orchStatus'});
71 node['statusProperties'].push({key: 'In-maintenance', value: '', testId: 'inMaint'});
76 /**********************************************
77 * should delete or remove child instance's
78 "new" -> should remove
79 !new" -> should change action status
80 **********************************************/
81 removeDeleteAllChild(node, serviceModelId: string, callback): void {
82 for (let nodeChild of node.children) {
83 if (nodeChild.data.action === ServiceInstanceActions.Create) {
84 if (!_.isNil(nodeChild.data) && !_.isNil(nodeChild.data.menuActions) && !_.isNil(nodeChild.data.menuActions['remove'])) {
85 nodeChild.data.menuActions['remove']['method'](nodeChild, serviceModelId);
88 if (!_.isNil(nodeChild.data) && !_.isNil(nodeChild.data.menuActions) && !_.isNil(nodeChild.data.menuActions['delete'])) {
89 nodeChild.data.menuActions['delete']['method'](nodeChild, serviceModelId);
93 callback(node, serviceModelId);
97 /**********************************************
98 * should undo delete child instance's
99 **********************************************/
100 undoDeleteAllChild(node, serviceModelId: string, callback): void {
101 for (let nodeChild of node.children) {
102 if (!_.isNil(nodeChild.data) && !_.isNil(nodeChild.data.menuActions) && !_.isNil(nodeChild.data.menuActions['undoDelete'])) {
103 nodeChild.data.menuActions['undoDelete']['method'](nodeChild, serviceModelId);
106 callback(node, serviceModelId);
109 /**********************************************
110 * should return true if can delete
111 **********************************************/
112 shouldShowDelete(node): boolean {
113 return this.shouldShowButtonGeneric(node, "delete")
116 /**********************************************
117 * should return true if can undo delete
118 **********************************************/
119 shouldShowUndoDelete(node): boolean {
120 const mode = this._store.getState().global.drawingBoardStatus;
121 if (mode === DrawingBoardModes.EDIT && !_.isNil(node.data.action) && !_.isNil(node.data.menuActions['undoDelete'])) {
122 if (node.data.action === ServiceInstanceActions.Create || node.data.action === ServiceInstanceActions.Delete) {
124 } else if (node.data.action.split('_').pop() === 'Delete') {
131 /**********************************************
132 * should return true if can remove or edit
133 * enabled only on edit/design mode and for new instances
134 **********************************************/
135 shouldShowRemoveAndEdit(node): boolean {
136 const mode = this._store.getState().global.drawingBoardStatus;
137 if (!_.isNil(node) && !_.isNil(node.data) && !_.isNil(node.data.action) && node.data.action === ServiceInstanceActions.Create &&
138 mode !== DrawingBoardModes.VIEW && mode !== DrawingBoardModes.RETRY) {
143 /**********************************************
144 * enabled only on edit/design
145 * enabled only if there's a newer version for VNF-M
146 **********************************************/
147 upgradeBottomUp(node,serviceModelId: string): void {
148 this.iterateOverTreeBranchAndRunAction(node, serviceModelId, VNFMethods.UPGRADE);
149 this._store.dispatch(upgradeService(serviceModelId));
152 private iterateOverTreeBranchAndRunAction(node, serviceModelId: string, actionMethod) {
153 while (_.has(node.parent, 'data') && _.has(node.parent.data, 'menuActions')
154 && !_.isNil(node.parent.data.menuActions[actionMethod])) {
156 node.data.menuActions[actionMethod]['method'](node, serviceModelId);
160 /****************************************************
161 * should return true if customer can upgrade a VFM *
162 ****************************************************/
163 shouldShowUpgrade(node, serviceModelId): boolean {
164 if (FeatureFlagsService.getFlagState(Features.FLAG_FLASH_REPLACE_VF_MODULE, this._store) &&
165 this.isThereAnUpdatedLatestVersion(serviceModelId)) {
166 return this.shouldShowButtonGeneric(node, VNFMethods.UPGRADE);
173 private isThereAnUpdatedLatestVersion(serviceModelId) : boolean{
174 let serviceInstance = this._store.getState().service.serviceInstance[serviceModelId];
175 return !_.isNil(serviceInstance.latestAvailableVersion) && (Number(serviceInstance.modelInfo.modelVersion) < serviceInstance.latestAvailableVersion);
178 private shouldShowButtonGeneric(node, method) {
179 const mode = this._store.getState().global.drawingBoardStatus;
180 if (!_.isNil(node) && !_.isNil(node.data) && !_.isNil(node.data.action) && !_.isNil(node.data.menuActions[method])) {
181 if (mode !== DrawingBoardModes.EDIT || node.data.action === ServiceInstanceActions.Create) {
184 else if (node.data.action === ServiceInstanceActions.None) {
191 /**********************************************
192 * return boolean according to
193 * current defined action of VFModule node
194 **********************************************/
195 shouldShowUndoUpgrade(node): boolean {
196 const mode = this._store.getState().global.drawingBoardStatus;
197 if (mode === DrawingBoardModes.EDIT && !_.isNil(node.data.action) && !_.isNil(node.data.menuActions[VNFMethods.UNDO_UPGRADE])) {
198 if (node.data.action === ServiceInstanceActions.Upgrade) {
200 } else if (node.data.action.split('_').pop() === ServiceInstanceActions.Upgrade) {
207 /**********************************************
208 * enabled only on edit/design
209 * enabled only if there's a newer version for VNF-M
210 **********************************************/
211 undoUpgradeBottomUp(node,serviceModelId: string): void {
212 this.iterateOverTreeBranchAndRunAction(node, serviceModelId, VNFMethods.UNDO_UPGRADE);
213 this._store.dispatch(undoUpgradeService(serviceModelId));
215 /**********************************************
216 * should return true if can duplicate by mode
217 **********************************************/
218 shouldShowDuplicate(node): boolean {
219 const mode = this._store.getState().global.drawingBoardStatus;
220 return !mode.includes('RETRY');
223 /**********************************************
224 * should return true if can audit info
225 **********************************************/
226 shouldShowAuditInfo(node): boolean {
227 return this.isRetryMode() || (!_.isNil(node.data) && !_.isNil(node.data.action) && node.data.action !== ServiceInstanceActions.Create);
231 isRetryMode(): boolean {
232 const mode = this._store.getState().global.drawingBoardStatus;
233 return mode.includes('RETRY');
237 /**********************************************
238 * should return true if can add node instances
239 **********************************************/
240 shouldShowAddIcon(): boolean{
241 const mode = this._store.getState().global.drawingBoardStatus;
242 return mode === DrawingBoardModes.EDIT || mode=== DrawingBoardModes.CREATE;
246 isReachedToMaxInstances(properties, counter, flags): boolean{
247 let maxInstances = Utils.getMaxFirstLevel(properties, flags);
248 if(_.isNil(maxInstances)){
251 return !(maxInstances > counter);
254 /************************************************
255 return number of instances with action Delete
256 @type: vnfs networks, vngGroups (not vfModule)
257 @node : node model from the left tree
258 ************************************************/
259 getExistingInstancesWithDeleteMode(node, serviceModelId: string, type: string): number {
261 const existingInstances = this._store.getState().service.serviceInstance[serviceModelId][type];
262 const modelUniqueId = node.data.modelUniqueId;
263 if (!_.isNil(existingInstances)) {
264 for (let instanceKey in existingInstances) {
265 if (!_.isNil(existingInstances[instanceKey].action)) {
266 if (existingInstances[instanceKey].modelInfo.modelUniqueId === modelUniqueId && existingInstances[instanceKey].action.split('_').pop() === 'Delete') {
276 isServiceOnDeleteMode(serviceId: string): boolean {
277 return this._store.getState().service.serviceInstance[serviceId].action === ServiceInstanceActions.Delete;
281 openModal(node : any | any[] , serviceModelId : string, cb : Function) : void {
282 let type: string = _.isArray(node) ? 'Service' : node.data.typeName;
283 let messageBoxData: MessageBoxData = new MessageBoxData(
285 `You are about to mark for delete this ${type} this will also mark all its children and remove all new instances just added`,
290 text: "Mark and remove",
292 callback: cb.bind(this, node, serviceModelId),
295 {text: "Don’t Remove", size: "medium", closeModal: true}
298 MessageBoxService.openModal.next(messageBoxData);
301 someChildHasCreateAction(nodes: any | any[]) : boolean {
302 let nodesArr = _.isArray(nodes) ? nodes : [nodes];
303 for(const node of nodesArr){
304 if(node.action === ServiceInstanceActions.Create) {return true;}
306 for (let nodeChild of node.children) {
307 if (nodeChild.action === ServiceInstanceActions.Create) {
310 if(nodeChild.children && nodeChild.children.length > 0){
311 for(let child of nodeChild.children){
312 let hasCreateAction = this.someChildHasCreateAction(child);
313 if(hasCreateAction) {
324 shouldShowDeleteInstanceWithChildrenModal(node : any | any[] , serviceModelId : string, cb : Function) : void {
325 if(this.someChildHasCreateAction(node)){
326 this.openModal(node , serviceModelId, cb);
328 cb(node, serviceModelId)
333 isFailed(node): boolean {
334 return !_.isNil(node.data) ? node.data.isFailed : false;
337 /************************************************
338 in a case the node is failed e.g. not instantiated correctly
339 the function will call to openRetryInstanceAuditInfoModal
340 @node : node model from the left tree
341 @serviceModelId : serviceModelId
343 @instanceType: instanceType
344 @modelInfoService : the model (vnf, vfmodule, network, vnfgroup)object that call to the function (this)
345 ************************************************/
346 openAuditInfoModal(node, serviceModelId, instance, instanceType, modelInfoService : ILevelNodeInfo){
347 AuditInfoModalComponent.openInstanceAuditInfoModal.next({
348 instanceId: serviceModelId,
350 model: modelInfoService.getModel(node.data.modelName, instance, this._store.getState().service.serviceHierarchy[serviceModelId]),
356 addGeneralInfoItems(modelInfoSpecificItems: ModelInformationItem[], type: ComponentInfoType, model, instance):ComponentInfoModel {
357 let modelInfoItems: ModelInformationItem[] = [
358 ModelInformationItem.createInstance("Model version", model ? model.version : null),
359 ModelInformationItem.createInstance("Model customization ID", model ? model.customizationUuid : null),
360 ModelInformationItem.createInstance("Instance ID", instance ? instance.instanceId : null),
361 ModelInformationItem.createInstance("Instance type", instance ? instance.instanceType : null),
362 ModelInformationItem.createInstance("In maintenance", instance? instance.inMaint : null),
364 modelInfoItems = modelInfoItems.concat(modelInfoSpecificItems);
365 return this.getComponentInfoModelByModelInformationItems(modelInfoItems, type, instance);
368 getComponentInfoModelByModelInformationItems(modelInfoItems: ModelInformationItem[], type: ComponentInfoType, instance){
369 const modelInfoItemsWithoutEmpty = _.filter(modelInfoItems, function(item){ return !item.values.every(_.isNil)});
370 return new ComponentInfoModel(type, modelInfoItemsWithoutEmpty, [], instance != null);
373 createMaximumToInstantiateModelInformationItem(model): ModelInformationItem {
374 return ModelInformationItem.createInstance(
376 !_.isNil(model.max) ? String(model.max) : Constants.ModelInfo.UNLIMITED_DEFAULT