Search in sources :

Example 6 with InfoBloxIPInfoResult

use of com.vmware.flowgate.infobloxworker.model.InfoBloxIPInfoResult in project flowgate by vmware.

the class InfobloxClientTest method queryHostNamesByIPTestZoneNonNull.

@Test
public void queryHostNamesByIPTestZoneNonNull() {
    String ip = "10.161.71.154";
    List<InfoBloxIPInfoResult> expectedResult = this.getExpectedResult();
    InfobloxClient infobloxClient = new InfobloxClient(getInfobloxFacilitySoftware()[0]);
    infobloxClient = Mockito.spy(infobloxClient);
    Mockito.doReturn(this.getJsonResultForQueryHostNamesZoneNonNull()).when(infobloxClient).getIpv4address(ip);
    List<InfoBloxIPInfoResult> actualResult = infobloxClient.queryIpv4addressByIP(ip);
    TestCase.assertEquals(expectedResult.size(), actualResult.size());
    for (int i = 0; i < expectedResult.size(); i++) {
        TestCase.assertEquals(expectedResult.get(i).getIpAddress(), actualResult.get(i).getIpAddress());
        TestCase.assertEquals(expectedResult.get(i).getHostName(), actualResult.get(i).getHostName());
        TestCase.assertEquals(expectedResult.get(i).getMacAddress(), actualResult.get(i).getMacAddress());
    }
}
Also used : InfobloxClient(com.vmware.flowgate.infobloxworker.service.InfobloxClient) InfoBloxIPInfoResult(com.vmware.flowgate.infobloxworker.model.InfoBloxIPInfoResult) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 7 with InfoBloxIPInfoResult

use of com.vmware.flowgate.infobloxworker.model.InfoBloxIPInfoResult 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

InfoBloxIPInfoResult (com.vmware.flowgate.infobloxworker.model.InfoBloxIPInfoResult)7 InfobloxClient (com.vmware.flowgate.infobloxworker.service.InfobloxClient)5 Test (org.junit.Test)4 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4 ArrayList (java.util.ArrayList)3 Asset (com.vmware.flowgate.common.model.Asset)1 AssetIPMapping (com.vmware.flowgate.common.model.AssetIPMapping)1 FacilitySoftwareConfig (com.vmware.flowgate.common.model.FacilitySoftwareConfig)1 IntegrationStatus (com.vmware.flowgate.common.model.IntegrationStatus)1 ConnectException (java.net.ConnectException)1 Async (org.springframework.scheduling.annotation.Async)1 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)1 ResourceAccessException (org.springframework.web.client.ResourceAccessException)1