diff --git a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java index 42d37d5f220..024307d95b3 100644 --- a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java +++ b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java @@ -2358,7 +2358,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } } - protected HashMap getVmStats(final Connection conn, final GetVmStatsCommand cmd, final List vmUUIDs, final String hostGuid) { + public HashMap getVmStats(final Connection conn, final GetVmStatsCommand cmd, final List vmUUIDs, final String hostGuid) { final HashMap vmResponseMap = new HashMap(); for (final String vmUUID : vmUUIDs) { @@ -6674,7 +6674,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe return false; } - protected VM getVM(final Connection conn, final String vmName) { + public VM getVM(final Connection conn, final String vmName) { // Look up VMs with the specified name Set vms; try { diff --git a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapper.java b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapper.java index 98989ab006f..9c2f08dc0e0 100644 --- a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapper.java +++ b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapper.java @@ -24,6 +24,7 @@ import java.util.Hashtable; import com.cloud.agent.api.Answer; import com.cloud.agent.api.Command; import com.cloud.agent.api.GetHostStatsCommand; +import com.cloud.agent.api.GetVmStatsCommand; import com.cloud.agent.api.ReadyCommand; import com.cloud.agent.api.RebootRouterCommand; import com.cloud.agent.api.proxy.CheckConsoleProxyLoadCommand; @@ -57,6 +58,7 @@ public class CitrixRequestWrapper extends RequestWrapper { map.put(WatchConsoleProxyLoadCommand.class, new CitrixWatchConsoleProxyLoadCommandWrapper()); map.put(ReadyCommand.class, new CitrixReadyCommandWrapper()); map.put(GetHostStatsCommand.class, new GetHostStatsCommandWrapper()); + map.put(GetVmStatsCommand.class, new GetVmStatsCommandWrapper()); } public static CitrixRequestWrapper getInstance() { diff --git a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/GetVmStatsCommandWrapper.java b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/GetVmStatsCommandWrapper.java new file mode 100644 index 00000000000..1b4863a99ab --- /dev/null +++ b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/GetVmStatsCommandWrapper.java @@ -0,0 +1,82 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +package com.cloud.hypervisor.xenserver.resource.wrapper; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.log4j.Logger; +import org.apache.xmlrpc.XmlRpcException; + +import com.cloud.agent.api.Answer; +import com.cloud.agent.api.GetVmStatsAnswer; +import com.cloud.agent.api.GetVmStatsCommand; +import com.cloud.agent.api.VmStatsEntry; +import com.cloud.hypervisor.xenserver.resource.CitrixResourceBase; +import com.cloud.resource.CommandWrapper; +import com.xensource.xenapi.Connection; +import com.xensource.xenapi.Types.XenAPIException; +import com.xensource.xenapi.VM; + +public final class GetVmStatsCommandWrapper extends CommandWrapper { + + private static final Logger s_logger = Logger.getLogger(GetVmStatsCommandWrapper.class); + + @Override + public Answer execute(final GetVmStatsCommand command, final CitrixResourceBase citrixResourceBase) { + final Connection conn = citrixResourceBase.getConnection(); + final List vmNames = command.getVmNames(); + final HashMap vmStatsNameMap = new HashMap(); + if (vmNames.size() == 0) { + return new GetVmStatsAnswer(command, vmStatsNameMap); + } + try { + + // Determine the UUIDs of the requested VMs + final List vmUUIDs = new ArrayList(); + + for (final String vmName : vmNames) { + final VM vm = citrixResourceBase.getVM(conn, vmName); + vmUUIDs.add(vm.getUuid(conn)); + } + + final HashMap vmStatsUUIDMap = citrixResourceBase.getVmStats(conn, command, vmUUIDs, command.getHostGuid()); + if (vmStatsUUIDMap == null) { + return new GetVmStatsAnswer(command, vmStatsNameMap); + } + + for (final Map.Entryentry : vmStatsUUIDMap.entrySet()) { + vmStatsNameMap.put(vmNames.get(vmUUIDs.indexOf(entry.getKey())), entry.getValue()); + } + + return new GetVmStatsAnswer(command, vmStatsNameMap); + } catch (final XenAPIException e) { + final String msg = "Unable to get VM stats" + e.toString(); + s_logger.warn(msg, e); + return new GetVmStatsAnswer(command, vmStatsNameMap); + } catch (final XmlRpcException e) { + final String msg = "Unable to get VM stats" + e.getMessage(); + s_logger.warn(msg, e); + return new GetVmStatsAnswer(command, vmStatsNameMap); + } + } +} \ No newline at end of file diff --git a/plugins/hypervisors/xenserver/test/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapperTest.java b/plugins/hypervisors/xenserver/test/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapperTest.java index 027a5fdae7f..e54e809bac7 100644 --- a/plugins/hypervisors/xenserver/test/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapperTest.java +++ b/plugins/hypervisors/xenserver/test/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapperTest.java @@ -8,6 +8,8 @@ import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import java.util.ArrayList; + import org.apache.cloudstack.storage.datastore.db.StoragePoolVO; import org.junit.Test; import org.junit.runner.RunWith; @@ -17,6 +19,7 @@ import org.mockito.runners.MockitoJUnitRunner; import com.cloud.agent.api.Answer; import com.cloud.agent.api.Command; import com.cloud.agent.api.GetHostStatsCommand; +import com.cloud.agent.api.GetVmStatsCommand; import com.cloud.agent.api.ReadyCommand; import com.cloud.agent.api.RebootAnswer; import com.cloud.agent.api.RebootCommand; @@ -136,6 +139,18 @@ public class CitrixRequestWrapperTest { assertTrue(answer.getResult()); } + + @Test + public void testGetVmStatsCommandCommand() { + final GetVmStatsCommand statsCommand = new GetVmStatsCommand(new ArrayList(), null, null); + + final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance(); + assertNotNull(wrapper); + + final Answer answer = wrapper.execute(statsCommand, citrixResourceBase); + + assertTrue(answer.getResult()); + } } class NotAValidCommand extends Command {