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 org.onap.so.bpmn.common.scripts
23 import org.camunda.bpm.engine.delegate.BpmnError
24 import org.camunda.bpm.engine.delegate.DelegateExecution
25 import org.json.JSONArray
26 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
27 import org.onap.so.bpmn.common.scripts.ExceptionUtil
28 import org.onap.so.bpmn.common.scripts.MsoUtils
29 import org.onap.so.bpmn.core.domain.*
30 import org.onap.so.bpmn.core.json.JsonUtils
31 import org.apache.commons.lang3.StringUtils
33 import static org.onap.so.bpmn.common.scripts.GenericUtils.*
35 import java.time.Duration
37 import org.onap.so.logger.MessageEnum
38 import org.onap.so.logger.MsoLogger
43 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, SniroUtils.class);
46 ExceptionUtil exceptionUtil = new ExceptionUtil()
47 JsonUtils jsonUtil = new JsonUtils()
49 private AbstractServiceTaskProcessor utils
51 public MsoUtils msoUtils = new MsoUtils()
53 public SniroUtils(AbstractServiceTaskProcessor taskProcessor) {
54 this.utils = taskProcessor
58 * This method builds the service-agnostic
59 * sniro json request to get a homing solution
60 * and license solution
64 * @param decomposition - ServiceDecomposition object
65 * @param subscriber - Subscriber information
66 * @param homingParams - Homing/Request parameters
68 * @return request - sniro v2 payload
72 public String buildRequest(DelegateExecution execution, String requestId, ServiceDecomposition decomposition, Subscriber subscriber, String homingParams){
73 msoLogger.debug("Started Building Sniro Request")
74 def callbackUrl = utils.createWorkflowMessageAdapterCallbackURL(execution, "SNIROResponse", requestId)
75 def transactionId = requestId
76 //ServiceInstance Info
77 ServiceInstance serviceInstance = decomposition.getServiceInstance()
79 if(serviceInstance == null){
80 msoLogger.debug("Unable to obtain Service Instance Id, ServiceInstance Object is null" )
81 exceptionUtil.buildAndThrowWorkflowException(execution, 400, "Internal Error - Unable to obtain Service Instance Id, ServiceInstance Object is null")
83 serviceInstanceId = serviceInstance.getInstanceId()
85 String type = decomposition.getServiceType()
86 String serviceType = ""
88 serviceType = """ "serviceType": "${type}", """
91 ModelInfo model = decomposition.getModelInfo()
92 String modelType = model.getModelType()
93 String modelInvariantId = model.getModelInvariantUuid()
94 String modelVersionId = model.getModelUuid()
95 String modelName = model.getModelName()
96 String modelVersion = model.getModelVersion()
98 String subscriberId = subscriber.getGlobalId()
99 String subscriberName = subscriber.getName()
100 String commonSiteId = subscriber.getCommonSiteId()
103 if(!isBlank(homingParams)){
104 orderInfo = homingParams.replaceAll("\"", "\\\\\"").replaceAll("\n", "").replaceAll("\r", "")
105 orderInfo = StringUtils.normalizeSpace(orderInfo)
108 //Determine RequestType
109 //TODO Implement better way to determine this
110 String requestType = "initial"
111 List<Resource> resources = decomposition.getServiceResources()
112 for(Resource r:resources){
113 HomingSolution currentSolution = r.getCurrentHomingSolution()
114 if(currentSolution != null){
115 String indicator = currentSolution.getServiceInstanceId()
116 if(indicator != null){
117 requestType = "speed changed"
122 String timeout = execution.getVariable("timeout")
123 Duration d = Duration.parse(timeout);
124 long timeoutSeconds = d.getSeconds();
127 String placementDemands = ""
128 StringBuilder sb = new StringBuilder()
129 List<Resource> resourceList = decomposition.getAllottedResources()
130 List<VnfResource> vnfResourceList = decomposition.getVnfResources()
132 //TODO should be service agnostic so this is just a temp solution to all vnfs to be sent in placement container for adiod
133 if(resourceList.isEmpty() || resourceList == null){
135 for(VnfResource vnf : vnfResourceList){
136 if(StringUtils.containsIgnoreCase(vnf.getNfRole(), "vce") || StringUtils.containsIgnoreCase(vnf.getNfRole(), "vpe")){
137 resourceList = decomposition.getVnfResources()
144 if(resourceList.isEmpty() || resourceList == null){
145 msoLogger.debug("Resources List is Empty")
147 for(Resource resource:resourceList){
148 ModelInfo resourceModelInfo = resource.getModelInfo()
149 ResourceInstance resourceInstance = resource.getResourceInstance()
150 def resourceInstanceType = resource.getResourceType()
151 def serviceResourceId = resource.getResourceId() //TODO - resourceId versus instanceId - should be what is put in AAI, whatever we put here will be what is in response, used to correlate
152 def resourceModuleName = resourceModelInfo.getModelInstanceName()
153 def resouceModelCustomizationId = resourceModelInfo.getModelCustomizationUuid()
154 def resouceModelInvariantId = resourceModelInfo.getModelInvariantUuid()
155 def resouceModelName = resourceModelInfo.getModelName()
156 def resouceModelVersion = resourceModelInfo.getModelVersion()
157 def resouceModelVersionId = resourceModelInfo.getModelUuid()
158 def resouceModelType = resourceModelInfo.getModelType()
159 def tenantId = "" //Optional
160 def tenantName = "" //Optional
163 String existingPlacement = ""
164 HomingSolution currentPlacement = resource.getCurrentHomingSolution()
165 if(currentPlacement != null){
166 String homedServiceInstanceId = currentPlacement.getServiceInstanceId()
168 ""","existingPlacement": {
169 "serviceInstanceId": "${homedServiceInstanceId}"
176 "resourceInstanceType": "${resourceInstanceType}",
177 "serviceResourceId": "${serviceResourceId}",
178 "resourceModuleName": "${resourceModuleName}",
179 "resourceModelInfo": {
180 "modelCustomizationId": "${resouceModelCustomizationId}",
181 "modelInvariantId": "${resouceModelInvariantId}",
182 "modelName": "${resouceModelName}",
183 "modelVersion": "${resouceModelVersion}",
184 "modelVersionId": "${resouceModelVersionId}",
185 "modelType": "${resouceModelType}"
187 "tenantId": "${tenantId}",
188 "tenantName": "${tenantName}"
192 placementDemands = sb.append(demand)
194 placementDemands = placementDemands.substring(0, placementDemands.length() - 1);
197 String licenseDemands = ""
198 sb = new StringBuilder()
199 if(vnfResourceList.isEmpty() || vnfResourceList == null){
200 msoLogger.debug("Vnf Resources List is Empty")
202 for(VnfResource vnfResource:vnfResourceList){
203 ModelInfo vnfResourceModelInfo = vnfResource.getModelInfo()
204 ResourceInstance vnfResourceInstance = vnfResource.getResourceInstance()
205 def resourceInstanceType = vnfResource.getResourceType()
206 def serviceResourceId = vnfResource.getResourceId()
207 def resourceModuleName = vnfResourceModelInfo.getModelInstanceName()
208 def resouceModelCustomizationId = vnfResourceModelInfo.getModelCustomizationUuid()
209 def resouceModelInvariantId = vnfResourceModelInfo.getModelInvariantUuid()
210 def resouceModelName = vnfResourceModelInfo.getModelName()
211 def resouceModelVersion = vnfResourceModelInfo.getModelVersion()
212 def resouceModelVersionId = vnfResourceModelInfo.getModelUuid()
213 def resouceModelType = vnfResourceModelInfo.getModelType()
215 String curentLicenseJson = ""
216 HomingSolution currentSol = vnfResource.getCurrentHomingSolution()
217 if(currentSol != null){
218 JSONArray entitlementPoolList = currentSol.getLicense().getEntitlementPoolListAsString()
219 JSONArray licenseKeyGroupList = currentSol.getLicense().getLicenseKeyGroupListAsString()
221 """ ,"existingLicense": {
222 "entitlementPoolUUID":
223 ${entitlementPoolList},
224 "licenseKeyGroupUUID":
225 ${licenseKeyGroupList}
232 "resourceInstanceType": "${resourceInstanceType}",
233 "serviceResourceId": "${serviceResourceId}",
234 "resourceModuleName": "${resourceModuleName}",
235 "resourceModelInfo": {
236 "modelCustomizationId": "${resouceModelCustomizationId}",
237 "modelInvariantId": "${resouceModelInvariantId}",
238 "modelName": "${resouceModelName}",
239 "modelVersion": "${resouceModelVersion}",
240 "modelVersionId": "${resouceModelVersionId}",
241 "modelType": "${resouceModelType}"
246 licenseDemands = sb.append(demand)
248 licenseDemands = licenseDemands.substring(0, licenseDemands.length() - 1);
254 "transactionId": "${transactionId}",
255 "requestId": "${requestId}",
256 "callbackUrl": "${callbackUrl}",
258 "requestType": "${requestType}",
264 "timeout": ${timeoutSeconds}
268 "serviceModelInfo": {
269 "modelType": "${modelType}",
270 "modelInvariantId": "${modelInvariantId}",
271 "modelVersionId": "${modelVersionId}",
272 "modelName": "${modelName}",
273 "modelVersion": "${modelVersion}"
276 "globalSubscriberId": "${subscriberId}",
277 "subscriberName": "${subscriberName}",
278 "subscriberCommonSiteId": "${commonSiteId}"
289 "serviceInstanceId": "${serviceInstanceId}",
290 "orderInfo": "{\\\"requestParameters\\\": ${orderInfo}}"
294 msoLogger.debug("Completed Building Sniro Request")
299 * This method validates the async callback response from Sniro.
300 * If the response contains an exception the method will build
301 * and throw a workflow exception.
304 * @param response - sniro async response
308 //TODO needs updating per sniro changes
309 public void validateCallbackResponse(DelegateExecution execution, String response){
311 String placements = ""
313 if(isBlank(response)){
314 exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Sniro Async Callback Response is Empty")
316 if(JsonUtils.jsonElementExist(response, "solutionInfo")){
317 if(JsonUtils.jsonElementExist(response, "solutionInfo.placementInfo")){
318 placements = jsonUtil.getJsonValue(response, "solutionInfo.placementInfo")
320 if(JsonUtils.jsonElementExist(response, "solutionInfo.licenseInfo")){
321 licenses = jsonUtil.getJsonValue(response, "solutionInfo.licenseInfo")
323 if((isBlank(placements) || placements.equalsIgnoreCase("[]")) && (isBlank(licenses) || licenses.equalsIgnoreCase("[]"))){
324 msoLogger.debug("Sniro Async Response does not contain: licenses or placements")
328 }else if(JsonUtils.jsonElementExist(response, "requestError") == true){
329 String errorMessage = ""
330 if(response.contains("policyException")){
331 String text = jsonUtil.getJsonValue(response, "requestError.policyException.text")
332 errorMessage = "Sniro Async Response contains a policy error: " + text
333 }else if(response.contains("serviceException")){
334 String text = jsonUtil.getJsonValue(response, "requestError.serviceException.text")
335 errorMessage = "Sniro Async Response contains a service error: " + text
337 errorMessage = "Sniro Async Response contains an error: not provided"
339 msoLogger.debug("Sniro Async Response contains an error: " + errorMessage)
340 exceptionUtil.buildAndThrowWorkflowException(execution, 400, errorMessage)
343 msoLogger.debug("Sniro Async Response contains an error: not provided")
344 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Sniro Async Response contains an error: not provided")
350 msoLogger.debug("Error encountered within Homing validateCallbackResponse method: " + e)
351 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in Sniro Homing Validate Async Response")