Search in sources :

Example 1 with NlyteAsset

use of com.vmware.flowgate.nlyteworker.model.NlyteAsset 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 NlyteAsset

use of com.vmware.flowgate.nlyteworker.model.NlyteAsset in project flowgate by vmware.

the class HandleAssetUtil method getAssetsFromNlyte.

/**
 * @param nlyteAssets
 * @return
 */
public List<Asset> getAssetsFromNlyte(String nlyteSource, List<NlyteAsset> nlyteAssets, HashMap<Integer, LocationGroup> locationMap, HashMap<Integer, Material> materialMap, HashMap<Integer, Manufacturer> manufacturerMap, HashMap<Long, String> chassisMountedAssetNumberAndChassisIdMap) {
    List<Asset> assetsFromNlyte = new ArrayList<Asset>();
    Asset asset;
    for (NlyteAsset nlyteAsset : nlyteAssets) {
        asset = new Asset();
        asset.setAssetNumber(nlyteAsset.getAssetNumber());
        asset.setCabinetAssetNumber(String.valueOf(nlyteAsset.getCabinetAssetID()));
        asset.setCabinetName(nlyteAsset.getCabinetName());
        asset.setTag(nlyteAsset.getTag());
        asset.setSerialnumber(nlyteAsset.getSerialNumber());
        asset.setAssetName(nlyteAsset.getAssetName());
        asset.setRow(nlyteAsset.getGridReferenceRow());
        asset.setCol(nlyteAsset.getGridReferenceColumn());
        asset = supplementLocation(asset, nlyteAsset.getLocationGroupID(), locationMap);
        asset = supplementMaterial(asset, nlyteAsset.getMaterialID(), manufacturerMap, materialMap);
        // we need to refactor the code
        if (asset.getCategory() == null) {
            continue;
        }
        if (asset.getCategory() == AssetCategory.Server) {
            if (chassisMountedAssetNumberAndChassisIdMap != null && chassisMountedAssetNumberAndChassisIdMap.containsKey(asset.getAssetNumber())) {
                Parent parent = new Parent();
                parent.setType(AssetCategory.Chassis.name());
                parent.setParentId(chassisMountedAssetNumberAndChassisIdMap.get(asset.getAssetNumber()));
                asset.setParent(parent);
            }
            // set the tenant information.
            List<CustomField> fields = nlyteAsset.getCustomFields();
            if (fields != null) {
                Tenant tenant = new Tenant();
                for (CustomField cf : fields) {
                    if (cf.getDataLabel().equals(CustomField.Owner)) {
                        String dataValue = cf.getDataValueString();
                        if (!StringUtils.isEmpty(dataValue)) {
                            tenant.setOwner(dataValue);
                            tenant.setTenant(dataValue);
                        }
                    } else if (cf.getDataLabel().equals(CustomField.Tenant_EndUser)) {
                        String dataValue = cf.getDataValueString();
                        if (!StringUtils.isEmpty(dataValue)) {
                            tenant.setTenant(dataValue);
                        }
                    } else if (cf.getDataLabel().equals(CustomField.HaaS_RequestedBy)) {
                        String dataValue = cf.getDataValueString();
                        if (!StringUtils.isEmpty(dataValue)) {
                            tenant.setTenant(dataValue);
                        }
                    } else if (cf.getDataLabel().equals(CustomField.Tenant_Manager)) {
                        String dataValue = cf.getDataValueString();
                        if (!StringUtils.isEmpty(dataValue)) {
                            tenant.setTenantManager(dataValue);
                        }
                    }
                }
                asset.setTenant(tenant);
            }
        }
        if (asset.getCategory().equals(AssetCategory.Cabinet)) {
            String contiguousUSpace = nlyteAsset.getContiguousUSpace();
            if (contiguousUSpace != null) {
                int freeSize = 0;
                String[] contiguousUSpaces = contiguousUSpace.split(FlowgateConstant.SPILIT_FLAG);
                for (String uSpace : contiguousUSpaces) {
                    freeSize += Integer.parseInt(uSpace);
                }
                asset.setFreeCapacity(freeSize);
            }
            List<CabinetU> cabinetus = nlyteAsset.getCabinetUs();
            if (cabinetus != null && !cabinetus.isEmpty()) {
                List<com.vmware.flowgate.common.model.CabinetU> flowgateCabinetUs = new ArrayList<com.vmware.flowgate.common.model.CabinetU>();
                for (CabinetU cabinetu : cabinetus) {
                    com.vmware.flowgate.common.model.CabinetU flowgateCabinetU = new com.vmware.flowgate.common.model.CabinetU();
                    flowgateCabinetU.setAssetsOnUnit(cabinetu.getAssetsOnU());
                    flowgateCabinetU.setCabinetUNumber(cabinetu.getCabinetUNumber());
                    if (CabinetU_State_Full.equals(cabinetu.getCabinetUState())) {
                        flowgateCabinetU.setUsed(true);
                    } else if (CabinetU_State_Free.equals(cabinetu.getCabinetUState())) {
                        flowgateCabinetU.setUsed(false);
                    }
                    flowgateCabinetUs.add(flowgateCabinetU);
                }
                try {
                    String cabinetUsInfo = mapper.writeValueAsString(flowgateCabinetUs);
                    HashMap<String, String> justficationfields = new HashMap<String, String>();
                    justficationfields.put(FlowgateConstant.CABINETUNITS, cabinetUsInfo);
                    asset.setJustificationfields(justficationfields);
                } catch (JsonProcessingException e) {
                    logger.error("Failed to get info of cabinetUs.");
                }
            }
        }
        if (asset.getCategory().equals(AssetCategory.Chassis)) {
            handleChassisSolts(asset, nlyteAsset);
        }
        if (asset.getCategory().equals(AssetCategory.Networks)) {
            if (chassisMountedAssetNumberAndChassisIdMap != null && chassisMountedAssetNumberAndChassisIdMap.containsKey(asset.getAssetNumber())) {
                Parent parent = new Parent();
                parent.setType(AssetCategory.Chassis.name());
                parent.setParentId(chassisMountedAssetNumberAndChassisIdMap.get(asset.getAssetNumber()));
                asset.setParent(parent);
            }
        }
        asset.setAssetSource(nlyteSource);
        AssetStatus status = new AssetStatus();
        status.setNetworkMapping(NetworkMapping.UNMAPPED);
        status.setPduMapping(PduMapping.UNMAPPED);
        asset.setStatus(status);
        UMounting uMounting = nlyteAsset.getuMounting();
        if (uMounting != null) {
            asset.setCabinetUnitPosition(uMounting.getCabinetUNumber());
            switch(MountingSide.valueOf(uMounting.getMountingSide())) {
                case Front:
                    asset.setMountingSide(MountingSide.Front);
                    break;
                case Back:
                    asset.setMountingSide(MountingSide.Back);
                    break;
                case Unmounted:
                    asset.setMountingSide(MountingSide.Unmounted);
                    break;
                default:
                    break;
            }
        }
        asset.setCreated(System.currentTimeMillis());
        assetsFromNlyte.add(asset);
    }
    return assetsFromNlyte;
}
Also used : Parent(com.vmware.flowgate.common.model.Parent) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AssetStatus(com.vmware.flowgate.common.AssetStatus) Tenant(com.vmware.flowgate.common.model.Tenant) NlyteAsset(com.vmware.flowgate.nlyteworker.model.NlyteAsset) Asset(com.vmware.flowgate.common.model.Asset) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) NlyteAsset(com.vmware.flowgate.nlyteworker.model.NlyteAsset) UMounting(com.vmware.flowgate.nlyteworker.model.UMounting) CustomField(com.vmware.flowgate.nlyteworker.model.CustomField) CabinetU(com.vmware.flowgate.nlyteworker.model.CabinetU)

