Search in sources :

Example 1 with WormholeException

use of com.vmware.flowgate.common.exception.WormholeException in project flowgate by vmware.

the class AuthVcUser method authenticateUser.

public void authenticateUser(String name, String password) throws URISyntaxException, InvalidLogin, InvalidLocale, SSLException {
    Client vmomiClient = null;
    SessionManager sessionManager = null;
    try {
        URI uri = new URI(serviceUrl);
        HttpConfiguration httpConfig = new HttpConfigurationImpl();
        httpConfig.setThumbprintVerifier(getThumbprintVerifier());
        HttpClientConfiguration clientConfig = HttpClientConfiguration.Factory.newInstance();
        // set customized SSL protocols
        // TlsClientConfiguration tlsClientConfiguration = new TlsClientConfiguration();
        httpConfig.setEnabledProtocols(new String[] { "TLSv1.1", "TLSv1.2" });
        clientConfig.setHttpConfiguration(httpConfig);
        vmomiClient = Client.Factory.createClient(uri, version, clientConfig);
        ManagedObjectReference svcRef = new ManagedObjectReference();
        svcRef.setType("ServiceInstance");
        svcRef.setValue("ServiceInstance");
        if (name == null || name.isEmpty()) {
            // VC session token auth
            vmomiClient.getBinding().setSession(vmomiClient.getBinding().createSession(password));
        }
        ServiceInstance instance = vmomiClient.createStub(ServiceInstance.class, svcRef);
        ServiceInstanceContent instanceContent = instance.retrieveContent();
        sessionManager = vmomiClient.createStub(SessionManager.class, instanceContent.getSessionManager());
        if (name != null && !name.isEmpty()) {
            // username/passowrd auth
            sessionManager.login(name, password, sessionManager.getDefaultLocale());
            sessionManager.logout();
        } else {
            // VC session token auth
            UserSession session = sessionManager.getCurrentSession();
            if (session == null) {
                throw new WormholeException("invalid vc session.");
            } else {
                logger.info(session.getUserName() + " is authenticated");
            }
        }
    } finally {
        if (vmomiClient != null) {
            vmomiClient.shutdown();
        }
    }
}
Also used : ServiceInstanceContent(com.vmware.vim.binding.vim.ServiceInstanceContent) HttpConfigurationImpl(com.vmware.vim.vmomi.client.http.impl.HttpConfigurationImpl) SessionManager(com.vmware.vim.binding.vim.SessionManager) UserSession(com.vmware.vim.binding.vim.UserSession) ServiceInstance(com.vmware.vim.binding.vim.ServiceInstance) WormholeException(com.vmware.flowgate.common.exception.WormholeException) HttpConfiguration(com.vmware.vim.vmomi.client.http.HttpConfiguration) Client(com.vmware.vim.vmomi.client.Client) URI(java.net.URI) HttpClientConfiguration(com.vmware.vim.vmomi.client.http.HttpClientConfiguration) ManagedObjectReference(com.vmware.vim.binding.vmodl.ManagedObjectReference)

Example 2 with WormholeException

use of com.vmware.flowgate.common.exception.WormholeException in project flowgate by vmware.

the class NlyteDataService method generateValueUnits.

public List<ValueUnit> generateValueUnits(List<PowerStripsRealtimeValue> values, HashMap<AdvanceSettingType, String> advanceSettingMap) {
    List<ValueUnit> valueunits = new ArrayList<ValueUnit>();
    long currenttime = System.currentTimeMillis();
    String dateFormat = advanceSettingMap.get(AdvanceSettingType.DateFormat);
    String timezone = advanceSettingMap.get(AdvanceSettingType.TimeZone);
    String current = advanceSettingMap.get(AdvanceSettingType.PDU_AMPS_UNIT);
    String power = advanceSettingMap.get(AdvanceSettingType.PDU_POWER_UNIT);
    String voltage = advanceSettingMap.get(AdvanceSettingType.PDU_VOLT_UNIT);
    for (PowerStripsRealtimeValue value : values) {
        String valueDateTime = value.getRecordedDateTime();
        long recordedTime = WormholeDateFormat.getLongTime(valueDateTime, dateFormat, timezone);
        if (recordedTime > currenttime || recordedTime == -1) {
            logger.error("Failed to translate the time string: " + valueDateTime);
            continue;
        }
        if (sensorValueTypeMap.containsKey(value.getName())) {
            ValueUnit valueunit = new ValueUnit();
            valueunit.setKey(sensorValueTypeMap.get(value.getName()));
            String unit = value.getUnit();
            MetricUnit targetUnit = null, sourceUnit = null;
            switch(sensorValueTypeMap.get(value.getName())) {
                case MetricName.PDU_TOTAL_CURRENT:
                    if (unit != null && !unit.isEmpty()) {
                        sourceUnit = MetricUnit.valueOf(unit.toUpperCase());
                    } else {
                        sourceUnit = MetricUnit.valueOf(current.toUpperCase());
                    }
                    targetUnit = MetricUnit.A;
                    break;
                case MetricName.PDU_TOTAL_POWER:
                    if (unit != null && !unit.isEmpty()) {
                        sourceUnit = MetricUnit.valueOf(unit.toUpperCase());
                    } else {
                        sourceUnit = MetricUnit.valueOf(power.toUpperCase());
                    }
                    targetUnit = MetricUnit.kW;
                    break;
                case MetricName.PDU_VOLTAGE:
                    if (unit != null && !unit.isEmpty()) {
                        sourceUnit = MetricUnit.valueOf(unit.toUpperCase());
                    } else {
                        sourceUnit = MetricUnit.valueOf(voltage.toUpperCase());
                    }
                    targetUnit = MetricUnit.V;
                    break;
                default:
                    break;
            }
            valueunit.setUnit(targetUnit.toString());
            try {
                valueunit.setValueNum(valueunit.translateUnit(value.getValue(), sourceUnit, targetUnit));
            } catch (WormholeException e) {
                logger.error("Cannot translate Unit", e);
            }
            valueunit.setTime(recordedTime);
            valueunits.add(valueunit);
        } else {
            continue;
        }
    }
    return valueunits;
}
Also used : ArrayList(java.util.ArrayList) MetricUnit(com.vmware.flowgate.common.model.ValueUnit.MetricUnit) WormholeException(com.vmware.flowgate.common.exception.WormholeException) ValueUnit(com.vmware.flowgate.common.model.ValueUnit) PowerStripsRealtimeValue(com.vmware.flowgate.nlyteworker.model.PowerStripsRealtimeValue)

