Search in sources :

Example 31 with Asset

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

the class OpenManageJobService method syncMetadataJob.

private void syncMetadataJob(FacilitySoftwareConfig integration) {
    wormholeApiClient.setServiceKey(serviceKeyConfig.getServiceKey());
    // check the status of integration
    try (OpenManageAPIClient client = createClient(integration)) {
        checkConnection(client, integration);
        // sync server
        List<Asset> oldServers = wormholeApiClient.getAllAssetsBySourceAndType(integration.getId(), AssetCategory.Server);
        Map<Long, Asset> assetNumberMap = generateAssetNumberMap(oldServers);
        int skip = defaultSkip;
        while (true) {
            CommonResult<Server> serversResult = client.getDevices(skip, defaultPageSize, Server.class);
            int totalCount = serversResult.getCount();
            if (totalCount == defaultSkip) {
                logger.info("Not found server from : {}.", integration.getName());
                break;
            }
            if (!serversResult.getValue().isEmpty()) {
                List<Asset> serverAssetsToSave = handleServerAssets(serversResult, assetNumberMap, integration.getId());
                if (!serverAssetsToSave.isEmpty()) {
                    List<Asset> openmanageServerToUpdate = new ArrayList<Asset>();
                    for (Asset asset : serverAssetsToSave) {
                        ResponseEntity<Void> res = wormholeApiClient.saveAssets(asset);
                        if (res.getStatusCode().is2xxSuccessful()) {
                            String uriPath = res.getHeaders().getLocation().getPath();
                            String id = uriPath.substring(uriPath.lastIndexOf("/") + 1);
                            asset.setId(id);
                            createMetricFormulas(asset);
                            openmanageServerToUpdate.add(asset);
                        }
                    }
                    // update asset about metricsFormula
                    wormholeApiClient.saveAssets(openmanageServerToUpdate);
                }
            }
            skip += defaultPageSize;
            if (skip > totalCount) {
                break;
            }
        }
        // sync chassis
        List<Asset> oldChassis = wormholeApiClient.getAllAssetsBySourceAndType(integration.getId(), AssetCategory.Chassis);
        Map<Long, Asset> chassisAssetNumberMap = generateAssetNumberMap(oldChassis);
        int chassiItemSkip = defaultSkip;
        while (true) {
            CommonResult<Chassis> chassisResult = client.getDevices(chassiItemSkip, defaultPageSize, Chassis.class);
            int totalCount = chassisResult.getCount();
            if (totalCount == defaultSkip) {
                logger.info("Not found chassis from : {}.", integration.getName());
                break;
            }
            if (!chassisResult.getValue().isEmpty()) {
                List<Asset> chassiAssetsToSave = handleChassisAssets(chassisResult, chassisAssetNumberMap, integration.getId());
                if (!chassiAssetsToSave.isEmpty()) {
                    wormholeApiClient.saveAssets(chassiAssetsToSave);
                }
            }
            chassiItemSkip += defaultPageSize;
            if (chassiItemSkip > totalCount) {
                break;
            }
        }
    }
}
Also used : Chassis(com.vmware.flowgate.openmanage.datamodel.Chassis) Server(com.vmware.flowgate.openmanage.datamodel.Server) ArrayList(java.util.ArrayList) OpenManageAPIClient(com.vmware.flowgate.openmanage.client.OpenManageAPIClient) Asset(com.vmware.flowgate.common.model.Asset)

Example 32 with Asset

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

the class OpenManageJobService method generateAssetNumberMap.

private Map<Long, Asset> generateAssetNumberMap(List<Asset> assets) {
    Map<Long, Asset> assetNumberMap = new HashMap<Long, Asset>();
    for (Asset asset : assets) {
        HashMap<String, String> justficationfileds = asset.getJustificationfields();
        if (justficationfileds != null && justficationfileds.containsKey(FlowgateConstant.OPENMANAGE)) {
            String openManageInfo = justficationfileds.get(FlowgateConstant.OPENMANAGE);
            try {
                Map<String, String> infoMap = getInfoMap(openManageInfo);
                String openManageDeviceId = infoMap.get(FlowgateConstant.ASSETNUMBER);
                assetNumberMap.put(Long.valueOf(openManageDeviceId), asset);
            } catch (IOException ioException) {
                logger.error("Deserializing device info error", ioException);
                continue;
            }
        }
    }
    return assetNumberMap;
}
Also used : HashMap(java.util.HashMap) Asset(com.vmware.flowgate.common.model.Asset) IOException(java.io.IOException)

Example 33 with Asset

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

the class OpenManageJobService method handleServerAssets.

public List<Asset> handleServerAssets(CommonResult<Server> serversResult, Map<Long, Asset> assetNumberMap, String integrationId) {
    List<Server> servers = serversResult.getValue();
    List<Asset> serverAssetsToSave = new ArrayList<Asset>();
    for (Server server : servers) {
        // filter by device status
        if (server.getStatus() == DeviceStatus.UNKNOWN.getValue() || server.getStatus() == DeviceStatus.NOSTATUS.getValue()) {
            continue;
        }
        Asset needToSaveServerAsset = null;
        if (assetNumberMap.containsKey(server.getId())) {
            needToSaveServerAsset = assetNumberMap.get(server.getId());
            if (deviceValueChanged(needToSaveServerAsset, server)) {
                serverAssetsToSave.add(needToSaveServerAsset);
            }
        } else {
            // new server asset
            needToSaveServerAsset = translateToAsset(server);
            needToSaveServerAsset.setCategory(AssetCategory.Server);
            needToSaveServerAsset.setAssetSource(integrationId);
            if (server.getDeviceSpecificData() != null) {
                ServerSpecificData data = server.getDeviceSpecificData();
                needToSaveServerAsset.setManufacturer(data.getManufacturer());
            }
            serverAssetsToSave.add(needToSaveServerAsset);
        }
    }
    return serverAssetsToSave;
}
Also used : Server(com.vmware.flowgate.openmanage.datamodel.Server) ArrayList(java.util.ArrayList) Asset(com.vmware.flowgate.common.model.Asset) ServerSpecificData(com.vmware.flowgate.openmanage.datamodel.ServerSpecificData)

