aboutsummaryrefslogtreecommitdiffstats
path: root/mso-api-handlers/mso-api-handler-common/src/main/java/org/openecomp/mso/apihandler/common/CamundaClient.java
blob: d893a12b24f577827c556e09817d48c131fa4517 (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
/*-
 * ============LICENSE_START=======================================================
 * OPENECOMP - MSO
 * ================================================================================
 * 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.apihandler.common;


import org.openecomp.mso.apihandler.camundabeans.*;
import org.openecomp.mso.logger.MessageEnum;
import org.openecomp.mso.logger.MsoLogger;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;

import javax.xml.bind.DatatypeConverter;
import java.io.IOException;

public class CamundaClient extends RequestClient{
	private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.APIH);

	public CamundaClient() {
		super(CommonConstants.CAMUNDA);
	}


	@Override
	public HttpResponse post(String camundaReqXML, String requestId,
			String requestTimeout, String schemaVersion, String serviceInstanceId, String action)
					throws ClientProtocolException, IOException{
		HttpPost post = new HttpPost(url);
		msoLogger.debug("Camunda url is: "+ url);
		String jsonReq = wrapRequest(camundaReqXML, requestId, serviceInstanceId, requestTimeout,  schemaVersion);

		StringEntity input = new StringEntity(jsonReq);
		input.setContentType(CommonConstants.CONTENT_TYPE_JSON);

		String encryptedCredentials = null;
		if(props!=null){
			encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH,null);
			if(encryptedCredentials != null){
				String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, CommonConstants.ENCRYPTION_KEY);
				if(userCredentials != null){
					post.addHeader("Authorization", "Basic " + new String(DatatypeConverter.printBase64Binary(userCredentials.getBytes())));
				}
			}
		}

		post.setEntity(input);
		HttpResponse response = client.execute(post);

		return response;
	}

	@Override
	public HttpResponse post(String jsonReq)
					throws ClientProtocolException, IOException{
		HttpPost post = new HttpPost(url);
		msoLogger.debug("Camunda url is: "+ url);
		//String jsonReq = wrapRequest(camundaReqXML, requestId, serviceInstanceId, requestTimeout,  schemaVersion);

		StringEntity input = new StringEntity(jsonReq);
		input.setContentType(CommonConstants.CONTENT_TYPE_JSON);

		String encryptedCredentials = null;
		if(props!=null){
			encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH,null);
			if(encryptedCredentials != null){
				String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, CommonConstants.ENCRYPTION_KEY);
				if(userCredentials != null){
					post.addHeader("Authorization", "Basic " + new String(DatatypeConverter.printBase64Binary(userCredentials.getBytes())));
				}
			}
		}

		post.setEntity(input);
		HttpResponse response = client.execute(post);

		return response;
	}

	@Override
	public HttpResponse post(String requestId, boolean isBaseVfModule,
			int recipeTimeout, String requestAction, String serviceInstanceId,
			String vnfId, String vfModuleId, String volumeGroupId, String networkId,
			String serviceType, String vnfType, String vfModuleType, String networkType,
			String requestDetails)
					throws ClientProtocolException, IOException{
		HttpPost post = new HttpPost(url);
		msoLogger.debug("Camunda url is: "+ url);
		String jsonReq = wrapVIDRequest(requestId, isBaseVfModule, recipeTimeout, requestAction,
				serviceInstanceId, vnfId, vfModuleId, volumeGroupId, networkId,
				serviceType, vnfType, vfModuleType, networkType, requestDetails);

		StringEntity input = new StringEntity(jsonReq);
		input.setContentType(CommonConstants.CONTENT_TYPE_JSON);

		String encryptedCredentials = null;
		if(props!=null){
			encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH,null);
			if(encryptedCredentials != null){
				String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, CommonConstants.ENCRYPTION_KEY);
				if(userCredentials != null){
					post.addHeader("Authorization", "Basic " + new String(DatatypeConverter.printBase64Binary(userCredentials.getBytes())));
				}
			}
		}

		post.setEntity(input);
		HttpResponse response = client.execute(post);

		return response;
	}

	private String wrapRequest(String reqXML, String requestId, String serviceInstanceId, String requestTimeout, String schemaVersion){
		String jsonReq = null;
		if(reqXML == null){
			reqXML ="";
		}
		if(requestTimeout == null){
			requestTimeout ="";
		}
		if(schemaVersion == null){
			schemaVersion = "";
		}


		try{
			CamundaRequest camundaRequest = new CamundaRequest();
			CamundaInput camundaInput = new CamundaInput();
			CamundaInput host = new CamundaInput();
			CamundaInput schema = new CamundaInput();
			CamundaInput reqid = new CamundaInput();
			CamundaInput svcid = new CamundaInput();
			CamundaInput timeout = new CamundaInput();
			camundaInput.setValue(reqXML);
			host.setValue(parseURL());
			schema.setValue(schemaVersion);
			reqid.setValue(requestId);
			svcid.setValue(serviceInstanceId);
			timeout.setValue(requestTimeout);
			camundaRequest.setServiceInput(camundaInput);
			camundaRequest.setHost(host);
			camundaRequest.setReqid(reqid);
			camundaRequest.setSvcid(svcid);
			camundaRequest.setSchema(schema);
			camundaRequest.setTimeout(timeout);
			ObjectMapper mapper = new ObjectMapper();
			mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);

			jsonReq = mapper.writeValueAsString(camundaRequest);
			msoLogger.debug("request body is " + jsonReq);
		}catch(Exception e){
			msoLogger.error(MessageEnum.APIH_WARP_REQUEST, "Camunda", "wrapRequest", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH Warp request", e);
		}
		return jsonReq;
	}

	private String wrapVIDRequest(String requestId, boolean isBaseVfModule,
			int recipeTimeout, String requestAction, String serviceInstanceId,
			String vnfId, String vfModuleId, String volumeGroupId, String networkId,
			String serviceType, String vnfType, String vfModuleType, String networkType,
			String requestDetails){
		String jsonReq = null;
		if(requestId == null){
			requestId ="";
		}
		if(requestAction == null){
			requestAction ="";
		}
		if(serviceInstanceId == null){
			serviceInstanceId ="";
		}
		if(vnfId == null){
			vnfId ="";
		}
		if(vfModuleId == null){
			vfModuleId ="";
		}
		if(volumeGroupId == null){
			volumeGroupId ="";
		}
		if(networkId == null){
			networkId ="";
		}
		if(serviceType == null){
			serviceType ="";
		}
		if(vnfType == null){
			vnfType ="";
		}
		if(vfModuleType == null){
			vfModuleType ="";
		}
		if(networkType == null){
			networkType ="";
		}
		if(requestDetails == null){
			requestDetails ="";
		}



		try{
			CamundaVIDRequest camundaRequest = new CamundaVIDRequest();
			CamundaInput serviceInput = new CamundaInput();
			CamundaInput host = new CamundaInput();
			CamundaInput requestIdInput= new CamundaInput();
			CamundaBooleanInput isBaseVfModuleInput = new CamundaBooleanInput();
			CamundaIntegerInput recipeTimeoutInput = new CamundaIntegerInput();
			CamundaInput requestActionInput = new CamundaInput();
			CamundaInput serviceInstanceIdInput = new CamundaInput();
			CamundaInput vnfIdInput = new CamundaInput();
			CamundaInput vfModuleIdInput = new CamundaInput();
			CamundaInput volumeGroupIdInput = new CamundaInput();
			CamundaInput networkIdInput = new CamundaInput();
			CamundaInput serviceTypeInput = new CamundaInput();
			CamundaInput vnfTypeInput = new CamundaInput();
			CamundaInput vfModuleTypeInput = new CamundaInput();
			CamundaInput networkTypeInput = new CamundaInput();

			host.setValue(parseURL());
			requestIdInput.setValue(requestId);
			isBaseVfModuleInput.setValue(isBaseVfModule);
			recipeTimeoutInput.setValue(recipeTimeout);
			requestActionInput.setValue(requestAction);
			serviceInstanceIdInput.setValue(serviceInstanceId);
			vnfIdInput.setValue(vnfId);
			vfModuleIdInput.setValue(vfModuleId);
			volumeGroupIdInput.setValue(volumeGroupId);
			networkIdInput.setValue(networkId);
			serviceTypeInput.setValue(serviceType);
			vnfTypeInput.setValue(vnfType);
			vfModuleTypeInput.setValue(vfModuleType);
			networkTypeInput.setValue(networkType);

			serviceInput.setValue(requestDetails);
			camundaRequest.setServiceInput(serviceInput);
			camundaRequest.setHost(host);
			camundaRequest.setRequestId(requestIdInput);
			camundaRequest.setMsoRequestId(requestIdInput);
			camundaRequest.setIsBaseVfModule(isBaseVfModuleInput);
			camundaRequest.setRecipeTimeout(recipeTimeoutInput);
			camundaRequest.setRequestAction(requestActionInput);
			camundaRequest.setServiceInstanceId(serviceInstanceIdInput);
			camundaRequest.setVnfId(vnfIdInput);
			camundaRequest.setVfModuleId(vfModuleIdInput);
			camundaRequest.setVolumeGroupId(volumeGroupIdInput);
			camundaRequest.setNetworkId(networkIdInput);
			camundaRequest.setServiceType(serviceTypeInput);
			camundaRequest.setVnfType(vnfTypeInput);
			camundaRequest.setVfModuleType(vfModuleTypeInput);
			camundaRequest.setNetworkType(networkTypeInput);

			ObjectMapper mapper = new ObjectMapper();
			mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);

			jsonReq = mapper.writeValueAsString(camundaRequest);
			msoLogger.debug("request body is " + jsonReq);
		}catch(Exception e){
			msoLogger.error(MessageEnum.APIH_WARP_REQUEST, "Camunda", "wrapVIDRequest", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH Warp request", e);
		}
		return jsonReq;
	}

	private String parseURL(){
		String[] parts = url.split(":");
		String host = "";
		if(parts.length>=2){
			host = parts[1];
			if(host.length()>2){
				host = host.substring(2);
			}
		}
		return host;
	}


}