Search in sources :

Example 1 with PowerIQAPIClient

use of com.vmware.flowgate.poweriqworker.client.PowerIQAPIClient in project flowgate by vmware.

the class PowerIQService method removeSensorMetaData.

public void removeSensorMetaData(FacilitySoftwareConfig powerIQ) {
    PowerIQAPIClient client = createClient(powerIQ);
    restClient.setServiceKey(serviceKeyConfig.getServiceKey());
    List<Asset> sensors = restClient.getAllAssetsBySourceAndType(powerIQ.getId(), AssetCategory.Sensors);
    List<Asset> servers = restClient.getAllAssetsByType(AssetCategory.Server);
    long currentTime = System.currentTimeMillis();
    String expiredTimeRangeValue = template.opsForValue().get(EventMessageUtil.EXPIREDTIMERANGE);
    long expiredTimeRange = 0l;
    if (expiredTimeRangeValue != null) {
        expiredTimeRange = Long.valueOf(expiredTimeRangeValue);
    } else {
        expiredTimeRange = FlowgateConstant.DEFAULTEXPIREDTIMERANGE;
    }
    for (Asset sensor : sensors) {
        if (!sensor.isExpired(currentTime, expiredTimeRange)) {
            continue;
        }
        Sensor sensorFromPowerIQ = client.getSensorById(String.valueOf(sensor.getAssetNumber()));
        if (sensorFromPowerIQ == null) {
            List<Asset> needToupdate = updateServer(servers, sensor.getId());
            restClient.saveAssets(needToupdate);
            restClient.removeAssetByID(sensor.getId());
        } else {
            sensor.setLastupdate(currentTime);
            restClient.saveAssets(sensor);
        }
    }
}
Also used : PowerIQAPIClient(com.vmware.flowgate.poweriqworker.client.PowerIQAPIClient) Asset(com.vmware.flowgate.common.model.Asset) Sensor(com.vmware.flowgate.poweriqworker.model.Sensor)

Example 2 with PowerIQAPIClient

use of com.vmware.flowgate.poweriqworker.client.PowerIQAPIClient in project flowgate by vmware.

the class PowerIQService method syncPowerIQAssetsMetaData.

public void syncPowerIQAssetsMetaData(FacilitySoftwareConfig powerIQ) {
    PowerIQAPIClient client = createClient(powerIQ);
    restClient.setServiceKey(serviceKeyConfig.getServiceKey());
    try {
        client.testConnection();
    } catch (ResourceAccessException e1) {
        if (e1.getCause().getCause() instanceof ConnectException) {
            checkAndUpdateIntegrationStatus(powerIQ, e1.getMessage());
            return;
        }
    } catch (HttpClientErrorException e) {
        logger.error("Failed to query data from PowerIQ", e);
        IntegrationStatus integrationStatus = powerIQ.getIntegrationStatus();
        if (integrationStatus == null) {
            integrationStatus = new IntegrationStatus();
        }
        integrationStatus.setStatus(IntegrationStatus.Status.ERROR);
        integrationStatus.setDetail(e.getMessage());
        integrationStatus.setRetryCounter(FlowgateConstant.DEFAULTNUMBEROFRETRIES);
        updateIntegrationStatus(powerIQ);
        return;
    }
    LocationInfo location = getLocationInfo(client);
    List<Asset> pdusFromFlowgate = restClient.getAllAssetsBySourceAndType(powerIQ.getId(), AssetCategory.PDU);
    Map<String, Asset> pduIDAndAssetMap = getPDUIDAndAssetMap(pdusFromFlowgate);
    boolean hasNewPduToSave = savePduAssetsToFlowgate(pduIDAndAssetMap, powerIQ.getId(), client, location);
    logger.info("Finish sync PDU metadata for " + powerIQ.getName());
    Map<String, Asset> exsitingSensorAssets = getAssetsFromWormhole(powerIQ.getId());
    saveSensorAssetsToFlowgate(exsitingSensorAssets, client, powerIQ.getId(), location);
    logger.info("Finish sync Sensor metadata for: " + powerIQ.getName());
    if (hasNewPduToSave) {
        try {
            EventMessage eventMessage = EventMessageUtil.createEventMessage(EventType.Aggregator, EventMessageUtil.AggregateAndCleanPowerIQPDU, "");
            String jobmessage = EventMessageUtil.convertEventMessageAsString(eventMessage);
            publisher.publish(EventMessageUtil.AggregatorTopic, jobmessage);
            logger.info("Send aggregate Pdu data command");
        } catch (IOException e) {
            logger.error("Failed to Send aggregate pdu data command", e);
        }
    }
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) IntegrationStatus(com.vmware.flowgate.common.model.IntegrationStatus) IOException(java.io.IOException) ResourceAccessException(org.springframework.web.client.ResourceAccessException) LocationInfo(com.vmware.flowgate.poweriqworker.model.LocationInfo) EventMessage(com.vmware.flowgate.common.model.redis.message.EventMessage) PowerIQAPIClient(com.vmware.flowgate.poweriqworker.client.PowerIQAPIClient) Asset(com.vmware.flowgate.common.model.Asset) ConnectException(java.net.ConnectException)

