因此,我使用交换机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获取值?或者我的逻辑完全搞砸了?
我已经看到你已经编辑了你的问题,但据我所知,这并不会使我的答案无效。你面临的问题就在于你如何从switchMap中检索数据。你正在尝试从switchMap中检索'destinationMac'的值,而不是你应该从与我的答案中所示的2个开关之一相关的内部映射中检索它。 – nivox