From a7a64477c28b433ba05afbe65155450d6ae3cba8 Mon Sep 17 00:00:00 2001 From: Bharath Thiruveedula Date: Mon, 7 Aug 2017 08:28:13 +0530 Subject: Add VSP lifecycle commands Change-Id: Ibdc78b3ab1921c160f8ee193fcd47be0c75f94a9 Issue-ID: CLI-16 Signed-off-by: Bharath T --- .../src/main/java/org/onap/cli/fw/OnapCommand.java | 3 ++- .../java/org/onap/cli/fw/ad/OnapAuthClient.java | 21 ++++++++++++++---- .../main/java/org/onap/cli/fw/conf/Constants.java | 1 + .../org/onap/cli/fw/conf/OnapCommandConfg.java | 25 ++++++++++++++++------ .../main/java/org/onap/cli/fw/http/HttpInput.java | 11 ++++++++++ .../org/onap/cli/fw/http/OnapHttpConnection.java | 3 ++- .../org/onap/cli/fw/utils/OnapCommandUtils.java | 5 +++++ framework/src/main/resources/onap.properties | 9 ++++++-- .../org/onap/cli/fw/ad/OnapAuthClientTest.java | 22 ++++++++++++++----- framework/src/test/resources/onap.properties | 7 +++++- 10 files changed, 87 insertions(+), 20 deletions(-) (limited to 'framework/src') 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 78ab5517..08cbc3bc 100644 --- a/framework/src/main/java/org/onap/cli/fw/OnapCommand.java +++ b/framework/src/main/java/org/onap/cli/fw/OnapCommand.java @@ -264,7 +264,8 @@ public abstract class OnapCommand { this.authClient = new OnapAuthClient( creds, this.getResult().isDebug(), - this.getService().getAuthType()); + this.getService(), + this.getParameters()); } if (isAuthRequired) { 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 93d5ad6b..e9d495c6 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 @@ -16,6 +16,8 @@ package org.onap.cli.fw.ad; +import java.util.HashMap; +import java.util.List; import java.util.Map; import org.apache.http.HttpStatus; @@ -26,6 +28,7 @@ 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.OnapCommandServiceNotFound; @@ -34,6 +37,7 @@ import org.onap.cli.fw.http.HttpResult; import org.onap.cli.fw.http.OnapHttpConnection; import com.jayway.jsonpath.JsonPath; +import org.onap.cli.fw.input.OnapCommandParameter; /** * Onap Auth client helps to do login and logout. @@ -48,13 +52,19 @@ public class OnapAuthClient { private OnapCredentials creds = null; + private OnapService service = new OnapService(); + private String authType = OnapCommandConfg.getAuthType(); - public OnapAuthClient(OnapCredentials creds, boolean debug, String... authType) throws OnapCommandHttpFailure { + private Map paramMap = new HashMap<>(); + + public OnapAuthClient(OnapCredentials creds, boolean debug, OnapService service, List params) throws OnapCommandHttpFailure, OnapCommandInvalidParameterValue { this.creds = creds; - if (authType.length > 0) { - this.authType = authType[0]; + this.service = service; + for (OnapCommandParameter param : params) { + paramMap.put(param.getName(), param.getValue().toString()); } + this.authType = service.getAuthType(); this.http = new OnapHttpConnection(creds.getHostUrl().startsWith("https"), debug); } @@ -82,7 +92,10 @@ public class OnapAuthClient { String authToken = BasicScheme.authenticate(new UsernamePasswordCredentials( creds.getUsername(), creds.getPassword()), "UTF-8", false).getValue(); - Map mapHeaders = OnapCommandConfg.getBasicCommonHeaders(); + Map mapHeaders = OnapCommandConfg.getBasicCommonHeaders(this.paramMap); + if(this.service.getName() != null){ + mapHeaders.putAll(OnapCommandConfg.getServiceHeaders(this.service.getName(), this.paramMap)); + } mapHeaders.put(OnapCommandConfg.getXAuthTokenName(), authToken); this.http.setCommonHeaders(mapHeaders); return; diff --git a/framework/src/main/java/org/onap/cli/fw/conf/Constants.java b/framework/src/main/java/org/onap/cli/fw/conf/Constants.java index a3724275..e9b158e7 100644 --- a/framework/src/main/java/org/onap/cli/fw/conf/Constants.java +++ b/framework/src/main/java/org/onap/cli/fw/conf/Constants.java @@ -75,6 +75,7 @@ public class Constants { public static final String CLIENT = "client"; public static final String ENTITY = "entity"; public static final String METHOD = "method"; + public static final String MULTIPART_ENTITY_NAME = "multipart_entity_name"; public static final String EXCEPTION = "exception"; public static final String SCOPE = "scope"; diff --git a/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConfg.java b/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConfg.java index 2fdb0a82..479431f2 100644 --- a/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConfg.java +++ b/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConfg.java @@ -16,6 +16,8 @@ package org.onap.cli.fw.conf; +import org.onap.cli.fw.input.OnapCommandParameter; + import java.io.IOException; import java.util.Arrays; import java.util.HashMap; @@ -99,21 +101,32 @@ public final class OnapCommandConfg { return prps.getProperty(Constants.SERVICE_AUTH, Constants.AUTH_BASIC); } - public static Map getBasicCommonHeaders() { + private static Map getHeaderValues(String headerKey, Map paramMap) { Map mapHeaders = new HashMap (); - - Arrays.stream(prps.getProperty(Constants.SERVICE_AUTH_BASIC_HTTP_HEADERS) // NOSONAR + Arrays.stream(prps.getProperty(headerKey) // NOSONAR .split(",")).map(String::trim).forEach(header -> { - String headerName = prps.getProperty(Constants.SERVICE_AUTH_BASIC_HTTP_HEADERS + "." + header); - String headerValue = prps.getProperty(Constants.SERVICE_AUTH_BASIC_HTTP_HEADERS + "." + header + ".value", null); + String headerName = prps.getProperty(headerKey+ "." + header); + String headerValue = prps.getProperty(headerKey + "." + header + ".value", null); if (headerValue != null) { headerValue = headerValue.replaceAll("uuid", UUID.randomUUID().toString()); + if (headerValue.contains("${")) { + String param = headerValue.substring(headerValue.indexOf("${")+2 ,headerValue.indexOf("}")); + String pattern = "${"+param+"}"; + headerValue = headerValue.replace(pattern, paramMap.getOrDefault(param, param)); + } } mapHeaders.put(headerName, headerValue); }); - return mapHeaders; } + public static Map getBasicCommonHeaders(Map paramMap) { + return getHeaderValues(Constants.SERVICE_AUTH_BASIC_HTTP_HEADERS, paramMap); + } + + public static Map getServiceHeaders(String serviceName, Map paramMap) { + String serviceHeader = Constants.SERVICE_AUTH_BASIC_HTTP_HEADERS+ "." + serviceName; + return getHeaderValues(serviceHeader, paramMap); + } public static Set getExcludeParamsForInternalCmd() { return Arrays.stream(prps.getProperty(Constants.EXCLUDE_PARAMS_INTERNAL_CMD) // NOSONAR diff --git a/framework/src/main/java/org/onap/cli/fw/http/HttpInput.java b/framework/src/main/java/org/onap/cli/fw/http/HttpInput.java index 3ab6cf4b..69439bd4 100644 --- a/framework/src/main/java/org/onap/cli/fw/http/HttpInput.java +++ b/framework/src/main/java/org/onap/cli/fw/http/HttpInput.java @@ -30,6 +30,8 @@ public class HttpInput { private String reqMethod = ""; + private String multipartEntityName = ""; + private Map reqHeaders = new HashMap<>(); private Map reqQueries = new HashMap<>(); @@ -83,6 +85,15 @@ public class HttpInput { return this; } + public String getMultipartEntityName() { + return this.multipartEntityName; + } + + public HttpInput setMultipartEntityName(String multipartEntityName) { + this.multipartEntityName = multipartEntityName; + return this; + } + public Map getReqQueries() { return reqQueries; } diff --git a/framework/src/main/java/org/onap/cli/fw/http/OnapHttpConnection.java b/framework/src/main/java/org/onap/cli/fw/http/OnapHttpConnection.java index b0ab11c4..a9df38c5 100644 --- a/framework/src/main/java/org/onap/cli/fw/http/OnapHttpConnection.java +++ b/framework/src/main/java/org/onap/cli/fw/http/OnapHttpConnection.java @@ -338,7 +338,8 @@ public class OnapHttpConnection { private HttpEntity getMultipartEntity(HttpInput input) { FileBody fileBody = new FileBody(new File(input.getBody().trim())); MultipartEntity multipartEntity = new MultipartEntity(); - multipartEntity.addPart("file", fileBody); + String fileName = input.getMultipartEntityName() != "" ? input.getMultipartEntityName() : "upload"; + multipartEntity.addPart(fileName, fileBody); return multipartEntity; } } diff --git a/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java index 9b56dece..625c644b 100644 --- a/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java +++ b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java @@ -126,6 +126,7 @@ import static org.onap.cli.fw.conf.Constants.NAME; import static org.onap.cli.fw.conf.Constants.ONAP_CMD_SCHEMA_VERSION; import static org.onap.cli.fw.conf.Constants.PARAMETERS; import static org.onap.cli.fw.conf.Constants.QUERIES; +import static org.onap.cli.fw.conf.Constants.MULTIPART_ENTITY_NAME; import static org.onap.cli.fw.conf.Constants.REQUEST; import static org.onap.cli.fw.conf.Constants.RESULTS; import static org.onap.cli.fw.conf.Constants.RESULT_MAP; @@ -915,6 +916,10 @@ public class OnapCommandUtils { cmd.getInput().setReqQueries(query); break; + case MULTIPART_ENTITY_NAME: + Object multipartEntityName = map.get(key2); + cmd.getInput().setMultipartEntityName(multipartEntityName.toString()); + break; } }catch (Exception ex) { throwOrCollect(new OnapCommandInvalidSchema(schemaName, ex), errorList, validate); diff --git a/framework/src/main/resources/onap.properties b/framework/src/main/resources/onap.properties index 8a2756e4..9d59f1e2 100644 --- a/framework/src/main/resources/onap.properties +++ b/framework/src/main/resources/onap.properties @@ -20,6 +20,11 @@ cli.http.basic.common_headers.x-transaction-id.value=req-uuid cli.http.basic.common_headers.x-app-id=X-FromAppId cli.http.basic.common_headers.x-app-id.value=onap-cli +# Service specific headers +cli.http.basic.common_headers.sdc=user-id +cli.http.basic.common_headers.sdc.user-id=USER_ID +cli.http.basic.common_headers.sdc.user-id.value=${onap-username} + #TODO mrkanag add support for aaf like defined above for basic #cli.service.auth=aaf @@ -39,11 +44,11 @@ cli.schema.result_params_mandatory_list=name, description, type cli.schema.http_sections=request,success_codes,result_map,sample_response cli.schema.http_mandatory_sections=equest, success_codes -cli.schema.http_request_params=uri,method,body,headers,queries +cli.schema.http_request_params=uri,method,body,headers,queries,multipart_entity_name cli.schema.http_request_mandatory_params=uri,method cli.schema.http_methods=post,get,delete,put,head cli.schema.boolean_values=true,false cli.schema.auth_values=none,basic -cli.schema.mode_values=direct,catalog \ No newline at end of file +cli.schema.mode_values=direct,catalog diff --git a/framework/src/test/java/org/onap/cli/fw/ad/OnapAuthClientTest.java b/framework/src/test/java/org/onap/cli/fw/ad/OnapAuthClientTest.java index f98846c6..c57be7d2 100644 --- a/framework/src/test/java/org/onap/cli/fw/ad/OnapAuthClientTest.java +++ b/framework/src/test/java/org/onap/cli/fw/ad/OnapAuthClientTest.java @@ -16,6 +16,9 @@ package org.onap.cli.fw.ad; +import java.util.ArrayList; +import java.util.List; + import static org.junit.Assert.assertEquals; import org.junit.AfterClass; @@ -30,6 +33,7 @@ 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 mockit.Invocation; import mockit.Mock; @@ -40,15 +44,19 @@ public class OnapAuthClientTest { OnapAuthClient client; @Before - public void setUp() throws OnapCommandHttpFailure { + public void setUp() throws OnapCommandHttpFailure, OnapCommandException { OnapCredentials creds = new OnapCredentials("test", "test123", "http://192.168.99.10:80"); - client = new OnapAuthClient(creds, true); + OnapService service = new OnapService(); + List params = new ArrayList<>(); + client = new OnapAuthClient(creds, true, service, params); } @Test public void loginFailedAuthIgnoredTest() throws OnapCommandException { OnapCredentials creds = new OnapCredentials("test", "test123", "http://192.168.99.10:80"); - OnapAuthClient client = new OnapAuthClient(creds, true); + OnapService service = new OnapService(); + List params = new ArrayList<>(); + OnapAuthClient client = new OnapAuthClient(creds, true, service, params); if (OnapCommandConfg.isAuthIgnored()) { client.getDebugInfo(); client.login(); @@ -58,7 +66,9 @@ public class OnapAuthClientTest { @Test public void logoutFailedAuthIgnoredTest() throws OnapCommandException { OnapCredentials creds = new OnapCredentials("test", "test123", "http://192.168.99.10:80"); - OnapAuthClient client = new OnapAuthClient(creds, true); + OnapService service = new OnapService(); + List params = new ArrayList<>(); + OnapAuthClient client = new OnapAuthClient(creds, true, service, params); if (OnapCommandConfg.isAuthIgnored()) { client.logout(); } @@ -67,7 +77,9 @@ public class OnapAuthClientTest { @Test public void getMsbUrlTest() throws OnapCommandException { OnapCredentials creds = new OnapCredentials("test", "test123", "http://192.168.99.10:80"); - OnapAuthClient client = new OnapAuthClient(creds, true); + OnapService service = new OnapService(); + List params = new ArrayList<>(); + OnapAuthClient client = new OnapAuthClient(creds, true, service, params); OnapService srv = new OnapService(); srv.setName("msb"); String msb = client.getServiceBasePath(srv); diff --git a/framework/src/test/resources/onap.properties b/framework/src/test/resources/onap.properties index 8a2756e4..cdbbfa84 100644 --- a/framework/src/test/resources/onap.properties +++ b/framework/src/test/resources/onap.properties @@ -20,6 +20,11 @@ cli.http.basic.common_headers.x-transaction-id.value=req-uuid cli.http.basic.common_headers.x-app-id=X-FromAppId cli.http.basic.common_headers.x-app-id.value=onap-cli +# Service specific headers +cli.http.basic.common_headers.sdc=user-id +cli.http.basic.common_headers.sdc.user-id=USER_ID +cli.http.basic.common_headers.sdc.user-id.value=${onap-username} + #TODO mrkanag add support for aaf like defined above for basic #cli.service.auth=aaf @@ -46,4 +51,4 @@ cli.schema.http_methods=post,get,delete,put,head cli.schema.boolean_values=true,false cli.schema.auth_values=none,basic -cli.schema.mode_values=direct,catalog \ No newline at end of file +cli.schema.mode_values=direct,catalog -- cgit 1.2.3-korg