Search in sources :

Example 1 with VelocityEmailTemplate

use of com.thinkbiganalytics.common.velocity.model.VelocityEmailTemplate in project kylo by Teradata.

the class EmailServiceLevelAgreementAction method parseVelocityTemplate.

private VelocityEmailTemplate parseVelocityTemplate(EmailServiceLevelAgreementActionConfiguration actionConfiguration, ServiceLevelAssessment assessment, Alert a) {
    Map<String, Object> map = new HashMap();
    map.put("alert", a);
    map.put("assessment", assessment);
    map.put("assessmentDescription", ServiceLevelAssessmentAlertUtil.getDescription(assessment, "<br/>"));
    map.put("slaName", assessment.getAgreement().getName());
    map.put("sla", assessment.getAgreement());
    Map<String, Object> dataMap = ServiceLevelAssessmentAlertUtil.getMetricDataAsMap(assessment);
    map.putAll(dataMap);
    String template = actionConfiguration.getVelocityTemplateId();
    if (StringUtils.isNotBlank(template)) {
        VelocityTemplate defaultTemplate = velocityTemplateProvider.findDefault(ServiceLevelAgreementEmailTemplate.EMAIL_TEMPLATE_TYPE);
        VelocityEmailTemplate defaultEmailTemplate = null;
        if (defaultTemplate != null) {
            defaultEmailTemplate = new VelocityEmailTemplate(defaultTemplate.getTitle(), defaultTemplate.getTemplate());
        } else {
            defaultEmailTemplate = emailService.getDefaultTemplate();
        }
        return velocityTemplateProvider.mergeEmailTemplate(template, map, defaultEmailTemplate);
    }
    return null;
}
Also used : VelocityTemplate(com.thinkbiganalytics.common.velocity.model.VelocityTemplate) HashMap(java.util.HashMap) VelocityEmailTemplate(com.thinkbiganalytics.common.velocity.model.VelocityEmailTemplate)

Example 2 with VelocityEmailTemplate

use of com.thinkbiganalytics.common.velocity.model.VelocityEmailTemplate in project kylo by Teradata.

the class EmailServiceLevelAgreementAction method respond.

@Override
public boolean respond(EmailServiceLevelAgreementActionConfiguration actionConfiguration, ServiceLevelAssessment assessment, Alert a) {
    log.info("Responding to SLA violation {}. for alert {} received from: {} ", assessment.getServiceLevelAgreementDescription().getName(), a.getId(), a.getSource());
    String desc = ServiceLevelAssessmentAlertUtil.getDescription(assessment);
    String slaName = assessment.getAgreement().getName();
    String emails = actionConfiguration.getEmailAddresses();
    String[] addresses = emails.split(",");
    String subject = "SLA Violated: " + slaName;
    String body = desc;
    VelocityEmailTemplate emailTemplate = parseVelocityTemplate(actionConfiguration, assessment, a);
    if (emailTemplate == null) {
        body = desc;
    } else {
        subject = emailTemplate.getSubject();
        body = emailTemplate.getBody();
    }
    final String finalSubject = subject;
    final String finalBody = body;
    log.info("sending {}  email to: {}", assessment.getServiceLevelAgreementDescription().getName(), addresses);
    Arrays.stream(addresses).forEach(address -> {
        emailService.sendMail(address.trim(), finalSubject, finalBody);
    });
    return true;
}
Also used : VelocityEmailTemplate(com.thinkbiganalytics.common.velocity.model.VelocityEmailTemplate)

Example 3 with VelocityEmailTemplate

use of com.thinkbiganalytics.common.velocity.model.VelocityEmailTemplate in project kylo by Teradata.

the class ServiceLevelAgreementRestController method sendTestTemplate.

@POST
@Path("/send-test-email-template")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation("Test sending the SLA email template.")
@ApiResponses({ @ApiResponse(code = 200, message = "Test and send the email template to a test address.  If unable to send or an error occurs it will be indicated in the response.", response = VelocityEmailTemplate.class) })
public VelocityEmailTemplate sendTestTemplate(TestSlaVelocityEmail template) {
    accessController.checkPermission(AccessController.SERVICES, FeedServicesAccessControl.EDIT_SERVICE_LEVEL_AGREEMENT_EMAIL_TEMPLATE);
    VelocityEmailTemplate parsedTemplate = testTemplate(template);
    String subject = parsedTemplate.getSubject();
    String body = parsedTemplate.getBody();
    TestSlaVelocityEmail testSlaVelocityEmail = new TestSlaVelocityEmail(subject, body, template.getEmailAddress());
    // if we have the plugin then send it
    try {
        Object emailService = SpringApplicationContext.getBean("slaEmailService");
        if (emailService != null) {
            MethodUtils.invokeMethod(emailService, "sendMail", template.getEmailAddress(), subject, body);
            testSlaVelocityEmail.setSuccess(true);
        }
    } catch (Exception e) {
        String message = e.getMessage();
        Throwable root = ExceptionUtils.getRootCause(e);
        if (root != null) {
            message = root.getMessage();
        }
        log.error("unable to send preview/test email for SLA template {} ", message, e);
        testSlaVelocityEmail.setExceptionMessage(message);
    }
    return testSlaVelocityEmail;
}
Also used : VelocityEmailTemplate(com.thinkbiganalytics.common.velocity.model.VelocityEmailTemplate) WebApplicationException(javax.ws.rs.WebApplicationException) InvalidOperationException(com.thinkbiganalytics.feedmgr.InvalidOperationException) TestSlaVelocityEmail(com.thinkbiganalytics.feedmgr.sla.TestSlaVelocityEmail) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 4 with VelocityEmailTemplate

