pythontitanbulbsrexster

Bulbs: Creating a vertex only when not already present


I am using bulbs to create vertices.I have written 3 functions given below to do the same.Just to help you understand the properties of the vertices I am listing below the output of the debug lines in each of the functions.

DEBUG LINE:
       self.logger.info('%s',self.switches)
OUTPUT:
    00:00:00:00:00:00:02:01
    00:00:00:00:00:00:02:02
    00:00:00:00:00:00:02:03
    00:00:00:00:00:00:02:04
    00:00:00:00:00:00:02:05
    00:00:00:00:00:00:02:06

DEBUG LINE:
    self.logger.info('%s',self.ports)
OUTPUT:
    {'type': 'port', 'number': 1, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:011', 'desc': 's1-eth1'}
    {'type': 'port', 'number': 2, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:012', 'desc': 's1-eth2'}
    {'type': 'port', 'number': 1, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:021', 'desc': 's2-eth1'}
    {'type': 'port', 'number': 2, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:022', 'desc': 's2-eth2'}
    {'type': 'port', 'number': 1, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:031', 'desc': 's3-eth1'}
    {'type': 'port', 'number': 2, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:032', 'desc': 's3-eth2'}
    {'type': 'port', 'number': 1, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:041', 'desc': 's4-eth1'}
    {'type': 'port', 'number': 2, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:042', 'desc': 's4-eth2'}
    {'type': 'port', 'number': 1, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:051', 'desc': 's5-eth1'}
    {'type': 'port', 'number': 2, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:052', 'desc': 's5-eth2'}
    {'type': 'port', 'number': 1, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:061', 'desc': 's6-eth1'}
    {'type': 'port', 'number': 2, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:062', 'desc': 's6-eth2'}

DEBUG LINE:
    self.logger.info('%s',self.devices)
OUTPUT:    
    {'dl_addr': '00:00:00:00:00:04', 'state': 'active', 'type': 'device'}
    {'dl_addr': '00:00:00:00:00:01', 'state': 'active', 'type': 'device'}
    {'dl_addr': '00:00:00:00:00:03', 'state': 'active', 'type': 'device'}
    {'dl_addr': '00:00:00:00:00:05', 'state': 'active', 'type': 'device'}
    {'dl_addr': '00:00:00:00:00:02', 'state': 'active', 'type': 'device'}
    {'dl_addr': '00:00:00:00:00:06', 'state': 'active', 'type': 'device'}

       def create_switch_vertices(self):
        for index,switch in enumerate(self.switch_list):
               dpid_str = dpid_to_str(switch.dp.id)
               dpid_str = ':'.join([dpid_str[i:i + 2] for i in range(0, len(dpid_str), 2)])
               self.switches = {'state':'active','dpid':dpid_str,'type':'switch'}
               self.logger.info('%s',dpid_str)
               self.sw_vertices.append(self.g.vertices.create({'state':'active','dpid':dpid_str,'type':'switch'}))


    def create_port_vertices(self):
         for index,switch in enumerate(self.switch_list):
                for port in switch.ports:
                        desc = port.name
                        hw_addr = port.hw_addr
                        state = 'active'
                        port_state = 1
                        number = port.port_no
                        dpid_str = dpid_to_str(port.dpid)
                        dpid_s = ':'.join([dpid_str[i:i + 2] for i in range(0, len(dpid_str), 2)])
                        port_id = dpid_s + str(number)
                        self.ports = {'desc':desc,'port_id':port_id,'state':state,'port_state':port_state,'number':number,'type':'port'}
                        self.logger.info('%s',self.ports)
                        self.port_vertices.append(self.g.vertices.create({'desc':desc,'port_id':port_id,'state':state,'port_state':port_state,'number':number,'type':'port'}))

    def create_host_vertices(self):
        for value in switch_port_to_host.values():
                self.devices = {'state':'active','dl_addr':value,'type':'device'}
                self.logger.info('%s',self.devices)
                self.host_vertices.append(self.g.vertices.create({'state':'active','dl_addr':value,'type':'device'}))

My questions are the following.

1) I have opened a rexster shell opened after running this code. If I do

g = rexster.getGraph("graph")
 ==>titangraph[embeddedcassandra:null]

 g.V('type', 'device').map       
 ==>{state=active, type=device, dl_addr=00:00:00:00:00:05}
 ==>{state=active, type=device, dl_addr=00:00:00:00:00:06}
 ==>{state=active, type=device, dl_addr=00:00:00:00:00:04}
 ==>{state=active, type=device, dl_addr=00:00:00:00:00:03}
 ==>{state=active, type=device, dl_addr=00:00:00:00:00:02}
 ==>{state=active, type=device, dl_addr=00:00:00:00:00:05}
 ==>{state=active, type=device, dl_addr=00:00:00:00:00:06}
 ==>{state=active, type=device, dl_addr=00:00:00:00:00:04}
 ==>{state=active, type=device, dl_addr=00:00:00:00:00:01}
 ==>{state=active, type=device, dl_addr=00:00:00:00:00:03}
 ==>{state=active, type=device, dl_addr=00:00:00:00:00:01}
 ==>{state=active, type=device, dl_addr=00:00:00:00:00:02}

g.V('type', 'switch').map     
 ==>{state=active, dpid=00:00:00:00:00:00:02:06, type=switch}
 ==>{state=active, dpid=00:00:00:00:00:00:02:05, type=switch}
 ==>{state=active, dpid=00:00:00:00:00:00:02:02, type=switch}
 ==>{state=active, dpid=00:00:00:00:00:00:02:04, type=switch}
 ==>{state=active, dpid=00:00:00:00:00:00:02:06, type=switch}
 ==>{state=active, dpid=00:00:00:00:00:00:02:03, type=switch}
 ==>{state=active, dpid=00:00:00:00:00:00:02:01, type=switch}
 ==>{state=active, dpid=00:00:00:00:00:00:02:04, type=switch}
 ==>{state=active, dpid=00:00:00:00:00:00:02:01, type=switch}
 ==>{state=active, dpid=00:00:00:00:00:00:02:05, type=switch}
 ==>{state=active, dpid=00:00:00:00:00:00:02:02, type=switch}
 ==>{state=active, dpid=00:00:00:00:00:00:02:03, type=switch}

  g.V('type', 'port').map       
  ==>{desc=s5-eth1, port_id=00:00:00:00:00:00:02:051, state=active, port_state=1, number=1, type=port}
  ==>{desc=s5-eth1, port_id=00:00:00:00:00:00:02:051, state=active, port_state=1, number=1, type=port}
  ==>{desc=s1-eth1, port_id=00:00:00:00:00:00:02:011, state=active, port_state=1, number=1, type=port} 
  ==>{desc=s2-eth1, port_id=00:00:00:00:00:00:02:021, state=active, port_state=1, number=1, type=port}
  ==>{desc=s6-eth1, port_id=00:00:00:00:00:00:02:061, state=active, port_state=1, number=1, type=port}
  ==>{desc=s3-eth1, port_id=00:00:00:00:00:00:02:031, state=active, port_state=1, number=1, type=port}
  ==>{desc=s3-eth2, port_id=00:00:00:00:00:00:02:032, state=active, port_state=1, number=2, type=port}
  ==>{desc=s1-eth2, port_id=00:00:00:00:00:00:02:012, state=active, port_state=1, number=2, type=port}
  ==>{desc=s3-eth1, port_id=00:00:00:00:00:00:02:031, state=active, port_state=1, number=1, type=port}
  ==>{desc=s4-eth1, port_id=00:00:00:00:00:00:02:041, state=active, port_state=1, number=1, type=port}
  ==>{desc=s6-eth2, port_id=00:00:00:00:00:00:02:062, state=active, port_state=1, number=2, type=port}
  ==>{desc=s1-eth2, port_id=00:00:00:00:00:00:02:012, state=active, port_state=1, number=2, type=port}
  ==>{desc=s3-eth2, port_id=00:00:00:00:00:00:02:032, state=active, port_state=1, number=2, type=port}
  ==>{desc=s6-eth2, port_id=00:00:00:00:00:00:02:062, state=active, port_state=1, number=2, type=port}
  ==>{desc=s4-eth2, port_id=00:00:00:00:00:00:02:042, state=active, port_state=1, number=2, type=port}
  ==>{desc=s5-eth2, port_id=00:00:00:00:00:00:02:052, state=active, port_state=1, number=2, type=port}
  ==>{desc=s1-eth1, port_id=00:00:00:00:00:00:02:011, state=active, port_state=1, number=1, type=port}
  ==>{desc=s4-eth1, port_id=00:00:00:00:00:00:02:041, state=active, port_state=1, number=1, type=port}
  ==>{desc=s5-eth2, port_id=00:00:00:00:00:00:02:052, state=active, port_state=1, number=2, type=port}
  ==>{desc=s2-eth2, port_id=00:00:00:00:00:00:02:022, state=active, port_state=1, number=2, type=port}
  ==>{desc=s6-eth1, port_id=00:00:00:00:00:00:02:061, state=active, port_state=1, number=1, type=port}
  ==>{desc=s2-eth2, port_id=00:00:00:00:00:00:02:022, state=active, port_state=1, number=2, type=port}
  ==>{desc=s4-eth2, port_id=00:00:00:00:00:00:02:042, state=active, port_state=1, number=2, type=port}
  ==>{desc=s2-eth1, port_id=00:00:00:00:00:00:02:021, state=active, port_state=1, number=1, type=port}

You can see multiple copies of the same vertices created probably when the code is ran multiple times.I want to avoid this situation.I want the vertex to be created only if it does not already exist.How do I achieve this.


Solution

  • This question was answered in the gremlin-users mailing list:

    https://groups.google.com/forum/#!topic/gremlin-users/5rVEiX744-c

    From the thread, one can either:

    1. write their own getOrCreate method in gremlin
    2. consider usage of the Bulbs native function