Search in sources :

Example 1 with MessageSubscriber

use of org.apache.cloudstack.framework.messagebus.MessageSubscriber in project cloudstack by apache.

the class TestMessageBus method testMiscMatch.

@Test
public void testMiscMatch() {
    MessageSubscriber subscriberAtParentLevel = new MessageSubscriber() {

        @Override
        public void onPublishMessage(String senderAddress, String subject, Object args) {
            Assert.assertTrue(subject.startsWith(("Host")) || subject.startsWith("VM"));
        }
    };
    MessageSubscriber subscriberAtChildLevel = new MessageSubscriber() {

        @Override
        public void onPublishMessage(String senderAddress, String subject, Object args) {
            Assert.assertTrue(subject.equals("Host.123"));
        }
    };
    subscriberAtParentLevel = Mockito.spy(subscriberAtParentLevel);
    subscriberAtChildLevel = Mockito.spy(subscriberAtChildLevel);
    _messageBus.subscribe("Host", subscriberAtParentLevel);
    _messageBus.subscribe("VM", subscriberAtParentLevel);
    _messageBus.subscribe("Host.123", subscriberAtChildLevel);
    _messageBus.publish(null, "Host.123", PublishScope.LOCAL, null);
    _messageBus.publish(null, "Host.321", PublishScope.LOCAL, null);
    _messageBus.publish(null, "VM.123", PublishScope.LOCAL, null);
    Mockito.verify(subscriberAtParentLevel).onPublishMessage(null, "Host.123", null);
    Mockito.verify(subscriberAtParentLevel).onPublishMessage(null, "Host.321", null);
    Mockito.verify(subscriberAtParentLevel).onPublishMessage(null, "VM.123", null);
    Mockito.verify(subscriberAtChildLevel).onPublishMessage(null, "Host.123", null);
    Mockito.reset(subscriberAtParentLevel);
    Mockito.reset(subscriberAtChildLevel);
    _messageBus.unsubscribe(null, subscriberAtParentLevel);
    _messageBus.publish(null, "Host.123", PublishScope.LOCAL, null);
    _messageBus.publish(null, "VM.123", PublishScope.LOCAL, null);
    Mockito.verify(subscriberAtChildLevel).onPublishMessage(null, "Host.123", null);
    Mockito.verify(subscriberAtParentLevel, Mockito.times(0)).onPublishMessage(null, "Host.123", null);
    Mockito.verify(subscriberAtParentLevel, Mockito.times(0)).onPublishMessage(null, "VM.123", null);
    _messageBus.clearAll();
}
Also used : MessageSubscriber(org.apache.cloudstack.framework.messagebus.MessageSubscriber) Test(org.junit.Test)

Example 2 with MessageSubscriber

use of org.apache.cloudstack.framework.messagebus.MessageSubscriber in project cloudstack by apache.

the class TestMessageBus method testExactSubjectMatch.

@Test
public void testExactSubjectMatch() {
    _messageBus.subscribe("Host", new MessageSubscriber() {

        @Override
        public void onPublishMessage(String senderAddress, String subject, Object args) {
            Assert.assertEquals(subject, "Host");
        }
    });
    _messageBus.publish(null, "Host", PublishScope.LOCAL, null);
    _messageBus.publish(null, "VM", PublishScope.LOCAL, null);
    _messageBus.clearAll();
}
Also used : MessageSubscriber(org.apache.cloudstack.framework.messagebus.MessageSubscriber) Test(org.junit.Test)

Example 3 with MessageSubscriber

use of org.apache.cloudstack.framework.messagebus.MessageSubscriber in project cloudstack by apache.

the class IAMApiServiceImpl method configure.