Example 3 with PowerIQAPIClient

use of com.vmware.flowgate.poweriqworker.client.PowerIQAPIClient in project flowgate by vmware.

the class PowerIQService method syncRealtimeData.

// Sync realtime Data logic need to refactor.
private void syncRealtimeData(FacilitySoftwareConfig powerIQ) {
    restClient.setServiceKey(serviceKeyConfig.getServiceKey());
    List<Asset> allMappedPdus = Arrays.asList(restClient.getMappedAsset(AssetCategory.PDU).getBody());
    if (allMappedPdus == null || allMappedPdus.isEmpty()) {
        return;
    }
    PowerIQAPIClient client = createClient(powerIQ);
    try {
        client.testConnection();
    } catch (HttpClientErrorException e) {
        logger.error("Failed to query data from PowerIQ", e);
        IntegrationStatus integrationStatus = powerIQ.getIntegrationStatus();
        if (integrationStatus == null) {
            integrationStatus = new IntegrationStatus();
        }
        integrationStatus.setStatus(IntegrationStatus.Status.ERROR);
        integrationStatus.setDetail(e.getMessage());
        integrationStatus.setRetryCounter(FlowgateConstant.DEFAULTNUMBEROFRETRIES);
        updateIntegrationStatus(powerIQ);
        return;
    } catch (ResourceAccessException e1) {
        if (e1.getCause().getCause() instanceof ConnectException) {
            checkAndUpdateIntegrationStatus(powerIQ, e1.getMessage());
            return;
        }
    }
    Map<String, Map<String, String>> pduAssetIdAndPduInfoMap = new HashMap<String, Map<String, String>>();
    for (Asset asset : allMappedPdus) {
        String id = null;
        Map<String, String> pduInfoMap = null;
        if (asset.getJustificationfields() != null) {
            HashMap<String, String> justficationfields = asset.getJustificationfields();
            String pduInfo = justficationfields.get(FlowgateConstant.PDU);
            try {
                pduInfoMap = getInfoMap(pduInfo);
            } catch (IOException e) {
                continue;
            }
            id = pduInfoMap.get(FlowgateConstant.PDU_ID_FROM_POWERIQ);
            // Only check the pdu that belong to the current PowerIQ.
            if (!asset.getAssetSource().contains((powerIQ.getId()))) {
                continue;
            }
        }
        if (id != null) {
            pduAssetIdAndPduInfoMap.put(asset.getId(), pduInfoMap);
        }
    }
    List<RealTimeData> pduMetricRealTimeDatas = getRealTimeDatas(pduAssetIdAndPduInfoMap, client, getAdvanceSetting(powerIQ));
    if (!pduMetricRealTimeDatas.isEmpty()) {
        restClient.saveRealTimeData(pduMetricRealTimeDatas);
        logger.info("Finish sync pdu realtime data for " + powerIQ.getName());
    } else {
        logger.info("Not found any pdu realtime data from " + powerIQ.getName());
    }
    List<Asset> allMappedServers = Arrays.asList(restClient.getMappedAsset(AssetCategory.Server).getBody());
    if (allMappedServers.isEmpty()) {
        logger.info("No mapped server found. End sync RealTime data Job");
        return;
    }
    // get sensor assetIds from pdu's metricsFormular attribute
    Set<String> sensorAssetIds = getAssetIdfromformular(allMappedPdus);
    // get sensor assetIds from server's metricsFromular attribute
    Set<String> sensorAssetIdsFromServer = getAssetIdfromformular(allMappedServers);
    sensorAssetIds.addAll(sensorAssetIdsFromServer);
    if (sensorAssetIds.isEmpty()) {
        return;
    }
    // filter sensors
    List<Asset> sensorFromPowerIQ = filterAssetsBySource(powerIQ.getId(), sensorAssetIds);
    List<RealTimeData> realTimeDatas = getSensorRealTimeData(powerIQ, sensorFromPowerIQ);
    logger.info("Received new Sensor data, data item size is:" + realTimeDatas.size());
    if (realTimeDatas.isEmpty()) {
        logger.info("Not found any sensor realtime data from " + powerIQ.getName());
        return;
    }
    restClient.saveRealTimeData(realTimeDatas);
    logger.info("Finish sync sensor realtime data for " + powerIQ.getName());
}
Also used : RealTimeData(com.vmware.flowgate.common.model.RealTimeData) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) HashMap(java.util.HashMap) IntegrationStatus(com.vmware.flowgate.common.model.IntegrationStatus) IOException(java.io.IOException) ResourceAccessException(org.springframework.web.client.ResourceAccessException) PowerIQAPIClient(com.vmware.flowgate.poweriqworker.client.PowerIQAPIClient) Asset(com.vmware.flowgate.common.model.Asset) Map(java.util.Map) HashMap(java.util.HashMap) ConnectException(java.net.ConnectException)

