Search in sources :

Example 16 with AssetIPMapping

use of com.vmware.flowgate.common.model.AssetIPMapping in project flowgate by vmware.

the class AssetControllerTest method updateHostNameIPMappingFailureExample.

@Test
public void updateHostNameIPMappingFailureExample() throws Exception {
    SetOperations<String, String> setOperations = Mockito.mock(SetOperations.class);
    when(template.hasKey(anyString())).thenReturn(false);
    when(template.opsForSet()).thenReturn(setOperations);
    when(setOperations.add(anyString(), any())).thenReturn(1l);
    Asset server = createAsset();
    server.setCategory(AssetCategory.Server);
    server.setAssetName("cloud-sha1-esx2");
    server = assetRepository.save(server);
    Asset server1 = createAsset();
    server1.setCategory(AssetCategory.Server);
    server1.setAssetName("cloud-sha1-esx8");
    server1 = assetRepository.save(server1);
    AssetIPMapping assetipmapping = createAssetIPMapping();
    assetipmapping.setAssetname(server.getAssetName());
    assetipmapping = assetIPMappingRepository.save(assetipmapping);
    AssetIPMapping newAssetIPMapping = createAssetIPMapping();
    newAssetIPMapping.setId(assetipmapping.getId());
    newAssetIPMapping.setAssetname("cloud-server1");
    newAssetIPMapping.setIp("192.168.0.1");
    expectedEx.expect(WormholeRequestException.class);
    expectedEx.expectMessage("Can't find any asset with the name : " + newAssetIPMapping.getAssetname());
    MvcResult result = this.mockMvc.perform(put("/v1/assets/mapping/hostnameip").contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(newAssetIPMapping))).andExpect(status().is5xxServerError()).andReturn();
    if (result.getResolvedException() != null) {
        assetRepository.deleteById(server.getId());
        assetRepository.deleteById(server1.getId());
        assetIPMappingRepository.deleteById(assetipmapping.getId());
        throw result.getResolvedException();
    }
}
Also used : AssetIPMapping(com.vmware.flowgate.common.model.AssetIPMapping) Asset(com.vmware.flowgate.common.model.Asset) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MvcResult(org.springframework.test.web.servlet.MvcResult) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 17 with AssetIPMapping

use of com.vmware.flowgate.common.model.AssetIPMapping in project flowgate by vmware.

the class AssetControllerTest method updateHostNameIPMappingMacIsNullExample.

@Test
public void updateHostNameIPMappingMacIsNullExample() throws Exception {
    SetOperations<String, String> setOperations = Mockito.mock(SetOperations.class);
    when(template.hasKey(anyString())).thenReturn(false);
    when(template.opsForSet()).thenReturn(setOperations);
    when(setOperations.add(anyString(), any())).thenReturn(1l);
    Asset server = createAsset();
    server.setCategory(AssetCategory.Server);
    server.setAssetName("cloud-sha1-esx2");
    server = assetRepository.save(server);
    Asset server1 = createAsset();
    server1.setCategory(AssetCategory.Server);
    server1.setAssetName("cloud-sha1-esx8");
    server1 = assetRepository.save(server1);
    AssetIPMapping assetipmapping = createAssetIPMapping();
    assetipmapping.setAssetname(server.getAssetName());
    assetipmapping = assetIPMappingRepository.save(assetipmapping);
    AssetIPMapping newAssetIPMapping = createAssetIPMapping();
    newAssetIPMapping.setAssetname(server1.getAssetName());
    newAssetIPMapping.setId(assetipmapping.getId());
    newAssetIPMapping.setMacAddress(null);
    this.mockMvc.perform(put("/v1/assets/mapping/hostnameip").contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(newAssetIPMapping))).andExpect(status().isOk());
    assetipmapping = assetIPMappingRepository.findById(assetipmapping.getId()).get();
    assetRepository.deleteById(server.getId());
    assetRepository.deleteById(server1.getId());
    assetIPMappingRepository.deleteById(assetipmapping.getId());
    TestCase.assertEquals(server1.getAssetName(), assetipmapping.getAssetname());
    TestCase.assertNull(assetipmapping.getMacAddress());
}
Also used : AssetIPMapping(com.vmware.flowgate.common.model.AssetIPMapping) Asset(com.vmware.flowgate.common.model.Asset) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 18 with AssetIPMapping

