def host_bind_handler(self, data):
        """ Handler for host_bind_event """
        assert(data is not None)
        try:
            bind_data   = data.__dict__
            dladdr      = pkt_utils.mac_to_str(bind_data['dladdr'])
            host_ipaddr = nxw_utils.convert_ipv4_to_str(bind_data['nwaddr'])

            # Check reason value
            reason     = int(bind_data['reason'])
            if not reason in self.__reasons:
                LOG.error("Got host_leave event with unsupported reason value")
                return CONTINUE
            reason_str = self.__reasons[reason]
            LOG.info("Received host_bind_ev with reason '%s'" % reason_str)

            # XXX FIXME: Insert mapping for values <--> reason
            if reason > 7:
                if dladdr not in self.hosts:
                    LOG.debug("Ignoring Received host_leave_ev for an host" + \
                              " not present in DB")
                    return CONTINUE
                else:
                    ret = self.__host_leave(dladdr)
                    if not ret:
                        return CONTINUE

            elif (reason < 3 or reason == 5) and (bind_data['nwaddr'] == 0):
                LOG.debug("Ignoring host_bind_ev without IPaddr info")
                return CONTINUE
            elif (reason > 2) and (reason != 5):
                LOG.error("Unsupported reason for host_bind_ev: '%s'" % \
                           reason_str)
                return CONTINUE

            LOG.info("Received host_bind_ev with the following data: %s" % \
                      str(bind_data))

            # Check for presence of the host in stored (internal) hosts
            if dladdr in self.hosts:
                if self.hosts[dladdr].ip_addr is None:
                    LOG.debug("Got host_bind_ev for an host not posted yet")
                    # Post host
                    payload = {"ip_addr"    : host_ipaddr,
                               "mac"        : dladdr,
                               "peer_dpid"  : self.hosts[dladdr].rem_dpid,
                               "peer_portno": self.hosts[dladdr].rem_port}
                    req = requests.post(url=self.url + "pckt_host",
                                        headers=self.hs,
                                        data=json.dumps(payload))
                    LOG.debug("URL=%s, response(code=%d, content=%s)",
                              req.url, req.status_code, str(req.content))
                else:
                    LOG.debug("Got host_bind_ev for an host already posted")

            else:
                LOG.debug("Got host_bind_ev for an host not authenticated yet")

        except Exception, err:
            LOG.error("Got error in host_bind_handler (%s)" % str(err))
    def host_auth_handler(self, data):
        """ Handler for host_auth_event """
        assert(data is not None)
        try:
            auth_data = data.__dict__
            LOG.info("Received host_auth_ev with the following data: %s" %
                      str(auth_data))
            dladdr      = pkt_utils.mac_to_str(auth_data['dladdr'])
            host_ipaddr = nxw_utils.convert_ipv4_to_str(auth_data['nwaddr'])
            try:
                # connect and open transaction
                self.db_conn.open_transaction()
                if auth_data['nwaddr'] == 0:
                    LOG.debug("Received auth_event without IP address...")
                    mac_addresses = self.db_conn.port_get_macs()
                    if dladdr in mac_addresses:
                        LOG.debug("Ignoring received auth_ev for OF switch...")
                        return CONTINUE

                    else:
                        # Since Datapath_join event for an OF switch with
                        # dladdr could be caught later, we need to store info
                        self.hosts[dladdr]          = nxw_utils.Host(dladdr)
                        self.hosts[dladdr].rem_dpid = auth_data['datapath_id']
                        self.hosts[dladdr].rem_port = auth_data['port']
                        return CONTINUE

                if dladdr in self.auth_hosts:
                    LOG.debug("Ignoring auth_event (more notifications for" + \
                              " multiple inter-switch links)")
                    return CONTINUE
                self.auth_hosts.append(dladdr)

                host_idx = None
                try:
                    host_idx = self.db_conn.host_get_index(dladdr)
                except Exception, err:
                    LOG.debug("Any host with mac='%s' in DB" % str(dladdr))

                if host_idx is None:
                    LOG.debug("Adding new host with MAC addr '%s'" % \
                              str(dladdr))
                    try:
                        # Insert Host info into DB
                        self.__host_insert_db(dladdr,
                                              auth_data['datapath_id'],
                                              auth_data['port'],
                                              host_ipaddr)
                        LOG.info("Added host '%s' into DB" % str(dladdr))
                        if dladdr in self.auth_hosts:
                            self.auth_hosts.remove(dladdr)
                            log.debug("Removed '%s' from (local) auth hosts" %
                                      str(dladdr))
                    except nxw_utils.DBException as err:
                        LOG.error(str(err))
                        self.db_conn.rollback()
                    except Exception, err:
                        LOG.error("Cannot insert host info into DB ('%s')")
                else:
