def stopSimulator(simualtor, msg=None): """ stop simulator : Stop simulator and quits.""" if (msg): print "Error reported:", msg cmd = simuProtocol_pb2.clientCommand() cmd.id = CMD_SIMULATOR_STOP ans = simulator.sendCommand(cmd) print "Simulator stoped......."
def stopSimulator( simualtor, msg=None): """ stop simulator : Stop simulator and quits.""" if ( msg ): print "Error reported:",msg cmd = simuProtocol_pb2.clientCommand() cmd.id = CMD_SIMULATOR_STOP ans = simulator.sendCommand( cmd ) print "Simulator stoped......."
def turnOnDevices(cmList, simulator): msgscmd = simuProtocol_pb2.clientCommand() msgscmd.id = CMD_SIMULATOR_SENDMSG for cm in cmList: pair = msgscmd.devices.add() pair.cmMac = cm[0] pair.cmtsMac = '' pair.msg = 'power_on' pair = msgscmd.devices.add() pair.cmMac = cm[0] pair.cmtsMac = '' pair.msg = 'dhcp_discover' ans = simulator.sendCommand( msgscmd )
def waitforips(simulator, totalCMs, devicecounter, delayBetweenLoops=.1, loops=40): cmsWithIP = 0 noChangeLoop = 0 cmd = simuProtocol_pb2.clientCommand() cmd.id = devicecounter while True: ans = simulator.sendCommand( cmd ) iIpCM = int(ans.ipCM) if (cmsWithIP < iIpCM): noChangeLoop = 0 cmsWithIP = iIpCM print "devices with IP",ans.ipCM else: noChangeLoop+=1 if ( iIpCM == totalCMs) or (noChangeLoop > loops): break time.sleep(delayBetweenLoops) return cmsWithIP
def setupCMTS(cmtsIP, cmtsMac, cmtsName, helperAddress, DHCPMac=None , simulator_ref = None): """ Setup the CMTS to be used during this simulation. cmtsIP : The IP for yor CMTS ( be aware of routing issues). ( yeap, is virtual ) cmtsMac : the macaddress for your CMTS. ( also virtual ) cmtsName : name of your virtual CMTS helperAddress : Where the CMTS should resend the unicast packets.( DHCP Server IP ) DHCPMac : if DHCP server and simulator are on the same network do not need set anything (arp will do the magic). In other case, need to set the MacAddres simulator_ref : simulator reference. """ # Get active CMTSs cmd = simuProtocol_pb2.clientCommand() # request for a lists of CMTS cmd.id = CMD_SIMULATOR_SHOWCMTS ans = simulator_ref.sendCommand( cmd ) if ( len(ans.macCmts._values) > 0 ): # mean we have at least one active CMTS print "Active CMTSs :",cmtsMac return ans.macCmts._values[0] else: print "No active CMTS. adding ...", cmtsMac,cmtsIP,cmtsName,helperAddress # building a command for adding a CMTS cmd.id = CMD_SIMULATOR_ADDCMTS item = cmd.CMTSDevices.add() item.cmtsMac = cmtsMac item.cmtsIP = cmtsIP item.cmtsName = cmtsName item.helperAddressIP = helperAddress item.helperAddressMAC = DHCPMac ans = simulator_ref.sendCommand( cmd ) if ( ans.id != ANS_OK ): print 'Error addind CMTS' raise ValueError else: print 'CMTS Added succesfully' return cmtsMac
def processRequest(self, clientsock): # 1st Receive fixed packet length msg = '' while len(msg) < 4: chunk = clientsock.recv(4 - len(msg)) if chunk == '': raise RuntimeError, "socket connection broken" msg = msg + chunk res = struct.unpack('<i', msg) SimulatorLogger.debug("Simulator will receive %d bytes from client", res[0]) # read data from network cmd = simuProtocol_pb2.clientCommand() networkData = '' while len(networkData) < res[0]: rawData = clientsock.recv(res[0] - len(networkData)) if rawData == '': raise RuntimeError, "socket connection broken" networkData += rawData cmd.ParseFromString(networkData) simAnswer = simuProtocol_pb2.simulatorAnswer() if cmd.id == CMD_SIMULATOR_START: SimulatorLogger.info("SIMULATOR_START cmd received...") self.simulator.simulatorStatus = RUNNING simAnswer.id = ANS_OK elif cmd.id == CMD_SIMULATOR_STOP: SimulatorLogger.info("SIMULATOR_STOP cmd received...") self.simulator.simulatorStatus = STOPPED self.simulator.machine.cms = {} self.simulator.machine.IPcms = {} simAnswer.id = ANS_OK elif cmd.id == CMD_SIMULATOR_EXIT: SimulatorLogger.info("SIMULATOR_EXIT cmd received...") self.simulator.simulatorStatus = STOPPED self.simulator.doLoop = False self.simulator.signal(simMessage('exit', mac='00:00:00:00:00:00')) simAnswer.id = ANS_OK elif cmd.id == CMD_SIMULATOR_FLUSHCMS: SimulatorLogger.info("SIMULATOR_FLUSHCMS cmd received...") self.simulator.machine.cms = {} self.simulator.machine.IPcms = {} simAnswer.id = ANS_OK elif cmd.id == CMD_SIMULATOR_FLUSHCMTS: SimulatorLogger.info("SIMULATOR_FLUSHCMTS cmd received...") self.simulator.machine.IPcmts = {} self.simulator.machine.cmts = {} simAnswer.id = ANS_OK elif cmd.id == CMD_SIMULATOR_GETCMIP: """ return for a given macaddres (specified in cmMac), it's IP in ipCM""" SimulatorLogger.info("SIMULATOR_GETCMIP cmd received...") item = cmd.devices._values[0] if (self.simulator.machine.cms.has_key(item.cmMac)): dev = self.simulator.machine.cms[item.cmMac] simAnswer.ipCM = dev.ip SimulatorLogger.info("SIMULATOR_GETCMIP IP assigned=%s", simAnswer.ipCM) simAnswer.id = ANS_OK else: SimulatorLogger.error( "SIMULATOR_GETCMIP did not find device %s", item.cmMac) simAnswer.id = ANS_ERR elif cmd.id == CMD_SIMULATOR_SHOWCMS: """ return a list of macaddres registered into simulator """ SimulatorLogger.info("SIMULATOR_SHOWCMS cmd received...") for mac in self.simulator.machine.cms.iterkeys(): simAnswer.macCms.append(mac) simAnswer.id = ANS_SHOWCMS elif cmd.id == CMD_SIMULATOR_SHOWCMS_DETAILED: SimulatorLogger.debug("SIMULATOR_SHOWCMS_DETAILED cmd received...") for mac, dev in self.simulator.machine.cms.iteritems(): item = simAnswer.cablemodems.add() item.cmMac = mac item.cmtsMac = '' item.ip = dev.ip if dev.cmTimers.has_key('dhcp_discover'): item.timer_dhcp_discover = dev.cmTimers['dhcp_discover'] if dev.cmTimers.has_key('dhcp_offer'): item.timer_dhcp_offer = dev.cmTimers['dhcp_offer'] if dev.cmTimers.has_key('dhcp_request'): item.timer_dhcp_request = dev.cmTimers['dhcp_request'] if dev.cmTimers.has_key('dhcp_ack'): item.timer_dhcp_ack = dev.cmTimers['dhcp_ack'] simAnswer.id = ANS_SHOWCMS_DETAILED elif cmd.id == CMD_SIMULATOR_GETAMOUNTCMS_WITH_IP: SimulatorLogger.debug( "SIMULATOR_GETAMOUNTCMS_WITH_IP cmd received...") cmsWithIP = 0 for mac, dev in self.simulator.machine.cms.iteritems(): if (dev.ip != "0.0.0.0"): cmsWithIP += 1 simAnswer.ipCM = str(cmsWithIP) simAnswer.id = ANS_OK elif cmd.id == CMD_SIMULATOR_GETAMOUNTCMS: SimulatorLogger.info("SIMULATOR_GETAMOUNTCMS cmd received...") simAnswer.ipCM = str(len(self.simulator.machine.cms)) simAnswer.id = ANS_OK elif cmd.id == CMD_SIMULATOR_SENDMSG: SimulatorLogger.debug("SIMULATOR_SENDMSG cmd received...") for index in cmd.devices._values: SimulatorLogger.debug("%s :: signal=%s", index.cmMac, index.msg) self.simulator.signal(simMessage(index.msg, mac=index.cmMac)) simAnswer.id = ANS_OK elif cmd.id == CMD_SIMULATOR_GETSTATUS_TFTP: cmCounter = 0 for mac, dev in self.simulator.machine.cms.iteritems(): if (dev.tftp.bootfileSize > 0): cmCounter += 1 self.answer.msg = cmCounter self.answer.setCmdId(ANS_GETSTATUS_TFTP) elif cmd.id == CMD_SIMULATOR_GETSTATUS: # XXX not implemented... self.answer.setCmdId(ANS_GETSTATUS) elif cmd.id == CMD_SIMULATOR_SHOWCMTS: SimulatorLogger.info("SIMULATOR_SHOWCMTS cmd received...") for mac in self.simulator.machine.cmts.iterkeys(): simAnswer.macCmts.append(mac) simAnswer.id = ANS_SHOWCMTS elif cmd.id == CMD_SIMULATOR_ADDCMTS: for index in cmd.CMTSDevices._values: newcmts = cmts.CMTS(CMTSName=index.cmtsName, macaddress=index.cmtsMac, ip=index.cmtsIP) cmtsOperation = self.simulator.add_cmts(newcmts) SimulatorLogger.info( "SIMULATOR_ADDCMTS cmd received (%s:%s)=%s", index.cmtsMac, index.cmtsIP, cmtsOperation) if (cmtsOperation and index.helperAddressIP): # turn on CMTS ( by default CMTS will be on ) self.simulator.signal( simMessage("power_on", mac=index.cmtsMac)) SimulatorLogger.info( "SIMULATOR_ADDCMTS setting Helper Address Parameters (%s:%s)", index.helperAddressIP, index.helperAddressMAC) opResult = newcmts.setDhcpServer(index.helperAddressIP, index.helperAddressMAC) else: opResult = ANS_ERR SimulatorLogger.error( "Invalid CM Mac(%s). CPE %s could not be registered", index.cmMac, index.cmMac) simAnswer.id = opResult elif cmd.id == CMD_SIMULATOR_ADDCPE: SimulatorLogger.info("Cmd received:ADD_CPE") for index in cmd.devices._values: if self.simulator.machine.cms.has_key( index.cmMac): # if the CM associated to CPE exists cm_cpe = self.simulator.machine.cms[index.cmMac] # CPE mac travels on cmtsMac field opResult = self.simulator.add_cpe( CPE(index.cmtsMac, cm=cm_cpe)) SimulatorLogger.info("CPE %s added behing CM %s", index.cmtsMac, index.cmMac) else: SimulatorLogger.error( "Invalid CM Mac(%s). CPE %s could not be registered", index.cmMac, index.cmtsMac) opResult = ANS_ERR simAnswer.id = opResult elif cmd.id == CMD_SIMULATOR_GETAMOUNTCPES_WITH_IP: SimulatorLogger.debug( "SIMULATOR_GETAMOUNTCPES_WITH_IP cmd received...") cmsWithIP = 0 for mac, dev in self.simulator.machine.cpes.iteritems(): if (dev.ip != "0.0.0.0"): cmsWithIP += 1 simAnswer.ipCM = str(cmsWithIP) simAnswer.id = ANS_OK elif cmd.id == CMD_SIMULATOR_ADDCM: SimulatorLogger.info("Cmd received:ADDCM") for index in cmd.devices._values: if self.simulator.machine.cmts.has_key(index.cmtsMac): localcmts = self.simulator.machine.cmts[index.cmtsMac] opResult = self.simulator.add_cm( CM(index.cmMac, cmts=localcmts)) else: pass #self.answer.answer.append( (data.maclist[index][0], False) ) #self.answer.msg="Did not find CMTS for association with CM" #self.answer.setCmdId(ANS_ERR) simAnswer.id = ANS_ADDCM else: SimulatorLogger.error("Unknown cmd received (%d)", data.cmdid) #clientsock.sendall( simAnswer.SerializeToString() ) answerSize = len(simAnswer.SerializeToString()) res = struct.pack('<i', answerSize) #SimulatorLogger.debug("client must received %d bytes", answerSize) clientsock.send(res) totalsent = 0 data = simAnswer.SerializeToString() while totalsent < answerSize: sent = clientsock.send(data[totalsent:]) if sent == 0: raise RuntimeError, "socket connection broken" totalsent = totalsent + sent
try: # read clienttools.setupCMTS function signature to understand every parameter cmtsMac = clienttools.setupCMTS('31.0.0.1', '00:16:36:dd:fa:00', 'UBR 1000', '192.168.204.149', '00:0c:29:f4:c1:fa', simulator_ref=simulator) # generates a list of totalCM len, with uniques mac address. i cmList = populateList(totalCM, cmtsMac) except: stopSimulator(simulator, "Could not setup CMTSs. Aborting") # Every CM on cmList, will be assigned ( "connected" ), to CMTS with macaddress cmtsMac cmd = simuProtocol_pb2.clientCommand() cmd.id = CMD_SIMULATOR_ADDCM for device in cmList: pair = cmd.devices.add() pair.cmMac = device[0] pair.cmtsMac = device[1] # send them all to simulator.... ans = simulator.sendCommand(cmd) if (ans.id == ANS_ERR): stopSimulatior(simulator, ans.msg) # request for a list of CM registered on the Simulator cmd.id = CMD_SIMULATOR_SHOWCMS ans = simulator.sendCommand(cmd) print "CMs registered for booting :", len(ans.macCms)
# Setup the birdge with the ip where the simulator is running... simulator = api.SimulatorBridge('localhost',10003) print "Client connected....." try: # read clienttools.setupCMTS function signature to understand every parameter virtualCMTSMac = clienttools.setupCMTS('31.0.0.1','00:16:36:dd:fa:00', 'UBR 1000', '192.168.204.149', '00:0c:29:f4:c1:fa',simulator_ref=simulator) except: stopSimulator(simulator, "Could not setup CMTS. Aborting") # generates a list of totalCM len, with uniques mac address and associate them to cmtsMac cmList = api.populateList( totalCM,virtualCMTSMac ) # Every CM on cmList, will be assigned ("connected"), to CMTS with macaddress cmtsMac cmd = simuProtocol_pb2.clientCommand() cmd.id = api.CMD_SIMULATOR_ADDCM for device in cmList: pair = cmd.devices.add() pair.cmMac = device[0] pair.cmtsMac = device[1] # send them all to simulator.... ans = simulator.sendCommand( cmd ) if (ans.id == api.ANS_ERR): stopSimulatior(simulator, ans.msg ) # now, add the CPEs to simulator cmd = simuProtocol_pb2.clientCommand() cmd.id = api.CMD_SIMULATOR_ADDCPE # we already have the CM mac on pair.cmMac, so need to add CPE Mac # for this, we use cmtsMac reg.
def processRequest( self, clientsock ): # 1st Receive fixed packet length msg = '' while len(msg) < 4: chunk = clientsock.recv(4-len(msg)) if chunk == '': raise RuntimeError, "socket connection broken" msg = msg + chunk res = struct.unpack('<i',msg) SimulatorLogger.debug("Simulator will receive %d bytes from client", res[0]) # read data from network cmd = simuProtocol_pb2.clientCommand() networkData = '' while len(networkData) < res[0]: rawData = clientsock.recv(res[0] - len(networkData) ) if rawData=='': raise RuntimeError, "socket connection broken" networkData+=rawData cmd.ParseFromString(networkData) simAnswer = simuProtocol_pb2.simulatorAnswer() if cmd.id == CMD_SIMULATOR_START: SimulatorLogger.info("SIMULATOR_START cmd received...") self.simulator.simulatorStatus = RUNNING simAnswer.id = ANS_OK elif cmd.id == CMD_SIMULATOR_STOP: SimulatorLogger.info("SIMULATOR_STOP cmd received...") self.simulator.simulatorStatus = STOPPED self.simulator.machine.cms = {} self.simulator.machine.IPcms = {} simAnswer.id = ANS_OK elif cmd.id == CMD_SIMULATOR_EXIT: SimulatorLogger.info("SIMULATOR_EXIT cmd received...") self.simulator.simulatorStatus = STOPPED self.simulator.doLoop=False self.simulator.signal( simMessage('exit',mac='00:00:00:00:00:00') ) simAnswer.id = ANS_OK elif cmd.id == CMD_SIMULATOR_FLUSHCMS: SimulatorLogger.info("SIMULATOR_FLUSHCMS cmd received...") self.simulator.machine.cms = {} self.simulator.machine.IPcms = {} simAnswer.id = ANS_OK elif cmd.id == CMD_SIMULATOR_FLUSHCMTS: SimulatorLogger.info("SIMULATOR_FLUSHCMTS cmd received...") self.simulator.machine.IPcmts = {} self.simulator.machine.cmts = {} simAnswer.id = ANS_OK elif cmd.id == CMD_SIMULATOR_GETCMIP: """ return for a given macaddres (specified in cmMac), it's IP in ipCM""" SimulatorLogger.info("SIMULATOR_GETCMIP cmd received...") item = cmd.devices._values[0] if ( self.simulator.machine.cms.has_key(item.cmMac) ): dev = self.simulator.machine.cms[ item.cmMac ] simAnswer.ipCM = dev.ip SimulatorLogger.info("SIMULATOR_GETCMIP IP assigned=%s",simAnswer.ipCM) simAnswer.id = ANS_OK else: SimulatorLogger.error("SIMULATOR_GETCMIP did not find device %s",item.cmMac) simAnswer.id = ANS_ERR elif cmd.id == CMD_SIMULATOR_SHOWCMS: """ return a list of macaddres registered into simulator """ SimulatorLogger.info("SIMULATOR_SHOWCMS cmd received...") for mac in self.simulator.machine.cms.iterkeys(): simAnswer.macCms.append(mac) simAnswer.id = ANS_SHOWCMS elif cmd.id == CMD_SIMULATOR_SHOWCMS_DETAILED: SimulatorLogger.debug("SIMULATOR_SHOWCMS_DETAILED cmd received...") for mac,dev in self.simulator.machine.cms.iteritems(): item = simAnswer.cablemodems.add() item.cmMac = mac item.cmtsMac = '' item.ip = dev.ip if dev.cmTimers.has_key('dhcp_discover'): item.timer_dhcp_discover = dev.cmTimers['dhcp_discover'] if dev.cmTimers.has_key('dhcp_offer'): item.timer_dhcp_offer = dev.cmTimers['dhcp_offer'] if dev.cmTimers.has_key('dhcp_request'): item.timer_dhcp_request = dev.cmTimers['dhcp_request'] if dev.cmTimers.has_key('dhcp_ack'): item.timer_dhcp_ack = dev.cmTimers['dhcp_ack'] simAnswer.id = ANS_SHOWCMS_DETAILED elif cmd.id == CMD_SIMULATOR_GETAMOUNTCMS_WITH_IP: SimulatorLogger.debug("SIMULATOR_GETAMOUNTCMS_WITH_IP cmd received...") cmsWithIP = 0 for mac,dev in self.simulator.machine.cms.iteritems(): if (dev.ip != "0.0.0.0"): cmsWithIP+=1 simAnswer.ipCM = str(cmsWithIP) simAnswer.id = ANS_OK elif cmd.id == CMD_SIMULATOR_GETAMOUNTCMS: SimulatorLogger.info("SIMULATOR_GETAMOUNTCMS cmd received...") simAnswer.ipCM = str(len(self.simulator.machine.cms)) simAnswer.id = ANS_OK elif cmd.id == CMD_SIMULATOR_SENDMSG: SimulatorLogger.debug("SIMULATOR_SENDMSG cmd received...") for index in cmd.devices._values: SimulatorLogger.debug("%s :: signal=%s", index.cmMac,index.msg) self.simulator.signal( simMessage(index.msg,mac=index.cmMac) ) simAnswer.id = ANS_OK elif cmd.id == CMD_SIMULATOR_GETSTATUS_TFTP: cmCounter = 0 for mac,dev in self.simulator.machine.cms.iteritems(): if (dev.tftp.bootfileSize > 0): cmCounter+=1 self.answer.msg = cmCounter self.answer.setCmdId(ANS_GETSTATUS_TFTP) elif cmd.id == CMD_SIMULATOR_GETSTATUS: # XXX not implemented... self.answer.setCmdId(ANS_GETSTATUS) elif cmd.id == CMD_SIMULATOR_SHOWCMTS: SimulatorLogger.info("SIMULATOR_SHOWCMTS cmd received...") for mac in self.simulator.machine.cmts.iterkeys(): simAnswer.macCmts.append(mac) simAnswer.id = ANS_SHOWCMTS elif cmd.id == CMD_SIMULATOR_ADDCMTS: for index in cmd.CMTSDevices._values: newcmts = cmts.CMTS(CMTSName=index.cmtsName,macaddress=index.cmtsMac,ip=index.cmtsIP) cmtsOperation = self.simulator.add_cmts( newcmts ) SimulatorLogger.info("SIMULATOR_ADDCMTS cmd received (%s:%s)=%s",index.cmtsMac,index.cmtsIP,cmtsOperation) if ( cmtsOperation and index.helperAddressIP ): # turn on CMTS ( by default CMTS will be on ) self.simulator.signal( simMessage("power_on",mac=index.cmtsMac) ) SimulatorLogger.info("SIMULATOR_ADDCMTS setting Helper Address Parameters (%s:%s)",index.helperAddressIP,index.helperAddressMAC) opResult = newcmts.setDhcpServer(index.helperAddressIP,index.helperAddressMAC) else: opResult = ANS_ERR SimulatorLogger.error("Invalid CM Mac(%s). CPE %s could not be registered",index.cmMac,index.cmMac) simAnswer.id = opResult elif cmd.id == CMD_SIMULATOR_ADDCPE: SimulatorLogger.info("Cmd received:ADD_CPE") for index in cmd.devices._values: if self.simulator.machine.cms.has_key(index.cmMac): # if the CM associated to CPE exists cm_cpe = self.simulator.machine.cms[ index.cmMac ] # CPE mac travels on cmtsMac field opResult = self.simulator.add_cpe( CPE(index.cmtsMac, cm=cm_cpe) ) SimulatorLogger.info("CPE %s added behing CM %s",index.cmtsMac,index.cmMac) else: SimulatorLogger.error("Invalid CM Mac(%s). CPE %s could not be registered",index.cmMac,index.cmtsMac) opResult = ANS_ERR simAnswer.id = opResult elif cmd.id == CMD_SIMULATOR_GETAMOUNTCPES_WITH_IP: SimulatorLogger.debug("SIMULATOR_GETAMOUNTCPES_WITH_IP cmd received...") cmsWithIP = 0 for mac,dev in self.simulator.machine.cpes.iteritems(): if (dev.ip != "0.0.0.0"): cmsWithIP+=1 simAnswer.ipCM = str(cmsWithIP) simAnswer.id = ANS_OK elif cmd.id == CMD_SIMULATOR_ADDCM: SimulatorLogger.info("Cmd received:ADDCM") for index in cmd.devices._values: if self.simulator.machine.cmts.has_key(index.cmtsMac): localcmts = self.simulator.machine.cmts[ index.cmtsMac ] opResult = self.simulator.add_cm( CM( index.cmMac, cmts=localcmts) ) else: pass #self.answer.answer.append( (data.maclist[index][0], False) ) #self.answer.msg="Did not find CMTS for association with CM" #self.answer.setCmdId(ANS_ERR) simAnswer.id = ANS_ADDCM else: SimulatorLogger.error("Unknown cmd received (%d)",data.cmdid) #clientsock.sendall( simAnswer.SerializeToString() ) answerSize = len(simAnswer.SerializeToString()) res = struct.pack('<i', answerSize) #SimulatorLogger.debug("client must received %d bytes", answerSize) clientsock.send(res) totalsent = 0 data = simAnswer.SerializeToString() while totalsent < answerSize: sent = clientsock.send(data[totalsent:]) if sent == 0: raise RuntimeError, "socket connection broken" totalsent = totalsent + sent