Search in sources :

Example 76 with JSONParser

use of org.json.simple.parser.JSONParser in project opennms by OpenNMS.

the class AlarmEventToIndexTest method jestClientAlarmToESTest.

/**
 * simple test to create an alarm change event which will create a new alarm in the alarm index
 * and create an alarm change event in the alarm change index
 */
@Test
public void jestClientAlarmToESTest() {
    LOG.debug("***************** start of test jestClientAlarmToESTest");
    EventToIndex eventToIndex = new EventToIndex();
    JestClient jestClient = null;
    try {
        // Get Jest client
        String esusername = "";
        String espassword = "";
        String elasticsearchUrl = "http://localhost:9200";
        RestClientFactory restClientFactory = new RestClientFactory(elasticsearchUrl, esusername, espassword);
        IndexNameFunction indexNameFunction = new IndexNameFunction("yyyy.MM");
        NodeCache nodeCache = new MockNodeCache();
        eventToIndex.setRestClientFactory(restClientFactory);
        eventToIndex.setNodeCache(nodeCache);
        eventToIndex.setIndexNameFunction(indexNameFunction);
        eventToIndex.setLogEventDescription(true);
        eventToIndex.setArchiveRawEvents(true);
        eventToIndex.setArchiveAlarms(true);
        eventToIndex.setArchiveAlarmChangeEvents(true);
        eventToIndex.setArchiveOldAlarmValues(true);
        eventToIndex.setArchiveNewAlarmValues(true);
        // create an alarm change event
        EventBuilder eb = new EventBuilder(ALARM_ACKNOWLEDGED_EVENT, EVENT_SOURCE_NAME);
        // copy in all values as json in params
        eb.addParam("oldalarmvalues", TEST_ALARM_JSON_1);
        eb.addParam("newalarmvalues", TEST_ALARM_JSON_1);
        Event event = eb.getEvent();
        event.setDbid(100);
        event.setNodeid((long) 34);
        // forward event to Elasticsearch
        eventToIndex.forwardEvents(Collections.singletonList(event));
        // waiting INDEX_WAIT_SECONDS seconds for index
        try {
            TimeUnit.SECONDS.sleep(INDEX_WAIT_SECONDS);
        } catch (InterruptedException e) {
        }
        // send query to check that alarm has been created
        jestClient = restClientFactory.getJestClient();
        // search for resulting alarm
        String query = "{\n" + "\n       \"query\": {" + "\n         \"match\": {" + "\n         \"alarmid\": \"807\"" + "\n          }" + "\n        }" + "\n     }";
        LOG.debug("alarm check search query: " + query);
        Search search = new Search.Builder(query).addIndex("opennms-*").build();
        SearchResult sresult = jestClient.execute(search);
        LOG.debug("received search sresult: " + sresult.getJsonString() + "\n   response code:" + sresult.getResponseCode() + "\n   error message: " + sresult.getErrorMessage());
        assertEquals(200, sresult.getResponseCode());
        JSONParser parser = new JSONParser();
        Object obj = parser.parse(sresult.getJsonString());
        JSONObject resultValues = (JSONObject) obj;
        JSONObject hits = (JSONObject) resultValues.get("hits");
        LOG.debug("search result hits:total=" + hits.get("total"));
        assertEquals(Long.valueOf(1), hits.get("total"));
        // waiting INDEX_WAIT_SECONDS seconds for index
        try {
            TimeUnit.SECONDS.sleep(INDEX_WAIT_SECONDS);
        } catch (InterruptedException e) {
        }
        // search for resulting alarm change event
        String eventquery = "{\n" + "\n       \"query\": {" + "\n         \"match\": {" + "\n         \"id\": \"100\"" + "\n          }" + "\n        }" + "\n     }";
        LOG.debug("event check search query: " + eventquery);
        Search eventsearch = new Search.Builder(eventquery).addIndex("opennms-*").build();
        SearchResult eventsresult = jestClient.execute(eventsearch);
        LOG.debug("received search eventsresult: " + eventsresult.getJsonString() + "\n   response code:" + eventsresult.getResponseCode() + "\n   error message: " + eventsresult.getErrorMessage());
        assertEquals(200, eventsresult.getResponseCode());
        Object obj2 = parser.parse(eventsresult.getJsonString());
        JSONObject eventsresultValues = (JSONObject) obj2;
        JSONObject eventhits = (JSONObject) eventsresultValues.get("hits");
        LOG.debug("search result event hits:total=" + eventhits.get("total"));
        assertEquals(Long.valueOf(1), eventhits.get("total"));
        JSONArray eventhitsvalues = (JSONArray) eventhits.get("hits");
        LOG.debug("   eventhitsvalues: " + eventhitsvalues.toJSONString());
        JSONObject hitObj = (JSONObject) eventhitsvalues.get(0);
        LOG.debug("   hitObj: " + hitObj.toJSONString());
        String typeStr = hitObj.get("_type").toString();
        LOG.debug("search result index type=" + typeStr);
        assertEquals(EVENT_INDEX_TYPE, typeStr);
        JSONObject sourceObj = (JSONObject) hitObj.get("_source");
        LOG.debug("   sourceObj: " + sourceObj.toJSONString());
        String eventUeiStr = sourceObj.get("eventuei").toString();
        LOG.debug("search result event eventueistr=" + eventUeiStr);
        assertEquals(ALARM_ACKNOWLEDGED_EVENT, eventUeiStr);
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new RuntimeException(ex);
    } finally {
        // shutdown client
        if (jestClient != null)
            jestClient.shutdownClient();
        if (eventToIndex != null)
            eventToIndex.close();
    }
    LOG.debug("***************** end of test jestClientAlarmToESTest");
}
Also used : IndexNameFunction(org.opennms.plugins.elasticsearch.rest.IndexNameFunction) NodeCache(org.opennms.plugins.elasticsearch.rest.NodeCache) EventBuilder(org.opennms.netmgt.model.events.EventBuilder) JSONArray(org.json.simple.JSONArray) SearchResult(io.searchbox.core.SearchResult) JestClient(io.searchbox.client.JestClient) EventBuilder(org.opennms.netmgt.model.events.EventBuilder) JSONObject(org.json.simple.JSONObject) RestClientFactory(org.opennms.plugins.elasticsearch.rest.RestClientFactory) Search(io.searchbox.core.Search) Event(org.opennms.netmgt.xml.event.Event) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) EventToIndex(org.opennms.plugins.elasticsearch.rest.EventToIndex) Test(org.junit.Test)

