예제 #1
0
 def __init__(self):
     self.keyChain = KeyChain()
     self.isDone = False
     self.ofmsg = OFMSG()
     self.outstanding = dict()  #a dictionary to keep track of outstanding Interests and retransmissions.
     #self.face = Face("127.0.0.1")
     self.face = Face()
     self.nodeid = OSCommand.getnodeid()
 def __init__(self):
     self.keyChain = KeyChain()
     self.isDone = False
     self.ofmsg = OFMSG()
     self.nodeid = OSCommand.getnodeid()
     self.face = Face()
     self.featurereq = FeatureReq()
     self.helloreq_name_list = []
     self.new_CtrlInfo_data = "---Initial CtrlInfo data---"  # used to get new ctrlinfo data and send to nodes.
     self.CtrlInfo_data = ""  # used to record used ctrlinfo data
예제 #3
0
    def onInterest_Feature(self, mainPrefix, interest, transport,
                           registeredPrefixId):
        print("++++++++ Received <<<FeatureReq>>> interest ++++++++ \n")
        feature_face = OSCommand.getface()
        feature_FIB = OSCommand.getFIB()
        nodeid = bytes(self.nodeid, 'utf-8')

        feature_data = nodeid + b'----' + feature_face + b'----' + feature_FIB

        data = OFMSG().create_feature_res_data(interest, feature_data)

        transport.send(data.wireEncode().toBuffer())
        print("++++++++ Sent <<<FeatureRes>>> Data ++++++++ \n")
        self.isDone = True  #
예제 #4
0
class FaceModMsg(object):
    '''FlowRemoved message is an interest send once, no need response '''

    def __init__(self):
        self.keyChain = KeyChain()
        self.ofmsg = OFMSG()
        self.face = Face()

    def run(self,facemod_suffix):
        try:
            self._sendFaceModInterest(facemod_suffix)
            n=0
            while n<200:
                self.face.processEvents()
                time.sleep(0.01)
                n+=1
        except RuntimeError as e:
            print("ERROR: %s" %  e)
        return True

    def _sendFaceModInterest(self,facemod_suffix):
        interest = self.ofmsg.create_facemod_msg_interest(facemod_suffix)
        uri = interest.getName().toUri()
        self.face.expressInterest(interest, self._onData, self._onTimeout)
        print("--------Sent <<<FaceMod>>> Msg for \n %s" % uri)

    def _onData(self, interest, data):
        pass
    def _onTimeout(self, interest):
        pass
예제 #5
0
class PacketIn(object):
    '''PacketIn message is an interest for inquiring unknown prefix '''

    def __init__(self):
        self.keyChain = KeyChain()
        self.isDone = False
        self.ofmsg = OFMSG()
        self.outstanding = dict()  #a dictionary to keep track of outstanding Interests and retransmissions.
        #self.face = Face("127.0.0.1")
        self.face = Face()
        self.nodeid = OSCommand.getnodeid()

    def run(self, unknown_prefix="/abcd/dfgh/tcd"):
        try:
            self._sendPacketinInterest(unknown_prefix)

            while not self.isDone:
                self.face.processEvents()
                time.sleep(0.01)
        except RuntimeError as e:
            print("ERROR: %s" %  e)
        return self.isDone


    def _sendPacketinInterest(self,unknown_prefix):
        interest = self.ofmsg.create_packetin_msg_interest(unknown_prefix, self.nodeid)
        uri = interest.getName().toUri()

        if uri not in self.outstanding:
            self.outstanding[uri] = 1
        self.face.expressInterest(interest, self._onData, self._onTimeout)
        print("######### Sent <<<PacketIn>>> Interest #########\n {0} \n".format(uri))



    def _onData(self, interest, data):
        payload = data.getContent()
        name = data.getName()
        print("Received <<<<FlowMod data>>>>from Controller ")

        self.nodeid = OSCommand.getnodeid()

        # add this item to flow table.
        FlowModDataList = NdnFlowTable.parse_FlowMod_Data(payload)
        # print(FlowModDataList)
        NdnFlowTable.updatendnflowtable(FlowModDataList,self.nodeid)
        print(NdnFlowTable)


        del self.outstanding[name.toUri()]
        self.isDone = True


    def _onTimeout(self, interest):
        name = interest.getName()
        uri = name.toUri()

        print("TIMEOUT #%d: %s" % (self.outstanding[uri], uri))
        self.outstanding[uri] += 1
        self.isDone = True
