def __init__(self, ryuapp):
     super(PacketLib, self).__init__()
     self.gateway = GatewayState(self.db.gateway_get_all())
     self.packet_arp = PacketARP(self, ryuapp)
     self.packet_ipv4 = PacketIPv4(self, ryuapp)
     self.packet_group = PacketGroup(self, ryuapp)
     self.flows = {}
 def __init__(self, ryuapp):
     super(PacketLib, self).__init__()
     self.gateway = GatewayState(self.db.gateway_get_all())
     self.packet_arp = PacketARP(self, ryuapp)
     self.packet_ipv4 = PacketIPv4(self, ryuapp)
     self.packet_group = PacketGroup(self, ryuapp)
     self.flows = {}
class PacketLib(db_base.Base):
    def __init__(self, ryuapp):
        super(PacketLib, self).__init__()
        self.gateway = GatewayState(self.db.gateway_get_all())
        self.packet_arp = PacketARP(self, ryuapp)
        self.packet_ipv4 = PacketIPv4(self, ryuapp)
        self.packet_group = PacketGroup(self, ryuapp)
        self.flows = {}

    def gateway_get(self, dpid):
        normal_dpid = dpid_lib.dpid_to_str(dpid)
        gateway = self.gateway.get(normal_dpid)
        if not gateway:
            gateway = self.db.gateway_get_by_datapath(normal_dpid)
            if gateway:
                self.gateway.add(gateway)
        return gateway

    def packet_in_handler(self, ev):
        """Check a packet-in message.

           Build and output a packet-out.
        """
        msg = ev.msg
        datapath = msg.datapath
        port = msg.match['in_port']
        gateway = self.gateway_get(datapath.id)

        if gateway is None or gateway.idc_id != CONF.idc_id:
            return

        pkt = packet.Packet(msg.data)
        pkt_ethernet = pkt.get_protocol(ethernet.ethernet)

        if not pkt_ethernet:
            LOG.info(_LI("drop non-ethernet packet"))
            return

        pkt_arp = pkt.get_protocol(arp.arp)
        pkt_ipv4 = pkt.get_protocol(ipv4.ipv4)

        if pkt_arp:
            self.packet_arp.run(msg, pkt_ethernet, pkt_arp, gateway)
        elif pkt_ipv4:
            pkt_tp = pkt.get_protocol(tcp.tcp) or \
                     pkt.get_protocol(udp.udp) or \
                     pkt.get_protocol(icmp.icmp)

            LOG.debug("packet-in msg %s %s %s from %s", datapath.id, pkt_ipv4,
                      pkt_tp, port)

            if pkt_tp and port:
                self.packet_ipv4.run(msg, pkt_ethernet, pkt_ipv4, pkt_tp,
                                     gateway)
        else:
            LOG.debug(_LI("drop non-arp and non-ip packet"))

    def switch_features_handler(self, ev):
        datapath = ev.msg.datapath
        ofproto = datapath.ofproto
        parser = datapath.ofproto_parser
        actions = [
            parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,
                                   ofproto.OFPCML_NO_BUFFER)
        ]
        self.packet_group.add_flow(datapath, actions=actions)
        #self.init_flow(datapath)

    def init_flow(self, dp):
        gateway = self.gateway_get(dp.id)

        if gateway is not None and gateway.idc_id == CONF.idc_id:
            self.packet_group.init_flow(dp, gateway)

    def group_delete(self, sid, did):
        src = self.db.server_get(sid)
        dst = self.db.server_get(did)

        src_gateway = self.gateway[src.host]
        dst_gateway = self.gateway[dst.host]

        if any((not src, not dst, not src_gateway, not dst_gateway)):
            LOG.warn("Instance could be not found.")
        else:
            self.packet_group.run(src, src_gateway, dst, dst_gateway)
class PacketLib(db_base.Base):

    def __init__(self, ryuapp):
        super(PacketLib, self).__init__()
        self.gateway = GatewayState(self.db.gateway_get_all())
        self.packet_arp = PacketARP(self, ryuapp)
        self.packet_ipv4 = PacketIPv4(self, ryuapp)
        self.packet_group = PacketGroup(self, ryuapp)
        self.flows = {}

    def gateway_get(self, dpid):
        normal_dpid = dpid_lib.dpid_to_str(dpid)
        gateway = self.gateway.get(normal_dpid)
        if not gateway:
            gateway = self.db.gateway_get_by_datapath(normal_dpid)
            if gateway:
                self.gateway.add(gateway)
        return gateway

    def packet_in_handler(self, ev):
        """Check a packet-in message.

           Build and output a packet-out.
        """
        msg = ev.msg
        datapath = msg.datapath
        port = msg.match['in_port']
        gateway = self.gateway_get(datapath.id)

        if gateway is None or gateway.idc_id != CONF.idc_id:
            return

        pkt = packet.Packet(msg.data)
        pkt_ethernet = pkt.get_protocol(ethernet.ethernet)

        if not pkt_ethernet:
            LOG.info(_LI("drop non-ethernet packet"))
            return

        pkt_arp = pkt.get_protocol(arp.arp)
        pkt_ipv4 = pkt.get_protocol(ipv4.ipv4)

        if pkt_arp:
            self.packet_arp.run(msg, pkt_ethernet, pkt_arp, gateway)
        elif pkt_ipv4:
            pkt_tp = pkt.get_protocol(tcp.tcp) or \
                     pkt.get_protocol(udp.udp) or \
                     pkt.get_protocol(icmp.icmp)

            LOG.debug("packet-in msg %s %s %s from %s", 
                      datapath.id, pkt_ipv4, pkt_tp, port)

            if pkt_tp and port:
                self.packet_ipv4.run(msg, pkt_ethernet, pkt_ipv4, pkt_tp, gateway)
        else:
            LOG.debug(_LI("drop non-arp and non-ip packet"))

    def switch_features_handler(self, ev):
        datapath = ev.msg.datapath
        ofproto = datapath.ofproto
        parser = datapath.ofproto_parser
        actions = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,
                                          ofproto.OFPCML_NO_BUFFER)]
        self.packet_group.add_flow(datapath, actions=actions)
        #self.init_flow(datapath)

    def init_flow(self, dp):
        gateway = self.gateway_get(dp.id)

        if gateway is not None and gateway.idc_id == CONF.idc_id:
            self.packet_group.init_flow(dp, gateway)

    def group_delete(self, sid, did):
        src = self.db.server_get(sid)
        dst = self.db.server_get(did)

        src_gateway = self.gateway[src.host]
        dst_gateway = self.gateway[dst.host]

        if any((not src, not dst, not src_gateway, not dst_gateway)):
            LOG.warn("Instance could be not found.")
        else:
            self.packet_group.run(src, src_gateway, dst, dst_gateway)