Example 77 with JSONParser

use of org.json.simple.parser.JSONParser in project metron by apache.

the class ProfileBuilderBolt method prepare.

@Override
public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) {
    super.prepare(stormConf, context, collector);
    if (periodDurationMillis <= 0) {
        throw new IllegalArgumentException("expect 'profiler.period.duration' >= 0");
    }
    if (profileTimeToLiveMillis <= 0) {
        throw new IllegalArgumentException("expect 'profiler.ttl' >= 0");
    }
    if (profileTimeToLiveMillis < periodDurationMillis) {
        throw new IllegalArgumentException("expect 'profiler.ttl' >= 'profiler.period.duration'");
    }
    if (maxNumberOfRoutes <= 0) {
        throw new IllegalArgumentException("expect 'profiler.max.routes.per.bolt' > 0");
    }
    if (windowDurationMillis <= 0) {
        throw new IllegalArgumentException("expect 'profiler.window.duration' > 0");
    }
    if (windowDurationMillis > periodDurationMillis) {
        throw new IllegalArgumentException("expect 'profiler.period.duration' >= 'profiler.window.duration'");
    }
    if (periodDurationMillis % windowDurationMillis != 0) {
        throw new IllegalArgumentException("expect 'profiler.period.duration' % 'profiler.window.duration' == 0");
    }
    this.collector = collector;
    this.parser = new JSONParser();
    this.messageDistributor = new DefaultMessageDistributor(periodDurationMillis, profileTimeToLiveMillis, maxNumberOfRoutes);
    this.configurations = new ProfilerConfigurations();
    this.flushSignal = new FixedFrequencyFlushSignal(periodDurationMillis);
    setupZookeeper();
}
Also used : JSONParser(org.json.simple.parser.JSONParser) DefaultMessageDistributor(org.apache.metron.profiler.DefaultMessageDistributor) ProfilerConfigurations(org.apache.metron.common.configuration.profiler.ProfilerConfigurations)