예제 #6
0
class HelloReq(object):
    '''Hello '''
    def __init__(self):
        self.keyChain = KeyChain()
        self.isDone = False
        self.ofmsg = OFMSG()
        self.outstanding = dict(
        )  #a dictionary to keep track of outstanding Interests and retransmissions.
        #self.face = Face("127.0.0.1")
        self.face = Face()
        self.nodeid = OSCommand.getnodeid()

    def run(self, hello_version_number):
        try:
            self._sendHelloInterest(hello_version_number)

            while not self.isDone:
                self.face.processEvents()
                time.sleep(0.01)
        except RuntimeError as e:
            print("ERROR: %s" % e)
        return self.isDone

    def _sendHelloInterest(self, hello_version_number=100001):
        interest = self.ofmsg.create_hello_req_interest(
            self.nodeid, hello_version_number)
        uri = interest.getName().toUri()

        if uri not in self.outstanding:
            self.outstanding[uri] = 1
        self.face.expressInterest(interest, self._onData, self._onTimeout)

        print("--------Sent <<<Helloreq>>> Interest -------- \n {0} \n".format(
            uri))

    def _onData(self, interest, data):
        payload = data.getContent()
        name = data.getName()
        print("--------Received <<<HelloRes>>> Data -------- \n {0} \n".format(
            payload.toRawStr()))
        del self.outstanding[name.toUri()]
        self.isDone = True

    def _onTimeout(self, interest):
        name = interest.getName()
        uri = name.toUri()

        print("TIMEOUT #%d: %s" % (self.outstanding[uri], uri))
        self.outstanding[uri] += 1

        if self.outstanding[uri] <= 3:
            self._sendHelloInterest()
        else:
            self.isDone = True
예제 #7
0
class ErrorMsg(object):
    '''Error report message is an interest for report error to controller'''
    def __init__(self):
        self.keyChain = KeyChain()
        self.isDone = False
        self.ofmsg = OFMSG()
        self.outstanding = dict(
        )  # a dictionary to keep track of outstanding Interests and retransmissions.
        # self.face = Face("127.0.0.1")
        self.face = Face()
        self.nodeid = OSCommand.getnodeid()

    def run(self, unknown_prefix="h1--0x0004--0x0000--faceid255-down"):
        try:
            self._sendErrorMsgInterest(unknown_prefix)

            while not self.isDone:
                self.face.processEvents()
                time.sleep(0.01)
        except RuntimeError as e:
            print("ERROR: %s" % e)
        return self.isDone

    def _sendErrorMsgInterest(self, error_prefix):
        interest = self.ofmsg.create_error_msg_interest(error_prefix)
        uri = interest.getName().toUri()

        if uri not in self.outstanding:
            self.outstanding[uri] = 1
        self.face.expressInterest(interest, self._onData, self._onTimeout)
        print("--------Sent <<<Error Msg>>> Interest for \n %s" % uri)

    def _onData(self, interest, data):
        payload = data.getContent()
        name = data.getName()
        print("Received <<<<Error Msg ACK>>>>from Controller ")

        del self.outstanding[name.toUri()]
        self.isDone = True

    def _onTimeout(self, interest):
        name = interest.getName()
        uri = name.toUri()

        print("TIMEOUT #%d: %s" % (self.outstanding[uri], uri))
        self.outstanding[uri] += 1

        if self.outstanding[uri] <= 3:
            self.run()
        else:
            self.isDone = True
예제 #8
0
    def __init__(self):
        self.ofmsg = OFMSG()

        self.hello_version_number = 200001

        # record the old record, which is used to compare to the new gotten ones
        self.channels_status_record = ""
        self.faces_status_record = ""
        self.fib_status_record = ""
        self.rib_status_record = ""

        # count the flapping time
        self.channels_flapping_time = 0
        self.faces_flapping_time = 0
        self.fib_flapping_time = 0
        self.rib_flapping_time = 0
