Search in sources :

Example 1 with NotificationMessage

use of com.epam.pipeline.entity.notification.NotificationMessage in project cloud-pipeline by epam.

the class NotificationManager method notifyLongRunningTask.

/**
 * Internal method for creating notification message that selecting appropriate email template from db,
 * serialize PipelineRun to key-value object and save it to notification_queue table.
 * @param run
 * @param settings defines, if a long initialization or long running message template should be used
 */
@Transactional(propagation = Propagation.REQUIRED)
public void notifyLongRunningTask(PipelineRun run, NotificationSettings settings) {
    LOGGER.debug(messageHelper.getMessage(MessageConstants.INFO_NOTIFICATION_SUBMITTED, run.getPodId()));
    NotificationMessage notificationMessage = new NotificationMessage();
    if (settings.isKeepInformedOwner()) {
        PipelineUser pipelineOwner = userManager.loadUserByName(run.getOwner());
        notificationMessage.setToUserId(pipelineOwner.getId());
    }
    List<Long> ccUserIds = getKeepInformedUserIds(settings);
    if (settings.isKeepInformedAdmins()) {
        ExtendedRole extendedRole = roleManager.loadRoleWithUsers(DefaultRoles.ROLE_ADMIN.getId());
        ccUserIds.addAll(extendedRole.getUsers().stream().map(PipelineUser::getId).collect(Collectors.toList()));
    }
    notificationMessage.setCopyUserIds(ccUserIds);
    notificationMessage.setTemplate(new NotificationTemplate(settings.getTemplateId()));
    if (notificationMessage.getTemplate() == null) {
        LOGGER.error(messageHelper.getMessage(MessageConstants.ERROR_NOTIFICATION_NOT_FOUND, settings.getTemplateId()));
    }
    notificationMessage.setTemplateParameters(PipelineRunMapper.map(run, settings.getThreshold()));
    monitoringNotificationDao.createMonitoringNotification(notificationMessage);
}
Also used : PipelineUser(com.epam.pipeline.entity.user.PipelineUser) ExtendedRole(com.epam.pipeline.entity.user.ExtendedRole) NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with NotificationMessage

use of com.epam.pipeline.entity.notification.NotificationMessage in project cloud-pipeline by epam.

the class NotificationManager method notifyIdleRuns.

/**
 * Issues a notification of an idle Pipeline Run for multiple runs.
 *
 * @param pipelineCpuRatePairs a list of pairs of PipelineRun and Double cpu usage rate value
 * @param notificationType a type of notification to be issued. Supported types are IDLE_RUN, IDLE_RUN_PAUSED,
 *                         IDLE_RUN_STOPPED
 * @throws IllegalArgumentException if notificationType is not from IDLE_RUN group
 */
