Search in sources :

Example 6 with Endpoint

use of com.redhat.cloud.notifications.models.Endpoint in project notifications-backend by RedHatInsights.

the class BehaviorGroupRepository method findBehaviorGroupsByEndpointId.

public List<BehaviorGroup> findBehaviorGroupsByEndpointId(String accountId, UUID endpointId) {
    Endpoint endpoint = entityManager.find(Endpoint.class, endpointId);
    if (endpoint == null) {
        throw new NotFoundException("Endpoint not found");
    }
    String query = "SELECT bg FROM BehaviorGroup bg LEFT JOIN FETCH bg.bundle JOIN bg.actions a WHERE bg.accountId = :accountId AND a.endpoint.id = :endpointId";
    List<BehaviorGroup> behaviorGroups = entityManager.createQuery(query, BehaviorGroup.class).setParameter("accountId", accountId).setParameter("endpointId", endpointId).getResultList();
    for (BehaviorGroup behaviorGroup : behaviorGroups) {
        behaviorGroup.filterOutActions();
    }
    return behaviorGroups;
}
Also used : Endpoint(com.redhat.cloud.notifications.models.Endpoint) BehaviorGroup(com.redhat.cloud.notifications.models.BehaviorGroup) NotFoundException(javax.ws.rs.NotFoundException)

Example 7 with Endpoint

use of com.redhat.cloud.notifications.models.Endpoint in project notifications-backend by RedHatInsights.

the class EndpointRepository method getEndpoint.

public Endpoint getEndpoint(String tenant, UUID id) {
    String query = "SELECT e FROM Endpoint e WHERE e.accountId = :accountId AND e.id = :id";
    try {
        Endpoint endpoint = entityManager.createQuery(query, Endpoint.class).setParameter("id", id).setParameter("accountId", tenant).getSingleResult();
        loadProperties(endpoint);
        return endpoint;
    } catch (NoResultException e) {
        return null;
    }
}
Also used : Endpoint(com.redhat.cloud.notifications.models.Endpoint) NoResultException(javax.persistence.NoResultException)

Example 8 with Endpoint

use of com.redhat.cloud.notifications.models.Endpoint in project notifications-backend by RedHatInsights.

the class EndpointRepository method getOrCreateEmailSubscriptionEndpoint.

@Transactional
public Endpoint getOrCreateEmailSubscriptionEndpoint(String accountId, EmailSubscriptionProperties properties) {
    List<Endpoint> emailEndpoints = getEndpointsPerCompositeType(accountId, null, Set.of(new CompositeEndpointType(EndpointType.EMAIL_SUBSCRIPTION)), null, null);
    loadProperties(emailEndpoints);
    Optional<Endpoint> endpointOptional = emailEndpoints.stream().filter(endpoint -> properties.hasSameProperties(endpoint.getProperties(EmailSubscriptionProperties.class))).findFirst();
    if (endpointOptional.isPresent()) {
        return endpointOptional.get();
    }
    Endpoint endpoint = new Endpoint();
    endpoint.setProperties(properties);
    endpoint.setAccountId(accountId);
    endpoint.setEnabled(true);
    endpoint.setDescription("System email endpoint");
    endpoint.setName("Email endpoint");
    endpoint.setType(EndpointType.EMAIL_SUBSCRIPTION);
    return createEndpoint(endpoint);
}
Also used : CompositeEndpointType(com.redhat.cloud.notifications.models.CompositeEndpointType) EmailSubscriptionProperties(com.redhat.cloud.notifications.models.EmailSubscriptionProperties) Endpoint(com.redhat.cloud.notifications.models.Endpoint) Logger(org.jboss.logging.Logger) NoResultException(javax.persistence.NoResultException) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Inject(javax.inject.Inject) HashSet(java.util.HashSet) WhereBuilder(com.redhat.cloud.notifications.db.builder.WhereBuilder) Map(java.util.Map) WebhookProperties(com.redhat.cloud.notifications.models.WebhookProperties) BadRequestException(javax.ws.rs.BadRequestException) Nullable(javax.annotation.Nullable) QueryBuilder(com.redhat.cloud.notifications.db.builder.QueryBuilder) CamelProperties(com.redhat.cloud.notifications.models.CamelProperties) EndpointProperties(com.redhat.cloud.notifications.models.EndpointProperties) Transactional(javax.transaction.Transactional) Query(com.redhat.cloud.notifications.db.Query) Set(java.util.Set) EntityManager(javax.persistence.EntityManager) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) NotFoundException(javax.ws.rs.NotFoundException) List(java.util.List) EndpointType(com.redhat.cloud.notifications.models.EndpointType) Optional(java.util.Optional) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Collections(java.util.Collections) CompositeEndpointType(com.redhat.cloud.notifications.models.CompositeEndpointType) Endpoint(com.redhat.cloud.notifications.models.Endpoint) EmailSubscriptionProperties(com.redhat.cloud.notifications.models.EmailSubscriptionProperties) Transactional(javax.transaction.Transactional)

Example 9 with Endpoint

use of com.redhat.cloud.notifications.models.Endpoint in project notifications-backend by RedHatInsights.

