Create link when loading topology on controller

This commit is contained in:
Julien Duponchelle committed 2016-06-15 16:26:16 +02:00
1 parent 742243e9df
commit 0569480953
7 files changed
+61 -32

No files matched your search

+1 -8
View File
@@ -200,14 +200,7 @@ class Controller:
topo_data.pop("type")
project = yield from self.add_project(path=os.path.dirname(path), **topo_data)
for compute in topology["computes"]:
yield from self.add_compute(**compute)
for node in topology["nodes"]:
compute = self.get_compute(node.pop("compute_id"))
name = node.pop("name")
node_id = node.pop("node_id")
yield from project.add_node(compute, name, node_id, **node)
yield from project.load()
@property
def projects(self):
+9 -2
View File
@@ -30,9 +30,12 @@ class Link:
Base class for links.
"""
def __init__(self, project):
def __init__(self, project, link_id=None):
self._id = str(uuid.uuid4())
if link_id:
self._id = link_id
else:
self._id = str(uuid.uuid4())
self._nodes = []
self._project = project
self._capturing = False
@@ -134,6 +137,10 @@ class Link:
def id(self):
return self._id
@property
def nodes(self):
return self._nodes
@property
def capturing(self):
return self._capturing
+36 -9
View File
@@ -24,11 +24,12 @@ import shutil
from uuid import UUID, uuid4
from .node import Node
from .topology import project_to_topology
from .topology import project_to_topology, load_topology
from .udp_link import UDPLink
from ..config import Config
from ..utils.path import check_path_allowed, get_default_project_directory
import logging
log = logging.getLogger(__name__)
@@ -245,11 +246,13 @@ class Project:
return self._nodes
@asyncio.coroutine
def add_link(self):
def add_link(self, link_id=None):
"""
Create a link. By default the link is empty
"""
link = UDPLink(self)
if link_id and link_id in self._links:
return self._links[link.id]
link = UDPLink(self, link_id=link_id)
self._links[link.id] = link
self.dump()
return link
@@ -307,18 +310,42 @@ class Project:
raise aiohttp.web.HTTPInternalServerError(text="Could not create project directory: {}".format(e))
return path
def _topology_file(self):
if self.name is None:
filename = "untitled.gns3"
else:
filename = self.name + ".gns3"
return os.path.join(self.path, filename)
@asyncio.coroutine
def load(self):
"""
Load topology elements
"""
path = self._topology_file()
topology = load_topology(path)["topology"]
for compute in topology["computes"]:
yield from self.controller.add_compute(**compute)
for node in topology["nodes"]:
compute = self.controller.get_compute(node.pop("compute_id"))
name = node.pop("name")
node_id = node.pop("node_id")
yield from self.add_node(compute, name, node_id, **node)
for link_data in topology["links"]:
link = yield from self.add_link(link_id=link_data["link_id"])
for node_link in link_data["nodes"]:
node = self.get_node(node_link["node_id"])
yield from link.add_node(node, node_link["adapter_number"], node_link["port_number"])
def dump(self):
"""
Dump topology to disk
"""
try:
if self.name is None:
filename = "untitled.gns3"
else:
filename = self.name + ".gns3"
topo = project_to_topology(self)
log.debug("Write %s", filename)
with open(os.path.join(self.path, filename), "w+") as f:
path = self._topology_file()
log.debug("Write %s", path)
with open(path, "w+") as f:
json.dump(topo, f, indent=4, sort_keys=True)
except OSError as e:
raise aiohttp.web.HTTPInternalServerError(text="Could not write topology: {}".format(e))
+1
View File
@@ -22,6 +22,7 @@ from ..version import __version__
GNS3_FILE_FORMAT_REVISION = 5
def project_to_topology(project):
"""
:return: A dictionnary with the topology ready to dump to a .gns3
+2 -2
View File
@@ -24,8 +24,8 @@ from .link import Link
class UDPLink(Link):
def __init__(self, project):
super().__init__(project)
def __init__(self, project, link_id=None):
super().__init__(project, link_id=link_id)
self._capture_node = None
@asyncio.coroutine