Search in sources :

Example 11 with NicDao

use of com.cloud.vm.dao.NicDao in project cosmic by MissionCriticalCloud.

the class NicPlugInOutRules method getNicsToChangeOnRouter.

private Pair<Map<String, PublicIpAddress>, Map<String, PublicIpAddress>> getNicsToChangeOnRouter(final NetworkTopologyVisitor visitor) {
    // 1) check which nics need to be plugged/unplugged and plug/unplug them
    final Map<String, PublicIpAddress> nicsToPlug = new HashMap<>();
    final Map<String, PublicIpAddress> nicsToUnplug = new HashMap<>();
    final VpcManager vpcMgr = visitor.getVirtualNetworkApplianceFactory().getVpcMgr();
    final NicDao nicDao = visitor.getVirtualNetworkApplianceFactory().getNicDao();
    // find out nics to unplug
    for (final PublicIpAddress ip : _ipAddresses) {
        final long publicNtwkId = ip.getNetworkId();
        // rules, release it on the backend
        if (!vpcMgr.isIpAllocatedToVpc(ip)) {
            ip.setState(IpAddress.State.Releasing);
        }
        if (ip.getState() == IpAddress.State.Releasing) {
            final Nic nic = nicDao.findByIp4AddressAndNetworkIdAndInstanceId(publicNtwkId, _router.getId(), ip.getAddress().addr());
            if (nic != null) {
                nicsToUnplug.put(ip.getVlanTag(), ip);
                s_logger.debug("Need to unplug the nic for ip=" + ip + "; vlan=" + ip.getVlanTag() + " in public network id =" + publicNtwkId);
            }
        }
    }
    // find out nics to plug
    for (final PublicIpAddress ip : _ipAddresses) {
        final URI broadcastUri = BroadcastDomainType.Vlan.toUri(ip.getVlanTag());
        final long publicNtwkId = ip.getNetworkId();
        // rules, release it on the backend
        if (!vpcMgr.isIpAllocatedToVpc(ip)) {
            ip.setState(IpAddress.State.Releasing);
        }
        if (ip.getState() == IpAddress.State.Allocated || ip.getState() == IpAddress.State.Allocating) {
            // nic has to be plugged only when there are no nics for this
            // vlan tag exist on VR
            final Nic nic = nicDao.findByNetworkIdInstanceIdAndBroadcastUri(publicNtwkId, _router.getId(), broadcastUri.toString());
            if (nic == null && nicsToPlug.get(ip.getVlanTag()) == null) {
                nicsToPlug.put(ip.getVlanTag(), ip);
                s_logger.debug("Need to plug the nic for ip=" + ip + "; vlan=" + ip.getVlanTag() + " in public network id =" + publicNtwkId);
            } else {
                final PublicIpAddress nicToUnplug = nicsToUnplug.get(ip.getVlanTag());
                if (nicToUnplug != null) {
                    final NicVO nicVO = nicDao.findByIp4AddressAndNetworkIdAndInstanceId(publicNtwkId, _router.getId(), nicToUnplug.getAddress().addr());
                    nicVO.setIPv4Address(ip.getAddress().addr());
                    nicDao.update(nicVO.getId(), nicVO);
                    s_logger.debug("Updated the nic " + nicVO + " with the new ip address " + ip.getAddress().addr());
                    nicsToUnplug.remove(ip.getVlanTag());
                }
            }
        }
    }
    final Pair<Map<String, PublicIpAddress>, Map<String, PublicIpAddress>> nicsToChange = new Pair<>(nicsToPlug, nicsToUnplug);
    return nicsToChange;
}
Also used : HashMap(java.util.HashMap) NicDao(com.cloud.vm.dao.NicDao) Nic(com.cloud.vm.Nic) URI(java.net.URI) PublicIpAddress(com.cloud.network.PublicIpAddress) VpcManager(com.cloud.network.vpc.VpcManager) NicVO(com.cloud.vm.NicVO) HashMap(java.util.HashMap) Map(java.util.Map) Pair(com.cloud.utils.Pair)

Example 12 with NicDao

use of com.cloud.vm.dao.NicDao in project cosmic by MissionCriticalCloud.

the class PasswordToRouterRules method accept.

@Override
public boolean accept(final NetworkTopologyVisitor visitor, final VirtualRouter router) throws ResourceUnavailableException {
    _router = router;
    final UserVmDao userVmDao = visitor.getVirtualNetworkApplianceFactory().getUserVmDao();
    userVmDao.loadDetails((UserVmVO) profile.getVirtualMachine());
    // for basic zone, send vm data/password information only to the router in the same pod
    final NicDao nicDao = visitor.getVirtualNetworkApplianceFactory().getNicDao();
    nicVo = nicDao.findById(nic.getId());
    return visitor.visit(this);
}
Also used : NicDao(com.cloud.vm.dao.NicDao) UserVmDao(com.cloud.vm.dao.UserVmDao)

