Search in sources :

Example 6 with NetworkOverviewTO

use of com.cloud.agent.api.to.overviews.NetworkOverviewTO in project cosmic by MissionCriticalCloud.

the class AdvancedNetworkVisitor method visit.

@Override
public boolean visit(final PrivateGatewayRules privateGW) throws ResourceUnavailableException {
    final VirtualRouter router = privateGW.getRouter();
    final NicProfile nicProfile = privateGW.getNicProfile();
    final boolean add = privateGW.isAddOperation();
    if (router.getState() == State.Running) {
        final PrivateIpVO ipVO = privateGW.retrivePrivateIP(this);
        final Network network = privateGW.retrievePrivateNetwork(this);
        final String netmask = NetUtils.getCidrNetmask(network.getCidr());
        final PrivateIpAddress ip = new PrivateIpAddress(ipVO, network.getBroadcastUri().toString(), network.getGateway(), netmask, nicProfile.getMacAddress());
        final Commands cmds = new Commands(Command.OnError.Stop);
        final List<Ip> ipsToExclude = new ArrayList<>();
        if (!add) {
            ipsToExclude.add(new Ip(ip.getIpAddress()));
        }
        final NetworkOverviewTO networkOverview = _commandSetupHelper.createNetworkOverviewFromRouter(router, new ArrayList<>(), ipsToExclude, new ArrayList<>(), null, null);
        final UpdateNetworkOverviewCommand updateNetworkOverviewCommand = _commandSetupHelper.createUpdateNetworkOverviewCommand(router, networkOverview);
        cmds.addCommand(updateNetworkOverviewCommand);
        try {
            if (_networkGeneralHelper.sendCommandsToRouter(router, cmds)) {
                s_logger.debug("Successfully applied ip association for ip " + ip + " in vpc network " + network);
                return true;
            } else {
                s_logger.warn("Failed to associate ip address " + ip + " in vpc network " + network);
                return false;
            }
        } catch (final Exception ex) {
            s_logger.warn("Failed to send  " + (add ? "add " : "delete ") + " private network " + network + " commands to rotuer ");
            return false;
        }
    } else if (router.getState() == State.Stopped || router.getState() == State.Stopping) {
        s_logger.debug("Router " + router.getInstanceName() + " is in " + router.getState() + ", so not sending setup private network command to the backend");
    } else {
        s_logger.warn("Unable to setup private gateway, virtual router " + router + " is not in the right state " + router.getState());
        throw new ResourceUnavailableException("Unable to setup Private gateway on the backend," + " virtual router " + router + " is not in the right state", DataCenter.class, router.getDataCenterId());
    }
    return true;
}
Also used : PrivateIpAddress(com.cloud.network.vpc.PrivateIpAddress) Ip(com.cloud.utils.net.Ip) ArrayList(java.util.ArrayList) PrivateIpVO(com.cloud.network.vpc.PrivateIpVO) UpdateNetworkOverviewCommand(com.cloud.agent.api.UpdateNetworkOverviewCommand) NicProfile(com.cloud.vm.NicProfile) VirtualRouter(com.cloud.network.router.VirtualRouter) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) DataCenter(com.cloud.dc.DataCenter) Network(com.cloud.network.Network) Commands(com.cloud.agent.manager.Commands) NetworkOverviewTO(com.cloud.agent.api.to.overviews.NetworkOverviewTO) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException)

Example 7 with NetworkOverviewTO

use of com.cloud.agent.api.to.overviews.NetworkOverviewTO in project cosmic by MissionCriticalCloud.

the class AdvancedNetworkVisitor method visit.

