Search in sources :

Example 11 with Asset

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

the class SyncWiremapDataJobTest method generatorWiremapDataTest.

@Test
public void generatorWiremapDataTest() {
    Asset assetFromflowgate = createServer();
    Map<String, String> network = new HashMap<String, String>();
    network.put("sin2-build-rdev1", "5c778c598ecf859960e2be30");
    Map<String, String> pdu = new HashMap<String, String>();
    pdu.put("w3r17c05-pdu4", "wertbhukloiup5996q23df530");
    List<EndDevice> devices = getDevices();
    Asset asset = labsdbService.generatorWiremapData(assetFromflowgate, pdu, devices, network);
    List<String> networks = asset.getSwitches();
    TestCase.assertEquals("5c778c598ecf859960e2be30", networks.get(0));
    String device = asset.getJustificationfields().get(FlowgateConstant.NETWORK_PORT_FOR_SERVER);
    TestCase.assertEquals("01" + FlowgateConstant.SEPARATOR + "sin2-build-rdev1" + FlowgateConstant.SEPARATOR + "onboard-1" + FlowgateConstant.SEPARATOR + "" + "5c778c598ecf859960e2be30", device);
}
Also used : EndDevice(com.vmware.flowgate.labsdb.common.EndDevice) HashMap(java.util.HashMap) Asset(com.vmware.flowgate.common.model.Asset) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 12 with Asset

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

the class VCDataService method queryHostMetaData.

public void queryHostMetaData(SDDCSoftwareConfig vc) {
    Map<String, ServerMapping> serverMappingMap = getVaildServerMapping(vc);
    if (serverMappingMap == null || serverMappingMap.isEmpty()) {
        logger.info("serverMapping is invaild");
        return;
    }
    try (VsphereClient vsphereClient = connectVsphere(vc)) {
        String vcInstanceUUID = vsphereClient.getVCUUID();
        Collection<HostSystem> hosts = vsphereClient.getAllHost();
        if (hosts == null || hosts.isEmpty()) {
            logger.error("vsphere: " + vsphereClient.getVCUUID() + " get hosts is null");
            return;
        }
        Collection<ClusterComputeResource> clusters = vsphereClient.getAllClusterComputeResource();
        Map<String, ClusterComputeResource> clusterMap = new HashMap<String, ClusterComputeResource>();
        if (clusters == null) {
            logger.error("vsphere: " + vsphereClient.getVCUUID() + " get clusters is null");
            return;
        }
        for (ClusterComputeResource cluster : clusters) {
            clusterMap.put(cluster._getRef().getValue(), cluster);
        }
        for (HostSystem host : hosts) {
            String mobId = host._getRef().getValue();
            if (serverMappingMap.containsKey(mobId)) {
                ServerMapping serverMapping = serverMappingMap.get(mobId);
                String assetId = serverMapping.getAsset();
                Asset hostMappingAsset = restClient.getAssetByID(assetId).getBody();
                if (hostMappingAsset == null) {
                    logger.error("serverMapping: " + serverMapping.getId() + " get asset: " + assetId + " is null");
                    continue;
                }
                HostInfo hostInfo = new HostInfo();
                HashMap<String, String> hostJustification = hostMappingAsset.getJustificationfields();
                if (hostJustification != null && !hostJustification.isEmpty()) {
                    String oldHostInfoString = hostJustification.get(FlowgateConstant.HOST_METADATA);
                    if (oldHostInfoString != null) {
                        try {
                            hostInfo = mapper.readValue(oldHostInfoString, HostInfo.class);
                        } catch (IOException ioException) {
                            logger.error("Cannot process message", ioException);
                            continue;
                        }
                    }
                }
                boolean hostNeedUpdate = false;
                boolean clusterNeedUpdate = false;
                hostNeedUpdate = feedHostMetaData(host, hostInfo);
                if (clusters != null && !clusters.isEmpty()) {
                    clusterNeedUpdate = feedClusterMetaData(clusterMap, host, hostInfo, vcInstanceUUID);
                }
                if (hostNeedUpdate || clusterNeedUpdate) {
                    try {
                        String vcHostObjStr = mapper.writeValueAsString(hostInfo);
                        hostJustification.put(FlowgateConstant.HOST_METADATA, vcHostObjStr);
                        hostMappingAsset.setJustificationfields(hostJustification);
                    } catch (JsonProcessingException jsonProcessingException) {
                        logger.error("Format host info map error", jsonProcessingException);
                        continue;
                    }
                    restClient.saveAssets(hostMappingAsset);
                } else {
                    logger.debug("host: " + mobId + " No update required");
                    continue;
                }
            }
        }
    } catch (ConnectionException connectionException) {
        checkAndUpdateIntegrationStatus(vc, connectionException.getMessage());
        return;
    } catch (ExecutionException executionException) {
        if (executionException.getCause() instanceof InvalidLogin) {
            logger.error("Failed to push data to " + vc.getServerURL(), executionException);
            checkAndUpdateIntegrationStatus(vc, "Invalid username or password.");
            return;
        }
    } catch (Exception exception) {
        logger.error("Failed to sync the host metadata to VC ", exception);
        return;
    }
}
Also used : ClusterComputeResource(com.vmware.vim.binding.vim.ClusterComputeResource) HashMap(java.util.HashMap) ServerMapping(com.vmware.flowgate.common.model.ServerMapping) VsphereClient(com.vmware.flowgate.vcworker.client.VsphereClient) IOException(java.io.IOException) ConnectionException(com.vmware.vim.vmomi.client.exception.ConnectionException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ExecutionException(java.util.concurrent.ExecutionException) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) HostSystem(com.vmware.vim.binding.vim.HostSystem) InvalidLogin(com.vmware.vim.binding.vim.fault.InvalidLogin) Asset(com.vmware.flowgate.common.model.Asset) ExecutionException(java.util.concurrent.ExecutionException) HostInfo(com.vmware.flowgate.vcworker.model.HostInfo) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ConnectionException(com.vmware.vim.vmomi.client.exception.ConnectionException)

