From b100c49fbd130e4fe58ce82f1e5f765f13917703 Mon Sep 17 00:00:00 2001 From: Kanagaraj M Date: Fri, 2 Aug 2019 14:10:30 +0530 Subject: Update framework Issue-ID: CLI-169 Change-Id: Ice38da575d2e349bb875149afd894e348bc20253 Signed-off-by: Kanagaraj Manickam k00365106 --- .../fw/http/auth/OnapCommandHttpAuthClient.java | 9 +--- .../org/onap/cli/fw/http/cmd/OnapHttpCommand.java | 57 +++++++++++++--------- .../cli/fw/http/conf/OnapCommandHttpConstants.java | 1 + .../cli/fw/http/connect/OnapHttpConnection.java | 41 +++++----------- .../cli/fw/http/utils/OnapCommandHttpUtils.java | 35 +++++++++---- .../onap/cli/fw/http/OnapHttpConnectionTest.java | 17 +++---- 6 files changed, 80 insertions(+), 80 deletions(-) (limited to 'profiles/http') diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/auth/OnapCommandHttpAuthClient.java b/profiles/http/src/main/java/org/onap/cli/fw/http/auth/OnapCommandHttpAuthClient.java index 9b7b4039..e5f17bec 100644 --- a/profiles/http/src/main/java/org/onap/cli/fw/http/auth/OnapCommandHttpAuthClient.java +++ b/profiles/http/src/main/java/org/onap/cli/fw/http/auth/OnapCommandHttpAuthClient.java @@ -45,9 +45,9 @@ public class OnapCommandHttpAuthClient { private Map loginCache = new HashMap<>(); - public OnapCommandHttpAuthClient(OnapHttpCommand cmd, boolean debug) throws OnapCommandHttpFailure { + public OnapCommandHttpAuthClient(OnapHttpCommand cmd) { this.cmd = cmd; - this.http = new OnapHttpConnection(debug); + this.http = new OnapHttpConnection(); } /** @@ -142,11 +142,6 @@ public class OnapCommandHttpAuthClient { } } - - public String getDebugInfo() { - return this.http.getDebugInfo(); - } - /** * Http call to external service. * diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/cmd/OnapHttpCommand.java b/profiles/http/src/main/java/org/onap/cli/fw/http/cmd/OnapHttpCommand.java index 14728ad0..53a2d042 100644 --- a/profiles/http/src/main/java/org/onap/cli/fw/http/cmd/OnapHttpCommand.java +++ b/profiles/http/src/main/java/org/onap/cli/fw/http/cmd/OnapHttpCommand.java @@ -16,6 +16,14 @@ package org.onap.cli.fw.http.cmd; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Optional; + import org.onap.cli.fw.cmd.OnapCommand; import org.onap.cli.fw.cmd.OnapCommandType; import org.onap.cli.fw.conf.OnapCommandConfig; @@ -34,19 +42,12 @@ import org.onap.cli.fw.http.utils.OnapCommandHttpUtils; import org.onap.cli.fw.input.OnapCommandParameter; import org.onap.cli.fw.output.OnapCommandResultAttribute; import org.onap.cli.fw.schema.OnapCommandSchema; +import org.onap.cli.fw.store.OnapCommandExecutionStore; import org.onap.cli.fw.utils.OnapCommandUtils; import org.onap.cli.http.mock.MockJsonGenerator; import org.onap.cli.http.mock.MockRequest; import org.onap.cli.http.mock.MockResponse; -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Optional; - /** * Oclip http Command. * @@ -56,6 +57,8 @@ public class OnapHttpCommand extends OnapCommand { private HttpInput input = new HttpInput(); + private HttpResult output = new HttpResult(); + private List successStatusCodes = new ArrayList<>(); private Map resultMap = new HashMap<>(); @@ -201,14 +204,11 @@ public class OnapHttpCommand extends OnapCommand { @Override protected void run() throws OnapCommandException { + this.authClient = new OnapCommandHttpAuthClient(this); + try { // For auth/catalog type commands, login and logout logic is not required boolean isAuthRequired = this.isAuthRequired(); - - this.authClient = new OnapCommandHttpAuthClient( - this, - this.getResult().isDebug()); - if (isAuthRequired) { this.authClient.login(); } @@ -218,24 +218,28 @@ public class OnapHttpCommand extends OnapCommand { if (isAuthRequired) { this.authClient.logout(); } - - if (this.getResult().isDebug() && authClient != null) { - this.getResult().setDebugInfo(this.authClient.getDebugInfo()); - } } catch (OnapCommandException e) { - if (this.getResult().isDebug() && authClient != null) { - this.getResult().setDebugInfo(this.authClient.getDebugInfo()); - } throw e; + } finally { + this.getResult().setDebugInfo(this.input.toString() + "\n" + this.output.toString()); } } protected void processRequest() throws OnapCommandException { HttpInput httpInput = OnapCommandHttpUtils.populateParameters(this.getParametersMap(), this.getInput()); - httpInput.setUri(this.authClient.getServiceUrl() + httpInput.getUri()); + if (!httpInput.getUri().startsWith("http")) { + httpInput.setUri(this.authClient.getServiceUrl() + httpInput.getUri()); + } - HttpResult output = this.authClient.run(httpInput); + this.setInput(httpInput); + + if (this.getExecutionContext() != null) { + OnapCommandExecutionStore.getStore().storeExectutionDebug(this.getExecutionContext(), this.getInput().toString()); + } else { + this.getResult().setDebugInfo(this.getInput().toString()); + } + this.output = this.authClient.run(this.getInput()); this.getResult().setOutput(output); if (!this.getSuccessStatusCodes().contains(output.getStatus())) { @@ -250,12 +254,17 @@ public class OnapHttpCommand extends OnapCommand { } Map> results = OnapCommandHttpUtils.populateOutputs(this.getResultMap(), output); - results = OnapCommandUtils.populateOutputsFromInputParameters(results, this.getParametersMap()); + //results = OnapCommandUtils.populateOutputsFromInputParameters(results, this.getParametersMap()); for (OnapCommandResultAttribute attr : this.getResult().getRecords()) { attr.setValues(results.get(attr.getName())); } - generateJsonMock(httpInput, output, this.getSchemaName()); + + try{ + generateJsonMock(this.getInput(), output, this.getSchemaName()); + } catch (OnapCommandFailedMocoGenerate e) { + //NO SONAR ignore it + } } private void generateJsonMock(HttpInput httpInput, HttpResult httpResult, String schemaName) diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/conf/OnapCommandHttpConstants.java b/profiles/http/src/main/java/org/onap/cli/fw/http/conf/OnapCommandHttpConstants.java index 2d14bb88..6a87ed13 100644 --- a/profiles/http/src/main/java/org/onap/cli/fw/http/conf/OnapCommandHttpConstants.java +++ b/profiles/http/src/main/java/org/onap/cli/fw/http/conf/OnapCommandHttpConstants.java @@ -94,6 +94,7 @@ public class OnapCommandHttpConstants { //context param public static final String CONTEXT = "context"; public static final String CONTEXT_REMOVE_EMPTY_JSON_NODES = "remove_empty_node"; + public static final String __BODY__ = "__body__"; // moco server const public static final String VERIFY_MOCO_HOST = "cli.verify.host"; diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/connect/OnapHttpConnection.java b/profiles/http/src/main/java/org/onap/cli/fw/http/connect/OnapHttpConnection.java index a2da0706..3f426bf3 100644 --- a/profiles/http/src/main/java/org/onap/cli/fw/http/connect/OnapHttpConnection.java +++ b/profiles/http/src/main/java/org/onap/cli/fw/http/connect/OnapHttpConnection.java @@ -27,15 +27,18 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; + import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; + import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.annotation.NotThreadSafe; import org.apache.http.client.CookieStore; import org.apache.http.client.HttpClient; +import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; import org.apache.http.client.methods.HttpGet; @@ -79,10 +82,6 @@ public class OnapHttpConnection { Map mapCommonHeaders = new HashMap<> (); - protected boolean debug = false; - - private String debugDetails = ""; - public static class TrustAllX509TrustManager implements X509TrustManager { @Override @@ -101,18 +100,6 @@ public class OnapHttpConnection { } } - /** - * OnapHttpConnection Constructor. - * - * @param debug - * boolean - * @throws OnapCommandHttpFailure - * exception - */ - public OnapHttpConnection(boolean debug) { - this.debug = debug; - } - private void initHttpClient(boolean isSecured) throws OnapCommandHttpFailure { if (this.httpClient == null) { try { @@ -137,10 +124,6 @@ public class OnapHttpConnection { } } - public String getDebugInfo() { - return this.debugDetails; - } - private Map getHttpHeaders(HttpResponse resp) { Map result = new HashMap<>(); @@ -244,9 +227,12 @@ public class OnapHttpConnection { } } - private void addCommonCookies(CookieStore cookieStore) { + private void addCommonCookies(HttpInput input, CookieStore cookieStore) { for (Entry header : this.mapCommonHeaders.entrySet()) { - Cookie cookie = new BasicClientCookie(header.getKey(), header.getValue()); + //take care of overriden headers in OCS YAML + String value = input.getReqHeaders().getOrDefault(header.getKey(), + header.getValue()); + Cookie cookie = new BasicClientCookie(header.getKey(), value); cookieStore.addCookie(cookie); } } @@ -267,7 +253,7 @@ public class OnapHttpConnection { } private void updateInputFromCookies(HttpInput input, CookieStore cookieStore) { - addCommonCookies(cookieStore); + addCommonCookies(input, cookieStore); for (String cookieName : input.getReqCookies().keySet()) { BasicClientCookie cookie = new BasicClientCookie(cookieName, input.getReqCookies().get(cookieName)); cookie.setDomain(this.getDomain(input.getUri())); @@ -320,6 +306,8 @@ public class OnapHttpConnection { } requestBase.setURI(URI.create(input.getUri())); + requestBase.setConfig(RequestConfig.custom() + .setSocketTimeout(30000).setConnectTimeout(50000).build()); for (Entry h : input.getReqHeaders().entrySet()) { requestBase.addHeader(h.getKey(), h.getValue()); @@ -328,7 +316,6 @@ public class OnapHttpConnection { HttpResult result = new HttpResult(); try { - this.debugDetails = ""; CookieStore cookieStore = new BasicCookieStore(); updateInputFromCookies(input, cookieStore); HttpContext localContext = new BasicHttpContext(); @@ -344,12 +331,6 @@ public class OnapHttpConnection { this.updateResultFromCookies(result, cookieStore.getCookies()); } catch (Exception e) { // NOSONAR throw new OnapCommandHttpFailure(e); - } finally { - String info = input + " " + result; - log.info(info); - if (this.debug) { - this.debugDetails = info; - } } return result; diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/utils/OnapCommandHttpUtils.java b/profiles/http/src/main/java/org/onap/cli/fw/http/utils/OnapCommandHttpUtils.java index f4a6cee5..5cab0a68 100644 --- a/profiles/http/src/main/java/org/onap/cli/fw/http/utils/OnapCommandHttpUtils.java +++ b/profiles/http/src/main/java/org/onap/cli/fw/http/utils/OnapCommandHttpUtils.java @@ -76,24 +76,22 @@ public class OnapCommandHttpUtils { } } + inp.setUri(OnapCommandUtils.replaceLineForSpecialValues(input.getUri())); inp.setUri(OnapCommandUtils.replaceLineFromInputParameters(input.getUri(), params)); inp.setMethod(input.getMethod().toLowerCase()); - for (String h : input.getReqHeaders().keySet()) { - String value = input.getReqHeaders().get(h); - inp.getReqHeaders().put(h, OnapCommandUtils.replaceLineFromInputParameters(value, params)); - } + boolean isRemoveEmptyNodes = Boolean.parseBoolean(input.getContext().getOrDefault(OnapCommandHttpConstants.CONTEXT_REMOVE_EMPTY_JSON_NODES, "false")); - for (String h : input.getReqQueries().keySet()) { - String value = input.getReqQueries().get(h); - inp.getReqQueries().put(h, OnapCommandUtils.replaceLineFromInputParameters(value, params)); + //Process for md5 + Map values = new HashMap<>(); + for (Map.Entry param: params.entrySet()) { + values.put(param.getKey(), param.getValue().getValue().toString()); } - boolean isRemoveEmptyNodes = Boolean.parseBoolean(input.getContext().getOrDefault(OnapCommandHttpConstants.CONTEXT_REMOVE_EMPTY_JSON_NODES, "false")); - if (input.getMultiparts().size() > 0) { for (HttpInput.Part part: input.getMultiparts()) { + part.setContent(OnapCommandUtils.replaceLineForSpecialValues(part.getContent(), values)); part.setContent(OnapCommandUtils.replaceLineFromInputParameters(part.getContent(), params)); if (isRemoveEmptyNodes) { part.setContent(OnapCommandHttpUtils.normalizeJson(part.getContent())); @@ -103,11 +101,28 @@ public class OnapCommandHttpUtils { inp.setMultiparts(input.getMultiparts()); } else { inp.setMultipartEntityName(input.getMultipartEntityName()); - inp.setBody(OnapCommandUtils.replaceLineFromInputParameters(input.getBody(), params)); + inp.setBody(OnapCommandUtils.replaceLineForSpecialValues(input.getBody(), values)); + inp.setBody(OnapCommandUtils.replaceLineFromInputParameters(inp.getBody(), params)); if (isRemoveEmptyNodes) { inp.setBody(OnapCommandHttpUtils.normalizeJson(inp.getBody())); } } + + //consider __body__ spl entry + values.put(OnapCommandHttpConstants.__BODY__, inp.getBody()); + + for (String h : input.getReqHeaders().keySet()) { + String value = input.getReqHeaders().get(h); + value = OnapCommandUtils.replaceLineForSpecialValues(value, values); + inp.getReqHeaders().put(h, OnapCommandUtils.replaceLineFromInputParameters(value, params)); + } + + for (String h : input.getReqQueries().keySet()) { + String value = input.getReqQueries().get(h); + value = OnapCommandUtils.replaceLineForSpecialValues(value, values); + inp.getReqQueries().put(h, OnapCommandUtils.replaceLineFromInputParameters(value, params)); + } + return inp; } diff --git a/profiles/http/src/test/java/org/onap/cli/fw/http/OnapHttpConnectionTest.java b/profiles/http/src/test/java/org/onap/cli/fw/http/OnapHttpConnectionTest.java index fab02ff2..f0115580 100644 --- a/profiles/http/src/test/java/org/onap/cli/fw/http/OnapHttpConnectionTest.java +++ b/profiles/http/src/test/java/org/onap/cli/fw/http/OnapHttpConnectionTest.java @@ -72,8 +72,7 @@ public class OnapHttpConnectionTest { } }; inp.setMethod("get"); - con = new OnapHttpConnection(true); - con.getDebugInfo(); + con = new OnapHttpConnection(); con.get(inp); } @@ -90,7 +89,7 @@ public class OnapHttpConnectionTest { }; inp.setMethod("post"); - con = new OnapHttpConnection(true); + con = new OnapHttpConnection(); con.post(inp); } @@ -108,7 +107,7 @@ public class OnapHttpConnectionTest { inp.setMethod("post"); inp.setBinaryData(true); - con = new OnapHttpConnection(true); + con = new OnapHttpConnection(); con.post(inp); } @@ -123,7 +122,7 @@ public class OnapHttpConnectionTest { } }; inp.setMethod("put"); - con = new OnapHttpConnection(true); + con = new OnapHttpConnection(); con.put(inp); } @@ -138,7 +137,7 @@ public class OnapHttpConnectionTest { } }; inp.setMethod("delete"); - con = new OnapHttpConnection(true); + con = new OnapHttpConnection(); con.delete(inp); } @@ -153,14 +152,14 @@ public class OnapHttpConnectionTest { } }; inp.setMethod("other"); - con = new OnapHttpConnection(true); + con = new OnapHttpConnection(); con.request(inp); } @Test() public void httpUnSecuredCloseExceptionTest() throws OnapCommandHttpFailure { inp.setMethod("other"); - con = new OnapHttpConnection(true); + con = new OnapHttpConnection(); con.close(); } @@ -185,7 +184,7 @@ public class OnapHttpConnectionTest { inp.setReqHeaders(new HashMap()); inp.setReqQueries(new HashMap()); inp.setUri("https://192.168.99.10:80"); - OnapHttpConnection con = new OnapHttpConnection(false); + OnapHttpConnection con = new OnapHttpConnection(); con.get(inp); } catch (OnapCommandHttpFailure e) { assertEquals("0x3001::IO Exception", e.getMessage()); -- cgit 1.2.3-korg