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 import {SharedTreeService} from "../shared.tree.service";
14 export class ObjectToInstanceTreeService {
15 constructor(private _objectToTreeService: ObjectToTreeService, private _errorMsgService: ErrorMsgService,
16 private store: NgRedux<AppState>, private _sharedTreeService: SharedTreeService) {
17 this.numberOfFailed = 0;
18 this.numberOfElements = 0;
22 /** store number of failed ******** ONLY IN RETRY MODE ******** **/
23 numberOfFailed: number = 0;
25 /** store number of existing elements **/
26 numberOfElements: number = 0;
28 /*****************************************************************
29 * return array of first level node with there child's
30 * @param serviceInstance - The service instance object from store
31 * @param serviceHierarchy - The service Hierarchy store
32 ****************************************************************/
33 convertServiceInstanceToTreeData(serviceInstance, serviceHierarchy): any[] {
34 this._errorMsgService.triggerClearError.next();
36 this.numberOfFailed = 0;
37 this.numberOfElements = 0;
39 //const serviceModelId:string = serviceInstance.modelInfo.modelVersionId;
40 const serviceModelId:string = serviceHierarchy.service.uuid;
41 const firstLevelOptions: ILevelNodeInfo[] = _this._objectToTreeService.getFirstLevelOptions(serviceInstance.isALaCarte);
42 for (let option of firstLevelOptions) {
43 _.forOwn(serviceInstance[option.name], function (instance, modelName) {
44 nodes.push(_this.getNodeInstance(modelName, null, instance, serviceHierarchy, option, serviceModelId));
47 return this.sortElementsByPosition(nodes);
50 /*****************************************************************
51 * should increase number of failed
52 * @param node - the current node
53 ****************************************************************/
54 increaseNumberOfFailed(node) {
55 if (node && node.isFailed) {
56 this.numberOfFailed++;
57 node['errors'] = !_.isNil(node['errors']) ? node['errors'] : {};
58 node['errors']["isFailed"] = true;
59 this._errorMsgService.triggerShowError.next(this._errorMsgService.getRetryErrorObject(this.numberOfFailed));
63 /*****************************************************************
64 * should increase number of existing elements
65 * @param node - the current node
66 ****************************************************************/
67 increaseNumberOfExcitingElements() {
68 this.numberOfElements++;
71 /*****************************************************************
72 * return array of first level node with there child's
76 * @param serviceHierarchy - The service Hierarchy store
78 * @param serviceModelId
80 ****************************************************************/
81 getNodeInstance(modelName: string, parentModel: any, instance: any, serviceHierarchy, option: ILevelNodeInfo, serviceModelId: string, parentType ?: string) {
82 const instanceModel = this._sharedTreeService.modelByIdentifiers(
83 serviceHierarchy, option.name,
84 this._sharedTreeService.modelUniqueNameOrId(instance), modelName
86 const model = option.getModel(instanceModel);
88 let optionalNodes = option.createInstanceTreeNode(instance, model, parentModel, modelName, serviceModelId);
89 this.increaseNumberOfFailed(optionalNodes);
90 this.increaseNumberOfExcitingElements();
91 let nodes: any[] = _.isArray(optionalNodes) ? optionalNodes : [optionalNodes];
92 for (let node of nodes) {
93 node = this.addingExtraDataToNode(node, modelName, parentModel, instance, serviceHierarchy, option, parentType);
94 let children = this.addNextInstanceTreeNode(instance, model, option, node, serviceHierarchy, serviceModelId);
95 if (!_.isNil(children) && children.length > 0) {
96 node.children = this.sortElementsByPosition(children);
98 this.updateScalingPolicy(node);
100 return nodes.length === 1 ? nodes[0] : nodes;
103 addingExtraDataToNode(node, modelName: string, parentModel: any, instance: any, serviceHierarchy, option: ILevelNodeInfo, parentType ?: string) {
105 node.trackById = _.isNil(node.trackById) ? DefaultDataGeneratorService.createRandomTrackById() : node['trackById'];
106 node.parentType = !_.isNil(parentType) ? parentType : "";
107 node.updatePoistionFunction = option.updatePosition;
108 node.position = option.getNodePosition(instance, node.dynamicModelName);
109 node.modelTypeName = option.name;
110 node.getModel = option.getModel.bind(option);
111 node.getInfo = !_.isNil(option.getInfo) ? option.getInfo.bind(option) : ()=>{};
112 node.componentInfoType = option.componentInfoType;
118 sortElementsByPosition(nodes: any[]): any[] {
119 if (!FeatureFlagsService.getFlagState(Features.FLAG_1911_INSTANTIATION_ORDER_IN_ASYNC_ALACARTE, this.store)) return nodes;
120 return nodes.sort((nodeA, nodeB) => {
121 return nodeA.position - nodeB.position;
125 /*****************************************************************
126 * return next level node with there child's
127 * @param parentInstance
129 * @param levelNodeInfo
131 * @param serviceHierarchy - The service Hierarchy store
132 * @param serviceModelId
133 ****************************************************************/
134 addNextInstanceTreeNode(parentInstance, parentModel, levelNodeInfo: ILevelNodeInfo, parentNode, serviceHierarchy, serviceModelId: string): any[] {
135 if (!_.isNil(levelNodeInfo.childNames)&& levelNodeInfo.childNames.length > 0) {
137 parentNode.children = [];
138 levelNodeInfo.childNames.forEach(function (childName) {
139 if (!_.isNil(parentInstance[childName])) {
140 let parentType = levelNodeInfo.type;
141 let nextLevelNodeInfo = levelNodeInfo.getNextLevelObject.apply(that, [childName]);
142 Object.keys(parentInstance[childName]).map((modelName) => {
143 let nextLevelInstance = parentInstance[childName][modelName];
144 let nodes: any[] | any = that.getNodeInstance(modelName, parentModel, nextLevelInstance, serviceHierarchy, nextLevelNodeInfo, serviceModelId, parentType);
145 if (_.isArray(nodes)) {
146 parentNode.children = parentNode.children.concat(nodes);
148 parentNode.children.push(nodes);
153 return this.sortElementsByPosition(parentNode.children);
155 return !_.isNil(parentNode) ? parentNode.children : null;
159 /************************************************************************************
160 * update instance scaling policy according to instance limit and existing children
162 *********************************************************************************/
163 updateScalingPolicy(node): void {
164 if(_.isNil(node)) return node;
165 node['errors'] = !_.isNil(node['errors']) ? node['errors'] : {};
166 if (!_.isNil(node['limitMembers']) && !_.isNil(node.children)) {
167 let effectiveChildren = (node.children).filter(child => [
168 ServiceInstanceActions.Create,
169 ServiceInstanceActions.None,
170 ServiceInstanceActions.Update
171 ].includes(child.action));
174 if (effectiveChildren.length > node.limitMembers) {
175 node['errors']["scalingError"] = true;
176 this._errorMsgService.triggerShowError.next(this._errorMsgService.getScalingErrorObject());
178 delete node['errors']["scalingError"];