1616// under the License.
1717package org .apache .cloudstack .hypervisor .xenserver ;
1818
19- import java .util .HashMap ;
2019import java .util .Map ;
2120
2221import org .apache .log4j .Logger ;
2322import org .apache .xmlrpc .XmlRpcException ;
2423
2524import com .cloud .exception .InvalidParameterValueException ;
25+ import com .cloud .utils .Pair ;
2626import com .cloud .utils .exception .CloudRuntimeException ;
2727import com .xensource .xenapi .Connection ;
2828import com .xensource .xenapi .Types ;
@@ -35,16 +35,22 @@ public static void setExtraConfigurationToVm(Connection conn, VM.Record vmr, VM
3535 Map <String , Object > recordMap = vmr .toMap ();
3636 for (String key : extraConfig .keySet ()) {
3737 String cfg = extraConfig .get (key );
38- Map <String , String > configParams = prepareKeyValuePair (cfg );
38+ // cfg is either param=value or map-param:key=value
39+ Pair <String , String > configParam = prepareKeyValuePair (cfg );
40+ if (configParam == null ) {
41+ LOG .warn ("Invalid extra config passed: " + cfg );
42+ continue ;
43+ }
3944
40- // paramKey is either param or param:key for map parameters
41- String paramKey = configParams . keySet (). toString (). replaceAll ( "[ \\ [ \\ ]]" , "" );
42- String paramValue = configParams . get ( paramKey );
45+ // paramKey is either param or map- param:key for map parameters
46+ String paramKey = configParam . first ( );
47+ String paramValue = configParam . second ( );
4348
44- //Map params
4549 if (paramKey .contains (":" )) {
50+ // Map params - paramKey is map-param:key
4651 applyConfigWithNestedKeyValue (conn , vm , recordMap , paramKey , paramValue );
4752 } else {
53+ // Params - paramKey is param
4854 applyConfigWithKeyValue (conn , vm , recordMap , paramKey , paramValue );
4955 }
5056 }
@@ -58,6 +64,7 @@ private static boolean isValidOperation(Map<String, Object> recordMap, String ac
5864 * Nested keys contain ":" between the paramKey and need to split into operation param and key
5965 * */
6066 private static void applyConfigWithNestedKeyValue (Connection conn , VM vm , Map <String , Object > recordMap , String paramKey , String paramValue ) {
67+ // paramKey is map-param:key
6168 int i = paramKey .indexOf (":" );
6269 String actualParam = paramKey .substring (0 , i );
6370 String keyName = paramKey .substring (i + 1 );
@@ -68,12 +75,13 @@ private static void applyConfigWithNestedKeyValue(Connection conn, VM vm, Map<St
6875 }
6976
7077 try {
78+ // map-param param with '_'
7179 switch (actualParam ) {
7280 case "VCPUs_params" :
7381 vm .addToVCPUsParams (conn , keyName , paramValue );
7482 break ;
7583 case "platform" :
76- vm .addToOtherConfig (conn , keyName , paramValue );
84+ vm .addToPlatform (conn , keyName , paramValue );
7785 break ;
7886 case "HVM_boot_params" :
7987 vm .addToHVMBootParams (conn , keyName , paramValue );
@@ -101,6 +109,7 @@ private static void applyConfigWithKeyValue(Connection conn, VM vm, Map<String,
101109 }
102110
103111 try {
112+ // param with '_'
104113 switch (paramKey ) {
105114 case "HVM_boot_policy" :
106115 vm .setHVMBootPolicy (conn , paramValue );
@@ -144,7 +153,7 @@ private static void applyConfigWithKeyValue(Connection conn, VM vm, Map<String,
144153 case "VCPUs_at_startup" :
145154 vm .setVCPUsAtStartup (conn , Long .valueOf (paramValue ));
146155 break ;
147- case "is-a-template " :
156+ case "is_a_template " :
148157 vm .setIsATemplate (conn , Boolean .valueOf (paramValue ));
149158 break ;
150159 case "memory_static_max" :
@@ -169,12 +178,28 @@ private static void applyConfigWithKeyValue(Connection conn, VM vm, Map<String,
169178 }
170179 }
171180
172- private static Map <String , String > prepareKeyValuePair (String cfg ) {
173- Map < String , String > configKeyPair = new HashMap <>();
181+ protected static Pair <String , String > prepareKeyValuePair (String cfg ) {
182+ // cfg is either param=value or map-param:key=value
174183 int indexOfEqualSign = cfg .indexOf ("=" );
175- String key = cfg .substring (0 , indexOfEqualSign ).replace ("-" , "_" );
184+ if (indexOfEqualSign <= 0 ) {
185+ return null ;
186+ }
187+
188+ String key ;
189+ // Replace '-' with '_' in param / map-param only
190+ if (cfg .contains (":" )) {
191+ int indexOfColon = cfg .indexOf (":" );
192+ if (indexOfColon <= 0 || indexOfEqualSign < indexOfColon ) {
193+ return null ;
194+ }
195+ String mapParam = cfg .substring (0 , indexOfColon ).replace ("-" , "_" );
196+ String paramKey = cfg .substring (indexOfColon + 1 , indexOfEqualSign );
197+ key = mapParam + ":" + paramKey ;
198+ } else {
199+ key = cfg .substring (0 , indexOfEqualSign ).replace ("-" , "_" );
200+ }
201+
176202 String value = cfg .substring (indexOfEqualSign + 1 );
177- configKeyPair .put (key , value );
178- return configKeyPair ;
203+ return new Pair <>(key , value );
179204 }
180205}
0 commit comments