@@ -1171,7 +1171,7 @@ private String validateConfigurationValue(final String name, String value, final
11711171 if (type .equals (Integer .class ) && NetworkModel .MACIdentifier .key ().equalsIgnoreCase (name )) {
11721172 try {
11731173 final int val = Integer .parseInt (value );
1174- //The value needs to be between 0 to 255 because the mac generation needs a value of 8 bit
1174+ //The value need to be between 0 to 255 because the mac generation needs a value of 8 bit
11751175 //0 value is considered as disable.
11761176 if (val < 0 || val > 255 ){
11771177 throw new InvalidParameterValueException (name +" value should be between 0 and 255. 0 value will disable this feature" );
@@ -1222,7 +1222,7 @@ private String validateConfigurationValue(final String name, String value, final
12221222 }
12231223 }
12241224
1225- if ( c == null ) {
1225+ if ( configuration == null ) {
12261226 //range validation has to be done per case basis, for now
12271227 //return in case of Configkey parameters
12281228 return null ;
@@ -1234,70 +1234,121 @@ private String validateConfigurationValue(final String name, String value, final
12341234 }
12351235
12361236 if (type .equals (String .class )) {
1237- if (range .equals ("privateip" )) {
1238- try {
1239- if (!NetUtils .isSiteLocalAddress (value )) {
1240- s_logger .error ("privateip range " + value + " is not a site local address for configuration variable " + name );
1241- return "Please enter a site local IP address." ;
1242- }
1243- } catch (final NullPointerException e ) {
1244- s_logger .error ("Error parsing ip address for " + name );
1245- throw new InvalidParameterValueException ("Error parsing ip address" );
1246- }
1247- } else if (range .equals ("netmask" )) {
1248- if (!NetUtils .isValidIp4Netmask (value )) {
1249- s_logger .error ("netmask " + value + " is not a valid net mask for configuration variable " + name );
1250- return "Please enter a valid netmask." ;
1251- }
1252- } else if (range .equals ("hypervisorList" )) {
1253- final String [] hypervisors = value .split ("," );
1254- if (hypervisors == null ) {
1255- return "Please enter hypervisor list, separated by comma" ;
1256- }
1257- for (final String hypervisor : hypervisors ) {
1258- if (HypervisorType .getType (hypervisor ) == HypervisorType .Any || HypervisorType .getType (hypervisor ) == HypervisorType .None ) {
1259- return "Please enter a valid hypervisor type" ;
1260- }
1261- }
1262- } else if (range .equalsIgnoreCase ("instanceName" )) {
1263- if (!NetUtils .verifyInstanceName (value )) {
1264- return "Instance name can not contain hyphen, space or plus sign" ;
1265- }
1266- } else if (range .equalsIgnoreCase ("domainName" )) {
1267- String domainName = value ;
1268- if (value .startsWith ("*" )) {
1269- domainName = value .substring (2 ); //skip the "*."
1270- }
1271- //max length for FQDN is 253 + 2, code adds xxx-xxx-xxx-xxx to domain name when creating URL
1272- if (domainName .length () >= 238 || !domainName .matches (DOMAIN_NAME_PATTERN )) {
1273- return "Please enter a valid string for domain name, prefixed with '*.' if applicable" ;
1274- }
1275- } else if (range .equals ("routes" )) {
1276- final String [] routes = value .split ("," );
1277- for (final String route : routes ) {
1278- if (route != null ) {
1279- final String routeToVerify = route .trim ();
1280- if (!NetUtils .isValidIp4Cidr (routeToVerify )) {
1281- throw new InvalidParameterValueException ("Invalid value for route: " + route + " in deny list. Valid format is list"
1282- + " of cidrs separated by coma. Example: 10.1.1.0/24,192.168.0.0/24" );
1283- }
1284- }
1285- }
1286- } else {
1287- final String [] options = range .split ("," );
1288- for (final String option : options ) {
1289- if (option .trim ().equalsIgnoreCase (value )) {
1290- return null ;
1291- }
1292- }
1293- s_logger .error ("configuration value for " + name + " is invalid" );
1294- return "Please enter : " + range ;
1237+ return validateIfStringValueIsInRange (name , value , range );
1238+ } else if (type .equals (Integer .class )) {
1239+ return validateIfIntValueIsInRange (name , value , range [0 ]);
1240+ }
1241+ return String .format ("Invalid value for configuration [%s]." , name );
1242+ }
1243+
1244+ protected String validateIfIntValueIsInRange (String name , String value , String range ) {
1245+ final String [] options = range .split ("-" );
1246+ if (options .length != 2 ) {
1247+ String msg = String .format ("Configuration range [%s] for [%s] is invalid." , range , name );
1248+ s_logger .error (msg );
1249+ return msg ;
1250+ }
1251+ final int min = Integer .parseInt (options [0 ]);
1252+ final int max = Integer .parseInt (options [1 ]);
1253+ final int val = Integer .parseInt (value );
1254+ if (val < min || val > max ) {
1255+ s_logger .error (String .format ("Invalid value for configuration [%s]. Please enter a value in the range [%s]." , name , range ));
1256+ return String .format ("The provided value is not valid for this configuration. Please enter an integer in the range: [%s]" , range );
1257+ }
1258+ return null ;
1259+ }
1260+
1261+
1262+ private String validateIfStringValueIsInRange (String name , String value , String ... range ) {
1263+ List <String > message = new ArrayList <String >();
1264+ String errMessage = "" ;
1265+ for (String rangeOption : range ) {
1266+ switch (rangeOption ) {
1267+ case "privateip" :
1268+ errMessage = validateRangePrivateIp (name , value );
1269+ break ;
1270+ case "hypervisorList" :
1271+ errMessage = validateRangeHypervisorList (value );
1272+ break ;
1273+ case "instanceName" :
1274+ errMessage = validateRangeInstanceName (value );
1275+ break ;
1276+ case "domainName" :
1277+ errMessage = validateRangeDomainName (value );
1278+ break ;
1279+ default :
1280+ errMessage = validateRangeOther (name , value , rangeOption );
1281+ }
1282+ if (StringUtils .isEmpty (errMessage )) {
1283+ return null ;
1284+ }
1285+ message .add (errMessage );
1286+ }
1287+ if (message .size () == 1 ) {
1288+ return String .format ("The provided value is not %s." , message .get (0 ));
1289+ }
1290+ return String .format ("The provided value is neither %s." , String .join (" NOR " , message ));
1291+ }
12951292
1293+ protected String validateRangePrivateIp (String name , String value ) {
1294+ try {
1295+ if (NetUtils .isSiteLocalAddress (value )) {
1296+ return null ;
1297+ }
1298+ s_logger .error (String .format ("Value [%s] is not a valid private IP range for configuration [%s]." , value , name ));
1299+ } catch (final NullPointerException e ) {
1300+ s_logger .error (String .format ("Error while parsing IP address for [%s]." , name ));
1301+ }
1302+ return "a valid site local IP address" ;
1303+ }
1304+
1305+ /**
1306+ * Valid values are XenServer, KVM, VMware, Hyperv, VirtualBox, Parralels, BareMetal, Simulator, Ovm, Ovm3, LXC
1307+ * Inputting "Any" will return the hypervisor type Any, other inputs will result in the hypervisor type none. Both of these are invalid values and will return an error message.
1308+ */
1309+ protected String validateRangeHypervisorList (String value ) {
1310+ final String [] hypervisors = value .split ("," );
1311+ for (final String hypervisor : hypervisors ) {
1312+ if (HypervisorType .getType (hypervisor ) == HypervisorType .Any || HypervisorType .getType (hypervisor ) == HypervisorType .None ) {
1313+ return "a valid hypervisor type" ;
12961314 }
12971315 }
12981316 return null ;
12991317 }
13001318
1319+ protected String validateRangeInstanceName (String value ) {
1320+ if (NetUtils .verifyInstanceName (value )) {
1321+ return null ;
1322+ }
1323+ return "a valid instance name (instance names cannot contain hyphen, space or plus sign)" ;
1324+ }
1325+
1326+ /**
1327+ * max length for FQDN is 253 + 2, code adds xxx-xxx-xxx-xxx to domain name when creating URL.
1328+ * If it starts with "*.", these two symbols are ignored and do not count towards the character limit.
1329+ */
1330+ protected String validateRangeDomainName (String value ) {
1331+ String domainName = value ;
1332+ if (value .startsWith ("*" )) {
1333+ domainName = value .substring (2 ); //skip the "*."
1334+ }
1335+ if (domainName .length () >= 238 || !domainName .matches (DOMAIN_NAME_PATTERN )) {
1336+ return "a valid domain name" ;
1337+ }
1338+ return null ;
1339+ }
1340+
1341+ protected String validateRangeOther (String name , String value , String rangeOption ) {
1342+ final String [] options = rangeOption .split ("," );
1343+ for (final String option : options ) {
1344+ if (option .trim ().equalsIgnoreCase (value )) {
1345+ return null ;
1346+ }
1347+ }
1348+ s_logger .error (String .format ("Invalid value for configuration [%s]." , name ));
1349+ return String .format ("a valid value for this configuration (Options are: [%s])" , rangeOption );
1350+ }
1351+
13011352
13021353 private boolean podHasAllocatedPrivateIPs (final long podId ) {
13031354 final HostPodVO pod = _podDao .findById (podId );
0 commit comments