diff options
author | Piyush Garg <piyush.garg1@amdocs.com> | 2018-08-29 15:40:53 +0530 |
---|---|---|
committer | Piyush Garg <piyush.garg1@amdocs.com> | 2018-08-31 20:48:21 +0000 |
commit | 0b6288e180c1d33bba88b3df5e03442c57058db9 (patch) | |
tree | 072c9c553f9ac7a4874ce97101be5c6e211d547e /controlloop/common/model-impl/rest/src/main | |
parent | 8279af376b435e1d7dd118a1955c5681edf3b847 (diff) |
Added support for VF Module Delete recipe
Enhanced SOActorServiceProvider to support VF Module Delete Recipe.
Added SOOperationType enum to map the policy recipe with the SO API that
we want to call for a recipe. vf module id from non-base vf module is
used to construct the delete request url and same will be deleted.
Updated SOManager so that it sends request to SO based on the
SOOperationType enum value.
Enhanced RESTManager to add support for the http delete using new class
HttpDeleteWithBody (to allow pass the payload to the SO delete VF module API).
Change-Id: I15b678ed9ebc85dfa7cb62bbf23e41c0fe6e4c69
Issue-ID: POLICY-1079
Signed-off-by: Piyush Garg <piyush.garg1@amdocs.com>
Diffstat (limited to 'controlloop/common/model-impl/rest/src/main')
2 files changed, 158 insertions, 90 deletions
diff --git a/controlloop/common/model-impl/rest/src/main/java/org/onap/policy/rest/HttpDeleteWithBody.java b/controlloop/common/model-impl/rest/src/main/java/org/onap/policy/rest/HttpDeleteWithBody.java new file mode 100644 index 000000000..262872262 --- /dev/null +++ b/controlloop/common/model-impl/rest/src/main/java/org/onap/policy/rest/HttpDeleteWithBody.java @@ -0,0 +1,52 @@ +/* + * ============LICENSE_START======================================================= + * rest + * ================================================================================ + * Copyright (C) 2018 Amdocs. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.rest; + +import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; + +import java.net.URI; + + +/** + * Allows for HTTP DELETE requests to contain a body, which the HttpDelete + * class does not support. + */ +public class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase { + public static final String METHOD_NAME = "DELETE"; + + public String getMethod() { + return METHOD_NAME; + } + + public HttpDeleteWithBody(final String uri) { + super(); + setURI(URI.create(uri)); + } + + public HttpDeleteWithBody(final URI uri) { + super(); + setURI(uri); + } + + public HttpDeleteWithBody() { + super(); + } +} diff --git a/controlloop/common/model-impl/rest/src/main/java/org/onap/policy/rest/RESTManager.java b/controlloop/common/model-impl/rest/src/main/java/org/onap/policy/rest/RESTManager.java index 39e0d8a7f..a7e373b5f 100644 --- a/controlloop/common/model-impl/rest/src/main/java/org/onap/policy/rest/RESTManager.java +++ b/controlloop/common/model-impl/rest/src/main/java/org/onap/policy/rest/RESTManager.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -20,15 +20,16 @@ package org.onap.policy.rest; -import java.io.IOException; import java.nio.charset.Charset; import java.util.Map; import java.util.Map.Entry; import javax.xml.bind.DatatypeConverter; + import org.apache.http.HttpHeaders; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; @@ -42,126 +43,141 @@ public class RESTManager { private static final Logger logger = LoggerFactory.getLogger(RESTManager.class); public class Pair<A, B> { - public final A a; - public final B b; + public final A first; + public final B second; - public Pair(A a, B b) { - this.a = a; - this.b = b; + public Pair(A first, B second) { + this.first = first; + this.second = second; } } /** * Perform REST Post. - * - * @param url the url - * @param username the user name - * @param password the password - * @param headers any headers + * + * @param url the url + * @param username the user name + * @param password the password + * @param headers any headers * @param contentType what the content type is - * @param body body to send + * @param body body to send * @return the response status code and the body */ public Pair<Integer, String> post(String url, String username, String password, - Map<String, String> headers, String contentType, String body) { - - String authHeader = makeAuthHeader(username, password); - - logger.debug("HTTP REQUEST: {} -> {} {} -> {}", url, username, - ((password != null) ? password.length() : "-"), contentType); - if (headers != null) { - logger.debug("Headers: "); - headers.forEach((name, value) -> logger.debug("{} -> {}", name, value)); + Map<String, String> headers, String contentType, String body) { + HttpPost post = new HttpPost(url); + addHeaders(post, username, password, headers); + post.addHeader("Content-Type", contentType); + try { + StringEntity input = new StringEntity(body); + input.setContentType(contentType); + post.setEntity(input); + } catch (Exception e) { + logger.error("post threw: ", e); + return null; } - logger.debug(body); + return sendRequest(post); + } - try (CloseableHttpClient client = - HttpClientBuilder - .create() - .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE) - .build()) { - - HttpPost post = new HttpPost(url); - if (headers != null) { - for (Entry<String, String> entry : headers.entrySet()) { - post.addHeader(entry.getKey(), headers.get(entry.getKey())); - } - } - post.addHeader("Content-Type", contentType); - if (authHeader != null) { - post.setHeader(HttpHeaders.AUTHORIZATION, authHeader); + /** + * Do a REST get. + * + * @param url URL + * @param username user name + * @param password password + * @param headers any headers to add + * @return a Pair for the response status and the body + */ + public Pair<Integer, String> get(String url, String username, String password, + Map<String, String> headers) { + HttpGet get = new HttpGet(url); + addHeaders(get, username, password, headers); + return sendRequest(get); + } + + /** + * Perform REST Delete. + * + * @param url the url + * @param username the user name + * @param password the password + * @param headers any headers + * @param contentType what the content type is + * @param body body (optional) to send + * @return the response status code and the body + */ + public Pair<Integer, String> delete(String url, String username, String password, Map<String, String> headers, + String contentType, String body) { + HttpDeleteWithBody delete = new HttpDeleteWithBody(url); + addHeaders(delete, username, password, headers); + delete.addHeader("Content-Type", contentType); + if (body != null && !body.isEmpty()) { + try { + StringEntity input = new StringEntity(body); + input.setContentType(contentType); + delete.setEntity(input); + } catch (Exception e) { + logger.error("delete threw: ", e); + return null; } + } + return sendRequest(delete); + } - StringEntity input = new StringEntity(body); - input.setContentType(contentType); - post.setEntity(input); + /** + * Send REST request. + * + * @param request http request to send + * @return the response status code and the body + */ + private Pair<Integer, String> sendRequest(HttpRequestBase request) { + if (logger.isDebugEnabled()) { + logger.debug("***** sendRequest to url {}:", request.getURI()); + } - HttpResponse response = client.execute(post); + try (CloseableHttpClient client = + HttpClientBuilder + .create() + .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE) + .build()) { + HttpResponse response = client.execute(request); if (response != null) { String returnBody = EntityUtils.toString(response.getEntity(), "UTF-8"); - logger.debug("HTTP POST Response Status Code: {}", + logger.debug("HTTP Response Status Code: {}", response.getStatusLine().getStatusCode()); - logger.debug("HTTP POST Response Body:"); + logger.debug("HTTP Response Body:"); logger.debug(returnBody); return new Pair<>(response.getStatusLine().getStatusCode(), returnBody); - } - else { - logger.error("Response from {} is null", url); + } else { + logger.error("Response from {} is null", request.getURI()); return null; } - } - catch (Exception e) { - logger.error("Failed to POST to {}", url, e); + } catch (Exception e) { + logger.error("Request failed to {}", request.getURI(), e); return null; } } /** - * Do a REST get. - * - * @param url URL - * @param username user name - * @param password password - * @param headers any headers to add - * @return a Pair for the response status and the body + * Add header to the request. + * + * @param request http request to send + * @param username the user name + * @param password the password + * @param headers any headers */ - public Pair<Integer, String> get(String url, String username, String password, - Map<String, String> headers) { - + private void addHeaders(HttpRequestBase request, String username, String password, Map<String, + String> headers) { String authHeader = makeAuthHeader(username, password); - - try (CloseableHttpClient client = - HttpClientBuilder - .create() - .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE) - .build()) { - - HttpGet get = new HttpGet(url); - if (headers != null) { - for (Entry<String, String> entry : headers.entrySet()) { - get.addHeader(entry.getKey(), headers.get(entry.getKey())); - } - } - if (authHeader != null) { - get.setHeader(HttpHeaders.AUTHORIZATION, authHeader); + if (headers != null) { + for (Entry<String, String> entry : headers.entrySet()) { + request.addHeader(entry.getKey(), headers.get(entry.getKey())); } - - HttpResponse response = client.execute(get); - - String returnBody = EntityUtils.toString(response.getEntity(), "UTF-8"); - - logger.debug("HTTP GET Response Status Code: {}", - response.getStatusLine().getStatusCode()); - logger.debug("HTTP GET Response Body:"); - logger.debug(returnBody); - - return new Pair<>(response.getStatusLine().getStatusCode(), returnBody); } - catch (IOException e) { - logger.error("Failed to GET to {}", url, e); - return null; + if (authHeader != null) { + request.setHeader(HttpHeaders.AUTHORIZATION, authHeader); } } |