// 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.router; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.ejb.Local; import org.apache.log4j.Logger; import com.cloud.agent.AgentManager.OnError; import com.cloud.agent.api.Answer; import com.cloud.agent.api.PlugNicCommand; import com.cloud.agent.api.to.NicTO; import com.cloud.agent.api.to.VirtualMachineTO; import com.cloud.agent.manager.Commands; import com.cloud.deploy.DataCenterDeployment; import com.cloud.deploy.DeployDestination; import com.cloud.deploy.DeploymentPlan; import com.cloud.exception.AgentUnavailableException; import com.cloud.exception.ConcurrentOperationException; import com.cloud.exception.InsufficientCapacityException; import com.cloud.exception.OperationTimedoutException; import com.cloud.exception.ResourceUnavailableException; import com.cloud.network.Network; import com.cloud.network.NetworkService; import com.cloud.network.PhysicalNetwork; import com.cloud.network.VirtualRouterProvider; import com.cloud.network.VirtualRouterProvider.VirtualRouterProviderType; import com.cloud.network.addr.PublicIp; import com.cloud.network.dao.PhysicalNetworkDao; import com.cloud.network.vpc.Vpc; import com.cloud.network.vpc.VpcVirtualNetworkApplianceService; import com.cloud.network.vpc.Dao.VpcDao; import com.cloud.network.vpc.Dao.VpcOfferingDao; import com.cloud.user.Account; import com.cloud.utils.Pair; import com.cloud.utils.component.Inject; import com.cloud.utils.db.DB; import com.cloud.utils.net.NetUtils; import com.cloud.vm.DomainRouterVO; import com.cloud.vm.ReservationContext; import com.cloud.vm.VirtualMachineProfile.Param; /** * @author Alena Prokharchyk */ @Local(value = { VpcVirtualNetworkApplianceManager.class, VpcVirtualNetworkApplianceService.class,}) public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplianceManagerImpl implements VpcVirtualNetworkApplianceManager{ private static final Logger s_logger = Logger.getLogger(VpcVirtualNetworkApplianceManagerImpl.class); @Inject VpcDao _vpcDao = null; @Inject VpcOfferingDao _vpcOffDao = null; @Inject PhysicalNetworkDao _pNtwkDao = null; @Inject NetworkService _ntwkService = null; @Override public List deployVirtualRouterInVpc(Vpc vpc, DeployDestination dest, Account owner, Map params) throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException { List routers = findOrDeployVirtualRouterInVpc(vpc, dest, owner, params); return startRouters(params, routers); } @DB protected List findOrDeployVirtualRouterInVpc(Vpc vpc, DeployDestination dest, Account owner, Map params) throws ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException { s_logger.debug("Deploying Virtual Router in VPC "+ vpc); Vpc vpcLock = _vpcDao.acquireInLockTable(vpc.getId()); if (vpcLock == null) { throw new ConcurrentOperationException("Unable to lock vpc " + vpc.getId()); } //1) Get deployment plan and find out the list of routers Pair> planAndRouters = getDeploymentPlanAndRouters(vpc.getId(), dest); DeploymentPlan plan = planAndRouters.first(); List routers = planAndRouters.second(); //2) Return routers if exist if (routers.size() >= 1) { return routers; } Long offeringId = _vpcOffDao.findById(vpc.getVpcOfferingId()).getServiceOfferingId(); if (offeringId == null) { offeringId = _offering.getId(); } //3) Deploy Virtual Router try { //FIXME - remove hardcoded provider type when decide if we want cross physical networks vpcs List pNtwks = _pNtwkDao.listByZone(vpc.getZoneId()); VirtualRouterProvider vrProvider = _vrProviderDao.findByNspIdAndType(pNtwks.get(0).getId(), VirtualRouterProviderType.VirtualRouter); PublicIp sourceNatIp = _networkMgr.assignSourceNatIpAddressToVpc(owner, vpc); DomainRouterVO router = deployRouter(owner, dest, plan, params, true, null, false, vrProvider, offeringId, sourceNatIp, vpc.getId()); routers.add(router); } finally { if (vpcLock != null) { _vpcDao.releaseFromLockTable(vpc.getId()); } } return routers; } protected Pair> getDeploymentPlanAndRouters(long vpcId, DeployDestination dest) { long dcId = dest.getDataCenter().getId(); DeploymentPlan plan = new DataCenterDeployment(dcId); List routers = _routerDao.listRoutersByVpcId(vpcId); return new Pair>(plan, routers); } @Override public boolean plugNic(Network network, NicTO nic, VirtualMachineTO vm, ReservationContext context, DeployDestination dest) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException { String networkDomain = network.getNetworkDomain(); String cidr = network.getCidr(); String dhcpRange = null; if (cidr != null) { dhcpRange = NetUtils.getDhcpRange(cidr); } boolean result = true; //FIXME - Anthony, here I send plug nic command try { Map params = new HashMap(); params.put(PlugNicCommand.Param.NetworkDomain, networkDomain); params.put(PlugNicCommand.Param.DhcpRange, dhcpRange); PlugNicCommand plugNicCmd = new PlugNicCommand(vm, nic, params); Commands cmds = new Commands(OnError.Stop); cmds.addCommand("plugnic", plugNicCmd); _agentMgr.send(dest.getHost().getId(), cmds); Answer plugNicAnswer = cmds.getAnswer(Answer.class); if (!(plugNicAnswer != null && plugNicAnswer.getResult())) { s_logger.warn("Unable to plug nic for vm " + vm.getHostName()); result = false; } } catch (OperationTimedoutException e) { throw new AgentUnavailableException("Unable to plug nic for vm " + vm.getHostName(), dest.getHost().getId(), e); } return result; } @Override public boolean unplugNic(Network network, NicTO nic, VirtualMachineTO vm, ReservationContext context, DeployDestination dest) throws ConcurrentOperationException, ResourceUnavailableException { //FIXME - Anthony, add unplug nic agent command return true; } @Override public boolean addVmToNetwork(VirtualRouter router, Network network) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException { return _itMgr.addVmToNetwork(router, network); } @Override public boolean removeVmFromNetwork(VirtualRouter router, Network network) { return _itMgr.removeVmFromNetwork(router, network); } }