2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package com.att.bpm.scripts
22 import org.camunda.bpm.engine.delegate.BpmnError
23 import org.camunda.bpm.engine.runtime.Execution
25 import org.openecomp.mso.rest.APIResponse;
26 import org.openecomp.mso.rest.RESTClient
27 import org.openecomp.mso.rest.RESTConfig
31 public MsoUtils utils = new MsoUtils()
32 public static final String NETWORK_GENERIC_VNF = 'aai_network_generic_vnf_uri'
33 public static final String NETWORK_VPN_BINDING = 'aai_network_vpn_binding_uri'
34 public static final String NETWORK_POLICY = 'aai_network_policy_uri'
35 public static final String NETWORK_VCE = 'aai_network_vce_uri'
36 public static final String NETWORK_L3_NETWORK = 'aai_network_l3_network_uri'
37 public static final String NETWORK_TABLE_REFERENCES = 'aai_network_table_reference_uri'
38 public static final String BUSINESS_CUSTOMER = 'aai_business_customer_uri'
39 public static final String BUSINESS_CUSTOMERV7 = 'aaiv7_business_customer_uri'
40 public static final String CLOUD_INFRASTRUCTURE_VOLUME_GROUP = 'aai_cloud_infrastructure_volume_group_uri'
41 public static final String CLOUD_INFRASTRUCTURE_CLOUD_REGION = 'aai_cloud_infrastructure_cloud_region_uri'
42 public static final String CLOUD_INFRASTRUCTURE_TENANT = 'aai_cloud_infrastructure_tenant_uri'
43 public static final String SEARCH_GENERIC_QUERY = 'aai_search_generic_query_uri'
44 public static final String SEARCH_NODES_QUERY = 'aai_search_nodes_query_uri'
45 public static final String AAI_NAMESPACE_STRING = 'http://org.openecomp.aai.inventory/'
47 private AbstractServiceTaskProcessor taskProcessor
49 public AaiUtil(AbstractServiceTaskProcessor taskProcessor) {
50 this.taskProcessor = taskProcessor
53 public String getNetworkGenericVnfEndpoint(Execution execution) {
54 def method = getClass().getSimpleName() + '.getNetworkGenericVnfUri(' +
55 'execution=' + execution.getId() +
57 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
58 taskProcessor.logDebug('Entered ' + method, isDebugLogEnabled)
60 String endpoint = execution.getVariable("URN_aai_endpoint")
61 def processKey = taskProcessor.getProcessKey(execution)
62 def uri = getUri(execution, NETWORK_GENERIC_VNF)
64 taskProcessor.logDebug('Exited ' + method, isDebugLogEnabled)
68 public String getNetworkGenericVnfUri(Execution execution) {
69 def method = getClass().getSimpleName() + '.getNetworkGenericVnfUri(' +
70 'execution=' + execution.getId() +
72 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
73 taskProcessor.logDebug('Entered ' + method, isDebugLogEnabled)
75 def processKey = taskProcessor.getProcessKey(execution)
76 def uri = getUri(execution, NETWORK_GENERIC_VNF)
78 taskProcessor.logDebug('Exited ' + method, isDebugLogEnabled)
84 public String getNetworkVpnBindingUri(Execution execution) {
85 def method = getClass().getSimpleName() + '.getNetworkVpnBindingUri(' +
86 'execution=' + execution.getId() +
88 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
89 taskProcessor.logDebug('Entered ' + method, isDebugLogEnabled)
91 def processKey = taskProcessor.getProcessKey(execution)
92 def uri = getUri(execution, NETWORK_VPN_BINDING)
94 taskProcessor.logDebug('Exited ' + method, isDebugLogEnabled)
98 public String getNetworkPolicyUri(Execution execution) {
99 def method = getClass().getSimpleName() + '.getNetworkPolicyUri(' +
100 'execution=' + execution.getId() +
102 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
103 taskProcessor.logDebug('Entered ' + method, isDebugLogEnabled)
105 def processKey = taskProcessor.getProcessKey(execution)
106 def uri = getUri(execution, NETWORK_POLICY)
108 taskProcessor.logDebug('Exited ' + method, isDebugLogEnabled)
112 public String getNetworkTableReferencesUri(Execution execution) {
113 def method = getClass().getSimpleName() + '.getNetworkTableReferencesUri(' +
114 'execution=' + execution.getId() +
116 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
117 taskProcessor.logDebug('Entered ' + method, isDebugLogEnabled)
119 def processKey = taskProcessor.getProcessKey(execution)
120 def uri = getUri(execution, NETWORK_TABLE_REFERENCES)
122 taskProcessor.logDebug('Exited ' + method, isDebugLogEnabled)
126 public String getNetworkVceUri(Execution execution) {
127 def method = getClass().getSimpleName() + '.getNetworkVceUri(' +
128 'execution=' + execution.getId() +
130 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
131 taskProcessor.logDebug('Entered ' + method, isDebugLogEnabled)
133 def processKey = taskProcessor.getProcessKey(execution)
134 def uri = getUri(execution, NETWORK_VCE)
136 taskProcessor.logDebug('Exited ' + method, isDebugLogEnabled)
140 public String getNetworkL3NetworkUri(Execution execution) {
141 def method = getClass().getSimpleName() + '.getNetworkL3NetworkUri(' +
142 'execution=' + execution.getId() +
144 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
145 taskProcessor.logDebug('Entered ' + method, isDebugLogEnabled)
147 def processKey = taskProcessor.getProcessKey(execution)
148 def uri = getUri(execution, NETWORK_L3_NETWORK)
150 taskProcessor.logDebug('Exited ' + method, isDebugLogEnabled)
154 public String getBusinessCustomerUri(Execution execution) {
155 def method = getClass().getSimpleName() + '.getBusinessCustomerUri(' +
156 'execution=' + execution.getId() +
158 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
159 taskProcessor.logDebug('Entered ' + method, isDebugLogEnabled)
161 def processKey = taskProcessor.getProcessKey(execution)
162 def uri = getUri(execution, BUSINESS_CUSTOMER)
164 taskProcessor.logDebug('Exited ' + method, isDebugLogEnabled)
168 public String getBusinessCustomerUriv7(Execution execution) {
169 def method = getClass().getSimpleName() + '.getBusinessCustomerUriv7(' +
170 'execution=' + execution.getId() +
172 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
173 taskProcessor.logDebug('Entered ' + method, isDebugLogEnabled)
175 def processKey = taskProcessor.getProcessKey(execution)
176 def uri = getUri(execution, BUSINESS_CUSTOMERV7)
178 taskProcessor.logDebug('Exited ' + method, isDebugLogEnabled)
182 public String getCloudInfrastructureCloudRegionEndpoint(Execution execution) {
183 def method = getClass().getSimpleName() + '.getCloudInfrastructureCloudRegionUri(' +
184 'execution=' + execution.getId() +
186 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
187 taskProcessor.logDebug('Entered ' + method, isDebugLogEnabled)
189 String endpoint = execution.getVariable("URN_aai_endpoint")
191 def processKey = taskProcessor.getProcessKey(execution)
192 def uri = getUri(execution, CLOUD_INFRASTRUCTURE_CLOUD_REGION)
194 taskProcessor.logDebug('Exited ' + method, isDebugLogEnabled)
195 return endpoint + uri
199 * This method is depracated, use getCloudInfrastructureRegionEndpoint instead
202 public String getCloudInfrastructureCloudRegionUri(Execution execution) {
203 def method = getClass().getSimpleName() + '.getCloudInfrastructureCloudRegionUri(' +
204 'execution=' + execution.getId() +
206 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
207 taskProcessor.logDebug('Entered ' + method, isDebugLogEnabled)
209 def processKey = taskProcessor.getProcessKey(execution)
210 def uri = getUri(execution, CLOUD_INFRASTRUCTURE_CLOUD_REGION)
212 taskProcessor.logDebug('Exited ' + method, isDebugLogEnabled)
216 public String getCloudInfrastructureVolumeGroupEndpoint(Execution execution) {
217 def method = getClass().getSimpleName() + '.getCloudInfrastructureVolumeGroupUri(' +
218 'execution=' + execution.getId() +
220 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
221 taskProcessor.logDebug('Entered ' + method, isDebugLogEnabled)
223 String endpoint = execution.getVariable("URN_aai_endpoint")
225 def processKey = taskProcessor.getProcessKey(execution)
226 def uri = getUri(execution, CLOUD_INFRASTRUCTURE_VOLUME_GROUP)
228 taskProcessor.logDebug('Exited ' + method, isDebugLogEnabled)
229 return endpoint + uri
232 public String getCloudInfrastructureVolumeGroupUri(Execution execution) {
233 def method = getClass().getSimpleName() + '.getCloudInfrastructureVolumeGroupUri(' +
234 'execution=' + execution.getId() +
236 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
237 taskProcessor.logDebug('Entered ' + method, isDebugLogEnabled)
239 def processKey = taskProcessor.getProcessKey(execution)
240 def uri = getUri(execution, CLOUD_INFRASTRUCTURE_VOLUME_GROUP)
242 taskProcessor.logDebug('Exited ' + method, isDebugLogEnabled)
246 public String getCloudInfrastructureTenantUri(Execution execution) {
247 def method = getClass().getSimpleName() + '.getCloudInfrastructureTenantUri(' +
248 'execution=' + execution.getId() +
250 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
251 taskProcessor.logDebug('Entered ' + method, isDebugLogEnabled)
253 def processKey = taskProcessor.getProcessKey(execution)
254 def uri = getUri(execution, CLOUD_INFRASTRUCTURE_TENANT)
256 taskProcessor.logDebug('Exited ' + method, isDebugLogEnabled)
260 public String getSearchNodesQueryUri(Execution execution) {
261 def method = getClass().getSimpleName() + '.getSearchNodesQueryUri(' +
262 'execution=' + execution.getId() +
264 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
265 taskProcessor.logDebug('Entered ' + method, isDebugLogEnabled)
267 def processKey = taskProcessor.getProcessKey(execution)
268 def uri = getUri(execution, SEARCH_NODES_QUERY)
270 taskProcessor.logDebug('Exited ' + method, isDebugLogEnabled)
274 public String getSearchNodesQueryEndpoint(Execution execution) {
275 def method = getClass().getSimpleName() + '.getSearchNodesQueryUri(' +
276 'execution=' + execution.getId() +
278 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
279 taskProcessor.logDebug('Entered ' + method, isDebugLogEnabled)
281 String endpoint = execution.getVariable("URN_aai_endpoint")
283 def processKey = taskProcessor.getProcessKey(execution)
284 def uri = getUri(execution, SEARCH_NODES_QUERY)
286 taskProcessor.logDebug('Exited ' + method, isDebugLogEnabled)
287 return endpoint + uri
290 public String getSearchGenericQueryUri(Execution execution) {
291 def method = getClass().getSimpleName() + '.getSearchGenericQueryUri(' +
292 'execution=' + execution.getId() +
294 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
295 taskProcessor.logDebug('Entered ' + method, isDebugLogEnabled)
297 def processKey = taskProcessor.getProcessKey(execution)
298 def uri = getUri(execution, SEARCH_GENERIC_QUERY)
300 taskProcessor.logDebug('Exited ' + method, isDebugLogEnabled)
304 public String getUri(Execution execution, String key) {
305 def method = getClass().getSimpleName() + '.getUri(' +
306 'execution=' + execution.getId() +
308 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
309 taskProcessor.logDebug('Entered ' + method, isDebugLogEnabled)
311 def processKey = taskProcessor.getProcessKey(execution)
312 def uriKey = "URN_mso_workflow_${processKey}_${key}"
313 def defaultUriKey = "URN_mso_workflow_default_${key}"
315 taskProcessor.logDebug('URI Key ' + uriKey, isDebugLogEnabled)
316 taskProcessor.logDebug('Default URI Key ' + defaultUriKey, isDebugLogEnabled)
318 def uri = execution.getVariable(uriKey)
319 if (uri == null || uri == "") {
320 taskProcessor.logDebug("Process specific key not defined, using default key $defaultUriKey", isDebugLogEnabled)
321 uri = execution.getVariable(defaultUriKey)
324 taskProcessor.logDebug('AAI URI is ' + uri, isDebugLogEnabled)
325 taskProcessor.logDebug('Exited ' + method, isDebugLogEnabled)
329 public String getNamespaceFromUri(String uri) {
330 String namespace = AAI_NAMESPACE_STRING
332 return namespace + uri.substring(uri.indexOf("v"), uri.indexOf("v")+2)
341 * This reusable method can be used for making AAI Get Calls. The url should
342 * be passed as a parameter along with the execution. The method will
343 * return an APIResponse.
348 * @return APIResponse
350 public APIResponse executeAAIGetCall(Execution execution, String url){
351 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
352 taskProcessor.logDebug(" ======== STARTED Execute AAI Get Process ======== ", isDebugEnabled)
354 String uuid = UUID.randomUUID()
355 taskProcessor.logDebug( "Generated uuid is: " + uuid, isDebugEnabled)
356 taskProcessor.logDebug( "URL to be used is: " + url, isDebugEnabled)
358 String basicAuthCred = utils.getBasicAuth(execution.getVariable("URN_aai_auth"),execution.getVariable("URN_mso_msoKey"))
360 RESTConfig config = new RESTConfig(url);
361 RESTClient client = new RESTClient(config).addHeader("X-FromAppId", "MSO").addHeader("X-TransactionId", uuid).addHeader("Accept","application/xml");
363 if (basicAuthCred != null && !"".equals(basicAuthCred)) {
364 client.addAuthorizationHeader(basicAuthCred)
367 APIResponse apiResponse = client.get()
371 taskProcessor.logDebug("Exception occured while executing AAI Get Call. Exception is: \n" + e, isDebugEnabled)
374 taskProcessor.logDebug( "======== COMPLETED Execute AAI Get Process ======== ", isDebugEnabled)
378 * This reusable method can be used for making AAI httpPut Calls. The url should
379 * be passed as a parameter along with the execution and payload. The method will
380 * return an APIResponse.
386 * @return APIResponse
388 public APIResponse executeAAIPutCall(Execution execution, String url, String payload){
389 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
390 taskProcessor.logDebug( " ======== Started Execute AAI Put Process ======== ", isDebugEnabled)
392 String uuid = UUID.randomUUID()
393 taskProcessor.logDebug( "Generated uuid is: " + uuid, isDebugEnabled)
395 taskProcessor.logDebug( "URL to be used is: " + url, isDebugEnabled)
396 String basicAuthCred = utils.getBasicAuth(execution.getVariable("URN_aai_auth"),execution.getVariable("URN_mso_msoKey"))
398 RESTConfig config = new RESTConfig(url);
399 RESTClient client = new RESTClient(config).addHeader("X-FromAppId", "MSO").addHeader("X-TransactionId", uuid).addHeader("Content-Type", "application/xml").addHeader("Accept","application/xml");
400 if (basicAuthCred != null && !"".equals(basicAuthCred)) {
401 client.addAuthorizationHeader(basicAuthCred)
403 APIResponse apiResponse = client.httpPut(payload)
407 taskProcessor.utils.log("ERROR", "Exception occured while executing AAI Put Call. Exception is: \n" + e, isDebugEnabled)
410 taskProcessor.logDebug( "======== Completed Execute AAI Put Process ======== ", isDebugEnabled)
415 * This reusable method can be used for making AAI Delete Calls. The url should
416 * be passed as a parameter along with the execution. The method will
417 * return an APIResponse.
422 * @return APIResponse
424 public APIResponse executeAAIDeleteCall(Execution execution, String url){
425 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
426 taskProcessor.logDebug( " ======== Started Execute AAI Delete Process ======== ", isDebugEnabled)
428 String uuid = UUID.randomUUID()
429 taskProcessor.logDebug( "Generated uuid is: " + uuid, isDebugEnabled)
431 taskProcessor.logDebug( "URL to be used is: " + url, isDebugEnabled)
432 String basicAuthCred = utils.getBasicAuth(execution.getVariable("URN_aai_auth"),execution.getVariable("URN_mso_msoKey"))
434 RESTConfig config = new RESTConfig(url);
435 RESTClient client = new RESTClient(config).addHeader("X-FromAppId", "MSO").addHeader("X-TransactionId", uuid).addHeader("Accept","application/xml");
436 if (basicAuthCred != null && !"".equals(basicAuthCred)) {
437 client.addAuthorizationHeader(basicAuthCred)
439 APIResponse apiResponse = client.delete()
444 taskProcessor.utils.log("ERROR", "Exception occured while executing AAI Delete Call. Exception is: \n" + e, isDebugEnabled)
447 taskProcessor.logDebug( "======== Completed Execute AAI Delete Process ======== ", isDebugEnabled)
451 /** Utilitty to get the Cloud Region from AAI
452 * Returns String cloud region id, (ie, cloud-region-id)
454 * @param url - url for AAI get cloud region
455 * @param backend - "PO" - real region, or "SDNC" - v2.5 (fake region).
458 //TODO: We should refactor this method to return WorkflowException instead of Error. Also to throw MSOWorkflowException which the calling flow will then catch.
460 public String getAAICloudReqion(Execution execution, String url, String backend, inputCloudRegion){
461 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
464 APIResponse apiResponse = executeAAIGetCall(execution, url)
465 String returnCode = apiResponse.getStatusCode()
466 String aaiResponseAsString = apiResponse.getResponseBodyAsString()
467 taskProcessor.utils.log("DEBUG", "Call AAI Cloud Region Return code: " + returnCode, isDebugEnabled)
468 execution.setVariable(execution.getVariable("prefix")+"queryCloudRegionReturnCode", returnCode)
469 //taskProcessor.utils.log("DEBUG", "Call AAI Cloud Region Response: " + aaiResponseAsString, isDebugEnabled)
470 //execution.setVariable(execution.getVariable("prefix")+"queryCloudRegionResponse", aaiResponseAsString)
472 if (returnCode == "200") {
473 taskProcessor.utils.log("DEBUG", "Call AAI Cloud Region is Successful.", isDebugEnabled)
475 String regionVersion = taskProcessor.utils.getNodeText(aaiResponseAsString, "cloud-region-version")
476 taskProcessor.utils.log("DEBUG", "Cloud Region Version from AAI for " + backend + " is: " + regionVersion, isDebugEnabled)
477 if (backend == "PO") {
478 regionId = taskProcessor.utils.getNodeText(aaiResponseAsString, "cloud-region-id")
479 } else { // backend not "PO"
480 if (regionVersion == "2.5" ) {
481 regionId = "AAIAIC25"
483 regionId = taskProcessor.utils.getNodeText(aaiResponseAsString, "cloud-region-id")
487 taskProcessor.utils.log("DEBUG", "Cloud Region Id from AAI " + backend + " is: " + regionId, isDebugEnabled)
490 } catch (Exception e) {
491 taskProcessor.utils.log("ERROR", "Exception occured while getting the Cloud Reqion. Exception is: \n" + e, isDebugEnabled)
495 if (returnCode == "404") {
496 if (backend == "PO") {
497 regionId = inputCloudRegion
498 } else { // backend not "PO"
499 regionId = "AAIAIC25"
501 taskProcessor.utils.log("DEBUG", "Cloud Region value for code='404' of " + backend + " is: " + regionId, isDebugEnabled)
504 taskProcessor.utils.log("ERROR", "Call AAI Cloud Region is NOT Successful.", isDebugEnabled)
508 }catch(Exception e) {
509 taskProcessor.utils.log("ERROR", "Exception occured while getting the Cloud Reqion. Exception is: \n" + e, isDebugEnabled)
514 /* returns xml Node with service-type of searchValue */
515 def searchServiceType(xmlInput, searchValue){
516 def fxml= new XmlSlurper().parseText(xmlInput)
517 def ret = fxml.'**'.find {it.'service-type' == searchValue}
521 /* returns xml Node with service-instance-id of searchValue */
522 def searchServiceInstanceId(xmlInput, searchValue){
523 def ret = xmlInput.'**'.find {it.'service-instance-id' == searchValue}