Sonsino, Ofir (os0695) | ff76b5e | 2018-07-10 15:57:37 +0300 | [diff] [blame] | 1 | import {Injectable} from '@angular/core'; |
| 2 | import {Constants} from "../shared/utils/constants"; |
| 3 | import {Utils} from "../utils/utils"; |
| 4 | import * as _ from 'lodash'; |
| 5 | import Parameter = Constants.Parameter; |
| 6 | import {ITreeNode} from "angular-tree-component/dist/defs/api"; |
| 7 | import {ServiceInstance} from "../shared/models/serviceInstance"; |
| 8 | import {VNFModel} from "../shared/models/vnfModel"; |
| 9 | import {ServiceNodeTypes} from "../shared/models/ServiceNodeTypes"; |
| 10 | import {VfModuleMap} from "../shared/models/vfModulesMap"; |
| 11 | import {VnfInstance} from "../shared/models/vnfInstance"; |
| 12 | import {VfModuleTreeNode} from "../shared/models/vfModuleTreeNode"; |
| 13 | import {VfModule} from "../shared/models/vfModule"; |
| 14 | import {VnfTreeNode} from "../shared/models/vnfTreeNode"; |
| 15 | import {NgRedux} from "@angular-redux/store"; |
| 16 | import {AppState} from "../store/reducers"; |
| 17 | import {InputType} from "../shared/models/inputTypes"; |
| 18 | |
| 19 | |
| 20 | @Injectable() |
| 21 | export class ServicePlanningService { |
| 22 | |
| 23 | modelDataTree: any[] = []; |
| 24 | drawingDataTree: any[] = []; |
| 25 | service: any = {name:'My Service'} ; |
| 26 | public requiredFields = { |
| 27 | VF: [InputType.LCP_REGION, InputType.TENANT, InputType.PLATFORM], |
| 28 | VFmodule: [] |
| 29 | }; |
| 30 | |
| 31 | constructor(private store: NgRedux<AppState>) {} |
| 32 | |
| 33 | |
| 34 | public getServiceName() :string{ |
| 35 | return this.service.name; |
| 36 | } |
| 37 | |
| 38 | public getServiceInstance(serviceModelId): ServiceInstance { |
| 39 | return this.store.getState().service.serviceInstance[serviceModelId]; |
| 40 | } |
| 41 | |
| 42 | public getVnfInstance(serviceModelId, vnfModelName): VnfInstance { |
| 43 | return this.getServiceInstance(serviceModelId).vnfs[vnfModelName]; |
| 44 | } |
| 45 | |
| 46 | public getVfModuleMap(serviceModelId, vnfModelName, vfModuleModelName): VfModuleMap { |
| 47 | let vnfInstance = this.getVnfInstance(serviceModelId, vnfModelName); |
| 48 | return _.get(vnfInstance, ['vfModules', vfModuleModelName]); |
| 49 | } |
| 50 | |
| 51 | public convertServiceModelToTreeNodes(serviceModel) { |
| 52 | let _this = this; |
| 53 | |
| 54 | _.forOwn(serviceModel.vnfs, function(item, key) { |
| 55 | _this.addFirstLevelModel(key, item, item.type, serviceModel); |
| 56 | }); |
| 57 | |
| 58 | _.forOwn(serviceModel.configurations, function(item, key) { |
| 59 | _this.addFirstLevelModel(key, item, ServiceNodeTypes.Configuration, serviceModel); |
| 60 | }); |
| 61 | |
| 62 | _.forOwn(serviceModel.networks, function(network, key) { |
| 63 | _this.addFirstLevelModel(key, network, ServiceNodeTypes.Network, serviceModel); |
| 64 | }); |
| 65 | |
| 66 | return this.modelDataTree; |
| 67 | } |
| 68 | |
| 69 | private addFirstLevelModel(key, value, valueType, serviceModel) { |
| 70 | |
| 71 | let node = this.convertItemToTreeNode(key, value, valueType, null, false); |
| 72 | let vnfInstance = this.getVnfInstance(serviceModel.service.uuid, key); |
| 73 | if(value.vfModules) { |
| 74 | node.children = Object.keys(value.vfModules).map((vmKey) => |
| 75 | this.convertItemToTreeNode(vmKey, value.vfModules[vmKey], ServiceNodeTypes.VFmodule, value, !vnfInstance)); |
| 76 | } |
| 77 | this.modelDataTree.push(node); |
| 78 | } |
| 79 | |
| 80 | private convertItemToTreeNode(key, value, valueType, parentModel , disabled) { |
| 81 | |
| 82 | return { |
| 83 | id: value.uuid, |
| 84 | name: key, |
| 85 | tooltip: valueType, |
| 86 | type: valueType, |
| 87 | count: value.count || 0, |
| 88 | max: value.max || 1, |
| 89 | children: [], |
| 90 | disabled: disabled, |
| 91 | dynamicInputs: this.updateDynamicInputsVnfDataFromModel(valueType, value), |
| 92 | userProvidedNaming: this.isUserProvidedNaming(valueType, value, parentModel) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | public convertServiceInstanceToTreeData(serviceInstance: ServiceInstance, modelId: string): any { |
| 97 | let drawingBoardData = []; |
| 98 | let _this = this; |
| 99 | _.forOwn(serviceInstance.vnfs, (vnfInstance, vnfModelName) => { |
| 100 | let vnfModel: VNFModel = _this.store.getState().service.serviceHierarchy[modelId].vnfs[vnfModelName]; |
| 101 | let vnfNode = new VnfTreeNode(vnfInstance, vnfModel); |
| 102 | |
| 103 | let vfModuleNodes = []; |
| 104 | _.forOwn(vnfInstance.vfModules, (vfModuleMap, vfModuleModelName) => { |
| 105 | _.forOwn(vfModuleMap, (vfModuleInstance, vfModuleInstsanceName) => { |
| 106 | let vfModule: VfModule = _this.store.getState().service.serviceHierarchy[modelId].vnfs[vnfModelName].vfModules[vfModuleModelName]; |
| 107 | let vfModuleNode: VfModuleTreeNode = new VfModuleTreeNode(vfModuleInstance, vfModule, vfModuleModelName); |
| 108 | vfModuleNodes.push(vfModuleNode); |
| 109 | }); |
| 110 | }); |
| 111 | vnfNode.children = vfModuleNodes; |
| 112 | drawingBoardData.push(vnfNode); |
| 113 | }); |
| 114 | |
| 115 | return drawingBoardData; |
| 116 | } |
| 117 | |
| 118 | public getArbitraryInputs(inputs) { |
| 119 | let parameter; |
| 120 | let parameterList = []; |
| 121 | for (let key in inputs) { |
| 122 | parameter = { |
| 123 | id : key, |
| 124 | type : Parameter.STRING, |
| 125 | name : key, |
| 126 | value : inputs[key][Parameter.DEFAULT], |
| 127 | isRequired : inputs[key][Parameter.REQUIRED], |
| 128 | description : inputs[key][Parameter.DESCRIPTION] |
| 129 | }; |
| 130 | switch (inputs[key][Parameter.TYPE]) { |
| 131 | case Parameter.INTEGER: |
| 132 | parameter.type = Parameter.NUMBER; |
| 133 | break; |
| 134 | case Parameter.BOOLEAN: |
| 135 | parameter.type = Parameter.BOOLEAN; |
| 136 | break; |
| 137 | case Parameter.RANGE: |
| 138 | break; |
| 139 | case Parameter.LIST: |
| 140 | parameter.type = Parameter.LIST; |
| 141 | break; |
| 142 | case Parameter.MAP: |
| 143 | parameter.type = Parameter.MAP; |
| 144 | break; |
| 145 | } |
| 146 | if ( Utils.hasContents(inputs[key][Parameter.CONSTRAINTS]) |
| 147 | && ( inputs[key][Parameter.CONSTRAINTS].length > 0 ) ) { |
| 148 | let constraintsArray = inputs[key][Parameter.CONSTRAINTS]; |
| 149 | this.addConstraintParameters (parameterList, constraintsArray, key, inputs, parameter); |
| 150 | } |
| 151 | else { |
| 152 | |
| 153 | parameterList.push(parameter); |
| 154 | } |
| 155 | } |
| 156 | return parameterList; |
| 157 | } |
| 158 | |
| 159 | private addConstraintParameters(parameterList, constraintsArray, key, inputs, parameter) { |
| 160 | // If there are constraints and the operator is "valid_values", |
| 161 | // use a select parameter type. |
| 162 | let i:number = constraintsArray.length; |
| 163 | let parameterPushed: boolean = false; |
| 164 | if ( i > 0 ) { |
| 165 | while ( (i--) && (!parameterPushed) ) { |
| 166 | let keys = Object.keys(constraintsArray[i]); |
| 167 | for ( let operator in keys ) { |
| 168 | switch (keys[operator]) { |
| 169 | case Parameter.VALID_VALUES: |
| 170 | let j: number = constraintsArray[i][Parameter.VALID_VALUES].length; |
| 171 | if ( j > 0 ) { |
| 172 | let oList = []; |
| 173 | let option; |
| 174 | while (j--) { |
| 175 | option = { |
| 176 | name: constraintsArray[i][Parameter.VALID_VALUES][j], |
| 177 | isDefault: false |
| 178 | }; |
| 179 | if ( (Utils.hasContents (inputs[key][Parameter.DEFAULT]) ) |
| 180 | && (inputs[key][Parameter.DEFAULT] === constraintsArray[i][Parameter.VALID_VALUES][j] ) ) { |
| 181 | option = { |
| 182 | name: constraintsArray[i][Parameter.VALID_VALUES][j], |
| 183 | isDefault: true |
| 184 | } |
| 185 | } |
| 186 | oList.push(option); |
| 187 | } |
| 188 | parameter.type = Parameter.SELECT; |
| 189 | parameter.optionList = oList; |
| 190 | parameterList.push(parameter); |
| 191 | parameterPushed = true; |
| 192 | } |
| 193 | break; |
| 194 | |
| 195 | case Parameter.EQUAL: |
| 196 | if ( constraintsArray[i][Parameter.EQUAL] != null ) { |
| 197 | //override parameter type |
| 198 | parameter.type = Parameter.STRING; |
| 199 | parameter.isReadOnly = true; |
| 200 | parameter.value = constraintsArray[i][Parameter.EQUAL]; |
| 201 | parameterList.push(parameter); |
| 202 | parameterPushed = true; |
| 203 | } |
| 204 | break; |
| 205 | |
| 206 | case Parameter.LENGTH: |
| 207 | if ( constraintsArray[i][Parameter.LENGTH] != null ) { |
| 208 | parameter.minLength = constraintsArray[i][Parameter.LENGTH]; |
| 209 | parameter.maxLength = constraintsArray[i][Parameter.LENGTH]; |
| 210 | parameterList.push(parameter); |
| 211 | parameterPushed = true; |
| 212 | } |
| 213 | break; |
| 214 | case Parameter.MAX_LENGTH: |
| 215 | if ( constraintsArray[i][Parameter.MAX_LENGTH] != null ) { |
| 216 | parameter.maxLength = constraintsArray[i][Parameter.MAX_LENGTH]; |
| 217 | parameterList.push(parameter); |
| 218 | parameterPushed = true; |
| 219 | } |
| 220 | break; |
| 221 | case Parameter.MIN_LENGTH: |
| 222 | if ( constraintsArray[i][Parameter.MIN_LENGTH] != null ) { |
| 223 | parameter.minLength = constraintsArray[i][Parameter.MIN_LENGTH]; |
| 224 | parameterList.push(parameter); |
| 225 | parameterPushed = true; |
| 226 | } |
| 227 | break; |
| 228 | case Parameter.IN_RANGE: |
| 229 | if ( constraintsArray[i][Parameter.IN_RANGE] != null ) { |
| 230 | if (constraintsArray[i][Parameter.IN_RANGE].length > 1 ) { |
| 231 | parameter.min = constraintsArray[i][Parameter.IN_RANGE][0]; |
| 232 | parameter.max = constraintsArray[i][Parameter.IN_RANGE][1]; |
| 233 | parameter.type = Parameter.NUMBER; |
| 234 | parameter.value = inputs[key][Parameter.DEFAULT]; |
| 235 | parameterList.push(parameter); |
| 236 | parameterPushed = true; |
| 237 | } |
| 238 | } |
| 239 | break; |
| 240 | case Parameter.GREATER_THAN: |
| 241 | if ( constraintsArray[i][Parameter.GREATER_THAN] != null ) { |
| 242 | parameter.type = Parameter.NUMBER; |
| 243 | parameter.min = constraintsArray[i][Parameter.GREATER_THAN]; |
| 244 | parameter.value = inputs[key][Parameter.DEFAULT]; |
| 245 | parameterList.push(parameter); |
| 246 | parameterPushed = true; |
| 247 | } |
| 248 | break; |
| 249 | }//switch |
| 250 | }//for |
| 251 | }//while |
| 252 | }//if |
| 253 | }; |
| 254 | |
| 255 | public static isVfModule(node:ITreeNode): boolean { |
| 256 | return node.data.type=='VFmodule'; |
| 257 | } |
| 258 | |
| 259 | public static isVnf(node:ITreeNode): boolean { |
| 260 | return node.data.type == ServiceNodeTypes.VF; |
| 261 | } |
| 262 | |
| 263 | updateDynamicInputsVnfDataFromModel(modelType: string, model: any): Array<any> { |
| 264 | let displayInputs; |
| 265 | if (modelType === ServiceNodeTypes.VFmodule) { |
| 266 | displayInputs = model.inputs; |
| 267 | } |
| 268 | return _.isEmpty(displayInputs) ? [] : this.getArbitraryInputs(displayInputs); |
| 269 | } |
| 270 | |
| 271 | isUserProvidedNaming(type: string, nodeModel: any, parentModel: any) : boolean { |
| 272 | let model; |
| 273 | if (type === ServiceNodeTypes.VFmodule) { |
| 274 | model = parentModel; |
| 275 | } |
| 276 | else { |
| 277 | model = nodeModel; |
| 278 | } |
| 279 | const ecompGeneratedNaming = model.properties.ecomp_generated_naming; |
| 280 | return ecompGeneratedNaming !== undefined && ecompGeneratedNaming === "false"; |
| 281 | } |
| 282 | } |