From add42777203999e99c310005e02d8dd31fff2f95 Mon Sep 17 00:00:00 2001 From: wilderrodrigues Date: Thu, 21 May 2015 13:59:49 +0200 Subject: [PATCH] CLOUDSTACK-8502 Added CitrixStorageSubSystemCommandWrapper to complete the CitrixResourceBase refactor. - All 101 unit tests are green --- .../com/cloud/resource/RequestWrapper.java | 4 ++ .../resource/CitrixResourceBase.java | 14 +++---- .../CitrixStorageSubSystemCommandWrapper.java | 38 +++++++++++++++++++ .../wrapper/CitrixRequestWrapperTest.java | 22 +++++++++++ 4 files changed, 69 insertions(+), 9 deletions(-) create mode 100644 plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixStorageSubSystemCommandWrapper.java diff --git a/core/src/com/cloud/resource/RequestWrapper.java b/core/src/com/cloud/resource/RequestWrapper.java index 31388d64dec..4e754d60a29 100644 --- a/core/src/com/cloud/resource/RequestWrapper.java +++ b/core/src/com/cloud/resource/RequestWrapper.java @@ -75,6 +75,8 @@ public abstract class RequestWrapper { commandWrapper = resourceCommands.get(commandClass2); keepCommandClass = commandClass2; + } catch (final ClassCastException e) { + throw new NullPointerException("No key found for '" + keepCommandClass.getClass() + "' in the Map!"); } catch (final NullPointerException e) { // Will now traverse all the resource hierarchy. Returning null // is not a problem. @@ -108,6 +110,8 @@ public abstract class RequestWrapper { keepResourceClass = resourceClass2; commandWrapper = retrieveCommands(command.getClass(), resourceCommands2); + } catch (final ClassCastException e) { + throw new NullPointerException("No key found for '" + command.getClass() + "' in the Map!"); } catch (final NullPointerException e) { throw e; } 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 2b3035c7570..6aaaf0e737b 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 @@ -46,7 +46,6 @@ import javax.naming.ConfigurationException; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; -import org.apache.cloudstack.storage.command.StorageSubSystemCommand; import org.apache.cloudstack.storage.to.TemplateObjectTO; import org.apache.cloudstack.storage.to.VolumeObjectTO; import org.apache.log4j.Logger; @@ -717,7 +716,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe if (record.isControlDomain || record.isASnapshot || record.isATemplate) { continue; // Skip DOM0 } - String platform = StringUtils.mapToString(record.platform); + final String platform = StringUtils.mapToString(record.platform); if (platform.isEmpty()) { continue; //Skip if platform is null } @@ -1642,13 +1641,6 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe @Override public Answer executeRequest(final Command cmd) { - - // We need this one because the StorageSubSystemCommand is from another - // hierarchy. - if (cmd instanceof StorageSubSystemCommand) { - return storageHandler.handleStorageCommands((StorageSubSystemCommand) cmd); - } - final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance(); try { return wrapper.execute(cmd, this); @@ -2012,6 +2004,10 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe return _migratewait; } + public StorageSubsystemCommandHandler getStorageHandler() { + return storageHandler; + } + protected boolean getHostInfo(final Connection conn) throws IllegalArgumentException { try { final Host myself = Host.getByUuid(conn, _host.getUuid()); diff --git a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixStorageSubSystemCommandWrapper.java b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixStorageSubSystemCommandWrapper.java new file mode 100644 index 00000000000..c14818ed620 --- /dev/null +++ b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixStorageSubSystemCommandWrapper.java @@ -0,0 +1,38 @@ +// +// 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 org.apache.cloudstack.storage.command.StorageSubSystemCommand; + +import com.cloud.agent.api.Answer; +import com.cloud.hypervisor.xenserver.resource.CitrixResourceBase; +import com.cloud.resource.CommandWrapper; +import com.cloud.resource.ResourceWrapper; +import com.cloud.storage.resource.StorageSubsystemCommandHandler; + +@ResourceWrapper(handles = StorageSubSystemCommand.class) +public final class CitrixStorageSubSystemCommandWrapper extends CommandWrapper { + + @Override + public Answer execute(final StorageSubSystemCommand command, final CitrixResourceBase citrixResourceBase) { + final StorageSubsystemCommandHandler handler = citrixResourceBase.getStorageHandler(); + return handler.handleStorageCommands(command); + } +} \ 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 6a1be8c27b6..8d66c6597a4 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 @@ -33,6 +33,8 @@ import java.util.Hashtable; import java.util.List; import java.util.Map; +import org.apache.cloudstack.storage.command.AttachAnswer; +import org.apache.cloudstack.storage.command.AttachCommand; import org.apache.cloudstack.storage.datastore.db.StoragePoolVO; import org.apache.cloudstack.storage.to.VolumeObjectTO; import org.apache.xmlrpc.XmlRpcException; @@ -109,6 +111,7 @@ import com.cloud.agent.api.storage.DestroyCommand; import com.cloud.agent.api.storage.PrimaryStorageDownloadCommand; import com.cloud.agent.api.storage.ResizeVolumeCommand; import com.cloud.agent.api.to.DataStoreTO; +import com.cloud.agent.api.to.DiskTO; import com.cloud.agent.api.to.IpAddressTO; import com.cloud.agent.api.to.NicTO; import com.cloud.agent.api.to.StorageFilerTO; @@ -123,6 +126,7 @@ import com.cloud.network.PhysicalNetworkSetupInfo; import com.cloud.storage.Storage.ImageFormat; import com.cloud.storage.Storage.StoragePoolType; import com.cloud.storage.VMTemplateStorageResourceAssoc; +import com.cloud.storage.resource.StorageSubsystemCommandHandler; import com.cloud.vm.DiskProfile; import com.cloud.vm.VirtualMachine; import com.xensource.xenapi.Connection; @@ -1807,6 +1811,24 @@ public class CitrixRequestWrapperTest { // Requires more testing, but the VirtualResourceRouting is quite big. assertNull(answer); } + + @Test + public void testStorageSubSystemCommand() { + final DiskTO disk = Mockito.mock(DiskTO.class); + final String vmName = "Test"; + final AttachCommand command = new AttachCommand(disk, vmName); + + final StorageSubsystemCommandHandler handler = Mockito.mock(StorageSubsystemCommandHandler.class); + when(citrixResourceBase.getStorageHandler()).thenReturn(handler); + + when(handler.handleStorageCommands(command)).thenReturn(new AttachAnswer(disk)); + + final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance(); + assertNotNull(wrapper); + + final Answer answer = wrapper.execute(command, citrixResourceBase); + assertTrue(answer.getResult()); + } } class NotAValidCommand extends Command {