Search in sources :

Example 1 with UserStatisticsVO

use of com.cloud.user.UserStatisticsVO in project cloudstack by apache.

the class UserStatisticsDaoImpl method listActiveAndRecentlyDeleted.

@Override
public List<UserStatisticsVO> listActiveAndRecentlyDeleted(Date minRemovedDate, int startIndex, int limit) {
    List<UserStatisticsVO> userStats = new ArrayList<UserStatisticsVO>();
    if (minRemovedDate == null)
        return userStats;
    TransactionLegacy txn = TransactionLegacy.currentTxn();
    try {
        String sql = ACTIVE_AND_RECENTLY_DELETED_SEARCH + " LIMIT " + startIndex + "," + limit;
        PreparedStatement pstmt = null;
        pstmt = txn.prepareAutoCloseStatement(sql);
        pstmt.setString(1, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), minRemovedDate));
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            userStats.add(toEntityBean(rs, false));
        }
    } catch (Exception ex) {
        s_logger.error("error saving user stats to cloud_usage db", ex);
    }
    return userStats;
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) UserStatisticsVO(com.cloud.user.UserStatisticsVO)

Example 2 with UserStatisticsVO

use of com.cloud.user.UserStatisticsVO in project cloudstack by apache.

the class VirtualNetworkApplianceManagerImpl method collectNetworkStatistics.

