Esempio n. 1
0
    def __init__(self, config_filename):

        if config_filename is None: # For reconstructing from data feed
            return;

        config_file = open(config_filename);
        config_data = json.load(config_file);
        config_file.close()

        self._sequence_number = 1; # Counter of transactions and snapshots
        self._slivers_by_urn = dict(); # Map of slivers by their URN

        # Determine if (and where) we're dumping transaction snapshots
        # Create directory if doesn't already exist
        self._archive_directory = None;
        if config_data.has_key(self.ARCHIVE_DIRECTORY_TAG):
            self._archive_directory = config_data[self.ARCHIVE_DIRECTORY_TAG]
            if not os.path.exists(self._archive_directory):
                os.mkdir(self._archive_directory)

        # Parse all public VLAN tags
        self._public_vlan_tags = \
            list(int(i) for i in config_data[self.VLAN_TAGS_TAG])

        # Set up a manager for all VLAN tags
        # MAX_SLICES is maxint since a VLAN may be part of many links
        self._vlan_tag_manager = AllocationManager(sys.maxint);
        for tag in range(self.MAX_VLAN_TAG):
            attribs = {self.PUBLIC_TAG : tag in self._public_vlan_tags}
            self._vlan_tag_manager.register(tag, attribs)

        # Parse all public IP addresses
        self._public_ip_addresses = \
            list(i.encode('utf-8') \
                     for i in config_data[self.PUBLIC_IP_ADDRESSES_TAG]);

        # Set up a manager for public IP addresses
        # MAX_SLICES is 1, since an IP address can only be on one NIC
        self._ip_address_manager = AllocationManager(1);
        for ip in self._public_ip_addresses:
            self._ip_address_manager.register(ip);

        # Parse all flavor capacities
        self._flavor_capacities = dict();
        for fc in config_data[self.FLAVOR_CAPACITIES_TAG]:
            flavor_type = fc.keys()[0].encode('utf-8')
            flavor_capacity = int(fc[flavor_type])
            self._flavor_capacities[flavor_type] = flavor_capacity;

        # Set up a manager for flavor resources
        self._flavor_manager = AllocationManager(1);
        for key in self._flavor_capacities.keys():
            flavor_capacity = self._flavor_capacities[key];
            attrs = {self.FLAVOR_TAG : int(key)};
            for i in range(flavor_capacity):
                resource_name = str(key) + "-" + str(i);
                self._flavor_manager.register(resource_name, attrs);
        self._flavor_allocations = dict(); # sliver_urn => flavor instance

        # Set up parameters as read from config file
        self._parameters = dict();
        params = config_data[self.PARAMETERS_TAG]
        for key in params:
            value = params[key]
            self._parameters[key.encode('utf-8')] = value.encode('utf-8');