<commit_before>/**
 * Copyright (c) 2013 Open Networking Laboratory
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of
 * the Software, and to permit persons to whom the Software is furnished to do
 * so,
 * subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
 * OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 */

/**
 * 
 */
package net.onrc.openvirtex.elements;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import net.onrc.openvirtex.elements.address.OVXIPAddress;
import net.onrc.openvirtex.elements.address.PhysicalIPAddress;
import net.onrc.openvirtex.elements.datapath.OVXSwitch;
import net.onrc.openvirtex.elements.datapath.PhysicalSwitch;
import net.onrc.openvirtex.elements.link.OVXLink;
import net.onrc.openvirtex.elements.link.PhysicalLink;
import net.onrc.openvirtex.elements.network.OVXNetwork;

import com.googlecode.concurrenttrees.radix.ConcurrentRadixTree;
import com.googlecode.concurrenttrees.radix.RadixTree;
import com.googlecode.concurrenttrees.radix.node.concrete.DefaultCharArrayNodeFactory;

public class OVXMap implements Mappable {

    private static OVXMap                                                    mapInstance;

    ConcurrentHashMap<OVXSwitch, ArrayList<PhysicalSwitch>>                  virtualSwitchMap;
    ConcurrentHashMap<PhysicalSwitch, ConcurrentHashMap<Integer, OVXSwitch>> physicalSwitchMap;
    ConcurrentHashMap<OVXLink, ArrayList<PhysicalLink>>                      virtualLinkMap;
    ConcurrentHashMap<PhysicalLink, ConcurrentHashMap<Integer, OVXLink>>     physicalLinkMap;
    ConcurrentHashMap<Integer, OVXNetwork>                                   networkMap;
    RadixTree<OVXIPAddress>                                                        physicalIPMap;
    RadixTree<ConcurrentHashMap<Integer, PhysicalIPAddress>> virtualIPMap;
    /**
     * constructor for OVXMap will be an empty constructor
     */
    private OVXMap() {
	this.virtualSwitchMap = new ConcurrentHashMap<OVXSwitch, ArrayList<PhysicalSwitch>>();
	this.physicalSwitchMap = new ConcurrentHashMap<PhysicalSwitch, ConcurrentHashMap<Integer, OVXSwitch>>();
	this.virtualLinkMap = new ConcurrentHashMap<OVXLink, ArrayList<PhysicalLink>>();
	this.physicalLinkMap = new ConcurrentHashMap<PhysicalLink, ConcurrentHashMap<Integer, OVXLink>>();
	this.networkMap = new ConcurrentHashMap<Integer, OVXNetwork>();
	this.physicalIPMap = new ConcurrentRadixTree<OVXIPAddress>(
	        new DefaultCharArrayNodeFactory());
	this.virtualIPMap = new ConcurrentRadixTree<ConcurrentHashMap<Integer, PhysicalIPAddress>>(new DefaultCharArrayNodeFactory());
    }

    /**
     * getInstance will get the instance of the class and if this already exists
     * then the existing object will be returned. This is a singleton class.
     * 
     * @return mapInstance Return the OVXMap object instance
     */
    public static OVXMap getInstance() {
	if (OVXMap.mapInstance == null) {
	    OVXMap.mapInstance = new OVXMap();
	}
	return OVXMap.mapInstance;
    }

    // ADD objects to dictionary

    /**
     * create the mapping between OVX switch to physical switch and from
     * physical switch to virtual switch
     * 
     * @param physicalSwitch Refers to the PhysicalSwitch from the PhysicalNetwork 
     * @param virtualSwitch Has type OVXSwitch and this switch is specific to a tenantId 
     * 
     */
    @Override
    public void addSwitch(final PhysicalSwitch physicalSwitch,
	    final OVXSwitch virtualSwitch) {
	this.addPhysicalSwitch(physicalSwitch, virtualSwitch);
	this.addVirtualSwitch(virtualSwitch, physicalSwitch);
    }

    /**
     * create the mapping between the virtual link to physical link and physical
     * link to virtual link
     * 
     * @param physicalLink Refers to the PhysicalLink in the PhysicalNetwork
     * @param virtualLink Refers to the OVXLink which consists of many PhysicalLinks and specific to a tenantId
     * 
     */
    @Override
    public void addLink(final PhysicalLink physicalLink,
	    final OVXLink virtualLink) {
	this.addPhysicalLink(physicalLink, virtualLink);
	this.addVirtualLink(virtualLink, physicalLink);
    }

    /**
     * This is the generic function which takes as arguments the PhysicalIPAddress
     * and the OVXIPAddress. This will add the value into both the physical
     * to virtual map and in the other direction.
     * 
     * @param physicalIP Refers to the PhysicalIPAddress which is created using the tenant id and virtualIP
     * @param virtualIP The IP address used within the VirtualNetwork
     */
    public void addIP(PhysicalIPAddress physicalIP, OVXIPAddress virtualIP) {
	this.addPhysicalIP(physicalIP, virtualIP);
	this.addVirtualIP(virtualIP, physicalIP);
    }
    