class Controller_Listener_CtrlInfo(object):
    def __init__(self):
        self.keyChain = KeyChain()
        self.isDone = False
        self.ofmsg = OFMSG()
        self.nodeid = OSCommand.getnodeid()
        self.face = Face()
        self.featurereq = FeatureReq()
        self.helloreq_name_list = []
        self.new_CtrlInfo_data = "---Initial CtrlInfo data---"  # used to get new ctrlinfo data and send to nodes.
        self.CtrlInfo_data = ""  # used to record used ctrlinfo data

    def ctrl_info_run(self):

        ControllerPrefixString = '/ndn/ie/tcd/controller01/ofndn/--/n1.0/36/0/0/'
        ControllerPrefix = Name(ControllerPrefixString)
        self.face.setCommandSigningInfo(self.keyChain, \
                                        self.keyChain.getDefaultCertificateName())

        self.face.registerPrefix(ControllerPrefix, self.onInterest_CtrlInfo, self.onRegisterFailed)  # run prefix
        #
        # # filters:
        # CtrlInfo_msg_prefix = Name('/ndn/ie/tcd/controller01/ofndn/--/n1.0/36/0/0/')
        # self.face.setInterestFilter(CtrlInfo_msg_prefix, self.onInterest_CtrlInfo)  # for CtrlInfo msg

        # Run the event loop forever. Use a short sleep to
        # prevent the Producer from using 100% of the CPU.
        while not self.isDone:  # listen hello cannot stop
            self.face.processEvents()
            time.sleep(0.01)

    def onInterest_CtrlInfo(self, mainPrefix, interest, transport, registeredPrefixId):
        print("******** Received <<<CtrlInfoReq>>> Interest ******** \n {0} \n".format(interest.getName().toUri()))
        while (self.new_CtrlInfo_data == self.CtrlInfo_data):  # wait for new data.
            time.sleep(15)
        self.CtrlInfo_data = self.new_CtrlInfo_data
        data = self.ofmsg.create_ctrlinfo_res_data(interest, self.CtrlInfo_data)
        transport.send(data.wireEncode().toBuffer())
        print("******** Sent <<<New CtrlInfo Res>>> Data ******** \n")

    def onInterest_Mian(self, mainPrefix, interest, transport, registeredPrefixId):
        pass

    def onRegisterFailed(self, ControllerPrefix):
        print("Register failed for prefix", ControllerPrefix.toUri())
        self.isDone = True
예제 #10
0
 def __init__(self):
     self.keyChain = KeyChain()
     self.ofmsg = OFMSG()
     self.face = Face()
