|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package com.cloud.storage.resource; |
| 20 | + |
| 21 | +import org.apache.cloudstack.storage.command.AttachCommand; |
| 22 | +import org.apache.cloudstack.storage.command.CopyCommand; |
| 23 | +import org.apache.cloudstack.storage.command.CreateObjectAnswer; |
| 24 | +import org.apache.cloudstack.storage.command.CreateObjectCommand; |
| 25 | +import org.apache.cloudstack.storage.command.DeleteCommand; |
| 26 | +import org.apache.cloudstack.storage.command.DettachCommand; |
| 27 | +import org.apache.cloudstack.storage.command.StorageSubSystemCommand; |
| 28 | +import org.apache.log4j.Logger; |
| 29 | + |
| 30 | +import com.cloud.agent.api.Answer; |
| 31 | +import com.cloud.agent.api.Command; |
| 32 | +import com.cloud.agent.api.to.DataObjectType; |
| 33 | +import com.cloud.agent.api.to.DataStoreTO; |
| 34 | +import com.cloud.agent.api.to.DataTO; |
| 35 | +import com.cloud.agent.api.to.DiskTO; |
| 36 | +import com.cloud.agent.api.to.NfsTO; |
| 37 | +import com.cloud.storage.DataStoreRole; |
| 38 | +import com.cloud.storage.Volume; |
| 39 | + |
| 40 | +public class StorageSubsystemCommandHandlerBase implements StorageSubsystemCommandHandler { |
| 41 | + private static final Logger s_logger = Logger.getLogger(StorageSubsystemCommandHandlerBase.class); |
| 42 | + private StorageProcessor processor; |
| 43 | + public StorageSubsystemCommandHandlerBase(StorageProcessor processor) { |
| 44 | + this.processor = processor; |
| 45 | + } |
| 46 | + @Override |
| 47 | + public Answer handleStorageCommands(StorageSubSystemCommand command) { |
| 48 | + if (command instanceof CopyCommand) { |
| 49 | + return this.execute((CopyCommand)command); |
| 50 | + } else if (command instanceof CreateObjectCommand) { |
| 51 | + return execute((CreateObjectCommand) command); |
| 52 | + } else if (command instanceof DeleteCommand) { |
| 53 | + return execute((DeleteCommand)command); |
| 54 | + } else if (command instanceof AttachCommand) { |
| 55 | + return execute((AttachCommand)command); |
| 56 | + } else if (command instanceof DettachCommand) { |
| 57 | + return execute((DettachCommand)command); |
| 58 | + } |
| 59 | + return new Answer((Command)command, false, "not implemented yet"); |
| 60 | + } |
| 61 | + |
| 62 | + protected Answer execute(CopyCommand cmd) { |
| 63 | + DataTO srcData = cmd.getSrcTO(); |
| 64 | + DataTO destData = cmd.getDestTO(); |
| 65 | + DataStoreTO srcDataStore = srcData.getDataStore(); |
| 66 | + DataStoreTO destDataStore = destData.getDataStore(); |
| 67 | + |
| 68 | + if ((srcData.getObjectType() == DataObjectType.TEMPLATE) && (srcDataStore instanceof NfsTO) && (destData.getDataStore().getRole() == DataStoreRole.Primary)) { |
| 69 | + //copy template to primary storage |
| 70 | + return processor.copyTemplateToPrimaryStorage(cmd); |
| 71 | + } else if (srcData.getObjectType() == DataObjectType.TEMPLATE && srcDataStore.getRole() == DataStoreRole.Primary && destDataStore.getRole() == DataStoreRole.Primary) { |
| 72 | + //clone template to a volume |
| 73 | + return processor.cloneVolumeFromBaseTemplate(cmd); |
| 74 | + } else if (srcData.getObjectType() == DataObjectType.VOLUME && (srcData.getDataStore().getRole() == DataStoreRole.ImageCache || srcDataStore.getRole() == DataStoreRole.Image)) { |
| 75 | + //copy volume from image cache to primary |
| 76 | + return processor.copyVolumeFromImageCacheToPrimary(cmd); |
| 77 | + } else if (srcData.getObjectType() == DataObjectType.VOLUME && srcData.getDataStore().getRole() == DataStoreRole.Primary) { |
| 78 | + if (destData.getObjectType() == DataObjectType.VOLUME) { |
| 79 | + return processor.copyVolumeFromPrimaryToSecondary(cmd); |
| 80 | + } else if (destData.getObjectType() == DataObjectType.TEMPLATE) { |
| 81 | + return processor.createTemplateFromVolume(cmd); |
| 82 | + } |
| 83 | + } else if (srcData.getObjectType() == DataObjectType.SNAPSHOT && srcData.getDataStore().getRole() == DataStoreRole.Primary) { |
| 84 | + return processor.backupSnasphot(cmd); |
| 85 | + } |
| 86 | + |
| 87 | + return new Answer(cmd, false, "not implemented yet"); |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + protected Answer execute(CreateObjectCommand cmd) { |
| 92 | + DataTO data = cmd.getData(); |
| 93 | + try { |
| 94 | + if (data.getObjectType() == DataObjectType.VOLUME) { |
| 95 | + return processor.createVolume(cmd); |
| 96 | + } else if (data.getObjectType() == DataObjectType.SNAPSHOT) { |
| 97 | + return processor.createSnapshot(cmd); |
| 98 | + } |
| 99 | + return new CreateObjectAnswer("not supported type"); |
| 100 | + } catch (Exception e) { |
| 101 | + s_logger.debug("Failed to create object: " + data.getObjectType() + ": " + e.toString()); |
| 102 | + return new CreateObjectAnswer(e.toString()); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + protected Answer execute(DeleteCommand cmd) { |
| 107 | + DataTO data = cmd.getData(); |
| 108 | + Answer answer = null; |
| 109 | + if (data.getObjectType() == DataObjectType.VOLUME) { |
| 110 | + answer = processor.deleteVolume(cmd); |
| 111 | + } else { |
| 112 | + answer = new Answer(cmd, false, "unsupported type"); |
| 113 | + } |
| 114 | + |
| 115 | + return answer; |
| 116 | + } |
| 117 | + |
| 118 | + protected Answer execute(AttachCommand cmd) { |
| 119 | + DiskTO disk = cmd.getDisk(); |
| 120 | + if (disk.getType() == Volume.Type.ISO) { |
| 121 | + return processor.attachIso(cmd); |
| 122 | + } else { |
| 123 | + return processor.attachVolume(cmd); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + protected Answer execute(DettachCommand cmd) { |
| 128 | + DiskTO disk = cmd.getDisk(); |
| 129 | + if (disk.getType() == Volume.Type.ISO) { |
| 130 | + return processor.dettachIso(cmd); |
| 131 | + } else { |
| 132 | + return processor.dettachVolume(cmd); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments