Search in sources :

Example 1 with Trigger

use of com.redhat.cloud.policies.app.model.engine.Trigger in project policies-ui-backend by RedHatInsights.

the class AdminService method findOrphans.

@GET
@Path("/verify")
public Response findOrphans() {
    Map<String, List<TTT>> orphanedPolicies = new HashMap<>();
    List<TTT> orphanedInDB = new ArrayList<>();
    List<TTT> orphanedInEngine = new ArrayList<>();
    // Find active policies that do not have a trigger in engine
    try (Stream<Policy> policies = Policy.streamAll()) {
        policies.forEach(p -> {
            try {
                engine.fetchTrigger(p.id, p.customerid);
            } catch (NotFoundException nfe) {
                orphanedInDB.add(new TTT(p.customerid, p.id));
            }
        });
    }
    orphanedPolicies.put("orphanedInDB", orphanedInDB);
    // Find triggers in engine for accounts and check if
    // they have an active trigger in the DB
    List<String> customersInDb;
    try (Stream<Policy> policies = Policy.streamAll()) {
        customersInDb = policies.map(p -> p.customerid).distinct().collect(Collectors.toList());
    }
    customersInDb.forEach(cid -> {
        List<Trigger> triggers = engine.findTriggersForCustomer(cid);
        triggers.forEach(t -> {
            Policy pol = Policy.findById(cid, UUID.fromString(t.id));
            if (pol == null) {
                orphanedInEngine.add(new TTT(cid, t.id));
            }
        });
    });
    orphanedPolicies.put("orphanedInEngine", orphanedInEngine);
    return Response.ok().entity(orphanedPolicies).build();
}
Also used : Policy(com.redhat.cloud.policies.app.model.Policy) Arrays(java.util.Arrays) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Path(javax.ws.rs.Path) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) HashSet(java.util.HashSet) StuffHolder(com.redhat.cloud.policies.app.StuffHolder) QueryParam(javax.ws.rs.QueryParam) Consumes(javax.ws.rs.Consumes) Map(java.util.Map) BigInteger(java.math.BigInteger) PolicyEngine(com.redhat.cloud.policies.app.PolicyEngine) ScheduledStatusProducer(com.redhat.cloud.policies.app.health.ScheduledStatusProducer) POST(javax.ws.rs.POST) RestClient(org.eclipse.microprofile.rest.client.inject.RestClient) Transactional(javax.transaction.Transactional) Set(java.util.Set) EntityManager(javax.persistence.EntityManager) UUID(java.util.UUID) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) NotFoundException(javax.ws.rs.NotFoundException) List(java.util.List) Policy(com.redhat.cloud.policies.app.model.Policy) Stream(java.util.stream.Stream) Response(javax.ws.rs.core.Response) FullTrigger(com.redhat.cloud.policies.app.model.engine.FullTrigger) RequestScoped(javax.enterprise.context.RequestScoped) Trigger(com.redhat.cloud.policies.app.model.engine.Trigger) PostConstruct(javax.annotation.PostConstruct) Msg(com.redhat.cloud.policies.app.model.Msg) Optional(java.util.Optional) ConfigProperty(org.eclipse.microprofile.config.inject.ConfigProperty) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NotFoundException(javax.ws.rs.NotFoundException) FullTrigger(com.redhat.cloud.policies.app.model.engine.FullTrigger) Trigger(com.redhat.cloud.policies.app.model.engine.Trigger) ArrayList(java.util.ArrayList) List(java.util.List) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 2 with Trigger

use of com.redhat.cloud.policies.app.model.engine.Trigger in project policies-ui-backend by RedHatInsights.

the class TestLifecycleManager method mockEngine.

