cloudstack/server/src/com/cloud/network/element/VpcVirtualRouterElement.java
2012-06-15 14:07:59 -07:00

257 lines
10 KiB
Java

// Copyright 2012 Citrix Systems, Inc. Licensed under the
// Apache License, Version 2.0 (the "License"); you may not use this
// file except in compliance with the License. Citrix Systems, Inc.
// reserves all rights not expressly granted by 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.
//
// Automatically generated by addcopyright.py at 04/03/2012
package com.cloud.network.element;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ejb.Local;
import org.apache.log4j.Logger;
import com.cloud.dc.DataCenter;
import com.cloud.deploy.DeployDestination;
import com.cloud.exception.ConcurrentOperationException;
import com.cloud.exception.InsufficientCapacityException;
import com.cloud.exception.ResourceUnavailableException;
import com.cloud.network.Network;
import com.cloud.network.Network.Capability;
import com.cloud.network.Network.Provider;
import com.cloud.network.Network.Service;
import com.cloud.network.NetworkService;
import com.cloud.network.router.VirtualRouter;
import com.cloud.network.router.VpcVirtualNetworkApplianceManager;
import com.cloud.network.vpc.Vpc;
import com.cloud.network.vpc.VpcService;
import com.cloud.network.vpc.VpcVirtualNetworkApplianceService;
import com.cloud.offering.NetworkOffering;
import com.cloud.utils.component.Inject;
import com.cloud.vm.DomainRouterVO;
import com.cloud.vm.NicProfile;
import com.cloud.vm.ReservationContext;
import com.cloud.vm.VirtualMachine;
import com.cloud.vm.VirtualMachineProfile;
/**
* @author Alena Prokharchyk
*/
@Local(value = NetworkElement.class)
public class VpcVirtualRouterElement extends VirtualRouterElement implements VpcProvider{
private static final Logger s_logger = Logger.getLogger(VpcVirtualRouterElement.class);
@Inject
NetworkService _ntwkService;
@Inject
VpcVirtualNetworkApplianceService _vpcElementService;
@Inject
VpcService _vpcService;
private static final Map<Service, Map<Capability, String>> capabilities = setCapabilities();
@Inject
VpcVirtualNetworkApplianceManager _vpcRouterMgr;
@Override
public boolean startVpc(Vpc vpc, DeployDestination dest, ReservationContext context) throws ConcurrentOperationException,
ResourceUnavailableException, InsufficientCapacityException {
Map<VirtualMachineProfile.Param, Object> params = new HashMap<VirtualMachineProfile.Param, Object>(1);
params.put(VirtualMachineProfile.Param.ReProgramGuestNetworks, true);
_vpcRouterMgr.deployVirtualRouterInVpc(vpc, dest, _accountMgr.getAccount(vpc.getAccountId()), params);
return true;
}
@Override
public boolean stopVpc(Vpc vpc) throws ConcurrentOperationException, ResourceUnavailableException {
List<DomainRouterVO> routers = _routerDao.listRoutersByVpcId(vpc.getId());
if (routers == null || routers.isEmpty()) {
return true;
}
boolean result = true;
for (DomainRouterVO router : routers) {
result = result && (_routerMgr.destroyRouter(router.getId()) != null);
}
return result;
}
@Override
public boolean implement(Network network, NetworkOffering offering, DeployDestination dest, ReservationContext context)
throws ResourceUnavailableException, ConcurrentOperationException,
InsufficientCapacityException {
Long vpcId = network.getVpcId();
if (vpcId == null) {
s_logger.warn("Network " + network + " is not associated with any VPC");
return false;
}
Vpc vpc = _vpcService.getVpc(vpcId);
Map<VirtualMachineProfile.Param, Object> params = new HashMap<VirtualMachineProfile.Param, Object>(1);
params.put(VirtualMachineProfile.Param.ReProgramGuestNetworks, true);
List<DomainRouterVO> routers = _vpcRouterMgr.deployVirtualRouterInVpc(vpc, dest, _accountMgr.getAccount(vpc.getAccountId()), params);
if ((routers == null) || (routers.size() == 0)) {
throw new ResourceUnavailableException("Can't find at least one running router!",
DataCenter.class, network.getDataCenterId());
}
boolean success = true;
for (VirtualRouter router : routers) {
//1) Check if router is already a part of the network
if (_ntwkService.isVmPartOfNetwork(router.getId(), network.getId())) {
s_logger.debug("Router " + router + " is already part of the network " + network);
continue;
}
//2) Call plugNics in the network service
success = success && _vpcElementService.addVmToNetwork(router, network);
}
if (!success) {
s_logger.warn("Failed to plug nic in network " + network + " for virtual router in vpc id=" + vpcId);
} else {
s_logger.debug("Successfully plugged nic in network " + network + " for virtual router in vpc id=" + vpcId);
}
return success;
}
@Override
public boolean prepare(Network network, NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm,
DeployDestination dest, ReservationContext context)
throws ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException {
Long vpcId = network.getVpcId();
if (vpcId == null) {
s_logger.warn("Network " + network + " is not associated with any VPC");
return false;
}
Vpc vpc = _vpcService.getVpc(vpcId);
Map<VirtualMachineProfile.Param, Object> params = new HashMap<VirtualMachineProfile.Param, Object>(1);
params.put(VirtualMachineProfile.Param.ReProgramGuestNetworks, true);
List<DomainRouterVO> routers = _vpcRouterMgr.deployVirtualRouterInVpc(vpc, dest, _accountMgr.getAccount(vpc.getAccountId()), params);
if ((routers == null) || (routers.size() == 0)) {
throw new ResourceUnavailableException("Can't find at least one running router!",
DataCenter.class, network.getDataCenterId());
}
boolean success = true;
for (VirtualRouter router : routers) {
//1) Check if router is already a part of the network
if (_ntwkService.isVmPartOfNetwork(router.getId(), network.getId())) {
s_logger.debug("Router " + router + " is already part of the network " + network);
continue;
}
//2) Call plugNics in the network service
success = success && _vpcElementService.addVmToNetwork(router, network);
}
if (!success) {
s_logger.warn("Failed to plug nic in network " + network + " for virtual router in vpc id=" + vpcId);
} else {
s_logger.debug("Successfully plugged nic in network " + network + " for virtual router in vpc id=" + vpcId);
}
return success;
}
@Override
public boolean shutdown(Network network, ReservationContext context, boolean cleanup)
throws ConcurrentOperationException, ResourceUnavailableException {
boolean success = true;
Long vpcId = network.getVpcId();
if (vpcId == null) {
s_logger.debug("Network " + network + " doesn't belong to any vpc, so skipping unplug nic part");
return success;
}
List<? extends VirtualRouter> routers = _routerDao.listRoutersByVpcId(vpcId);
for (VirtualRouter router : routers) {
//1) Check if router is already a part of the network
if (!_ntwkService.isVmPartOfNetwork(router.getId(), network.getId())) {
s_logger.debug("Router " + router + " is not a part the network " + network);
continue;
}
//2) Call unplugNics in the network service
success = success && _vpcElementService.removeVmFromNetwork(router, network);
}
if (!success) {
s_logger.warn("Failed to unplug nic in network " + network + " for virtual router in vpc id=" + vpcId);
} else {
s_logger.debug("Successfully unplugged nic in network " + network + " for virtual router in vpc id=" + vpcId);
}
return success;
}
@Override
public boolean destroy(Network config) throws ConcurrentOperationException, ResourceUnavailableException {
boolean success = true;
Long vpcId = config.getVpcId();
if (vpcId == null) {
s_logger.debug("Network " + config + " doesn't belong to any vpc, so skipping unplug nic part");
return success;
}
List<? extends VirtualRouter> routers = _routerDao.listRoutersByVpcId(vpcId);
for (VirtualRouter router : routers) {
//1) Check if router is already a part of the network
if (!_ntwkService.isVmPartOfNetwork(router.getId(), config.getId())) {
s_logger.debug("Router " + router + " is not a part the network " + config);
continue;
}
//2) Call unplugNics in the network service
success = success && _vpcElementService.removeVmFromNetwork(router, config);
}
if (!success) {
s_logger.warn("Failed to unplug nic in network " + config + " for virtual router in vpc id=" + vpcId);
} else {
s_logger.debug("Successfully unplugged nic in network " + config + " for virtual router in vpc id=" + vpcId);
}
return success;
}
@Override
public Provider getProvider() {
return Provider.VPCVirtualRouter;
}
private static Map<Service, Map<Capability, String>> setCapabilities() {
Map<Service, Map<Capability, String>> capabilities = VirtualRouterElement.capabilities;
Map<Capability, String> sourceNatCapabilities = capabilities.get(Service.SourceNat);
sourceNatCapabilities.put(Capability.RedundantRouter, "false");
capabilities.put(Service.SourceNat, sourceNatCapabilities);
return capabilities;
}
@Override
public Map<Service, Map<Capability, String>> getCapabilities() {
return capabilities;
}
}