Search in sources :

Example 1 with NlyteAPIClient

use of com.vmware.flowgate.nlyteworker.restclient.NlyteAPIClient in project flowgate by vmware.

the class NlyteDataService method removeInActiveData.

private void removeInActiveData(FacilitySoftwareConfig nlyte) {
    NlyteAPIClient nlyteAPIclient = createClient(nlyte);
    restClient.setServiceKey(serviceKeyConfig.getServiceKey());
    List<Asset> servers = restClient.getAllAssetsBySourceAndType(nlyte.getId(), AssetCategory.Server);
    List<Asset> pdus = restClient.getAllAssetsBySourceAndType(nlyte.getId(), AssetCategory.PDU);
    List<Asset> networks = restClient.getAllAssetsBySourceAndType(nlyte.getId(), AssetCategory.Networks);
    List<Asset> cabinets = restClient.getAllAssetsBySourceAndType(nlyte.getId(), AssetCategory.Cabinet);
    long currentTime = System.currentTimeMillis();
    // remove inactive pdus information from server and remove inactive pdus
    long expiredTimeRange = FlowgateConstant.DEFAULTEXPIREDTIMERANGE;
    String expiredTimeValue = template.opsForValue().get(EventMessageUtil.EXPIREDTIMERANGE);
    if (expiredTimeValue != null) {
        expiredTimeRange = Long.valueOf(expiredTimeValue);
    }
    for (Asset pdu : pdus) {
        if (!pdu.isExpired(currentTime, expiredTimeRange)) {
            continue;
        }
        NlyteAsset asset = nlyteAPIclient.getAssetbyAssetNumber(AssetCategory.PDU, pdu.getAssetNumber());
        if (asset == null || !assetIsActived(asset, AssetCategory.PDU)) {
            servers = removePduFromServer(servers, pdu.getId());
            restClient.saveAssets(servers);
            restClient.removeAssetByID(pdu.getId());
        } else if (asset != null && assetIsActived(asset, AssetCategory.PDU)) {
            pdu.setLastupdate(currentTime);
            restClient.saveAssets(pdu);
        }
    }
    // remove inactive network information from servers and remove inactive networks
    for (Asset network : networks) {
        if (!network.isExpired(currentTime, expiredTimeRange)) {
            continue;
        }
        NlyteAsset asset = nlyteAPIclient.getAssetbyAssetNumber(AssetCategory.Networks, network.getAssetNumber());
        if (asset == null || !assetIsActived(asset, AssetCategory.Networks)) {
            servers = removeNetworkFromServer(servers, network.getId());
            restClient.saveAssets(servers);
            restClient.removeAssetByID(network.getId());
        } else if (asset != null && assetIsActived(asset, AssetCategory.Networks)) {
            network.setLastupdate(currentTime);
            restClient.saveAssets(network);
        }
    }
    // remove cabinets
    for (Asset cabinet : cabinets) {
        if (!cabinet.isExpired(currentTime, expiredTimeRange)) {
            continue;
        }
        NlyteAsset asset = nlyteAPIclient.getAssetbyAssetNumber(AssetCategory.Cabinet, cabinet.getAssetNumber());
        if (asset == null || !assetIsActived(asset, AssetCategory.Cabinet)) {
            restClient.removeAssetByID(cabinet.getId());
        } else if (asset != null && assetIsActived(asset, AssetCategory.Cabinet)) {
            cabinet.setLastupdate(currentTime);
            restClient.saveAssets(cabinet);
        }
    }
    // get all serverMapping
    SDDCSoftwareConfig[] vcs = restClient.getInternalSDDCSoftwareConfigByType(SDDCSoftwareConfig.SoftwareType.VCENTER).getBody();
    SDDCSoftwareConfig[] vros = restClient.getInternalSDDCSoftwareConfigByType(SDDCSoftwareConfig.SoftwareType.VRO).getBody();
    List<ServerMapping> mappings = new ArrayList<ServerMapping>();
    for (SDDCSoftwareConfig vc : vcs) {
        mappings.addAll(new ArrayList<>(Arrays.asList(restClient.getServerMappingsByVC(vc.getId()).getBody())));
    }
    for (SDDCSoftwareConfig vro : vros) {
        mappings.addAll(new ArrayList<>(Arrays.asList(restClient.getServerMappingsByVRO(vro.getId()).getBody())));
    }
    // remove inactive asset from serverMapping and remove inactive servers
    for (Asset server : servers) {
        if (!server.isExpired(currentTime, expiredTimeRange)) {
            continue;
        }
        NlyteAsset asset = nlyteAPIclient.getAssetbyAssetNumber(AssetCategory.Server, server.getAssetNumber());
        if (asset == null || !assetIsActived(asset, AssetCategory.Server)) {
            for (ServerMapping mapping : mappings) {
                if (mapping.getAsset() == null) {
                    continue;
                }
                if (server.getId().equals(mapping.getAsset())) {
                    mapping.setAsset(null);
                    restClient.saveServerMapping(mapping);
                }
            }
            restClient.removeAssetByID(server.getId());
        } else if (asset != null && assetIsActived(asset, AssetCategory.Server)) {
            server.setLastupdate(currentTime);
            restClient.saveAssets(server);
        }
    }
}
Also used : SDDCSoftwareConfig(com.vmware.flowgate.common.model.SDDCSoftwareConfig) NlyteAPIClient(com.vmware.flowgate.nlyteworker.restclient.NlyteAPIClient) ServerMapping(com.vmware.flowgate.common.model.ServerMapping) NlyteAsset(com.vmware.flowgate.nlyteworker.model.NlyteAsset) ArrayList(java.util.ArrayList) NlyteAsset(com.vmware.flowgate.nlyteworker.model.NlyteAsset) Asset(com.vmware.flowgate.common.model.Asset)

