Search in sources :

Example 1 with ApiDiscoveryResponse

use of org.apache.cloudstack.api.response.ApiDiscoveryResponse in project cloudstack by apache.

the class ApiDiscoveryServiceImpl method start.

@Override
public boolean start() {
    if (s_apiNameDiscoveryResponseMap == null) {
        long startTime = System.nanoTime();
        s_apiNameDiscoveryResponseMap = new HashMap<String, ApiDiscoveryResponse>();
        Set<Class<?>> cmdClasses = new LinkedHashSet<Class<?>>();
        for (PluggableService service : _services) {
            s_logger.debug(String.format("getting api commands of service: %s", service.getClass().getName()));
            cmdClasses.addAll(service.getCommands());
        }
        cmdClasses.addAll(this.getCommands());
        cacheResponseMap(cmdClasses);
        long endTime = System.nanoTime();
        s_logger.info("Api Discovery Service: Annotation, docstrings, api relation graph processed in " + (endTime - startTime) / 1000000.0 + " ms");
    }
    return true;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PluggableService(com.cloud.utils.component.PluggableService) ApiDiscoveryResponse(org.apache.cloudstack.api.response.ApiDiscoveryResponse)

Example 2 with ApiDiscoveryResponse

use of org.apache.cloudstack.api.response.ApiDiscoveryResponse in project cloudstack by apache.

the class ApiDiscoveryTest method verifyListApis.

@Test
public void verifyListApis() throws Exception {
    ListResponse<ApiDiscoveryResponse> responses = (ListResponse<ApiDiscoveryResponse>) s_discoveryService.listApis(testUser, null);
    assertNotNull("Responses should not be null", responses);
    if (responses != null) {
        assertTrue("No. of response items > 2", responses.getCount().intValue() == 2);
        for (ApiDiscoveryResponse response : responses.getResponses()) {
            assertFalse("API name is empty", response.getName().isEmpty());
            assertFalse("API description is empty", response.getDescription().isEmpty());
        }
    }
}
Also used : ListResponse(org.apache.cloudstack.api.response.ListResponse) ApiDiscoveryResponse(org.apache.cloudstack.api.response.ApiDiscoveryResponse) Test(org.junit.Test)

Example 3 with ApiDiscoveryResponse

use of org.apache.cloudstack.api.response.ApiDiscoveryResponse in project cloudstack by apache.

the class ApiDiscoveryTest method verifyListSingleApi.

@Test
public void verifyListSingleApi() throws Exception {
    ListResponse<ApiDiscoveryResponse> responses = (ListResponse<ApiDiscoveryResponse>) s_discoveryService.listApis(testUser, testApiName);
    assertNotNull("Responses should not be null", responses);
    if (responses != null) {
        ApiDiscoveryResponse response = responses.getResponses().get(0);
        assertTrue("No. of response items should be one", responses.getCount() == 1);
        assertEquals("Error in api name", testApiName, response.getName());
        assertEquals("Error in api description", testApiDescription, response.getDescription());
        assertEquals("Error in api since", testApiSince, response.getSince());
        assertEquals("Error in api isAsync", testApiAsync, response.getAsync());
    }
}
Also used : ListResponse(org.apache.cloudstack.api.response.ListResponse) ApiDiscoveryResponse(org.apache.cloudstack.api.response.ApiDiscoveryResponse) Test(org.junit.Test)

Example 4 with ApiDiscoveryResponse

use of org.apache.cloudstack.api.response.ApiDiscoveryResponse in project cloudstack by apache.

the class ApiDiscoveryServiceImpl method cacheResponseMap.

protected Map<String, List<String>> cacheResponseMap(Set<Class<?>> cmdClasses) {
    Map<String, List<String>> responseApiNameListMap = new HashMap<String, List<String>>();
    for (Class<?> cmdClass : cmdClasses) {
        APICommand apiCmdAnnotation = cmdClass.getAnnotation(APICommand.class);
        if (apiCmdAnnotation == null) {
            apiCmdAnnotation = cmdClass.getSuperclass().getAnnotation(APICommand.class);
        }
        if (apiCmdAnnotation == null || !apiCmdAnnotation.includeInApiDoc() || apiCmdAnnotation.name().isEmpty()) {
            continue;
        }
        String apiName = apiCmdAnnotation.name();
        if (s_logger.isTraceEnabled()) {
            s_logger.trace("Found api: " + apiName);
        }
        ApiDiscoveryResponse response = getCmdRequestMap(cmdClass, apiCmdAnnotation);
        Class<? extends BaseResponse> responseClass = apiCmdAnnotation.responseObject();
        String responseName = responseClass.getName();
        if (!responseName.contains("SuccessResponse")) {
            if (!responseApiNameListMap.containsKey(responseName)) {
                responseApiNameListMap.put(responseName, new ArrayList<String>());
            }
            responseApiNameListMap.get(responseName).add(apiName);
        }
        response.setRelated(responseName);
        Set<Field> responseFields = ReflectionUtils.getAllFields(responseClass);
        for (Field responseField : responseFields) {
            ApiResponseResponse responseResponse = getFieldResponseMap(responseField);
            response.addApiResponse(responseResponse);
        }
        response.setObjectName("api");
        s_apiNameDiscoveryResponseMap.put(apiName, response);
    }
    for (String apiName : s_apiNameDiscoveryResponseMap.keySet()) {
        ApiDiscoveryResponse response = s_apiNameDiscoveryResponseMap.get(apiName);
        Set<ApiParameterResponse> processedParams = new HashSet<ApiParameterResponse>();
        for (ApiParameterResponse param : response.getParams()) {
            if (responseApiNameListMap.containsKey(param.getRelated())) {
                List<String> relatedApis = responseApiNameListMap.get(param.getRelated());
                param.setRelated(StringUtils.defaultString(StringUtils.join(relatedApis, ",")));
            } else {
                param.setRelated(null);
            }
            processedParams.add(param);
        }
        response.setParams(processedParams);
        if (responseApiNameListMap.containsKey(response.getRelated())) {
            List<String> relatedApis = responseApiNameListMap.get(response.getRelated());
            relatedApis.remove(apiName);
            response.setRelated(StringUtils.join(relatedApis, ","));
        } else {
            response.setRelated(null);
        }
        s_apiNameDiscoveryResponseMap.put(apiName, response);
    }
    return responseApiNameListMap;
}
Also used : HashMap(java.util.HashMap) ApiResponseResponse(org.apache.cloudstack.api.response.ApiResponseResponse) ApiParameterResponse(org.apache.cloudstack.api.response.ApiParameterResponse) APICommand(org.apache.cloudstack.api.APICommand) Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) List(java.util.List) ApiDiscoveryResponse(org.apache.cloudstack.api.response.ApiDiscoveryResponse) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 5 with ApiDiscoveryResponse

