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