GithubHelp home page GithubHelp logo

rpsl4j-parser

A Routing Policy Specification Language parsing library forked from RIPE-NNC's whois project. The library contains the subset of classes required to parse and represent RPSL objects, along with added functionality to facilitate inspection of complex attributes.

Building and deploying

$ mvn package

for deployment instructions, refer to the rpsl4j-generator

Usage

Parsing string into RPSLObject set

String rpslDocumentString = "...";
Set<RpslObject> objectSet = new HashSet<>():
RpslObjectStringReader rpslReader = new RpslObjectStringReader(rpslDocumentString);

for(String objStr : rpslReader)
    objectSet.add(RpslObject.parse(objStr));

You can also use RpslObjectFileReader or the more general RpslObjectStreamReader if they are a better fit for your project.

Read the attributes of the RPSL object

RpslObject routeExample = RpslObject.parse("route: 1.1.1.0/24\n
origin: AS1\n mnt-by: MNT-EXAMPLE # Need to change owner\n");

// The value of the class attribute is retrieved as follows
RpslAttribute routePrefixAttr = routeExample.getTypeAttribute();

// The value of a specific attribute can be retrieved using the AttributeType enumerator
RpslAttribute originASAttr = routeExample.findAttribute(AttributeType.ORIGIN);

// Many attribute types have corresponding classes that provide useful encapsulation and functionality.
// Refer to the net.ripe.db.whois.common.rpsl.attrs package
//
AutNum originAS = AutNum.parse(originAsAttr.getCleanValue());
AddressPrefixRange routePrefix = AddressPrefixRange.parse(routePrefixAttr.getCleanValue());

// If you want the full text of an attribute (including comments etc), use RpslAttribute#getValue() instead of RpslAttribute#getCleanValue()
System.out.println(routeExample.findAttribute(AttributeType.MNT_BY).getValue());

Using the generated lexers to inspect complex attributes

When rpsl4j-parser is compiled, parsers and lexers are generated from bison and jflex sources for many RPSL attributes. These can be found in the net.ripe.db.whois.common.generated package and are located in the target/generated-sources/jflex directory. These are primarily used for validity checks but, in combination with the AttributeLexerWrapper class, can be used to inspect supported attributes more deeply.

The simplest interface to this functionality is the RpslAttribute#getTokenList method. This method will either return a list of syntax tokens (describd below) or an empty list, so it's important to check which attributes lexer's exist for before calling it.

The method returns the Java type List<Pair<String, List<String>>>, which represents the type [(TOKEN_TYPE, [TOKEN_VALUES])]. An example of this process follows:

RpslAttribute importAttribute = RpslAttrbute.parse(AttributeType.IMPORT, "import: from AS2 accept AS1, 1.2.3.4/5");

// Generate the token list for the attribute
List<Pair<String, List<String>>> importTokenList = importAttribtue.getTokenList();

System.out.println(importTokenList); // prints "[(from, [AS2]), (accept, [AS1, 1.2.3.4/5])]""

// Iterate through the tokens
for(Pair<String, List<String>> tokenPair : exportAttribute.getTokenList()) {
    String tokenType = tokenPair.getLeft();
    List<String> values = tokenPair.getRight();

    if(tokenType.equals("from"))
        setOrigins(values);
    else if(tokenType.equals("accept"))
        setIncoming(values);
}

AttributeLexerWrapper works by generating a table of lexer states and their corresponding token names via reflection, then running the lexer over an attribute; recording tokens and runs of values. Unfortunately there is no easy way of predetermining the structure of a token list without thoroughly studying the attribute's corresponding lexer. The best approach at present is by experiment and testing.

refer to the rpsl4j-generator library for more complex uses of AttributeLexerWrapper.

License

Excluding code already licensed by RIPE under the BSD license, the source code is licensed under the GNU Affero General Public License.

rpsl4j's Projects

rpsl4j-generator icon rpsl4j-generator

A framework for configuring routing services and devices using the Routing Policy Specification Language.

rpsl4j-opendaylight icon rpsl4j-opendaylight

An emitter library for configuring OpenDaylight Lithium's BGP speaker implementation using the Routing Policy Specification Language and rpsl4j-generator.

rpsl4j-parser icon rpsl4j-parser

Routing Policy Specification Language implementation for Java

vagrant-environment icon vagrant-environment

A virtualised environment for packaging rpsl4j and testing/developing against OpenDaylight Lithium and Quagga bgpd.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.