Implant vid-app-common org.onap.vid.job (main and test)
[vid.git] / vid-app-common / src / main / java / org / onap / vid / mso / MsoUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2018 - 2019 Nokia. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.vid.mso;
23
24 import static org.onap.vid.utils.KotlinUtilsKt.JACKSON_OBJECT_MAPPER;
25
26 import com.fasterxml.jackson.core.JsonParseException;
27 import com.fasterxml.jackson.core.JsonProcessingException;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import io.joshworks.restclient.http.HttpResponse;
30 import java.io.IOException;
31 import org.apache.commons.lang3.StringUtils;
32 import org.apache.commons.lang3.exception.ExceptionUtils;
33 import org.onap.vid.exceptions.GenericUncheckedException;
34
35 public class MsoUtil {
36
37     static final ObjectMapper objectMapper = new ObjectMapper();
38
39     private MsoUtil() {
40     }
41
42     public static MsoResponseWrapper wrapResponse(RestObject<String> restObject) {
43         String response = restObject.get() != null ? restObject.get() : restObject.getRaw();
44         int status = restObject.getStatusCode();
45         return new MsoResponseWrapper(status, response);
46     }
47
48     public static <T> MsoResponseWrapper wrapResponse(HttpResponse<T> httpResponse)  {
49         MsoResponseWrapper msoResponseWrapper = new MsoResponseWrapper();
50         msoResponseWrapper.setStatus(httpResponse.getStatus());
51         if (httpResponse.getRawBody() != null) {
52             try {
53                 T body = httpResponse.getBody();
54                 String entityStr = body instanceof String ? (String) body : objectMapper.writeValueAsString(httpResponse.getBody());
55                 msoResponseWrapper.setEntity(entityStr);
56             } catch(JsonProcessingException e) {
57                 ExceptionUtils.rethrow(e);
58             }
59         }
60         return msoResponseWrapper;
61     }
62
63     public static String formatExceptionAdditionalInfo(int statusCode, String msoResponse) {
64         String errorMsg = "Http Code:" + statusCode;
65         if (!StringUtils.isEmpty(msoResponse)) {
66             String filteredJson;
67             try {
68                 filteredJson = StringUtils.defaultIfEmpty(
69                         JACKSON_OBJECT_MAPPER.readTree(msoResponse).path("serviceException").toString().replaceAll("[\\{\\}]","") ,
70                         msoResponse
71                 );
72             } catch (JsonParseException e) {
73                 filteredJson = msoResponse;
74             } catch (IOException e) {
75                 throw new GenericUncheckedException(e);
76             }
77
78             errorMsg = errorMsg + ", " + filteredJson;
79         }
80         return errorMsg;
81     }
82 }