Example 3 with NlyteAsset

use of com.vmware.flowgate.nlyteworker.model.NlyteAsset in project flowgate by vmware.

the class SycnRealTimeDataJobTest method testHandleChassisSolts3.

@Test
public void testHandleChassisSolts3() {
    Asset asset = createAsset();
    List<ChassisMountedAssetMap> chassisMaps = new ArrayList<ChassisMountedAssetMap>();
    ChassisMountedAssetMap cMap1 = new ChassisMountedAssetMap();
    cMap1.setMountedAssetID(105);
    cMap1.setColumnPosition(1);
    cMap1.setRowPosition(1);
    cMap1.setMountingSide("Front");
    cMap1.setSlotName("1");
    chassisMaps.add(cMap1);
    ChassisMountedAssetMap cMap2 = new ChassisMountedAssetMap();
    cMap2.setMountedAssetID(198);
    cMap2.setColumnPosition(1);
    cMap2.setRowPosition(2);
    cMap2.setMountingSide("Back");
    cMap2.setSlotName("2");
    chassisMaps.add(cMap2);
    NlyteAsset nlyteAsset = getNlyteAsset().get(0);
    nlyteAsset.setChassisMountedAssetMaps(chassisMaps);
    HandleAssetUtil util = new HandleAssetUtil();
    util.handleChassisSolts(asset, nlyteAsset);
    String chassisInfo = asset.getJustificationfields().get(FlowgateConstant.CHASSIS);
    ObjectMapper mapper = new ObjectMapper();
    try {
        Map<String, String> chassisInfoMap = mapper.readValue(chassisInfo, new TypeReference<Map<String, String>>() {
        });
        List<FlowgateChassisSlot> slots = mapper.readValue(chassisInfoMap.get(FlowgateConstant.CHASSISSLOTS), new TypeReference<List<FlowgateChassisSlot>>() {
        });
        for (FlowgateChassisSlot chassisslot : slots) {
            if (chassisslot.getSlotName().equals("1")) {
                TestCase.assertEquals("Front", chassisslot.getMountingSide());
                TestCase.assertEquals(Integer.valueOf(105), chassisslot.getMountedAssetNumber());
            } else if (chassisslot.getSlotName().equals("2")) {
                TestCase.assertEquals("Back", chassisslot.getMountingSide());
                TestCase.assertEquals(Integer.valueOf(198), chassisslot.getMountedAssetNumber());
            } else {
                TestCase.fail();
            }
        }
    } catch (JsonProcessingException e) {
        TestCase.fail(e.getMessage());
    }
}
Also used : NlyteAsset(com.vmware.flowgate.nlyteworker.model.NlyteAsset) ArrayList(java.util.ArrayList) ChassisMountedAssetMap(com.vmware.flowgate.nlyteworker.model.ChassisMountedAssetMap) FlowgateChassisSlot(com.vmware.flowgate.common.model.FlowgateChassisSlot) HandleAssetUtil(com.vmware.flowgate.nlyteworker.scheduler.job.common.HandleAssetUtil) NlyteAsset(com.vmware.flowgate.nlyteworker.model.NlyteAsset) Asset(com.vmware.flowgate.common.model.Asset) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) ChassisMountedAssetMap(com.vmware.flowgate.nlyteworker.model.ChassisMountedAssetMap) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 4 with NlyteAsset