use of com.vmware.flowgate.common.model.AssetIPMapping in project flowgate by vmware.

the class AssetControllerTest method getHostNameByIPExample.

@Test
public void getHostNameByIPExample() throws Exception {
    AssetIPMapping mapping1 = createAssetIPMapping();
    assetIPMappingRepository.save(mapping1);
    AssetIPMapping mapping2 = createAssetIPMapping();
    assetIPMappingRepository.save(mapping2);
    try {
        FieldDescriptor[] fieldpath = new FieldDescriptor[] { fieldWithPath("id").description("ID of the AssetIPMapping, created by flowgate"), fieldWithPath("ip").description("IP of AssetIPMapping."), fieldWithPath("macAddress").description("macAddress of IP."), fieldWithPath("assetname").description("name of asset.") };
        this.mockMvc.perform(get("/v1/assets/mapping/hostnameip/ip/{ip}", mapping1.getIp())).andExpect(status().isOk()).andDo(document("assets-getHostNameByIP-example", pathParameters(parameterWithName("ip").description("IP of AssetIPMapping.")), responseFields(fieldWithPath("[]").description("An array of AssetIPMapping")).andWithPrefix("[].", fieldpath)));
    } finally {
        assetIPMappingRepository.deleteById(mapping1.getId());
        assetIPMappingRepository.deleteById(mapping2.getId());
    }
}
Also used : AssetIPMapping(com.vmware.flowgate.common.model.AssetIPMapping) FieldDescriptor(org.springframework.restdocs.payload.FieldDescriptor) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 19 with AssetIPMapping

use of com.vmware.flowgate.common.model.AssetIPMapping in project flowgate by vmware.

the class InfoBloxService method executeAsync.