@Override
public boolean configure(final String name, final Map<String, Object> params) throws ConfigurationException {
    _messageBus.subscribe(AccountManager.MESSAGE_ADD_ACCOUNT_EVENT, new MessageSubscriber() {

        @Override
        public void onPublishMessage(String senderAddress, String subject, Object obj) {
            HashMap<Long, Long> acctGroupMap = (HashMap<Long, Long>) obj;
            for (Long accountId : acctGroupMap.keySet()) {
                Long groupId = acctGroupMap.get(accountId);
                s_logger.debug("MessageBus message: new Account Added: " + accountId + ", adding it to groupId :" + groupId);
                addAccountToIAMGroup(accountId, groupId);
                // add it to domain group too
                AccountVO account = _accountDao.findById(accountId);
                Domain domain = _domainDao.findById(account.getDomainId());
                if (domain != null) {
                    List<IAMGroup> domainGroups = listDomainGroup(domain);
                    if (domainGroups != null) {
                        for (IAMGroup group : domainGroups) {
                            addAccountToIAMGroup(accountId, new Long(group.getId()));
                        }
                    }
                }
            }
        }
    });
    _messageBus.subscribe(AccountManager.MESSAGE_REMOVE_ACCOUNT_EVENT, new MessageSubscriber() {

        @Override
        public void onPublishMessage(String senderAddress, String subject, Object obj) {
            Long accountId = ((Long) obj);
            if (accountId != null) {
                s_logger.debug("MessageBus message: Account removed: " + accountId + ", releasing the group associations");
                removeAccountFromIAMGroups(accountId);
            }
        }
    });
    _messageBus.subscribe(DomainManager.MESSAGE_ADD_DOMAIN_EVENT, new MessageSubscriber() {

        @Override
        public void onPublishMessage(String senderAddress, String subject, Object obj) {
            Long domainId = ((Long) obj);
            if (domainId != null) {
                s_logger.debug("MessageBus message: new Domain created: " + domainId + ", creating a new group");
                Domain domain = _domainDao.findById(domainId);
                _iamSrv.createIAMGroup("DomainGrp-" + domain.getUuid(), "Domain group", domain.getPath());
            }
        }
    });
    _messageBus.subscribe(DomainManager.MESSAGE_REMOVE_DOMAIN_EVENT, new MessageSubscriber() {

        @Override
        public void onPublishMessage(String senderAddress, String subject, Object obj) {
            Long domainId = ((Long) obj);
            if (domainId != null) {
                s_logger.debug("MessageBus message: Domain removed: " + domainId + ", removing the domain group");
                Domain domain = _domainDao.findById(domainId);
                List<IAMGroup> groups = listDomainGroup(domain);
                for (IAMGroup group : groups) {
                    _iamSrv.deleteIAMGroup(group.getId());
                }
            }
        }
    });
    _messageBus.subscribe(TemplateManager.MESSAGE_REGISTER_PUBLIC_TEMPLATE_EVENT, new MessageSubscriber() {

        @Override
        public void onPublishMessage(String senderAddress, String subject, Object obj) {
            Long templateId = (Long) obj;
            if (templateId != null) {
                s_logger.debug("MessageBus message: new public template registered: " + templateId + ", grant permission to default root admin, domain admin and normal user policies");
                _iamSrv.addIAMPermissionToIAMPolicy(new Long(Account.ACCOUNT_TYPE_ADMIN + 1), VirtualMachineTemplate.class.getSimpleName(), PermissionScope.RESOURCE.toString(), templateId, "listTemplates", AccessType.UseEntry.toString(), Permission.Allow, false);
                _iamSrv.addIAMPermissionToIAMPolicy(new Long(Account.ACCOUNT_TYPE_DOMAIN_ADMIN + 1), VirtualMachineTemplate.class.getSimpleName(), PermissionScope.RESOURCE.toString(), templateId, "listTemplates", AccessType.UseEntry.toString(), Permission.Allow, false);
                _iamSrv.addIAMPermissionToIAMPolicy(new Long(Account.ACCOUNT_TYPE_NORMAL + 1), VirtualMachineTemplate.class.getSimpleName(), PermissionScope.RESOURCE.toString(), templateId, "listTemplates", AccessType.UseEntry.toString(), Permission.Allow, false);
            }
        }
    });
    _messageBus.subscribe(TemplateManager.MESSAGE_RESET_TEMPLATE_PERMISSION_EVENT, new MessageSubscriber() {

        @Override
        public void onPublishMessage(String senderAddress, String subject, Object obj) {
            Long templateId = (Long) obj;
            if (templateId != null) {
                s_logger.debug("MessageBus message: reset template permission: " + templateId);
                resetTemplatePermission(templateId);
            }
        }
    });
    _messageBus.subscribe(EntityManager.MESSAGE_REMOVE_ENTITY_EVENT, new MessageSubscriber() {

        @Override
        public void onPublishMessage(String senderAddress, String subject, Object obj) {
            Pair<Class<?>, Long> entity = (Pair<Class<?>, Long>) obj;
            if (entity != null) {
                String entityType = entity.first().getSimpleName();
                Long entityId = entity.second();
                s_logger.debug("MessageBus message: delete an entity: (" + entityType + "," + entityId + "), remove its related permission");
                _iamSrv.removeIAMPermissionForEntity(entityType, entityId);
            }
        }
    });
    _messageBus.subscribe(EntityManager.MESSAGE_GRANT_ENTITY_EVENT, new MessageSubscriber() {

        @Override
        public void onPublishMessage(String senderAddress, String subject, Object obj) {
            Map<String, Object> permit = (Map<String, Object>) obj;
            if (permit != null) {
                Class<?> entityType = (Class<?>) permit.get(ApiConstants.ENTITY_TYPE);
                Long entityId = (Long) permit.get(ApiConstants.ENTITY_ID);
                AccessType accessType = (AccessType) permit.get(ApiConstants.ACCESS_TYPE);
                String action = (String) permit.get(ApiConstants.IAM_ACTION);
                List<Long> acctIds = (List<Long>) permit.get(ApiConstants.ACCOUNTS);
                s_logger.debug("MessageBus message: grant accounts permission to an entity: (" + entityType + "," + entityId + ")");
                grantEntityPermissioinToAccounts(entityType.getSimpleName(), entityId, accessType, action, acctIds);
            }
        }
    });
    _messageBus.subscribe(EntityManager.MESSAGE_REVOKE_ENTITY_EVENT, new MessageSubscriber() {

        @Override
        public void onPublishMessage(String senderAddress, String subject, Object obj) {
            Map<String, Object> permit = (Map<String, Object>) obj;
            if (permit != null) {
                Class<?> entityType = (Class<?>) permit.get(ApiConstants.ENTITY_TYPE);
                Long entityId = (Long) permit.get(ApiConstants.ENTITY_ID);
                AccessType accessType = (AccessType) permit.get(ApiConstants.ACCESS_TYPE);
                String action = (String) permit.get(ApiConstants.IAM_ACTION);
                List<Long> acctIds = (List<Long>) permit.get(ApiConstants.ACCOUNTS);
                s_logger.debug("MessageBus message: revoke from accounts permission to an entity: (" + entityType + "," + entityId + ")");
                revokeEntityPermissioinFromAccounts(entityType.getSimpleName(), entityId, accessType, action, acctIds);
            }
        }
    });
    _messageBus.subscribe(EntityManager.MESSAGE_ADD_DOMAIN_WIDE_ENTITY_EVENT, new MessageSubscriber() {

        @Override
        public void onPublishMessage(String senderAddress, String subject, Object obj) {
            Map<String, Object> params = (Map<String, Object>) obj;
            if (params != null) {
                addDomainWideResourceAccess(params);
            }
        }
    });
    return super.configure(name, params);
}
Also used : MessageSubscriber(org.apache.cloudstack.framework.messagebus.MessageSubscriber) IAMGroup(org.apache.cloudstack.iam.api.IAMGroup) HashMap(java.util.HashMap) AccountVO(com.cloud.user.AccountVO) List(java.util.List) ArrayList(java.util.ArrayList) Domain(com.cloud.domain.Domain) Map(java.util.Map) HashMap(java.util.HashMap) AccessType(org.apache.cloudstack.acl.SecurityChecker.AccessType) SSHKeyPair(com.cloud.user.SSHKeyPair) Pair(com.cloud.utils.Pair)