use of com.vmware.flowgate.nlyteworker.model.NlyteAsset in project flowgate by vmware.

the class SycnRealTimeDataJobTest method testHandleChassisSolts1.

@Test
public void testHandleChassisSolts1() {
    Asset asset = createAsset();
    List<ChassisSlot> chassisSolts = new ArrayList<ChassisSlot>();
    ChassisSlot slot = new ChassisSlot();
    slot.setChassisAssetID(105);
    slot.setColumnPosition(1);
    slot.setRowPosition(1);
    slot.setMountingSide("Front");
    slot.setSlotName("1");
    slot.setId(78);
    chassisSolts.add(slot);
    ChassisSlot slot2 = new ChassisSlot();
    slot2.setChassisAssetID(105);
    slot2.setColumnPosition(1);
    slot2.setRowPosition(2);
    slot2.setMountingSide("Back");
    slot2.setSlotName("2");
    slot2.setId(79);
    chassisSolts.add(slot2);
    NlyteAsset nlyteAsset = getNlyteAsset().get(0);
    nlyteAsset.setChassisSlots(chassisSolts);
    HandleAssetUtil util = new HandleAssetUtil();
    util.handleChassisSolts(asset, nlyteAsset);
    String chassisInfo = asset.getJustificationfields().get(FlowgateConstant.CHASSIS);
    ObjectMapper mapper = new ObjectMapper();
    try {
        Map<String, String> chassisInfoMap = mapper.readValue(chassisInfo, new TypeReference<Map<String, String>>() {
        });
        List<FlowgateChassisSlot> slots = mapper.readValue(chassisInfoMap.get(FlowgateConstant.CHASSISSLOTS), new TypeReference<List<FlowgateChassisSlot>>() {
        });
        for (FlowgateChassisSlot chassisslot : slots) {
            if (chassisslot.getSlotName().equals("1")) {
                TestCase.assertEquals("Front", chassisslot.getMountingSide());
            } else if (chassisslot.getSlotName().equals("2")) {
                TestCase.assertEquals("Back", chassisslot.getMountingSide());
            } else {
                TestCase.fail();
            }
        }
    } catch (JsonProcessingException e) {
        TestCase.fail(e.getMessage());
    }
}
Also used : NlyteAsset(com.vmware.flowgate.nlyteworker.model.NlyteAsset) ArrayList(java.util.ArrayList) FlowgateChassisSlot(com.vmware.flowgate.common.model.FlowgateChassisSlot) HandleAssetUtil(com.vmware.flowgate.nlyteworker.scheduler.job.common.HandleAssetUtil) NlyteAsset(com.vmware.flowgate.nlyteworker.model.NlyteAsset) Asset(com.vmware.flowgate.common.model.Asset) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) ChassisMountedAssetMap(com.vmware.flowgate.nlyteworker.model.ChassisMountedAssetMap) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FlowgateChassisSlot(com.vmware.flowgate.common.model.FlowgateChassisSlot) ChassisSlot(com.vmware.flowgate.nlyteworker.model.ChassisSlot) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 5 with NlyteAsset