Example 13 with Asset

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

the class VCDataService method feedData.

private void feedData(Map<String, Asset> assetDictionary, List<ServerMapping> validMapping, Map<String, HostSystem> hostDictionary) {
    restClient.setServiceKey(serviceKeyConfig.getServiceKey());
    for (ServerMapping validServer : validMapping) {
        HostSystem host = hostDictionary.get(validServer.getVcMobID());
        Asset asset = assetDictionary.get(validServer.getAsset());
        feedAssetMetricsFormulars(asset);
        BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(asset);
        for (String key : VCConstants.hostCustomAttrMapping.keySet()) {
            host.setCustomValue(VCConstants.hostCustomAttrMapping.get(key), String.valueOf(wrapper.getPropertyValue(key)));
        }
        Map<String, String> idNamePortMapping = getPDUSwitchIDNamePortMapping(asset);
        if (asset.getPdus() != null) {
            // check pdu and port
            List<String> pduNameList = new ArrayList<String>();
            for (String pduid : asset.getPdus()) {
                if (idNamePortMapping.containsKey(pduid)) {
                    pduNameList.add(idNamePortMapping.get(pduid));
                } else {
                    Asset pduAsset = restClient.getAssetByID(pduid).getBody();
                    if (pduAsset != null) {
                        pduNameList.add(pduAsset.getAssetName());
                    }
                }
            }
            host.setCustomValue(VCConstants.ASSET_PDUs, String.join(",", pduNameList));
        }
        if (asset.getSwitches() != null) {
            List<String> switchNameList = new ArrayList<String>();
            for (String switchID : asset.getSwitches()) {
                if (idNamePortMapping.containsKey(switchID)) {
                    switchNameList.add(idNamePortMapping.get(switchID));
                } else {
                    Asset switchAsset = restClient.getAssetByID(switchID).getBody();
                    if (switchAsset != null) {
                        switchNameList.add(switchAsset.getAssetName());
                    }
                }
            }
            host.setCustomValue(VCConstants.ASSET_SWITCHs, String.join(",", switchNameList));
        }
    }
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) ServerMapping(com.vmware.flowgate.common.model.ServerMapping) HostSystem(com.vmware.vim.binding.vim.HostSystem) ArrayList(java.util.ArrayList) Asset(com.vmware.flowgate.common.model.Asset)

Example 14 with Asset

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

the class VCDataService method syncCustomerAttrsData.

