aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/MSOCommonBPMN/src/main/groovy/org/openecomp/mso/bpmn/common/scripts/SNIROUtils.groovy
blob: 1233d0423b8db999225f371c34d0933a946c95e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.mso.bpmn.common.scripts

import org.camunda.bpm.engine.delegate.DelegateExecution
import org.json.JSONArray
import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
import org.openecomp.mso.bpmn.common.scripts.MsoUtils
import org.openecomp.mso.bpmn.core.domain.*
import org.openecomp.mso.bpmn.core.json.JsonUtils
import org.apache.commons.lang3.StringUtils

import static  org.openecomp.mso.bpmn.common.scripts.GenericUtils.*

class SNIROUtils{

	ExceptionUtil exceptionUtil = new ExceptionUtil()
	JsonUtils jsonUtil = new JsonUtils()

	private AbstractServiceTaskProcessor utils

	public MsoUtils msoUtils = new MsoUtils()

	public SNIROUtils(AbstractServiceTaskProcessor taskProcessor) {
		this.utils = taskProcessor
	}

	/**
	 * This method builds the service-agnostic
	 * sniro json request to get a homing solution
	 * and license solution
	 *
	 * @param execution
	 * @param requestId
	 * @param decomposition - ServiceDecomposition object
	 * @param subscriber - Subscriber information
	 * @param homingParams - Homing/Request parameters
	 *
	 * @return request - sniro v2 payload
	 *
	 * @author cb645j
	 */
	public String buildRequest(DelegateExecution execution, String requestId, ServiceDecomposition decomposition, Subscriber subscriber, String homingParams){
		def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
		utils.log("DEBUG", "Started Building Sniro Request", isDebugEnabled)
		def callbackUrl = utils.createWorkflowMessageAdapterCallbackURL(execution, "SNIROResponse", requestId)
		def transactionId = requestId
		//ServiceInstance Info
		ServiceInstance serviceInstance = decomposition.getServiceInstance()
		def serviceInstanceId
		if(serviceInstance == null){
			utils.log("DEBUG", "Unable to obtain Service Instance Id, ServiceInstance Object is null" , isDebugEnabled)
			exceptionUtil.buildAndThrowWorkflowException(execution, 400, "Internal Error - Unable to obtain Service Instance Id, ServiceInstance Object is null")
		}else{
			serviceInstanceId = serviceInstance.getInstanceId()
		}
		//Model Info
		ModelInfo model = decomposition.getModelInfo()
		String modelType = model.getModelType()
		String modelInvariantId = model.getModelInvariantUuid()
		String modelVersionId = model.getModelUuid()
		String modelName = model.getModelName()
		String modelVersion = model.getModelVersion()
		//Subscriber Info
		String subscriberId = subscriber.getGlobalId()
		String subscriberName = subscriber.getName()
		String commonSiteId = subscriber.getCommonSiteId()
		//OrderInfo
		String orderInfo
		if(!isBlank(homingParams)){
			orderInfo = homingParams.replaceAll("\"", "\\\\\"").replaceAll("\n", "").replaceAll("\r", "")
			orderInfo = StringUtils.normalizeSpace(orderInfo)
		}

		//Determine RequestType
		//TODO Figure out better way to determine this
		String requestType = "initial"
		List<Resource> resources = decomposition.getServiceResources()
		for(Resource r:resources){
			HomingSolution currentSolution = (HomingSolution) r.getCurrentHomingSolution()
			if(currentSolution != null){
				requestType = "speed changed"
			}
		}

		int timeoutSeconds = 1800
		String timeout = execution.getVariable("timeout")
		if(isNotBlank(timeout)){
			String subT = timeout.substring(2, timeout.length() - 1)
			int timeoutInt = Integer.parseInt(subT)
			timeoutSeconds = timeoutInt * 60
		}

		//Demands
		String placementDemands = ""
		StringBuilder sb = new StringBuilder()
		List<Resource> resourceList = decomposition.getServiceAllottedResources()
		List<VnfResource> vnfResourceList = decomposition.getServiceVnfs()

		// TODO: We should include both alloted resources and service resources in the placementDeamnds- not one or the other.
		if(resourceList.isEmpty() || resourceList == null){
			utils.log("DEBUG", "Allotted Resources List is empty - will try to get service VNFs instead.", isDebugEnabled)
			resourceList = decomposition.getServiceVnfs()
		}

		if(resourceList.isEmpty() || resourceList == null){
			utils.log("DEBUG", "Resources List is Empty", isDebugEnabled)
		}else{
			for(Resource resource:resourceList){
				ModelInfo resourceModelInfo = resource.getModelInfo()
				ResourceInstance resourceInstance = resource.getResourceInstance()
				def resourceInstanceType = resource.getResourceType()
				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
				def resourceModuleName = resourceModelInfo.getModelInstanceName()
				def resouceModelCustomizationId = resourceModelInfo.getModelCustomizationUuid()
				def resouceModelInvariantId = resourceModelInfo.getModelInvariantUuid()
				def resouceModelName = resourceModelInfo.getModelName()
				def resouceModelVersion = resourceModelInfo.getModelVersion()
				def resouceModelVersionId = resourceModelInfo.getModelUuid()
				def resouceModelType = resourceModelInfo.getModelType()
				def tenantId = "" //Optional
				def tenantName = "" //Optional


				String existingPlacement = ""
				HomingSolution currentPlacement = resource.getCurrentHomingSolution()
				if(currentPlacement != null){
					String homedServiceInstanceId = currentPlacement.getServiceInstanceId()
					existingPlacement =
					""","existingPlacement": {
                           "serviceInstanceId": "${homedServiceInstanceId}"
                    }"""
				}


				String demand =
					"""{
						"resourceInstanceType": "${resourceInstanceType}",
						"serviceResourceId": "${serviceResourceId}",
						"resourceModuleName": "${resourceModuleName}",
						"resourceModelInfo": {
							"modelCustomizationId": "${resouceModelCustomizationId}",
							"modelInvariantId": "${resouceModelInvariantId}",
							"modelName": "${resouceModelName}",
							"modelVersion": "${resouceModelVersion}",
							"modelVersionId": "${resouceModelVersionId}",
							"modelType": "${resouceModelType}"
						},
						"tenantId": "${tenantId}",
						"tenantName": "${tenantName}"
						${existingPlacement}
					},"""

				placementDemands = sb.append(demand)
			}
			placementDemands = placementDemands.substring(0, placementDemands.length() - 1);
		}

		String licenseDemands = ""
		sb = new StringBuilder()
		if(vnfResourceList.isEmpty() || vnfResourceList == null){
			utils.log("DEBUG", "Vnf Resources List is Empty", isDebugEnabled)
		}else{
			for(VnfResource vnfResource:vnfResourceList){
				ModelInfo vnfResourceModelInfo = vnfResource.getModelInfo()
				ResourceInstance vnfResourceInstance = vnfResource.getResourceInstance()
				def resourceInstanceType = vnfResource.getResourceType()
				def serviceResourceId = vnfResource.getResourceId()
				def resourceModuleName = vnfResourceModelInfo.getModelInstanceName()
				def resouceModelCustomizationId = vnfResourceModelInfo.getModelCustomizationUuid()
				def resouceModelInvariantId = vnfResourceModelInfo.getModelInvariantUuid()
				def resouceModelName = vnfResourceModelInfo.getModelName()
				def resouceModelVersion = vnfResourceModelInfo.getModelVersion()
				def resouceModelVersionId = vnfResourceModelInfo.getModelUuid()
				def resouceModelType = vnfResourceModelInfo.getModelType()

				String curentLicenseJson = ""
				HomingSolution currentSol = vnfResource.getCurrentHomingSolution()
				if(currentSol != null){
					JSONArray entitlementPoolList = currentSol.getLicense().getEntitlementPoolListAsString()
					JSONArray licenseKeyGroupList = currentSol.getLicense().getLicenseKeyGroupListAsString()
					curentLicenseJson =
					""" ,"existingLicense": [
                           {
                                 "entitlementPoolUUID":
									${entitlementPoolList},
                                 "licenseKeyGroupUUID":
									${licenseKeyGroupList}

                           }
                    	]"""
				}

				String demand =
				"""{
						"resourceInstanceType": "${resourceInstanceType}",
						"serviceResourceId": "${serviceResourceId}",
						"resourceModuleName": "${resourceModuleName}",
						"resourceModelInfo": {
							"modelCustomizationId": "${resouceModelCustomizationId}",
							"modelInvariantId": "${resouceModelInvariantId}",
							"modelName": "${resouceModelName}",
							"modelVersion": "${resouceModelVersion}",
							"modelVersionId": "${resouceModelVersionId}",
							"modelType": "${resouceModelType}"
						}
						${curentLicenseJson}
					},"""

					licenseDemands = sb.append(demand)
			}
			licenseDemands = licenseDemands.substring(0, licenseDemands.length() - 1);
		}

		String request =
				"""{
	  	"requestInfo": {
				"transactionId": "${transactionId}",
				"requestId": "${requestId}",
				"callbackUrl": "${callbackUrl}",
				"sourceId": "mso",
				"requestType": "${requestType}",
				"optimizer": [
					"placement",
					"license"
				],
				"numSolutions": 1,
				"timeout": ${timeoutSeconds}
				},
		"placementInfo": {
			"serviceModelInfo": {
				"modelType": "${modelType}",
				"modelInvariantId": "${modelInvariantId}",
				"modelVersionId": "${modelVersionId}",
				"modelName": "${modelName}",
				"modelVersion": "${modelVersion}"
				},
			"subscriberInfo": {
				"globalSubscriberId": "${subscriberId}",
				"subscriberName": "${subscriberName}",
				"subscriberCommonSiteId": "${commonSiteId}"
				},
			"demandInfo": {
				"placementDemand": [
					${placementDemands}
				],
				"licenseDemand": [
					${licenseDemands}
				]
			},
			"policyId": [],
			"serviceInstanceId": "${serviceInstanceId}",
			"orderInfo": "{\\\"requestParameters\\\": ${orderInfo}}"
		}
	  }"""

	  	utils.log("DEBUG", "Completed Building Sniro Request", isDebugEnabled)
		return request
	}