Example 13 with NicDao

use of com.cloud.vm.dao.NicDao in project cloudstack by apache.

the class VirtualMachineModel method setServiceInstanceNics.

private void setServiceInstanceNics(ModelController controller, VMInstanceVO instance) throws IOException {
    NicDao nicDao = controller.getNicDao();
    ContrailManager manager = controller.getManager();
    NetworkDao networkDao = controller.getNetworkDao();
    List<NicVO> nics = nicDao.listByVmId(_instanceId);
    for (NicVO nic : nics) {
        String tag;
        switch(nic.getDeviceId()) {
            case 0:
                tag = "management";
                break;
            case 1:
                tag = "left";
                break;
            case 2:
                tag = "right";
                break;
            default:
                tag = null;
        }
        VMInterfaceModel vmiModel = getVMInterface(nic.getUuid());
        if (vmiModel == null) {
            vmiModel = new VMInterfaceModel(nic.getUuid());
            vmiModel.addToVirtualMachine(this);
            NetworkVO network = networkDao.findById(nic.getNetworkId());
            VirtualNetworkModel vnModel = manager.getDatabase().lookupVirtualNetwork(network.getUuid(), manager.getCanonicalName(network), network.getTrafficType());
            assert vnModel != null;
            vmiModel.addToVirtualNetwork(vnModel);
        }
        vmiModel.setProperties(controller, instance, nic);
        vmiModel.setServiceTag(tag);
    }
}
Also used : NetworkVO(com.cloud.network.dao.NetworkVO) NetworkDao(com.cloud.network.dao.NetworkDao) NicDao(com.cloud.vm.dao.NicDao) ContrailManager(org.apache.cloudstack.network.contrail.management.ContrailManager) NicVO(com.cloud.vm.NicVO)

Example 14 with NicDao

use of com.cloud.vm.dao.NicDao in project cloudstack by apache.

the class PasswordToRouterRules method accept.

@Override
public boolean accept(final NetworkTopologyVisitor visitor, final VirtualRouter router) throws ResourceUnavailableException {
    _router = router;
    UserVmDao userVmDao = visitor.getVirtualNetworkApplianceFactory().getUserVmDao();
    userVmDao.loadDetails((UserVmVO) profile.getVirtualMachine());
    // for basic zone, send vm data/password information only to the router in the same pod
    NicDao nicDao = visitor.getVirtualNetworkApplianceFactory().getNicDao();
    nicVo = nicDao.findById(nic.getId());
    return visitor.visit(this);
}
Also used : NicDao(com.cloud.vm.dao.NicDao) UserVmDao(com.cloud.vm.dao.UserVmDao)

Example 15 with NicDao

use of com.cloud.vm.dao.NicDao in project cloudstack by apache.

the class NicPlugInOutRules method getNicsToChangeOnRouter.

