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;
private String cmdSchemaName;
- private String cmdVersion;
+ private String productVersion;
private OnapService onapService = new OnapService();
private OnapCommandResult cmdResult = new OnapCommandResult();
- protected OnapAuthClient authClient;
-
protected boolean isInitialzied = false;
protected CommandType type = CommandType.CMD;
}
}
- 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;
}
*/
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.
*
// (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;
}
}
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);
}
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()) {
headers.put(attr.getName(), attr.getValues().get(0));
}
}
-
+
this.http.setCommonHeaders(headers);
}
}
OnapCommand logout = this.findAuthCommand("logout");
-
+
OnapCommandUtils.copyParamsFrom(this.cmd, logout);
-
+
logout.execute();
-
+
this.http.close();
}
* 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;
}
}
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;
}
}
/**
* Onap Service credentials.
*/
+//mrkanag deprecate it
public class OnapCredentials {
/*
*/
public class OnapService {
/*
- * Onap Service name like gso.
+ * Onap Service name like aai.
*/
private String serviceName;
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;
private Map<String, String> resultMap = new HashMap<>();
+ protected OnapAuthClient authClient;
+
public void setInput(HttpInput input) {
this.input = input;
}
@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());
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 {
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);
// }
return client;
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
- | InvocationTargetException | OnapCommandException e) {
+ | InvocationTargetException e) {
throw new OnapCommandClientInitialzationFailed(this.getName(), e);
}
}
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();
@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");
@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();
@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");
@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();
}
}
- 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() + "/");
swagger.initializeResult(obj1);
}
- @Test(expected = NullPointerException.class)
+ @Test
public void initializeResultTest3() throws OnapCommandException {
OnapSwaggerCommandImpl swagger = new OnapSwaggerCommandImpl();
swagger.initializeSchema("onap-test-schema.yaml");