private void mockEngine() {
    mockServerClient.when(request().withPath("/hawkular/alerts/triggers/c49e92c4-dead-beef-9200-245b31933e94/enable").withHeader("Hawkular-Tenant", "1234")).respond(response().withStatusCode(500).withReasonPhrase("Internal server error").withHeader("Content-Type", "application/json").withBody("{ \"errorMessage\" : \"something went wrong\" }"));
    // -------------------------------
    List<Trigger> triggers = new ArrayList<>();
    Trigger trigger = new Trigger();
    trigger.id = "bd0ee2ec-eec0-44a6-8bb1-29c4179fc21c";
    trigger.lifecycle = new ArrayList<>();
    Map<String, Object> ev = new HashMap<>();
    Calendar cal = getInstance();
    cal.set(2020, MAY, 8, 10, 0, 0);
    ev.put("status", "ALERT_GENERATE");
    ev.put("stime", cal.getTimeInMillis());
    trigger.lifecycle.add(ev);
    ev = new HashMap<>();
    cal.set(2020, MAY, 9, 10, 0, 0);
    ev.put("status", "ALERT_GENERATE");
    ev.put("stime", cal.getTimeInMillis());
    trigger.lifecycle.add(ev);
    ev = new HashMap<>();
    cal.set(2020, MAY, 10, 10, 0, 0);
    ev.put("status", "ALERT_GENERATE");
    ev.put("stime", cal.getTimeInMillis());
    trigger.lifecycle.add(ev);
    triggers.add(trigger);
    mockServerClient.when(request().withPath("/hawkular/alerts/triggers").withQueryStringParameter("triggerIds", "bd0ee2ec-eec0-44a6-8bb1-29c4179fc21c").withHeader("Hawkular-Tenant", "1234").withMethod("GET")).respond(response().withStatusCode(200).withHeader("Content-Type", "application/json").withBody(JsonBody.json(triggers)));
    // -------------------------------
    FullTrigger ft = new FullTrigger();
    ft.trigger.id = "00000000-0000-0000-0000-000000000001";
    JsonBody ftBody = JsonBody.json(ft);
    mockServerClient.when(request().withPath("/hawkular/alerts/triggers/trigger/00000000-0000-0000-0000-000000000001").withMethod("GET")).respond(response().withStatusCode(200).withHeader("Content-Type", "application/json").withBody(ftBody));
    mockServerClient.when(request().withPath("/hawkular/alerts/triggers/.*/enable").withMethod("PUT")).respond(response().withStatusCode(200));
    mockServerClient.when(request().withPath("/hawkular/alerts/triggers/.*/enable").withMethod("DELETE")).respond(response().withStatusCode(200));
    // Simulate internal engine issue
    mockServerClient.when(request().withPath("/hawkular/alerts/triggers/trigger").withBody(new RegexBody(".*-dead-beef-9200-.*")).withHeader("Hawkular-Tenant", "1234")).respond(response().withStatusCode(500).withHeader("Content-Type", "application/json").withBody("{ \"msg\" : \"ok\" }"));
    // ------------ status page
    mockServerClient.when(request().withPath("/hawkular/alerts/triggers").withQueryStringParameter("triggerIds", "dummy").withHeader("Hawkular-Tenant", "dummy")).respond(response().withStatusCode(200).withHeader("Content-Type", "application/json").withBody("[]"));
    mockServerClient.when(request().withPath("/apps")).respond(response().withBody("[]").withHeader("Content-Type", "application/json").withStatusCode(200));
    // ------------
    mockServerClient.when(request().withPath("/hawkular/alerts/triggers/trigger").withHeader("Hawkular-Tenant", "1234")).respond(response().withStatusCode(200).withHeader("Content-Type", "application/json").withBody("{ \"msg\" : \"ok\" }"));
    mockServerClient.when(request().withPath("/hawkular/alerts/triggers/c49e92c4-dead-beef-9200-245b31933e94").withHeader("Hawkular-Tenant", "1234")).respond(response().withStatusCode(500).withHeader("Content-Type", "application/json").withBody("{ \"errorMessage\" : \"something went wrong\" }"));
    mockServerClient.when(request().withPath("/hawkular/alerts/triggers/c49e92c4-764c-4163-9200-245b31933e94").withMethod("DELETE").withHeader("Hawkular-Tenant", "1234")).respond(response().withStatusCode(404).withHeader("Content-Type", "application/json").withBody("{ \"errorMessage\" : \"does not exist\" }"));
    mockServerClient.when(request().withPath("/hawkular/alerts/triggers/.*").withMethod("DELETE").withHeader("Hawkular-Tenant", "1234")).respond(response().withStatusCode(200).withHeader("Content-Type", "application/json").withBody("{ \"msg\" : \"ok\" }"));
    mockServerClient.when(request().withPath("/hawkular/alerts/triggers/trigger/.*").withMethod("PUT").withHeader("Hawkular-Tenant", "1234")).respond(response().withStatusCode(200).withHeader("Content-Type", "application/json").withBody("{ \"msg\" : \"ok\" }"));
}
Also used : HashMap(java.util.HashMap) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) FullTrigger(com.redhat.cloud.policies.app.model.engine.FullTrigger) JsonBody(org.mockserver.model.JsonBody) RegexBody(org.mockserver.model.RegexBody) FullTrigger(com.redhat.cloud.policies.app.model.engine.FullTrigger) Trigger(com.redhat.cloud.policies.app.model.engine.Trigger)

Aggregations

FullTrigger (com.redhat.cloud.policies.app.model.engine.FullTrigger)2 Trigger (com.redhat.cloud.policies.app.model.engine.Trigger)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 PolicyEngine (com.redhat.cloud.policies.app.PolicyEngine)1 StuffHolder (com.redhat.cloud.policies.app.StuffHolder)1 ScheduledStatusProducer (com.redhat.cloud.policies.app.health.ScheduledStatusProducer)1 Msg (com.redhat.cloud.policies.app.model.Msg)1 Policy (com.redhat.cloud.policies.app.model.Policy)1 BigInteger (java.math.BigInteger)1 Arrays (java.util.Arrays)1 Calendar (java.util.Calendar)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 UUID (java.util.UUID)1 Logger (java.util.logging.Logger)1 Collectors (java.util.stream.Collectors)1