the class EndpointProcessor method process.

public void process(Event event) {
    processedItems.increment();
    List<Endpoint> endpoints = endpointRepository.getTargetEndpoints(event.getAccountId(), event.getEventType());
    // Target endpoints are grouped by endpoint type.
    endpointTargeted.increment(endpoints.size());
    Map<EndpointType, List<Endpoint>> endpointsByType = endpoints.stream().collect(Collectors.groupingBy(Endpoint::getType));
    for (Map.Entry<EndpointType, List<Endpoint>> entry : endpointsByType.entrySet()) {
        /*
             * For each endpoint type, the list of target endpoints is sent alongside with the action to the relevant processor.
             * Each processor returns a list of history entries. All of the returned lists are flattened into a single list.
             */
        EndpointTypeProcessor processor = endpointTypeToProcessor(entry.getKey());
        List<NotificationHistory> historyEntries = processor.process(event, entry.getValue());
        // Now each history entry is persisted.
        for (NotificationHistory history : historyEntries) {
            try {
                notificationHistoryRepository.createNotificationHistory(history);
            } catch (Exception e) {
                LOGGER.errorf("Notification history creation failed for %s", history.getEndpoint());
            }
        }
    }
}
Also used : Endpoint(com.redhat.cloud.notifications.models.Endpoint) EndpointTypeProcessor(com.redhat.cloud.notifications.processors.EndpointTypeProcessor) NotificationHistory(com.redhat.cloud.notifications.models.NotificationHistory) EndpointType(com.redhat.cloud.notifications.models.EndpointType) List(java.util.List) Map(java.util.Map)

Example 10 with Endpoint

use of com.redhat.cloud.notifications.models.Endpoint in project notifications-backend by RedHatInsights.

the class FromCamelHistoryFiller method reinjectIfNeeded.

private void reinjectIfNeeded(Map<String, Object> payloadMap) {
    if (!enableReInject || (payloadMap.containsKey("successful") && ((Boolean) payloadMap.get("successful")))) {
        return;
    }
    String historyId = (String) payloadMap.get("historyId");
    log.infof("Notification with id %s was not successful, resubmitting for further processing", historyId);
    Endpoint ep = notificationHistoryRepository.getEndpointForHistoryId(historyId);
    Event event = new Event();
    Payload.PayloadBuilder payloadBuilder = new Payload.PayloadBuilder();
    payloadMap.forEach(payloadBuilder::withAdditionalProperty);
    // TODO augment with details from Endpoint and original event
    event.setPayload(payloadBuilder.build());
    // Save the original id, as we may need it in the future.
    Context.ContextBuilder contextBuilder = new Context.ContextBuilder();
    contextBuilder.withAdditionalProperty("original-id", historyId);
    if (ep != null) {
        // TODO For the current tests. EP should not be null in real life
        contextBuilder.withAdditionalProperty("failed-integration", ep.getName());
    }
    Action action = new Action.ActionBuilder().withId(UUID.randomUUID()).withBundle("console").withApplication("notifications").withEventType("integration-failed").withAccountId(ep != null ? ep.getAccountId() : "").withContext(contextBuilder.build()).withTimestamp(LocalDateTime.now(ZoneOffset.UTC)).withEvents(Collections.singletonList(event)).withRecipients(Collections.singletonList(new Recipient.RecipientBuilder().withOnlyAdmins(true).withIgnoreUserPreferences(true).build())).build();
    String ser = Parser.encode(action);
    // Add the message id in Kafka header for the de-duplicator
    Message<String> message = buildMessageWithId(ser);
    emitter.send(message);
}
Also used : Context(com.redhat.cloud.notifications.ingress.Context) Action(com.redhat.cloud.notifications.ingress.Action) Endpoint(com.redhat.cloud.notifications.models.Endpoint) Event(com.redhat.cloud.notifications.ingress.Event) Payload(com.redhat.cloud.notifications.ingress.Payload)

Aggregations

Endpoint (com.redhat.cloud.notifications.models.Endpoint)57 Test (org.junit.jupiter.api.Test)23 JsonObject (io.vertx.core.json.JsonObject)22 QuarkusTest (io.quarkus.test.junit.QuarkusTest)20 WebhookProperties (com.redhat.cloud.notifications.models.WebhookProperties)18 DbIsolatedTest (com.redhat.cloud.notifications.db.DbIsolatedTest)15 CamelProperties (com.redhat.cloud.notifications.models.CamelProperties)14 NotificationHistory (com.redhat.cloud.notifications.models.NotificationHistory)14 Header (io.restassured.http.Header)13 List (java.util.List)13 Transactional (javax.transaction.Transactional)13 Inject (javax.inject.Inject)12 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 EndpointType (com.redhat.cloud.notifications.models.EndpointType)10 Collectors (java.util.stream.Collectors)10 EmailSubscriptionProperties (com.redhat.cloud.notifications.models.EmailSubscriptionProperties)9 Event (com.redhat.cloud.notifications.models.Event)9 Response (io.restassured.response.Response)9 Map (java.util.Map)9 UUID (java.util.UUID)9