Sonsino, Ofir (os0695) | ff76b5e | 2018-07-10 15:57:37 +0300 | [diff] [blame] | 1 | import { Constants } from '../shared/utils/constants'; |
| 2 | import Parameter = Constants.Parameter; |
| 3 | import * as _ from 'lodash' |
| 4 | |
| 5 | export class Utils { |
| 6 | |
| 7 | public static clampNumber = (number, min, max) => { |
| 8 | return Math.max(min, Math.min(number, max)); |
| 9 | }; |
| 10 | |
| 11 | public static hasContents(object: Object): boolean { |
| 12 | if (object === undefined || object === null || object === "") { |
| 13 | return false; |
| 14 | } |
| 15 | return true; |
| 16 | }; |
| 17 | |
| 18 | public static convertModel(serviceModel) { |
| 19 | |
| 20 | let isNewFlow:boolean = false; |
| 21 | |
| 22 | for (let networkCustomizationName in serviceModel.networks) { |
| 23 | let networkModel = serviceModel.networks[networkCustomizationName]; |
| 24 | if ( networkModel.customizationUuid != null ) { |
| 25 | isNewFlow = true; |
| 26 | break; |
| 27 | } |
| 28 | } |
| 29 | if ( !isNewFlow ) { |
| 30 | for (let vnfCustomizationName in serviceModel.vnfs) { |
| 31 | let vnfModel = serviceModel.vnfs[vnfCustomizationName]; |
| 32 | if ( vnfModel.customizationUuid != null ) { |
| 33 | isNewFlow = true; |
| 34 | break; |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | if ( isNewFlow ) { |
| 39 | return (Utils.convertNewModel (serviceModel) ); |
| 40 | } |
| 41 | else { |
| 42 | return (Utils.convertOldModel (serviceModel) ); |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | private static convertNewModel (serviceModel ) { |
| 47 | let completeResources = new Array(); |
| 48 | let resource = {}; |
| 49 | let convertedAsdcModel = { |
| 50 | "service": serviceModel.service, |
| 51 | "networks": {}, |
| 52 | "vnfs": {}, |
| 53 | "pnfs": serviceModel.pnfs, |
| 54 | "serviceProxies": serviceModel.serviceProxies, |
| 55 | "completeDisplayInputs": {}, |
| 56 | "isNewFlow": true |
| 57 | }; |
| 58 | |
| 59 | for(let key in serviceModel.service.inputs) { |
| 60 | if(_.includes(["instance_node_target", "naming_policy", "vf_instance_name"], key)) { |
| 61 | delete convertedAsdcModel.service.inputs[key]; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | for (let networkCustomizationName in serviceModel.networks) { |
| 66 | let networkModel = serviceModel.networks[networkCustomizationName]; |
| 67 | |
| 68 | convertedAsdcModel.networks[networkModel.customizationUuid] = { |
| 69 | "uuid": networkModel.uuid, |
| 70 | "invariantUuid": networkModel.invariantUuid, |
| 71 | "version": networkModel.version, |
| 72 | "name": networkModel.name, |
| 73 | "modelCustomizationName": networkModel.modelCustomizationName, |
| 74 | "customizationUuid": networkModel.customizationUuid, |
| 75 | "inputs": "", |
| 76 | "description": networkModel.description, |
| 77 | "commands": {}, |
| 78 | "displayInputs": {} |
| 79 | }; |
| 80 | |
| 81 | resource = { |
| 82 | "name": networkModel.modelCustomizationName, |
| 83 | "description": networkModel.description |
| 84 | }; |
| 85 | |
| 86 | completeResources.push (resource); |
| 87 | |
| 88 | } |
| 89 | |
| 90 | _.forEach(serviceModel.configurations, function(element) { |
| 91 | element.isConfig = true; |
| 92 | }); |
| 93 | _.forEach(serviceModel.pnfs, function(element, key) { |
| 94 | element.isPnf= true; |
| 95 | element.modelCustomizationName= key; |
| 96 | }); |
| 97 | let mergedVnfs = Object.assign(serviceModel.vnfs, serviceModel.configurations, serviceModel.pnfs); |
| 98 | |
| 99 | for (let vnfCustomizationName in mergedVnfs) { |
| 100 | let vnfModel = mergedVnfs[vnfCustomizationName]; |
| 101 | let vnfCustomizationUuid = vnfModel.customizationUuid; |
| 102 | convertedAsdcModel.vnfs[vnfModel.customizationUuid] = { |
| 103 | "uuid": vnfModel.uuid, |
| 104 | "invariantUuid": vnfModel.invariantUuid, |
| 105 | "version": vnfModel.version, |
| 106 | "name": vnfModel.name, |
| 107 | "modelCustomizationName": vnfModel.modelCustomizationName, |
| 108 | "customizationUuid": vnfModel.customizationUuid, |
| 109 | "inputs": "", |
| 110 | "description": vnfModel.description, |
| 111 | "vfModules": {}, |
| 112 | "volumeGroups": {}, |
| 113 | "commands": {}, |
| 114 | "displayInputs": {}, |
| 115 | "properties": {}, |
| 116 | "nfRole": "", |
| 117 | "nfType": "", |
| 118 | "sourceNodes": vnfModel.sourceNodes, |
| 119 | "collectorNodes": vnfModel.collectorNodes, |
| 120 | "isConfigurationByPolicy": vnfModel.configurationByPolicy ? vnfModel.configurationByPolicy : false, |
| 121 | "isConfig": vnfModel.isConfig ? vnfModel.isConfig : false, |
| 122 | "isPnf": vnfModel.isPnf ? vnfModel.isPnf : false |
| 123 | }; |
| 124 | |
| 125 | resource = { |
| 126 | "name": vnfModel.modelCustomizationName, |
| 127 | "description": vnfModel.description |
| 128 | }; |
| 129 | completeResources.push (resource); |
| 130 | |
| 131 | if (vnfModel.commands != null) { |
| 132 | /* |
| 133 | * commands: { |
| 134 | * internal_net_param_ntu: { |
| 135 | * command: get_input, |
| 136 | * displaName: internal_net_param_ntu, |
| 137 | * inputName: vccfd1_internal_net_param_ntu // pointer to input key |
| 138 | * } |
| 139 | * If the input name (ptr) is one of instance_node_target, naming_policy or vf_instance_name |
| 140 | * then ignore it |
| 141 | * |
| 142 | */ |
| 143 | |
| 144 | convertedAsdcModel.vnfs[vnfCustomizationUuid].properties=vnfModel.properties; |
| 145 | // |
| 146 | let vnf_type = ""; |
| 147 | let vnf_role = ""; |
| 148 | let vnf_function = ""; |
| 149 | let vnf_code = ""; |
| 150 | if ( !( _.isEmpty(vnfModel.properties) ) ) { |
| 151 | if (this.hasContents (vnfModel.properties.nf_type) ) { |
| 152 | vnf_type = vnfModel.properties.nf_type; |
| 153 | } |
| 154 | if (this.hasContents (vnfModel.properties.nf_role) ) { |
| 155 | vnf_role = vnfModel.properties.nf_role; |
| 156 | } |
| 157 | if (this.hasContents (vnfModel.properties.nf_function) ) { |
| 158 | vnf_function = vnfModel.properties.nf_function; |
| 159 | } |
| 160 | if (this.hasContents (vnfModel.properties.nf_naming_code) ) { |
| 161 | vnf_code = vnfModel.properties.nf_naming_code; |
| 162 | } |
| 163 | } |
| 164 | convertedAsdcModel.vnfs[vnfCustomizationUuid]["nfType"] = vnf_type; |
| 165 | convertedAsdcModel.vnfs[vnfCustomizationUuid]["nfRole"] = vnf_role; |
| 166 | convertedAsdcModel.vnfs[vnfCustomizationUuid]["nfFunction"] = vnf_function; |
| 167 | convertedAsdcModel.vnfs[vnfCustomizationUuid]["nfCode"] = vnf_code; |
| 168 | // |
| 169 | for (let vfModuleCustomizationName in serviceModel.vnfs[vnfCustomizationName].vfModules) { |
| 170 | let vfModuleModel = serviceModel.vnfs[vnfCustomizationName].vfModules[vfModuleCustomizationName]; |
| 171 | convertedAsdcModel.vnfs[vnfCustomizationUuid].vfModules[vfModuleModel.customizationUuid] = vfModuleModel; |
| 172 | } |
| 173 | |
| 174 | for (let volumeGroupCustomizationName in serviceModel.vnfs[vnfCustomizationName].volumeGroups) { |
| 175 | let volumeGroupModel = serviceModel.vnfs[vnfCustomizationName].volumeGroups[volumeGroupCustomizationName]; |
| 176 | convertedAsdcModel.vnfs[vnfCustomizationUuid].volumeGroups[volumeGroupModel.customizationUuid] = volumeGroupModel; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | return (convertedAsdcModel); |
| 182 | }; |
| 183 | |
| 184 | private static convertOldModel(serviceModel ) { |
| 185 | let resource = {}; |
| 186 | let convertedAsdcModel = { |
| 187 | "service": serviceModel.service, |
| 188 | "networks": {}, |
| 189 | "vnfs": {}, |
| 190 | "pnfs": serviceModel.pnfs, |
| 191 | "serviceProxies": serviceModel.serviceProxies, |
| 192 | "completeDisplayInputs": {}, |
| 193 | "isNewFlow": false |
| 194 | }; |
| 195 | let completeResources = new Array(); |
| 196 | for (let networkCustomizationName in serviceModel.networks) { |
| 197 | let networkModel = serviceModel.networks[networkCustomizationName]; |
| 198 | convertedAsdcModel.networks[networkModel.invariantUuid] = {}; |
| 199 | //convertedAsdcModel.networks[networkModel.invariantUuid][networkModel.version] = networkModel; |
| 200 | // need a network model to test this |
| 201 | convertedAsdcModel.networks[networkModel.uuid] = { |
| 202 | "uuid": networkModel.uuid, |
| 203 | "invariantUuid": networkModel.invariantUuid, |
| 204 | "version": networkModel.version, |
| 205 | "name": networkModel.name, |
| 206 | "modelCustomizationName": networkModel.modelCustomizationName, |
| 207 | "customizationUuid": networkModel.customizationUuid, |
| 208 | "inputs": "", |
| 209 | "description": networkModel.description, |
| 210 | "commands": {}, |
| 211 | "displayInputs": {} |
| 212 | }; |
| 213 | resource = { |
| 214 | "name": networkModel.modelCustomizationName, |
| 215 | "description": networkModel.description |
| 216 | }; |
| 217 | completeResources.push (resource); |
| 218 | } |
| 219 | |
| 220 | _.forEach(serviceModel.configurations, function(element) { |
| 221 | element.isConfig = true; |
| 222 | }); |
| 223 | _.forEach(serviceModel.pnfs, function(element, key) { |
| 224 | element.isPnf= true; |
| 225 | element.modelCustomizationName= key; |
| 226 | }); |
| 227 | let mergedVnfs = Object.assign(serviceModel.vnfs, serviceModel.configurations, serviceModel.pnfs); |
| 228 | |
| 229 | for (let vnfCustomizationName in mergedVnfs) { |
| 230 | let vnfModel = mergedVnfs[vnfCustomizationName]; |
| 231 | convertedAsdcModel.vnfs[vnfModel.uuid] = { |
| 232 | "uuid": vnfModel.uuid, |
| 233 | "invariantUuid": vnfModel.invariantUuid, |
| 234 | "version": vnfModel.version, |
| 235 | "name": vnfModel.name, |
| 236 | "modelCustomizationName": vnfModel.modelCustomizationName, |
| 237 | "customizationUuid": vnfModel.customizationUuid, |
| 238 | "inputs": "", |
| 239 | "description": vnfModel.description, |
| 240 | "vfModules": {}, |
| 241 | "volumeGroups": {}, |
| 242 | "commands": {}, |
| 243 | "displayInputs": {}, |
| 244 | "sourceNodes": vnfModel.sourceNodes, |
| 245 | "collectorNodes": vnfModel.collectorNodes, |
| 246 | "isConfigurationByPolicy": vnfModel.configurationByPolicy ? vnfModel.configurationByPolicy : false, |
| 247 | "isConfig": vnfModel.isConfig ? vnfModel.isConfig : false, |
| 248 | "isPnf": vnfModel.isPnf ? vnfModel.isPnf : false |
| 249 | }; |
| 250 | resource = { |
| 251 | "name": vnfModel.modelCustomizationName, |
| 252 | "description": vnfModel.description |
| 253 | }; |
| 254 | completeResources.push (resource); |
| 255 | |
| 256 | for (let vfModuleCustomizationName in serviceModel.vnfs[vnfCustomizationName].vfModules) { |
| 257 | let vfModuleModel = serviceModel.vnfs[vnfCustomizationName].vfModules[vfModuleCustomizationName]; |
| 258 | convertedAsdcModel.vnfs[vnfModel.uuid].vfModules[vfModuleModel.uuid] = vfModuleModel; |
| 259 | } |
| 260 | |
| 261 | for (let volumeGroupCustomizationName in serviceModel.vnfs[vnfCustomizationName].volumeGroups) { |
| 262 | let volumeGroupModel = serviceModel.vnfs[vnfCustomizationName].volumeGroups[volumeGroupCustomizationName]; |
| 263 | convertedAsdcModel.vnfs[vnfModel.uuid].volumeGroups[volumeGroupModel.uuid] = volumeGroupModel; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | let completeDisplayInputs = {}; |
| 268 | |
| 269 | return (convertedAsdcModel); |
| 270 | }; |
| 271 | } |