Search in sources :

Example 1 with Parent

use of com.vmware.flowgate.poweriqworker.model.Parent in project flowgate by vmware.

the class SyncSensorMetaDataJobTest method getRows.

List<Row> getRows() {
    List<Row> rows = new ArrayList<Row>();
    Row row = new Row();
    row.setName("2");
    row.setId(1);
    Parent parent = new Parent();
    parent.setId(2);
    parent.setType("aisle");
    row.setParent(parent);
    rows.add(row);
    return rows;
}
Also used : Parent(com.vmware.flowgate.poweriqworker.model.Parent) ArrayList(java.util.ArrayList) Row(com.vmware.flowgate.poweriqworker.model.Row)

Example 2 with Parent

use of com.vmware.flowgate.poweriqworker.model.Parent in project flowgate by vmware.

the class SyncSensorMetaDataJobTest method getRacks.

List<Rack> getRacks() {
    List<Rack> racks = new ArrayList<Rack>();
    Rack rack = new Rack();
    rack.setName("rack");
    rack.setId(2);
    Parent parent = new Parent();
    parent.setId(1);
    parent.setType("row");
    rack.setParent(parent);
    racks.add(rack);
    return racks;
}
Also used : Rack(com.vmware.flowgate.poweriqworker.model.Rack) Parent(com.vmware.flowgate.poweriqworker.model.Parent) ArrayList(java.util.ArrayList)

Example 3 with Parent

use of com.vmware.flowgate.poweriqworker.model.Parent in project flowgate by vmware.

the class SyncSensorMetaDataJobTest method createSensor.

Sensor createSensor() {
    Sensor sensor = new Sensor();
    Parent parent = new Parent();
    parent.setId(2);
    parent.setType("rack");
    sensor.setParent(parent);
    return sensor;
}
Also used : Parent(com.vmware.flowgate.poweriqworker.model.Parent) Sensor(com.vmware.flowgate.poweriqworker.model.Sensor)

Example 4 with Parent

use of com.vmware.flowgate.poweriqworker.model.Parent in project flowgate by vmware.

the class SyncSensorMetaDataJobTest method getAisles.

List<Aisle> getAisles() {
    List<Aisle> aisles = new ArrayList<Aisle>();
    Aisle aisle = new Aisle();
    aisle.setName("aisle");
    aisle.setId(2);
    Parent parent = new Parent();
    parent.setId(8);
    parent.setType("room");
    aisle.setParent(parent);
    aisles.add(aisle);
    return aisles;
}
Also used : Aisle(com.vmware.flowgate.poweriqworker.model.Aisle) Parent(com.vmware.flowgate.poweriqworker.model.Parent) ArrayList(java.util.ArrayList)

Example 5 with Parent

use of com.vmware.flowgate.poweriqworker.model.Parent in project flowgate by vmware.

the class PowerIQService method saveSensorAssetsToFlowgate.