@Transactional(propagation = Propagation.REQUIRED)
public void notifyIdleRuns(List<Pair<PipelineRun, Double>> pipelineCpuRatePairs, NotificationType notificationType) {
    if (CollectionUtils.isEmpty(pipelineCpuRatePairs)) {
        return;
    }
    Assert.isTrue(NotificationSettings.NotificationGroup.IDLE_RUN == notificationType.getGroup(), "Only IDLE_RUN group notification types are allowed");
    NotificationSettings idleRunSettings = notificationSettingsManager.load(notificationType);
    if (idleRunSettings == null || !idleRunSettings.isEnabled()) {
        LOGGER.info("No template configured for idle pipeline run notifications or it was disabled!");
        return;
    }
    List<Long> ccUserIds = getKeepInformedUserIds(idleRunSettings);
    if (idleRunSettings.isKeepInformedAdmins()) {
        ExtendedRole extendedRole = roleManager.loadRoleWithUsers(DefaultRoles.ROLE_ADMIN.getId());
        ccUserIds.addAll(extendedRole.getUsers().stream().map(PipelineUser::getId).collect(Collectors.toList()));
    }
    double idleCpuLevel = preferenceManager.getPreference(SystemPreferences.SYSTEM_IDLE_CPU_THRESHOLD_PERCENT);
    Map<String, PipelineUser> pipelineOwners = userManager.loadUsersByNames(pipelineCpuRatePairs.stream().map(p -> p.getLeft().getOwner()).collect(Collectors.toList())).stream().collect(Collectors.toMap(PipelineUser::getUserName, user -> user));
    List<NotificationMessage> messages = pipelineCpuRatePairs.stream().map(pair -> {
        NotificationMessage message = new NotificationMessage();
        message.setTemplate(new NotificationTemplate(idleRunSettings.getTemplateId()));
        message.setTemplateParameters(PipelineRunMapper.map(pair.getLeft(), null));
        message.getTemplateParameters().put("idleCpuLevel", idleCpuLevel);
        message.getTemplateParameters().put("cpuRate", pair.getRight() * PERCENT);
        message.setToUserId(pipelineOwners.getOrDefault(pair.getLeft().getOwner(), new PipelineUser()).getId());
        message.setCopyUserIds(ccUserIds);
        return message;
    }).collect(Collectors.toList());
    monitoringNotificationDao.createMonitoringNotifications(messages);
}
Also used : PipelineRunMapper(com.epam.pipeline.mapper.PipelineRunMapper) Arrays(java.util.Arrays) MessageConstants(com.epam.pipeline.common.MessageConstants) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) SystemPreferences(com.epam.pipeline.manager.preference.SystemPreferences) ArrayList(java.util.ArrayList) PipelineRun(com.epam.pipeline.entity.pipeline.PipelineRun) Matcher(java.util.regex.Matcher) Pair(org.apache.commons.lang3.tuple.Pair) MessageHelper(com.epam.pipeline.common.MessageHelper) Propagation(org.springframework.transaction.annotation.Propagation) ExtendedRole(com.epam.pipeline.entity.user.ExtendedRole) Service(org.springframework.stereotype.Service) Map(java.util.Map) NotificationType(com.epam.pipeline.entity.notification.NotificationSettings.NotificationType) IssueComment(com.epam.pipeline.entity.issue.IssueComment) TypeReference(com.fasterxml.jackson.core.type.TypeReference) PipelineUser(com.epam.pipeline.entity.user.PipelineUser) EntityManager(com.epam.pipeline.manager.EntityManager) NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) Logger(org.slf4j.Logger) AbstractSecuredEntity(com.epam.pipeline.entity.AbstractSecuredEntity) PreferenceManager(com.epam.pipeline.manager.preference.PreferenceManager) RoleManager(com.epam.pipeline.manager.user.RoleManager) DefaultRoles(com.epam.pipeline.entity.user.DefaultRoles) JsonMapper(com.epam.pipeline.config.JsonMapper) NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate) NotificationMessageVO(com.epam.pipeline.controller.vo.notification.NotificationMessageVO) Collectors(java.util.stream.Collectors) List(java.util.List) UserManager(com.epam.pipeline.manager.user.UserManager) MonitoringNotificationDao(com.epam.pipeline.dao.notification.MonitoringNotificationDao) NotificationSettings(com.epam.pipeline.entity.notification.NotificationSettings) CollectionUtils(org.springframework.util.CollectionUtils) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) Issue(com.epam.pipeline.entity.issue.Issue) Collections(java.util.Collections) Transactional(org.springframework.transaction.annotation.Transactional) Assert(org.springframework.util.Assert) ExtendedRole(com.epam.pipeline.entity.user.ExtendedRole) PipelineUser(com.epam.pipeline.entity.user.PipelineUser) NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) NotificationSettings(com.epam.pipeline.entity.notification.NotificationSettings) NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with NotificationMessage

use of com.epam.pipeline.entity.notification.NotificationMessage in project cloud-pipeline by epam.

the class NotificationManager method toMessage.

private NotificationMessage toMessage(final NotificationMessageVO messageVO) {
    final NotificationMessage message = new NotificationMessage();
    message.setSubject(messageVO.getSubject());
    message.setBody(messageVO.getBody());
    message.setTemplateParameters(messageVO.getParameters());
    final List<Long> copyUserIds = Optional.ofNullable(messageVO.getCopyUsers()).orElseGet(Collections::emptyList).stream().map(this::getUserByName).map(PipelineUser::getId).collect(Collectors.toList());
    message.setCopyUserIds(copyUserIds);
    message.setToUserId(getUserByName(messageVO.getToUser()).getId());
    return message;
}
Also used : NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) Collections(java.util.Collections)

