因此,我正在使用交换机 ID 为两个交换机创建嵌套哈希映射,然后输入源 Mac 地址和端口。
例如 switch1 应该包含它自己的映射,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 和 port#:
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(switch1) ping 到 h3(switch2) 时,我收到了请求,但我仍然收到错误消息"Destination MAC address unknown: flooding"
我的问题是,我是否正确地从嵌套的 HashMap 中获取值?还是我的逻辑完全搞砸了?