@Override
public boolean visit(final VpcIpAssociationRules vpcip) throws ResourceUnavailableException {
    final VirtualRouter router = vpcip.getRouter();
    final Commands cmds = new Commands(Command.OnError.Continue);
    final List<PublicIpAddress> ipsToSend = vpcip.getIpsToSend();
    if (!ipsToSend.isEmpty()) {
        final List<Ip> ipsToExclude = new ArrayList<>();
        _commandSetupHelper.findIpsToExclude(ipsToSend, ipsToExclude);
        final NetworkOverviewTO networkOverview = _commandSetupHelper.createNetworkOverviewFromRouter(router, new ArrayList<>(), ipsToExclude, new ArrayList<>(), null, null);
        final UpdateNetworkOverviewCommand updateNetworkOverviewCommand = _commandSetupHelper.createUpdateNetworkOverviewCommand(router, networkOverview);
        updateNetworkOverviewCommand.setPlugNics(true);
        cmds.addCommand(updateNetworkOverviewCommand);
        return _networkGeneralHelper.sendCommandsToRouter(router, cmds);
    } else {
        return true;
    }
}
Also used : PublicIpAddress(com.cloud.network.PublicIpAddress) Ip(com.cloud.utils.net.Ip) Commands(com.cloud.agent.manager.Commands) ArrayList(java.util.ArrayList) NetworkOverviewTO(com.cloud.agent.api.to.overviews.NetworkOverviewTO) UpdateNetworkOverviewCommand(com.cloud.agent.api.UpdateNetworkOverviewCommand) VirtualRouter(com.cloud.network.router.VirtualRouter)

Example 8 with NetworkOverviewTO

use of com.cloud.agent.api.to.overviews.NetworkOverviewTO in project cosmic by MissionCriticalCloud.

the class AdvancedNetworkVisitor method visit.

@Override
public boolean visit(final StaticRoutesRules staticRoutesRules) throws ResourceUnavailableException {
    final VirtualRouter router = staticRoutesRules.getRouter();
    final Commands cmds = new Commands(Command.OnError.Continue);
    final NetworkOverviewTO networkOverview = _commandSetupHelper.createNetworkOverviewFromRouter(router, new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), null, null);
    final UpdateNetworkOverviewCommand updateNetworkOverviewCommand = _commandSetupHelper.createUpdateNetworkOverviewCommand(router, networkOverview);
    cmds.addCommand(updateNetworkOverviewCommand);
    return _networkGeneralHelper.sendCommandsToRouter(router, cmds);
}
Also used : Commands(com.cloud.agent.manager.Commands) NetworkOverviewTO(com.cloud.agent.api.to.overviews.NetworkOverviewTO) UpdateNetworkOverviewCommand(com.cloud.agent.api.UpdateNetworkOverviewCommand) VirtualRouter(com.cloud.network.router.VirtualRouter)

Example 9 with NetworkOverviewTO

use of com.cloud.agent.api.to.overviews.NetworkOverviewTO in project cosmic by MissionCriticalCloud.

the class VpcVirtualNetworkApplianceManagerImpl method applySite2SiteVpn.

private boolean applySite2SiteVpn(final boolean isCreate, final VirtualRouter router, final Site2SiteVpnConnection conn) throws ResourceUnavailableException {
    final Commands cmds = new Commands(Command.OnError.Continue);
    final NetworkOverviewTO networkOverview = _commandSetupHelper.createNetworkOverviewFromRouter(router, new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), null, isCreate ? null : conn);
    final UpdateNetworkOverviewCommand updateNetworkOverviewCommand = _commandSetupHelper.createUpdateNetworkOverviewCommand(router, networkOverview);
    cmds.addCommand(updateNetworkOverviewCommand);
    return _nwHelper.sendCommandsToRouter(router, cmds);
}
Also used : Commands(com.cloud.agent.manager.Commands) NetworkOverviewTO(com.cloud.agent.api.to.overviews.NetworkOverviewTO) UpdateNetworkOverviewCommand(com.cloud.agent.api.UpdateNetworkOverviewCommand)

Example 10 with NetworkOverviewTO

use of com.cloud.agent.api.to.overviews.NetworkOverviewTO in project cosmic by MissionCriticalCloud.

the class VpcVirtualNetworkApplianceManagerImpl method finalizeCommandsOnStart.