Example 4 with MessageSubscriber

use of org.apache.cloudstack.framework.messagebus.MessageSubscriber in project cloudstack by apache.

the class NuageVspManagerImpl method initMessageBusListeners.

@DB
private void initMessageBusListeners() {
    // Create corresponding enterprise and profile in VSP when creating a CS Domain
    _messageBus.subscribe(DomainManager.MESSAGE_ADD_DOMAIN_EVENT, new MessageSubscriber() {

        @Override
        public void onPublishMessage(String senderAddress, String subject, Object args) {
            Long domainId = (Long) args;
            Domain domain = _domainDao.findById(domainId);
            try {
                _domainDao.acquireInLockTable(domain.getId());
                List<NuageVspDeviceVO> nuageVspDevices = _nuageVspDao.listAll();
                for (NuageVspDeviceVO nuageVspDevice : nuageVspDevices) {
                    VspDomain vspDomain = _nuageVspEntityBuilder.buildVspDomain(domain);
                    SyncDomainCommand cmd = new SyncDomainCommand(vspDomain, SyncDomainCommand.Type.ADD);
                    _agentMgr.easySend(nuageVspDevice.getHostId(), cmd);
                }
            } finally {
                _domainDao.releaseFromLockTable(domain.getId());
            }
        }
    });
    // Clean up corresponding resources in VSP when deleting a CS Domain
    _messageBus.subscribe(DomainManager.MESSAGE_PRE_REMOVE_DOMAIN_EVENT, new MessageSubscriber() {

        @Override
        public void onPublishMessage(String senderAddress, String subject, Object args) {
            DomainVO domain = (DomainVO) args;
            List<NuageVspDeviceVO> nuageVspDevices = _nuageVspDao.listAll();
            for (NuageVspDeviceVO nuageVspDevice : nuageVspDevices) {
                VspDomainCleanUp vspDomainCleanUp = _nuageVspEntityBuilder.buildVspDomainCleanUp(domain);
                CleanUpDomainCommand cmd = new CleanUpDomainCommand(vspDomainCleanUp);
                _agentMgr.easySend(nuageVspDevice.getHostId(), cmd);
            }
        }
    });
    // Delete corresponding enterprise and profile in VSP when deleting a CS Domain
    _messageBus.subscribe(DomainManager.MESSAGE_REMOVE_DOMAIN_EVENT, new MessageSubscriber() {

        @Override
        public void onPublishMessage(String senderAddress, String subject, Object args) {
            DomainVO domain = (DomainVO) args;
            List<NuageVspDeviceVO> nuageVspDevices = _nuageVspDao.listAll();
            for (NuageVspDeviceVO nuageVspDevice : nuageVspDevices) {
                VspDomain vspDomain = _nuageVspEntityBuilder.buildVspDomain(domain);
                SyncDomainCommand syncCmd = new SyncDomainCommand(vspDomain, SyncDomainCommand.Type.REMOVE);
                _agentMgr.easySend(nuageVspDevice.getHostId(), syncCmd);
            }
        }
    });
}
Also used : NuageVspDeviceVO(com.cloud.network.NuageVspDeviceVO) VspDomain(net.nuage.vsp.acs.client.api.model.VspDomain) MessageSubscriber(org.apache.cloudstack.framework.messagebus.MessageSubscriber) SyncDomainCommand(com.cloud.agent.api.sync.SyncDomainCommand) DomainVO(com.cloud.domain.DomainVO) CleanUpDomainCommand(com.cloud.agent.api.manager.CleanUpDomainCommand) VspDomainCleanUp(net.nuage.vsp.acs.client.api.model.VspDomainCleanUp) ArrayList(java.util.ArrayList) List(java.util.List) VspDomain(net.nuage.vsp.acs.client.api.model.VspDomain) Domain(com.cloud.domain.Domain) DB(com.cloud.utils.db.DB)

