bbaebb64dc1381ae90db8be66d2d2f6f0428b130
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / onap / so / bpmn / common / recipe / BpmnRestClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2018 Huawei Technologies Co., Ltd. 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.bpmn.common.recipe;
22
23 import java.io.IOException;
24 import java.security.GeneralSecurityException;
25
26 import javax.xml.bind.DatatypeConverter;
27
28 import org.apache.http.HttpResponse;
29 import org.apache.http.client.ClientProtocolException;
30 import org.apache.http.client.HttpClient;
31 import org.apache.http.client.config.RequestConfig;
32 import org.apache.http.client.methods.HttpPost;
33 import org.apache.http.entity.StringEntity;
34 import org.apache.http.impl.client.HttpClientBuilder;
35 import org.onap.so.bpmn.core.UrnPropertiesReader;
36 import org.onap.so.logger.MessageEnum;
37 import org.onap.so.logger.MsoLogger;
38 import org.onap.so.utils.CryptoUtils;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.stereotype.Component;
41
42 /**
43  * Support to call resource recipes from the BPMN workflow.
44  * Such as call resource recipe in service workflow.
45  * <br>
46  * <p>
47  * </p>
48  * 
49  * @author
50  * @version ONAP Beijing Release 2018-02-27
51  */
52 @Component
53 public class BpmnRestClient {
54
55     public static final String DEFAULT_BPEL_AUTH = "admin:admin";
56
57     public static final String ENCRYPTION_KEY = "aa3871669d893c7fb8abbcda31b88b4f";
58
59     public static final String CONTENT_TYPE_JSON = "application/json";
60
61     public static final String CAMUNDA_AUTH = "camundaAuth";
62
63     private static final  String MSO_PROP_APIHANDLER_INFRA = "MSO_PROP_APIHANDLER_INFRA";
64     @Autowired
65     private UrnPropertiesReader urnPropertiesReader;
66     private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, BpmnRestClient.class);
67
68     private static boolean noProperties = true;
69
70     //because for NS it will take a long time the time out of the resouce will be 2 hours.
71     private static final String DEFAULT_TIME_OUT = "7200";
72
73     public synchronized final boolean getNoPropertiesState() {
74         return noProperties;
75     }
76
77     /**
78      * post the recipe Uri
79      * <br>
80      * 
81      * @param recipeUri The request recipe uri
82      * @param requestId the request id
83      * @param recipeTimeout The recipe time out
84      * @param requestAction The request action
85      * @param serviceInstanceId The service instance id
86      * @param serviceType The service Type
87      * @param requestDetails The request Details, these information is from runtime
88      * @param recipeParamXsd The recipe params, its from recipe design
89      * @return The response of the recipe.
90      * @throws ClientProtocolException
91      * @throws IOException
92      * @since ONAP Beijing Release
93      */
94     public HttpResponse post(String recipeUri, String requestId, int recipeTimeout, String requestAction, String serviceInstanceId, String serviceType,
95             String requestDetails, String recipeParamXsd) throws ClientProtocolException, IOException {
96
97         HttpClient client = HttpClientBuilder.create().build();
98
99         HttpPost post = new HttpPost(recipeUri);
100         RequestConfig requestConfig =
101                 RequestConfig.custom().setSocketTimeout(recipeTimeout).setConnectTimeout(recipeTimeout).setConnectionRequestTimeout(recipeTimeout).build();
102         post.setConfig(requestConfig);
103         msoLogger.debug("call the bpmn,  url:" + recipeUri);
104         String jsonReq = wrapResourceRequest(requestId, recipeTimeout, requestAction, serviceInstanceId, serviceType, requestDetails, recipeParamXsd);
105
106         StringEntity input = new StringEntity(jsonReq);
107         input.setContentType(CONTENT_TYPE_JSON);
108         String encryptedCredentials;
109         encryptedCredentials = urnPropertiesReader.getVariable(CAMUNDA_AUTH);
110         if(encryptedCredentials != null) {
111             String userCredentials = getEncryptedPropValue(encryptedCredentials, DEFAULT_BPEL_AUTH, ENCRYPTION_KEY);
112             if(userCredentials != null) {
113                 post.addHeader("Authorization", "Basic " + DatatypeConverter.printBase64Binary(userCredentials.getBytes()));
114             }
115         }
116         
117         post.setEntity(input);
118         return client.execute(post);
119     }
120
121     /**
122      * prepare the resource recipe bpmn request.
123      * <br>
124      * 
125      * @param requestId
126      * @param recipeTimeout
127      * @param requestAction
128      * @param serviceInstanceId
129      * @param serviceType
130      * @param requestDetails
131      * @param recipeParams
132      * @return
133      * @since ONAP Beijing Release
134      */
135     private String wrapResourceRequest(String requestId, int recipeTimeout, String requestAction, String serviceInstanceId, String serviceType,
136             String requestDetails, String recipeParams) {
137         String jsonReq = null;
138         if(requestId == null) {
139             requestId = "";
140         }
141         if(requestAction == null) {
142             requestAction = "";
143         }
144         if(serviceInstanceId == null) {
145             serviceInstanceId = "";
146         }
147
148         if(requestDetails == null) {
149             requestDetails = "";
150         }
151
152         try {
153             ResourceRecipeRequest recipeRequest = new ResourceRecipeRequest();
154             BpmnParam resourceInput = new BpmnParam();
155             BpmnParam host = new BpmnParam();
156             BpmnParam requestIdInput = new BpmnParam();
157             BpmnParam requestActionInput = new BpmnParam();
158             BpmnParam serviceInstanceIdInput = new BpmnParam();
159             BpmnParam serviceTypeInput = new BpmnParam();
160             BpmnParam recipeParamsInput = new BpmnParam();
161             BpmnIntegerParam recipeTimeoutInput = new BpmnIntegerParam();
162             recipeTimeoutInput.setValue(recipeTimeout);
163             // host.setValue(parseURL());
164             requestIdInput.setValue(requestId);
165             requestActionInput.setValue(requestAction);
166             serviceInstanceIdInput.setValue(serviceInstanceId);
167             serviceTypeInput.setValue(serviceType);
168             recipeParamsInput.setValue(recipeParams);
169             resourceInput.setValue(requestDetails);
170             recipeRequest.setHost(host);
171             recipeRequest.setRequestId(requestIdInput);
172             recipeRequest.setRequestAction(requestActionInput);
173             recipeRequest.setServiceInstanceId(serviceInstanceIdInput);
174             recipeRequest.setServiceType(serviceTypeInput);
175             recipeRequest.setRecipeParams(recipeParamsInput);
176             recipeRequest.setResourceInput(resourceInput);
177             recipeRequest.setRecipeTimeout(recipeTimeoutInput);
178             jsonReq = recipeRequest.toString();
179             msoLogger.trace("request body is " + jsonReq);
180         } catch(Exception e) {
181             msoLogger.error(MessageEnum.APIH_WARP_REQUEST, "Camunda", "wrapVIDRequest", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH Warp request",
182                     e);
183         }
184         return jsonReq;
185     }
186
187     /**
188      * <br>
189      * 
190      * @param prop
191      * @param defaultValue
192      * @param encryptionKey
193      * @return
194      * @since ONAP Beijing Release
195      */
196     protected String getEncryptedPropValue(String prop, String defaultValue, String encryptionKey) {
197         try {
198             return CryptoUtils.decrypt(prop, encryptionKey);
199         } catch(GeneralSecurityException e) {
200             msoLogger.debug("Security exception", e);
201         }
202         return defaultValue;
203     }
204
205 }