Search in sources :

Example 11 with SDDCSoftwareConfig

use of com.vmware.flowgate.common.model.SDDCSoftwareConfig 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 12 with SDDCSoftwareConfig

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

the class VCDataService method executeAsync.

@Override
@Async("asyncServiceExecutor")
public void executeAsync(EventMessage message) {
    // update the value.
    if (message.getType() != EventType.VCenter) {
        logger.warn("Drop none vcenter message " + message.getType());
        return;
    }
    // TO, this should be comment out since it may contain vc password.
    logger.info("message received");
    Set<EventUser> users = message.getTarget().getUsers();
    for (EventUser command : users) {
        logger.info(command.getId());
        switch(command.getId()) {
            case EventMessageUtil.VCENTER_SyncData:
                // it will sync all the data depend on the type in the vcjoblist.
                String messageString = null;
                while ((messageString = template.opsForList().rightPop(EventMessageUtil.vcJobList)) != null) {
                    EventMessage payloadMessage = null;
                    try {
                        payloadMessage = mapper.readValue(messageString, EventMessageImpl.class);
                    } catch (IOException ioException) {
                        logger.error("Cannot process message", ioException);
                    }
                    if (payloadMessage == null) {
                        continue;
                    }
                    SDDCSoftwareConfig vcInfo = null;
                    try {
                        vcInfo = mapper.readValue(payloadMessage.getContent(), SDDCSoftwareConfig.class);
                    } catch (IOException ioException) {
                        logger.error("Cannot process message", ioException);
                    }
                    if (null == vcInfo) {
                        continue;
                    }
                    for (EventUser payloadCommand : payloadMessage.getTarget().getUsers()) {
                        switch(payloadCommand.getId()) {
                            case EventMessageUtil.VCENTER_SyncCustomerAttrs:
                                syncCustomAttributes(vcInfo);
                                logger.info("Finish sync customer attributes for " + vcInfo.getName());
                                break;
                            case EventMessageUtil.VCENTER_SyncCustomerAttrsData:
                                syncCustomerAttrsData(vcInfo);
                                logger.info("Finish sync data for " + vcInfo.getName());
                                break;
                            case EventMessageUtil.VCENTER_QueryHostMetaData:
                                queryHostMetaData(vcInfo);
                                logger.info("Finish query host metadata for " + vcInfo.getName());
                                break;
                            case EventMessageUtil.VCENTER_QueryHostUsageData:
                                queryHostMetrics(vcInfo);
                                logger.info("Finish query host usage for " + vcInfo.getName());
                                break;
                            default:
                                break;
                        }
                    }
                }
                break;
            case EventMessageUtil.VCENTER_SyncCustomerAttrs:
                logger.warn("VCENTER_SyncCustomerAttrs command is depreacted. use VCENTER_SyncData instead");
                break;
            case EventMessageUtil.VCENTER_SyncCustomerAttrsData:
                logger.warn("VCENTER_SyncCustomerAttrsData command is depreacted. use VCENTER_SyncData instead");
                break;
            default:
                logger.warn("Not supported command");
                break;
        }
    }
}
Also used : SDDCSoftwareConfig(com.vmware.flowgate.common.model.SDDCSoftwareConfig) EventMessage(com.vmware.flowgate.common.model.redis.message.EventMessage) EventUser(com.vmware.flowgate.common.model.redis.message.EventUser) EventMessageImpl(com.vmware.flowgate.common.model.redis.message.impl.EventMessageImpl) IOException(java.io.IOException) Async(org.springframework.scheduling.annotation.Async)

Example 13 with SDDCSoftwareConfig

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

the class VCDataServiceTest method testCheckAndUpdateIntegrationStatus.