Example 5 with MessageSubscriber

use of org.apache.cloudstack.framework.messagebus.MessageSubscriber in project cloudstack by apache.

the class DeploymentPlanningManagerImpl method configure.

@Override
public boolean configure(final String name, final Map<String, Object> params) throws ConfigurationException {
    _agentMgr.registerForHostEvents(this, true, false, true);
    VirtualMachine.State.getStateMachine().registerListener(this);
    _messageBus.subscribe("VM_ReservedCapacity_Free", new MessageSubscriber() {

        @Override
        public void onPublishMessage(String senderAddress, String subject, Object obj) {
            VMInstanceVO vm = ((VMInstanceVO) obj);
            s_logger.debug("MessageBus message: host reserved capacity released for VM: " + vm.getLastHostId() + ", checking if host reservation can be released for host:" + vm.getLastHostId());
            Long hostId = vm.getLastHostId();
            checkHostReservationRelease(hostId);
        }
    });
    _vmCapacityReleaseInterval = NumbersUtil.parseInt(_configDao.getValue(Config.CapacitySkipcountingHours.key()), 3600);
    String hostReservationReleasePeriod = _configDao.getValue(Config.HostReservationReleasePeriod.key());
    if (hostReservationReleasePeriod != null) {
        _hostReservationReleasePeriod = Long.parseLong(hostReservationReleasePeriod);
        if (_hostReservationReleasePeriod <= 0)
            _hostReservationReleasePeriod = Long.parseLong(Config.HostReservationReleasePeriod.getDefaultValue());
    }
    _timer = new Timer("HostReservationReleaseChecker");
    _nodeId = ManagementServerNode.getManagementServerId();
    return super.configure(name, params);
}
Also used : MessageSubscriber(org.apache.cloudstack.framework.messagebus.MessageSubscriber) Timer(java.util.Timer) VMInstanceVO(com.cloud.vm.VMInstanceVO)

Aggregations

MessageSubscriber (org.apache.cloudstack.framework.messagebus.MessageSubscriber)9 Domain (com.cloud.domain.Domain)3 Test (org.junit.Test)3 DomainVO (com.cloud.domain.DomainVO)2 Account (com.cloud.user.Account)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 ConfigurationException (javax.naming.ConfigurationException)2 CleanUpDomainCommand (com.cloud.agent.api.manager.CleanUpDomainCommand)1 SyncDomainCommand (com.cloud.agent.api.sync.SyncDomainCommand)1 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 NuageVspDeviceVO (com.cloud.network.NuageVspDeviceVO)1 AccountVO (com.cloud.user.AccountVO)1 SSHKeyPair (com.cloud.user.SSHKeyPair)1 Pair (com.cloud.utils.Pair)1 DB (com.cloud.utils.db.DB)1 VMInstanceVO (com.cloud.vm.VMInstanceVO)1