summaryrefslogtreecommitdiffstats
path: root/bpmn/MSOCommonBPMN/src/main/groovy/org/onap/so/bpmn/common/scripts/DoHandleOofRequest.groovy
blob: 1964a189f17c60778eb9b039a7cbf31ecd3a55dc (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
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2020 Wipro Limited. 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.onap.so.bpmn.common.scripts

import javax.ws.rs.core.Response

import org.apache.commons.lang3.StringUtils
import org.camunda.bpm.engine.delegate.DelegateExecution
import org.onap.logging.filter.base.ONAPComponents
import org.onap.so.bpmn.core.UrnPropertiesReader
import org.onap.so.bpmn.core.json.JsonUtils
import org.onap.so.client.HttpClient
import org.onap.so.client.HttpClientFactory
import org.onap.so.client.oof.adapter.beans.payload.OofRequest
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import com.fasterxml.jackson.databind.ObjectMapper


import static org.onap.so.bpmn.common.scripts.GenericUtils.*


class DoHandleOofRequest extends AbstractServiceTaskProcessor {
	
	ExceptionUtil exceptionUtil = new ExceptionUtil()
	JsonUtils jsonUtil = new JsonUtils()
	private static final Logger logger = LoggerFactory.getLogger(DoHandleOofRequest.class)

	@Override
	public void preProcessRequest(DelegateExecution execution) {
		logger.debug("In Preprocess Oof Request Handler")
		String apiPath = execution.getVariable("apiPath")
		if (isBlank(apiPath)) {
			String msg = "Cannot process OOF adapter call : API PATH is null"
			exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
		} 
		
		//msoRequestId is used for correlation
		String requestId = execution.getVariable("correlator")
		if (isBlank(requestId)) {
			String msg = "Cannot process OOF adapter call : correlator is null"
			exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
		}
		
		String messageType = execution.getVariable("messageType")
		if (isBlank(messageType)) {
			String msg = "Cannot process OOF adapter call : messageType is null"
			exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
		}
		
		String timeout = execution.getVariable("timeout")
		if (isBlank(timeout)) {
			timeout = UrnPropertiesReader.getVariable("mso.adapters.oof.timeout", execution);
			if (isBlank(timeout)) {
				logger.debug("Setting OOF timeout to default : PT30M")
				timeout = "PT30M"
			}
		}
		
		Object requestDetails = execution.getVariable("oofRequest")
		OofRequest oofRequestPayload = new OofRequest()
		oofRequestPayload.setApiPath(apiPath)
		oofRequestPayload.setRequestDetails(requestDetails)
		ObjectMapper objectMapper = new ObjectMapper()
		String requestJson = objectMapper.writeValueAsString(oofRequestPayload)
		execution.setVariable("oofRequestPayload", requestJson)
	}
	
	public void callOofAdapter(DelegateExecution execution) {
		logger.debug("Start callOofAdapter")
		String requestId = execution.getVariable("msoRequestId")
		String oofAdapterEndpoint = UrnPropertiesReader.getVariable("mso.adapters.oof.endpoint", execution)
		String basicAuthCred = execution.getVariable("BasicAuthHeaderValue")
		URL requestUrl = new URL(oofAdapterEndpoint)
		String oofRequest = execution.getVariable("oofRequestPayload")
		logger.debug("oofRequest : " + oofRequest)
		HttpClient httpClient = new HttpClientFactory().newJsonClient(requestUrl, ONAPComponents.EXTERNAL)
		Response httpResponse = httpClient.post(oofRequest)
		int responseCode = httpResponse.getStatus()
		logger.debug("OOF sync response code is: " + responseCode)
        if(responseCode < 200 || responseCode >= 300){
			exceptionUtil.buildAndThrowWorkflowException(execution, responseCode, "Received a Bad Sync Response from OOF.")
		}
	}
	
}