/** * Adds a host to the MAC/VLAN->SwitchPort mapping * @param sw The switch to add the mapping to * @param mac The MAC address of the host to add * @param vlan The VLAN that the host is on * @param portVal The switchport that the host is on */ protected void addToPortMap(IOFSwitch sw, long mac, short vlan, short portVal) { Map<MacVlanPair,Short> swMap = macVlanToSwitchPortMap.get(sw); if (vlan == (short) 0xffff) { // OFMatch.loadFromPacket sets VLAN ID to 0xffff if the packet contains no VLAN tag; // for our purposes that is equivalent to the default VLAN ID 0 vlan = 0; } if (swMap == null) { // May be accessed by REST API so we need to make it thread safe swMap = Collections.synchronizedMap(new LRULinkedHashMap<MacVlanPair,Short>(MAX_MACS_PER_SWITCH)); macVlanToSwitchPortMap.put(sw, swMap); } swMap.put(new MacVlanPair(mac, vlan), portVal); }
/** * Adds a host to the MAC/VLAN->SwitchPort mapping * @param sw The switch to add the mapping to * @param mac The MAC address of the host to add * @param vlan The VLAN that the host is on * @param portVal The switchport that the host is on */ protected void addToPortMap(IOFSwitch sw, MacAddress mac, VlanVid vlan, OFPort portVal) { Map<MacVlanPair, OFPort> swMap = macVlanToSwitchPortMap.get(sw); if (vlan == VlanVid.FULL_MASK) { // OFMatch.loadFromPacket sets VLAN ID to 0xffff if the packet contains no VLAN tag; // for our purposes that is equivalent to the default VLAN ID 0 vlan = VlanVid.ofVlan(0); } if (swMap == null) { // May be accessed by REST API so we need to make it thread safe swMap = Collections.synchronizedMap(new LRULinkedHashMap<MacVlanPair, OFPort>(MAX_MACS_PER_SWITCH)); macVlanToSwitchPortMap.put(sw, swMap); } swMap.put(new MacVlanPair(mac, vlan), portVal); }
/** * Adds a host to the MAC/VLAN->SwitchPort mapping * @param sw The switch to add the mapping to * @param mac The MAC address of the host to add * @param vlan The VLAN that the host is on * @param portVal The switchport that the host is on */ protected void addToPortMap(IOFSwitch sw, MacAddress mac, VlanVid vlan, OFPort portVal) { Map<MacVlanPair, OFPort> swMap = macVlanToSwitchPortMap.get(sw); if (vlan == VlanVid.FULL_MASK || vlan == null) { vlan = VlanVid.ofVlan(0); } if (swMap == null) { // May be accessed by REST API so we need to make it thread safe swMap = Collections.synchronizedMap(new LRULinkedHashMap<MacVlanPair, OFPort>(MAX_MACS_PER_SWITCH)); macVlanToSwitchPortMap.put(sw, swMap); } swMap.put(new MacVlanPair(mac, vlan), portVal); }
/** * Removes a host from the MAC/VLAN->SwitchPort mapping * @param sw The switch to remove the mapping from * @param mac The MAC address of the host to remove * @param vlan The VLAN that the host is on */ protected void removeFromPortMap(IOFSwitch sw, MacAddress mac, VlanVid vlan) { if (vlan == VlanVid.FULL_MASK) { vlan = VlanVid.ofVlan(0); } Map<MacVlanPair, OFPort> swMap = macVlanToSwitchPortMap.get(sw); if (swMap != null) { swMap.remove(new MacVlanPair(mac, vlan)); } }
/** * Get the port that a MAC/VLAN pair is associated with * @param sw The switch to get the mapping from * @param mac The MAC address to get * @param vlan The VLAN number to get * @return The port the host is on */ public OFPort getFromPortMap(IOFSwitch sw, MacAddress mac, VlanVid vlan) { if (vlan == VlanVid.FULL_MASK || vlan == null) { vlan = VlanVid.ofVlan(0); } Map<MacVlanPair, OFPort> swMap = macVlanToSwitchPortMap.get(sw); if (swMap != null) { return swMap.get(new MacVlanPair(mac, vlan)); } // if none found return null; }
/** * Clears the MAC/VLAN -> SwitchPort map for a single switch * @param sw The switch to clear the mapping for */ public void clearLearnedTable(IOFSwitch sw) { Map<MacVlanPair, OFPort> swMap = macVlanToSwitchPortMap.get(sw); if (swMap != null) { swMap.clear(); } }
@Override public void init(FloodlightModuleContext context) throws FloodlightModuleException { macVlanToSwitchPortMap = new ConcurrentHashMap<IOFSwitch, Map<MacVlanPair, OFPort>>(); floodlightProviderService = context.getServiceImpl(IFloodlightProviderService.class); debugCounterService = context.getServiceImpl(IDebugCounterService.class); restApiService = context.getServiceImpl(IRestApiService.class); }
protected Map<String, Object> formatTableEntry(MacVlanPair key, OFPort port) { Map<String, Object> entry = new HashMap<String, Object>(); entry.put("mac", key.mac.toString()); entry.put("vlan", key.vlan.getVlan()); entry.put("port", port.getPortNumber()); return entry; }
protected List<Map<String, Object>> getOneSwitchTable(Map<MacVlanPair, OFPort> switchMap) { List<Map<String, Object>> switchTable = new ArrayList<Map<String, Object>>(); for (Entry<MacVlanPair, OFPort> entry : switchMap.entrySet()) { switchTable.add(formatTableEntry(entry.getKey(), entry.getValue())); } return switchTable; }