private void syncCustomerAttrsData(SDDCSoftwareConfig vcInfo) {
    restClient.setServiceKey(serviceKeyConfig.getServiceKey());
    try (VsphereClient vsphereClient = connectVsphere(vcInfo)) {
        ServerMapping[] mappings = null;
        try {
            mappings = restClient.getServerMappingsByVC(vcInfo.getId()).getBody();
        } catch (HttpClientErrorException clientError) {
            if (clientError.getRawStatusCode() != HttpStatus.NOT_FOUND.value()) {
                throw clientError;
            }
            mappings = new ServerMapping[0];
        }
        HashMap<String, ServerMapping> mobIdDictionary = new HashMap<String, ServerMapping>();
        for (ServerMapping mapping : mappings) {
            mobIdDictionary.put(mapping.getVcMobID(), mapping);
        }
        List<ServerMapping> validMapping = new ArrayList<ServerMapping>();
        Collection<HostSystem> hosts = vsphereClient.getAllHost();
        Map<String, HostSystem> hostDictionary = new HashMap<String, HostSystem>();
        for (HostSystem host : hosts) {
            String mobId = host._getRef().getValue();
            String hostName = host.getName();
            if (mobIdDictionary.containsKey(mobId)) {
                ServerMapping serverMapping = mobIdDictionary.get(mobId);
                if (!serverMapping.getVcHostName().equals(hostName)) {
                    // need to update the hostname.
                    serverMapping.setVcHostName(hostName);
                    restClient.saveServerMapping(serverMapping);
                }
                if (serverMapping.getAsset() != null) {
                    validMapping.add(serverMapping);
                } else {
                    // check the hostNameIP mapping
                    String ipaddress = IPAddressUtil.getIPAddress(hostName);
                    if (null != ipaddress) {
                        AssetIPMapping[] ipMappings = restClient.getHostnameIPMappingByIP(ipaddress).getBody();
                        if (null != ipMappings && ipMappings.length > 0) {
                            // update the mapping
                            String assetName = ipMappings[0].getAssetname();
                            Asset asset = restClient.getAssetByName(assetName).getBody();
                            if (asset != null) {
                                serverMapping.setAsset(asset.getId());
                                restClient.saveServerMapping(serverMapping);
                                validMapping.add(serverMapping);
                                feedAssetMetricsFormulars(asset);
                            }
                        } else {
                            // seems we don't have the ip hostname mapping. Notify infoblox to check the ip
                            logger.info("Notify infoblox to check ip: " + ipaddress);
                            publisher.publish(null, ipaddress);
                        }
                    }
                }
                hostDictionary.put(mobId, host);
            } else {
                ServerMapping newMapping = new ServerMapping();
                newMapping.setVcHostName(hostName);
                newMapping.setVcID(vcInfo.getId());
                newMapping.setVcMobID(mobId);
                newMapping.setVcInstanceUUID(vsphereClient.getVCUUID());
                String ipaddress = IPAddressUtil.getIPAddress(hostName);
                logger.info(String.format("hostName %s, ipaddress: %s", hostName, ipaddress));
                // publish message to queue.
                if (null != ipaddress) {
                    logger.info("Notify infoblox");
                    publisher.publish(null, hostName);
                    AssetIPMapping[] ipMappings = restClient.getHostnameIPMappingByIP(ipaddress).getBody();
                    if (null != ipMappings && ipMappings.length > 0) {
                        // update the mapping
                        String assetName = ipMappings[0].getAssetname();
                        Asset asset = restClient.getAssetByName(assetName).getBody();
                        if (asset != null) {
                            newMapping.setAsset(asset.getId());
                            feedAssetMetricsFormulars(asset);
                        }
                    }
                }
                restClient.saveServerMapping(newMapping);
            }
        }
        // feed meta data to VC.
        Asset[] assets = restClient.getAssetsByVCID(vcInfo.getId()).getBody();
        Map<String, Asset> assetDictionary = new HashMap<String, Asset>();
        for (Asset asset : assets) {
            assetDictionary.put(asset.getId(), asset);
        }
        feedData(assetDictionary, validMapping, hostDictionary);
        validClusterHostsLocationAntiaffinity(vcInfo, assetDictionary, validMapping);
    } catch (ConnectionException connectionException) {
        checkAndUpdateIntegrationStatus(vcInfo, connectionException.getMessage());
        return;
    } catch (ExecutionException executionException) {
        if (executionException.getCause() instanceof InvalidLogin) {
            logger.error("Failed to push data to " + vcInfo.getServerURL(), executionException);
            IntegrationStatus integrationStatus = vcInfo.getIntegrationStatus();
            if (integrationStatus == null) {
                integrationStatus = new IntegrationStatus();
            }
            integrationStatus.setStatus(IntegrationStatus.Status.ERROR);
            integrationStatus.setDetail("Invalid username or password.");
            integrationStatus.setRetryCounter(FlowgateConstant.DEFAULTNUMBEROFRETRIES);
            updateIntegrationStatus(vcInfo);
            return;
        }
    } catch (Exception exception) {
        logger.error("Failed to push data to " + vcInfo.getServerURL(), exception);
    }
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) HashMap(java.util.HashMap) VsphereClient(com.vmware.flowgate.vcworker.client.VsphereClient) ServerMapping(com.vmware.flowgate.common.model.ServerMapping) IntegrationStatus(com.vmware.flowgate.common.model.IntegrationStatus) ArrayList(java.util.ArrayList) ConnectionException(com.vmware.vim.vmomi.client.exception.ConnectionException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ExecutionException(java.util.concurrent.ExecutionException) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) AssetIPMapping(com.vmware.flowgate.common.model.AssetIPMapping) HostSystem(com.vmware.vim.binding.vim.HostSystem) InvalidLogin(com.vmware.vim.binding.vim.fault.InvalidLogin) Asset(com.vmware.flowgate.common.model.Asset) ExecutionException(java.util.concurrent.ExecutionException) ConnectionException(com.vmware.vim.vmomi.client.exception.ConnectionException)