private Pair<Map<String, PublicIpAddress>, Map<String, PublicIpAddress>> getNicsToChangeOnRouter(final NetworkTopologyVisitor visitor) {
    // 1) check which nics need to be plugged/unplugged and plug/unplug them
    final Map<String, PublicIpAddress> nicsToPlug = new HashMap<String, PublicIpAddress>();
    final Map<String, PublicIpAddress> nicsToUnplug = new HashMap<String, PublicIpAddress>();
    VpcManager vpcMgr = visitor.getVirtualNetworkApplianceFactory().getVpcMgr();
    NicDao nicDao = visitor.getVirtualNetworkApplianceFactory().getNicDao();
    IPAddressDao ipAddressDao = visitor.getVirtualNetworkApplianceFactory().getIpAddressDao();
    FirewallRulesDao rulesDao = visitor.getVirtualNetworkApplianceFactory().getFirewallRulesDao();
    // find out nics to unplug
    for (PublicIpAddress ip : _ipAddresses) {
        long publicNtwkId = ip.getNetworkId();
        // rules, release it on the backend
        if (!vpcMgr.isIpAllocatedToVpc(ip)) {
            ip.setState(IpAddress.State.Releasing);
        }
        if (ip.getState() == IpAddress.State.Releasing) {
            NicVO nic = nicDao.findByIp4AddressAndNetworkIdAndInstanceId(publicNtwkId, _router.getId(), ip.getAddress().addr());
            if (nic != null) {
                final List<IPAddressVO> allIps = ipAddressDao.listByAssociatedVpc(ip.getVpcId(), null);
                boolean ipUpdated = false;
                for (IPAddressVO allIp : allIps) {
                    if (allIp.getId() != ip.getId() && allIp.getVlanId() == ip.getVlanId() && (allIp.isSourceNat() || rulesDao.countRulesByIpIdAndState(allIp.getId(), FirewallRule.State.Active) > 0 || (allIp.isOneToOneNat() && allIp.getRuleState() == null))) {
                        s_logger.debug("Updating the nic " + nic + " with new ip address " + allIp.getAddress().addr());
                        nic.setIPv4Address(allIp.getAddress().addr());
                        nicDao.update(nic.getId(), nic);
                        ipUpdated = true;
                        break;
                    }
                }
                if (!ipUpdated) {
                    nicsToUnplug.put(ip.getVlanTag(), ip);
                    s_logger.debug("Need to unplug the nic for ip=" + ip + "; vlan=" + ip.getVlanTag() + " in public network id =" + publicNtwkId);
                }
            }
        }
    }
    // find out nics to plug
    for (PublicIpAddress ip : _ipAddresses) {
        URI broadcastUri = BroadcastDomainType.Vlan.toUri(ip.getVlanTag());
        long publicNtwkId = ip.getNetworkId();
        // rules, release it on the backend
        if (!vpcMgr.isIpAllocatedToVpc(ip)) {
            ip.setState(IpAddress.State.Releasing);
        }
        if (ip.getState() == IpAddress.State.Allocated || ip.getState() == IpAddress.State.Allocating) {
            // nic has to be plugged only when there are no nics for this
            // vlan tag exist on VR
            Nic nic = nicDao.findByNetworkIdInstanceIdAndBroadcastUri(publicNtwkId, _router.getId(), broadcastUri.toString());
            if (nic == null && nicsToPlug.get(ip.getVlanTag()) == null) {
                nicsToPlug.put(ip.getVlanTag(), ip);
                s_logger.debug("Need to plug the nic for ip=" + ip + "; vlan=" + ip.getVlanTag() + " in public network id =" + publicNtwkId);
            } else {
                final PublicIpAddress nicToUnplug = nicsToUnplug.get(ip.getVlanTag());
                if (nicToUnplug != null) {
                    NicVO nicVO = nicDao.findByIp4AddressAndNetworkIdAndInstanceId(publicNtwkId, _router.getId(), nicToUnplug.getAddress().addr());
                    nicVO.setIPv4Address(ip.getAddress().addr());
                    nicDao.update(nicVO.getId(), nicVO);
                    s_logger.debug("Updated the nic " + nicVO + " with the new ip address " + ip.getAddress().addr());
                    nicsToUnplug.remove(ip.getVlanTag());
                }
            }
        }
    }
    Pair<Map<String, PublicIpAddress>, Map<String, PublicIpAddress>> nicsToChange = new Pair<Map<String, PublicIpAddress>, Map<String, PublicIpAddress>>(nicsToPlug, nicsToUnplug);
    return nicsToChange;
}
Also used : HashMap(java.util.HashMap) NicDao(com.cloud.vm.dao.NicDao) IPAddressDao(com.cloud.network.dao.IPAddressDao) Nic(com.cloud.vm.Nic) FirewallRulesDao(com.cloud.network.dao.FirewallRulesDao) URI(java.net.URI) PublicIpAddress(com.cloud.network.PublicIpAddress) VpcManager(com.cloud.network.vpc.VpcManager) IPAddressVO(com.cloud.network.dao.IPAddressVO) NicVO(com.cloud.vm.NicVO) HashMap(java.util.HashMap) Map(java.util.Map) Pair(com.cloud.utils.Pair)

Aggregations

NicDao (com.cloud.vm.dao.NicDao)19 UserVmDao (com.cloud.vm.dao.UserVmDao)12 NicVO (com.cloud.vm.NicVO)6 PublicIpAddress (com.cloud.network.PublicIpAddress)5 Nic (com.cloud.vm.Nic)4 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)3 DataCenterDao (com.cloud.dc.dao.DataCenterDao)2 IPAddressDao (com.cloud.network.dao.IPAddressDao)2 IPAddressVO (com.cloud.network.dao.IPAddressVO)2 NetworkVO (com.cloud.network.dao.NetworkVO)2 VpcManager (com.cloud.network.vpc.VpcManager)2 VMTemplateDao (com.cloud.storage.dao.VMTemplateDao)2 Pair (com.cloud.utils.Pair)2 UserVmVO (com.cloud.vm.UserVmVO)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 NetworkUsageCommand (com.cloud.agent.api.NetworkUsageCommand)1 Commands (com.cloud.agent.manager.Commands)1