Example 2 with NlyteAPIClient

use of com.vmware.flowgate.nlyteworker.restclient.NlyteAPIClient in project flowgate by vmware.

the class NlyteDataService method SyncAlldata.

private void SyncAlldata(FacilitySoftwareConfig nlyte) {
    NlyteAPIClient nlyteAPIclient = createClient(nlyte);
    restClient.setServiceKey(serviceKeyConfig.getServiceKey());
    HandleAssetUtil assetUtil = new HandleAssetUtil();
    List<NlyteAsset> nlyteAssets = null;
    try {
        nlyteAssets = nlyteAPIclient.getAssets(true, AssetCategory.Cabinet);
    } catch (HttpClientErrorException e) {
        logger.error("Failed to query data from Nlyte", e);
        IntegrationStatus integrationStatus = nlyte.getIntegrationStatus();
        if (integrationStatus == null) {
            integrationStatus = new IntegrationStatus();
        }
        integrationStatus.setStatus(IntegrationStatus.Status.ERROR);
        integrationStatus.setDetail(e.getMessage());
        integrationStatus.setRetryCounter(FlowgateConstant.DEFAULTNUMBEROFRETRIES);
        updateIntegrationStatus(nlyte);
        return;
    } catch (ResourceAccessException e1) {
        if (e1.getCause().getCause() instanceof ConnectException) {
            checkAndUpdateIntegrationStatus(nlyte, e1.getMessage());
            return;
        }
    }
    HashMap<Long, String> chassisMountedAssetNumberAndChassisIdMap = null;
    HashMap<Integer, LocationGroup> locationMap = assetUtil.initLocationGroupMap(nlyteAPIclient);
    HashMap<Integer, Manufacturer> manufacturerMap = assetUtil.initManufacturersMap(nlyteAPIclient);
    HashMap<Integer, Material> cabinetMaterialMap = new HashMap<Integer, Material>();
    List<Material> cabinetMaterials = nlyteAPIclient.getMaterials(true, HandleAssetUtil.cabinetMaterials);
    for (Material material : cabinetMaterials) {
        material.setMaterialType(AssetCategory.Cabinet);
        cabinetMaterialMap.put(material.getMaterialID(), material);
    }
    List<Asset> cabinetsNeedToSaveOrUpdate = generateAssets(nlyte.getId(), nlyteAssets, locationMap, manufacturerMap, cabinetMaterialMap, AssetCategory.Cabinet, chassisMountedAssetNumberAndChassisIdMap);
    if (cabinetsNeedToSaveOrUpdate.isEmpty()) {
        logger.info("No cabinet asset need to save");
    } else {
        restClient.saveAssets(cabinetsNeedToSaveOrUpdate);
        logger.info("Finish sync the cabinets data for: " + nlyte.getName() + ", size: " + cabinetsNeedToSaveOrUpdate.size());
    }
    // init cabinetIdAndNameMap
    HashMap<Integer, String> cabinetIdAndNameMap = getCabinetIdAndNameMap(nlyteAssets);
    nlyteAssets = nlyteAPIclient.getAssets(true, AssetCategory.Chassis);
    nlyteAssets = supplementCabinetName(cabinetIdAndNameMap, nlyteAssets);
    List<Material> chassisMaterials = nlyteAPIclient.getMaterials(true, HandleAssetUtil.chassisMaterials);
    HashMap<Integer, Material> chassisMaterialMap = new HashMap<Integer, Material>();
    for (Material material : chassisMaterials) {
        material.setMaterialType(AssetCategory.Chassis);
        chassisMaterialMap.put(material.getMaterialID(), material);
    }
    List<Asset> chassisNeedToSaveOrUpdate = generateAssets(nlyte.getId(), nlyteAssets, locationMap, manufacturerMap, chassisMaterialMap, AssetCategory.Chassis, chassisMountedAssetNumberAndChassisIdMap);
    if (chassisNeedToSaveOrUpdate.isEmpty()) {
        logger.info("No chassis asset need to save");
    } else {
        restClient.saveAssets(chassisNeedToSaveOrUpdate);
        logger.info("Finish sync the chassis data for: " + nlyte.getName() + ", size: " + chassisNeedToSaveOrUpdate.size());
    }
    List<Asset> chassisFromFlowgate = restClient.getAllAssetsBySourceAndType(nlyte.getId(), AssetCategory.Chassis);
    chassisMountedAssetNumberAndChassisIdMap = generateMountedAssetNumberAndChassisAssetIdMap(chassisFromFlowgate);
    nlyteAssets = nlyteAPIclient.getAssets(true, AssetCategory.Server);
    nlyteAssets = supplementCabinetName(cabinetIdAndNameMap, nlyteAssets);
    HashMap<Integer, Material> materialMap = assetUtil.initServerMaterialsMap(nlyteAPIclient);
    List<Asset> serversNeedToSaveOrUpdate = generateAssets(nlyte.getId(), nlyteAssets, locationMap, manufacturerMap, materialMap, AssetCategory.Server, chassisMountedAssetNumberAndChassisIdMap);
    if (serversNeedToSaveOrUpdate.isEmpty()) {
        logger.info("No server asset need to save");
    } else {
        restClient.saveAssets(serversNeedToSaveOrUpdate);
        logger.info("Finish sync the servers data for: " + nlyte.getName() + ", size: " + serversNeedToSaveOrUpdate.size());
    }
    HashMap<Integer, Material> pduMaterialMap = new HashMap<Integer, Material>();
    nlyteAssets = nlyteAPIclient.getAssets(true, AssetCategory.PDU);
    nlyteAssets = supplementCabinetName(cabinetIdAndNameMap, nlyteAssets);
    List<Material> powerStripMaterials = nlyteAPIclient.getMaterials(true, HandleAssetUtil.powerStripMaterial);
    for (Material material : powerStripMaterials) {
        material.setMaterialType(AssetCategory.PDU);
        pduMaterialMap.put(material.getMaterialID(), material);
    }
    List<Asset> pDUsNeedToSaveOrUpdate = generateAssets(nlyte.getId(), nlyteAssets, locationMap, manufacturerMap, pduMaterialMap, AssetCategory.PDU, chassisMountedAssetNumberAndChassisIdMap);
    if (pDUsNeedToSaveOrUpdate.isEmpty()) {
        logger.info("No pdu asset need to save");
    } else {
        savePduAssetAndUpdatePduUsageFormula(pDUsNeedToSaveOrUpdate);
        logger.info("Finish sync the pdus data for: " + nlyte.getName() + ", size: " + pDUsNeedToSaveOrUpdate.size());
    }
    HashMap<Integer, Material> networkMaterialMap = new HashMap<Integer, Material>();
    nlyteAssets = nlyteAPIclient.getAssets(true, AssetCategory.Networks);
    nlyteAssets = supplementCabinetName(cabinetIdAndNameMap, nlyteAssets);
    List<Material> networkMaterials = nlyteAPIclient.getMaterials(true, HandleAssetUtil.networkMaterials);
    for (Material material : networkMaterials) {
        material.setMaterialType(AssetCategory.Networks);
        networkMaterialMap.put(material.getMaterialID(), material);
    }
    List<Asset> networkersNeedToSaveOrUpdate = generateAssets(nlyte.getId(), nlyteAssets, locationMap, manufacturerMap, networkMaterialMap, AssetCategory.Networks, chassisMountedAssetNumberAndChassisIdMap);
    if (networkersNeedToSaveOrUpdate.isEmpty()) {
        logger.info("No network asset need to save");
    } else {
        restClient.saveAssets(networkersNeedToSaveOrUpdate);
        logger.info("Finish sync the networks data for: " + nlyte.getName() + ", size: " + networkersNeedToSaveOrUpdate.size());
    }
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) HashMap(java.util.HashMap) IntegrationStatus(com.vmware.flowgate.common.model.IntegrationStatus) NlyteAsset(com.vmware.flowgate.nlyteworker.model.NlyteAsset) Material(com.vmware.flowgate.nlyteworker.model.Material) ResourceAccessException(org.springframework.web.client.ResourceAccessException) NlyteAPIClient(com.vmware.flowgate.nlyteworker.restclient.NlyteAPIClient) HandleAssetUtil(com.vmware.flowgate.nlyteworker.scheduler.job.common.HandleAssetUtil) Manufacturer(com.vmware.flowgate.nlyteworker.model.Manufacturer) NlyteAsset(com.vmware.flowgate.nlyteworker.model.NlyteAsset) Asset(com.vmware.flowgate.common.model.Asset) LocationGroup(com.vmware.flowgate.nlyteworker.model.LocationGroup) ConnectException(java.net.ConnectException)

