[英]Getting a key and value from nested Hashmap
So, I'm creating a nested hash map for two switches using the switches ID and then putting in a source Mac address and a port. 因此,我正在使用交换机ID为两个交换机创建嵌套的哈希映射,然后放入源Mac地址和端口。
Eg switch1 should contain its own mappings, as should switch2 and both switches should obviously communicate with each other, so I've set the HashMap up like so: 例如,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());
}
Now, what I would like to do is check each switch's key (either 1 or 2) and then check that each switch has the correct sourceMac and port# when checking a given destinationMac: 现在,我想做的是检查每个交换机的键(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);
}
When I run the code and try to ping from h1(switch1) to h3(switch2) I get requests back, but I still get the error message "Destination MAC address unknown: flooding"
当我运行代码并尝试从h1(switch1)ping到h3(switch2)时,我收到了请求,但仍然收到错误消息
"Destination MAC address unknown: flooding"
My question is, am I getting the values from the nested HashMap correctly? 我的问题是,我是否从嵌套HashMap中正确获取值? Or is my logic completely messed up?
还是我的逻辑完全搞砸了?
for(String s: map.keySet()){
HashMap<Long, Short> switchMap =map.get(s);
if(switchMap.containsValue(destinationMac)){
return switchMap.get(destinationMac);
}
}
The way you're checking the presence of the destination address is wrong. 您检查目标地址是否存在的方式是错误的。 It should be something like:
应该是这样的:
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);
}
For this to work you have to initialize all your switch mapping with an empty Map<Long, Short>
. 为此,您必须使用空的
Map<Long, Short>
初始化所有开关映射。
An even more generic approach would be this: 更为通用的方法是:
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);
}
Doing it this way you do not even need to pre-initialize the switchMap. 通过这种方式,您甚至不需要预先初始化switchMap。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.