Example 34 with Asset

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

the class OpenManageJobTest method createAsset.

Asset createAsset() {
    Asset asset = new Asset();
    asset.setAssetName("pek-wor-server-02");
    asset.setAssetNumber(12345);
    asset.setAssetSource("l9i8728d55368540fcba1692");
    asset.setCategory(AssetCategory.Server);
    asset.setModel("Dell 750");
    asset.setManufacturer("Dell");
    return asset;
}
Also used : Asset(com.vmware.flowgate.common.model.Asset)

Example 35 with Asset

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

the class OpenManageJobTest method handleServerAssetsTest.

@Test
public void handleServerAssetsTest() {
    CommonResult<Server> serversResult = new CommonResult<Server>();
    List<Server> servers = new ArrayList<Server>();
    Server server1 = new Server();
    server1.setAssetTag("lkw456");
    server1.setModel("los23");
    server1.setId(258l);
    server1.setDeviceName("myserver1");
    server1.setPowerState(PowerState.ON.getValue());
    server1.setStatus(DeviceStatus.NORMAL.getValue());
    ServerSpecificData specificData = new ServerSpecificData();
    specificData.setManufacturer("Dell");
    server1.setDeviceSpecificData(specificData);
    servers.add(server1);
    Server server2 = new Server();
    server2.setAssetTag("asdqw2");
    server2.setModel("sr4d");
    server2.setId(252l);
    server2.setDeviceName("myserver2");
    server2.setPowerState(PowerState.OFF.getValue());
    server2.setStatus(DeviceStatus.NORMAL.getValue());
    ServerSpecificData specificData2 = new ServerSpecificData();
    specificData2.setManufacturer("Dell");
    server2.setDeviceSpecificData(specificData2);
    servers.add(server2);
    serversResult.setValue(servers);
    Asset asset = new Asset();
    asset.setCategory(AssetCategory.Server);
    asset.setAssetName("myserver2");
    asset.setModel("sr4d");
    asset.setManufacturer("Dell");
    asset.setTag("0121");
    Map<String, String> openManageMap = new HashMap<String, String>();
    openManageMap.put(FlowgateConstant.ASSETNUMBER, String.valueOf(server2.getId()));
    HashMap<String, String> justficationfileds = new HashMap<String, String>();
    try {
        String openManageInfo = mapper.writeValueAsString(openManageMap);
        justficationfileds.put(FlowgateConstant.OPENMANAGE, openManageInfo);
        asset.setJustificationfields(justficationfileds);
    } catch (JsonProcessingException e) {
        TestCase.fail(e.getMessage());
    }
    Map<Long, Asset> assetNumberMap = new HashMap<Long, Asset>();
    assetNumberMap.put(252l, asset);
    String source = "integrationId";
    List<Asset> assets = openmanageJobService.handleServerAssets(serversResult, assetNumberMap, source);
    TestCase.assertEquals(servers.size(), assets.size());
    for (Asset serverAsset : assets) {
        HashMap<String, String> valueMap = serverAsset.getJustificationfields();
        if (server1.getDeviceName().equals(serverAsset.getAssetName())) {
            TestCase.assertEquals(server1.getAssetTag(), serverAsset.getTag());
            TestCase.assertEquals(server1.getModel(), serverAsset.getModel());
            TestCase.assertEquals(server1.getDeviceSpecificData().getManufacturer(), serverAsset.getManufacturer());
            if (valueMap.isEmpty() || !valueMap.containsKey(FlowgateConstant.OPENMANAGE)) {
                TestCase.fail("Asset number is required");
            }
            String openManageInfo = valueMap.get(FlowgateConstant.OPENMANAGE);
            try {
                Map<String, String> infoMap = mapper.readValue(openManageInfo, new TypeReference<Map<String, String>>() {
                });
                ;
                String openManageDeviceId = infoMap.get(FlowgateConstant.ASSETNUMBER);
                TestCase.assertEquals(server1.getId(), Long.valueOf(openManageDeviceId).longValue());
            } catch (IOException ioException) {
                TestCase.fail(ioException.getMessage());
            }
        } else if (server2.getDeviceName().equals(serverAsset.getAssetName())) {
            TestCase.assertEquals(server2.getAssetTag(), serverAsset.getTag());
            TestCase.assertEquals(FlowgatePowerState.OFFHARD.name(), valueMap.get(FlowgateConstant.POWERSTATE));
        } else {
            TestCase.fail("Invalid asset");
        }
    }
}
Also used : Server(com.vmware.flowgate.openmanage.datamodel.Server) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ServerSpecificData(com.vmware.flowgate.openmanage.datamodel.ServerSpecificData) Asset(com.vmware.flowgate.common.model.Asset) CommonResult(com.vmware.flowgate.openmanage.datamodel.CommonResult) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Map(java.util.Map) HashMap(java.util.HashMap) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) 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