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.
This example demonstrates the integration in a simplified form. It intentionally outputs only a small subset of the available properties (the registered network details) to keep the illustration clear. Many more IP Intelligence properties can be enabled - refer to the Property Dictionary for the complete list. The commented-out getPopulatedProperties call further down shows how to output every populated property instead of the fixed subset.
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 using Performance Profile settings.
By default the example uses the free Lite IP Intelligence data file, which it downloads when it is missing, and records Country, which only the Enterprise data file has, as needing it. To obtain an Enterprise data file, 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.asString;
import static fiftyone.ipintelligence.examples.shared.PropertyHelper.asStringProperty;
import static fiftyone.ipintelligence.examples.shared.DataFileHelper.LITE_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";
public static final String HEADER_EVIDENCE_YML =
dataDir + "/evidence.yml";
private static final String CONTACT_US = "/contact-us?utm_source=code&utm_medium=example&utm_campaign=51degrees-java&utm_content=console-src-main-java-fiftyone-ipintelligence-examples-console-offlineprocessing.java&utm_term=enterprise-data-file";
public static void main(String[] args) throws Exception {
configureLogback(getFilePath("logback.xml"));
String dataFile = (args.length > 0) ? args[0] : LITE_DATA_FILE_REL_PATH;
File evidenceFile = DataFileHelper.findDataFile(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 dataFileLocation;
try {
dataFileLocation = 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). " +
"An explicit path can be supplied via the {} environment variable.",
dataFile, DataFileHelper.IPI_PATH_VARIABLE);
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(dataFileLocation, false)
.setShareUsage(false)
.setAutoUpdate(false)
.setPerformanceProfile(Constants.PerformanceProfiles.LowMemory)
.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;
boolean needsEnterprise = false;
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()));
if (DataFileHelper.hasProperty(engine, "Country")) {
resultMap.put("ip.Country", asString(ipData.getCountry()));
} else {
resultMap.put("ip.Country", DataFileHelper.NEEDS_ENTERPRISE);
needsEnterprise = true;
}
writer.write("---\n");
yaml.dump(flowData.getEvidence().asKeyMap(), writer);
yaml.dump(resultMap, writer);
writer.flush();
}
count++;
}
if (needsEnterprise) {
writer.write("# Some of these properties need the " +
"Enterprise data file. Contact us for a copy at " +
CONTACT_US + "\n");
}
writer.write("...\n");
writer.flush();
logger.info("Finished processing {} records", count);
}
}
}
@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()));
}
}
static void run(String dataFile, InputStream is, OutputStream os)
Process a YAML representation of evidence - and create a YAML output containing the processed evidenc...
Definition OfflineProcessing.java:137