@Override
    public void emit(OutboundPacket packet) {
        DeviceId devId = packet.sendThrough();
        String scheme = devId.toString().split(":")[0];

        if (!scheme.equals(this.id().scheme())) {
            throw new IllegalArgumentException(
                    "Don't know how to handle Device with scheme " + scheme);
        }

        Dpid dpid = Dpid.dpid(devId.uri());
        OpenFlowSwitch sw = controller.getSwitch(dpid);
        if (sw == null) {
            log.warn("Device {} isn't available?", devId);
            return;
        }

        OFPort inPort;
        if (packet.inPort() != null) {
            inPort = portDesc(packet.inPort()).getPortNo();
        } else {
            inPort = OFPort.CONTROLLER;
        }

        //Ethernet eth = new Ethernet();
        //eth.deserialize(packet.data().array(), 0, packet.data().array().length);
        OFPortDesc p = null;
        for (Instruction inst : packet.treatment().allInstructions()) {
            if (inst.type().equals(Instruction.Type.OUTPUT)) {
                p = portDesc(((OutputInstruction) inst).port());

                OFPacketOut po = packetOut(sw, packet.data().array(), p.getPortNo(), inPort);
                sw.sendMsg(po);
            }
        }

    }
@Test
    public void emit() {

        MacAddress mac1 = MacAddress.of("00:00:00:11:00:01");
        MacAddress mac2 = MacAddress.of("00:00:00:22:00:02");

        ARP arp = new ARP();
        arp.setSenderProtocolAddress(ANY)
        .setSenderHardwareAddress(mac1.getBytes())
        .setTargetHardwareAddress(mac2.getBytes())
        .setTargetProtocolAddress(ANY)
        .setHardwareType((short) 0)
        .setProtocolType((short) 0)
        .setHardwareAddressLength((byte) 6)
        .setProtocolAddressLength((byte) 4)
        .setOpCode((byte) 0);

        Ethernet eth = new Ethernet();
        eth.setVlanID(VLANID)
        .setEtherType(Ethernet.TYPE_ARP)
        .setSourceMACAddress("00:00:00:11:00:01")
        .setDestinationMACAddress("00:00:00:22:00:02")
        .setPayload(arp);

        //the should-be working setup.
        OutboundPacket passPkt = outPacket(DID, TR, eth);
        sw.setRole(RoleState.MASTER);
        provider.emit(passPkt);
        assertEquals("invalid switch", sw, controller.current);
        assertEquals("message not sent", PLIST.size(), sw.sent.size());
        sw.sent.clear();

        //Send with different IN_PORT
        OutboundPacket inPortPkt = outPacket(DID, TR_ALL, eth, IN_PORT);
        sw.setRole(RoleState.MASTER);
        provider.emit(inPortPkt);
        assertEquals("invalid switch", sw, controller.current);
        assertEquals("message not sent", PLIST_ALL.size(), sw.sent.size());
        OFMessage ofMessage = sw.sent.get(0);
        assertEquals("Wrong OF message type", OFType.PACKET_OUT, ofMessage.getType());
        OFPacketOut packetOut = (OFPacketOut) ofMessage;
        assertEquals("Wrong in port", OFPort.of(IN_PORT_PN), packetOut.getInPort());
        assertEquals("Unexpected number of actions", 1, packetOut.getActions().size());
        OFAction ofAction = packetOut.getActions().get(0);
        assertEquals("Packet out action should be type output", OFActionType.OUTPUT, ofAction.getType());
        OFActionOutput ofActionOutput = (OFActionOutput) ofAction;
        assertEquals("Output should be ALL", OFPort.ALL, ofActionOutput.getPort());
        sw.sent.clear();

        //wrong Role
        //sw.setRole(RoleState.SLAVE);
        //provider.emit(passPkt);
        //assertEquals("invalid switch", sw, controller.current);
        //assertEquals("message sent incorrectly", 0, sw.sent.size());

        //sw.setRole(RoleState.MASTER);

        //missing switch
        OutboundPacket swFailPkt = outPacket(DID_MISSING, TR, eth);
        provider.emit(swFailPkt);
        assertNull("invalid switch", controller.current);
        assertEquals("message sent incorrectly", 0, sw.sent.size());

        //to missing port
        //OutboundPacket portFailPkt = outPacket(DID, TR_MISSING, eth);
        //provider.emit(portFailPkt);
        //assertEquals("extra message sent", 1, sw.sent.size());

    }