From a3c545c6eb843fceb5b60015ad0ab713eb42982a Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Wed, 13 Dec 2023 17:46:22 +0530 Subject: [PATCH 1/6] server: fix url check for managed storages Fixes #8352 Some managed storages may not need a valid URL to be passed. We can skip check and extraction of host or path from url of such storages Signed-off-by: Abhishek Kumar --- .../com/cloud/storage/StorageManagerImpl.java | 60 +++++++++++-------- .../cloud/storage/StorageManagerImplTest.java | 23 +++++++ 2 files changed, 57 insertions(+), 26 deletions(-) diff --git a/server/src/main/java/com/cloud/storage/StorageManagerImpl.java b/server/src/main/java/com/cloud/storage/StorageManagerImpl.java index 4bc471c5084c..8a4df6a2e5d9 100644 --- a/server/src/main/java/com/cloud/storage/StorageManagerImpl.java +++ b/server/src/main/java/com/cloud/storage/StorageManagerImpl.java @@ -132,6 +132,7 @@ import org.apache.cloudstack.storage.object.ObjectStoreEntity; import org.apache.cloudstack.storage.to.VolumeObjectTO; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang.ObjectUtils; import org.apache.commons.lang3.EnumUtils; import org.apache.commons.lang.time.DateUtils; import org.apache.commons.lang3.StringUtils; @@ -808,7 +809,7 @@ protected String createLocalStoragePoolName(Host host, StoragePoolInfo storagePo @Override public PrimaryDataStoreInfo createPool(CreateStoragePoolCmd cmd) throws ResourceInUseException, IllegalArgumentException, UnknownHostException, ResourceUnavailableException { String providerName = cmd.getStorageProviderName(); - Map uriParams = extractUriParamsAsMap(cmd.getUrl()); + Map uriParams = extractUriParamsAsMap(cmd.getUrl(), cmd.isManaged()); DataStoreProvider storeProvider = _dataStoreProviderMgr.getDataStoreProvider(providerName); if (storeProvider == null) { @@ -923,42 +924,47 @@ public PrimaryDataStoreInfo createPool(CreateStoragePoolCmd cmd) throws Resource return (PrimaryDataStoreInfo)_dataStoreMgr.getDataStore(store.getId(), DataStoreRole.Primary); } - private Map extractUriParamsAsMap(String url){ + protected Map extractUriParamsAsMap(String url, boolean managed) { Map uriParams = new HashMap<>(); UriUtils.UriInfo uriInfo = UriUtils.getUriInfo(url); String scheme = uriInfo.getScheme(); String storageHost = uriInfo.getStorageHost(); String storagePath = uriInfo.getStoragePath(); - try { - if (scheme == null) { - throw new InvalidParameterValueException("scheme is null " + url + ", add nfs:// (or cifs://) as a prefix"); - } else if (scheme.equalsIgnoreCase("nfs")) { - if (storageHost == null || storagePath == null || storageHost.trim().isEmpty() || storagePath.trim().isEmpty()) { - throw new InvalidParameterValueException("host or path is null, should be nfs://hostname/path"); - } - } else if (scheme.equalsIgnoreCase("cifs")) { - // Don't validate against a URI encoded URI. + if (scheme == null) { + if (managed) { + return uriParams; + } + throw new InvalidParameterValueException("scheme is null " + url + ", add nfs:// (or cifs://) as a prefix"); + } + boolean isHostOrPathBlank = StringUtils.isAnyBlank(storagePath, storageHost); + if (scheme.equalsIgnoreCase("nfs")) { + if (isHostOrPathBlank) { + throw new InvalidParameterValueException("host or path is null, should be nfs://hostname/path"); + } + } else if (scheme.equalsIgnoreCase("cifs")) { + // Don't validate against a URI encoded URI. + try { URI cifsUri = new URI(url); String warnMsg = UriUtils.getCifsUriParametersProblems(cifsUri); if (warnMsg != null) { throw new InvalidParameterValueException(warnMsg); } - } else if (scheme.equalsIgnoreCase("sharedMountPoint")) { - if (storagePath == null) { - throw new InvalidParameterValueException("host or path is null, should be sharedmountpoint://localhost/path"); - } - } else if (scheme.equalsIgnoreCase("rbd")) { - if (storagePath == null) { - throw new InvalidParameterValueException("host or path is null, should be rbd://hostname/pool"); - } - } else if (scheme.equalsIgnoreCase("gluster")) { - if (storageHost == null || storagePath == null || storageHost.trim().isEmpty() || storagePath.trim().isEmpty()) { - throw new InvalidParameterValueException("host or path is null, should be gluster://hostname/volume"); - } + } catch (URISyntaxException e) { + throw new InvalidParameterValueException(url + " is not a valid uri"); + } + } else if (scheme.equalsIgnoreCase("sharedMountPoint")) { + if (storagePath == null) { + throw new InvalidParameterValueException("host or path is null, should be sharedmountpoint://localhost/path"); + } + } else if (scheme.equalsIgnoreCase("rbd")) { + if (storagePath == null) { + throw new InvalidParameterValueException("host or path is null, should be rbd://hostname/pool"); + } + } else if (scheme.equalsIgnoreCase("gluster")) { + if (isHostOrPathBlank) { + throw new InvalidParameterValueException("host or path is null, should be gluster://hostname/volume"); } - } catch (URISyntaxException e) { - throw new InvalidParameterValueException(url + " is not a valid uri"); } String hostPath = null; @@ -975,7 +981,9 @@ private Map extractUriParamsAsMap(String url){ uriParams.put("host", storageHost); uriParams.put("hostPath", hostPath); uriParams.put("userInfo", uriInfo.getUserInfo()); - uriParams.put("port", uriInfo.getPort() + ""); + if (uriInfo.getPort() > 0) { + uriParams.put("port", uriInfo.getPort() + ""); + } return uriParams; } diff --git a/server/src/test/java/com/cloud/storage/StorageManagerImplTest.java b/server/src/test/java/com/cloud/storage/StorageManagerImplTest.java index 478547bff1ef..859782dede0c 100644 --- a/server/src/test/java/com/cloud/storage/StorageManagerImplTest.java +++ b/server/src/test/java/com/cloud/storage/StorageManagerImplTest.java @@ -23,6 +23,7 @@ import com.cloud.vm.dao.VMInstanceDao; import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao; import org.apache.cloudstack.storage.datastore.db.StoragePoolVO; +import org.apache.commons.collections.MapUtils; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -34,6 +35,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.Map; @RunWith(MockitoJUnitRunner.class) public class StorageManagerImplTest { @@ -157,4 +159,25 @@ public void storagePoolCompatibleWithVolumePoolTestVolumeWithoutPoolIdInAllocate } + @Test + public void testExtractUriParamsAsMapWithSolidFireUrl() { + String sfUrl = "MVIP=1.2.3.4;SVIP=6.7.8.9;clusterAdminUsername=admin;" + + "clusterAdminPassword=password;clusterDefaultMinIops=1000;" + + "clusterDefaultMaxIops=2000;clusterDefaultBurstIopsPercentOfMaxIops=2"; + Map uriParams = storageManagerImpl.extractUriParamsAsMap(sfUrl, true); + Assert.assertTrue(MapUtils.isEmpty(uriParams)); + } + + @Test + public void testExtractUriParamsAsMapWithNFSUrl() { + String scheme = "nfs"; + String host = "HOST"; + String path = "/PATH"; + String sfUrl = String.format("%s://%s%s", scheme, host, path); + Map uriParams = storageManagerImpl.extractUriParamsAsMap(sfUrl, false); + Assert.assertTrue(MapUtils.isNotEmpty(uriParams)); + Assert.assertEquals(scheme, uriParams.get("scheme")); + Assert.assertEquals(host, uriParams.get("host")); + Assert.assertEquals(path, uriParams.get("hostPath")); + } } From ec1da3c838b383049faa4cd00962aa76ad8dbe45 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Wed, 13 Dec 2023 17:57:41 +0530 Subject: [PATCH 2/6] remove unnecessary imports Signed-off-by: Abhishek Kumar --- .../main/java/com/cloud/storage/StorageManagerImpl.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/server/src/main/java/com/cloud/storage/StorageManagerImpl.java b/server/src/main/java/com/cloud/storage/StorageManagerImpl.java index 8a4df6a2e5d9..2043cf505ef0 100644 --- a/server/src/main/java/com/cloud/storage/StorageManagerImpl.java +++ b/server/src/main/java/com/cloud/storage/StorageManagerImpl.java @@ -18,10 +18,12 @@ import static com.cloud.utils.NumbersUtil.toHumanReadableSize; +import java.io.UnsupportedEncodingException; import java.math.BigDecimal; import java.math.BigInteger; import java.net.URI; import java.net.URISyntaxException; +import java.net.URLDecoder; import java.net.UnknownHostException; import java.nio.file.Files; import java.sql.PreparedStatement; @@ -132,9 +134,8 @@ import org.apache.cloudstack.storage.object.ObjectStoreEntity; import org.apache.cloudstack.storage.to.VolumeObjectTO; import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.lang.ObjectUtils; -import org.apache.commons.lang3.EnumUtils; import org.apache.commons.lang.time.DateUtils; +import org.apache.commons.lang3.EnumUtils; import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; import org.springframework.stereotype.Component; @@ -255,8 +256,6 @@ import com.cloud.vm.VirtualMachine.State; import com.cloud.vm.dao.VMInstanceDao; import com.google.common.collect.Sets; -import java.io.UnsupportedEncodingException; -import java.net.URLDecoder; @Component From 33785a8a6ae791098a848c74f714e969f1d574ef Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Wed, 13 Dec 2023 22:37:16 +0530 Subject: [PATCH 3/6] refactor Signed-off-by: Abhishek Kumar --- .../com/cloud/storage/StorageManagerImpl.java | 28 +++++++++++++------ .../cloud/storage/StorageManagerImplTest.java | 4 +-- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/server/src/main/java/com/cloud/storage/StorageManagerImpl.java b/server/src/main/java/com/cloud/storage/StorageManagerImpl.java index 2043cf505ef0..410728d4f89e 100644 --- a/server/src/main/java/com/cloud/storage/StorageManagerImpl.java +++ b/server/src/main/java/com/cloud/storage/StorageManagerImpl.java @@ -134,6 +134,7 @@ import org.apache.cloudstack.storage.object.ObjectStoreEntity; import org.apache.cloudstack.storage.to.VolumeObjectTO; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections.MapUtils; import org.apache.commons.lang.time.DateUtils; import org.apache.commons.lang3.EnumUtils; import org.apache.commons.lang3.StringUtils; @@ -808,7 +809,7 @@ protected String createLocalStoragePoolName(Host host, StoragePoolInfo storagePo @Override public PrimaryDataStoreInfo createPool(CreateStoragePoolCmd cmd) throws ResourceInUseException, IllegalArgumentException, UnknownHostException, ResourceUnavailableException { String providerName = cmd.getStorageProviderName(); - Map uriParams = extractUriParamsAsMap(cmd.getUrl(), cmd.isManaged()); + Map uriParams = extractUriParamsAsMap(cmd.getUrl()); DataStoreProvider storeProvider = _dataStoreProviderMgr.getDataStoreProvider(providerName); if (storeProvider == null) { @@ -822,7 +823,10 @@ public PrimaryDataStoreInfo createPool(CreateStoragePoolCmd cmd) throws Resource Long podId = cmd.getPodId(); Long zoneId = cmd.getZoneId(); - ScopeType scopeType = uriParams.get("scheme").toString().equals("file") ? ScopeType.HOST : ScopeType.CLUSTER; + ScopeType scopeType = ScopeType.CLUSTER; + if ("file".equalsIgnoreCase(uriParams.get("scheme"))) { + scopeType = ScopeType.HOST; + } String scope = cmd.getScope(); if (scope != null) { try { @@ -889,7 +893,9 @@ public PrimaryDataStoreInfo createPool(CreateStoragePoolCmd cmd) throws Resource params.put("managed", cmd.isManaged()); params.put("capacityBytes", cmd.getCapacityBytes()); params.put("capacityIops", cmd.getCapacityIops()); - params.putAll(uriParams); + if (MapUtils.isNotEmpty(uriParams)) { + params.putAll(uriParams); + } DataStoreLifeCycle lifeCycle = storeProvider.getDataStoreLifeCycle(); DataStore store = null; @@ -923,18 +929,22 @@ public PrimaryDataStoreInfo createPool(CreateStoragePoolCmd cmd) throws Resource return (PrimaryDataStoreInfo)_dataStoreMgr.getDataStore(store.getId(), DataStoreRole.Primary); } - protected Map extractUriParamsAsMap(String url, boolean managed) { + protected Map extractUriParamsAsMap(String url) { Map uriParams = new HashMap<>(); - UriUtils.UriInfo uriInfo = UriUtils.getUriInfo(url); + UriUtils.UriInfo uriInfo; + try { + uriInfo = UriUtils.getUriInfo(url); + } catch (CloudRuntimeException cre) { + s_logger.debug(String.format("URI validation for url: %s failed, returning empty uri params", url)); + return uriParams; + } String scheme = uriInfo.getScheme(); String storageHost = uriInfo.getStorageHost(); String storagePath = uriInfo.getStoragePath(); if (scheme == null) { - if (managed) { - return uriParams; - } - throw new InvalidParameterValueException("scheme is null " + url + ", add nfs:// (or cifs://) as a prefix"); + s_logger.debug(String.format("Scheme for url: %s is not found, returning empty uri params", url)); + return uriParams; } boolean isHostOrPathBlank = StringUtils.isAnyBlank(storagePath, storageHost); if (scheme.equalsIgnoreCase("nfs")) { diff --git a/server/src/test/java/com/cloud/storage/StorageManagerImplTest.java b/server/src/test/java/com/cloud/storage/StorageManagerImplTest.java index 859782dede0c..c9a27bb64226 100644 --- a/server/src/test/java/com/cloud/storage/StorageManagerImplTest.java +++ b/server/src/test/java/com/cloud/storage/StorageManagerImplTest.java @@ -164,7 +164,7 @@ public void testExtractUriParamsAsMapWithSolidFireUrl() { String sfUrl = "MVIP=1.2.3.4;SVIP=6.7.8.9;clusterAdminUsername=admin;" + "clusterAdminPassword=password;clusterDefaultMinIops=1000;" + "clusterDefaultMaxIops=2000;clusterDefaultBurstIopsPercentOfMaxIops=2"; - Map uriParams = storageManagerImpl.extractUriParamsAsMap(sfUrl, true); + Map uriParams = storageManagerImpl.extractUriParamsAsMap(sfUrl); Assert.assertTrue(MapUtils.isEmpty(uriParams)); } @@ -174,7 +174,7 @@ public void testExtractUriParamsAsMapWithNFSUrl() { String host = "HOST"; String path = "/PATH"; String sfUrl = String.format("%s://%s%s", scheme, host, path); - Map uriParams = storageManagerImpl.extractUriParamsAsMap(sfUrl, false); + Map uriParams = storageManagerImpl.extractUriParamsAsMap(sfUrl); Assert.assertTrue(MapUtils.isNotEmpty(uriParams)); Assert.assertEquals(scheme, uriParams.get("scheme")); Assert.assertEquals(host, uriParams.get("host")); From af9a284d02d2239b396192b996f6b274ac400b13 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Wed, 13 Dec 2023 23:11:48 +0530 Subject: [PATCH 4/6] fix Signed-off-by: Abhishek Kumar --- .../src/main/java/com/cloud/storage/StorageManagerImpl.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/com/cloud/storage/StorageManagerImpl.java b/server/src/main/java/com/cloud/storage/StorageManagerImpl.java index 410728d4f89e..88ca8af901db 100644 --- a/server/src/main/java/com/cloud/storage/StorageManagerImpl.java +++ b/server/src/main/java/com/cloud/storage/StorageManagerImpl.java @@ -824,7 +824,7 @@ public PrimaryDataStoreInfo createPool(CreateStoragePoolCmd cmd) throws Resource Long zoneId = cmd.getZoneId(); ScopeType scopeType = ScopeType.CLUSTER; - if ("file".equalsIgnoreCase(uriParams.get("scheme"))) { + if ("file".equals(uriParams.get("scheme"))) { scopeType = ScopeType.HOST; } String scope = cmd.getScope(); @@ -900,7 +900,7 @@ public PrimaryDataStoreInfo createPool(CreateStoragePoolCmd cmd) throws Resource DataStoreLifeCycle lifeCycle = storeProvider.getDataStoreLifeCycle(); DataStore store = null; try { - if (params.get("scheme").toString().equals("file")) { + if ("file".equals(uriParams.get("scheme"))) { store = createLocalStorage(params); } else { store = lifeCycle.initialize(params); From e8e0d312bf85b623113756de0ca4458c9a408b31 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Thu, 14 Dec 2023 11:36:25 +0530 Subject: [PATCH 5/6] checks Signed-off-by: Abhishek Kumar --- .../com/cloud/storage/StorageManagerImpl.java | 28 +++++++++++++------ .../cloud/storage/StorageManagerImplTest.java | 26 +++++++++++++++++ 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/server/src/main/java/com/cloud/storage/StorageManagerImpl.java b/server/src/main/java/com/cloud/storage/StorageManagerImpl.java index 88ca8af901db..e6a9b1f485f4 100644 --- a/server/src/main/java/com/cloud/storage/StorageManagerImpl.java +++ b/server/src/main/java/com/cloud/storage/StorageManagerImpl.java @@ -685,12 +685,19 @@ public boolean stop() { return true; } - private DataStore createLocalStorage(Map poolInfos) throws ConnectionException{ + protected DataStore createLocalStorage(Map poolInfos) throws ConnectionException{ Object existingUuid = poolInfos.get("uuid"); if( existingUuid == null ){ poolInfos.put("uuid", UUID.randomUUID().toString()); } - String hostAddress = poolInfos.get("host").toString(); + String hostAddress = poolInfos.get("host") == null ? null : poolInfos.get("host").toString(); + if (StringUtils.isEmpty(hostAddress)) { + throw new InvalidParameterValueException("Invalid host provided"); + } + String hostPath = poolInfos.get("hostPath") == null ? null : poolInfos.get("hostPath").toString(); + if (StringUtils.isEmpty(hostPath)) { + throw new InvalidParameterValueException("Invalid path provided"); + } Host host = _hostDao.findByName(hostAddress); if( host == null ) { @@ -709,8 +716,8 @@ private DataStore createLocalStorage(Map poolInfos) throws Conne StoragePoolInfo pInfo = new StoragePoolInfo(poolInfos.get("uuid").toString(), host.getPrivateIpAddress(), - poolInfos.get("hostPath").toString(), - poolInfos.get("hostPath").toString(), + hostPath, + hostPath, StoragePoolType.Filesystem, capacityBytes, 0, @@ -810,6 +817,7 @@ protected String createLocalStoragePoolName(Host host, StoragePoolInfo storagePo public PrimaryDataStoreInfo createPool(CreateStoragePoolCmd cmd) throws ResourceInUseException, IllegalArgumentException, UnknownHostException, ResourceUnavailableException { String providerName = cmd.getStorageProviderName(); Map uriParams = extractUriParamsAsMap(cmd.getUrl()); + boolean isFileScheme = "file".equals(uriParams.get("scheme")); DataStoreProvider storeProvider = _dataStoreProviderMgr.getDataStoreProvider(providerName); if (storeProvider == null) { @@ -824,7 +832,7 @@ public PrimaryDataStoreInfo createPool(CreateStoragePoolCmd cmd) throws Resource Long zoneId = cmd.getZoneId(); ScopeType scopeType = ScopeType.CLUSTER; - if ("file".equals(uriParams.get("scheme"))) { + if (isFileScheme) { scopeType = ScopeType.HOST; } String scope = cmd.getScope(); @@ -900,7 +908,7 @@ public PrimaryDataStoreInfo createPool(CreateStoragePoolCmd cmd) throws Resource DataStoreLifeCycle lifeCycle = storeProvider.getDataStoreLifeCycle(); DataStore store = null; try { - if ("file".equals(uriParams.get("scheme"))) { + if (isFileScheme) { store = createLocalStorage(params); } else { store = lifeCycle.initialize(params); @@ -935,7 +943,9 @@ protected Map extractUriParamsAsMap(String url) { try { uriInfo = UriUtils.getUriInfo(url); } catch (CloudRuntimeException cre) { - s_logger.debug(String.format("URI validation for url: %s failed, returning empty uri params", url)); + if (s_logger.isDebugEnabled()) { + s_logger.debug(String.format("URI validation for url: %s failed, returning empty uri params", url)); + } return uriParams; } @@ -943,7 +953,9 @@ protected Map extractUriParamsAsMap(String url) { String storageHost = uriInfo.getStorageHost(); String storagePath = uriInfo.getStoragePath(); if (scheme == null) { - s_logger.debug(String.format("Scheme for url: %s is not found, returning empty uri params", url)); + if (s_logger.isDebugEnabled()) { + s_logger.debug(String.format("Scheme for url: %s is not found, returning empty uri params", url)); + } return uriParams; } boolean isHostOrPathBlank = StringUtils.isAnyBlank(storagePath, storageHost); diff --git a/server/src/test/java/com/cloud/storage/StorageManagerImplTest.java b/server/src/test/java/com/cloud/storage/StorageManagerImplTest.java index c9a27bb64226..ba5f2baf9327 100644 --- a/server/src/test/java/com/cloud/storage/StorageManagerImplTest.java +++ b/server/src/test/java/com/cloud/storage/StorageManagerImplTest.java @@ -17,6 +17,8 @@ package com.cloud.storage; import com.cloud.agent.api.StoragePoolInfo; +import com.cloud.exception.ConnectionException; +import com.cloud.exception.InvalidParameterValueException; import com.cloud.host.Host; import com.cloud.storage.dao.VolumeDao; import com.cloud.vm.VMInstanceVO; @@ -34,6 +36,7 @@ import org.mockito.junit.MockitoJUnitRunner; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -180,4 +183,27 @@ public void testExtractUriParamsAsMapWithNFSUrl() { Assert.assertEquals(host, uriParams.get("host")); Assert.assertEquals(path, uriParams.get("hostPath")); } + + @Test(expected = InvalidParameterValueException.class) + public void testCreateLocalStorageHostFailure() { + Map test = new HashMap<>(); + test.put("host", null); + try { + storageManagerImpl.createLocalStorage(test); + } catch (ConnectionException e) { + throw new RuntimeException(e); + } + } + + @Test(expected = InvalidParameterValueException.class) + public void testCreateLocalStoragePathFailure() { + Map test = new HashMap<>(); + test.put("host", "HOST"); + test.put("hostPath", ""); + try { + storageManagerImpl.createLocalStorage(test); + } catch (ConnectionException e) { + throw new RuntimeException(e); + } + } } From cb2442e03eda0d4035bfa025eb25020db293a544 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Thu, 14 Dec 2023 18:59:12 +0530 Subject: [PATCH 6/6] refactor Signed-off-by: Abhishek Kumar --- .../com/cloud/storage/StorageManagerImpl.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/server/src/main/java/com/cloud/storage/StorageManagerImpl.java b/server/src/main/java/com/cloud/storage/StorageManagerImpl.java index e6a9b1f485f4..04889d0b2a83 100644 --- a/server/src/main/java/com/cloud/storage/StorageManagerImpl.java +++ b/server/src/main/java/com/cloud/storage/StorageManagerImpl.java @@ -685,19 +685,21 @@ public boolean stop() { return true; } + protected String getValidatedPareForLocalStorage(Object obj, String paramName) { + String result = obj == null ? null : obj.toString(); + if (StringUtils.isEmpty(result)) { + throw new InvalidParameterValueException(String.format("Invalid %s provided", paramName)); + } + return result; + } + protected DataStore createLocalStorage(Map poolInfos) throws ConnectionException{ Object existingUuid = poolInfos.get("uuid"); if( existingUuid == null ){ poolInfos.put("uuid", UUID.randomUUID().toString()); } - String hostAddress = poolInfos.get("host") == null ? null : poolInfos.get("host").toString(); - if (StringUtils.isEmpty(hostAddress)) { - throw new InvalidParameterValueException("Invalid host provided"); - } - String hostPath = poolInfos.get("hostPath") == null ? null : poolInfos.get("hostPath").toString(); - if (StringUtils.isEmpty(hostPath)) { - throw new InvalidParameterValueException("Invalid path provided"); - } + String hostAddress = getValidatedPareForLocalStorage(poolInfos.get("host"), "host"); + String hostPath = getValidatedPareForLocalStorage(poolInfos.get("hostPath"), "path"); Host host = _hostDao.findByName(hostAddress); if( host == null ) {