Search in sources :

Example 6 with SensorParserGroup

use of org.apache.metron.common.configuration.SensorParserGroup in project metron by apache.

the class SensorParserGroupControllerIntegrationTest method testGetAll.

@Test
public void testGetAll() throws Exception {
    SensorParserGroup group1 = JSONUtils.INSTANCE.load(group1BroSquid, SensorParserGroup.class);
    this.sensorParserGroupService.save(group1);
    TestUtils.assertEventually(() -> assertEquals(group1, this.sensorParserGroupService.findOne("group1")));
    SensorParserGroup group2 = JSONUtils.INSTANCE.load(group2YafJsonMap, SensorParserGroup.class);
    this.sensorParserGroupService.save(group2);
    TestUtils.assertEventually(() -> assertEquals(group2, this.sensorParserGroupService.findOne("group2")));
    this.mockMvc.perform(get(sensorParserGroupUrl).with(httpBasic(user, password))).andExpect(status().isOk()).andExpect(content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))).andExpect(jsonPath("$.*", hasSize(2))).andExpect(jsonPath("$.group1.*", hasSize(numFields.get()))).andExpect(jsonPath("$.group1.name").value("group1")).andExpect(jsonPath("$.group1.description").value("group1 description")).andExpect(jsonPath("$.group1.sensors[0]").value("squid")).andExpect(jsonPath("$.group1.sensors[1]").value("bro")).andExpect(jsonPath("$.group2.*", hasSize(numFields.get()))).andExpect(jsonPath("$.group2.name").value("group2")).andExpect(jsonPath("$.group2.description").value("group2 description")).andExpect(jsonPath("$.group2.sensors[0]").value("jsonMap")).andExpect(jsonPath("$.group2.sensors[1]").value("yaf"));
}
Also used : SensorParserGroup(org.apache.metron.common.configuration.SensorParserGroup) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 7 with SensorParserGroup

use of org.apache.metron.common.configuration.SensorParserGroup in project metron by apache.

the class SensorParserGroupControllerIntegrationTest method testDelete.

@Test
public void testDelete() throws Exception {
    SensorParserGroup group1 = JSONUtils.INSTANCE.load(group1BroSquid, SensorParserGroup.class);
    this.sensorParserGroupService.save(group1);
    TestUtils.assertEventually(() -> assertEquals(group1, this.sensorParserGroupService.findOne("group1")));
    this.mockMvc.perform(delete(sensorParserGroupUrl + "/group1").with(httpBasic(user, password)).with(csrf())).andExpect(status().isOk());
    this.mockMvc.perform(delete(sensorParserGroupUrl + "/missingGroup").with(httpBasic(user, password))).andExpect(status().isNotFound());
    {
        // we must wait for the config to find its way into the config.
        TestUtils.assertEventually(() -> assertNull(sensorParserGroupService.findOne("group1")));
    }
}
Also used : SensorParserGroup(org.apache.metron.common.configuration.SensorParserGroup) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 8 with SensorParserGroup

use of org.apache.metron.common.configuration.SensorParserGroup in project metron by apache.

the class SensorParserGroupServiceImpl method delete.

/**
 * Deletes a SensorParserGroup from Zookeeper.
 * @param name SensorParserGroup name
 * @return True if a SensorParserGroup was deleted
 * @throws RestException Writing to Zookeeper resulted in an error
 */
@Override
public boolean delete(String name) throws RestException {
    ParserConfigurations parserConfigurations = cache.get(ParserConfigurations.class);
    Map<String, SensorParserGroup> groups = parserConfigurations.getSensorParserGroups();
    boolean deleted = false;
    if (groups.containsKey(name)) {
        groups.remove(name);
        saveGroups(parserConfigurations, new HashSet<>(groups.values()));
        deleted = true;
    }
    return deleted;
}
Also used : ParserConfigurations(org.apache.metron.common.configuration.ParserConfigurations) SensorParserGroup(org.apache.metron.common.configuration.SensorParserGroup)

