don't show delete in menu action while edit a macro service
[vid.git] / vid-webpack-master / src / app / drawingBoard / service-planning / objectsToTree / objectToInstanceTree / objectToInstanceTree.service.ts
1 import {Injectable} from "@angular/core";
2 import {ILevelNodeInfo} from "../models/basic.model.info";
3 import {ObjectToTreeService} from "../objectToTree.service";
4 import {DefaultDataGeneratorService} from "../../../../shared/services/defaultDataServiceGenerator/default.data.generator.service";
5 import * as _ from "lodash";
6 import {ServiceInstanceActions} from "../../../../shared/models/serviceInstanceActions";
7 import {ErrorMsgService} from "../../../../shared/components/error-msg/error-msg.service";
8 import {FeatureFlagsService, Features} from "../../../../shared/services/featureFlag/feature-flags.service";
9 import {NgRedux} from "@angular-redux/store";
10 import {AppState} from "../../../../shared/store/reducers";
11
12 @Injectable()
13 export class ObjectToInstanceTreeService {
14   constructor(private _objectToTreeService: ObjectToTreeService, private _errorMsgService: ErrorMsgService, private store: NgRedux<AppState>) {
15     this.numberOfFailed = 0;
16     this.numberOfElements = 0;
17
18   }
19
20   /** store number of failed ********  ONLY IN RETRY MODE  ******** **/
21   numberOfFailed: number = 0;
22
23   /** store number of existing elements **/
24   numberOfElements: number = 0;
25
26   /*****************************************************************
27    * return array of first level node with there child's
28    * @param serviceInstance - The service instance object from store
29    * @param serviceHierarchy - The service Hierarchy store
30    ****************************************************************/
31   convertServiceInstanceToTreeData(serviceInstance, serviceHierarchy): any[] {
32     this._errorMsgService.triggerClearError.next();
33     let nodes = [];
34     this.numberOfFailed = 0;
35     this.numberOfElements = 0;
36     let _this = this;
37     const serviceModelId:string = serviceInstance.modelInfo.modelVersionId;
38     const firstLevelOptions: ILevelNodeInfo[] = _this._objectToTreeService.getFirstLevelOptions();
39     for (let option of firstLevelOptions) {
40       _.forOwn(serviceInstance[option.name], function (instance, modelName) {
41         nodes.push(_this.getNodeInstance(modelName, null, instance, serviceHierarchy, option, serviceModelId));
42       });
43     }
44     return this.sortElementsByPosition(nodes);
45   }
46
47   /*****************************************************************
48    * should increase number of failed
49    * @param node - the current node
50    ****************************************************************/
51   increaseNumberOfFailed(node) {
52     if (node && node.isFailed) {
53       this.numberOfFailed++;
54       node['errors'] = !_.isNil(node['errors']) ? node['errors'] : {};
55       node['errors']["isFailed"] = true;
56       this._errorMsgService.triggerShowError.next(this._errorMsgService.getRetryErrorObject(this.numberOfFailed));
57     }
58   }
59
60   /*****************************************************************
61    * should increase number of existing elements
62    * @param node - the current node
63    ****************************************************************/
64   increaseNumberOfExcitingElements() {
65     this.numberOfElements++;
66   }
67
68   /*****************************************************************
69    * return array of first level node with there child's
70    * @param modelName
71    * @param parentModel
72    * @param instance
73    * @param serviceHierarchy - The service Hierarchy store
74    * @param option
75    * @param serviceModelId
76    * @param parentType
77    ****************************************************************/
78   getNodeInstance(modelName: string, parentModel: any, instance: any, serviceHierarchy, option: ILevelNodeInfo, serviceModelId: string, parentType ?: string) {
79     const model = option.getModel(modelName, instance, serviceHierarchy);
80
81     let optionalNodes = option.createInstanceTreeNode(instance, model, parentModel, modelName, serviceModelId);
82     this.increaseNumberOfFailed(optionalNodes);
83     this.increaseNumberOfExcitingElements();
84     let nodes: any[] = _.isArray(optionalNodes) ? optionalNodes : [optionalNodes];
85     for (let node of nodes) {
86       node = this.addingExtraDataToNode(node, modelName, parentModel, instance, serviceHierarchy, option, parentType);
87       let children = this.addNextInstanceTreeNode(instance, model, option, node, serviceHierarchy, serviceModelId);
88       if (!_.isNil(children) && children.length > 0) {
89         node.children = this.sortElementsByPosition(children);
90       }
91       this.updateScalingPolicy(node);
92     }
93     return nodes.length === 1 ? nodes[0] : nodes;
94   }
95
96   addingExtraDataToNode(node, modelName: string, parentModel: any, instance: any, serviceHierarchy, option: ILevelNodeInfo, parentType ?: string) {
97     if(!_.isNil(node)){
98       node.trackById = _.isNil(node.trackById) ? DefaultDataGeneratorService.createRandomTrackById() : node['trackById'];
99       node.parentType = !_.isNil(parentType) ? parentType : "";
100       node.updatePoistionFunction = option.updatePosition;
101       node.position = option.getNodePosition(instance, node.dynamicModelName);
102       node.getModel = option.getModel.bind(option);
103       node.getInfo = !_.isNil(option.getInfo) ? option.getInfo.bind(option) : ()=>{};
104       node.componentInfoType = option.componentInfoType;
105     }
106
107     return node;
108   }
109
110   sortElementsByPosition(nodes: any[]): any[] {
111     if (!FeatureFlagsService.getFlagState(Features.FLAG_1911_INSTANTIATION_ORDER_IN_ASYNC_ALACARTE, this.store)) return nodes;
112     return nodes.sort((nodeA, nodeB) => {
113       return nodeA.position - nodeB.position;
114     });
115   }
116
117   /*****************************************************************
118    * return next level node with there child's
119    * @param parentInstance
120    * @param parentModel
121    * @param levelNodeInfo
122    * @param parentNode
123    * @param serviceHierarchy - The service Hierarchy store
124    * @param serviceModelId
125    ****************************************************************/
126   addNextInstanceTreeNode(parentInstance, parentModel, levelNodeInfo: ILevelNodeInfo, parentNode, serviceHierarchy, serviceModelId: string): any[] {
127     if (!_.isNil(levelNodeInfo.childNames)&& levelNodeInfo.childNames.length > 0) {
128       const that = this;
129       parentNode.children = [];
130       levelNodeInfo.childNames.forEach(function (childName)  {
131         if (!_.isNil(parentInstance[childName])) {
132           let parentType = levelNodeInfo.type;
133           let nextLevelNodeInfo = levelNodeInfo.getNextLevelObject.apply(that, [childName]);
134           Object.keys(parentInstance[childName]).map((modelName) => {
135             let nextLevelInstance = parentInstance[childName][modelName];
136             let nodes: any[] | any = that.getNodeInstance(modelName, parentModel, nextLevelInstance, serviceHierarchy, nextLevelNodeInfo, serviceModelId, parentType);
137             if (_.isArray(nodes)) {
138               parentNode.children = parentNode.children.concat(nodes);
139             } else {
140               parentNode.children.push(nodes);
141             }
142           });
143         }
144       });
145       return this.sortElementsByPosition(parentNode.children);
146     }
147     return !_.isNil(parentNode) ? parentNode.children : null;
148   }
149
150
151   /************************************************************************************
152    * update instance scaling policy according to instance limit and existing children
153    * @param node
154    *********************************************************************************/
155   updateScalingPolicy(node): void {
156     if(_.isNil(node)) return node;
157     node['errors'] = !_.isNil(node['errors']) ? node['errors'] : {};
158     if (!_.isNil(node['limitMembers']) && !_.isNil(node.children)) {
159       let effectiveChildren = (node.children).filter(child => [
160         ServiceInstanceActions.Create,
161         ServiceInstanceActions.None,
162         ServiceInstanceActions.Update
163       ].includes(child.action));
164
165
166       if (effectiveChildren.length > node.limitMembers) {
167         node['errors']["scalingError"] = true;
168         this._errorMsgService.triggerShowError.next(this._errorMsgService.getScalingErrorObject());
169       } else {
170         delete node['errors']["scalingError"];
171       }
172
173     }
174   }
175 }