Example 78 with JSONParser

use of org.json.simple.parser.JSONParser in project metron by apache.

the class ProfileSplitterBolt method prepare.

@Override
public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) {
    super.prepare(stormConf, context, collector);
    this.collector = collector;
    this.parser = new JSONParser();
    this.router = new DefaultMessageRouter(getStellarContext());
    this.clockFactory = new DefaultClockFactory();
}
Also used : DefaultClockFactory(org.apache.metron.profiler.clock.DefaultClockFactory) JSONParser(org.json.simple.parser.JSONParser) DefaultMessageRouter(org.apache.metron.profiler.DefaultMessageRouter)

Example 79 with JSONParser

use of org.json.simple.parser.JSONParser in project metron by apache.

the class ProfileSplitterBoltTest method setup.

@Before
public void setup() throws ParseException {
    // parse the input message
    JSONParser parser = new JSONParser();
    message = (JSONObject) parser.parse(input);
    // ensure the tuple returns the expected json message
    when(tuple.getBinary(0)).thenReturn(input.getBytes());
}
Also used : JSONParser(org.json.simple.parser.JSONParser) Before(org.junit.Before)

Example 80 with JSONParser

use of org.json.simple.parser.JSONParser in project metron by apache.

the class ProfilerFunctionsTest method testProfilerApplyWithJSONObject.

@Test
public void testProfilerApplyWithJSONObject() throws Exception {
    // initialize the profiler
    state.put("config", helloWorldProfilerDef);
    StandAloneProfiler profiler = run("PROFILER_INIT(config)", StandAloneProfiler.class);
    state.put("profiler", profiler);
    // apply a message to the profiler
    JSONParser parser = new JSONParser();
    JSONObject jsonObject = (JSONObject) parser.parse(message);
    state.put("jsonObj", jsonObject);
    StandAloneProfiler result = run("PROFILER_APPLY(jsonObj, profiler)", StandAloneProfiler.class);
    // validate
    assertSame(profiler, result);
    assertEquals(1, profiler.getProfileCount());
    assertEquals(1, profiler.getMessageCount());
    assertEquals(1, profiler.getRouteCount());
}
Also used : StandAloneProfiler(org.apache.metron.profiler.StandAloneProfiler) JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) Test(org.junit.Test)

Aggregations

JSONParser (org.json.simple.parser.JSONParser)200 JSONObject (org.json.simple.JSONObject)168 JSONArray (org.json.simple.JSONArray)73 ParseException (org.json.simple.parser.ParseException)49 HttpClient (org.apache.commons.httpclient.HttpClient)44 GetMethod (org.apache.commons.httpclient.methods.GetMethod)34 Test (org.junit.Test)28 HashMap (java.util.HashMap)23 Map (java.util.Map)19 IOException (java.io.IOException)18 File (java.io.File)16 Before (org.junit.Before)15 URL (java.net.URL)14 ArrayList (java.util.ArrayList)13 InputStreamReader (java.io.InputStreamReader)12 PostMethod (org.apache.commons.httpclient.methods.PostMethod)11 MapLayer (au.org.emii.portal.menu.MapLayer)9 Point (com.vividsolutions.jts.geom.Point)9 FileReader (java.io.FileReader)9 InputStream (java.io.InputStream)7