Search in sources :

Example 1 with FlowEvent

use of org.talend.esb.sam.server.persistence.FlowEvent in project tesb-rt-se by Talend.

the class SAMRestServiceImpl method aggregateFlowDetails.

public FlowDetails aggregateFlowDetails(List<FlowEvent> flowEvents) {
    Map<Long, List<CustomInfo>> customInfo = new HashMap<Long, List<CustomInfo>>();
    Set<Long> allEvents = new HashSet<Long>();
    for (FlowEvent flowEvent : flowEvents) {
        long flowEventId = flowEvent.getId();
        allEvents.add(flowEventId);
        String custKey = flowEvent.getCustomKey();
        String custValue = flowEvent.getCustomValue();
        if (null != custKey) {
            if (!customInfo.containsKey(flowEventId)) {
                customInfo.put(flowEventId, new ArrayList<CustomInfo>());
            }
            CustomInfo custom = new CustomInfo();
            custom.setKey(custKey);
            custom.setValue(custValue);
            customInfo.get(flowEventId).add(custom);
        }
    }
    List<AggregatedFlowEvent> aggregatedFlowEventList = new ArrayList<AggregatedFlowEvent>();
    for (FlowEvent flowEvent : flowEvents) {
        long flowEventId = flowEvent.getId();
        if (allEvents.contains(flowEventId)) {
            allEvents.remove(flowEventId);
            AggregatedFlowEvent aggregatedFlowEvent = new AggregatedFlowEvent();
            aggregatedFlowEvent.setContentCut(flowEvent.isContentCut());
            aggregatedFlowEvent.setCustomId(flowEvent.getCustomId());
            aggregatedFlowEvent.setDetails(uriInfo.getBaseUriBuilder().path("event").path(String.valueOf(flowEventId)).build());
            aggregatedFlowEvent.setType(flowEvent.getType());
            aggregatedFlowEvent.setFlowID(flowEvent.getFlowID());
            aggregatedFlowEvent.setHost(flowEvent.getHost());
            aggregatedFlowEvent.setId(flowEventId);
            aggregatedFlowEvent.setIp(flowEvent.getIp());
            aggregatedFlowEvent.setMessageID(flowEvent.getMessageID());
            aggregatedFlowEvent.setOperation(flowEvent.getOperation());
            aggregatedFlowEvent.setPort(flowEvent.getPort());
            aggregatedFlowEvent.setPrincipal(flowEvent.getPrincipal());
            aggregatedFlowEvent.setProcess(flowEvent.getProcess());
            aggregatedFlowEvent.setTimestamp(flowEvent.getTimestamp());
            aggregatedFlowEvent.setTransport(flowEvent.getTransport());
            if (customInfo.containsKey(flowEventId)) {
                aggregatedFlowEvent.setCustomInfo(customInfo.get(flowEventId));
            }
            aggregatedFlowEventList.add(aggregatedFlowEvent);
        }
    }
    FlowDetails flowDetails = new FlowDetails();
    flowDetails.setEvents(aggregatedFlowEventList);
    return flowDetails;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FlowEvent(org.talend.esb.sam.server.persistence.FlowEvent) AggregatedFlowEvent(org.talend.esb.sam.server.persistence.AggregatedFlowEvent) CustomInfo(org.talend.esb.sam.server.persistence.CustomInfo) ArrayList(java.util.ArrayList) List(java.util.List) FlowDetails(org.talend.esb.sam.server.persistence.FlowDetails) AggregatedFlowEvent(org.talend.esb.sam.server.persistence.AggregatedFlowEvent) HashSet(java.util.HashSet)

Example 2 with FlowEvent

use of org.talend.esb.sam.server.persistence.FlowEvent in project tesb-rt-se by Talend.

the class SamRestServiceImplTest method test.

@Test
public void test() {
    Event e = writeEventtoDb("flow123", EventTypeEnum.REQ_IN);
    List<FlowEvent> flowEvents = samProvider.getFlowDetails("flow123");
    Assert.assertNotNull(flowEvents);
    Assert.assertTrue(!flowEvents.isEmpty());
    Response response = restService.getFlow("flow123");
    String responseString = response.readEntity(String.class);
    System.out.println(responseString);
    JSONParser parser = new JSONParser();
    JSONObject obj = null;
    try {
        obj = (JSONObject) parser.parse(responseString);
    } catch (Exception ex) {
        ex.printStackTrace();
        Assert.fail(ex.getMessage());
    }
    Assert.assertNotNull(obj);
    JSONObject events = (JSONObject) obj.get("events");
    Assert.assertNotNull(events);
    Assert.assertFalse((Boolean) (events.get("contentCut")));
    Assert.assertEquals("custom_id1", events.get("customId"));
    Assert.assertTrue(((String) events.get("details")).startsWith("local://sam-rest/event/"));
    Assert.assertEquals("flow123", events.get("flowID"));
    Assert.assertEquals("localhost", events.get("host"));
    Assert.assertEquals(new Long(2), events.get("id"));
    Assert.assertEquals("127.0.0.1", events.get("ip"));
    Assert.assertEquals("mid1", events.get("messageID"));
    Assert.assertEquals("seekBook", events.get("operation"));
    Assert.assertEquals("portType_1", events.get("port"));
    Assert.assertEquals("principal1", events.get("principal"));
    Assert.assertEquals("pid1", events.get("process"));
    Assert.assertEquals("HTTP", events.get("transport"));
    Assert.assertEquals("REQ_IN", events.get("type"));
    // Test not existing flow
    response = restService.getFlow("notExistingFlow");
    Assert.assertEquals(404, response.getStatus());
    // Test getEvent
    response = restService.getEvent("notANumber");
    Assert.assertEquals(400, response.getStatus());
    response = restService.getEvent("-1");
    Assert.assertEquals(404, response.getStatus());
    response = restService.getEvent(Long.toString(e.getPersistedId()));
    Assert.assertEquals(200, response.getStatus());
    try {
        obj = (JSONObject) parser.parse(response.readEntity(String.class));
    } catch (Exception ex) {
        ex.printStackTrace();
        Assert.fail(ex.getMessage());
    }
    Assert.assertEquals("<seekBook>Survival in the Arctic</seekBook>", obj.get("content"));
    Assert.assertFalse((Boolean) obj.get("contentCut"));
    Assert.assertEquals("custom_id1", obj.get("customId"));
    Assert.assertEquals("flow123", obj.get("flowID"));
    Assert.assertEquals("localhost", obj.get("host"));
    Assert.assertEquals(new Long(2), obj.get("id"));
    Assert.assertEquals("127.0.0.1", obj.get("ip"));
    Assert.assertEquals("mid1", obj.get("messageID"));
    Assert.assertEquals("seekBook", obj.get("operation"));
    Assert.assertEquals("portType_1", obj.get("port"));
    Assert.assertEquals("principal1", obj.get("principal"));
    Assert.assertEquals("pid1", obj.get("process"));
    Assert.assertEquals("HTTP", obj.get("transport"));
    Assert.assertEquals("REQ_IN", obj.get("type"));
    // ---------------------------------------------------
    Event e2 = writeEventtoDb("flow123", EventTypeEnum.REQ_OUT);
    response = restService.getFlows(0, 10);
    try {
        obj = (JSONObject) parser.parse(response.readEntity(String.class));
    } catch (Exception ex) {
        ex.printStackTrace();
        Assert.fail(ex.getMessage());
    }
    Assert.assertEquals(new Long(1), obj.get("count"));
    obj = (JSONObject) obj.get("aggregated");
    Assert.assertNotNull(obj);
    Assert.assertEquals("localhost", obj.get("consumerHost"));
    Assert.assertEquals("127.0.0.1", obj.get("consumerIP"));
    Assert.assertEquals("local://sam-rest/flow/flow123", obj.get("details"));
    Assert.assertEquals("flow123", obj.get("flowID"));
    Assert.assertEquals("seekBook", obj.get("operation"));
    Assert.assertEquals("portType_1", obj.get("port"));
    Assert.assertEquals("localhost", obj.get("providerHost"));
    Assert.assertEquals("127.0.0.1", obj.get("providerIP"));
    Assert.assertEquals("HTTP", obj.get("transport"));
}
Also used : Response(javax.ws.rs.core.Response) FlowEvent(org.talend.esb.sam.server.persistence.FlowEvent) JSONObject(org.json.simple.JSONObject) Event(org.talend.esb.sam.common.event.Event) FlowEvent(org.talend.esb.sam.server.persistence.FlowEvent) JSONParser(org.json.simple.parser.JSONParser) Test(org.junit.Test)

Example 3 with FlowEvent

use of org.talend.esb.sam.server.persistence.FlowEvent in project tesb-rt-se by Talend.

the class SAMRestServiceImpl method getEvent.

@Override
public Response getEvent(String id) {
    Integer eventId;
    try {
        eventId = Integer.parseInt(id);
    } catch (NumberFormatException ex) {
        throw new IllegalParameterException("Error during converting " + id + " parameter to Integer", ex);
    }
    FlowEvent event = provider.getEventDetails(eventId);
    if (null == event) {
        throw new ResourceNotFoundException("There no event with " + id + " ID can be found");
    }
    return Response.ok(event).build();
}
Also used : FlowEvent(org.talend.esb.sam.server.persistence.FlowEvent) AggregatedFlowEvent(org.talend.esb.sam.server.persistence.AggregatedFlowEvent) IllegalParameterException(org.talend.esb.sam.service.rest.exception.IllegalParameterException) ResourceNotFoundException(org.talend.esb.sam.service.rest.exception.ResourceNotFoundException)

Aggregations

FlowEvent (org.talend.esb.sam.server.persistence.FlowEvent)3 AggregatedFlowEvent (org.talend.esb.sam.server.persistence.AggregatedFlowEvent)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Response (javax.ws.rs.core.Response)1 JSONObject (org.json.simple.JSONObject)1 JSONParser (org.json.simple.parser.JSONParser)1 Test (org.junit.Test)1 Event (org.talend.esb.sam.common.event.Event)1 CustomInfo (org.talend.esb.sam.server.persistence.CustomInfo)1 FlowDetails (org.talend.esb.sam.server.persistence.FlowDetails)1 IllegalParameterException (org.talend.esb.sam.service.rest.exception.IllegalParameterException)1 ResourceNotFoundException (org.talend.esb.sam.service.rest.exception.ResourceNotFoundException)1