2f69e397c38a3c9319c084032b90dbed2d1e275f
[vid.git] / vid-app-common / src / main / java / org / onap / vid / aai / PombaRestInterface.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  * Modifications Copyright (C) 2019 IBM.
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.vid.aai;
24
25 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
26 import org.onap.vid.aai.util.*;
27 import org.onap.vid.utils.Logging;
28 import org.springframework.http.HttpMethod;
29
30 import javax.ws.rs.client.Client;
31 import javax.ws.rs.client.Entity;
32 import javax.ws.rs.core.MediaType;
33 import javax.ws.rs.core.Response;
34 import java.util.UUID;
35
36 import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY;
37
38 public class PombaRestInterface extends AAIRestInterface {
39         
40     private Client client = null;
41
42     public PombaRestInterface (HttpsAuthClient httpsAuthClientFactory, ServletRequestHelper servletRequestHelper, SystemPropertyHelper systemPropertyHelper) {
43         super(httpsAuthClientFactory, servletRequestHelper, systemPropertyHelper);
44     }
45
46     @Override
47     protected void initRestClient()
48     {
49         if (client == null) {
50             try {
51                 client = httpsAuthClientFactory.getClient(HttpClientMode.WITH_KEYSTORE);
52             }
53             catch (Exception e) {
54                 logger.info(EELFLoggerDelegate.errorLogger, "Exception in REST call to DB in initRestClient", e);
55                 logger.debug(EELFLoggerDelegate.debugLogger, "Exception in REST call to DB : ", e);
56             }
57         }
58     }
59
60
61     public Response RestPost(String fromAppId, String url, String payload) {
62         String methodName = "RestPost";
63         String transId = UUID.randomUUID().toString();
64         try {
65             initRestClient();
66
67             Logging.logRequest(outgoingRequestsLogger, HttpMethod.POST, url, payload);
68             final Response cres = client.target(url)
69                     .request()
70                     .accept(MediaType.APPLICATION_JSON)
71                     .header(TRANSACTION_ID_HEADER, transId)
72                     .header(FROM_APP_ID_HEADER,  fromAppId)
73                     .header(REQUEST_ID_HEADER_KEY, extractOrGenerateRequestId())
74                     .post(Entity.entity(payload, MediaType.APPLICATION_JSON));
75             Logging.logResponse(outgoingRequestsLogger, HttpMethod.POST, url, cres);
76
77             if (cres.getStatusInfo().getFamily().equals(Response.Status.Family.SUCCESSFUL)) {
78                 logger.info(EELFLoggerDelegate.errorLogger, getValidResponseLogMessage(methodName));
79                 logger.debug(EELFLoggerDelegate.debugLogger, getValidResponseLogMessage(methodName));
80             } else {
81                 logger.debug(EELFLoggerDelegate.debugLogger, getInvalidResponseLogMessage(url, methodName, cres));
82             }
83             return cres;
84         } catch (Exception e) {
85             logger.debug(EELFLoggerDelegate.debugLogger, getFailedResponseLogMessage(url, methodName, e));
86         }
87         return null;
88     }
89
90 }
91
92