예제 #11
0
class Controller_Listener(object):
    def __init__(self):
        self.keyChain = KeyChain()
        self.isDone = False
        self.ofmsg = OFMSG()
        self.nodeid = OSCommand.getnodeid()
        self.face = Face()
        self.featurereq = FeatureReq()
        self.helloreq_name_list = []
        self.new_CtrlInfo_data = "---Initial CtrlInfo data---"  # used to get new ctrlinfo data and send to nodes.
        self.CtrlInfo_data = ""  # used to record used ctrlinfo data

    def run(self):

        ControllerPrefixString = '/ndn/ie/tcd/controller01/ofndn/'
        ControllerPrefix = Name(ControllerPrefixString)
        self.face.setCommandSigningInfo(self.keyChain, \
                                   self.keyChain.getDefaultCertificateName())

        self.face.registerPrefix(ControllerPrefix, self.onInterest_Mian,
                                 self.onRegisterFailed)  # run prefix

        #filters:
        # hello_msg_prefix = Name('/ndn/ie/tcd/controller01/ofndn/--/n1.0/0/0/0/')
        # self.face.setInterestFilter(hello_msg_prefix,self.onInterest_Hello)   #for HelloReq msg

        error_msg_prefix = Name(
            '/ndn/ie/tcd/controller01/ofndn/--/n1.0/1/0/0/')
        self.face.setInterestFilter(error_msg_prefix,
                                    self.onInterest_ErrorMsg)  # for Error msg

        packetin_msg_prefix = Name(
            '/ndn/ie/tcd/controller01/ofndn/--/n1.0/10/0/0/')
        self.face.setInterestFilter(
            packetin_msg_prefix, self.onInterest_PacketIn)  #for packetin msg

        FlowRemoved_msg_prefix = Name(
            '/ndn/ie/tcd/controller01/ofndn/--/n1.0/11/0/0/')
        self.face.setInterestFilter(
            FlowRemoved_msg_prefix,
            self.onInterest_FlowRemoved)  #for FlowRemoved msg

        # cannot be here, conflict with helloreq, since both of them occupy the 'listening channel' and will not release.
        # CtrlInfo_msg_prefix = Name('/ndn/ie/tcd/controller01/ofndn/--/n1.0/36/0/0/')
        # self.face.setInterestFilter(CtrlInfo_msg_prefix, self.onInterest_CtrlInfo)

        # Run the event loop forever. Use a short sleep to
        # prevent the Producer from using 100% of the CPU.
        while not self.isDone:  #listen hello cannot stop
            self.face.processEvents()
            time.sleep(0.01)

    def ctrl_info_run(self):

        ControllerPrefixString = '/ndn/ie/tcd/controller01/ofndn/'
        ControllerPrefix = Name(ControllerPrefixString)
        self.face.setCommandSigningInfo(self.keyChain, \
                                        self.keyChain.getDefaultCertificateName())

        self.face.registerPrefix(ControllerPrefix, self.onInterest_Mian,
                                 self.onRegisterFailed)  # run prefix

        # filters:
        CtrlInfo_msg_prefix = Name(
            '/ndn/ie/tcd/controller01/ofndn/--/n1.0/36/0/0/')
        self.face.setInterestFilter(
            CtrlInfo_msg_prefix, self.onInterest_CtrlInfo)  # for CtrlInfo msg
        # Run the event loop forever. Use a short sleep to
        # prevent the Producer from using 100% of the CPU.
        while not self.isDone:  # listen hello cannot stop
            self.face.processEvents()
            time.sleep(0.01)

    def onInterest_PacketIn(self, mainPrefix, interest, transport,
                            registeredPrefixId):
        print("######### Received <<<PacketIn>>> Interest #########\n {0} \n".
              format(interest.getName().toUri()))

        (node_id,
         unknown_prefix) = NdnFlowTable.parse_Packetin_Interest(interest)
        node_id = node_id.strip('/')

        # FlowModDataList: [ep(0),face(1),prefix(2),cookie(3),command(4),idle_timeout(5),
        # hard_timeout(6), priority(7),buffer_id(8),out_face(9),flag(10), action(11)]
        flowmod_data = self.create_PacketIn_Data(node_id, unknown_prefix)
        data = self.ofmsg.create_flowmod_data(interest, flowmod_data)
        transport.send(data.wireEncode().toBuffer())
        print('===== Send [ FlowMod Msg ] to {0}====='.format(node_id))

    def create_PacketIn_Data(self, node_id, unknown_prefix):
        # This function is just used for demonstration to send exact flowmod message.

        if node_id == 'h1':
            data_tring = '*---*---{}---None---0x0000---3600---36000---1---None---267---0x0001---0x0000'.format(
                unknown_prefix)
        elif node_id == 'h2':
            data_tring = '*---*---{}---None---0x0000---3600---36000---1---None---271---0x0001---0x0000'.format(
                unknown_prefix)
        elif node_id == 'h3':
            data_tring = '*---*---{}---None---0x0000---3600---36000---1---None---269---0x0001---0x0000'.format(
                unknown_prefix)
        elif node_id == 'h4':
            data_tring = '*---*---{}---None---0x0000---3600---36000---1---None---271---0x0001---0x0000'.format(
                unknown_prefix)
        elif node_id == 'h5':
            data_tring = '*---*---{}---None---0x0000---3600---36000---1---None---276---0x0001---0x0000'.format(
                unknown_prefix)
        elif node_id == 'h6':
            data_tring = '*---*---{}---None---0x0000---3600---36000---1---None---261---0x0001---0x0000'.format(
                unknown_prefix)
        elif node_id == 'h7':
            data_tring = '*---*---{}---None---0x0000---3600---36000---1---None---260---0x0001---0x0000'.format(
                unknown_prefix)
        elif node_id == 'h8':
            data_tring = '*---*---{}---None---0x0000---3600---36000---1---None---260---0x0001---0x0000'.format(
                unknown_prefix)
        elif node_id == 'h9':
            data_tring = '*---*---{}---None---0x0000---3600---36000---1---None---261---0x0001---0x0000'.format(
                unknown_prefix)
        elif node_id == 'h10':
            data_tring = '*---*---{}---None---0x0000---3600---36000---1---None---255---0x0001---0x0000'.format(
                unknown_prefix)
        elif node_id == 'a':
            data_tring = '*---*---{}---None---0x0000---3600---36000---1---None---255---0x0001---0x0000'.format(
                unknown_prefix)
        elif node_id == 'b':
            data_tring = '*---*---{}---None---0x0000---3600---36000---1---None---255---0x0001---0x0000'.format(
                unknown_prefix)
        elif node_id == 'c':
            data_tring = '*---*---{}---None---0x0000---3600---36000---1---None---255---0x0001---0x0000'.format(
                unknown_prefix)
        elif node_id == 'd':
            data_tring = '*---*---{}---None---0x0000---3600---36000---1---None---255---0x0001---0x0000'.format(
                unknown_prefix)
        elif node_id == 'e':
            data_tring = '*---*---{}---None---0x0000---3600---36000---1---None---255---0x0001---0x0000'.format(
                unknown_prefix)
        elif node_id == 'f':
            data_tring = '*---*---{}---None---0x0000---3600---36000---1---None---255---0x0001---0x0000'.format(
                unknown_prefix)
        else:
            data_tring = '*---*---{}---None---0x0000---3600---36000---1---None---255---0x0001---0x0000'.format(
                unknown_prefix)
        return data_tring

    def onInterest_FlowRemoved(self, mainPrefix, interest, transport,
                               registeredPrefixId):

        print("------Received: <<<FlowRemoved>>> Msg ------")  # for test

    def onInterest_CtrlInfo(self, mainPrefix, interest, transport,
                            registeredPrefixId):
        print(
            "******** Received <<<CtrlInfoReq>>> Interest ******** \n {0} \n".
            format(interest.getName().toUri()))
        while (self.new_CtrlInfo_data == self.CtrlInfo_data
               ):  # wait for new data.
            time.sleep(5)
        self.CtrlInfo_data = self.new_CtrlInfo_data
        data = self.ofmsg.create_ctrlinfo_res_data(interest,
                                                   self.CtrlInfo_data)
        transport.send(data.wireEncode().toBuffer())
        print("******** Sent <<<New CtrlInfo Res>>> Data ******** \n")

    def onInterest_Hello(self, mainPrefix, interest, transport,
                         registeredPrefixId):
        print("\n --------Received <<<HelloReq>>> Interest --------\n {0} \n".
              format(interest.getName().toUri()))  # for test
        print("--------Sent <<<HelloRes>>> Data -------- \n")
        hello_data = '[This is hello response data]'
        data = self.ofmsg.create_hello_res_data(interest, hello_data)
        transport.send(data.wireEncode().toBuffer())
        NodePrefixTable.updatenodeprefixtable(
            interest)  #to add NPT and fetch feature

    def onInterest_ErrorMsg(self, mainPrefix, interest, transport,
                            registeredPrefixId):
        print("--------received <<<Error Msg>>> interest:\n" +
              interest.getName().toUri())  # for test
        errormsg_data = 'Error Report Acknowledge'
        data = self.ofmsg.create_errorAck_data(interest, errormsg_data)
        transport.send(data.wireEncode().toBuffer())
        print("--------sent <<<Error Msg ACK>>>---------")

        #parse the errorMsg interest to get error information.

    def onInterest_Mian(self, mainPrefix, interest, transport,
                        registeredPrefixId):
        pass

    def onRegisterFailed(self, ControllerPrefix):
        print("Register failed for prefix", ControllerPrefix.toUri())
        self.isDone = True
