Search in sources :

Example 16 with Command

use of com.cloud.legacymodel.communication.command.Command in project cosmic by MissionCriticalCloud.

the class GsonHelper method setDefaultGsonConfig.

static Gson setDefaultGsonConfig(final GsonBuilder builder) {
    final InterfaceTypeAdaptor<DataStoreTO> dsAdaptor = new InterfaceTypeAdaptor<>();
    builder.registerTypeAdapter(DataStoreTO.class, dsAdaptor);
    final InterfaceTypeAdaptor<DataTO> dtAdaptor = new InterfaceTypeAdaptor<>();
    builder.registerTypeAdapter(DataTO.class, dtAdaptor);
    final ArrayTypeAdaptor<Command> cmdAdaptor = new ArrayTypeAdaptor<>();
    builder.registerTypeAdapter(new TypeToken<Command[]>() {
    }.getType(), cmdAdaptor);
    final ArrayTypeAdaptor<Answer> ansAdaptor = new ArrayTypeAdaptor<>();
    builder.registerTypeAdapter(new TypeToken<Answer[]>() {
    }.getType(), ansAdaptor);
    final Gson gson = builder.create();
    dsAdaptor.initGson(gson);
    dtAdaptor.initGson(gson);
    cmdAdaptor.initGson(gson);
    ansAdaptor.initGson(gson);
    return gson;
}
Also used : Answer(com.cloud.legacymodel.communication.answer.Answer) DataStoreTO(com.cloud.legacymodel.to.DataStoreTO) DataTO(com.cloud.legacymodel.to.DataTO) Command(com.cloud.legacymodel.communication.command.Command) TypeToken(com.google.gson.reflect.TypeToken) Gson(com.google.gson.Gson)

Example 17 with Command

use of com.cloud.legacymodel.communication.command.Command in project cosmic by MissionCriticalCloud.

the class Request method log.

protected String log(final String msg, final boolean logContent) {
    final StringBuilder content = new StringBuilder();
    if (logContent) {
        if (_cmds == null) {
            try {
                _cmds = s_gson.fromJson(_content, this instanceof Response ? Answer[].class : Command[].class);
            } catch (final RuntimeException e) {
                s_logger.error("Unable to convert to json: " + _content);
                throw e;
            }
        }
        s_gogger.toJson(_cmds, content);
        if (content.length() <= (1 + _cmds.length * 3)) {
            return null;
        }
    } else {
        if (_cmds == null) {
            _cmds = s_gson.fromJson(_content, this instanceof Response ? Answer[].class : Command[].class);
        }
        content.append("{ ");
        for (final Command cmd : _cmds) {
            content.append(cmd.getClass().getSimpleName()).append(", ");
        }
        content.replace(content.length() - 2, content.length(), " }");
    }
    final StringBuilder buf = new StringBuilder("Seq ");
    buf.append(_agentId).append("-").append(_seq).append(": ");
    buf.append(msg);
    buf.append(" { ").append(getType());
    if (_agentName != null) {
        buf.append(", MgmtId: ").append(_mgmtId).append(", via: ").append(_via).append("(" + _agentName + ")");
    } else {
        buf.append(", MgmtId: ").append(_mgmtId).append(", via: ").append(_via);
    }
    buf.append(", Ver: ").append(_ver.toString());
    buf.append(", Flags: ").append(Integer.toBinaryString(getFlags())).append(", ");
    final String cleanContent = content.toString();
    if (cleanContent.contains("password")) {
        buf.append(cleanPassword(cleanContent));
    } else {
        buf.append(content);
    }
    buf.append(" }");
    return buf.toString();
}
Also used : Answer(com.cloud.legacymodel.communication.answer.Answer) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) Command(com.cloud.legacymodel.communication.command.Command)

Example 18 with Command

use of com.cloud.legacymodel.communication.command.Command in project cosmic by MissionCriticalCloud.

the class LibvirtRequestWrapper method processAnnotations.

protected Hashtable<Class<? extends Command>, LibvirtCommandWrapper> processAnnotations(final Set<Class<? extends LibvirtCommandWrapper>> wrappers) {
    final String errorMessage = "Error when adding Xen command to map ==> '{0}'. LibvirtCommandWrapper class is ==> '{1}'";
    final Hashtable<Class<? extends Command>, LibvirtCommandWrapper> commands = new Hashtable<>();
    for (final Class<? extends LibvirtCommandWrapper> wrapper : wrappers) {
        final ResourceWrapper annotation = wrapper.getAnnotation(ResourceWrapper.class);
        if (annotation == null) {
            // Just in case people add classes without the annotation in the package and we don't see it.
            continue;
        }
        try {
            commands.put(annotation.handles(), wrapper.newInstance());
        } catch (final InstantiationException | IllegalAccessException e) {
            s_logger.warn(MessageFormat.format(errorMessage, e.getLocalizedMessage(), wrapper.toString()));
        }
    }
    return commands;
}
Also used : ResourceWrapper(com.cloud.common.request.ResourceWrapper) Command(com.cloud.legacymodel.communication.command.Command) Hashtable(java.util.Hashtable)

Example 19 with Command