    /**
     * This function will create a map indexed on the key PhysicalIPAddress with
     * value OVXIPAddress.
     *  
     * @param physicalIP refers to the PhysicalIPAddress which is created using the tenant id and virtualIP
     * @param virtualIP  the IP address used within the VirtualNetwork
     */
    public void addPhysicalIP(PhysicalIPAddress physicalIP, OVXIPAddress virtualIP) {
	this.physicalIPMap.put(physicalIP.toString(), virtualIP);
    }
    
    /**
     * This function will create a map indexed on the key OVXIPAddress with value
     * a ConcurrentHashMap mapping the tenant id to the PhysicalIPAddress
     * 
     * @param virtualIP the IP address used within the VirtualNetwork
     * @param physicalIP refers to the PhysicalIPAddress which is created using the tenant id and virtualIP
     */
    public void addVirtualIP(OVXIPAddress virtualIP, PhysicalIPAddress physicalIP) {
	ConcurrentHashMap<Integer, PhysicalIPAddress> ipMap = this.virtualIPMap.getValueForExactKey(virtualIP.toString());
	if (ipMap == null)
	    ipMap = new ConcurrentHashMap<Integer, PhysicalIPAddress>();
	ipMap.put(virtualIP.getTenantId(),physicalIP);
    }
    
    /**
     * This function sets up the mapping from the physicalSwitch to the 
     * tenant id and virtualSwitch which has been specified
     * 
     * @param physicalSwitch A PhysicalSwitch object which is found in the PhysicalNetwork
     * @param virtualSwitch An OVXSwitch object which is found in the OVXNetwork
     * 
     */
    private void addPhysicalSwitch(final PhysicalSwitch physicalSwitch,
	    final OVXSwitch virtualSwitch) {
	this.physicalSwitchMap.get(physicalSwitch).put(
	        virtualSwitch.getTenantId(), virtualSwitch);
    }

    /**
     * sets up the mapping from the physical link to the OVXLinks which
     * contain the given physical link
     * <commit_msg>adding virtual and physical IP to map should be private methods<commit_after>/**
 * Copyright (c) 2013 Open Networking Laboratory
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of
 * the Software, and to permit persons to whom the Software is furnished to do
 * so,
 * subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
 * OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 */

/**
 * 
 */
package net.onrc.openvirtex.elements;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import net.onrc.openvirtex.elements.address.OVXIPAddress;
import net.onrc.openvirtex.elements.address.PhysicalIPAddress;
import net.onrc.openvirtex.elements.datapath.OVXSwitch;
import net.onrc.openvirtex.elements.datapath.PhysicalSwitch;
import net.onrc.openvirtex.elements.link.OVXLink;
import net.onrc.openvirtex.elements.link.PhysicalLink;
import net.onrc.openvirtex.elements.network.OVXNetwork;

import com.googlecode.concurrenttrees.radix.ConcurrentRadixTree;
import com.googlecode.concurrenttrees.radix.RadixTree;
import com.googlecode.concurrenttrees.radix.node.concrete.DefaultCharArrayNodeFactory;

public class OVXMap implements Mappable {

    private static OVXMap                                                    mapInstance;

    ConcurrentHashMap<OVXSwitch, ArrayList<PhysicalSwitch>>                  virtualSwitchMap;
    ConcurrentHashMap<PhysicalSwitch, ConcurrentHashMap<Integer, OVXSwitch>> physicalSwitchMap;
    ConcurrentHashMap<OVXLink, ArrayList<PhysicalLink>>                      virtualLinkMap;
    ConcurrentHashMap<PhysicalLink, ConcurrentHashMap<Integer, OVXLink>>     physicalLinkMap;
    ConcurrentHashMap<Integer, OVXNetwork>                                   networkMap;
    RadixTree<OVXIPAddress>                                                        physicalIPMap;
    RadixTree<ConcurrentHashMap<Integer, PhysicalIPAddress>> virtualIPMap;
    /**
     * constructor for OVXMap will be an empty constructor
     */
    private OVXMap() {
	this.virtualSwitchMap = new ConcurrentHashMap<OVXSwitch, ArrayList<PhysicalSwitch>>();
	this.physicalSwitchMap = new ConcurrentHashMap<PhysicalSwitch, ConcurrentHashMap<Integer, OVXSwitch>>();
	this.virtualLinkMap = new ConcurrentHashMap<OVXLink, ArrayList<PhysicalLink>>();
	this.physicalLinkMap = new ConcurrentHashMap<PhysicalLink, ConcurrentHashMap<Integer, OVXLink>>();
	this.networkMap = new ConcurrentHashMap<Integer, OVXNetwork>();
	this.physicalIPMap = new ConcurrentRadixTree<OVXIPAddress>(
	        new DefaultCharArrayNodeFactory());
	this.virtualIPMap = new ConcurrentRadixTree<ConcurrentHashMap<Integer, PhysicalIPAddress>>(new DefaultCharArrayNodeFactory());
    }

