[pox-dev] POX controller with sqlalchemy backend error

Karthik Sharma karthik.sharma at gmail.com
Thu May 2 14:36:54 PDT 2013


I am wirting a POX controller which uses a database backend.


I have this peice of code.I replaced

#if session.query(SourcetoPort).filter_by(src_address=str(packet.dst)).count():

 with

if session.query(exists().where(SourcetoPort.src_address ==
str(packet.dst))).scalar() is not None:
Now I am getting the following error.


  File "/home/karthik/pox/tutorial.py", line 86, in act_like_switch
    self.send_packet(packet_in.buffer_id,
packet_in.data,q_res.port_no, packet_in.in_port)AttributeError:
'NoneType' object has no attribute 'port_no'^CINFO:core:Going down...



The code is given below.The above piece of code used to work with
count query.Why is it now working with exists query. I am very new to
python and sqlalchemy.Can someone advise why this is happening.?

class SourcetoPort(Base):
    """"""
    __tablename__ = 'source_to_port'
    id = Column(Integer, primary_key=True)
    port_no        = Column(Integer)
    src_address    = Column(String,index=True)

    #----------------------------------------------------------------------
    def __init__(self, src_address,port_no):
        """"""
        self.src_address = src_address
        self.port_no     = port_no


  def act_like_switch (self, packet, packet_in):
    """
    Implement switch-like behavior.
    """
    # Learn the port for the source MAC
    #print "RECIEVED FROM PORT ",packet_in.in_port , "SOURCE ",packet.src
    # create a Session
    #Session = sessionmaker(bind=engine)
    #session = Session()
    self.mac_to_port[packet.src]=packet_in.in_port
    #if self.mac_to_port.get(packet.dst)!=None:
    print "count for
dst",session.query(SourcetoPort).filter_by(src_address=str(packet.dst)).count(),str(packet.dst)
    #if session.query(SourcetoPort).filter_by(src_address=str(packet.dst)).count():
    if session.query(exists().where(SourcetoPort.src_address ==
str(packet.dst))).scalar() is not None:
           #send this packet
           print "got info from the database"
           q_res =
session.query(SourcetoPort).filter_by(src_address=str(packet.dst)).first()
           self.send_packet(packet_in.buffer_id,
packet_in.data,q_res.port_no, packet_in.in_port)
           #create a flow modification message
           msg = of.ofp_flow_mod()
           #set the fields to match from the incoming packet
           msg.match = of.ofp_match.from_packet(packet)
           #send the rule to the switch so that it does not query the
controller again.
           msg.actions.append(of.ofp_action_output(port=q_res.port_no))
           #push the rule
           self.connection.send(msg)
    else:
           #flood this packet out as we don't know about this node.
           print "flooding the first packet"
           self.send_packet(packet_in.buffer_id, packet_in.data,
                       of.OFPP_FLOOD, packet_in.in_port)
           #self.matrix[(packet.src,packet.dst)]+=1
           entry = SourcetoPort(src_address=str(packet.src) ,
port_no=packet_in.in_port)
           #add the record to the session object
           session.add(entry)
           #add the record to the session object
           session.commit()

Regards,

Karthik Sharma
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.noxrepo.org/pipermail/pox-dev-noxrepo.org/attachments/20130503/241539b5/attachment-0001.htm>


More information about the pox-dev mailing list