Example 15 with Asset

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

the class VCDataServiceTest method testQueryHostMetrics.

@Test
public void testQueryHostMetrics() throws Exception {
    SDDCSoftwareConfig vc = Mockito.mock(SDDCSoftwareConfig.class);
    HashMap<String, ServerMapping> serverMappingMap = new HashMap<String, ServerMapping>();
    ServerMapping mapping1 = new ServerMapping();
    mapping1.setVcMobID("vc1");
    mapping1.setAsset("asset1");
    serverMappingMap.put(mapping1.getVcMobID(), mapping1);
    ServerMapping mapping2 = new ServerMapping();
    mapping2.setVcMobID("vc2");
    mapping2.setAsset("asset2");
    serverMappingMap.put(mapping2.getVcMobID(), mapping2);
    doReturn(serverMappingMap).when(service).getVaildServerMapping(any());
    doReturn(vsphereClient).when(service).connectVsphere(any());
    HostSystem host1 = Mockito.mock(HostSystem.class);
    HostSystem host2 = Mockito.mock(HostSystem.class);
    Collection<HostSystem> hosts = new ArrayList<>();
    hosts.add(host1);
    hosts.add(host2);
    when(vsphereClient.getAllHost()).thenReturn(hosts);
    ManagedObjectReference mor1 = Mockito.mock(ManagedObjectReference.class);
    ManagedObjectReference mor2 = Mockito.mock(ManagedObjectReference.class);
    when(host1._getRef()).thenReturn(mor1);
    when(mor1.getValue()).thenReturn("vc1");
    when(host2._getRef()).thenReturn(mor2);
    when(mor2.getValue()).thenReturn("vc2");
    Asset asset = new Asset();
    asset.setId("asset1");
    Map<String, String> metricsFormulars = new HashMap<>();
    Map<String, String> metrics = new HashMap<>();
    metricsFormulars.put(FlowgateConstant.HOST_METRICS, mapper.writeValueAsString(metrics));
    asset.setMetricsformulars(metricsFormulars);
    ResponseEntity<Asset> assets = Mockito.mock(ResponseEntity.class);
    when(restClient.getAssetByID(anyString())).thenReturn(assets);
    when(assets.getBody()).thenReturn(asset);
    doNothing().when(service).feedHostUsageData(any(), any(), any());
    service.queryHostMetrics(vc);
}
Also used : SDDCSoftwareConfig(com.vmware.flowgate.common.model.SDDCSoftwareConfig) HashMap(java.util.HashMap) ServerMapping(com.vmware.flowgate.common.model.ServerMapping) HostSystem(com.vmware.vim.binding.vim.HostSystem) ArrayList(java.util.ArrayList) Asset(com.vmware.flowgate.common.model.Asset) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ManagedObjectReference(com.vmware.vim.binding.vmodl.ManagedObjectReference) Test(org.junit.Test)

Aggregations

Asset (com.vmware.flowgate.common.model.Asset)173 Test (org.junit.Test)96 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)90 HashMap (java.util.HashMap)86 ArrayList (java.util.ArrayList)82 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)36 NlyteAsset (com.vmware.flowgate.nlyteworker.model.NlyteAsset)35 Map (java.util.Map)34 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)31 RealTimeData (com.vmware.flowgate.common.model.RealTimeData)23 IOException (java.io.IOException)23 ServerMapping (com.vmware.flowgate.common.model.ServerMapping)22 MetricData (com.vmware.flowgate.common.model.MetricData)16 HandleAssetUtil (com.vmware.flowgate.nlyteworker.scheduler.job.common.HandleAssetUtil)16 HashSet (java.util.HashSet)15 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)13 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)13 List (java.util.List)12 MvcResult (org.springframework.test.web.servlet.MvcResult)12 TypeReference (com.fasterxml.jackson.core.type.TypeReference)10