Provides an example of processing a YAML file containing evidence for IP Intelligence. There are 20,000 examples in the supplied file of evidence representing IP addresses.
We create an IP Intelligence pipeline to read the data and find out about the associated network information, we write this data to a YAML formatted output stream.
As well as explaining the basic operation of offline processing using the defaults, for advanced operation this example can be used to experiment with tuning IP Intelligence for performance and predictive power using Performance Profile settings.
This example requires an enterprise IP Intelligence data file (.ipi). To obtain an enterprise data file for testing, please contact us.
package fiftyone.ipintelligence.examples.console;
import fiftyone.ipintelligence.IPIntelligencePipelineBuilder;
import fiftyone.ipintelligence.examples.shared.DataFileHelper;
import fiftyone.ipintelligence.engine.onpremise.flowelements.IPIntelligenceOnPremiseEngine;
import fiftyone.ipintelligence.shared.IPIntelligenceData;
import fiftyone.pipeline.core.data.FlowData;
import fiftyone.pipeline.core.flowelements.Pipeline;
import fiftyone.pipeline.engines.Constants;
import fiftyone.pipeline.engines.data.AspectPropertyValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import java.io.*;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.stream.Collectors;
import static fiftyone.common.testhelpers.LogbackHelper.configureLogback;
import static fiftyone.ipintelligence.examples.shared.PropertyHelper.asStringProperty;
import static fiftyone.ipintelligence.examples.shared.DataFileHelper.ENTERPRISE_DATA_FILE_REL_PATH;
import static fiftyone.pipeline.util.FileFinder.getFilePath;
public class OfflineProcessing {
static final Logger logger = LoggerFactory.getLogger(OfflineProcessing.class);
private static final String dataDir = "ip-intelligence-data";
public static final String HEADER_EVIDENCE_YML =
dataDir + "/evidence.yml";
public static void main(String[] args) throws Exception {
configureLogback(getFilePath("logback.xml"));
String dataFile = (args.length > 0) ? args[0] : ENTERPRISE_DATA_FILE_REL_PATH;
File evidenceFile = getFilePath(HEADER_EVIDENCE_YML);
run(dataFile, Files.newInputStream(evidenceFile.toPath()), System.out);
}
@SuppressWarnings("unchecked")
public static void run(String dataFile, InputStream is, OutputStream os)
throws Exception {
String detectionFile;
try {
detectionFile = DataFileHelper.getDataFileLocation(dataFile);
} catch (Exception e) {
logger.error("Failed to find IP Intelligence data file at '{}'. " +
"Please provide a valid path to an IP Intelligence data file (.ipi).", dataFile);
throw e;
}
DumperOptions dumperOptions = new DumperOptions();
dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
dumperOptions.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);
dumperOptions.setSplitLines(false);
Yaml yaml = new Yaml(dumperOptions);
Iterator<Object> evidenceIterator = yaml.loadAll(is).iterator();
logger.info("Constructing pipeline with on-premise IP Intelligence " +
"engine from file " + dataFile);
try (Pipeline pipeline = new IPIntelligencePipelineBuilder()
.useOnPremise(detectionFile, false)
.setShareUsage(false)
.setAutoUpdate(false)
.setPerformanceProfile(Constants.PerformanceProfiles.MaxPerformance)
.build()) {
IPIntelligenceOnPremiseEngine engine = pipeline.getElement(IPIntelligenceOnPremiseEngine.class);
logger.info("IP Intelligence data file was created {}", engine.getDataFilePublishedDate());
try (Writer writer = new OutputStreamWriter(os)) {
int count = 0;
while (evidenceIterator.hasNext() && count < 20) {
try (FlowData flowData = pipeline.createFlowData()) {
flowData.addEvidence(
filterEvidence((Map<String, String>) evidenceIterator.next(),
"server."));
flowData.process();
IPIntelligenceData ipData = flowData.get(IPIntelligenceData.class);
Map<String, ? super Object> resultMap = new HashMap<>();
resultMap.put("ip.RegisteredName", asStringProperty(ipData.getRegisteredName()));
resultMap.put("ip.RegisteredOwner", asStringProperty(ipData.getRegisteredOwner()));
resultMap.put("ip.RegisteredCountry", asStringProperty(ipData.getRegisteredCountry()));
writer.write("---\n");
yaml.dump(flowData.getEvidence().asKeyMap(), writer);
yaml.dump(resultMap, writer);
writer.flush();
}
count++;
}
writer.write("...\n");
writer.flush();
logger.info("Finished processing {} records", count);
if (engine.getDataSourceTier().equals("Lite")) {
logger.warn("You have used a Lite data file which has " +
"limited properties and is of limited accuracy");
logger.info("The example requires an Enterprise data file " +
"to work fully. Find out about the Enterprise " +
"data file here: https://51degrees.com/pricing");
}
}
}
}
@SuppressWarnings("SameParameterValue")
private static Map<String, String> filterEvidence(Map<String, String> evidence, String prefix) {
return evidence.entrySet()
.stream()
.filter(e -> e.getKey().startsWith(prefix))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
@SuppressWarnings({"SameParameterValue", "unused"})
private static Map<String, Object> getPopulatedProperties(IPIntelligenceData data, String prefix) {
return data.asKeyMap().entrySet()
.stream()
.filter(e -> ((AspectPropertyValue<?>) e.getValue()).hasValue())
.collect(Collectors.toMap(e -> prefix + e.getKey(),
e -> ((AspectPropertyValue<?>)e.getValue()).getValue()));
}
}