use of com.thinkbiganalytics.common.velocity.model.VelocityEmailTemplate in project kylo by Teradata.

the class ServiceLevelAgreementRestController method testTemplate.

@POST
@Path("/test-email-template")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation("Tests a velocity SLA email template.  This will validate and return the parsed template allowing you to preview it.  If an parse exception occurs it will be indicated in the content of the response.")
@ApiResponses({ @ApiResponse(code = 200, message = "The the SLA email template.", response = VelocityEmailTemplate.class) })
public VelocityEmailTemplate testTemplate(VelocityEmailTemplate template) {
    accessController.checkPermission(AccessController.SERVICES, FeedServicesAccessControl.EDIT_SERVICE_LEVEL_AGREEMENT_EMAIL_TEMPLATE);
    Map<String, Object> map = new HashMap();
    InMemorySLAProvider slaProvider = new InMemorySLAProvider();
    Metric m1 = new SimpleMetric();
    com.thinkbiganalytics.metadata.sla.api.ServiceLevelAgreement sla = slaProvider.builder().name("Test SLA").description("SLA Description").obligationBuilder(ObligationGroup.Condition.REQUIRED).metric(m1).build().build();
    map.put("sla", sla);
    SimpleServiceLevelAssessor simpleServiceLevelAssessor = new SimpleServiceLevelAssessor();
    simpleServiceLevelAssessor.registerMetricAssessor(new TestMetricAssessor());
    com.thinkbiganalytics.metadata.sla.api.ServiceLevelAssessment assessment = simpleServiceLevelAssessor.assess(sla);
    map.put("assessment", assessment);
    map.put("assessmentDescription", ServiceLevelAssessmentAlertUtil.getDescription(assessment, "<br/>"));
    String subject = velocityTemplateProvider.testTemplate(template.getSubject(), map);
    String body = velocityTemplateProvider.testTemplate(template.getBody(), map);
    return new VelocityEmailTemplate(subject, body);
}
Also used : HashMap(java.util.HashMap) SimpleServiceLevelAssessor(com.thinkbiganalytics.metadata.sla.spi.core.SimpleServiceLevelAssessor) VelocityEmailTemplate(com.thinkbiganalytics.common.velocity.model.VelocityEmailTemplate) InMemorySLAProvider(com.thinkbiganalytics.metadata.sla.spi.core.InMemorySLAProvider) Metric(com.thinkbiganalytics.metadata.sla.api.Metric) TestMetric(com.thinkbiganalytics.metadata.api.sla.TestMetric) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 5 with VelocityEmailTemplate

use of com.thinkbiganalytics.common.velocity.model.VelocityEmailTemplate in project kylo by Teradata.

the class SlaEmailService method getDefaultTemplate.

public VelocityEmailTemplate getDefaultTemplate() {
    if (defaultTemplate == null) {
        try {
            Resource defaultTemplateResource = new ClassPathResource("default.email.template.vm");
            BufferedReader br = new BufferedReader(new InputStreamReader(defaultTemplateResource.getInputStream()), 1024);
            StringBuilder stringBuilder = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                stringBuilder.append(line).append('\n');
            }
            br.close();
            String body = stringBuilder.toString();
            String subject = "SLA Violation for $sla.name";
            defaultTemplate = new VelocityEmailTemplate(subject, body);
        } catch (Exception e) {
        // log it
        }
    }
    return defaultTemplate;
}
Also used : InputStreamReader(java.io.InputStreamReader) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) BufferedReader(java.io.BufferedReader) VelocityEmailTemplate(com.thinkbiganalytics.common.velocity.model.VelocityEmailTemplate) ClassPathResource(org.springframework.core.io.ClassPathResource) MessagingException(javax.mail.MessagingException)

Aggregations

VelocityEmailTemplate (com.thinkbiganalytics.common.velocity.model.VelocityEmailTemplate)5 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 HashMap (java.util.HashMap)2 POST (javax.ws.rs.POST)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 VelocityTemplate (com.thinkbiganalytics.common.velocity.model.VelocityTemplate)1 InvalidOperationException (com.thinkbiganalytics.feedmgr.InvalidOperationException)1 TestSlaVelocityEmail (com.thinkbiganalytics.feedmgr.sla.TestSlaVelocityEmail)1 TestMetric (com.thinkbiganalytics.metadata.api.sla.TestMetric)1 Metric (com.thinkbiganalytics.metadata.sla.api.Metric)1 InMemorySLAProvider (com.thinkbiganalytics.metadata.sla.spi.core.InMemorySLAProvider)1 SimpleServiceLevelAssessor (com.thinkbiganalytics.metadata.sla.spi.core.SimpleServiceLevelAssessor)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 MessagingException (javax.mail.MessagingException)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1 Resource (org.springframework.core.io.Resource)1