aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/main/java/org/onap/vid/mso/RestMsoImplementation.java
diff options
context:
space:
mode:
Diffstat (limited to 'vid-app-common/src/main/java/org/onap/vid/mso/RestMsoImplementation.java')
-rw-r--r--vid-app-common/src/main/java/org/onap/vid/mso/RestMsoImplementation.java237
1 files changed, 117 insertions, 120 deletions
diff --git a/vid-app-common/src/main/java/org/onap/vid/mso/RestMsoImplementation.java b/vid-app-common/src/main/java/org/onap/vid/mso/RestMsoImplementation.java
index 0494facd5..31836f66a 100644
--- a/vid-app-common/src/main/java/org/onap/vid/mso/RestMsoImplementation.java
+++ b/vid-app-common/src/main/java/org/onap/vid/mso/RestMsoImplementation.java
@@ -3,15 +3,17 @@ package org.onap.vid.mso;
import com.att.eelf.configuration.EELFLogger;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.codec.binary.Base64;
+import org.apache.http.HttpException;
import org.eclipse.jetty.util.security.Password;
+import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
+import org.onap.portalsdk.core.util.SystemProperties;
+import org.onap.vid.aai.ExceptionWithRequestInfo;
import org.onap.vid.aai.util.HttpClientMode;
import org.onap.vid.aai.util.HttpsAuthClient;
import org.onap.vid.client.HttpBasicClient;
import org.onap.vid.exceptions.GenericUncheckedException;
import org.onap.vid.mso.rest.RestInterface;
import org.onap.vid.utils.Logging;
-import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
-import org.onap.portalsdk.core.util.SystemProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
@@ -21,53 +23,44 @@ import javax.ws.rs.client.Invocation;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.Response;
-import java.text.DateFormat;
-import java.text.SimpleDateFormat;
import java.util.Collections;
-import java.util.Date;
-import java.util.UUID;
+import java.util.Optional;
import static org.onap.vid.utils.Logging.*;
/**
* Created by pickjonathan on 26/06/2017.
*/
-public class RestMsoImplementation implements RestInterface {
-
- public static final String START_LOG = " start";
- public static final String APPLICATION_JSON = "application/json";
- public static final String WITH_STATUS = " with status=";
- public static final String URL_LOG = ", url=";
- public static final String NO_RESPONSE_ENTITY_LOG = " No response entity, this is probably ok, e=";
- public static final String WITH_URL_LOG = " with url=";
- public static final String EXCEPTION_LOG = ", Exception: ";
- public static final String REST_API_SUCCESSFULL_LOG = " REST api was successfull!";
- public static final String REST_API_POST_WAS_SUCCESSFUL_LOG = " REST api POST was successful!";
+public abstract class RestMsoImplementation implements RestInterface {
+
/**
* The logger.
*/
- EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RestMsoImplementation.class);
+ protected EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RestMsoImplementation.class);
private final EELFLogger outgoingRequestsLogger = Logging.getRequestsLogger("mso");
- /**
- * The Constant dateFormat.
- */
- static final DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
-
/** The client. */
private Client client = null;
@Autowired
- HttpsAuthClient httpsAuthClient;
-
+ protected HttpsAuthClient httpsAuthClient;
+
+ private static final String START_LOG = " start";
+ private static final String APPLICATION_JSON = "application/json";
+ private static final String WITH_STATUS = " with status=";
+ private static final String URL_LOG = ", url=";
+ private static final String NO_RESPONSE_ENTITY_LOG = " No response entity, this is probably ok, e=";
+ private static final String WITH_URL_LOG = " with url=";
+ private static final String EXCEPTION_LOG = ", Exception: ";
+ private static final String REST_API_SUCCESSFULL_LOG = " REST api was successfull!";
+ private static final String REST_MSG_TEMPLATE = "start {}->{}({}, {}, {})";
/** The common headers. */
/**
* Instantiates a new mso rest interface.
*/
@SuppressWarnings("Duplicates")
- @Override
- public MultivaluedHashMap<String, Object> initMsoClient()
+ protected MultivaluedHashMap<String, Object> initMsoClient()
{
final String methodname = "initRestClient()";
@@ -83,15 +76,11 @@ public class RestMsoImplementation implements RestInterface {
MultivaluedHashMap<String, Object> commonHeaders = new MultivaluedHashMap();
commonHeaders.put("Authorization", Collections.singletonList(("Basic " + authStringEnc)));
- //Pass calling application identifier to SO
- commonHeaders.put("X-FromAppId", Collections.singletonList(SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME)));
- try {
- commonHeaders.put(REQUEST_ID_HEADER_KEY, Collections.singletonList(Logging.extractOrGenerateRequestId()));
- }
- catch (IllegalStateException e){
- //in async jobs we don't have any HttpServletRequest
- commonHeaders.put(REQUEST_ID_HEADER_KEY, Collections.singletonList(UUID.randomUUID().toString()));
- }
+ commonHeaders.put("X-ONAP-PartnerName", Collections.singletonList("VID"));
+
+ String requestIdValue = Logging.extractOrGenerateRequestId();
+ commonHeaders.put(REQUEST_ID_HEADER_KEY, Collections.singletonList(requestIdValue));
+ commonHeaders.put(ONAP_REQUEST_ID_HEADER_KEY, Collections.singletonList(requestIdValue));
boolean useSsl = true;
@@ -108,54 +97,65 @@ public class RestMsoImplementation implements RestInterface {
client = HttpBasicClient.getClient();
}
} catch (Exception e) {
- logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + methodname + " Unable to get the SSL client");
+ logger.info(EELFLoggerDelegate.errorLogger,methodname + " Unable to get the SSL client");
}
}
return commonHeaders;
}
- public <T> void Get (T t, String sourceId, String path, RestObject<T> restObject ) {
+ public <T> RestObjectWithRequestInfo<T> Get(T t, String path, RestObject<T> restObject, boolean warpException) {
String methodName = "Get";
logger.debug(EELFLoggerDelegate.debugLogger, methodName + START_LOG);
- String url="";
- restObject.set(t);
+ String url = null;
+ String rawData = null;
+ Integer status = null;
- url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
+ try {
+ restObject.set(t);
+ url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
- MultivaluedHashMap<String, Object> commonHeaders = initMsoClient();
- Logging.logRequest(outgoingRequestsLogger, HttpMethod.GET, url);
- final Response cres = client.target(url)
- .request()
- .accept(APPLICATION_JSON)
- .headers(commonHeaders)
- .get();
- Logging.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, cres);
- int status = cres.getStatus();
- restObject.setStatusCode (status);
+ MultivaluedHashMap<String, Object> commonHeaders = initMsoClient();
+ Logging.logRequest(outgoingRequestsLogger, HttpMethod.GET, url);
+ final Response cres = client.target(url)
+ .request()
+ .accept(APPLICATION_JSON)
+ .headers(commonHeaders)
+ .get();
+ Logging.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, cres);
- if (status == 200 || status == 202) {
- t = (T) cres.readEntity(t.getClass());
- restObject.set(t);
- logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + methodName + REST_API_SUCCESSFULL_LOG);
+ cres.bufferEntity();
+ status = cres.getStatus();
+ rawData = cres.readEntity(String.class);
- } else {
- throw new GenericUncheckedException(methodName + WITH_STATUS + status + ", url= " + url );
- }
+ restObject.setStatusCode(status);
- logger.debug(EELFLoggerDelegate.debugLogger,methodName + " received status=" + status );
+ if (status == 200 || status == 202) {
+ t = (T) cres.readEntity(t.getClass());
+ restObject.set(t);
+ logger.debug(EELFLoggerDelegate.debugLogger, methodName + REST_API_SUCCESSFULL_LOG);
- return;
+ } else {
+ throw new GenericUncheckedException(new HttpException(methodName + WITH_STATUS + status + " (200 or 202 expected), url= " + url));
+ }
+
+ logger.debug(EELFLoggerDelegate.debugLogger, methodName + " received status=" + status);
+
+ return new RestObjectWithRequestInfo<>(HttpMethod.GET, url, restObject, status, rawData);
+ } catch (RuntimeException e) {
+ throw warpException ? new ExceptionWithRequestInfo(HttpMethod.GET, url, rawData, status, e) : e;
+ }
}
- public <T> RestObject<T> GetForObject(String sourceID, String path, Class<T> clazz) {
+ @Override
+ public <T> RestObject<T> GetForObject(String path, Class<T> clazz) {
final String methodName = getMethodName();
- logger.debug(EELFLoggerDelegate.debugLogger, "start {}->{}({}, {}, {})", getMethodCallerName(), methodName, sourceID, path, clazz);
+ logger.debug(EELFLoggerDelegate.debugLogger, "start {}->{}({}, {})", getMethodCallerName(), methodName, path, clazz);
String url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
- logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " sending request to url= " + url);
+ logger.debug(EELFLoggerDelegate.debugLogger, "<== " + methodName + " sending request to url= " + url);
MultivaluedHashMap<String, Object> commonHeaders = initMsoClient();
Logging.logRequest(outgoingRequestsLogger, HttpMethod.GET, url);
@@ -169,9 +169,9 @@ public class RestMsoImplementation implements RestInterface {
int status = cres.getStatus();
if (status == 200 || status == 202) {
- logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + methodName + REST_API_SUCCESSFULL_LOG);
+ logger.debug(EELFLoggerDelegate.debugLogger, methodName + REST_API_SUCCESSFULL_LOG);
} else {
- logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + WITH_STATUS +status+ URL_LOG +url);
+ logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + WITH_STATUS +status+ URL_LOG +url);
}
logger.debug(EELFLoggerDelegate.debugLogger,methodName + " received status=" + status );
@@ -180,13 +180,13 @@ public class RestMsoImplementation implements RestInterface {
}
@Override
- public <T> void Delete(T t, Object r, String sourceID, String path, RestObject<T> restObject) {
+ public <T> void Delete(T t, Object r, String path, RestObject<T> restObject) {
String methodName = "Delete";
String url="";
- Response cres = null;
+ Response cres;
- logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + START_LOG);
+ logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + START_LOG);
try {
MultivaluedHashMap<String, Object> commonHeaders = initMsoClient();
@@ -206,16 +206,16 @@ public class RestMsoImplementation implements RestInterface {
if (status == 404) { // resource not found
String msg = "Resource does not exist...: " + cres.getStatus();
- logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + msg);
+ logger.debug(EELFLoggerDelegate.debugLogger,"<== " + msg);
} else if (status == 200 || status == 204){
- logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + "Resource " + url + " deleted");
+ logger.debug(EELFLoggerDelegate.debugLogger,"<== " + "Resource " + url + " deleted");
} else if (status == 202) {
- String msg = "Delete in progress: " + status;
- logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + msg);
+ String msg = "Delete in progress: " + status;
+ logger.debug(EELFLoggerDelegate.debugLogger,"<== " + msg);
}
else {
String msg = "Deleting Resource failed: " + status;
- logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + msg);
+ logger.debug(EELFLoggerDelegate.debugLogger,"<== " + msg);
}
try {
@@ -223,37 +223,40 @@ public class RestMsoImplementation implements RestInterface {
restObject.set(t);
}
catch ( Exception e ) {
- logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + NO_RESPONSE_ENTITY_LOG
+ logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + NO_RESPONSE_ENTITY_LOG
+ e.getMessage());
}
}
catch (Exception e)
{
- logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + WITH_URL_LOG +url+ EXCEPTION_LOG + e.toString());
+ logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + WITH_URL_LOG +url+ EXCEPTION_LOG + e.toString());
throw e;
}
}
- public <T> RestObject<T> PostForObject(Object requestDetails, String sourceID, String path, Class<T> clazz) {
- logger.debug(EELFLoggerDelegate.debugLogger, "start {}->{}({}, {}, {}, {})", getMethodCallerName(), getMethodName(), requestDetails, sourceID, path, clazz);
- RestObject<T> restObject = new RestObject<>();
- Post(clazz, requestDetails, path, restObject);
- return restObject;
+ public <T> RestObject<T> PostForObject(Object requestDetails, String path, Class<T> clazz) {
+ logger.debug(EELFLoggerDelegate.debugLogger, REST_MSG_TEMPLATE, getMethodCallerName(), getMethodName(), requestDetails, path, clazz);
+ return restCall(HttpMethod.POST, clazz, requestDetails, path);
+ }
+
+ public <T> RestObject<T> DeleteForObject(Object requestDetails, String path, Class<T> clazz) {
+ logger.debug(EELFLoggerDelegate.debugLogger, REST_MSG_TEMPLATE, getMethodCallerName(), getMethodName(), requestDetails, path, clazz);
+ return restCall(HttpMethod.DELETE, clazz, requestDetails, path);
}
@Override
- public <T> void Post(T t, Object r, String sourceID, String path, RestObject<T> restObject) {
- logger.debug(EELFLoggerDelegate.debugLogger, "start {}->{}({}, {}, {}, {})", getMethodCallerName(), getMethodName(), t.getClass(), r, sourceID, path);
- Post(t.getClass(), r, path, restObject);
+ public void Post(String t, Object r, String path, RestObject<String> restObject) {
+ logger.debug(EELFLoggerDelegate.debugLogger, REST_MSG_TEMPLATE, getMethodCallerName(), getMethodName(), t.getClass(), r, path);
+ restObject.copyFrom(restCall(HttpMethod.POST, String.class, r, path));
}
public Invocation.Builder prepareClient(String path, String methodName) {
MultivaluedHashMap<String, Object> commonHeaders = initMsoClient();
String url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
- logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " sending request to url= " + url);
+ logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + " sending request to url= " + url);
// Change the content length
return client.target(url)
.request()
@@ -261,49 +264,44 @@ public class RestMsoImplementation implements RestInterface {
.headers(commonHeaders);
}
+ public <T> RestObject<T> restCall(HttpMethod httpMethod, Class<T> tClass, Object payload, String path) {
+ return restCall(httpMethod, tClass, payload, path, Optional.empty());
+ }
- public <T> void Post(Class<?> tClass, Object requestDetails, String path, RestObject<T> restObject) {
- String methodName = "Post";
+ /*
+ user id is needed to be pass as X-RequestorID in new MSO flows like Delete instanceGroup
+ */
+ public <T> RestObject<T> restCall(HttpMethod httpMethod, Class<T> tClass, Object payload, String path, Optional<String> userId) {
+ String methodName = httpMethod.name();
String url="";
try {
MultivaluedHashMap<String, Object> commonHeaders = initMsoClient();
+ userId.ifPresent(id->commonHeaders.put("X-RequestorID", Collections.singletonList(id)));
url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
- Logging.logRequest(outgoingRequestsLogger, HttpMethod.POST, url, requestDetails);
+ Logging.logRequest(outgoingRequestsLogger, httpMethod, url, payload);
// Change the content length
- final Response cres = client.target(url)
+ final Invocation.Builder restBuilder = client.target(url)
.request()
.accept(APPLICATION_JSON)
- .headers(commonHeaders)
- .post(Entity.entity(requestDetails, MediaType.APPLICATION_JSON));
- Logging.logResponse(outgoingRequestsLogger, HttpMethod.POST, url, cres);
- final RestObject<T> cresToRestObject = cresToRestObject(cres, tClass);
- restObject.set(cresToRestObject.get());
- restObject.setStatusCode(cresToRestObject.getStatusCode());
- restObject.setRaw(cresToRestObject.getRaw());
-
- int status = cres.getStatus();
- restObject.setStatusCode (status);
+ .headers(commonHeaders);
- if ( status >= 200 && status <= 299 ) {
- logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + methodName + REST_API_POST_WAS_SUCCESSFUL_LOG);
- logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + REST_API_POST_WAS_SUCCESSFUL_LOG);
-
- } else {
- logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + WITH_STATUS +status+ URL_LOG +url);
- }
+ Invocation restInvocation = payload==null ?
+ restBuilder.build(httpMethod.name()) :
+ restBuilder.build(httpMethod.name(), Entity.entity(payload, MediaType.APPLICATION_JSON));
+ final Response cres = restInvocation.invoke();
- } catch (Exception e)
- {
- logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + WITH_URL_LOG +url+ EXCEPTION_LOG + e.toString());
+ Logging.logResponse(outgoingRequestsLogger, httpMethod, url, cres);
+ return cresToRestObject(cres, tClass);
+ }
+ catch (Exception e) {
+ logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + WITH_URL_LOG +url+ EXCEPTION_LOG + e.toString());
throw e;
-
}
- logger.debug(EELFLoggerDelegate.debugLogger, "end {}() => ({}){}", getMethodName(), tClass, restObject);
}
private <T> RestObject<T> cresToRestObject(Response cres, Class<?> tClass) {
@@ -319,10 +317,10 @@ public class RestMsoImplementation implements RestInterface {
}
catch ( Exception e ) {
try {
- logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + getMethodCallerName() + " Error reading response entity as " + tClass + ": , e="
+ logger.debug(EELFLoggerDelegate.debugLogger, "<== " + getMethodCallerName() + " Error reading response entity as " + tClass + ": , e="
+ e.getMessage() + ", Entity=" + rawEntity);
} catch (Exception e2) {
- logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + getMethodCallerName() + NO_RESPONSE_ENTITY_LOG
+ logger.debug(EELFLoggerDelegate.debugLogger, "<== " + getMethodCallerName() + NO_RESPONSE_ENTITY_LOG
+ e.getMessage());
}
}
@@ -335,12 +333,12 @@ public class RestMsoImplementation implements RestInterface {
}
@Override
- public <T> void Put(T t, org.onap.vid.changeManagement.RequestDetailsWrapper r, String sourceID, String path, RestObject<T> restObject) {
+ public <T> void Put(T t, org.onap.vid.changeManagement.RequestDetailsWrapper r, String path, RestObject<T> restObject) {
String methodName = "Put";
String url="";
- logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + START_LOG);
+ logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + START_LOG);
try {
@@ -354,7 +352,6 @@ public class RestMsoImplementation implements RestInterface {
.accept(APPLICATION_JSON)
.headers(commonHeaders)
//.header("content-length", 201)
- //.header("X-FromAppId", sourceID)
.put(Entity.entity(r, MediaType.APPLICATION_JSON));
Logging.logResponse(outgoingRequestsLogger, HttpMethod.PUT, url, cres);
@@ -364,7 +361,7 @@ public class RestMsoImplementation implements RestInterface {
restObject.set(t);
}
catch ( Exception e ) {
- logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + NO_RESPONSE_ENTITY_LOG
+ logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + NO_RESPONSE_ENTITY_LOG
+ e.getMessage());
}
@@ -372,16 +369,16 @@ public class RestMsoImplementation implements RestInterface {
restObject.setStatusCode (status);
if ( status >= 200 && status <= 299 ) {
- logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + methodName + REST_API_POST_WAS_SUCCESSFUL_LOG);
- logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + REST_API_POST_WAS_SUCCESSFUL_LOG);
+ logger.info(EELFLoggerDelegate.errorLogger, "<== " + methodName + REST_API_SUCCESSFULL_LOG);
+ logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + REST_API_SUCCESSFULL_LOG);
} else {
- logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + WITH_STATUS +status+ URL_LOG +url);
+ logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + WITH_STATUS +status+ URL_LOG +url);
}
} catch (Exception e)
{
- logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + WITH_URL_LOG +url+ EXCEPTION_LOG + e.toString());
+ logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + WITH_URL_LOG +url+ EXCEPTION_LOG + e.toString());
throw e;
}