0

そこで、スイッチ ID を使用して 2 つのスイッチのネストされたハッシュ マップを作成し、ソース 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) を確認し、特定の宛先 Mac を確認するときに、各スイッチに正しい 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) から h3(switch2) に ping を実行しようとすると、要求は返されますが、エラー メッセージが表示されます。"Destination MAC address unknown: flooding"

私の質問は、ネストされた HashMap から値を正しく取得していますか? それとも私のロジックが完全にめちゃくちゃですか?

4

2 に答える 2

3
for(String s: map.keySet()){
    HashMap<Long, Short> switchMap =map.get(s);
    if(switchMap.containsValue(destinationMac)){ 
        return switchMap.get(destinationMac);
    }
}
于 2015-10-24T15:10:17.377 に答える