You do... session.query(exists().where(SourcetoPort.src_address == str(packet.dst))).scalar()
Do exists() queries always return true or false? In that case, your if statement would ALWAYS be true. On the other hand, if dropping the exists... session.query(where(SourcetoPort.src_address == str(packet.dst))).scalar() .. would return None if there were no matches, then it seems like this would work. More generally, it seems like your two queries are almost equivalent. Why do both? Why not just something like... q_res = session.query(SourcetoPort).filter_by(src_address=str(packet.dst)).first() if q_res is not None: self.send_packet(packet_in.buffer_id, packet_in.data,q_res.port_no, packet_in.in_port) ... ? For that matter, why not try printing out the results of both queries before the if statement and see if they're the same or not? Good luck. -- Murphy On May 2, 2013, at 3:27 PM, Karthik Sharma wrote: > Hi I have a funciton act_like_switch defined below. > > 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() > > I am first checking if the specified mac address exists in the database > > if session.query(exists().where(SourcetoPort.src_address == > str(packet.dst))).scalar() is not None: > > If it does I am doing a query to retrieve the information as below. > > 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) > > for some reason query is returning a none object.I don't know why after the > if condition above succeeds.Any help is appreciated. > > Regards, > Karthik.