Example 4 with NotificationMessage

use of com.epam.pipeline.entity.notification.NotificationMessage in project cloud-pipeline by epam.

the class NotificationManagerTest method testNotifyIssueComment.

@Test
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Throwable.class)
public void testNotifyIssueComment() {
    Pipeline pipeline = createPipeline(testOwner);
    Issue issue = createIssue(testUser2, pipeline);
    issueDao.createIssue(issue);
    IssueComment comment = new IssueComment();
    comment.setIssueId(issue.getId());
    comment.setText("Notify @TestUser1");
    comment.setAuthor(testUser2.getUserName());
    notificationManager.notifyIssueComment(comment, issue, comment.getText());
    List<NotificationMessage> messages = monitoringNotificationDao.loadAllNotifications();
    Assert.assertEquals(1, messages.size());
    NotificationMessage message = messages.get(0);
    Assert.assertEquals(testUser2.getId(), message.getToUserId());
    Assert.assertEquals(issueCommentTemplate.getId(), message.getTemplate().getId());
    Assert.assertTrue(message.getCopyUserIds().stream().anyMatch(id -> id.equals(testUser1.getId())));
    Assert.assertTrue(message.getCopyUserIds().stream().anyMatch(id -> id.equals(testOwner.getId())));
    updateKeepInformedOwner(issueCommentSettings, false);
    notificationManager.notifyIssueComment(comment, issue, comment.getText());
    messages = monitoringNotificationDao.loadAllNotifications();
    Assert.assertEquals(2, messages.size());
    Assert.assertNull(messages.get(1).getToUserId());
}
Also used : Arrays(java.util.Arrays) DateTimeZone(org.joda.time.DateTimeZone) DoneablePod(io.fabric8.kubernetes.api.model.DoneablePod) PodMonitor(com.epam.pipeline.manager.cluster.PodMonitor) Autowired(org.springframework.beans.factory.annotation.Autowired) SystemPreferences(com.epam.pipeline.manager.preference.SystemPreferences) MockitoAnnotations(org.mockito.MockitoAnnotations) PipelineRun(com.epam.pipeline.entity.pipeline.PipelineRun) ExtendedRole(com.epam.pipeline.entity.user.ExtendedRole) Map(java.util.Map) IssueComment(com.epam.pipeline.entity.issue.IssueComment) PipelineUser(com.epam.pipeline.entity.user.PipelineUser) MockBean(org.springframework.boot.test.mock.mockito.MockBean) Pipeline(com.epam.pipeline.entity.pipeline.Pipeline) DateUtils(com.epam.pipeline.entity.utils.DateUtils) AbstractSecuredEntity(com.epam.pipeline.entity.AbstractSecuredEntity) NotificationTemplateDao(com.epam.pipeline.dao.notification.NotificationTemplateDao) DefaultRoles(com.epam.pipeline.entity.user.DefaultRoles) NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate) List(java.util.List) MonitoringNotificationDao(com.epam.pipeline.dao.notification.MonitoringNotificationDao) NotificationSettings(com.epam.pipeline.entity.notification.NotificationSettings) UserDao(com.epam.pipeline.dao.user.UserDao) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) EntityVO(com.epam.pipeline.controller.vo.EntityVO) Mockito.mock(org.mockito.Mockito.mock) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) NotificationSettingsDao(com.epam.pipeline.dao.notification.NotificationSettingsDao) Mock(org.mockito.Mock) PipelineRunManager(com.epam.pipeline.manager.pipeline.PipelineRunManager) Duration(org.joda.time.Duration) TestApplication(com.epam.pipeline.app.TestApplication) PipelineDao(com.epam.pipeline.dao.pipeline.PipelineDao) ArgumentCaptor(org.mockito.ArgumentCaptor) Propagation(org.springframework.transaction.annotation.Propagation) AbstractManagerTest(com.epam.pipeline.manager.AbstractManagerTest) KubernetesManager(com.epam.pipeline.manager.cluster.KubernetesManager) KubernetesTestUtils(com.epam.pipeline.util.KubernetesTestUtils) NotificationType(com.epam.pipeline.entity.notification.NotificationSettings.NotificationType) Before(org.junit.Before) NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) PodStatus(io.fabric8.kubernetes.api.model.PodStatus) CustomAssertions.assertThrows(com.epam.pipeline.util.CustomAssertions.assertThrows) DateTime(org.joda.time.DateTime) TaskStatus(com.epam.pipeline.entity.pipeline.TaskStatus) Matchers(org.hamcrest.Matchers) Pod(io.fabric8.kubernetes.api.model.Pod) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) NotificationMessageVO(com.epam.pipeline.controller.vo.notification.NotificationMessageVO) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) PodResource(io.fabric8.kubernetes.client.dsl.PodResource) Mockito.verify(org.mockito.Mockito.verify) PodList(io.fabric8.kubernetes.api.model.PodList) ContextConfiguration(org.springframework.test.context.ContextConfiguration) IssueDao(com.epam.pipeline.dao.issue.IssueDao) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Assert(org.junit.Assert) Issue(com.epam.pipeline.entity.issue.Issue) Collections(java.util.Collections) Transactional(org.springframework.transaction.annotation.Transactional) Issue(com.epam.pipeline.entity.issue.Issue) NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) IssueComment(com.epam.pipeline.entity.issue.IssueComment) Pipeline(com.epam.pipeline.entity.pipeline.Pipeline) AbstractManagerTest(com.epam.pipeline.manager.AbstractManagerTest) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with NotificationMessage

