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