@Override
@Async("asyncServiceExecutor")
public void executeAsync(EventMessage eventMessage) {
    // The data format of the message. should be an ip address.
    // Do the business here.
    String message = eventMessage.getContent();
    logger.info(String.format("Try to find hostname for ip: %s", message));
    // check message , make sure it is an valid ip address;
    wormholeAPIClient.setServiceKey(serviceKeyConfig.getServiceKey());
    FacilitySoftwareConfig[] infoBloxes = wormholeAPIClient.getFacilitySoftwareInternalByType(SoftwareType.InfoBlox).getBody();
    List<InfoBloxIPInfoResult> infoBloxIPInfoResults = new ArrayList<>();
    List<InfoBloxIPInfoResult> hostRecordResults = null;
    List<InfoBloxIPInfoResult> ipv4addressResults = null;
    for (FacilitySoftwareConfig infoblox : infoBloxes) {
        if (!infoblox.checkIsActive()) {
            continue;
        }
        InfobloxClient client = buildInfobloxClient(infoblox);
        IntegrationStatus integrationStatus = infoblox.getIntegrationStatus();
        IntegrationStatus.Status status = integrationStatus == null ? null : integrationStatus.getStatus();
        try {
            if (status == null || IntegrationStatus.Status.ACTIVE.equals(status)) {
                hostRecordResults = client.queryHostRecordByIP(message);
                logger.debug("queryHostRecordByIP: {}", hostRecordResults);
                if (hostRecordResults != null && !hostRecordResults.isEmpty()) {
                    infoBloxIPInfoResults.addAll(hostRecordResults);
                }
            }
            if (hostRecordResults == null || hostRecordResults.isEmpty()) {
                ipv4addressResults = client.queryIpv4addressByIP(message);
                logger.debug("queryHostNamesByIP: {}", ipv4addressResults);
                if (ipv4addressResults != null && !ipv4addressResults.isEmpty()) {
                    infoBloxIPInfoResults.addAll(ipv4addressResults);
                }
                if (!IntegrationStatus.Status.WARNING.equals(status) && ipv4addressResults != null) {
                    updateIntegrationStatusToWarning(infoblox, message);
                }
            }
        } catch (ResourceAccessException e) {
            if (e.getCause().getCause() instanceof ConnectException) {
                checkAndUpdateIntegrationStatus(infoblox, e.getCause().getCause().getMessage());
                continue;
            }
        } catch (HttpClientErrorException e1) {
            logger.error("Failed to query data from Infoblox", e1);
            if (integrationStatus == null) {
                integrationStatus = new IntegrationStatus();
            }
            integrationStatus.setStatus(IntegrationStatus.Status.ERROR);
            integrationStatus.setDetail(e1.getMessage());
            integrationStatus.setRetryCounter(FlowgateConstant.DEFAULTNUMBEROFRETRIES);
            infoblox.setIntegrationStatus(integrationStatus);
            updateIntegrationStatus(infoblox);
            continue;
        }
        if (integrationStatus != null && integrationStatus.getRetryCounter() > 0) {
            integrationStatus.setRetryCounter(FlowgateConstant.DEFAULTNUMBEROFRETRIES);
            updateIntegrationStatus(infoblox);
        }
        if (!infoBloxIPInfoResults.isEmpty()) {
            for (InfoBloxIPInfoResult infoBloxIPInfoResult : infoBloxIPInfoResults) {
                try {
                    Asset asset = wormholeAPIClient.getAssetByName(infoBloxIPInfoResult.getHostName()).getBody();
                    if (asset == null) {
                        logger.info(String.format("hostname (%s) no found!", infoBloxIPInfoResult.getHostName()));
                        continue;
                    }
                } catch (HttpClientErrorException e) {
                    logger.error(String.format("Error when searching %s", infoBloxIPInfoResult.getHostName()), e);
                    continue;
                }
                AssetIPMapping tempMapping = new AssetIPMapping();
                tempMapping.setAssetname(infoBloxIPInfoResult.getHostName());
                tempMapping.setMacAddress(infoBloxIPInfoResult.getMacAddress());
                tempMapping.setIp(message);
                AssetIPMapping[] mappings = wormholeAPIClient.getHostnameIPMappingByIP(message).getBody();
                boolean isNewMapping = true;
                if (null != mappings && mappings.length > 0) {
                    for (AssetIPMapping mapping : mappings) {
                        if (tempMapping.getAssetname().equals(mapping.getAssetname())) {
                            if (!StringUtils.equals(mapping.getMacAddress(), tempMapping.getMacAddress())) {
                                mapping.setMacAddress(tempMapping.getMacAddress());
                                wormholeAPIClient.updateHostnameIPMapping(mapping);
                            }
                            isNewMapping = false;
                            break;
                        }
                    }
                }
                if (isNewMapping) {
                    wormholeAPIClient.createHostnameIPMapping(tempMapping);
                }
                logger.info(String.format("Find hostname %s for ip %s", infoBloxIPInfoResult.getHostName(), message));
                return;
            }
        }
    }
    logger.info(String.format("Cannot find the hostname for IP: %s", message));
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) IntegrationStatus(com.vmware.flowgate.common.model.IntegrationStatus) ArrayList(java.util.ArrayList) ResourceAccessException(org.springframework.web.client.ResourceAccessException) InfoBloxIPInfoResult(com.vmware.flowgate.infobloxworker.model.InfoBloxIPInfoResult) InfobloxClient(com.vmware.flowgate.infobloxworker.service.InfobloxClient) AssetIPMapping(com.vmware.flowgate.common.model.AssetIPMapping) FacilitySoftwareConfig(com.vmware.flowgate.common.model.FacilitySoftwareConfig) Asset(com.vmware.flowgate.common.model.Asset) ConnectException(java.net.ConnectException) Async(org.springframework.scheduling.annotation.Async)

Aggregations

AssetIPMapping (com.vmware.flowgate.common.model.AssetIPMapping)19 Test (org.junit.Test)12 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)12 Asset (com.vmware.flowgate.common.model.Asset)10 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)6 ArrayList (java.util.ArrayList)4 IntegrationStatus (com.vmware.flowgate.common.model.IntegrationStatus)3 MvcResult (org.springframework.test.web.servlet.MvcResult)3 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)3 ServerMapping (com.vmware.flowgate.common.model.ServerMapping)2 EventMessage (com.vmware.flowgate.common.model.redis.message.EventMessage)2 IOException (java.io.IOException)2 ConnectException (java.net.ConnectException)2 HashMap (java.util.HashMap)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 FacilitySoftwareConfig (com.vmware.flowgate.common.model.FacilitySoftwareConfig)1 MetricData (com.vmware.flowgate.common.model.MetricData)1 WormholeRequestException (com.vmware.flowgate.exception.WormholeRequestException)1 InfoBloxIPInfoResult (com.vmware.flowgate.infobloxworker.model.InfoBloxIPInfoResult)1 InfobloxClient (com.vmware.flowgate.infobloxworker.service.InfobloxClient)1