diff options
8 files changed, 107 insertions, 114 deletions
diff --git a/framework/src/main/java/org/onap/cli/fw/OnapCommand.java b/framework/src/main/java/org/onap/cli/fw/OnapCommand.java index b100b155..9be9b88e 100644 --- a/framework/src/main/java/org/onap/cli/fw/OnapCommand.java +++ b/framework/src/main/java/org/onap/cli/fw/OnapCommand.java @@ -21,8 +21,6 @@ import java.util.List; import java.util.Map; import java.util.Optional; -import org.onap.cli.fw.ad.OnapAuthClient; -import org.onap.cli.fw.ad.OnapCredentials; import org.onap.cli.fw.ad.OnapService; import org.onap.cli.fw.cmd.CommandType; import org.onap.cli.fw.conf.Constants; @@ -59,7 +57,7 @@ public abstract class OnapCommand { private String cmdSchemaName; - private String cmdVersion; + private String productVersion; private OnapService onapService = new OnapService(); @@ -67,8 +65,6 @@ public abstract class OnapCommand { private OnapCommandResult cmdResult = new OnapCommandResult(); - protected OnapAuthClient authClient; - protected boolean isInitialzied = false; protected CommandType type = CommandType.CMD; @@ -278,37 +274,7 @@ public abstract class OnapCommand { } } - try { - // For auth type commands, login and logout logic is not required - boolean isAuthRequired = !this.onapService.isNoAuth() - && "false".equals(paramMap.get(Constants.DEFAULT_PARAMETER_OUTPUT_NO_AUTH).getValue()) - && this.getType().equals(CommandType.CMD); - - if (!isCommandInternal()) { - this.authClient = new OnapAuthClient( - this, - this.getResult().isDebug()); - } - - if (isAuthRequired) { - this.authClient.login(); - } - - this.run(); - - if (isAuthRequired) { - this.authClient.logout(); - } - - if (this.cmdResult.isDebug() && authClient != null) { - this.cmdResult.setDebugInfo(this.authClient.getDebugInfo()); - } - } catch (OnapCommandException e) { - if (this.cmdResult.isDebug() && authClient != null) { - this.cmdResult.setDebugInfo(this.authClient.getDebugInfo()); - } - throw e; - } + this.run(); return this.cmdResult; } @@ -319,13 +285,6 @@ public abstract class OnapCommand { */ protected abstract void run() throws OnapCommandException; - /* - * Get my service base path (endpoint). - */ - protected String getBasePath() throws OnapCommandException { - return this.authClient.getServiceUrl(); - } - /** * Returns the service service version it supports. * @@ -348,10 +307,10 @@ public abstract class OnapCommand { // (mrkanag) Add toString for all command, parameter, result, etc objects in JSON format public void setVersion(String version) { - this.cmdVersion = version; + this.productVersion = version; } public String getVersion() { - return this.cmdVersion; + return this.productVersion; } } diff --git a/framework/src/main/java/org/onap/cli/fw/ad/OnapAuthClient.java b/framework/src/main/java/org/onap/cli/fw/ad/OnapAuthClient.java index 0a033e66..40db8841 100644 --- a/framework/src/main/java/org/onap/cli/fw/ad/OnapAuthClient.java +++ b/framework/src/main/java/org/onap/cli/fw/ad/OnapAuthClient.java @@ -17,45 +17,35 @@ package org.onap.cli.fw.ad; import java.util.HashMap; -import java.util.List; import java.util.Map; -import org.apache.http.HttpStatus; -import org.apache.http.auth.UsernamePasswordCredentials; -import org.apache.http.impl.auth.BasicScheme; import org.onap.cli.fw.OnapCommand; import org.onap.cli.fw.OnapCommandRegistrar; +import org.onap.cli.fw.cmd.OnapHttpCommand; import org.onap.cli.fw.conf.Constants; import org.onap.cli.fw.conf.OnapCommandConfg; import org.onap.cli.fw.error.OnapCommandException; -import org.onap.cli.fw.error.OnapCommandExecutionFailed; import org.onap.cli.fw.error.OnapCommandHttpFailure; import org.onap.cli.fw.error.OnapCommandInvalidParameterValue; -import org.onap.cli.fw.error.OnapCommandLoginFailed; -import org.onap.cli.fw.error.OnapCommandLogoutFailed; import org.onap.cli.fw.error.OnapCommandNotFound; -import org.onap.cli.fw.error.OnapCommandServiceNotFound; import org.onap.cli.fw.http.HttpInput; import org.onap.cli.fw.http.HttpResult; import org.onap.cli.fw.http.OnapHttpConnection; -import org.onap.cli.fw.input.OnapCommandParameter; import org.onap.cli.fw.output.OnapCommandResultAttribute; import org.onap.cli.fw.utils.OnapCommandUtils; -import com.jayway.jsonpath.JsonPath; - /** * Onap Auth client helps to do login and logout. * */ public class OnapAuthClient { - private OnapCommand cmd = null; - + private OnapHttpCommand cmd = null; + private OnapHttpConnection http = null; - public OnapAuthClient(OnapCommand cmd, boolean debug) throws OnapCommandHttpFailure, OnapCommandInvalidParameterValue { - this.cmd = cmd; + public OnapAuthClient(OnapHttpCommand cmd, boolean debug) throws OnapCommandHttpFailure, OnapCommandInvalidParameterValue { + this.cmd = cmd; this.http = new OnapHttpConnection(debug); } @@ -72,12 +62,12 @@ public class OnapAuthClient { return; } - OnapCommand login = this.findAuthCommand("login"); - + OnapCommand login = this.findAuthCommand("login"); + OnapCommandUtils.copyParamsFrom(this.cmd, login); login.getParametersMap().get(Constants.DEAFULT_PARAMETER_HOST_URL).setValue(this.getServiceUrl(login)); login.execute(); - + //It is safely assumed that all outputs are considered as common http headers. Map<String, String> headers = new HashMap<>(); for (OnapCommandResultAttribute attr: login.getResult().getRecords()) { @@ -86,7 +76,7 @@ public class OnapAuthClient { headers.put(attr.getName(), attr.getValues().get(0)); } } - + this.http.setCommonHeaders(headers); } @@ -103,11 +93,11 @@ public class OnapAuthClient { } OnapCommand logout = this.findAuthCommand("logout"); - + OnapCommandUtils.copyParamsFrom(this.cmd, logout); - + logout.execute(); - + this.http.close(); } @@ -118,31 +108,31 @@ public class OnapAuthClient { * exception */ public String getServiceUrl() throws OnapCommandException { - return this.getServiceUrl(this.cmd); + return this.getServiceUrl(this.cmd); } private String getServiceUrl(OnapCommand cmd) throws OnapCommandException { - if (cmd.getService().isModeDirect()){ - return cmd.getParametersMap().get(Constants.DEAFULT_PARAMETER_HOST_URL).getValue().toString(); + if (cmd.getService().isModeDirect()){ + return cmd.getParametersMap().get(Constants.DEAFULT_PARAMETER_HOST_URL).getValue().toString(); } else { //Catalog mode OnapCommand catalog = OnapCommandRegistrar.getRegistrar().get("catalog"); - + OnapCommandUtils.copyParamsFrom(cmd, catalog); - + catalog.execute(); - + String hostUrl = catalog.getResult().getRecordsMap().get(Constants.CATALOG_SERVICE_HOST_URL).getValues().get(0); hostUrl = hostUrl.trim(); if (hostUrl.endsWith("/")) { - hostUrl = hostUrl.substring(0, hostUrl.length()-1); + hostUrl = hostUrl.substring(0, hostUrl.length()-1); } - + String basePath = catalog.getResult().getRecordsMap().get(Constants.CATALOG_SERVICE_BASE_PATH).getValues().get(0); basePath = basePath.trim(); if (basePath.startsWith("/")) { - basePath = basePath.substring(1); + basePath = basePath.substring(1); } - + return hostUrl + "/" + basePath; } } @@ -164,28 +154,34 @@ public class OnapAuthClient { public HttpResult run(HttpInput input) throws OnapCommandHttpFailure { return this.http.request(input); } - + + /** + * + * @param authAction login/logout + * @return + * @throws OnapCommandException + */ private OnapCommand findAuthCommand(String authAction) throws OnapCommandException { - OnapCommand auth = null; - try { - //Find the auth command for the given service and version under current enabled product - auth = OnapCommandRegistrar.getRegistrar().get( - this.cmd.getService().getName() + "-" + - this.cmd.getService().getVersion() + "-" + - this.cmd.getService().getAuthType() + "-" + authAction); + OnapCommand auth = null; + try { + //Find the auth command for the given service and version under current enabled product + auth = OnapCommandRegistrar.getRegistrar().get( + this.cmd.getService().getName() + "-" + + this.cmd.getService().getVersion() + "-" + + this.cmd.getService().getAuthType() + "-" + authAction); } catch (OnapCommandNotFound e) { - try { - //Find the auth command for the given service under current enabled product - auth = OnapCommandRegistrar.getRegistrar().get( - this.cmd.getService().getName() + "-" + - this.cmd.getService().getAuthType() + "-" + authAction); + try { + //Find the auth command for the given service under current enabled product + auth = OnapCommandRegistrar.getRegistrar().get( + this.cmd.getService().getName() + "-" + + this.cmd.getService().getAuthType() + "-" + authAction); } catch (OnapCommandNotFound e1) { - //Find the auth command for current enabled product - auth = OnapCommandRegistrar.getRegistrar().get( - this.cmd.getService().getAuthType() + "-" + authAction); + //Find the auth command for current enabled product + auth = OnapCommandRegistrar.getRegistrar().get( + this.cmd.getService().getAuthType() + "-" + authAction); } } - - return auth; + + return auth; } } diff --git a/framework/src/main/java/org/onap/cli/fw/ad/OnapCredentials.java b/framework/src/main/java/org/onap/cli/fw/ad/OnapCredentials.java index 02291b22..02cbef7b 100644 --- a/framework/src/main/java/org/onap/cli/fw/ad/OnapCredentials.java +++ b/framework/src/main/java/org/onap/cli/fw/ad/OnapCredentials.java @@ -19,6 +19,7 @@ package org.onap.cli.fw.ad; /** * Onap Service credentials. */ +//mrkanag deprecate it public class OnapCredentials { /* diff --git a/framework/src/main/java/org/onap/cli/fw/ad/OnapService.java b/framework/src/main/java/org/onap/cli/fw/ad/OnapService.java index 18451472..44b79078 100644 --- a/framework/src/main/java/org/onap/cli/fw/ad/OnapService.java +++ b/framework/src/main/java/org/onap/cli/fw/ad/OnapService.java @@ -24,7 +24,7 @@ import org.onap.cli.fw.conf.OnapCommandConfg; */ public class OnapService { /* - * Onap Service name like gso. + * Onap Service name like aai. */ private String serviceName; diff --git a/framework/src/main/java/org/onap/cli/fw/cmd/OnapHttpCommand.java b/framework/src/main/java/org/onap/cli/fw/cmd/OnapHttpCommand.java index 247c16a0..b541181e 100644 --- a/framework/src/main/java/org/onap/cli/fw/cmd/OnapHttpCommand.java +++ b/framework/src/main/java/org/onap/cli/fw/cmd/OnapHttpCommand.java @@ -23,6 +23,7 @@ import java.util.List; import java.util.Map; import org.onap.cli.fw.OnapCommand; +import org.onap.cli.fw.ad.OnapAuthClient; import org.onap.cli.fw.conf.Constants; import org.onap.cli.fw.conf.OnapCommandConfg; import org.onap.cli.fw.error.OnapCommandException; @@ -48,6 +49,8 @@ public class OnapHttpCommand extends OnapCommand { private Map<String, String> resultMap = new HashMap<>(); + protected OnapAuthClient authClient; + public void setInput(HttpInput input) { this.input = input; } @@ -84,6 +87,41 @@ public class OnapHttpCommand extends OnapCommand { @Override protected void run() throws OnapCommandException { + try { + // For auth type commands, login and logout logic is not required + boolean isAuthRequired = !this.getService().isNoAuth() + && "false".equals(this.getParametersMap().get(Constants.DEFAULT_PARAMETER_OUTPUT_NO_AUTH).getValue()) + && this.getType().equals(CommandType.CMD); + + if (!isCommandInternal()) { + this.authClient = new OnapAuthClient( + this, + this.getResult().isDebug()); + } + + if (isAuthRequired) { + this.authClient.login(); + } + + this.processRequest(); + + 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; + } + } + + protected void processRequest() throws OnapCommandException { + HttpInput httpInput = OnapCommandUtils.populateParameters(this.getParametersMap(), this.getInput()); httpInput.setUri(this.authClient.getServiceUrl() + httpInput.getUri()); diff --git a/framework/src/main/java/org/onap/cli/fw/cmd/OnapSwaggerCommand.java b/framework/src/main/java/org/onap/cli/fw/cmd/OnapSwaggerCommand.java index 18fd1def..57fe8412 100644 --- a/framework/src/main/java/org/onap/cli/fw/cmd/OnapSwaggerCommand.java +++ b/framework/src/main/java/org/onap/cli/fw/cmd/OnapSwaggerCommand.java @@ -16,18 +16,17 @@ package org.onap.cli.fw.cmd; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.List; + import org.onap.cli.fw.OnapCommand; import org.onap.cli.fw.error.OnapCommandClientInitialzationFailed; -import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.error.OnapCommandResultInitialzationFailed; import org.onap.cli.fw.output.OnapCommandResultAttribute; import org.onap.cli.fw.run.OnapCommandExecutor; import org.onap.cli.fw.utils.OnapCommandUtils; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.List; - public abstract class OnapSwaggerCommand extends OnapCommand { @@ -52,7 +51,8 @@ public abstract class OnapSwaggerCommand extends OnapCommand { protected <T> T initializeApiClient(T client) throws OnapCommandClientInitialzationFailed { try { Method basePath = client.getClass().getMethod("setBasePath", String.class); - basePath.invoke(client, this.getBasePath()); + //mrkanag set the basepath + basePath.invoke(client, "/"); // if (this.getAuthToken() != null) { // Method apiKey = client.getClass().getMethod("setApiKey", String.class); @@ -60,7 +60,7 @@ public abstract class OnapSwaggerCommand extends OnapCommand { // } return client; } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException | OnapCommandException e) { + | InvocationTargetException e) { throw new OnapCommandClientInitialzationFailed(this.getName(), e); } } diff --git a/framework/src/test/java/org/onap/cli/fw/ad/OnapAuthClientCommandBasedTest.java b/framework/src/test/java/org/onap/cli/fw/ad/OnapAuthClientCommandBasedTest.java index 77ef680d..e6054127 100644 --- a/framework/src/test/java/org/onap/cli/fw/ad/OnapAuthClientCommandBasedTest.java +++ b/framework/src/test/java/org/onap/cli/fw/ad/OnapAuthClientCommandBasedTest.java @@ -21,18 +21,17 @@ import static org.junit.Assert.fail; import org.junit.Test; import org.onap.cli.fw.OnapCommand; import org.onap.cli.fw.OnapCommandRegistrar; +import org.onap.cli.fw.cmd.OnapHttpCommand; import org.onap.cli.fw.conf.Constants; import org.onap.cli.fw.conf.OnapCommandConfg; import org.onap.cli.fw.error.OnapCommandException; public class OnapAuthClientCommandBasedTest { - OnapCommand cmd; - @Test public void internalCommandTest() { try { - cmd = OnapCommandRegistrar.getRegistrar().get("sample-test"); + OnapCommand cmd = OnapCommandRegistrar.getRegistrar().get("sample-test"); cmd.getService().setName(OnapCommandConfg.getInternalCmd()); cmd.execute(); @@ -45,7 +44,7 @@ public class OnapAuthClientCommandBasedTest { @Test public void yesCatalogYesAuthTest() throws OnapCommandException { try { - cmd = getCommand("sample-test-schema-yes-auth-yes-catalog.yaml"); + OnapHttpCommand cmd = getCommand("sample-test-schema-yes-auth-yes-catalog.yaml"); cmd.getParametersMap().get(Constants.DEAFULT_PARAMETER_HOST_URL).setValue("http://localhost:8080"); cmd.getParametersMap().get(Constants.DEAFULT_PARAMETER_USERNAME).setValue("test"); cmd.getParametersMap().get(Constants.DEAFULT_PARAMETER_PASS_WORD).setValue("password"); @@ -60,7 +59,7 @@ public class OnapAuthClientCommandBasedTest { @Test public void yesCatalogNoAuthTest() throws OnapCommandException { try { - cmd = getCommand("sample-test-schema-no-auth-yes-catalog.yaml"); + OnapHttpCommand cmd = getCommand("sample-test-schema-no-auth-yes-catalog.yaml"); cmd.getParametersMap().get(Constants.DEAFULT_PARAMETER_HOST_URL).setValue("http://localhost:8080"); cmd.execute(); @@ -73,7 +72,7 @@ public class OnapAuthClientCommandBasedTest { @Test public void noCatalogYesAuthTest() throws OnapCommandException { try { - cmd = getCommand("sample-test-schema-yes-auth-no-catalog.yaml"); + OnapHttpCommand cmd = getCommand("sample-test-schema-yes-auth-no-catalog.yaml"); cmd.getParametersMap().get(Constants.DEAFULT_PARAMETER_HOST_URL).setValue("http://localhost:8080"); cmd.getParametersMap().get(Constants.DEAFULT_PARAMETER_USERNAME).setValue("test"); cmd.getParametersMap().get(Constants.DEAFULT_PARAMETER_PASS_WORD).setValue("password"); @@ -88,7 +87,7 @@ public class OnapAuthClientCommandBasedTest { @Test public void noCatalogNoAuthTest() throws OnapCommandException { try { - cmd = getCommand("sample-test-schema-no-auth-no-catalog.yaml"); + OnapHttpCommand cmd = getCommand("sample-test-schema-no-auth-no-catalog.yaml"); cmd.getParametersMap().get(Constants.DEAFULT_PARAMETER_HOST_URL).setValue("http://localhost:8080"); cmd.execute(); @@ -98,10 +97,10 @@ public class OnapAuthClientCommandBasedTest { } } - private OnapCommand getCommand(String yaml) throws OnapCommandException { - OnapCommand cmd = new OnapCommand() { + private OnapHttpCommand getCommand(String yaml) throws OnapCommandException { + OnapHttpCommand cmd = new OnapHttpCommand() { @Override - protected void run() throws OnapCommandException { + protected void processRequest() throws OnapCommandException { if (!this.getService().isModeDirect()) { String url = this.authClient.getServiceUrl(); assert url.equals(this.getParametersMap().get(Constants.DEAFULT_PARAMETER_HOST_URL).getValue() + "/"); diff --git a/framework/src/test/java/org/onap/cli/fw/cmd/OnapSwaggerCommandTest.java b/framework/src/test/java/org/onap/cli/fw/cmd/OnapSwaggerCommandTest.java index 628b9a42..47e3e050 100644 --- a/framework/src/test/java/org/onap/cli/fw/cmd/OnapSwaggerCommandTest.java +++ b/framework/src/test/java/org/onap/cli/fw/cmd/OnapSwaggerCommandTest.java @@ -55,7 +55,7 @@ public class OnapSwaggerCommandTest { swagger.initializeResult(obj1); } - @Test(expected = NullPointerException.class) + @Test public void initializeResultTest3() throws OnapCommandException { OnapSwaggerCommandImpl swagger = new OnapSwaggerCommandImpl(); swagger.initializeSchema("onap-test-schema.yaml"); |