    /**
     * getInstance will get the instance of the class and if this already exists
     * then the existing object will be returned. This is a singleton class.
     * 
     * @return mapInstance Return the OVXMap object instance
     */
    public static OVXMap getInstance() {
	if (OVXMap.mapInstance == null) {
	    OVXMap.mapInstance = new OVXMap();
	}
	return OVXMap.mapInstance;
    }

    // ADD objects to dictionary

    /**
     * create the mapping between OVX switch to physical switch and from
     * physical switch to virtual switch
     * 
     * @param physicalSwitch Refers to the PhysicalSwitch from the PhysicalNetwork 
     * @param virtualSwitch Has type OVXSwitch and this switch is specific to a tenantId 
     * 
     */
    @Override
    public void addSwitch(final PhysicalSwitch physicalSwitch,
	    final OVXSwitch virtualSwitch) {
	this.addPhysicalSwitch(physicalSwitch, virtualSwitch);
	this.addVirtualSwitch(virtualSwitch, physicalSwitch);
    }

    /**
     * create the mapping between the virtual link to physical link and physical
     * link to virtual link
     * 
     * @param physicalLink Refers to the PhysicalLink in the PhysicalNetwork
     * @param virtualLink Refers to the OVXLink which consists of many PhysicalLinks and specific to a tenantId
     * 
     */
    @Override
    public void addLink(final PhysicalLink physicalLink,
	    final OVXLink virtualLink) {
	this.addPhysicalLink(physicalLink, virtualLink);
	this.addVirtualLink(virtualLink, physicalLink);
    }

    /**
     * This is the generic function which takes as arguments the PhysicalIPAddress
     * and the OVXIPAddress. This will add the value into both the physical
     * to virtual map and in the other direction.
     * 
     * @param physicalIP Refers to the PhysicalIPAddress which is created using the tenant id and virtualIP
     * @param virtualIP The IP address used within the VirtualNetwork
     */
    public void addIP(PhysicalIPAddress physicalIP, OVXIPAddress virtualIP) {
	this.addPhysicalIP(physicalIP, virtualIP);
	this.addVirtualIP(virtualIP, physicalIP);
    }
    
    /**
     * This function will create a map indexed on the key PhysicalIPAddress with
     * value OVXIPAddress.
     *  
     * @param physicalIP refers to the PhysicalIPAddress which is created using the tenant id and virtualIP
     * @param virtualIP  the IP address used within the VirtualNetwork
     */
    private void addPhysicalIP(PhysicalIPAddress physicalIP, OVXIPAddress virtualIP) {
	this.physicalIPMap.put(physicalIP.toString(), virtualIP);
    }
    
    /**
     * This function will create a map indexed on the key OVXIPAddress with value
     * a ConcurrentHashMap mapping the tenant id to the PhysicalIPAddress
     * 
     * @param virtualIP the IP address used within the VirtualNetwork
     * @param physicalIP refers to the PhysicalIPAddress which is created using the tenant id and virtualIP
     */
    private void addVirtualIP(OVXIPAddress virtualIP, PhysicalIPAddress physicalIP) {
	ConcurrentHashMap<Integer, PhysicalIPAddress> ipMap = this.virtualIPMap.getValueForExactKey(virtualIP.toString());
	if (ipMap == null)
	    ipMap = new ConcurrentHashMap<Integer, PhysicalIPAddress>();
	ipMap.put(virtualIP.getTenantId(),physicalIP);
    }
    
    /**
     * This function sets up the mapping from the physicalSwitch to the 
     * tenant id and virtualSwitch which has been specified
     * 
     * @param physicalSwitch A PhysicalSwitch object which is found in the PhysicalNetwork
     * @param virtualSwitch An OVXSwitch object which is found in the OVXNetwork
     * 
     */
    private void addPhysicalSwitch(final PhysicalSwitch physicalSwitch,
	    final OVXSwitch virtualSwitch) {
	this.physicalSwitchMap.get(physicalSwitch).put(
	        virtualSwitch.getTenantId(), virtualSwitch);
    }

    /**
     * sets up the mapping from the physical link to the OVXLinks which
     * contain the given physical link
     * 
{'fraction_non_alphanumeric': 0.06688918558077436, 'fraction_numerical': 0.000534045393858478, 'mean_word_length': 3.5968088370665847, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2484604', 'n_tokens_mistral': 4060, 'n_tokens_neox': 3835, 'n_words': 1344}