use of com.vmware.flowgate.nlyteworker.model.NlyteAsset in project flowgate by vmware.

the class SycnRealTimeDataJobTest method testGenerateNewAsset2.

@Test
public void testGenerateNewAsset2() {
    List<NlyteAsset> nlyteAssets = getNlyteAsset();
    HashMap<Integer, LocationGroup> locationMap = getLocationMap();
    HashMap<Integer, Material> materialMap = new HashMap<Integer, Material>();
    Material material = new Material();
    material.setMaterialID(6251);
    material.setManufacturerID(14);
    material.setMaterialName("Cisco 1721 Modular Access Router");
    material.setMaterialType(AssetCategory.Networks);
    materialMap.put(6251, material);
    HashMap<Integer, Manufacturer> manufacturerMap = new HashMap<Integer, Manufacturer>();
    Manufacturer manufacturer = new Manufacturer();
    manufacturer.setManufacturerID(14);
    manufacturer.setDetail("Cisco");
    manufacturerMap.put(14, manufacturer);
    Mockito.when(this.wormholeAPIClient.getAllAssetsBySourceAndType("l9i8728d55368540fcba1692", AssetCategory.Server)).thenReturn(new ArrayList<Asset>());
    HashMap<Long, String> chassisMountedAssetNumberAndChassisIDMap = new HashMap<Long, String>();
    chassisMountedAssetNumberAndChassisIDMap.put(197L, "asdqe945kjsdf09uw45ms");
    List<Asset> assets = nlyteDataService.generateAssets("l9i8728d55368540fcba1692", nlyteAssets, locationMap, manufacturerMap, materialMap, AssetCategory.Networks, chassisMountedAssetNumberAndChassisIDMap);
    for (Asset asset : assets) {
        if ("sin2-blrqeops-esxstress024".equals(asset.getAssetName())) {
            TestCase.assertEquals(197, asset.getAssetNumber());
            TestCase.assertEquals("SG-07-04", asset.getRoom());
            TestCase.assertEquals("Cisco 1721 Modular Access Router", asset.getModel());
            TestCase.assertEquals("Cisco", asset.getManufacturer());
            TestCase.assertEquals("asdqe945kjsdf09uw45ms", asset.getParent().getParentId());
        }
    }
}
Also used : HashMap(java.util.HashMap) NlyteAsset(com.vmware.flowgate.nlyteworker.model.NlyteAsset) Material(com.vmware.flowgate.nlyteworker.model.Material) 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) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Aggregations

NlyteAsset (com.vmware.flowgate.nlyteworker.model.NlyteAsset)17 Asset (com.vmware.flowgate.common.model.Asset)13 HandleAssetUtil (com.vmware.flowgate.nlyteworker.scheduler.job.common.HandleAssetUtil)10 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)9 Test (org.junit.Test)8 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)8 LocationGroup (com.vmware.flowgate.nlyteworker.model.LocationGroup)6 Manufacturer (com.vmware.flowgate.nlyteworker.model.Manufacturer)6 Material (com.vmware.flowgate.nlyteworker.model.Material)6 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4 FlowgateChassisSlot (com.vmware.flowgate.common.model.FlowgateChassisSlot)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 ChassisMountedAssetMap (com.vmware.flowgate.nlyteworker.model.ChassisMountedAssetMap)3 ChassisSlot (com.vmware.flowgate.nlyteworker.model.ChassisSlot)3 NlyteAPIClient (com.vmware.flowgate.nlyteworker.restclient.NlyteAPIClient)3 List (java.util.List)3 Map (java.util.Map)3 ArgumentMatchers.anyList (org.mockito.ArgumentMatchers.anyList)3 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)3