summaryrefslogtreecommitdiffstats
path: root/bpmn/so-bpmn-infrastructure-common/src/main/groovy/org/onap/so/bpmn/infrastructure/scripts/DoSendCommandToNSSMF.groovy
blob: 63cd3c72f9d15e7f9f44911d592a979ef0c7dac9 (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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 # Copyright (c) 2019, CMCC Technologies Co., Ltd.
 #
 # 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.onap.so.bpmn.infrastructure.scripts

import com.fasterxml.jackson.databind.ObjectMapper
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import org.camunda.bpm.engine.delegate.BpmnError
import org.camunda.bpm.engine.delegate.DelegateExecution
import org.onap.logging.filter.base.ONAPComponents
import org.onap.so.beans.nsmf.*
import org.onap.so.bpmn.common.scripts.*
import org.onap.so.bpmn.common.util.OofInfraUtils
import org.onap.so.bpmn.core.UrnPropertiesReader
import org.onap.so.bpmn.core.WorkflowException
import org.onap.so.bpmn.core.domain.ServiceArtifact
import org.onap.so.bpmn.core.domain.ServiceDecomposition
import org.onap.so.bpmn.core.json.JsonUtils
import org.onap.so.client.HttpClient
import org.onap.so.client.HttpClientFactory
import org.onap.so.logger.ErrorCode
import org.onap.so.logger.LoggingAnchor
import org.onap.so.logger.MessageEnum
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.web.util.UriUtils

import javax.ws.rs.core.Response
import java.lang.reflect.Type

/**
 * This class supports the DoCreateVnf building block subflow
 * with the creation of a generic vnf for
 * infrastructure.
 *
 */
class DoSendCommandToNSSMF extends AbstractServiceTaskProcessor {

    private static final Logger logger = LoggerFactory.getLogger( DoSendCommandToNSSMF.class);
	String Prefix="DoCNSSMF_"
    ExceptionUtil exceptionUtil = new ExceptionUtil()
    JsonUtils jsonUtil = new JsonUtils()
    VidUtils vidUtils = new VidUtils(this)
    SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils()
    OofInfraUtils oofInfraUtils = new OofInfraUtils()

	/**
	 * This method gets and validates the incoming
	 * request.
	 *
	 * @param - execution
	 *
	 */
	public void preProcessRequest(DelegateExecution execution) {

		execution.setVariable("prefix",Prefix)
		logger.debug("STARTED Do sendcommandtoNssmf PreProcessRequest Process")

		/*******************/
		try{
			// Get Variables
			String e2eserviceInstanceId = execution.getVariable("e2eserviceInstanceId")
			String serviceInstanceId = execution.getVariable("e2eserviceInstanceId")
			execution.setVariable("e2eserviceInstanceId", e2eserviceInstanceId)
			execution.setVariable("serviceInstanceId", serviceInstanceId)
		 	logger.debug("Incoming e2eserviceInstanceId is: " + e2eserviceInstanceId)

			String NSIserviceid =  execution.getVariable("NSIserviceid")
			execution.setVariable("NSIserviceid", NSIserviceid)
			logger.debug("Incoming NSI id is: " + NSIserviceid)


			String nssiMap  = execution.getVariable("nssiMap")
			Type type = new TypeToken<HashMap<String, NSSI>>(){}.getType()
			Map<String, NSSI> DonssiMap = new Gson().fromJson(nssiMap,type)
            String strDonssiMap = mapToJsonStr(DonssiMap)
			execution.setVariable("DonssiMap",strDonssiMap)
			logger.debug("Incoming DonssiMap is: " + strDonssiMap)

			String requestId = execution.getVariable("msoRequestId")
			execution.setVariable("msoRequestId", requestId)

			String operationType = execution.getVariable("operationType")
			execution.setVariable("operationType", operationType)
			logger.debug("Incoming operationType is: " + operationType)

            if (operationType == "activation") {
				execution.setVariable("activationSequence","an,tn,cn")
			}else {
				execution.setVariable("activationSequence","cn,tn,an")
			}
			execution.setVariable("activationIndex",0)
			execution.setVariable("miniute", "0")
			execution.setVariable("activateNumberSlice",0)

			logger.info("the end !!")
		}catch(BpmnError b){
			logger.debug("Rethrowing MSOWorkflowException")
			throw b
		}catch(Exception e){
			logger.info("the end of catch !!")
			logger.debug(" Error Occured in DoSendCommandToNSSMF PreProcessRequest method!" + e.getMessage())
			exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoSendCommandToNSSMF PreProcessRequest")

		}
		logger.trace("COMPLETED DoSendCommandToNSSMF PreProcessRequest Process")
	}
    private String mapToJsonStr(Map<String, NSSI> stringNSSIHashMap) {
        HashMap<String, NSSI> map = new HashMap<String, NSSI>()
        for(Map.Entry<String, NSSI> child:stringNSSIHashMap.entrySet())
        {
            map.put(child.getKey(), child.getValue())
        }
        return new Gson().toJson(map)
    }
	public	void getNSSIformlist(DelegateExecution execution) {

		String  nssiMap = execution.getVariable("DonssiMap")
		Type type = new TypeToken<HashMap<String, NSSI>>(){}.getType()
        Map<String, NSSI> DonssiMap = new Gson().fromJson(nssiMap,type)
		String isNSSIActivate = execution.getVariable("isNSSIActivate")

		String activationSequence01 = execution.getVariable("activationSequence")
	    String[] strlist = activationSequence01.split(",")

		int  activationIndex = execution.getVariable("activationIndex")
		int indexcurrent = 0
		if (isNSSIActivate == "true")
		{
			execution.setVariable("isGetSuccessfull", "false")
		}else{for (int index = activationIndex; index < 3;index++) {
			String domaintype01 = strlist[index]
			if (DonssiMap.containsKey(domaintype01)) {
				NSSI nssiobject = DonssiMap.get(domaintype01)
				execution.setVariable("domainType", domaintype01)
				execution.setVariable("nssiId", nssiobject.getNssiId())
				execution.setVariable("modelInvariantUuid", nssiobject.getModelInvariantId())
				execution.setVariable("modelUuid", nssiobject.getModelVersionId())
				execution.setVariable("isGetSuccessfull", "true")
				String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
				String modelUuid = execution.getVariable("modelUuid")
				//here modelVersion is not set, we use modelUuid to decompose the service.
				String serviceModelInfo = """{
            "modelInvariantUuid":"${modelInvariantUuid}",
            "modelUuid":"${modelUuid}",
            "modelVersion":""
             }"""
				execution.setVariable("serviceModelInfo", serviceModelInfo)
				indexcurrent = index
				execution.setVariable("activationIndex", indexcurrent)
				break
			}else
			{
				indexcurrent = index + 1

			}
		}
			if ( activationIndex > 2) {
				execution.setVariable("isGetSuccessfull", "false")
			}
			execution.setVariable("activationIndex", indexcurrent)}

	}
	/**
	 * get vendor Info
	 * @param execution
	 */
	private void processDecomposition(DelegateExecution execution) {
		logger.debug("***** processDecomposition *****")

		try {
			ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition") as ServiceDecomposition
            ServiceArtifact serviceArtifact = serviceDecomposition.getServiceInfo().getServiceArtifact().get(0)
			String content = serviceArtifact.getContent()
			String vendor = jsonUtil.getJsonValue(content, "metadata.vendor")
			//String domainType  = jsonUtil.getJsonValue(content, "metadata.domainType")

			execution.setVariable("vendor", vendor)
		//	currentNSSI['domainType'] = domainType
			logger.info("processDecomposition, current vendor-domainType:" +  vendor)

		} catch (any) {
			String exceptionMessage = "Bpmn error encountered in deallocate nssi. processDecomposition() - " + any.getMessage()
			logger.debug(exceptionMessage)
			exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
		}
		logger.debug("***** Exit processDecomposition *****")
	}
	public	void UpdateIndex(DelegateExecution execution) {
		def activationIndex = execution.getVariable("activationIndex")
		int activateNumberSlice = execution.getVariable("activateNumberSlice") as Integer
		def activationCount= execution.getVariable("activationCount")
		//DecimalFormat df1 = new DecimalFormat("##%")
		int  rate = (activateNumberSlice / activationCount) * 100
		if (rate == 100)
		{
			execution.setVariable("isNSSIActivate","true")
		}
		else{
			execution.setVariable("isNSSIActivate","false")
		}
		activationIndex = activationIndex + 1
		execution.setVariable("activationIndex",activationIndex)
		logger.trace("the Progress of activation is " + rate.toString() + "%" )
		try{
			String serviceId = execution.getVariable("serviceInstanceId")
			String operationId = UUID.randomUUID().toString()
			String operationType =  execution.getVariable("operationType")
			String userId = ""
			String result = (operationType.equals("activation"))? "ACTIVATING": "DEACTIVATING"
			int progress = rate
			String reason = ""
			String operationContent = "Service activation in progress"
			logger.debug("Generated new operation for Service Instance serviceId:" + serviceId + " operationId:" + operationId)
			serviceId = UriUtils.encode(serviceId,"UTF-8")
			execution.setVariable("e2eserviceInstanceId", serviceId)
			execution.setVariable("operationId", operationId)
			execution.setVariable("operationType", operationType)

			def dbAdapterEndpoint = UrnPropertiesReader.getVariable("mso.adapters.openecomp.db.endpoint",execution)
			execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
			logger.debug("DB Adapter Endpoint is: " + dbAdapterEndpoint)

			String payload =
					"""<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                        xmlns:ns="http://org.onap.so/requestsdb">
                        <soapenv:Header/>
                        <soapenv:Body>
                            <ns:initServiceOperationStatus xmlns:ns="http://org.onap.so/requestsdb">
                            <serviceId>${MsoUtils.xmlEscape(serviceId)}</serviceId>
                            <operationId>${MsoUtils.xmlEscape(operationId)}</operationId>
                            <operationType>${MsoUtils.xmlEscape(operationType)}</operationType>
                            <userId>${MsoUtils.xmlEscape(userId)}</userId>
                            <result>${MsoUtils.xmlEscape(result)}</result>
                            <operationContent>${MsoUtils.xmlEscape(operationContent)}</operationContent>
                            <progress>${MsoUtils.xmlEscape(progress)}</progress>
                            <reason>${MsoUtils.xmlEscape(reason)}</reason>
                        </ns:initServiceOperationStatus>
                    </soapenv:Body>
                </soapenv:Envelope>"""

			payload = utils.formatXml(payload)
			execution.setVariable("CVFMI_updateServiceOperStatusRequest", payload)
			logger.debug("Outgoing CVFMI_updateServiceOperStatusRequest: \n" + payload)

		}catch(Exception e){
			logger.error(LoggingAnchor.FIVE, MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
					"Exception Occured Processing Activate Slice .", "BPMN",
					ErrorCode.UnknownError.getValue(), "Exception is:\n" + e)
			execution.setVariable("CVFMI_ErrorResponse", "Error Occurred during Activate Slice Method:\n" + e.getMessage())
		}
		logger.trace("finished Activate Slice")
	}
	public void WaitForReturn(DelegateExecution execution) {
		//logger.debug("Query : "+ Jobid)
		def miniute=execution.getVariable("miniute")
		Thread.sleep(10000)
		int miniute01  = Integer.parseInt(miniute) + 1
		logger.debug("waiting for : "+ miniute + "miniutes")
		execution.setVariable("miniute", String.valueOf(miniute01))
	}
	public void GetTheStatusOfActivation(DelegateExecution execution) {

		String snssai= execution.getVariable("snssai")
		String domaintype = execution.getVariable("domainType")
		String NSIserviceid=execution.getVariable("NSIserviceid")
		String nssiId = execution.getVariable("nssiId")
		String Jobid=execution.getVariable("JobId")
		def miniute=execution.getVariable("miniute")
		String vendor = execution.getVariable("vendor")
		String jobstatus ="error"


		logger.debug("Query the jobid activation of SNSSAI: "+ Jobid)
		logger.debug("the domain is : "+ domaintype)
		logger.debug("the NSSID is : "+nssiId)
		logger.debug("the NSIserviceid is : "+NSIserviceid)

        JobStatusRequest jobStatusRequest = new JobStatusRequest()

        EsrInfo info = new EsrInfo()
		info.setNetworkType(NetworkType.fromString(domaintype))
		info.setVendor(vendor)

		jobStatusRequest.setNsiId(NSIserviceid)
		jobStatusRequest.setNssiId(nssiId)
		jobStatusRequest.setEsrInfo(info)


		ObjectMapper mapper = new ObjectMapper()
		String Reqjson = mapper.writeValueAsString(jobStatusRequest)
		String isActivateSuccessfull=false

		String urlString = UrnPropertiesReader.getVariable("mso.adapters.nssmf.endpoint", execution)
		String nssmfRequest = urlString + "/api/rest/provMns/v1/NSS/jobs/" +Jobid

		//send request to active  NSSI TN option
		URL url = new URL(nssmfRequest)

        HttpClient httpClient = new HttpClientFactory().newJsonClient(url,  ONAPComponents.EXTERNAL)
		Response httpResponse = httpClient.post(Reqjson)

		int responseCode = httpResponse.getStatus()
		logger.debug("NSSMF activation response code is: " + responseCode)

		if (responseCode == 404) {
			exceptionUtil.buildAndThrowWorkflowException(execution, responseCode, "Received a Bad job status Response from NSSMF.")
			isActivateSuccessfull = false
			execution.setVariable("isActivateSuccessfull", isActivateSuccessfull)
			jobstatus="error"
		}else if(responseCode == 200) {
			if (httpResponse.hasEntity()) {
				JobStatusResponse jobStatusResponse = httpResponse.readEntity(JobStatusResponse.class)
				execution.setVariable("statusDescription", jobStatusResponse.getResponseDescriptor().getStatusDescription())
				jobstatus = jobStatusResponse.getResponseDescriptor().getStatus()
				switch(jobstatus) {
					case "started":
					case "processing":
						isActivateSuccessfull = "waitting"
						execution.setVariable("isActivateSuccessfull", isActivateSuccessfull)
						break
					case "finished":
						isActivateSuccessfull = "true"
						execution.setVariable("isActivateSuccessfull", isActivateSuccessfull)
						execution.setVariable("activateNumberSlice",execution.getVariable("activateNumberSlice")+ 1)
						break
					case "error":
					default:
						isActivateSuccessfull = "false"
						execution.setVariable("isActivateSuccessfull", isActivateSuccessfull)

				}
				if(Integer.parseInt(miniute) > 6 )
				{
					isActivateSuccessfull = "false"
					execution.setVariable("isActivateSuccessfull", isActivateSuccessfull)
					exceptionUtil.buildAndThrowWorkflowException(execution, responseCode, "Received a timeout job status Response from NSSMF.")
				}
			}else
			{
				exceptionUtil.buildAndThrowWorkflowException(execution, responseCode, "Received a Bad job status Response from NSSMF.")
				isActivateSuccessfull = false
				execution.setVariable("isActivateSuccessfull", isActivateSuccessfull)
			}
		} else {
			isActivateSuccessfull = false
			execution.setVariable("isActivateSuccessfull", isActivateSuccessfull)
			exceptionUtil.buildAndThrowWorkflowException(execution, responseCode, "Received a Bad job status Response from NSSMF.")
		}
	}
	public void SendCommandToNssmf(DelegateExecution execution) {

		String snssai= execution.getVariable("snssai")
		String domaintype = execution.getVariable("domainType")
		String NSIserviceid=execution.getVariable("NSIserviceid")
		String nssiId = execution.getVariable("nssiId")
		String vendor = execution.getVariable("vendor")


		logger.debug("the domain is : "+domaintype)
		logger.debug("SNSSAI: "+snssai +" will be activated")
		logger.debug("the NSSID is : "+nssiId)
		logger.debug("the NSIserviceid is : "+NSIserviceid)

        EsrInfo esr = new EsrInfo();
		esr.setNetworkType(NetworkType.fromString(domaintype))
		esr.setVendor(vendor)

        ActDeActNssi actNssi = new ActDeActNssi();
		actNssi.setNsiId(NSIserviceid);
		actNssi.setNssiId(nssiId);
        NssiActDeActRequest actRequest = new NssiActDeActRequest();
		actRequest.setActDeActNssi(actNssi);
		actRequest.setEsrInfo(esr)

		ObjectMapper mapper = new ObjectMapper();
		String json = mapper.writeValueAsString(actRequest);


		String urlString = UrnPropertiesReader.getVariable("mso.adapters.nssmf.endpoint", execution)

		//Prepare auth for NSSMF - Begin
		def authHeader = ""
		String basicAuth = UrnPropertiesReader.getVariable("mso.nssmf.auth", execution)
		String operationType = execution.getVariable("operationType")

		String nssmfRequest = urlString + "/api/rest/provMns/v1/NSS/" + snssai + "/" + operationType

		//send request to active  NSSI TN option
		URL url = new URL(nssmfRequest)

        HttpClient httpClient = new HttpClientFactory().newJsonClient(url, ONAPComponents.EXTERNAL)
		Response httpResponse = httpClient.post(json)

		int responseCode = httpResponse.getStatus()
		logger.debug("NSSMF activate response code is: " + responseCode)
		checkNssmfResponse(httpResponse, execution)

        NssiResponse nssmfResponse = httpResponse.readEntity(NssiResponse.class)
		String jobId  = nssmfResponse.getJobId() ?: ""
 		execution.setVariable("JobId", jobId)

	}
	private void checkNssmfResponse(Response httpResponse, DelegateExecution execution) {
		int responseCode = httpResponse.getStatus()
		logger.debug("NSSMF response code is: " + responseCode)

		if ( responseCode < 200 || responseCode > 202 || !httpResponse.hasEntity()) {
			exceptionUtil.buildAndThrowWorkflowException(execution, responseCode, "Received a Bad Response from NSSMF.")
			String  isNSSIActivated = "false"
			execution.setVariable("isNSSIActivated", isNSSIActivated)
			execution.setVariable("isNSSIActivate","false")
		}else{
			String  isNSSIActivated = "true"
			execution.setVariable("isNSSIActivated", isNSSIActivated)
		}
	}


	void sendSyncError (DelegateExecution execution) {
		logger.trace("start sendSyncError")
		try {
			String errorMessage = ""
			if (execution.getVariable("WorkflowException") instanceof WorkflowException) {
				WorkflowException wfe = execution.getVariable("WorkflowException")
				errorMessage = wfe.getErrorMessage()
			} else {
				errorMessage = "Sending Sync Error."
			}

			String buildworkflowException =
					"""<aetgt:WorkflowException xmlns:aetgt="http://org.onap/so/workflow/schema/v1">
					<aetgt:ErrorMessage>${MsoUtils.xmlEscape(errorMessage)}</aetgt:ErrorMessage>
					<aetgt:ErrorCode>7000</aetgt:ErrorCode>
				   </aetgt:WorkflowException>"""

			logger.debug(buildworkflowException)
			sendWorkflowResponse(execution, 500, buildworkflowException)

		} catch (Exception ex) {
			logger.debug("Sending Sync Error Activity Failed. " + "\n" + ex.getMessage())
		}
		logger.trace("finished sendSyncError")
	}
}