Search in sources :

Example 1 with FirewallRulesDao

use of com.cloud.network.dao.FirewallRulesDao 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

PublicIpAddress (com.cloud.network.PublicIpAddress)1 FirewallRulesDao (com.cloud.network.dao.FirewallRulesDao)1 IPAddressDao (com.cloud.network.dao.IPAddressDao)1 IPAddressVO (com.cloud.network.dao.IPAddressVO)1 VpcManager (com.cloud.network.vpc.VpcManager)1 Pair (com.cloud.utils.Pair)1 Nic (com.cloud.vm.Nic)1 NicVO (com.cloud.vm.NicVO)1 NicDao (com.cloud.vm.dao.NicDao)1 URI (java.net.URI)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1