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