	/**
	 * This method validates the callback response
	 * from Sniro. If the response contains an
	 * exception the method will build and throw
	 * a workflow exception.
	 *
	 * @param execution
	 * @param response - the async callback response from sniro
	 *
	 * @author cb645j
	 */
	public void validateCallbackResponse(DelegateExecution execution, String response){
		def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
		String placements = ""
		if(isBlank(response)){
			exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Sniro Async Callback Response is Empty")
		}else{
			if(JsonUtils.jsonElementExist(response, "solutionInfo.placement")){
				placements = jsonUtil.getJsonValue(response, "solutionInfo.placement")
				if(isBlank(placements) || placements.equalsIgnoreCase("[]")){
					String statusMessage = jsonUtil.getJsonValue(response, "statusMessage")
					if(isBlank(statusMessage)){
						utils.log("DEBUG", "Error Occured in Homing: Sniro Async Callback Response does not contain placement solution.", isDebugEnabled)
						exceptionUtil.buildAndThrowWorkflowException(execution, 400, "Sniro Async Callback Response does not contain placement solution.")
					}else{
						utils.log("DEBUG", "Error Occured in Homing: " + statusMessage, isDebugEnabled)
						exceptionUtil.buildAndThrowWorkflowException(execution, 400, statusMessage)
					}
				}else{
					return
				}
			}else if(JsonUtils.jsonElementExist(response, "requestError") == true){
				String errorMessage = ""
				if(response.contains("policyException")){
					String text = jsonUtil.getJsonValue(response, "requestError.policyException.text")
					errorMessage = "Sniro Async Callback Response contains a Request Error Policy Exception: " + text
				}else if(response.contains("serviceException")){
					String text = jsonUtil.getJsonValue(response, "requestError.serviceException.text")
					errorMessage = "Sniro Async Callback Response contains a Request Error Service Exception: " + text
				}else{
					errorMessage = "Sniro Async Callback Response contains a Request Error. Unable to determine the Request Error Exception."
				}
				utils.log("DEBUG", "Error Occured in Homing: " + errorMessage, isDebugEnabled)
				exceptionUtil.buildAndThrowWorkflowException(execution, 400, errorMessage)

			}else{
				utils.log("DEBUG", "Error Occured in Homing: Received an Unknown Async Callback Response from Sniro.", isDebugEnabled)
				exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Received an Unknown Async Callback Response from Sniro.")
			}
		}

	}


}