|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package org.apache.cloudstack.api.command.admin.network; |
| 18 | + |
| 19 | +import org.apache.log4j.Logger; |
| 20 | + |
| 21 | +import org.apache.cloudstack.acl.SecurityChecker.AccessType; |
| 22 | +import org.apache.cloudstack.api.ACL; |
| 23 | +import org.apache.cloudstack.api.APICommand; |
| 24 | +import org.apache.cloudstack.api.ApiConstants; |
| 25 | +import org.apache.cloudstack.api.ApiErrorCode; |
| 26 | +import org.apache.cloudstack.api.BaseAsyncCmd; |
| 27 | +import org.apache.cloudstack.api.Parameter; |
| 28 | +import org.apache.cloudstack.api.ResponseObject.ResponseView; |
| 29 | +import org.apache.cloudstack.api.ServerApiException; |
| 30 | +import org.apache.cloudstack.api.response.NetworkOfferingResponse; |
| 31 | +import org.apache.cloudstack.api.response.NetworkResponse; |
| 32 | +import org.apache.cloudstack.context.CallContext; |
| 33 | + |
| 34 | +import com.cloud.event.EventTypes; |
| 35 | +import com.cloud.exception.InvalidParameterValueException; |
| 36 | +import com.cloud.network.Network; |
| 37 | +import com.cloud.offering.NetworkOffering; |
| 38 | +import com.cloud.user.Account; |
| 39 | +import com.cloud.user.User; |
| 40 | + |
| 41 | +@APICommand(name = "migrateNetwork", description = "moves a network to another physical network", responseObject = NetworkResponse.class, responseView = ResponseView.Restricted, entityType = {Network.class}, |
| 42 | + requestHasSensitiveInfo = false, responseHasSensitiveInfo = false) |
| 43 | +public class MigrateNetworkCmd extends BaseAsyncCmd { |
| 44 | + public static final Logger s_logger = Logger.getLogger(MigrateNetworkCmd.class.getName()); |
| 45 | + |
| 46 | + private static final String s_name = "migratenetworkresponse"; |
| 47 | + |
| 48 | + ///////////////////////////////////////////////////// |
| 49 | + //////////////// API parameters ///////////////////// |
| 50 | + ///////////////////////////////////////////////////// |
| 51 | + @ACL(accessType = AccessType.OperateEntry) |
| 52 | + @Parameter(name=ApiConstants.NETWORK_ID, type=CommandType.UUID, entityType = NetworkResponse.class, |
| 53 | + required=true, description="the ID of the network") |
| 54 | + protected Long id; |
| 55 | + |
| 56 | + @Parameter(name = ApiConstants.NETWORK_OFFERING_ID, type = CommandType.UUID, entityType = NetworkOfferingResponse.class, description = "network offering ID") |
| 57 | + private Long networkOfferingId; |
| 58 | + |
| 59 | + @Parameter(name = ApiConstants.RESUME, type = CommandType.BOOLEAN, description = "true if previous network migration cmd failed") |
| 60 | + private Boolean resume; |
| 61 | + |
| 62 | + ///////////////////////////////////////////////////// |
| 63 | + /////////////////// Accessors /////////////////////// |
| 64 | + ///////////////////////////////////////////////////// |
| 65 | + |
| 66 | + public Long getId() { |
| 67 | + return id; |
| 68 | + } |
| 69 | + |
| 70 | + public Long getNetworkOfferingId() { |
| 71 | + return networkOfferingId; |
| 72 | + } |
| 73 | + |
| 74 | + public Boolean getResume() { |
| 75 | + return resume != null ? resume : false; |
| 76 | + } |
| 77 | + |
| 78 | + ///////////////////////////////////////////////////// |
| 79 | + /////////////// API Implementation/////////////////// |
| 80 | + ///////////////////////////////////////////////////// |
| 81 | + |
| 82 | + @Override |
| 83 | + public String getCommandName() { |
| 84 | + return s_name; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public long getEntityOwnerId() { |
| 89 | + Network network = _networkService.getNetwork(id); |
| 90 | + if (network == null) { |
| 91 | + throw new InvalidParameterValueException("Networkd id=" + id + " doesn't exist"); |
| 92 | + } else { |
| 93 | + return _networkService.getNetwork(id).getAccountId(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void execute() { |
| 99 | + User callerUser = _accountService.getActiveUser(CallContext.current().getCallingUserId()); |
| 100 | + Account callerAccount = _accountService.getActiveAccountById(callerUser.getAccountId()); |
| 101 | + Network network = _networkService.getNetwork(id); |
| 102 | + if (network == null) { |
| 103 | + throw new InvalidParameterValueException("Couldn't find network by id"); |
| 104 | + } |
| 105 | + |
| 106 | + Network result = |
| 107 | + _networkService.migrateGuestNetwork(getId(), getNetworkOfferingId(), callerAccount, callerUser, getResume()); |
| 108 | + |
| 109 | + if (result != null) { |
| 110 | + NetworkResponse response = _responseGenerator.createNetworkResponse(ResponseView.Restricted, result); |
| 111 | + response.setResponseName(getCommandName()); |
| 112 | + setResponseObject(response); |
| 113 | + } else { |
| 114 | + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update network"); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public String getEventDescription() { |
| 120 | + StringBuilder eventMsg = new StringBuilder("Migrating network: " + getId()); |
| 121 | + if (getNetworkOfferingId() != null) { |
| 122 | + Network network = _networkService.getNetwork(getId()); |
| 123 | + if (network == null) { |
| 124 | + throw new InvalidParameterValueException("Network id=" + id + " doesn't exist"); |
| 125 | + } |
| 126 | + if (network.getNetworkOfferingId() != getNetworkOfferingId()) { |
| 127 | + NetworkOffering oldOff = _entityMgr.findById(NetworkOffering.class, network.getNetworkOfferingId()); |
| 128 | + NetworkOffering newOff = _entityMgr.findById(NetworkOffering.class, getNetworkOfferingId()); |
| 129 | + if (newOff == null) { |
| 130 | + throw new InvalidParameterValueException("Network offering id supplied is invalid"); |
| 131 | + } |
| 132 | + |
| 133 | + eventMsg.append(". Original network offering id: " + oldOff.getUuid() + ", new network offering id: " + newOff.getUuid()); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return eventMsg.toString(); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public String getEventType() { |
| 142 | + return EventTypes.EVENT_NETWORK_MIGRATE; |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public String getSyncObjType() { |
| 147 | + return BaseAsyncCmd.networkSyncObject; |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public Long getSyncObjId() { |
| 152 | + return id; |
| 153 | + } |
| 154 | + |
| 155 | +} |
0 commit comments