use of org.apache.cloudstack.api.response.ApiDiscoveryResponse in project cloudstack by apache.

the class ApiDiscoveryTest method verifyListVirtualMachinesTagsField.

@Test
public void verifyListVirtualMachinesTagsField() throws Exception {
    ListResponse<ApiDiscoveryResponse> responses = (ListResponse<ApiDiscoveryResponse>) s_discoveryService.listApis(testUser, listVmsCmdName);
    assertNotNull("Response should not be null", responses);
    if (responses != null) {
        assertEquals("No. of response items should be one", 1, (int) responses.getCount());
        ApiDiscoveryResponse response = responses.getResponses().get(0);
        List<ApiResponseResponse> tagsResponse = response.getApiResponse().stream().filter(resp -> StringUtils.equals(resp.getName(), "tags")).collect(Collectors.toList());
        assertEquals("Tags field should be present in listVirtualMachines response fields", tagsResponse.size(), 1);
    }
}
Also used : Arrays(java.util.Arrays) BeforeClass(org.junit.BeforeClass) ApiDiscoveryResponse(org.apache.cloudstack.api.response.ApiDiscoveryResponse) ApiResponseResponse(org.apache.cloudstack.api.response.ApiResponseResponse) APIChecker(org.apache.cloudstack.acl.APIChecker) ConfigurationException(javax.naming.ConfigurationException) APICommand(org.apache.cloudstack.api.APICommand) StringUtils(org.apache.commons.lang3.StringUtils) Matchers.anyString(org.mockito.Matchers.anyString) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) User(com.cloud.user.User) PluggableService(com.cloud.utils.component.PluggableService) UserVO(com.cloud.user.UserVO) Assert.assertNotNull(org.junit.Assert.assertNotNull) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) ListVMsCmd(org.apache.cloudstack.api.command.user.vm.ListVMsCmd) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) Matchers.any(org.mockito.Matchers.any) List(java.util.List) ListApisCmd(org.apache.cloudstack.api.command.user.discovery.ListApisCmd) Assert.assertFalse(org.junit.Assert.assertFalse) ListResponse(org.apache.cloudstack.api.response.ListResponse) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) ListResponse(org.apache.cloudstack.api.response.ListResponse) ApiResponseResponse(org.apache.cloudstack.api.response.ApiResponseResponse) ApiDiscoveryResponse(org.apache.cloudstack.api.response.ApiDiscoveryResponse) Test(org.junit.Test)

Aggregations

ApiDiscoveryResponse (org.apache.cloudstack.api.response.ApiDiscoveryResponse)8 ListResponse (org.apache.cloudstack.api.response.ListResponse)5 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 User (com.cloud.user.User)2 PluggableService (com.cloud.utils.component.PluggableService)2 Field (java.lang.reflect.Field)2 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 APIChecker (org.apache.cloudstack.acl.APIChecker)2 APICommand (org.apache.cloudstack.api.APICommand)2 ApiParameterResponse (org.apache.cloudstack.api.response.ApiParameterResponse)2 ApiResponseResponse (org.apache.cloudstack.api.response.ApiResponseResponse)2 UserVO (com.cloud.user.UserVO)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 ConfigurationException (javax.naming.ConfigurationException)1