c4bc852ff93aab12a7dec1aaacf1c51f2c9a94e2
[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     private void initRestClient()
46     {
47         if (client == null) {
48             try {
49                 client = httpsAuthClientFactory.getClient(HttpClientMode.UNSECURE);
50             }
51             catch (Exception e) {
52                 logger.info(EELFLoggerDelegate.errorLogger, "Exception in REST call to DB in initRestClient" + e.toString());
53                 logger.debug(EELFLoggerDelegate.debugLogger, "Exception in REST call to DB : " + e.toString());
54             }
55         }
56     }
57
58
59     public Response RestPost(String fromAppId, String url, String payload) {
60         String methodName = "RestPost";
61         String transId = UUID.randomUUID().toString();
62         try {
63             initRestClient();
64
65             Logging.logRequest(outgoingRequestsLogger, HttpMethod.POST, url, payload);
66             final Response cres = client.target(url)
67                     .request()
68                     .accept(MediaType.APPLICATION_JSON)
69                     .header(TRANSACTION_ID_HEADER, transId)
70                     .header(FROM_APP_ID_HEADER,  fromAppId)
71                     .header(REQUEST_ID_HEADER_KEY, extractOrGenerateRequestId())
72                     .post(Entity.entity(payload, MediaType.APPLICATION_JSON));
73             Logging.logResponse(outgoingRequestsLogger, HttpMethod.POST, url, cres);
74
75             if (cres.getStatusInfo().getFamily().equals(Response.Status.Family.SUCCESSFUL)) {
76                 logger.info(EELFLoggerDelegate.errorLogger, getValidResponseLogMessage(methodName));
77                 logger.debug(EELFLoggerDelegate.debugLogger, getValidResponseLogMessage(methodName));
78             } else {
79                 logger.debug(EELFLoggerDelegate.debugLogger, getInvalidResponseLogMessage(url, methodName, cres));
80             }
81         } catch (Exception e) {
82             logger.debug(EELFLoggerDelegate.debugLogger, getFailedResponseLogMessage(url, methodName, e));
83         }
84         return null;
85     }
86
87 }
88
89