@Test
public void testDeviceAdded() {

	
	reset(topology);
	expect(topology.isAttachmentPointPort(DatapathId.of(1L), OFPort.of(1))).andReturn(true).anyTimes();
	expect(topology.isAttachmentPointPort(DatapathId.of(1L), OFPort.of(2))).andReturn(true).anyTimes();
	expect(topology.isAttachmentPointPort(DatapathId.of(2L), OFPort.of(1))).andReturn(true).anyTimes();
	expect(topology.isAttachmentPointPort(DatapathId.of(2L), OFPort.of(2))).andReturn(true).anyTimes();
	replay(topology);
	
	int[] cidr = new int[2];
	ACLRule rule1, rule2;
	IResultSet resultSet;
	Iterator<IResultSet> it;
	Map<String, Object> row;
	// rule1 indicates host(10.0.0.0/28) can not access TCP port 80 in host(10.0.0.254/32)
	rule1 = new ACLRule();
	rule1.setNw_src("10.0.0.0/28");
	cidr = IPAddressUtil.parseCIDR("10.0.0.0/28");
	rule1.setNw_src_prefix(cidr[0]);
	rule1.setNw_src_maskbits(cidr[1]);
	rule1.setNw_dst("10.0.0.254/32");
	cidr = IPAddressUtil.parseCIDR("10.0.0.254/32");
	rule1.setNw_dst_prefix(cidr[0]);
	rule1.setNw_dst_maskbits(cidr[1]);
	rule1.setNw_proto(6);
	rule1.setTp_dst(80);
	rule1.setAction(Action.DENY);
	assertEquals(acl.addRule(rule1), true);
	assertEquals(acl.getRules().size(), 1);
	
	// a new AP[dpid:00:00:00:00:00:00:00:01 port:1 ip:10.0.0.1] appears
	deviceManager.learnEntity(
			Ethernet.toLong(Ethernet.toMACAddress("00:00:00:00:00:01")),
			null, IPv4.toIPv4Address("10.0.0.1"), 1L, 1);
	
	resultSet = storageService.getRow(
			StaticFlowEntryPusher.TABLE_NAME, "ACLRule_1_00:00:00:00:00:00:00:01");
	it = resultSet.iterator();
	while(it.hasNext()){
		row = it.next().getRow();
		assertEquals(row.get("switch").toString(),"00:00:00:00:00:00:00:01");
		assertEquals(row.get("priority").toString(),"30000");
		assertEquals(row.get("eth_type").toString(),"2048");
		assertEquals(row.get("ipv4_src").toString(),"10.0.0.0/28");
		assertEquals(row.get("ipv4_dst").toString(),"10.0.0.254/32");
		assertEquals(row.get("ip_proto").toString(),"6");
		assertEquals(row.get("tp_dst").toString(),"80");
		assertEquals(row.get("actions"), null);
	}
	
	// a new AP[dpid:00:00:00:00:00:00:00:01 port:2 ip:10.0.0.2] appears
	deviceManager.learnEntity(
			Ethernet.toLong(Ethernet.toMACAddress("00:00:00:00:00:02")),
			null, IPv4.toIPv4Address("10.0.0.2"), 1L, 2);
	
	resultSet = storageService.getRow(
			StaticFlowEntryPusher.TABLE_NAME, "ACLRule_1_00:00:00:00:00:00:00:01");
	it = resultSet.iterator();
	int count = 0;
	while(it.hasNext()){
		row = it.next().getRow();
		count++;
	}
	// there is no extra flow entry added
	assertEquals(count, 1);
	
	// rule2 indicates that no ICMP packets can reach host[10.0.0.3/32]
	rule2 = new ACLRule();
	rule2.setNw_dst("10.0.0.3/32");
	cidr = IPAddressUtil.parseCIDR("10.0.0.3/32");
	rule2.setNw_dst_prefix(cidr[0]);
	rule2.setNw_dst_maskbits(cidr[1]);
	rule2.setNw_proto(1);
	rule2.setAction(Action.DENY);
	assertEquals(acl.addRule(rule2), true);
	assertEquals(acl.getRules().size(), 2);
	// a new AP[dpid:00:00:00:00:00:00:00:02 port:1 ip:10.0.0.3] appears
	deviceManager.learnEntity(
			Ethernet.toLong(Ethernet.toMACAddress("00:00:00:00:00:03")),
			null, IPv4.toIPv4Address("10.0.0.3"), 2L, 1);
	
	resultSet = storageService.getRow(
			StaticFlowEntryPusher.TABLE_NAME, "ACLRule_2_00:00:00:00:00:00:00:02");
	it = resultSet.iterator();
	while(it.hasNext()){
		row = it.next().getRow();
		assertEquals(row.get("switch").toString(),"00:00:00:00:00:00:00:02");
		assertEquals(row.get("priority").toString(),"29999");
		assertEquals(row.get("eth_type").toString(),"2048");
		assertEquals(row.get("ipv4_src"), null);
		assertEquals(row.get("ipv4_dst").toString(),"10.0.0.3/32");
		assertEquals(row.get("ip_proto").toString(),"1");
		assertEquals(row.get("tp_dst"), null);
		assertEquals(row.get("actions"), null);
	}
}