public void saveSensorAssetsToFlowgate(Map<String, Asset> exsitingSensorAssets, PowerIQAPIClient client, String assetSource, LocationInfo location) {
    List<Asset> pdusFromFlowgate = restClient.getAllAssetsBySourceAndType(assetSource, AssetCategory.PDU);
    Map<String, Asset> pduAssetMap = getPDUIDAndAssetMap(pdusFromFlowgate);
    List<Sensor> sensors = null;
    int limit = 100;
    int offset = 0;
    List<Asset> newAssetsNeedToSave = null;
    List<Asset> oldAssetsNeedToupdate = null;
    while ((sensors = client.getSensors(limit, offset)) != null) {
        if (sensors.isEmpty()) {
            logger.warn(String.format("No sensor data from /api/v2/sensors?limit=%s&offset=%s", limit, offset));
            break;
        }
        newAssetsNeedToSave = new ArrayList<Asset>();
        oldAssetsNeedToupdate = new ArrayList<Asset>();
        for (Sensor sensor : sensors) {
            // Filter AbsoluteHumiditySensor
            if (subCategoryMap.get(sensor.getType()) == null) {
                continue;
            }
            Asset asset = new Asset();
            Map<String, String> sensorMap = new HashMap<String, String>();
            sensorMap.put(FlowgateConstant.SENSOR_ID_FROM_POWERIQ, String.valueOf(sensor.getId()));
            sensorMap.put(FlowgateConstant.POSITION, sensor.getPosition());
            if (sensor.getPduId() != null) {
                Asset pduAsset = pduAssetMap.get(String.valueOf(sensor.getPduId()));
                if (pduAsset == null) {
                    asset = fillLocation(sensor.getParent(), location);
                } else {
                    // If the sensor's has pdu information. Then it can use the PDU's location info.
                    com.vmware.flowgate.common.model.Parent parent = new com.vmware.flowgate.common.model.Parent();
                    parent.setParentId(String.valueOf(sensor.getPduId()));
                    parent.setType(FlowgateConstant.PDU);
                    asset.setParent(parent);
                    asset.setRoom(pduAsset.getRoom());
                    asset.setFloor(pduAsset.getFloor());
                    asset.setBuilding(pduAsset.getBuilding());
                    asset.setCity(pduAsset.getCity());
                    asset.setCountry(pduAsset.getCountry());
                    asset.setRegion(pduAsset.getRegion());
                    // Record the pdu_assetId for the sensor.
                    sensorMap.put(FlowgateConstant.PDU_ASSET_ID, pduAsset.getId());
                    // Record the sensorId and sensor_source for the pdu.
                    pduAsset = aggregatorSensorIdAndSourceForPdu(pduAsset, sensor, assetSource);
                }
            } else {
                asset = fillLocation(sensor.getParent(), location);
            }
            Asset assetToUpdate = exsitingSensorAssets.get(String.valueOf(sensor.getId()));
            if (assetToUpdate != null) {
                assetToUpdate.setAssetName(sensor.getName());
                assetToUpdate.setRow(asset.getRow());
                assetToUpdate.setRoom(asset.getRoom());
                assetToUpdate.setFloor(asset.getFloor());
                assetToUpdate.setBuilding(asset.getBuilding());
                assetToUpdate.setCity(asset.getCity());
                assetToUpdate.setCountry(asset.getCountry());
                assetToUpdate.setRegion(asset.getRegion());
                assetToUpdate.setSerialnumber(sensor.getSerialNumber());
                assetToUpdate.setParent(asset.getParent());
                HashMap<String, String> oldjustificationfields = assetToUpdate.getJustificationfields();
                Map<String, String> oldSensorInfoMap = null;
                try {
                    oldSensorInfoMap = getInfoMap(oldjustificationfields.get(FlowgateConstant.SENSOR));
                } catch (IOException e) {
                    logger.error("Format sensor info map error", e);
                }
                if (oldSensorInfoMap != null) {
                    oldSensorInfoMap.put(FlowgateConstant.PDU_ASSET_ID, sensorMap.get(FlowgateConstant.PDU_ASSET_ID));
                    oldSensorInfoMap.put(FlowgateConstant.SENSOR_ID_FROM_POWERIQ, sensorMap.get(FlowgateConstant.SENSOR_ID_FROM_POWERIQ));
                    oldSensorInfoMap.put(FlowgateConstant.POSITION, sensorMap.get(FlowgateConstant.POSITION));
                    try {
                        String sensorInfo = mapper.writeValueAsString(oldSensorInfoMap);
                        oldjustificationfields.put(FlowgateConstant.SENSOR, sensorInfo);
                    } catch (JsonProcessingException e) {
                        logger.error("Format sensor info map error", e);
                    }
                }
                assetToUpdate.setJustificationfields(oldjustificationfields);
                assetToUpdate.setLastupdate(System.currentTimeMillis());
                assetToUpdate.setMountingSide(sensorMountingSide.get(sensor.getPosition().toUpperCase()));
                // save
                oldAssetsNeedToupdate.add(assetToUpdate);
            } else {
                HashMap<String, String> justificationfieldsForSensor = new HashMap<String, String>();
                try {
                    justificationfieldsForSensor.put(FlowgateConstant.SENSOR, mapper.writeValueAsString(sensorMap));
                } catch (JsonProcessingException e) {
                    logger.error("Format sensor info map error", e);
                }
                asset.setAssetName(sensor.getName());
                asset.setJustificationfields(justificationfieldsForSensor);
                asset.setSerialnumber(sensor.getSerialNumber());
                asset.setAssetSource(assetSource);
                asset.setCategory(AssetCategory.Sensors);
                asset.setSubCategory(subCategoryMap.get(sensor.getType()));
                asset.setCreated(System.currentTimeMillis());
                if (sensor.getPosition() != null) {
                    asset.setMountingSide(sensorMountingSide.get(sensor.getPosition().toUpperCase()));
                }
                // save
                newAssetsNeedToSave.add(asset);
            }
        }
        restClient.saveAssets(oldAssetsNeedToupdate);
        if (!newAssetsNeedToSave.isEmpty()) {
            // We need the assetId of sensor asset, so it should be saved first.
            List<Asset> sensorAlreadySaved = new ArrayList<Asset>();
            for (Asset asset : newAssetsNeedToSave) {
                ResponseEntity<Void> res = restClient.saveAssets(asset);
                if (res.getStatusCode().is2xxSuccessful()) {
                    String assetId = getAssetIdByResponseEntity(res);
                    asset.setId(assetId);
                    sensorAlreadySaved.add(asset);
                    if (AssetSubCategory.Humidity.equals(asset.getSubCategory()) || AssetSubCategory.Temperature.equals(asset.getSubCategory())) {
                        Map<String, String> metricsFormulas = new HashMap<>(1);
                        Map<String, String> sensorFormulas = new HashMap<>(1);
                        sensorFormulas.put(asset.getSubCategory().toString(), asset.getId());
                        metricsFormulas.put(FlowgateConstant.SENSOR, asset.metricsFormulaToString(sensorFormulas));
                        asset.setMetricsformulars(metricsFormulas);
                        // save sensor formula
                        restClient.saveAssets(asset);
                    } else if (asset.getSubCategory() == null) {
                        Map<String, String> metricsFormulas = new HashMap<>(1);
                        Map<String, String> sensorFormulas = new HashMap<>(2);
                        sensorFormulas.put(MetricName.HUMIDITY, asset.getId());
                        sensorFormulas.put(MetricName.TEMPERATURE, asset.getId());
                        metricsFormulas.put(FlowgateConstant.SENSOR, asset.metricsFormulaToString(sensorFormulas));
                        asset.setMetricsformulars(metricsFormulas);
                        // save sensor formula
                        restClient.saveAssets(asset);
                    }
                }
            }
            Set<Asset> pduAssetNeedToUpdate = updatePduMetricformular(sensorAlreadySaved, pduAssetMap);
            restClient.saveAssets(new ArrayList<Asset>(pduAssetNeedToUpdate));
        }
        offset += limit;
    }
}
Also used : HashMap(java.util.HashMap) Parent(com.vmware.flowgate.poweriqworker.model.Parent) ArrayList(java.util.ArrayList) Asset(com.vmware.flowgate.common.model.Asset) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Map(java.util.Map) HashMap(java.util.HashMap) Sensor(com.vmware.flowgate.poweriqworker.model.Sensor)

Aggregations

Parent (com.vmware.flowgate.poweriqworker.model.Parent)7 ArrayList (java.util.ArrayList)6 Sensor (com.vmware.flowgate.poweriqworker.model.Sensor)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Asset (com.vmware.flowgate.common.model.Asset)1 Aisle (com.vmware.flowgate.poweriqworker.model.Aisle)1 Floor (com.vmware.flowgate.poweriqworker.model.Floor)1 Rack (com.vmware.flowgate.poweriqworker.model.Rack)1 Room (com.vmware.flowgate.poweriqworker.model.Room)1 Row (com.vmware.flowgate.poweriqworker.model.Row)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1