Example 4 with PowerIQAPIClient

use of com.vmware.flowgate.poweriqworker.client.PowerIQAPIClient in project flowgate by vmware.

the class PowerIQService method getSensorRealTimeData.

public List<RealTimeData> getSensorRealTimeData(FacilitySoftwareConfig powerIQ, List<Asset> assets) {
    HashMap<AdvanceSettingType, String> advanceSetting = getAdvanceSetting(powerIQ);
    List<RealTimeData> realtimeDatas = new ArrayList<RealTimeData>();
    String dateFormat = advanceSetting.get(AdvanceSettingType.DateFormat);
    String timezone = advanceSetting.get(AdvanceSettingType.TimeZone);
    String temperature = advanceSetting.get(AdvanceSettingType.TEMPERATURE_UNIT);
    String humidity = advanceSetting.get(AdvanceSettingType.HUMIDITY_UNIT);
    PowerIQAPIClient powerIQAPIClient = createClient(powerIQ);
    for (Asset asset : assets) {
        HashMap<String, String> sensorExtraInfo = asset.getJustificationfields();
        String sensorInfo = sensorExtraInfo.get(FlowgateConstant.SENSOR);
        Map<String, String> sensorInfoMap = null;
        try {
            sensorInfoMap = getInfoMap(sensorInfo);
        } catch (IOException e2) {
            continue;
        }
        String sensorId = sensorInfoMap.get(FlowgateConstant.SENSOR_ID_FROM_POWERIQ);
        Sensor sensor = null;
        try {
            sensor = powerIQAPIClient.getSensorById(sensorId);
        } catch (HttpClientErrorException e) {
            logger.error("Failed to query data from PowerIQ", e);
            IntegrationStatus integrationStatus = powerIQ.getIntegrationStatus();
            if (integrationStatus == null) {
                integrationStatus = new IntegrationStatus();
            }
            integrationStatus.setStatus(IntegrationStatus.Status.ERROR);
            integrationStatus.setDetail(e.getMessage());
            integrationStatus.setRetryCounter(FlowgateConstant.DEFAULTNUMBEROFRETRIES);
            updateIntegrationStatus(powerIQ);
            break;
        } catch (ResourceAccessException e1) {
            if (e1.getCause().getCause() instanceof ConnectException) {
                checkAndUpdateIntegrationStatus(powerIQ, e1.getMessage());
                break;
            }
            break;
        }
        SensorReading reading = sensor.getReading();
        if (reading == null || reading.getId() == 0) {
            continue;
        }
        RealTimeData realTimeData = new RealTimeData();
        String valueDateTime = reading.getReadingTime();
        long recordedTime = WormholeDateFormat.getLongTime(valueDateTime, dateFormat, timezone);
        if (recordedTime == -1) {
            logger.error("Failed to translate the time string: " + valueDateTime + ".And the dateformat is " + dateFormat);
            continue;
        }
        List<ValueUnit> values = new ArrayList<ValueUnit>();
        ValueUnit value = new ValueUnit();
        value.setTime(recordedTime);
        value.setKey(sensorAndMetricMap.get(sensor.getType()));
        String unit = reading.getUom();
        MetricUnit sourceUnit = null, targetUnit = null;
        switch(sensorAndMetricMap.get(sensor.getType())) {
            case MetricName.HUMIDITY:
                if (unit != null && !unit.isEmpty()) {
                    if (unit.equals("%")) {
                        sourceUnit = MetricUnit.percent;
                    } else {
                        sourceUnit = MetricUnit.valueOf(unit);
                    }
                } else {
                    if (humidity.equals("%")) {
                        sourceUnit = MetricUnit.percent;
                    } else {
                        sourceUnit = MetricUnit.valueOf(humidity);
                    }
                }
                targetUnit = MetricUnit.percent;
                break;
            case MetricName.TEMPERATURE:
                if (unit != null && !unit.isEmpty()) {
                    sourceUnit = MetricUnit.valueOf(unit.toUpperCase());
                } else {
                    sourceUnit = MetricUnit.valueOf(temperature.toUpperCase());
                }
                targetUnit = MetricUnit.C;
                break;
            default:
                break;
        }
        Double metricsValue = reading.getValue();
        if (metricsValue == null) {
            continue;
        }
        try {
            value.setValueNum(value.translateUnit(metricsValue, sourceUnit, targetUnit));
        } catch (WormholeException e) {
            logger.error("Cannot translate Unit", e);
        }
        if (targetUnit.toString().equals(MetricUnit.percent.toString())) {
            value.setUnit("%");
        } else {
            value.setUnit(targetUnit.toString());
        }
        values.add(value);
        realTimeData.setAssetID(asset.getId());
        realTimeData.setTime(recordedTime);
        realTimeData.setValues(values);
        realTimeData.setId(asset.getId() + "_" + recordedTime);
        realtimeDatas.add(realTimeData);
    }
    return realtimeDatas;
}
Also used : RealTimeData(com.vmware.flowgate.common.model.RealTimeData) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) IntegrationStatus(com.vmware.flowgate.common.model.IntegrationStatus) ArrayList(java.util.ArrayList) WormholeException(com.vmware.flowgate.common.exception.WormholeException) IOException(java.io.IOException) ResourceAccessException(org.springframework.web.client.ResourceAccessException) SensorReading(com.vmware.flowgate.poweriqworker.model.SensorReading) PowerIQAPIClient(com.vmware.flowgate.poweriqworker.client.PowerIQAPIClient) Asset(com.vmware.flowgate.common.model.Asset) MetricUnit(com.vmware.flowgate.common.model.ValueUnit.MetricUnit) ValueUnit(com.vmware.flowgate.common.model.ValueUnit) AdvanceSettingType(com.vmware.flowgate.common.model.FacilitySoftwareConfig.AdvanceSettingType) Sensor(com.vmware.flowgate.poweriqworker.model.Sensor) ConnectException(java.net.ConnectException)

Aggregations

Asset (com.vmware.flowgate.common.model.Asset)4 PowerIQAPIClient (com.vmware.flowgate.poweriqworker.client.PowerIQAPIClient)4 IntegrationStatus (com.vmware.flowgate.common.model.IntegrationStatus)3 IOException (java.io.IOException)3 ConnectException (java.net.ConnectException)3 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)3 ResourceAccessException (org.springframework.web.client.ResourceAccessException)3 RealTimeData (com.vmware.flowgate.common.model.RealTimeData)2 Sensor (com.vmware.flowgate.poweriqworker.model.Sensor)2 WormholeException (com.vmware.flowgate.common.exception.WormholeException)1 AdvanceSettingType (com.vmware.flowgate.common.model.FacilitySoftwareConfig.AdvanceSettingType)1 ValueUnit (com.vmware.flowgate.common.model.ValueUnit)1 MetricUnit (com.vmware.flowgate.common.model.ValueUnit.MetricUnit)1 EventMessage (com.vmware.flowgate.common.model.redis.message.EventMessage)1 LocationInfo (com.vmware.flowgate.poweriqworker.model.LocationInfo)1 SensorReading (com.vmware.flowgate.poweriqworker.model.SensorReading)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1