3 * ================================================================================
4 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
5 * =============================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * * ============LICENSE_END=========================================================
19 package org.onap.appc.flow.controller.executorImpl;
21 import com.att.eelf.configuration.EELFLogger;
22 import com.att.eelf.configuration.EELFManager;
23 import org.glassfish.jersey.client.ClientConfig;
24 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
25 import org.glassfish.jersey.client.ClientProperties;
27 import javax.ws.rs.client.Client;
28 import javax.ws.rs.client.ClientBuilder;
29 import javax.ws.rs.client.Entity;
30 import javax.ws.rs.client.WebTarget;
31 import javax.ws.rs.core.Response.Status;
32 import javax.ws.rs.core.Feature;
34 import java.util.Collections;
35 import java.util.HashMap;
37 import java.util.Optional;
38 import javax.net.ssl.HostnameVerifier;
39 import javax.net.ssl.SSLContext;
40 import javax.ws.rs.HttpMethod;
41 import javax.ws.rs.core.MediaType;
42 import org.apache.commons.lang3.StringUtils;
43 import org.onap.appc.flow.controller.data.Response;
44 import org.onap.appc.flow.controller.data.Transaction;
45 import org.onap.appc.flow.controller.interfaces.FlowExecutorInterface;
46 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
49 public class RestExecutor implements FlowExecutorInterface {
51 private static final EELFLogger log = EELFManager.getInstance().getLogger(RestExecutor.class);
54 public Map<String, String> execute(Transaction transaction, SvcLogicContext ctx) throws Exception {
55 log.info("Configuring Rest Operation....." + transaction.toString());
56 Map<String, String> outputMessage = new HashMap<>();
60 System.setProperty("jsse.enableSNIExtension", "false");
61 SSLContext sslContext = SSLContext.getInstance("SSL");
62 sslContext.init(null, new javax.net.ssl.TrustManager[] {new SecureRestClientTrustManager()}, null);
63 client = createClient(sslContext);
64 if ((transaction.getuId() != null) && (transaction.getPswd() != null)) {
65 client.register(HttpAuthenticationFeature.basic(transaction.getuId(), transaction.getPswd()));
67 WebTarget webResource = client.target(new URI(transaction.getExecutionEndPoint()));
68 webResource.property("Content-Type", "application/json;charset=UTF-8");
70 javax.ws.rs.core.Response clientResponse = getClientResponse(transaction, webResource).orElseThrow(() -> new Exception(
71 "Cannot determine the state of : " + transaction.getActionLevel() + " HTTP response is null"));
73 processClientResponse(clientResponse, transaction, outputMessage);
75 log.info("Completed Rest Operation.....");
77 } catch (Exception e) {
78 log.debug("failed in RESTCONT Action (" + transaction.getExecutionRPC() + ") for the resource "
79 + transaction.getExecutionEndPoint() + ", fault message :" + e.getMessage());
80 throw new Exception("Error While Sending Rest Request", e);
90 private HostnameVerifier getHostnameVerifier() {
91 return (hostname, sslSession) -> true;
94 Client createClient(SSLContext ctx) {
95 return ClientBuilder.newBuilder().sslContext(ctx).hostnameVerifier(getHostnameVerifier()).build();
98 private Optional<javax.ws.rs.core.Response> getClientResponse(Transaction transaction, WebTarget webResource) {
99 String responseDataType = MediaType.APPLICATION_JSON;
100 String requestDataType = MediaType.APPLICATION_JSON;
101 javax.ws.rs.core.Response clientResponse = null;
103 log.info("Starting Rest Operation.....");
104 if (HttpMethod.GET.equalsIgnoreCase(transaction.getExecutionRPC())) {
105 clientResponse = webResource.request(responseDataType).get(javax.ws.rs.core.Response.class);
106 } else if (HttpMethod.POST.equalsIgnoreCase(transaction.getExecutionRPC())) {
107 clientResponse = webResource.request(requestDataType).post(Entity.json(transaction.getPayload()),
108 javax.ws.rs.core.Response.class);
109 } else if (HttpMethod.PUT.equalsIgnoreCase(transaction.getExecutionRPC())) {
110 clientResponse = webResource.request(requestDataType).put(Entity.json(transaction.getPayload()),
111 javax.ws.rs.core.Response.class);
112 } else if (HttpMethod.DELETE.equalsIgnoreCase(transaction.getExecutionRPC())) {
113 clientResponse = webResource.request(requestDataType).delete(javax.ws.rs.core.Response.class);
115 return Optional.ofNullable(clientResponse);
118 private void processClientResponse(javax.ws.rs.core.Response clientResponse, Transaction transaction,
119 Map<String, String> outputMessage) throws Exception {
121 if (clientResponse.getStatus() == Status.OK.getStatusCode()) {
122 Response response = new Response();
123 response.setResponseCode(String.valueOf(Status.OK.getStatusCode()));
124 transaction.setResponses(Collections.singletonList(response));
125 outputMessage.put("restResponse", clientResponse.readEntity(String.class));
127 String errorMsg = clientResponse.readEntity(String.class);
128 if (StringUtils.isNotBlank(errorMsg)) {
129 log.debug("Error Message from Client Response" + errorMsg);
131 throw new Exception("Cannot determine the state of : " + transaction.getActionLevel()
132 + " HTTP error code : " + clientResponse.getStatus());