Example #3
0
    def __flow_create(self, dpid, info):
        try:
            nw_src = info["match"].get("nw_src")
            if nw_src:
                nw_src = nxw_utils.convert_ipv4_to_str(nw_src)

            nw_dst = info["match"].get("nw_dst")
            if nw_dst:
                nw_dst = nxw_utils.convert_ipv4_to_str(nw_dst)

            payload = {
                "dpid": dpid,
                "table_id": info["table_id"],
                "input_port": info["match"].get("in_port"),
                "idle_timeout": info["idle_timeout"],
                "hard_timeout": info["hard_timeout"],
                "priority": info["priority"],
                "cookie": info["cookie"],
                "datalink_type": info["match"].get("dl_type"),
                "datalink_vlan": info["match"].get("dl_vlan"),
                "datalink_vlan_priority": info["match"].get("dl_vlan_pcp"),
                "datalink_source": info["match"].get("dl_src"),
                "datalink_destination": info["match"].get("dl_dst"),
                "network_source": nw_src,
                "network_destination": nw_dst,
                "network_source_num_wild": info["match"].get("nw_src_n_wild"),
                "network_destination_num_wild": info["match"].get("nw_dst_n_wild"),
                "network_protocol": info["match"].get("nw_proto"),
                "transport_source": info["match"].get("tp_src"),
                "transport_destination": info["match"].get("tp_dst"),
            }

            r_ = requests.post(url=self._url + "pckt_flows", params=payload)
            if r_.text != "Operation completed":
                FFLOG.error("An error occurring during flow-post!")

        except Exception as e:
            FFLOG.error(str(e))
    def host_auth_handler(self, data):
        """ Handler for host_auth_event """
        assert(data is not None)
        try:
            auth_data = data.__dict__
            LOG.info("Received host_auth_ev with the following data: %s" %
                      str(auth_data))
            dladdr      = pkt_utils.mac_to_str(auth_data['dladdr'])
            host_ipaddr = nxw_utils.convert_ipv4_to_str(auth_data['nwaddr'])

            if dladdr in self.hosts:
                LOG.debug("Ignoring auth_event (more notifications for" + \
                          " multiple inter-switch links)")
                return CONTINUE

            r_ = dpid_from_host(auth_data['datapath_id'])
            if not r_: return CONTINUE

            self.hosts[dladdr]          = nxw_utils.Host(dladdr)
            self.hosts[dladdr].rem_dpid = r_
            self.hosts[dladdr].rem_port = auth_data['port']

            if auth_data['nwaddr'] == 0:
                LOG.debug("Received auth_event without IP address...")

            else:
                LOG.debug("Received auth_event with IP address info...")
                self.hosts[dladdr].ip_addr = host_ipaddr
                # post host
                payload = {"ip_addr"     : host_ipaddr,
                           "mac"         : dladdr,
                           "peer_dpid"   : r_,
                           "peer_portno" : auth_data['port']}
                req = requests.post(url=self.url + "pckt_host",
                                    headers=self.hs, data=json.dumps(payload))
                LOG.debug("URL=%s, response(code=%d, content=%s)",
                          req.url, req.status_code, str(req.content))

        except Exception, err:
            LOG.error("Got errors in host_auth_ev handler ('%s')" % str(err))
    def host_bind_handler(self, data):
        """ Handler for host_binf_event """
        assert(data is not None)
        try:
            bind_data   = data.__dict__
            dladdr      = pkt_utils.mac_to_str(bind_data['dladdr'])
            host_ipaddr = nxw_utils.convert_ipv4_to_str(bind_data['nwaddr'])

            # Check reason value
            reason     = int(bind_data['reason'])
            if not reason in self.__reasons:
                LOG.error("Got host_leave event with unsupported reason value")
                return CONTINUE
            reason_str = self.__reasons[reason]
            LOG.info("Received host_bind_ev with reason '%s'" % reason_str)

            # XXX FIXME: Insert mapping for values <--> reason
            if reason > 7:
                ret = self.__host_leave(dladdr)
                if not ret:
                    return CONTINUE
            elif (reason < 3 or reason == 5) and (bind_data['nwaddr'] == 0):
                LOG.debug("Ignoring host_bind_ev without IPaddr info")
                return CONTINUE
            elif (reason > 2) and (reason != 5):
                LOG.error("Unsupported reason for host_bind_ev: '%s'" % \
                           reason_str)
                return CONTINUE

            LOG.info("Received host_bind_ev with the following data: %s" % \
                      str(bind_data))

            try:
                try:
                    self.db_conn.open_transaction()
                    host_idx = None
                    host_idx = self.db_conn.host_get_index(dladdr)
                    LOG.debug("Host with mac_addr '%s' has index '%s'" % \
                               (str(dladdr), str(host_idx)))
                except Exception, err:
                    LOG.debug("Any host with mac='%s' in DB" % str(dladdr))

                if dladdr in self.hosts:
                    LOG.debug("Got host_bind_ev for an host not present " + \
                              "in DB yet")

                    # Insert Host info into DB
                    self.__host_insert_db(dladdr,
                                          self.hosts[dladdr].rem_dpid,
                                          self.hosts[dladdr].rem_port,
                                          host_ipaddr)
                    LOG.info("Added host '%s' into DB" % str(dladdr))
                    self.hosts.pop(dladdr)
                    # XXX FIXME Remove auth_hosts (maintain only self.hosts)
                    if dladdr in self.auth_hosts:
                        self.auth_hosts.remove(dladdr)

                else:
                    LOG.debug("Got host_bind_ev for an host already " + \
                              "present in DB")
                    LOG.debug("Updating host info in DB...")
                    self.db_conn.host_update(dladdr, host_ipaddr)
                    self.db_conn.commit()
                    LOG.info("Host info updated successfully")
            except nxw_utils.DBException as err:
                LOG.error(str(err))
                self.db_conn.rollback()
            finally:
                self.db_conn.close()


            # check ior-dispatcher on pce node
            if not self.ior_topo and not self.pce_topology_enable():
                LOG.error("Unable to contact ior-dispatcher on PCE node!")
                return CONTINUE

            nodes = [host_ipaddr]
            # Update flow-pce topology (hosts)
            LOG.debug("Hosts=%s", nodes)
            self.fpce.add_node_from_string(host_ipaddr)
            # update flow-pce topology (links between DPID and host)
            try:
                # connect and open transaction
                self.db_conn.open_transaction()
                # Host_insert
                dpid    = self.db_conn.host_get_dpid(dladdr)
                in_port = self.db_conn.host_get_inport(dladdr)
            except nxw_utils.DBException as err:
                LOG.error(str(err))
                self.db_conn.rollback()
            finally:
                self.db_conn.close()

            try:
                node = self.node_get_frompidport(dpid, in_port)
                nodes.append(node)
            except nxw_utils.DBException as err:
                LOG.error(str(err))
            except Exception, err:
                LOG.error(str(err))