From a129fe4e66202486145b1a61cc64e8573f59980c Mon Sep 17 00:00:00 2001 From: Edison Su Date: Fri, 4 Oct 2013 14:17:49 -0700 Subject: [PATCH] add missing files --- .../user/snapshot/RevertSnapshotCmd.java | 93 +++++++++++++++++++ .../storage/command/ForgetObjectCmd.java | 37 ++++++++ .../command/IntroduceObjectAnswer.java | 33 +++++++ .../storage/command/IntroduceObjectCmd.java | 38 ++++++++ .../storage/helper/HypervisorHelper.java | 31 +++++++ .../storage/helper/HypervisorHelperImpl.java | 77 +++++++++++++++ 6 files changed, 309 insertions(+) create mode 100644 api/src/org/apache/cloudstack/api/command/user/snapshot/RevertSnapshotCmd.java create mode 100644 core/src/org/apache/cloudstack/storage/command/ForgetObjectCmd.java create mode 100644 core/src/org/apache/cloudstack/storage/command/IntroduceObjectAnswer.java create mode 100644 core/src/org/apache/cloudstack/storage/command/IntroduceObjectCmd.java create mode 100644 engine/storage/src/org/apache/cloudstack/storage/helper/HypervisorHelper.java create mode 100644 engine/storage/src/org/apache/cloudstack/storage/helper/HypervisorHelperImpl.java diff --git a/api/src/org/apache/cloudstack/api/command/user/snapshot/RevertSnapshotCmd.java b/api/src/org/apache/cloudstack/api/command/user/snapshot/RevertSnapshotCmd.java new file mode 100644 index 00000000000..946eebd180f --- /dev/null +++ b/api/src/org/apache/cloudstack/api/command/user/snapshot/RevertSnapshotCmd.java @@ -0,0 +1,93 @@ +/* + * 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 org.apache.cloudstack.api.command.user.snapshot; + +import com.cloud.event.EventTypes; +import com.cloud.storage.Snapshot; +import com.cloud.user.Account; +import org.apache.cloudstack.api.APICommand; +import org.apache.cloudstack.api.ApiCommandJobType; +import org.apache.cloudstack.api.ApiConstants; +import org.apache.cloudstack.api.ApiErrorCode; +import org.apache.cloudstack.api.BaseAsyncCmd; +import org.apache.cloudstack.api.BaseCmd; +import org.apache.cloudstack.api.Parameter; +import org.apache.cloudstack.api.ServerApiException; +import org.apache.cloudstack.api.response.SnapshotResponse; +import org.apache.cloudstack.api.response.SuccessResponse; +import org.apache.cloudstack.context.CallContext; + +@APICommand(name = "RevertSnapshot", description = "revert a volume snapshot.", responseObject = SnapshotResponse.class) +public class RevertSnapshotCmd extends BaseAsyncCmd { + private static final String s_name = "revertsnapshotresponse"; + @Parameter(name= ApiConstants.ID, type= BaseCmd.CommandType.UUID, entityType = SnapshotResponse.class, + required=true, description="The ID of the snapshot") + private Long id; + + public Long getId() { + return id; + } + + + @Override + public String getCommandName() { + return s_name; + } + + @Override + public long getEntityOwnerId() { + Snapshot snapshot = _entityMgr.findById(Snapshot.class, getId()); + if (snapshot != null) { + return snapshot.getAccountId(); + } + + return Account.ACCOUNT_ID_SYSTEM; // no account info given, parent this command to SYSTEM so ERROR events are tracked + } + + @Override + public String getEventType() { + return EventTypes.EVENT_SNAPSHOT_REVERT; + } + + @Override + public String getEventDescription() { + return "revert snapshot: " + getId(); + } + + public ApiCommandJobType getInstanceType() { + return ApiCommandJobType.Snapshot; + } + + public Long getInstanceId() { + return getId(); + } + + @Override + public void execute(){ + CallContext.current().setEventDetails("Snapshot Id: "+getId()); + boolean result = _snapshotService.revertSnapshot(getId()); + if (result) { + SuccessResponse response = new SuccessResponse(getCommandName()); + response.setResponseName(getCommandName()); + this.setResponseObject(response); + } else { + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to revert snapshot"); + } + } +} diff --git a/core/src/org/apache/cloudstack/storage/command/ForgetObjectCmd.java b/core/src/org/apache/cloudstack/storage/command/ForgetObjectCmd.java new file mode 100644 index 00000000000..58fb7802019 --- /dev/null +++ b/core/src/org/apache/cloudstack/storage/command/ForgetObjectCmd.java @@ -0,0 +1,37 @@ +/* + * 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 org.apache.cloudstack.storage.command; + +import com.cloud.agent.api.Command; +import com.cloud.agent.api.to.DataTO; + +public class ForgetObjectCmd extends Command implements StorageSubSystemCommand { + private DataTO dataTO; + public ForgetObjectCmd(DataTO data) { + this.dataTO = data; + } + + public DataTO getDataTO() { + return this.dataTO; + } + @Override + public boolean executeInSequence() { + return false; + } +} diff --git a/core/src/org/apache/cloudstack/storage/command/IntroduceObjectAnswer.java b/core/src/org/apache/cloudstack/storage/command/IntroduceObjectAnswer.java new file mode 100644 index 00000000000..03c74b8aaa0 --- /dev/null +++ b/core/src/org/apache/cloudstack/storage/command/IntroduceObjectAnswer.java @@ -0,0 +1,33 @@ +/* + * 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 org.apache.cloudstack.storage.command; + +import com.cloud.agent.api.Answer; +import com.cloud.agent.api.to.DataTO; + +public class IntroduceObjectAnswer extends Answer { + private DataTO dataTO; + public IntroduceObjectAnswer(DataTO dataTO) { + this.dataTO = dataTO; + } + + public DataTO getDataTO() { + return this.dataTO; + } +} diff --git a/core/src/org/apache/cloudstack/storage/command/IntroduceObjectCmd.java b/core/src/org/apache/cloudstack/storage/command/IntroduceObjectCmd.java new file mode 100644 index 00000000000..1aabed2d279 --- /dev/null +++ b/core/src/org/apache/cloudstack/storage/command/IntroduceObjectCmd.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 org.apache.cloudstack.storage.command; + +import com.cloud.agent.api.Command; +import com.cloud.agent.api.to.DataTO; + +public class IntroduceObjectCmd extends Command implements StorageSubSystemCommand { + private DataTO dataTO; + public IntroduceObjectCmd(DataTO dataTO) { + this.dataTO = dataTO; + } + + public DataTO getDataTO() { + return this.dataTO; + } + + @Override + public boolean executeInSequence() { + return false; + } +} diff --git a/engine/storage/src/org/apache/cloudstack/storage/helper/HypervisorHelper.java b/engine/storage/src/org/apache/cloudstack/storage/helper/HypervisorHelper.java new file mode 100644 index 00000000000..40ced1d832c --- /dev/null +++ b/engine/storage/src/org/apache/cloudstack/storage/helper/HypervisorHelper.java @@ -0,0 +1,31 @@ +/* + * 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 org.apache.cloudstack.storage.helper; + +import com.cloud.agent.api.to.DataTO; +import org.apache.cloudstack.engine.subsystem.api.storage.DataObject; +import org.apache.cloudstack.engine.subsystem.api.storage.Scope; +import org.apache.cloudstack.storage.to.SnapshotObjectTO; + +public interface HypervisorHelper { + DataTO introduceObject(DataTO object, Scope scope, Long storeId); + boolean forgetObject(DataTO object, Scope scope, Long storeId); + SnapshotObjectTO takeSnapshot(SnapshotObjectTO snapshotObjectTO, Scope scope); + boolean revertSnapshot(SnapshotObjectTO snapshotObjectTO, Scope scope); +} diff --git a/engine/storage/src/org/apache/cloudstack/storage/helper/HypervisorHelperImpl.java b/engine/storage/src/org/apache/cloudstack/storage/helper/HypervisorHelperImpl.java new file mode 100644 index 00000000000..81e6f7c69c5 --- /dev/null +++ b/engine/storage/src/org/apache/cloudstack/storage/helper/HypervisorHelperImpl.java @@ -0,0 +1,77 @@ +/* + * 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 org.apache.cloudstack.storage.helper; + +import com.cloud.agent.api.Answer; +import com.cloud.agent.api.to.DataTO; +import com.cloud.utils.exception.CloudRuntimeException; +import org.apache.cloudstack.engine.subsystem.api.storage.EndPoint; +import org.apache.cloudstack.engine.subsystem.api.storage.EndPointSelector; +import org.apache.cloudstack.engine.subsystem.api.storage.Scope; +import org.apache.cloudstack.storage.command.ForgetObjectCmd; +import org.apache.cloudstack.storage.command.IntroduceObjectAnswer; +import org.apache.cloudstack.storage.command.IntroduceObjectCmd; +import org.apache.cloudstack.storage.to.SnapshotObjectTO; +import org.apache.log4j.Logger; + +import javax.inject.Inject; + +public class HypervisorHelperImpl implements HypervisorHelper { + private static final Logger s_logger = Logger.getLogger(HypervisorHelperImpl.class); + @Inject + EndPointSelector selector; + + @Override + public DataTO introduceObject(DataTO object, Scope scope, Long storeId) { + EndPoint ep = selector.select(scope, storeId); + IntroduceObjectCmd cmd = new IntroduceObjectCmd(object); + Answer answer = ep.sendMessage(cmd); + if (answer == null || !answer.getResult()) { + String errMsg = answer == null ? null : answer.getDetails(); + throw new CloudRuntimeException("Failed to introduce object, due to " + errMsg); + } + IntroduceObjectAnswer introduceObjectAnswer = (IntroduceObjectAnswer)answer; + return introduceObjectAnswer.getDataTO(); + } + + @Override + public boolean forgetObject(DataTO object, Scope scope, Long storeId) { + EndPoint ep = selector.select(scope, storeId); + ForgetObjectCmd cmd = new ForgetObjectCmd(object); + Answer answer = ep.sendMessage(cmd); + if (answer == null || !answer.getResult()) { + String errMsg = answer == null ? null : answer.getDetails(); + if (errMsg != null) { + s_logger.debug("Failed to forget object: " + errMsg); + } + return false; + } + return true; + } + + @Override + public SnapshotObjectTO takeSnapshot(SnapshotObjectTO snapshotObjectTO, Scope scope) { + return null; //To change body of implemented methods use File | Settings | File Templates. + } + + @Override + public boolean revertSnapshot(SnapshotObjectTO snapshotObjectTO, Scope scope) { + return false; //To change body of implemented methods use File | Settings | File Templates. + } +}