1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-12-27 01:08:33 +00:00
gns3-server/gns3server/utils/application_id.py
grossmj c5412bf970 Allocate application IDs for IOU nodes on the controller.
An application ID is used by IOU to generate its interface Mac addresses. They must be unique across all opened projects sharing the same computes to avoid Mac address collisions.
2020-02-10 15:20:49 +08:00

47 lines
1.7 KiB
Python

# -*- coding: utf-8 -*-
#
# Copyright (C) 2017 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import aiohttp
import logging
log = logging.getLogger(__name__)
def get_next_application_id(projects, compute):
"""
Calculates free application_id from given nodes
:param projects: all projects managed by controller
:param compute: Compute instance
:raises HTTPConflict when exceeds number
:return: integer first free id
"""
nodes = []
# look for application id for in all nodes across all opened projects that share the same compute
for project in projects.values():
if project.status == "opened" and compute in project.computes:
nodes.extend(list(project.nodes.values()))
used = set([n.properties["application_id"] for n in nodes if n.node_type == "iou"])
pool = set(range(1, 512))
try:
return (pool - used).pop()
except KeyError:
raise aiohttp.web.HTTPConflict(text="Cannot create a new IOU node (limit of 512 nodes across all opened projects using compute {} reached".format(compute.name))