@Test
public void testCheckAndUpdateIntegrationStatus() {
    SDDCSoftwareConfig vc = Mockito.spy(new SDDCSoftwareConfig());
    IntegrationStatus integrationStatus = Mockito.spy(new IntegrationStatus());
    String message = "message";
    vc.setIntegrationStatus(null);
    Mockito.doNothing().when(service).updateIntegrationStatus(any(SDDCSoftwareConfig.class));
    service.checkAndUpdateIntegrationStatus(vc, message);
    TestCase.assertEquals(1, vc.getIntegrationStatus().getRetryCounter());
    Mockito.when(vc.getIntegrationStatus()).thenReturn(integrationStatus);
    Mockito.when(integrationStatus.getRetryCounter()).thenReturn(FlowgateConstant.MAXNUMBEROFRETRIES);
    service.checkAndUpdateIntegrationStatus(vc, message);
    TestCase.assertEquals(IntegrationStatus.Status.ERROR, integrationStatus.getStatus());
    TestCase.assertEquals(message, integrationStatus.getDetail());
}
Also used : SDDCSoftwareConfig(com.vmware.flowgate.common.model.SDDCSoftwareConfig) IntegrationStatus(com.vmware.flowgate.common.model.IntegrationStatus) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 14 with SDDCSoftwareConfig

use of com.vmware.flowgate.common.model.SDDCSoftwareConfig 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)

Example 15 with SDDCSoftwareConfig

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

the class VCDataServiceTest method testQueryHostMetaDataForclustersIsNull.

@Test
public void testQueryHostMetaDataForclustersIsNull() 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);
    when(vsphereClient.getAllClusterComputeResource()).thenReturn(null);
    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");
    HashMap<String, String> hostJustification = new HashMap<String, String>();
    HostInfo hostInfo = new HostInfo();
    String vcHostObjStr = mapper.writeValueAsString(hostInfo);
    hostJustification.put(FlowgateConstant.HOST_METADATA, vcHostObjStr);
    asset.setJustificationfields(hostJustification);
    ResponseEntity<Asset> assets = Mockito.mock(ResponseEntity.class);
    when(restClient.getAssetByID(anyString())).thenReturn(assets);
    when(assets.getBody()).thenReturn(asset);
    doReturn(true).when(service).feedHostMetaData(any(), any());
    doReturn(true).when(service).feedClusterMetaData(any(), any(), any(), anyString());
    service.queryHostMetaData(vc);
}
Also used : HashMap(java.util.HashMap) ServerMapping(com.vmware.flowgate.common.model.ServerMapping) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) SDDCSoftwareConfig(com.vmware.flowgate.common.model.SDDCSoftwareConfig) HostSystem(com.vmware.vim.binding.vim.HostSystem) Asset(com.vmware.flowgate.common.model.Asset) HostInfo(com.vmware.flowgate.vcworker.model.HostInfo) ManagedObjectReference(com.vmware.vim.binding.vmodl.ManagedObjectReference) Test(org.junit.Test)

Aggregations

SDDCSoftwareConfig (com.vmware.flowgate.common.model.SDDCSoftwareConfig)42 Test (org.junit.Test)22 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)16 ServerMapping (com.vmware.flowgate.common.model.ServerMapping)11 ArrayList (java.util.ArrayList)9 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)9 WormholeRequestException (com.vmware.flowgate.exception.WormholeRequestException)8 MvcResult (org.springframework.test.web.servlet.MvcResult)8 SoftwareType (com.vmware.flowgate.common.model.SDDCSoftwareConfig.SoftwareType)7 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 Asset (com.vmware.flowgate.common.model.Asset)6 IOException (java.io.IOException)6 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)5 AuthVcUser (com.vmware.flowgate.auth.AuthVcUser)5 EventMessage (com.vmware.flowgate.common.model.redis.message.EventMessage)5 HashMap (java.util.HashMap)5 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)5 HostSystem (com.vmware.vim.binding.vim.HostSystem)4 ManagedObjectReference (com.vmware.vim.binding.vmodl.ManagedObjectReference)4