use of com.cloud.legacymodel.communication.command.Command 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, 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.legacymodel.network.Ip) PublicIp(com.cloud.network.addr.PublicIp) ArrayList(java.util.ArrayList) Vpc(com.cloud.legacymodel.network.vpc.Vpc) PrivateIpVO(com.cloud.network.vpc.PrivateIpVO) NetworkACLItemVO(com.cloud.network.vpc.NetworkACLItemVO) StaticRouteProfile(com.cloud.legacymodel.network.vpc.StaticRouteProfile) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) Network(com.cloud.legacymodel.network.Network) AggregationControlCommand(com.cloud.legacymodel.communication.command.AggregationControlCommand) VMOverviewTO(com.cloud.legacymodel.to.VMOverviewTO) NetworkOverviewTO(com.cloud.legacymodel.to.NetworkOverviewTO) PlugNicCommand(com.cloud.legacymodel.communication.command.PlugNicCommand) Pair(com.cloud.legacymodel.utils.Pair) PublicIp(com.cloud.network.addr.PublicIp) Nic(com.cloud.legacymodel.network.Nic) NetworkUsageCommand(com.cloud.legacymodel.communication.command.NetworkUsageCommand) UpdateNetworkOverviewCommand(com.cloud.legacymodel.communication.command.UpdateNetworkOverviewCommand) NicProfile(com.cloud.vm.NicProfile) ConfigurationException(javax.naming.ConfigurationException) ResourceUnavailableException(com.cloud.legacymodel.exceptions.ResourceUnavailableException) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) Provider(com.cloud.legacymodel.network.Network.Provider) VirtualRouterProvider(com.cloud.network.VirtualRouterProvider) VpcVO(com.cloud.network.vpc.VpcVO) PlugNicCommand(com.cloud.legacymodel.communication.command.PlugNicCommand) Command(com.cloud.legacymodel.communication.command.Command) NetworkUsageCommand(com.cloud.legacymodel.communication.command.NetworkUsageCommand) AggregationControlCommand(com.cloud.legacymodel.communication.command.AggregationControlCommand) UpdateVmOverviewCommand(com.cloud.legacymodel.communication.command.UpdateVmOverviewCommand) UpdateNetworkOverviewCommand(com.cloud.legacymodel.communication.command.UpdateNetworkOverviewCommand) VirtualRouterProvider(com.cloud.network.VirtualRouterProvider) IPAddressVO(com.cloud.network.dao.IPAddressVO) DomainRouterVO(com.cloud.vm.DomainRouterVO) UserStatisticsVO(com.cloud.user.UserStatisticsVO) UpdateVmOverviewCommand(com.cloud.legacymodel.communication.command.UpdateVmOverviewCommand)

Example 20 with Command

use of com.cloud.legacymodel.communication.command.Command in project cosmic by MissionCriticalCloud.

the class XenServerGuru method finalizeExpungeVolumes.

@Override
public List<Command> finalizeExpungeVolumes(final VirtualMachine vm) {
    final List<Command> commands = new ArrayList<>();
    final List<VolumeVO> volumes = _volumeDao.findByInstance(vm.getId());
    // will simply lead to the SR that supports the root volume being removed
    if (volumes != null) {
        for (final VolumeVO volume : volumes) {
            final StoragePoolVO storagePool = _storagePoolDao.findById(volume.getPoolId());
            // so the volume was never assigned to a storage pool)
            if (storagePool != null && storagePool.isManaged()) {
                final DataTO volTO = _volFactory.getVolume(volume.getId()).getTO();
                final DiskTO disk = new DiskTO(volTO, volume.getDeviceId(), volume.getPath(), volume.getVolumeType());
                final DettachCommand cmd = new DettachCommand(disk, vm.getInstanceName());
                cmd.setManaged(true);
                cmd.setStorageHost(storagePool.getHostAddress());
                cmd.setStoragePort(storagePool.getPort());
                cmd.set_iScsiName(volume.get_iScsiName());
                commands.add(cmd);
            }
        }
    }
    return commands;
}
Also used : DataTO(com.cloud.legacymodel.to.DataTO) VolumeVO(com.cloud.storage.VolumeVO) DettachCommand(com.cloud.legacymodel.communication.command.DettachCommand) StorageSubSystemCommand(com.cloud.legacymodel.communication.command.StorageSubSystemCommand) Command(com.cloud.legacymodel.communication.command.Command) CopyCommand(com.cloud.legacymodel.communication.command.CopyCommand) ArrayList(java.util.ArrayList) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO) DettachCommand(com.cloud.legacymodel.communication.command.DettachCommand) DiskTO(com.cloud.legacymodel.to.DiskTO)

Aggregations

Command (com.cloud.legacymodel.communication.command.Command)21 Answer (com.cloud.legacymodel.communication.answer.Answer)13 StartupCommand (com.cloud.legacymodel.communication.command.startup.StartupCommand)10 AgentControlCommand (com.cloud.legacymodel.communication.command.agentcontrol.AgentControlCommand)9 PingCommand (com.cloud.legacymodel.communication.command.PingCommand)8 ReadyCommand (com.cloud.legacymodel.communication.command.ReadyCommand)7 ShutdownCommand (com.cloud.legacymodel.communication.command.ShutdownCommand)7 StartupRoutingCommand (com.cloud.legacymodel.communication.command.startup.StartupRoutingCommand)7 AgentUnavailableException (com.cloud.legacymodel.exceptions.AgentUnavailableException)7 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)7 PingRoutingCommand (com.cloud.legacymodel.communication.command.PingRoutingCommand)6 StartupAnswer (com.cloud.legacymodel.communication.answer.StartupAnswer)5 Response (com.cloud.common.transport.Response)4 AgentControlAnswer (com.cloud.legacymodel.communication.answer.AgentControlAnswer)4 CheckHealthCommand (com.cloud.legacymodel.communication.command.CheckHealthCommand)4 CronCommand (com.cloud.legacymodel.communication.command.CronCommand)4 MaintainCommand (com.cloud.legacymodel.communication.command.MaintainCommand)4 StartupProxyCommand (com.cloud.legacymodel.communication.command.startup.StartupProxyCommand)4 StartupSecondaryStorageCommand (com.cloud.legacymodel.communication.command.startup.StartupSecondaryStorageCommand)4 StartupStorageCommand (com.cloud.legacymodel.communication.command.startup.StartupStorageCommand)4