Example 3 with WormholeException

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

Example 4 with WormholeException

use of com.vmware.flowgate.common.exception.WormholeException in project flowgate by vmware.

the class EncryptionGuard method decode.

/**
 * Decrypt the encrypted text against given secret key.
 *
 * @param encodedText
 *           the encrypted string
 * @return the clear string, or null if encrypted string is null
 * @throws WormholeException
 *            if input arguments is null
 */
public static String decode(String encodedText) throws GeneralSecurityException, UnsupportedEncodingException {
    if (encodedText == null) {
        return null;
    }
    if (encodedText.length() < SALT_SIZE) {
        throw new WormholeException("This encodedText is invalid");
    // throw EncryptionException.SHORT_ENCRYPTED_STRING(encodedText);
    }
    String initKey = FlowgateKeystore.getEncryptKey();
    Key key = new SecretKeySpec(initKey.getBytes(), "AES");
    encodedText.substring(0, SALT_SIZE);
    String encryptedText = encodedText.substring(SALT_SIZE);
    byte[] encryptedBytes = Base64.getDecoder().decode(encryptedText);
    Cipher cipher = getCiperInternal(Cipher.DECRYPT_MODE, key);
    byte[] outputBytes = cipher.doFinal(encryptedBytes);
    String outputText = new String(outputBytes, UTF8_ENCODING);
    return outputText.substring(SALT_SIZE);
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) WormholeException(com.vmware.flowgate.common.exception.WormholeException) Cipher(javax.crypto.Cipher) Key(java.security.Key)

Aggregations

WormholeException (com.vmware.flowgate.common.exception.WormholeException)4 ValueUnit (com.vmware.flowgate.common.model.ValueUnit)2 MetricUnit (com.vmware.flowgate.common.model.ValueUnit.MetricUnit)2 ArrayList (java.util.ArrayList)2 Asset (com.vmware.flowgate.common.model.Asset)1 AdvanceSettingType (com.vmware.flowgate.common.model.FacilitySoftwareConfig.AdvanceSettingType)1 IntegrationStatus (com.vmware.flowgate.common.model.IntegrationStatus)1 RealTimeData (com.vmware.flowgate.common.model.RealTimeData)1 PowerStripsRealtimeValue (com.vmware.flowgate.nlyteworker.model.PowerStripsRealtimeValue)1 PowerIQAPIClient (com.vmware.flowgate.poweriqworker.client.PowerIQAPIClient)1 Sensor (com.vmware.flowgate.poweriqworker.model.Sensor)1 SensorReading (com.vmware.flowgate.poweriqworker.model.SensorReading)1 ServiceInstance (com.vmware.vim.binding.vim.ServiceInstance)1 ServiceInstanceContent (com.vmware.vim.binding.vim.ServiceInstanceContent)1 SessionManager (com.vmware.vim.binding.vim.SessionManager)1 UserSession (com.vmware.vim.binding.vim.UserSession)1 ManagedObjectReference (com.vmware.vim.binding.vmodl.ManagedObjectReference)1 Client (com.vmware.vim.vmomi.client.Client)1 HttpClientConfiguration (com.vmware.vim.vmomi.client.http.HttpClientConfiguration)1 HttpConfiguration (com.vmware.vim.vmomi.client.http.HttpConfiguration)1