diff options
author | Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com> | 2017-09-30 22:48:55 +0530 |
---|---|---|
committer | Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com> | 2017-11-14 14:39:25 +0530 |
commit | 74f654c7cc41908d0109f60c7554f329f0b29daf (patch) | |
tree | d6346121d296db3da12b2b0b3a36f07f76a3857c /framework/src/main/java | |
parent | d39f12c5af170277b2c3daf8cff86553e3a0d2ec (diff) |
Migrate auth logic into http command
Login/Logout logic is moved into http command
as this framework would be enhanced to support
other command types like ssh
Issue-Id: CLI-66
Change-Id: I33936f8871dfa4c000f8fcabb9cf17e96fc71e0b
Signed-off-by: Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
Diffstat (limited to 'framework/src/main/java')
6 files changed, 97 insertions, 103 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); } } |