2
votes

I'm using TitanGraphDB + Cassandra. I'm starting Titan as follows

cd titan-cassandra-0.3.1
bin/titan.sh config/titan-server-rexster.xml config/titan-server-cassandra.properties

I have a Rexster shell that I can use to communicate to Titan + Cassandra above.

cd rexster-console-2.3.0
bin/rexster-console.sh

I'm attempting to model a network topology using Titan Graph DB. I want to program the Titan Graph DB from my python program. I'm using bulbs package for that. I create five types of vertices

 - switch
 - port 
 - device
 - flow
 - flow_entry

I create edges between vertices that are connected logically. The edges are not labelled.

Let us say I want to test the connectivity between Vertex A and Vertex B

I have a groovy script is_connected.groovy

def isConnected (portA, portB) {
    return portA.both().retain([portB]).hasNext()
}

Now from my rexster console

g = rexster.getGraph("graph")
==>titangraph[embeddedcassandra:null]
rexster[groovy]> g.V('type', 'flow')    
==>v[116]
==>v[100]
rexster[groovy]> g.V('type', 'flow_entry')
==>v[120]
==>v[104]

As you can see above I have two vertices of type flow v[116] and v[100]

I have two vertices of type flow_entry v[120] and v[104]

I want to check for the connectivity between v[120] and v[116] for e.g

rexster[groovy]> ?e is_connected.groovy       
==>null
rexster[groovy]> is_connected(g.v(116),g.v(120))
==>true

So far so good.Now I want to be able to use this script from my python program that imports bulbs package.

My directory structure is as follows.

Projects/ryu
        --> ryu/app_simple_switch.py

Projects/ryu_extras
        --> rexster-console-2.3.0
        --> titan-cassandra-0.3.1

My script is_connected.groovy which contains isConnected() function/procedure is kept in Projects/ryu_extras/rexster-console-2.3.0

Now from my python program which is in Projects/ryu/ryu/app/simple_switch.py I try to do the following.

self.g.scripts.update('Projects/ryu_extras/rexster-console-2.3.0/is_connected.groovy')        # add file to scripts index
script = self.g.scripts.get('isConnected')      # get a function by its name
params = dict(portA=flow,portB=fe1)             # put function params in dict
items = self.g.gremlin.query(script, params)
self.create_outgoing_edge(flow,fe1)
self.create_outgoing_edge(fe1,sw_vertex)

I get the following error.

hub: uncaught exception: Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/ryu/lib/hub.py", line 48, in _launch
    func(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/ryu/base/app_manager.py", line 256, in _event_loop
    handler(ev)
  File "/home/karthik/Projects/ryu/ryu/app/simple_switch.py", line 322, in _packet_in_handler
    self.compute_path(src,dst,datapath)
  File "/home/karthik/Projects/ryu/ryu/app/simple_switch.py", line 289, in compute_path
    self.g.scripts.update('/home/karthik/Projects/ryu_extras/rexster-console-2.3.0/is_connected.groovy')   # add file to scripts index
  File "/usr/local/lib/python2.7/dist-packages/bulbs/groovy.py", line 120, in update
    methods = self._get_methods(file_path)
  File "/usr/local/lib/python2.7/dist-packages/bulbs/groovy.py", line 160, in _get_methods
    return Parser(file_path).get_methods()
  File "/usr/local/lib/python2.7/dist-packages/bulbs/groovy.py", line 255, in __init__
    Scanner(handlers).scan(groovy_file)
  File "/usr/local/lib/python2.7/dist-packages/bulbs/groovy.py", line 246, in scan
    self.get_item(fin,line)
  File "/usr/local/lib/python2.7/dist-packages/bulbs/groovy.py", line 236, in get_item
    content = "\n".join(sections).strip()
TypeError: sequence item 2: expected string or Unicode, NoneType found

As you can see the error is in scripts.update() function.I just can't seem to figure out what I am doing wrong.Any help would be appreciated.

2

2 Answers

2
votes

You need to save your scripts in a file named gremlin.groovy or specify the script's namespace when you get it from the Bulbs script index.

Like Rexster, Bulbs uses the first part of the Groovy filename as a namespace.

For example, methods defined in gremlin.groovy files are added to the Bulbs gremlin namespace.

All of Bulbs' pre-defined Gremlin scripts are defined in gremlin.groovy files and thus gremlin is the default namespace:

You can have multiple/additional gremlin.groovy files in your app. Do this if you want to keep everything under the same namespace or if you want to override a pre-defined method:

>>> g.scripts.update("/path/to/gremlin.groovy") # add scripts to gremlin namespace

See... https://github.com/espeed/bulbs/blob/f666fa89b3c99bc0a6b4e964aa1bff70b05a2e96/bulbs/groovy.py#L112

You can create app-specific and model-specific namespaces by defining your Gremlin methods in files with names like myapp.groovy or somemodel.groovy:

>>> g.scripts.update("/path/to/myapp.groovy")     # add scripts to myapp namespace
>>> g.scripts.update("/path/to/somemodel.groovy") # add scripts to somemodel namespace

And then to get a script under a particular namespace, do:

>>> script = g.scripts.get('myapp:isConnected')      # use prefix notation, or...
>>> script = g.scripts.get('isConnected', 'myapp')   # specify namespace as arg

See... https://github.com/espeed/bulbs/blob/f666fa89b3c99bc0a6b4e964aa1bff70b05a2e96/bulbs/groovy.py#L77

To generate concatenated server-side script files for each namespace, use the g.make_script_files() method:

>>> g.make_script_files() # write files to the current dir, or...
>>> g.make_script_files("/path/to/scripts/dir")    # write files to specified dir

The make_scripte_files() method will create a separate .groovy file for each namespace. If you override a method in a namespace, only the latest will be included in the generated file.

See... https://github.com/espeed/bulbs/blob/f666fa89b3c99bc0a6b4e964aa1bff70b05a2e96/bulbs/rexster/graph.py#L62

For more details, see...

1
votes

There might be a "bulbs way" to do this, but you could try to expose your function globally by putting it on the server with Rexster. Then in the <script-engine><init-scripts> section you can just add your is_connected.groovy. The sample rexster.xml should already have an example of this:

<script-engines>
    <script-engine>
        <name>gremlin-groovy</name>
        <reset-threshold>-1</reset-threshold>
        <init-scripts>config/is_connected.groovy</init-scripts>
        <imports>com.tinkerpop.rexster.client.*</imports>
        <static-imports>java.lang.Math.PI</static-imports>
    </script-engine>
</script-engines>