from pox.core import core
import pox.openflow.libopenflow_01 as of
import re
import datetime
import time
from sqlalchemy import create_engine, ForeignKey
from sqlalchemy import Column, Date, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, backref
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.sql.expression import exists
log = core.getLogger()
engine = create_engine('mysql://root@192.168.129.139/nwtopology', echo=False)
Base = declarative_base()
Session = sessionmaker(bind=engine)
session = Session()
class SourcetoPort(Base):
""""""
__tablename__ = 'source_to_port'
id = Column(Integer, primary_key=True)
port_no = Column(Integer)
src_address = Column(String(16),index=True)
#-----------------------------------------
def __init__(self, src_address,port_no):
""""""
self.src_address = src_address
self.port_no = port_no
#create tables
Base.metadata.create_all(engine)
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 ,"DEST" , packet.dst
self.mac_to_port[packet.src]=packet_in.in_port
r_res = session.query(SourcetoPort).filter_by(src_address=str(packet.src)).first()
if r_res is None:
print "inserting an entry"
start = time.time()
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()
end = time.time()
elapsed = end - start
print "elapsed insert time ",elapsed
else:
print "entry already present"
我有一个网络,我将数据包从一个主机发送到另一个主机。预期的行为是第一次 src_address,port 对应该插入到 sql 数据库中,然后它应该只进行检索操作。我已经测试了这个功能使用 sqlite 正确使用本地数据库。然后我使用 mysql 转移到远程数据库。
现在我发现
if r_res is None:
永远是真的,意义
r_res = session.query(SourcetoPort).filter_by(src_address=str(packet.src)).first()
总是失败。
我怀疑这与mysql的一些错误查询/添加与sqlite不同。
It did print out a warning as below when inserting an entry.
/usr/lib/python2.7/dist-packages/sqlalchemy/engine/default.py:330: Warning: Data truncated for column 'src_address' at row 1
cursor.execute(statement, parameters)
我无法弄清楚究竟是什么问题。
根据评论,我打印出我要插入的 src_address 的大小。
print 'length of src addreess',len(str(packet.src))
it prints 17
我已经将 src_address 的大小增加到 17,32 和 64。我仍然得到同样的错误