2015-10-24 116 views
0

因此,我使用交换机ID为两台交换机创建了一个嵌套哈希映射,然后放入一个源Mac地址和一个端口。从嵌套Hashmap获取密钥和值

E.g.交换机1应包含它自己的映射,应该SWITCH2和两个开关显然应该相互沟通,所以我设置的HashMap中像这样:

HashMap<String, HashMap<Long, Short>> map = new HashMap<String, HashMap<Long,Short>>(); 

if(sw.getId() == 1){ 
     switchMap.put("1", new HashMap<Long, Short>()); 
     switchMap.get("1").put(sourceMac, (short) pi.getInPort()); 
} 
else if(sw.getId() == 2){ 
     switchMap.put("2", new HashMap<Long, Short>()); 
     switchMap.get("2").put(sourceMac, (short) pi.getInPort()); 
} 

现在,我希望做的是检查每个开关的键(1或2),然后检查在检查给定的destinationMac时每个开关具有正确sourceMac和端口#:

Long destinationMac = Ethernet.toLong(match.getDataLayerDestination()); 

if (switchMap.containsKey("1") && switchMap.containsValue(destinationMac)) {  
    /* Now we can retrieve the port number. This will be the port that we need to send the 
    packet over to reach the host to which that MAC address belongs. */ 
    short destinationPort = (short) switchMap.get("1").get(destinationMac); 
    /*Write the packet to a port, use the destination port we have just found.*/ 
    installFlowMod(sw, pi, match, destinationPort, 50, 100, cntx); 
} 
else if (switchMap.containsKey("2") && switchMap.containsValue(destinationMac)) { 
    /* Now we can retrieve the port number. This will be the port that we need to send the 
    packet over to reach the host to which that MAC address belongs. */ 
    short destinationPort = (short) switchMap.get("2").get(destinationMac) 
    /*Write the packet to a port, use the destination port we have just found.*/ 
    installFlowMod(sw, pi, match, destinationPort, 50, 100, cntx); 
} 
else { 
    log.debug("Destination MAC address unknown: flooding"); 
    writePacketToPort(sw, pi, OFPort.OFPP_FLOOD.getValue(), cntx); 
} 

当我运行的代码,并试图从H1(交换机1)来ping到H3( switch2)我收到请求,但仍收到错误消息"Destination MAC address unknown: flooding"

我的问题是,我正确地从嵌套的HashMap获取值?或者我的逻辑完全搞砸了?

+0

我已经看到你已经编辑了你的问题,但据我所知,这并不会使我的答案无效。你面临的问题就在于你如何从switchMap中检索数据。你正在尝试从switchMap中检索'destinationMac'的值,而不是你应该从与我的答案中所示的2个开关之一相关的内部映射中检索它。 – nivox

回答

1

你检查目的地址存在的方式是错误的。它应该是这样的:

Short portS1 = switchMap.get("1").get(destinationMac) 
if (portS1 != null) { 
    installFlowMod(sw, pi, match, portS1, 50, 100, cntx); 
} 

Short portS2 = switchMap.get("1").get(destinationMac) 
if (portS2 != null) { 
    installFlowMod(sw, pi, match, portS2, 50, 100, cntx); 
} 

对于这个工作,你必须初始化一个空Map<Long, Short>所有交换机映射。

一个更通用的方法是这样的:

for (Map.Entry<String, Map<Long, Short>> e: switchMap.entrySet()) { 
    Short port = e.getValue().get(destinationMac); 
    if (port != null) installFlowMod(sw, pi, match, port, 50, 100, cntx); 
} 

这样做,这样,你甚至不需要预先初始化switchMap。

+0

我试过这两种解决方案,但我一直在得到一个空指针异常。我的调试日志只显示switch1连接,然后断开连接,然后重新连接。交换机确实需要连接到对方以及它们各自的主机 – Papantonia

+0

我建议编辑您的问题以便提及您遇到的异常,否则读者很难尝试和帮助您,因为我们没有必要的背景。最后记住,我们不知道你的算法的内部工作。你得到的答案仅限于内部映射中的查找。如果您现在面临不同的问题,最好关闭这个问题并打开一个关注当前问题的不同问题。 – nivox

3
for(String s: map.keySet()){ 
    HashMap<Long, Short> switchMap =map.get(s); 
    if(switchMap.containsValue(destinationMac)){ 
     return switchMap.get(destinationMac); 
    } 
}