summaryrefslogtreecommitdiffstats
path: root/controlloop/common/model-impl/rest
diff options
context:
space:
mode:
authorPiyush Garg <piyush.garg1@amdocs.com>2018-08-29 15:40:53 +0530
committerPiyush Garg <piyush.garg1@amdocs.com>2018-08-31 20:48:21 +0000
commit0b6288e180c1d33bba88b3df5e03442c57058db9 (patch)
tree072c9c553f9ac7a4874ce97101be5c6e211d547e /controlloop/common/model-impl/rest
parent8279af376b435e1d7dd118a1955c5681edf3b847 (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')
-rw-r--r--controlloop/common/model-impl/rest/src/main/java/org/onap/policy/rest/HttpDeleteWithBody.java52
-rw-r--r--controlloop/common/model-impl/rest/src/main/java/org/onap/policy/rest/RESTManager.java196
-rw-r--r--controlloop/common/model-impl/rest/src/test/java/org/onap/policy/rest/HttpDeleteWithBodyTest.java36
-rwxr-xr-xcontrolloop/common/model-impl/rest/src/test/java/org/onap/policy/rest/TestGet.java18
-rwxr-xr-xcontrolloop/common/model-impl/rest/src/test/java/org/onap/policy/rest/TestPair.java8
5 files changed, 207 insertions, 103 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);
}
}
diff --git a/controlloop/common/model-impl/rest/src/test/java/org/onap/policy/rest/HttpDeleteWithBodyTest.java b/controlloop/common/model-impl/rest/src/test/java/org/onap/policy/rest/HttpDeleteWithBodyTest.java
new file mode 100644
index 000000000..0cb5f3aef
--- /dev/null
+++ b/controlloop/common/model-impl/rest/src/test/java/org/onap/policy/rest/HttpDeleteWithBodyTest.java
@@ -0,0 +1,36 @@
+/*
+ * ============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 static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+public class HttpDeleteWithBodyTest {
+
+ @Test
+ public void getMethod() {
+ String url = "http://www.example.org";
+ HttpDeleteWithBody deleteWithBody = new HttpDeleteWithBody(url);
+ assertEquals("DELETE", deleteWithBody.getMethod());
+ assertEquals(url, deleteWithBody.getURI().toString());
+ }
+} \ No newline at end of file
diff --git a/controlloop/common/model-impl/rest/src/test/java/org/onap/policy/rest/TestGet.java b/controlloop/common/model-impl/rest/src/test/java/org/onap/policy/rest/TestGet.java
index a8301810c..7e881df51 100755
--- a/controlloop/common/model-impl/rest/src/test/java/org/onap/policy/rest/TestGet.java
+++ b/controlloop/common/model-impl/rest/src/test/java/org/onap/policy/rest/TestGet.java
@@ -39,9 +39,9 @@ public class TestGet {
RESTManager mgr = new RESTManager();
Pair<Integer, String> result = mgr.get("http://www.example.org", null, null, null);
- assertEquals((Integer)200, result.a);
- assertTrue(result.b != null);
- assertTrue(result.b.length() > 0);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
}
@Test
@@ -49,9 +49,9 @@ public class TestGet {
RESTManager mgr = new RESTManager();
Pair<Integer, String> result = mgr.get("http://www.example.org", "", null, null);
- assertEquals((Integer)200, result.a);
- assertTrue(result.b != null);
- assertTrue(result.b.length() > 0);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
}
@Test
@@ -59,8 +59,8 @@ public class TestGet {
RESTManager mgr = new RESTManager();
Pair<Integer, String> result = mgr.get("http://www.example.org", "user", null, null);
- assertEquals((Integer)200, result.a);
- assertTrue(result.b != null);
- assertTrue(result.b.length() > 0);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
}
} \ No newline at end of file
diff --git a/controlloop/common/model-impl/rest/src/test/java/org/onap/policy/rest/TestPair.java b/controlloop/common/model-impl/rest/src/test/java/org/onap/policy/rest/TestPair.java
index aea5a7387..28e9934d1 100755
--- a/controlloop/common/model-impl/rest/src/test/java/org/onap/policy/rest/TestPair.java
+++ b/controlloop/common/model-impl/rest/src/test/java/org/onap/policy/rest/TestPair.java
@@ -32,11 +32,11 @@ public class TestPair {
RESTManager mgr = new RESTManager();
Pair<Integer, Integer> pii = mgr.new Pair<>(1, 2);
- assertEquals((Integer) 1, (Integer) pii.a);
- assertEquals((Integer) 2, (Integer) pii.b);
+ assertEquals((Integer) 1, (Integer) pii.first);
+ assertEquals((Integer) 2, (Integer) pii.second);
Pair<Integer, String> pis = mgr.new Pair<>(1, "test");
- assertEquals((Integer) 1, (Integer) pis.a);
- assertEquals("test", pis.b);
+ assertEquals((Integer) 1, (Integer) pis.first);
+ assertEquals("test", pis.second);
}
}