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