Search in sources :

Example 1 with Chassis

use of com.vmware.flowgate.openmanage.datamodel.Chassis 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 2 with Chassis

use of com.vmware.flowgate.openmanage.datamodel.Chassis in project flowgate by vmware.

the class OpenManageJobTest method executeJobTest.

@Test
public void executeJobTest() {
    FacilitySoftwareConfig config = new FacilitySoftwareConfig();
    config.setId("executeJob");
    IntegrationStatus integrationStatus = new IntegrationStatus();
    integrationStatus.setStatus(IntegrationStatus.Status.ACTIVE);
    doNothing().when(this.openManageAPIClient).getToken();
    doNothing().when(this.wormholeAPIClient).setServiceKey(any(String.class));
    Mockito.when(this.openmanageJobService.createClient(config)).thenReturn(this.openManageAPIClient);
    Mockito.when(this.wormholeAPIClient.saveAssets(any(Asset.class))).thenReturn(saveAsset());
    Mockito.when(this.wormholeAPIClient.saveAssets(ArgumentMatchers.anyList())).thenReturn(null);
    Mockito.when(this.wormholeAPIClient.getAllAssetsBySourceAndType(config.getId(), AssetCategory.Server)).thenReturn(getAllAssetsBySourceAndType());
    Mockito.when(this.wormholeAPIClient.getAllAssetsBySourceAndType(config.getId(), AssetCategory.Chassis)).thenReturn(new ArrayList<Asset>());
    Mockito.when(this.openManageAPIClient.getDevices(any(Integer.class), any(Integer.class), ArgumentMatchers.eq(Server.class))).thenReturn(getCommonResultServer());
    Mockito.when(this.openManageAPIClient.getDevices(any(Integer.class), any(Integer.class), ArgumentMatchers.eq(Chassis.class))).thenReturn(new CommonResult<Chassis>());
    openmanageJobService.executeJob(EventMessageUtil.OpenManage_SyncAssetsMetaData, config);
}
Also used : Chassis(com.vmware.flowgate.openmanage.datamodel.Chassis) Server(com.vmware.flowgate.openmanage.datamodel.Server) IntegrationStatus(com.vmware.flowgate.common.model.IntegrationStatus) FacilitySoftwareConfig(com.vmware.flowgate.common.model.FacilitySoftwareConfig) Asset(com.vmware.flowgate.common.model.Asset) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 3 with Chassis

use of com.vmware.flowgate.openmanage.datamodel.Chassis in project flowgate by vmware.

the class OpenManageJobService method handleChassisAssets.

public List<Asset> handleChassisAssets(CommonResult<Chassis> chassisResult, Map<Long, Asset> assetNumberMap, String integrationId) {
    List<Chassis> chassisList = chassisResult.getValue();
    List<Asset> chassiAssetsToSave = new ArrayList<Asset>();
    for (Chassis chassis : chassisList) {
        // filter by device status
        if (chassis.getStatus() == DeviceStatus.UNKNOWN.getValue() || chassis.getStatus() == DeviceStatus.NOSTATUS.getValue()) {
            continue;
        }
        Asset needToSaveChassisAsset = null;
        if (assetNumberMap.containsKey(chassis.getId())) {
            needToSaveChassisAsset = assetNumberMap.get(chassis.getId());
            if (deviceValueChanged(needToSaveChassisAsset, chassis)) {
                chassiAssetsToSave.add(needToSaveChassisAsset);
            }
        } else {
            // new chassis asset
            needToSaveChassisAsset = translateToAsset(chassis);
            needToSaveChassisAsset.setCategory(AssetCategory.Chassis);
            needToSaveChassisAsset.setAssetSource(integrationId);
            chassiAssetsToSave.add(needToSaveChassisAsset);
        }
    }
    return chassiAssetsToSave;
}
Also used : Chassis(com.vmware.flowgate.openmanage.datamodel.Chassis) ArrayList(java.util.ArrayList) Asset(com.vmware.flowgate.common.model.Asset)

Example 4 with Chassis

use of com.vmware.flowgate.openmanage.datamodel.Chassis in project flowgate by vmware.

the class OpenManageJobTest method handleChassiAssetsTest.

@Test
public void handleChassiAssetsTest() {
    CommonResult<Chassis> serversResult = new CommonResult<Chassis>();
    List<Chassis> chassisList = new ArrayList<Chassis>();
    Chassis chassis1 = new Chassis();
    chassis1.setAssetTag("chassis1");
    chassis1.setModel("los23");
    chassis1.setId(2583l);
    chassis1.setDeviceName("mychassis1");
    chassis1.setPowerState(PowerState.ON.getValue());
    chassis1.setStatus(DeviceStatus.NORMAL.getValue());
    chassisList.add(chassis1);
    Chassis chassis2 = new Chassis();
    chassis2.setAssetTag("chassis2");
    chassis2.setModel("sr4d");
    chassis2.setId(2523l);
    chassis2.setDeviceName("mychassis2");
    chassis2.setPowerState(PowerState.ON.getValue());
    chassis2.setStatus(DeviceStatus.NORMAL.getValue());
    chassisList.add(chassis2);
    Chassis chassis3 = new Chassis();
    chassis3.setAssetTag("chassis3");
    chassis3.setModel("chassis");
    chassis3.setId(2522l);
    chassis3.setDeviceName("mychassis3");
    chassis3.setPowerState(PowerState.ON.getValue());
    chassis3.setStatus(DeviceStatus.UNKNOWN.getValue());
    chassisList.add(chassis3);
    serversResult.setValue(chassisList);
    Asset asset = new Asset();
    asset.setCategory(AssetCategory.Chassis);
    asset.setAssetName("mychassis2");
    asset.setModel("sr4d");
    asset.setManufacturer("Dell");
    asset.setTag("0121");
    Map<String, String> openManageMap = new HashMap<String, String>();
    openManageMap.put(FlowgateConstant.ASSETNUMBER, String.valueOf(chassis2.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.handleChassisAssets(serversResult, assetNumberMap, source);
    TestCase.assertEquals(chassisList.size() - 1, assets.size());
    for (Asset chassisAsset : assets) {
        if (chassis1.getDeviceName().equals(chassisAsset.getAssetName())) {
            TestCase.assertEquals(chassis1.getAssetTag(), chassisAsset.getTag());
            TestCase.assertEquals(chassis1.getModel(), chassisAsset.getModel());
            HashMap<String, String> valueMap = chassisAsset.getJustificationfields();
            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(chassis1.getId(), Long.valueOf(openManageDeviceId).longValue());
            } catch (IOException ioException) {
                TestCase.fail(ioException.getMessage());
            }
        } else if (chassis2.getDeviceName().equals(chassisAsset.getAssetName())) {
            TestCase.assertEquals(chassis2.getAssetTag(), chassisAsset.getTag());
        } else {
            TestCase.fail("Invalid asset");
        }
    }
}
Also used : Chassis(com.vmware.flowgate.openmanage.datamodel.Chassis) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) 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)4 Chassis (com.vmware.flowgate.openmanage.datamodel.Chassis)4 ArrayList (java.util.ArrayList)3 Server (com.vmware.flowgate.openmanage.datamodel.Server)2 Test (org.junit.Test)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 FacilitySoftwareConfig (com.vmware.flowgate.common.model.FacilitySoftwareConfig)1 IntegrationStatus (com.vmware.flowgate.common.model.IntegrationStatus)1 OpenManageAPIClient (com.vmware.flowgate.openmanage.client.OpenManageAPIClient)1 CommonResult (com.vmware.flowgate.openmanage.datamodel.CommonResult)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1