예제 #12
0
class FeatureReq(object):
    def __init__(self):
        self.keyChain = KeyChain()
        self.isDone = False
        self.ofmsg = OFMSG()
        self.outstanding = dict(
        )  # a dictionary to keep track of outstanding Interests and retransmissions.
        self.face = Face()
        self.nodeid = OSCommand.getnodeid()

    def run(self, original_hello_req_interest):
        try:
            self._sendFeatureInterest(original_hello_req_interest)

            while not self.isDone:
                self.face.processEvents()
                time.sleep(0.01)

        except RuntimeError as e:
            print("ERROR: %s" % e)

    def _sendFeatureInterest(self, feature_req_interest_name):
        interest = self.ofmsg.create_feature_req_interest(
            feature_req_interest_name)
        uri = interest.getName().toUri()

        if uri not in self.outstanding:
            self.outstanding[uri] = 1
        self.face.expressInterest(interest, self._onFeatureData,
                                  self._onTimeout)

        print("++++++++ Sent <<<FeatureReq>>> Interest ++++++++ \n {0} \n".
              format(uri))

    def _onFeatureData(self, interest, data):

        # originalfeaturedata = data.getContent.toRawStr()
        # #originalfeaturedata.toRawStr()
        # name = data.getName()
        # self.featurerdata.monitoring_function(originalfeaturedata)
        # print("Received new featureData ----updating FIB & Face table")
        # #print("Received data: ", payload.toRawStr())
        #
        # del self.outstanding[name.toUri()]
        # self.isDone = True

        payload = data.getContent()
        contentx = payload.toRawStr()
        name = data.getName()
        print("++++++++ Received <<<FeatureRes>>> Data ++++++++ \n")
        FeatureDate.run(contentx)

        del self.outstanding[name.toUri()]
        self.isDone = True
        print('========FIB Database has been updated=======')
        print('========Face Database has been updated=======\n')

    def _onTimeout(self, interest):
        name = interest.getName()
        uri = name.toUri()

        print("TIMEOUT #%d: %s" % (self.outstanding[uri], uri))
        self.outstanding[uri] += 1

        if self.outstanding[uri] <= 3:
            self._sendFeatureInterest(interest)
        else:
            self.isDone = True