@Override
public boolean finalizeCommandsOnStart(final Commands cmds, final VirtualMachineProfile profile) {
    final DomainRouterVO domainRouterVO = _routerDao.findById(profile.getId());
    final boolean isVpc = domainRouterVO.getVpcId() != null;
    if (!isVpc) {
        return super.finalizeCommandsOnStart(cmds, profile);
    }
    if (domainRouterVO.getState() == State.Starting || domainRouterVO.getState() == State.Running) {
        final List<Nic> nicsToExclude = new ArrayList<>();
        final List<Ip> ipsToExclude = new ArrayList<>();
        final List<StaticRouteProfile> staticRoutesToExclude = new ArrayList<>();
        // 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>> syncNics = new ArrayList<>();
        final List<Pair<Nic, Network>> guestNics = new ArrayList<>();
        final List<Pair<Nic, Network>> publicNics = new ArrayList<>();
        final List<? extends Nic> routerNics = _nicDao.listByVmId(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<>(routerNic, network);
                if (GuestType.Sync.equals(network.getGuestType())) {
                    syncNics.add(guestNic);
                } else {
                    guestNics.add(guestNic);
                }
            } else if (network.getTrafficType() == TrafficType.Public) {
                final Pair<Nic, Network> publicNic = new Pair<>(routerNic, network);
                publicNics.add(publicNic);
            }
        }
        final List<Command> usageCmds = new ArrayList<>();
        // 3) PREPARE PLUG NIC COMMANDS
        try {
            // add VPC router to sync networks
            for (final Pair<Nic, Network> nicNtwk : syncNics) {
                final Nic syncNic = nicNtwk.first();
                // plug sync nic
                final PlugNicCommand plugNicCmd = new PlugNicCommand(_nwHelper.getNicTO(domainRouterVO, syncNic.getNetworkId(), null), domainRouterVO.getInstanceName(), domainRouterVO.getType());
                cmds.addCommand(plugNicCmd);
            }
            // add VPC router to public networks
            final List<PublicIp> sourceNat = new ArrayList<>(1);
            for (final Pair<Nic, Network> nicNtwk : publicNics) {
                final Nic publicNic = nicNtwk.first();
                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());
                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);
                }
                _commandSetupHelper.createPublicIpACLsCommands(domainRouterVO, cmds);
            }
            // create ip assoc for source nat
            if (!sourceNat.isEmpty()) {
                _commandSetupHelper.findIpsToExclude(sourceNat, ipsToExclude);
            }
            // add VPC router to guest networks
            for (final Pair<Nic, Network> nicNtwk : guestNics) {
                final Nic guestNic = nicNtwk.first();
                // plug guest nic
                final PlugNicCommand plugNicCmd = new PlugNicCommand(_nwHelper.getNicTO(domainRouterVO, guestNic.getNetworkId(), null), domainRouterVO.getInstanceName(), domainRouterVO.getType());
                cmds.addCommand(plugNicCmd);
                if (_networkModel.isPrivateGateway(guestNic.getNetworkId())) {
                    // set private network
                    final PrivateIpVO ipVO = _privateIpDao.findByIpAndSourceNetworkId(guestNic.getNetworkId(), guestNic.getIPv4Address());
                    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);
                    }
                }
            }
        } catch (final Exception ex) {
            s_logger.warn("Failed to add router " + domainRouterVO + " to network due to exception ", ex);
            return false;
        }
        // 4) REPROGRAM GUEST NETWORK
        boolean reprogramGuestNtwks = profile.getParameter(Param.ReProgramGuestNetworks) == null || (Boolean) profile.getParameter(Param.ReProgramGuestNetworks);
        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 = Provider.getProvider(vrProvider.getType().toString());
        if (provider == null) {
            throw new CloudRuntimeException("Cannot find related provider of virtual router provider: " + vrProvider.getType().toString());
        }
        boolean isDhcpSupported = false;
        for (final Pair<Nic, Network> nicNtwk : guestNics) {
            final Nic guestNic = nicNtwk.first();
            final AggregationControlCommand startCmd = new AggregationControlCommand(Action.Start, domainRouterVO.getInstanceName(), controlNic.getIPv4Address(), _routerControlHelper.getRouterIpInNetwork(guestNic.getNetworkId(), domainRouterVO.getId()));
            cmds.addCommand(startCmd);
            if (reprogramGuestNtwks) {
                finalizeIpAssocForNetwork(domainRouterVO, provider, guestNic.getNetworkId(), ipsToExclude);
                finalizeNetworkRulesForNetwork(cmds, domainRouterVO, provider, guestNic.getNetworkId());
            }
            isDhcpSupported = isDhcpSupported || _networkModel.isProviderSupportServiceInNetwork(guestNic.getNetworkId(), Service.Dhcp, provider);
            final AggregationControlCommand finishCmd = new AggregationControlCommand(Action.Finish, domainRouterVO.getInstanceName(), controlNic.getIPv4Address(), _routerControlHelper.getRouterIpInNetwork(guestNic.getNetworkId(), domainRouterVO.getId()));
            cmds.addCommand(finishCmd);
        }
        final NetworkOverviewTO networkOverview = _commandSetupHelper.createNetworkOverviewFromRouter(domainRouterVO, nicsToExclude, ipsToExclude, staticRoutesToExclude, null, null);
        final UpdateNetworkOverviewCommand updateNetworkOverviewCommand = _commandSetupHelper.createUpdateNetworkOverviewCommand(domainRouterVO, networkOverview);
        updateNetworkOverviewCommand.setPlugNics(true);
        cmds.addCommand(updateNetworkOverviewCommand);
        if (isDhcpSupported) {
            final VMOverviewTO vmOverview = _commandSetupHelper.createVmOverviewFromRouter(domainRouterVO);
            final UpdateVmOverviewCommand updateVmOverviewCommand = _commandSetupHelper.createUpdateVmOverviewCommand(domainRouterVO, vmOverview);
            cmds.addCommand(updateVmOverviewCommand);
        }
        // 5) RE-APPLY VR Configuration
        final Vpc vpc = _vpcDao.findById(domainRouterVO.getVpcId());
        _commandSetupHelper.createVRConfigCommands(vpc, domainRouterVO, cmds);
        // Add network usage commands
        cmds.addCommands(usageCmds);
    }
    return true;
}
Also used : Ip(com.cloud.utils.net.Ip) PublicIp(com.cloud.network.addr.PublicIp) ArrayList(java.util.ArrayList) Vpc(com.cloud.network.vpc.Vpc) PrivateIpVO(com.cloud.network.vpc.PrivateIpVO) NetworkACLItemVO(com.cloud.network.vpc.NetworkACLItemVO) StaticRouteProfile(com.cloud.network.vpc.StaticRouteProfile) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.cloud.network.Network) AggregationControlCommand(com.cloud.agent.api.routing.AggregationControlCommand) VMOverviewTO(com.cloud.agent.api.to.overviews.VMOverviewTO) NetworkOverviewTO(com.cloud.agent.api.to.overviews.NetworkOverviewTO) PlugNicCommand(com.cloud.agent.api.PlugNicCommand) Pair(com.cloud.utils.Pair) PublicIp(com.cloud.network.addr.PublicIp) Nic(com.cloud.vm.Nic) NetworkUsageCommand(com.cloud.agent.api.NetworkUsageCommand) UpdateNetworkOverviewCommand(com.cloud.agent.api.UpdateNetworkOverviewCommand) NicProfile(com.cloud.vm.NicProfile) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) 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) UpdateNetworkOverviewCommand(com.cloud.agent.api.UpdateNetworkOverviewCommand) AggregationControlCommand(com.cloud.agent.api.routing.AggregationControlCommand) UpdateVmOverviewCommand(com.cloud.agent.api.UpdateVmOverviewCommand) 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) UpdateVmOverviewCommand(com.cloud.agent.api.UpdateVmOverviewCommand)

Aggregations

NetworkOverviewTO (com.cloud.agent.api.to.overviews.NetworkOverviewTO)15 UpdateNetworkOverviewCommand (com.cloud.agent.api.UpdateNetworkOverviewCommand)13 Commands (com.cloud.agent.manager.Commands)12 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)8 ArrayList (java.util.ArrayList)7 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)5 Network (com.cloud.network.Network)5 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)5 Ip (com.cloud.utils.net.Ip)5 ConfigurationException (javax.naming.ConfigurationException)5 DataCenter (com.cloud.dc.DataCenter)4 VirtualRouter (com.cloud.network.router.VirtualRouter)4 PrivateIpVO (com.cloud.network.vpc.PrivateIpVO)3 Vpc (com.cloud.network.vpc.Vpc)3 NicProfile (com.cloud.vm.NicProfile)3 UpdateVmOverviewCommand (com.cloud.agent.api.UpdateVmOverviewCommand)2 VMOverviewTO (com.cloud.agent.api.to.overviews.VMOverviewTO)2 PublicIpAddress (com.cloud.network.PublicIpAddress)2 PrivateIpAddress (com.cloud.network.vpc.PrivateIpAddress)2 Nic (com.cloud.vm.Nic)2