Example 3 with NlyteAPIClient

use of com.vmware.flowgate.nlyteworker.restclient.NlyteAPIClient in project flowgate by vmware.

the class NlyteDataService method syncMappedData.

private void syncMappedData(FacilitySoftwareConfig nlyte) {
    NlyteAPIClient nlyteAPIclient = new NlyteAPIClient(nlyte);
    HandleAssetUtil assetUtil = new HandleAssetUtil();
    List<NlyteAsset> nlyteAssets = null;
    try {
        nlyteAssets = nlyteAPIclient.getAssets(true, AssetCategory.Server);
    } catch (HttpClientErrorException e) {
        logger.error("Failed to query data from Nlyte", e);
        IntegrationStatus integrationStatus = nlyte.getIntegrationStatus();
        if (integrationStatus == null) {
            integrationStatus = new IntegrationStatus();
        }
        integrationStatus.setStatus(IntegrationStatus.Status.ERROR);
        integrationStatus.setDetail(e.getMessage());
        integrationStatus.setRetryCounter(FlowgateConstant.DEFAULTNUMBEROFRETRIES);
        updateIntegrationStatus(nlyte);
        return;
    } catch (ResourceAccessException e1) {
        if (e1.getCause().getCause() instanceof ConnectException) {
            checkAndUpdateIntegrationStatus(nlyte, e1.getMessage());
            return;
        }
    }
    HashMap<Integer, LocationGroup> locationMap = assetUtil.initLocationGroupMap(nlyteAPIclient);
    HashMap<Integer, Material> materialMap = assetUtil.initServerMaterialsMap(nlyteAPIclient);
    HashMap<Integer, Manufacturer> manufacturerMap = assetUtil.initManufacturersMap(nlyteAPIclient);
    saveAssetForMappedData(nlyte.getId(), nlyteAssets, locationMap, materialMap, manufacturerMap, AssetCategory.Server);
    HashMap<Integer, Material> pduMaterialMap = new HashMap<Integer, Material>();
    nlyteAssets = nlyteAPIclient.getAssets(true, AssetCategory.PDU);
    List<Material> powerStripMaterials = nlyteAPIclient.getMaterials(true, HandleAssetUtil.powerStripMaterial);
    for (Material material : powerStripMaterials) {
        material.setMaterialType(AssetCategory.PDU);
        pduMaterialMap.put(material.getMaterialID(), material);
    }
    saveAssetForMappedData(nlyte.getId(), nlyteAssets, locationMap, pduMaterialMap, manufacturerMap, AssetCategory.PDU);
    HashMap<Integer, Material> networksMaterialMap = new HashMap<Integer, Material>();
    nlyteAssets = nlyteAPIclient.getAssets(true, AssetCategory.Networks);
    List<Material> networkMaterials = nlyteAPIclient.getMaterials(true, HandleAssetUtil.networkMaterials);
    for (Material material : networkMaterials) {
        material.setMaterialType(AssetCategory.Networks);
        networksMaterialMap.put(material.getMaterialID(), material);
    }
    saveAssetForMappedData(nlyte.getId(), nlyteAssets, locationMap, networksMaterialMap, manufacturerMap, AssetCategory.Networks);
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) HashMap(java.util.HashMap) IntegrationStatus(com.vmware.flowgate.common.model.IntegrationStatus) NlyteAsset(com.vmware.flowgate.nlyteworker.model.NlyteAsset) Material(com.vmware.flowgate.nlyteworker.model.Material) ResourceAccessException(org.springframework.web.client.ResourceAccessException) NlyteAPIClient(com.vmware.flowgate.nlyteworker.restclient.NlyteAPIClient) HandleAssetUtil(com.vmware.flowgate.nlyteworker.scheduler.job.common.HandleAssetUtil) Manufacturer(com.vmware.flowgate.nlyteworker.model.Manufacturer) LocationGroup(com.vmware.flowgate.nlyteworker.model.LocationGroup) ConnectException(java.net.ConnectException)

Aggregations

NlyteAsset (com.vmware.flowgate.nlyteworker.model.NlyteAsset)3 NlyteAPIClient (com.vmware.flowgate.nlyteworker.restclient.NlyteAPIClient)3 Asset (com.vmware.flowgate.common.model.Asset)2 IntegrationStatus (com.vmware.flowgate.common.model.IntegrationStatus)2 LocationGroup (com.vmware.flowgate.nlyteworker.model.LocationGroup)2 Manufacturer (com.vmware.flowgate.nlyteworker.model.Manufacturer)2 Material (com.vmware.flowgate.nlyteworker.model.Material)2 HandleAssetUtil (com.vmware.flowgate.nlyteworker.scheduler.job.common.HandleAssetUtil)2 ConnectException (java.net.ConnectException)2 HashMap (java.util.HashMap)2 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)2 ResourceAccessException (org.springframework.web.client.ResourceAccessException)2 SDDCSoftwareConfig (com.vmware.flowgate.common.model.SDDCSoftwareConfig)1 ServerMapping (com.vmware.flowgate.common.model.ServerMapping)1 ArrayList (java.util.ArrayList)1