Skip to content

Commit

Permalink
example usage of OCCI grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
dizz committed Mar 22, 2011
1 parent afbaff3 commit 2d3bd00
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 0 deletions.
105 changes: 105 additions & 0 deletions occi-antlr-java/examples/OcciChomper.java
@@ -0,0 +1,105 @@
package occi.lexpar.demo;

import java.util.ArrayList;
import java.util.HashMap;

import occi.lexpar.OcciParser;

public class OcciChomper {

public static void main(String[] args) {

//Link text format
String link = "Link: " +
"</storage/disk03>; " +
"rel='http://example.com/occi/resource#storage';" +
"self='/link/456-456-456';" +
"category='http://example.com/occi/link#disk_drive';" +
"com.example.drive0.interface='ide0'," +
"com.example.drive1.interface='title'"
;

//Category text format
String category = "Category: " +
"storage; " +
"scheme='http://schemas.ogf.org/occi/infrastructure#';" +
"class='kind'; title='Storage Resource';" +
"rel='http://schemas.ogf.org/occi/core#resource';" +
"location=/storage/;" +
"attributes='occi.storage.size occi.storage.state';" +
"actions='http://schemas.ogf.org/occi/infrastructure/storage/action#resize'";

//Attribute text format
String attributes = "X-OCCI-Attribute: " +
"occi.compute.architechture='x86', " +
"occi.compute.cores=2, " +
"occi.compute.hostname='testserver', " +
"occi.compute.speed=2.66, " +
"occi.compute.memory=3.0, " +
"occi.compute.state='active'";

//Location text format
String location = "X-OCCI-Location: http://example.com/compute/123, http://example.com/compute/456";

//All the above combined as text format
String combined = category + "\n" + link + "\n" + attributes;

System.out.println("Will parse:\n" + link);

//parse links
ArrayList res;
try {
res = OcciParser.getParser(link).link();
System.out.println("No errors on parsing link header.");
System.out.println("Result: " + res.toString() + "\n");
} catch (Exception e2) {
e2.printStackTrace();
System.exit(0);
}

//parse category
System.out.println("Will parse:\n" + category);
try {
res = OcciParser.getParser(category).category();
System.out.println("No errors on parsing category header.");
System.out.println("Result: " + res.toString() + "\n");
} catch (Exception e1) {
e1.printStackTrace();
System.exit(0);
}

//parse attributes
System.out.println("Will parse:\n" + attributes);
HashMap res1;
try {
res1 = OcciParser.getParser(attributes).attribute();
System.out.println("No errors on parsing attribute header.");
System.out.println("Result: " + res1.toString() + "\n");
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}

//parse attributes
System.out.println("Will parse:\n" + location);
try {
res = OcciParser.getParser(location).location();
System.out.println("No errors on parsing location header.");
System.out.println("Result: " + res.toString() + "\n");
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}

//parse a set of combined OCCI headers
System.out.println("Will parse:\n" + combined);
try {
res1 = OcciParser.getParser(combined).headers();
System.out.println("No errors on parsing combined header.");
System.out.println("Result: " + res1.toString() + "\n");
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
}
54 changes: 54 additions & 0 deletions occi-antlr-java/examples/OcciQueryListing.java
@@ -0,0 +1,54 @@
package occi.lexpar.demo;

import java.util.HashMap;

import occi.lexpar.OcciParser;

public class OcciQueryListing {

public static String listing =
"Category: start; scheme='http://schemas.ogf.org/occi/infrastructure/compute/action#'; class='action'; title='Start Compute Action'\n"+
"Category: stop; scheme='http://schemas.ogf.org/occi/infrastructure/compute/action#'; class='action'; title='Stop Compute Action'; attributes='acpioff graceful poweroff'\n"+
"Category: restart; scheme='http://schemas.ogf.org/occi/infrastructure/compute/action#'; class='action'; title='Restart Compute Action'; attributes='warm graceful cold'\n"+
"Category: suspend; scheme='http://schemas.ogf.org/occi/infrastructure/compute/action#'; class='action'; title='Suspend Compute Action'; attributes='hibernate suspend'\n"+
"Category: up; scheme='http://schemas.ogf.org/occi/infrastructure/network/action#'; class='action'; title='Up Network Action'\n"+
"Category: down; scheme='http://schemas.ogf.org/occi/infrastructure/network/action#'; class='action'; title='Down Network Action'\n"+
"Category: online; scheme='http://schemas.ogf.org/occi/infrastructure/storage/action#'; class='action'; title='Online Storage Action'\n"+
"Category: offline; scheme='http://schemas.ogf.org/occi/infrastructure/storage/action#'; class='action'; title='Offline Storage Action'\n"+
"Category: backup; scheme='http://schemas.ogf.org/occi/infrastructure/storage/action#'; class='action'; title='Backup Storage Action'\n"+
"Category: snapshot; scheme='http://schemas.ogf.org/occi/infrastructure/storage/action#'; class='action'; title='Snapshot Storage Action'\n"+
"Category: resize; scheme='http://schemas.ogf.org/occi/infrastructure/storage/action#'; class='action'; title='Resize Storage Action'\n"+
"Category: degrade; scheme='http://schemas.ogf.org/occi/infrastructure/storage/action#'; class='action'; title='Degrade Storage Action'\n"+
"Category: stop; scheme='http://sla-at-soi.eu/occi/infrastructure/service/action#'; class='action'; title='A Stop Service Action'\n"+
"Category: start; scheme='http://sla-at-soi.eu/occi/infrastructure/service/action#'; class='action'; title='A Start Service Action'\n"+
"Category: restart; scheme='http://sla-at-soi.eu/occi/infrastructure/service/action#'; class='action'; title='A Restart Service Action'\n"+
"Category: suspend; scheme='http://sla-at-soi.eu/occi/infrastructure/service/action#'; class='action'; title='A Suspend Service Action'\n"+
"Category: entity; scheme='http://schemas.ogf.org/occi/core#'; class='kind'; title='Entity type'; attributes='occi.core.title occi.core.id'\n"+
"Category: resource; scheme='http://schemas.ogf.org/occi/core#'; class='kind'; title='Resource type'; rel='http://schemas.ogf.org/occi/core#entity'; attributes='occi.core.summary'\n"+
"Category: compute; scheme='http://schemas.ogf.org/occi/infrastructure#'; class='kind'; title='Compute type'; rel='http://schemas.ogf.org/occi/core#resource'; location=/compute; attributes='occi.compute.hostname occi.compute.architecture occi.compute.memory occi.compute.speed occi.compute.cores occi.compute.state'; actions='http://schemas.ogf.org/occi/infrastructure/compute/action#suspend http://schemas.ogf.org/occi/infrastructure/compute/action#stop http://schemas.ogf.org/occi/infrastructure/compute/action#start http://schemas.ogf.org/occi/infrastructure/compute/action#restart'\n"+
"Category: network; scheme='http://schemas.ogf.org/occi/infrastructure#'; class='kind'; title='Network type'; rel='http://schemas.ogf.org/occi/core#resource'; location=/network; attributes='occi.network.label occi.network.vlan occi.network.state'; actions='http://schemas.ogf.org/occi/infrastructure/network/action#down http://schemas.ogf.org/occi/infrastructure/network/action#up'\n"+
"Category: storage; scheme='http://schemas.ogf.org/occi/infrastructure#'; class='kind'; title='Storage type'; rel='http://schemas.ogf.org/occi/core#resource'; location=/storage; attributes='occi.storage.size occi.storage.state'; actions='http://schemas.ogf.org/occi/infrastructure/storage/action#offline http://schemas.ogf.org/occi/infrastructure/storage/action#backup http://schemas.ogf.org/occi/infrastructure/storage/action#snapshot http://schemas.ogf.org/occi/infrastructure/storage/action#degrade http://schemas.ogf.org/occi/infrastructure/storage/action#resize http://schemas.ogf.org/occi/infrastructure/storage/action#online'\n"+
"Category: service; scheme='http://sla-at-soi.eu/occi/infrastructure#'; class='kind'; title='Service type'; rel='http://schemas.ogf.org/occi/core#resource'; location=/service; attributes='eu.slasoi.infrastructure.service.monitoringconfig eu.slasoi.infrastructure.service.state eu.slasoi.infrastructure.service.name'; actions='http://sla-at-soi.eu/occi/infrastructure/service/action#start http://sla-at-soi.eu/occi/infrastructure/service/action#stop http://sla-at-soi.eu/occi/infrastructure/service/action#suspend http://sla-at-soi.eu/occi/infrastructure/service/action#restart'\n"+
"Category: link; scheme='http://schemas.ogf.org/occi/core#'; class='kind'; title='Link type'; rel='http://schemas.ogf.org/occi/core#entity'; location=/link; attributes='occi.core.target occi.core.source'\n"+
"Category: networkinterface; scheme='http://schemas.ogf.org/occi/infrastructure#'; class='kind'; title='A network interface link'; rel='http://schemas.ogf.org/occi/core#link'; location=/link; attributes='occi.networkinterface.state occi.networkinterface.interface occi.networkinterface.mac'\n"+
"Category: storagelink; scheme='http://schemas.ogf.org/occi/infrastructure#'; class='kind'; title='A storage link'; rel='http://schemas.ogf.org/occi/core#link'; location=/link; attributes='occi.storagelink.mountpoint occi.storagelink.state occi.storagelink.deviceid'\n"+
"Category: slasoicompute; scheme='http://sla-at-soi.eu/occi/infrastructure#'; class='mixin'; title='A SLA@SOI specific compute'; location=/compute; attributes='eu.sla-at-soi.image-identifier eu.sla-at-soi.physical-host'\n"+
"Category: ipnetwork; scheme='http://schemas.ogf.org/occi/infrastructure/network#'; class='mixin'; title='An IP Networking Mixin'; location=/network; attributes='occi.network.address occi.network.gateway occi.network.allocation'\n"+
"Category: ipnetworkinterface; scheme='http://schemas.ogf.org/occi/infrastructure/networkinterface#'; class='mixin'; title='An IP Network Interface Mixin'; location=/link; attributes='occi.networkinterface.address occi.networkinterface.gateway occi.networkinterface.allocation'\n"+
"Category: os_tpl; scheme='http://schemas.ogf.org/occi/infrastructure#'; class='mixin'; title='An OS Template Mixin'; location=/os_tpls\n"+
"Category: resource_tpl; scheme='http://schemas.ogf.org/occi/infrastructure#'; class='mixin'; title='A Resource Template Mixin'; location=/resource_tpls\n";

public static void main(String[] args) {

//parse links
HashMap res;
try {
res = OcciParser.getParser(listing).headers();
System.out.println("No errors on parsing results from query interface.");
System.out.println("Result: " + res.toString() + "\n");
} catch (Exception e2) {
e2.printStackTrace();
System.exit(0);
}
}
}

0 comments on commit 2d3bd00

Please sign in to comment.