예제 #13
0
class CtrlInfoReq(object):
    '''Request Control information '''

    def __init__(self):
        self.keyChain = KeyChain()
        self.isDone = False
        self.ofmsg = OFMSG()
        self.outstanding = dict()  # a dictionary to keep track of outstanding Interests and retransmissions.
        # self.face = Face("127.0.0.1")
        self.face = Face()
        self.nodeid = OSCommand.getnodeid()
        self.send_ctrlinfo_interest = True
        self.ctrlinfo_operation = CtrlInfo_Operation()

    def run(self):
        ctrlinfo_version_number = 100001
        while True:
            if (self.send_ctrlinfo_interest):
                try:
                    self._sendCtrlInfoInterest(ctrlinfo_version_number)
                    ctrlinfo_version_number += 1
                    self.send_ctrlinfo_interest = False  # repeat to send this interest.
                    self.isDone = False

                    while not self.isDone:
                        self.face.processEvents()
                        time.sleep(0.01)
                except RuntimeError as e:
                    print("ERROR: %s" % e)

            time.sleep(0.1)


    def _sendCtrlInfoInterest(self, ctrlinfo_version_number=100001):
        interest = self.ofmsg.create_ctrlinfo_req_interest(self.nodeid, ctrlinfo_version_number)
        uri = interest.getName().toUri()

        if uri not in self.outstanding:
            self.outstanding[uri] = 1
        self.face.expressInterest(interest, self._onData, self._onTimeout)
        print("******** Sent <<<CtrlInfoReq>>> Interest ******** \n {0} \n".format(uri))

    def _onData(self, interest, data):
        payload = data.getContent()
        name = data.getName()
        print("Received New <<<Control Information>>>------- \n", payload.toRawStr())
        del self.outstanding[name.toUri()]
        self.isDone = True
        self.send_ctrlinfo_interest = True
        self.ctrlinfo_operation.run(payload)



    def _onTimeout(self, interest):
        name = interest.getName()
        uri = name.toUri()

        print("TIMEOUT #%d: %s" % (self.outstanding[uri], uri))
        self.outstanding[uri] += 1

        if self.outstanding[uri] <= 3:
            self._sendCtrlInfoInterest()
        else:
            self.isDone = True