@Override
public <T extends VirtualRouter> void collectNetworkStatistics(final T router, final Nic nic) {
    if (router == null) {
        return;
    }
    final String privateIP = router.getPrivateIpAddress();
    if (privateIP != null) {
        final boolean forVpc = router.getVpcId() != null;
        List<Nic> routerNics = new ArrayList<Nic>();
        if (nic != null) {
            routerNics.add(nic);
        } else {
            routerNics.addAll(_nicDao.listByVmId(router.getId()));
        }
        for (final Nic routerNic : routerNics) {
            final Network network = _networkModel.getNetwork(routerNic.getNetworkId());
            // [TODO] Avoiding the NPE now, but I have to find out what is going on with the network. - Wilder Rodrigues
            if (network == null) {
                s_logger.error("Could not find a network with ID => " + routerNic.getNetworkId() + ". It might be a problem!");
                continue;
            }
            if (forVpc && network.getTrafficType() == TrafficType.Public || !forVpc && network.getTrafficType() == TrafficType.Guest && network.getGuestType() == Network.GuestType.Isolated) {
                final NetworkUsageCommand usageCmd = new NetworkUsageCommand(privateIP, router.getHostName(), forVpc, routerNic.getIPv4Address());
                final String routerType = router.getType().toString();
                final UserStatisticsVO previousStats = _userStatsDao.findBy(router.getAccountId(), router.getDataCenterId(), network.getId(), forVpc ? routerNic.getIPv4Address() : null, router.getId(), routerType);
                NetworkUsageAnswer answer = null;
                try {
                    answer = (NetworkUsageAnswer) _agentMgr.easySend(router.getHostId(), usageCmd);
                } catch (final Exception e) {
                    s_logger.warn("Error while collecting network stats from router: " + router.getInstanceName() + " from host: " + router.getHostId(), e);
                    continue;
                }
                if (answer != null) {
                    if (!answer.getResult()) {
                        s_logger.warn("Error while collecting network stats from router: " + router.getInstanceName() + " from host: " + router.getHostId() + "; details: " + answer.getDetails());
                        continue;
                    }
                    try {
                        if (answer.getBytesReceived() == 0 && answer.getBytesSent() == 0) {
                            s_logger.debug("Recieved and Sent bytes are both 0. Not updating user_statistics");
                            continue;
                        }
                        final NetworkUsageAnswer answerFinal = answer;
                        Transaction.execute(new TransactionCallbackNoReturn() {

                            @Override
                            public void doInTransactionWithoutResult(final TransactionStatus status) {
                                final UserStatisticsVO stats = _userStatsDao.lock(router.getAccountId(), router.getDataCenterId(), network.getId(), forVpc ? routerNic.getIPv4Address() : null, router.getId(), routerType);
                                if (stats == null) {
                                    s_logger.warn("unable to find stats for account: " + router.getAccountId());
                                    return;
                                }
                                if (previousStats != null && (previousStats.getCurrentBytesReceived() != stats.getCurrentBytesReceived() || previousStats.getCurrentBytesSent() != stats.getCurrentBytesSent())) {
                                    s_logger.debug("Router stats changed from the time NetworkUsageCommand was sent. " + "Ignoring current answer. Router: " + answerFinal.getRouterName() + " Rcvd: " + answerFinal.getBytesReceived() + "Sent: " + answerFinal.getBytesSent());
                                    return;
                                }
                                if (stats.getCurrentBytesReceived() > answerFinal.getBytesReceived()) {
                                    if (s_logger.isDebugEnabled()) {
                                        s_logger.debug("Received # of bytes that's less than the last one.  " + "Assuming something went wrong and persisting it. Router: " + answerFinal.getRouterName() + " Reported: " + toHumanReadableSize(answerFinal.getBytesReceived()) + " Stored: " + toHumanReadableSize(stats.getCurrentBytesReceived()));
                                    }
                                    stats.setNetBytesReceived(stats.getNetBytesReceived() + stats.getCurrentBytesReceived());
                                }
                                stats.setCurrentBytesReceived(answerFinal.getBytesReceived());
                                if (stats.getCurrentBytesSent() > answerFinal.getBytesSent()) {
                                    if (s_logger.isDebugEnabled()) {
                                        s_logger.debug("Received # of bytes that's less than the last one.  " + "Assuming something went wrong and persisting it. Router: " + answerFinal.getRouterName() + " Reported: " + toHumanReadableSize(answerFinal.getBytesSent()) + " Stored: " + toHumanReadableSize(stats.getCurrentBytesSent()));
                                    }
                                    stats.setNetBytesSent(stats.getNetBytesSent() + stats.getCurrentBytesSent());
                                }
                                stats.setCurrentBytesSent(answerFinal.getBytesSent());
                                if (!_dailyOrHourly) {
                                    // update agg bytes
                                    stats.setAggBytesSent(stats.getNetBytesSent() + stats.getCurrentBytesSent());
                                    stats.setAggBytesReceived(stats.getNetBytesReceived() + stats.getCurrentBytesReceived());
                                }
                                _userStatsDao.update(stats.getId(), stats);
                            }
                        });
                    } catch (final Exception e) {
                        s_logger.warn("Unable to update user statistics for account: " + router.getAccountId() + " Rx: " + toHumanReadableSize(answer.getBytesReceived()) + "; Tx: " + toHumanReadableSize(answer.getBytesSent()));
                    }
                }
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Nic(com.cloud.vm.Nic) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) NetworkUsageCommand(com.cloud.agent.api.NetworkUsageCommand) ConnectionException(com.cloud.exception.ConnectionException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) JsonSyntaxException(com.google.gson.JsonSyntaxException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ParseException(java.text.ParseException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) ConfigurationException(javax.naming.ConfigurationException) Network(com.cloud.network.Network) NetworkUsageAnswer(com.cloud.agent.api.NetworkUsageAnswer) UserStatisticsVO(com.cloud.user.UserStatisticsVO)

Example 3 with UserStatisticsVO

use of com.cloud.user.UserStatisticsVO in project cloudstack by apache.

the class VpcVirtualNetworkApplianceManagerImpl method finalizeCommandsOnStart.

@Override
public boolean finalizeCommandsOnStart(final Commands cmds, final VirtualMachineProfile profile) {
    final DomainRouterVO domainRouterVO = _routerDao.findById(profile.getId());
    Map<String, String> details = new HashMap<String, String>();
    if (profile.getHypervisorType() == Hypervisor.HypervisorType.VMware) {
        HypervisorGuru hvGuru = _hvGuruMgr.getGuru(profile.getHypervisorType());
        VirtualMachineTO vmTO = hvGuru.implement(profile);
        if (vmTO.getDetails() != null) {
            details = vmTO.getDetails();
        }
    }
    final boolean isVpc = domainRouterVO.getVpcId() != null;
    if (!isVpc) {
        return super.finalizeCommandsOnStart(cmds, profile);
    }
    if (domainRouterVO.getState() == State.Starting || domainRouterVO.getState() == State.Running) {
        // 1) FORM SSH CHECK COMMAND
        final NicProfile controlNic = getControlNic(profile);
        if (controlNic == null) {
            s_logger.error("Control network doesn't exist for the router " + domainRouterVO);
            return false;
        }
        finalizeSshAndVersionAndNetworkUsageOnStart(cmds, profile, domainRouterVO, controlNic);
        // 2) FORM PLUG NIC COMMANDS
        final List<Pair<Nic, Network>> guestNics = new ArrayList<Pair<Nic, Network>>();
        final List<Pair<Nic, Network>> publicNics = new ArrayList<Pair<Nic, Network>>();
        final List<Pair<Nic, Network>> privateGatewayNics = new ArrayList<Pair<Nic, Network>>();
        final Map<String, String> vlanMacAddress = new HashMap<String, String>();
        final List<? extends Nic> routerNics = _nicDao.listByVmIdOrderByDeviceId(profile.getId());
        for (final Nic routerNic : routerNics) {
            final Network network = _networkModel.getNetwork(routerNic.getNetworkId());
            if (network.getTrafficType() == TrafficType.Guest) {
                final Pair<Nic, Network> guestNic = new Pair<Nic, Network>(routerNic, network);
                if (_networkModel.isPrivateGateway(routerNic.getNetworkId())) {
                    privateGatewayNics.add(guestNic);
                } else {
                    guestNics.add(guestNic);
                }
            } else if (network.getTrafficType() == TrafficType.Public) {
                final Pair<Nic, Network> publicNic = new Pair<Nic, Network>(routerNic, network);
                publicNics.add(publicNic);
                final String vlanTag = BroadcastDomainType.getValue(routerNic.getBroadcastUri());
                vlanMacAddress.put(vlanTag, routerNic.getMacAddress());
            }
        }
        // Public and Guest networks start from device_id = 1
        int deviceId = 1;
        final List<Command> usageCmds = new ArrayList<Command>();
        // 3) PREPARE PLUG NIC COMMANDS
        try {
            // add VPC router to public networks
            final List<PublicIp> sourceNat = new ArrayList<PublicIp>(1);
            for (final Pair<Nic, Network> nicNtwk : publicNics) {
                final Nic publicNic = updateNicWithDeviceId(nicNtwk.first().getId(), deviceId);
                deviceId++;
                final Network publicNtwk = nicNtwk.second();
                final IPAddressVO userIp = _ipAddressDao.findByIpAndSourceNetworkId(publicNtwk.getId(), publicNic.getIPv4Address());
                if (userIp.isSourceNat()) {
                    final PublicIp publicIp = PublicIp.createFromAddrAndVlan(userIp, _vlanDao.findById(userIp.getVlanId()));
                    sourceNat.add(publicIp);
                    if (domainRouterVO.getPublicIpAddress() == null) {
                        final DomainRouterVO routerVO = _routerDao.findById(domainRouterVO.getId());
                        routerVO.setPublicIpAddress(publicNic.getIPv4Address());
                        routerVO.setPublicNetmask(publicNic.getIPv4Netmask());
                        routerVO.setPublicMacAddress(publicNic.getMacAddress());
                        _routerDao.update(routerVO.getId(), routerVO);
                    }
                }
                final PlugNicCommand plugNicCmd = new PlugNicCommand(_nwHelper.getNicTO(domainRouterVO, publicNic.getNetworkId(), publicNic.getBroadcastUri().toString()), domainRouterVO.getInstanceName(), domainRouterVO.getType(), details);
                cmds.addCommand(plugNicCmd);
                final VpcVO vpc = _vpcDao.findById(domainRouterVO.getVpcId());
                final NetworkUsageCommand netUsageCmd = new NetworkUsageCommand(domainRouterVO.getPrivateIpAddress(), domainRouterVO.getInstanceName(), true, publicNic.getIPv4Address(), vpc.getCidr());
                usageCmds.add(netUsageCmd);
                UserStatisticsVO stats = _userStatsDao.findBy(domainRouterVO.getAccountId(), domainRouterVO.getDataCenterId(), publicNtwk.getId(), publicNic.getIPv4Address(), domainRouterVO.getId(), domainRouterVO.getType().toString());
                if (stats == null) {
                    stats = new UserStatisticsVO(domainRouterVO.getAccountId(), domainRouterVO.getDataCenterId(), publicNic.getIPv4Address(), domainRouterVO.getId(), domainRouterVO.getType().toString(), publicNtwk.getId());
                    _userStatsDao.persist(stats);
                }
            }
            // create ip assoc for source nat
            if (!sourceNat.isEmpty()) {
                _commandSetupHelper.createVpcAssociatePublicIPCommands(domainRouterVO, sourceNat, cmds, vlanMacAddress);
            }
            // add VPC router to private gateway networks
            for (final Pair<Nic, Network> nicNtwk : privateGatewayNics) {
                final Nic guestNic = updateNicWithDeviceId(nicNtwk.first().getId(), deviceId);
                deviceId++;
                // plug guest nic
                final PlugNicCommand plugNicCmd = new PlugNicCommand(_nwHelper.getNicTO(domainRouterVO, guestNic.getNetworkId(), null), domainRouterVO.getInstanceName(), domainRouterVO.getType(), details);
                cmds.addCommand(plugNicCmd);
                // set private network
                final PrivateIpVO ipVO = _privateIpDao.findByIpAndSourceNetworkId(guestNic.getNetworkId(), guestNic.getIPv4Address());
                final Network network = _networkDao.findById(guestNic.getNetworkId());
                BroadcastDomainType.getValue(network.getBroadcastUri());
                final String netmask = NetUtils.getCidrNetmask(network.getCidr());
                final PrivateIpAddress ip = new PrivateIpAddress(ipVO, network.getBroadcastUri().toString(), network.getGateway(), netmask, guestNic.getMacAddress());
                final List<PrivateIpAddress> privateIps = new ArrayList<PrivateIpAddress>(1);
                privateIps.add(ip);
                _commandSetupHelper.createVpcAssociatePrivateIPCommands(domainRouterVO, privateIps, cmds, true);
                final Long privateGwAclId = _vpcGatewayDao.getNetworkAclIdForPrivateIp(ipVO.getVpcId(), ipVO.getNetworkId(), ipVO.getIpAddress());
                if (privateGwAclId != null) {
                    // set network acl on private gateway
                    final List<NetworkACLItemVO> networkACLs = _networkACLItemDao.listByACL(privateGwAclId);
                    s_logger.debug("Found " + networkACLs.size() + " network ACLs to apply as a part of VPC VR " + domainRouterVO + " start for private gateway ip = " + ipVO.getIpAddress());
                    _commandSetupHelper.createNetworkACLsCommands(networkACLs, domainRouterVO, cmds, ipVO.getNetworkId(), true);
                }
            }
            // add VPC router to guest networks
            for (final Pair<Nic, Network> nicNtwk : guestNics) {
                final Nic guestNic = updateNicWithDeviceId(nicNtwk.first().getId(), deviceId);
                deviceId++;
                // plug guest nic
                final PlugNicCommand plugNicCmd = new PlugNicCommand(_nwHelper.getNicTO(domainRouterVO, guestNic.getNetworkId(), null), domainRouterVO.getInstanceName(), domainRouterVO.getType(), details);
                cmds.addCommand(plugNicCmd);
                // set guest network
                final VirtualMachine vm = _vmDao.findById(domainRouterVO.getId());
                final NicProfile nicProfile = _networkModel.getNicProfile(vm, guestNic.getNetworkId(), null);
                final SetupGuestNetworkCommand setupCmd = _commandSetupHelper.createSetupGuestNetworkCommand(domainRouterVO, true, nicProfile);
                cmds.addCommand(setupCmd);
            }
        } catch (final Exception ex) {
            s_logger.warn("Failed to add router " + domainRouterVO + " to network due to exception ", ex);
            return false;
        }
        // 4) RE-APPLY ALL STATIC ROUTE RULES
        final List<? extends StaticRoute> routes = _staticRouteDao.listByVpcId(domainRouterVO.getVpcId());
        final List<StaticRouteProfile> staticRouteProfiles = new ArrayList<StaticRouteProfile>(routes.size());
        final Map<Long, VpcGateway> gatewayMap = new HashMap<Long, VpcGateway>();
        for (final StaticRoute route : routes) {
            VpcGateway gateway = gatewayMap.get(route.getVpcGatewayId());
            if (gateway == null) {
                gateway = _entityMgr.findById(VpcGateway.class, route.getVpcGatewayId());
                gatewayMap.put(gateway.getId(), gateway);
            }
            staticRouteProfiles.add(new StaticRouteProfile(route, gateway));
        }
        s_logger.debug("Found " + staticRouteProfiles.size() + " static routes to apply as a part of vpc route " + domainRouterVO + " start");
        if (!staticRouteProfiles.isEmpty()) {
            _commandSetupHelper.createStaticRouteCommands(staticRouteProfiles, domainRouterVO, cmds);
        }
        // 5) RE-APPLY ALL REMOTE ACCESS VPNs
        final RemoteAccessVpnVO vpn = _vpnDao.findByAccountAndVpc(domainRouterVO.getAccountId(), domainRouterVO.getVpcId());
        if (vpn != null) {
            _commandSetupHelper.createApplyVpnCommands(true, vpn, domainRouterVO, cmds);
        }
        // 6) REPROGRAM GUEST NETWORK
        boolean reprogramGuestNtwks = true;
        if (profile.getParameter(Param.ReProgramGuestNetworks) != null && (Boolean) profile.getParameter(Param.ReProgramGuestNetworks) == false) {
            reprogramGuestNtwks = false;
        }
        final VirtualRouterProvider vrProvider = _vrProviderDao.findById(domainRouterVO.getElementId());
        if (vrProvider == null) {
            throw new CloudRuntimeException("Cannot find related virtual router provider of router: " + domainRouterVO.getHostName());
        }
        final Provider provider = Network.Provider.getProvider(vrProvider.getType().toString());
        if (provider == null) {
            throw new CloudRuntimeException("Cannot find related provider of virtual router provider: " + vrProvider.getType().toString());
        }
        if (reprogramGuestNtwks && publicNics.size() > 0) {
            finalizeMonitorService(cmds, profile, domainRouterVO, provider, publicNics.get(0).second().getId(), true);
        }
        for (final Pair<Nic, Network> nicNtwk : guestNics) {
            final Nic guestNic = nicNtwk.first();
            final long guestNetworkId = guestNic.getNetworkId();
            final AggregationControlCommand startCmd = new AggregationControlCommand(Action.Start, domainRouterVO.getInstanceName(), controlNic.getIPv4Address(), _routerControlHelper.getRouterIpInNetwork(guestNetworkId, domainRouterVO.getId()));
            cmds.addCommand(startCmd);
            if (reprogramGuestNtwks) {
                finalizeIpAssocForNetwork(cmds, domainRouterVO, provider, guestNetworkId, vlanMacAddress);
                finalizeNetworkRulesForNetwork(cmds, domainRouterVO, provider, guestNetworkId);
                finalizeMonitorService(cmds, profile, domainRouterVO, provider, guestNetworkId, true);
            }
            finalizeUserDataAndDhcpOnStart(cmds, domainRouterVO, provider, guestNetworkId);
            final AggregationControlCommand finishCmd = new AggregationControlCommand(Action.Finish, domainRouterVO.getInstanceName(), controlNic.getIPv4Address(), _routerControlHelper.getRouterIpInNetwork(guestNetworkId, domainRouterVO.getId()));
            cmds.addCommand(finishCmd);
        }
        // Add network usage commands
        cmds.addCommands(usageCmds);
    }
    return true;
}
Also used : RemoteAccessVpnVO(com.cloud.network.dao.RemoteAccessVpnVO) PrivateIpAddress(com.cloud.network.vpc.PrivateIpAddress) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PrivateIpVO(com.cloud.network.vpc.PrivateIpVO) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) NetworkACLItemVO(com.cloud.network.vpc.NetworkACLItemVO) HypervisorGuru(com.cloud.hypervisor.HypervisorGuru) StaticRouteProfile(com.cloud.network.vpc.StaticRouteProfile) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.cloud.network.Network) AggregationControlCommand(com.cloud.agent.api.routing.AggregationControlCommand) VpcGateway(com.cloud.network.vpc.VpcGateway) PlugNicCommand(com.cloud.agent.api.PlugNicCommand) Pair(com.cloud.utils.Pair) StaticRoute(com.cloud.network.vpc.StaticRoute) PublicIp(com.cloud.network.addr.PublicIp) Nic(com.cloud.vm.Nic) NetworkUsageCommand(com.cloud.agent.api.NetworkUsageCommand) NicProfile(com.cloud.vm.NicProfile) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VirtualRouterProvider(com.cloud.network.VirtualRouterProvider) Provider(com.cloud.network.Network.Provider) VpcVO(com.cloud.network.vpc.VpcVO) PlugNicCommand(com.cloud.agent.api.PlugNicCommand) SetupGuestNetworkCommand(com.cloud.agent.api.SetupGuestNetworkCommand) AggregationControlCommand(com.cloud.agent.api.routing.AggregationControlCommand) Command(com.cloud.agent.api.Command) NetworkUsageCommand(com.cloud.agent.api.NetworkUsageCommand) VirtualRouterProvider(com.cloud.network.VirtualRouterProvider) IPAddressVO(com.cloud.network.dao.IPAddressVO) DomainRouterVO(com.cloud.vm.DomainRouterVO) UserStatisticsVO(com.cloud.user.UserStatisticsVO) SetupGuestNetworkCommand(com.cloud.agent.api.SetupGuestNetworkCommand) VirtualMachine(com.cloud.vm.VirtualMachine)

Example 4 with UserStatisticsVO

use of com.cloud.user.UserStatisticsVO in project cloudstack by apache.

the class UserVmJoinDaoImpl method addVmRxTxDataToResponse.

private void addVmRxTxDataToResponse(UserVmJoinVO userVm, UserVmResponse userVmResponse) {
    Long bytesReceived = 0L;
    Long bytesSent = 0L;
    SearchBuilder<UserStatisticsVO> sb = userStatsDao.createSearchBuilder();
    sb.and("deviceId", sb.entity().getDeviceId(), Op.EQ);
    SearchCriteria<UserStatisticsVO> sc = sb.create();
    sc.setParameters("deviceId", userVm.getId());
    for (UserStatisticsVO stat : userStatsDao.search(sc, null)) {
        bytesReceived += stat.getNetBytesReceived() + stat.getCurrentBytesReceived();
        bytesSent += stat.getNetBytesSent() + stat.getCurrentBytesSent();
    }
    userVmResponse.setBytesReceived(bytesReceived);
    userVmResponse.setBytesSent(bytesSent);
}
Also used : UserStatisticsVO(com.cloud.user.UserStatisticsVO)

Example 5 with UserStatisticsVO

use of com.cloud.user.UserStatisticsVO in project cloudstack by apache.

the class NicPlugInOutRules method accept.

@Override
public boolean accept(final NetworkTopologyVisitor visitor, final VirtualRouter router) throws ResourceUnavailableException {
    _router = router;
    Pair<Map<String, PublicIpAddress>, Map<String, PublicIpAddress>> nicsToChange = getNicsToChangeOnRouter(visitor);
    Map<String, PublicIpAddress> nicsToPlug = nicsToChange.first();
    Map<String, PublicIpAddress> nicsToUnplug = nicsToChange.second();
    NetworkModel networkModel = visitor.getVirtualNetworkApplianceFactory().getNetworkModel();
    VirtualMachineManager itMgr = visitor.getVirtualNetworkApplianceFactory().getItMgr();
    NicDao nicDao = visitor.getVirtualNetworkApplianceFactory().getNicDao();
    VpcVirtualNetworkApplianceService routerService = visitor.getVirtualNetworkApplianceFactory().getRouterService();
    // de-associate IPs before unplugging nics
    if (!nicsToUnplug.isEmpty()) {
        NetworkTopologyContext networkTopologyContext = visitor.getVirtualNetworkApplianceFactory().getNetworkTopologyContext();
        final DataCenterDao dcDao = visitor.getVirtualNetworkApplianceFactory().getDcDao();
        final DataCenterVO dcVO = dcDao.findById(router.getDataCenterId());
        final NetworkTopology networkTopology = networkTopologyContext.retrieveNetworkTopology(dcVO);
        final String typeString = "vpc ip association before unplugging nics";
        final boolean isPodLevelException = false;
        final boolean failWhenDisconnect = false;
        final Long podId = null;
        final VpcIpAssociationRules ipAssociationRules = new VpcIpAssociationRules(_network, _ipAddresses);
        final boolean result = networkTopology.applyRules(_network, router, typeString, isPodLevelException, podId, failWhenDisconnect, new RuleApplierWrapper<RuleApplier>(ipAssociationRules));
        if (!result) {
            s_logger.warn("Failed to de-associate IPs before unplugging nics");
            return false;
        }
    }
    // 1) Unplug the nics
    for (Entry<String, PublicIpAddress> entry : nicsToUnplug.entrySet()) {
        PublicIpAddress ip = entry.getValue();
        NicVO nic = nicDao.findByIp4AddressAndNetworkIdAndInstanceId(ip.getNetworkId(), _router.getId(), ip.getAddress().addr());
        if (nic != null) {
            s_logger.info("Collect network statistics for nic " + nic + " from router " + _router);
            routerService.collectNetworkStatistics(_router, nic);
        }
        Network publicNtwk = null;
        try {
            publicNtwk = networkModel.getNetwork(entry.getValue().getNetworkId());
            URI broadcastUri = BroadcastDomainType.Vlan.toUri(entry.getKey());
            itMgr.removeVmFromNetwork(_router, publicNtwk, broadcastUri);
        } catch (ConcurrentOperationException e) {
            s_logger.warn("Failed to remove router " + _router + " from vlan " + entry.getKey() + " in public network " + publicNtwk + " due to ", e);
            return false;
        }
    }
    _netUsageCommands = new Commands(Command.OnError.Continue);
    VpcDao vpcDao = visitor.getVirtualNetworkApplianceFactory().getVpcDao();
    VpcVO vpc = vpcDao.findById(_router.getVpcId());
    // 2) Plug the nics
    for (String vlanTag : nicsToPlug.keySet()) {
        PublicIpAddress ip = nicsToPlug.get(vlanTag);
        // have to plug the nic(s)
        NicProfile defaultNic = new NicProfile();
        if (ip.isSourceNat()) {
            defaultNic.setDefaultNic(true);
        }
        defaultNic.setIPv4Address(ip.getAddress().addr());
        defaultNic.setIPv4Gateway(ip.getGateway());
        defaultNic.setIPv4Netmask(ip.getNetmask());
        defaultNic.setMacAddress(ip.getMacAddress());
        defaultNic.setBroadcastType(BroadcastDomainType.Vlan);
        defaultNic.setBroadcastUri(BroadcastDomainType.Vlan.toUri(ip.getVlanTag()));
        defaultNic.setIsolationUri(IsolationType.Vlan.toUri(ip.getVlanTag()));
        NicProfile publicNic = null;
        Network publicNtwk = null;
        try {
            publicNtwk = networkModel.getNetwork(ip.getNetworkId());
            publicNic = itMgr.addVmToNetwork(_router, publicNtwk, defaultNic);
        } catch (ConcurrentOperationException e) {
            s_logger.warn("Failed to add router " + _router + " to vlan " + vlanTag + " in public network " + publicNtwk + " due to ", e);
        } catch (InsufficientCapacityException e) {
            s_logger.warn("Failed to add router " + _router + " to vlan " + vlanTag + " in public network " + publicNtwk + " due to ", e);
        } finally {
            if (publicNic == null) {
                s_logger.warn("Failed to add router " + _router + " to vlan " + vlanTag + " in public network " + publicNtwk);
                return false;
            }
        }
        // Create network usage commands. Send commands to router after
        // IPAssoc
        NetworkUsageCommand netUsageCmd = new NetworkUsageCommand(_router.getPrivateIpAddress(), _router.getInstanceName(), true, defaultNic.getIPv4Address(), vpc.getCidr());
        _netUsageCommands.addCommand(netUsageCmd);
        UserStatisticsDao userStatsDao = visitor.getVirtualNetworkApplianceFactory().getUserStatsDao();
        UserStatisticsVO stats = userStatsDao.findBy(_router.getAccountId(), _router.getDataCenterId(), publicNtwk.getId(), publicNic.getIPv4Address(), _router.getId(), _router.getType().toString());
        if (stats == null) {
            stats = new UserStatisticsVO(_router.getAccountId(), _router.getDataCenterId(), publicNic.getIPv4Address(), _router.getId(), _router.getType().toString(), publicNtwk.getId());
            userStatsDao.persist(stats);
        }
    }
    // VpcIpAssociation is done.
    return true;
}
Also used : NicDao(com.cloud.vm.dao.NicDao) URI(java.net.URI) UserStatisticsDao(com.cloud.user.dao.UserStatisticsDao) PublicIpAddress(com.cloud.network.PublicIpAddress) VpcDao(com.cloud.network.vpc.dao.VpcDao) Network(com.cloud.network.Network) Commands(com.cloud.agent.manager.Commands) NetworkModel(com.cloud.network.NetworkModel) VpcVirtualNetworkApplianceService(com.cloud.network.VpcVirtualNetworkApplianceService) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) NicVO(com.cloud.vm.NicVO) DataCenterVO(com.cloud.dc.DataCenterVO) DataCenterDao(com.cloud.dc.dao.DataCenterDao) NetworkTopologyContext(org.apache.cloudstack.network.topology.NetworkTopologyContext) NetworkUsageCommand(com.cloud.agent.api.NetworkUsageCommand) NicProfile(com.cloud.vm.NicProfile) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) VpcVO(com.cloud.network.vpc.VpcVO) NetworkTopology(org.apache.cloudstack.network.topology.NetworkTopology) VirtualMachineManager(com.cloud.vm.VirtualMachineManager) HashMap(java.util.HashMap) Map(java.util.Map) UserStatisticsVO(com.cloud.user.UserStatisticsVO)

Aggregations

UserStatisticsVO (com.cloud.user.UserStatisticsVO)19 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)9 ArrayList (java.util.ArrayList)9 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)8 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)7 HashMap (java.util.HashMap)7 ConfigurationException (javax.naming.ConfigurationException)7 NetworkUsageCommand (com.cloud.agent.api.NetworkUsageCommand)6 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)6 Network (com.cloud.network.Network)6 PreparedStatement (java.sql.PreparedStatement)6 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)5 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)4 Nic (com.cloud.vm.Nic)4 ResultSet (java.sql.ResultSet)4 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)3 Provider (com.cloud.network.Network.Provider)3 VirtualRouterProvider (com.cloud.network.VirtualRouterProvider)3 VpcVO (com.cloud.network.vpc.VpcVO)3 NicProfile (com.cloud.vm.NicProfile)3