Example 9 with SensorParserGroup

use of org.apache.metron.common.configuration.SensorParserGroup in project metron by apache.

the class SensorParserGroupServiceImpl method save.

/**
 * Saves a SensorParserGroup in Zookeeper.  Checks for various error conditions including empty sensors field, missing
 * configs for each sensor and sensors already included in another group.
 * @param sensorParserGroup
 * @return
 * @throws RestException
 */
@Override
public SensorParserGroup save(SensorParserGroup sensorParserGroup) throws RestException {
    ParserConfigurations parserConfigurations = cache.get(ParserConfigurations.class);
    Map<String, SensorParserGroup> groups = new HashMap<>(parserConfigurations.getSensorParserGroups());
    groups.remove(sensorParserGroup.getName());
    if (sensorParserGroup.getSensors().size() == 0) {
        throw new RestException("A parser group must contain sensors");
    }
    for (String sensor : sensorParserGroup.getSensors()) {
        // check if sensor config exists
        if (sensorParserConfigService.findOne(sensor) == null) {
            throw new RestException(String.format("Could not find config for sensor %s", sensor));
        }
        // check if sensor is in another group
        for (SensorParserGroup group : groups.values()) {
            Set<String> groupSensors = group.getSensors();
            if (groupSensors.contains(sensor)) {
                throw new RestException(String.format("Sensor %s is already in group %s", sensor, group.getName()));
            }
        }
    }
    groups.put(sensorParserGroup.getName(), sensorParserGroup);
    saveGroups(parserConfigurations, new HashSet<>(groups.values()));
    return sensorParserGroup;
}
Also used : HashMap(java.util.HashMap) ParserConfigurations(org.apache.metron.common.configuration.ParserConfigurations) RestException(org.apache.metron.rest.RestException) SensorParserGroup(org.apache.metron.common.configuration.SensorParserGroup)

Example 10 with SensorParserGroup

use of org.apache.metron.common.configuration.SensorParserGroup in project metron by apache.

the class StormStatusServiceImpl method getTopologyId.

/**
 * Retrieves the Storm topology id from the given topology name.  If a topology name is detected to be an aggregate
 * parser topology, the SensorParserGroups are checked for a match.
 * @param name Topology or SensorParserGroup name
 * @return Topology id
 */
protected String getTopologyId(String name) {
    String id = null;
    for (TopologyStatus topology : getTopologySummary().getTopologies()) {
        String topologyName = topology.getName();
        // check sensor group
        if (topologyName.contains(ParserTopologyCLI.STORM_JOB_SEPARATOR)) {
            Set<String> sensors = new HashSet<>(Arrays.asList(topologyName.split(ParserTopologyCLI.STORM_JOB_SEPARATOR)));
            SensorParserGroup group = sensorParserGroupService.findOne(name);
            if (group == null) {
                break;
            } else if (sensors.equals(group.getSensors())) {
                id = topology.getId();
                break;
            }
        }
        if (topologyName.equals(name)) {
            id = topology.getId();
            break;
        }
    }
    return id;
}
Also used : SensorParserGroup(org.apache.metron.common.configuration.SensorParserGroup) TopologyStatus(org.apache.metron.rest.model.TopologyStatus) HashSet(java.util.HashSet)

Aggregations

SensorParserGroup (org.apache.metron.common.configuration.SensorParserGroup)18 Test (org.junit.jupiter.api.Test)14 ParserConfigurations (org.apache.metron.common.configuration.ParserConfigurations)9 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)5 SensorParserConfig (org.apache.metron.common.configuration.SensorParserConfig)4 RestException (org.apache.metron.rest.RestException)4 TopologyResponse (org.apache.metron.rest.model.TopologyResponse)2 TopologyStatus (org.apache.metron.rest.model.TopologyStatus)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 TopologySummary (org.apache.metron.rest.model.TopologySummary)1