use of com.epam.pipeline.entity.notification.NotificationMessage in project cloud-pipeline by epam.

the class NotificationManagerTest method testWontNotifyAdminsIfConfigured.

@Test
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Throwable.class)
public void testWontNotifyAdminsIfConfigured() {
    NotificationSettings settings = notificationSettingsDao.loadNotificationSettings(1L);
    settings.setKeepInformedAdmins(false);
    notificationSettingsDao.updateNotificationSettings(settings);
    notificationManager.notifyLongRunningTask(longRunnging, settings);
    List<NotificationMessage> messages = monitoringNotificationDao.loadAllNotifications();
    Assert.assertEquals(1, messages.size());
    Assert.assertEquals(admin.getId(), messages.get(0).getToUserId());
    Assert.assertTrue(messages.get(0).getCopyUserIds().isEmpty());
    Assert.assertEquals(longRunningTemplate.getId(), messages.get(0).getTemplate().getId());
    settings.setKeepInformedAdmins(false);
    settings.setInformedUserIds(Collections.singletonList(userDao.loadUserByName("admin").getId()));
    notificationSettingsDao.updateNotificationSettings(settings);
    notificationManager.notifyLongRunningTask(longRunnging, settings);
    messages = monitoringNotificationDao.loadAllNotifications();
    Assert.assertTrue(messages.get(messages.size() - 1).getCopyUserIds().contains(admin.getId()));
}
Also used : NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) NotificationSettings(com.epam.pipeline.entity.notification.NotificationSettings) AbstractManagerTest(com.epam.pipeline.manager.AbstractManagerTest) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

NotificationMessage (com.epam.pipeline.entity.notification.NotificationMessage)26 Transactional (org.springframework.transaction.annotation.Transactional)25 Test (org.junit.Test)18 PipelineUser (com.epam.pipeline.entity.user.PipelineUser)15 NotificationTemplate (com.epam.pipeline.entity.notification.NotificationTemplate)14 AbstractManagerTest (com.epam.pipeline.manager.AbstractManagerTest)8 NotificationSettings (com.epam.pipeline.entity.notification.NotificationSettings)7 PipelineRun (com.epam.pipeline.entity.pipeline.PipelineRun)6 AbstractSpringTest (com.epam.pipeline.notifier.AbstractSpringTest)6 NotificationMessageVO (com.epam.pipeline.controller.vo.notification.NotificationMessageVO)5 ExtendedRole (com.epam.pipeline.entity.user.ExtendedRole)5 Collections (java.util.Collections)5 AbstractSecuredEntity (com.epam.pipeline.entity.AbstractSecuredEntity)4 Issue (com.epam.pipeline.entity.issue.Issue)4 IssueComment (com.epam.pipeline.entity.issue.IssueComment)4 DefaultRoles (com.epam.pipeline.entity.user.DefaultRoles)4 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)4 Arrays (java.util.Arrays)4 List (java.util.List)4 Map (java.util.Map)4