mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 14:01:15 +02:00
12 files changed
+534
-6
No files matched your search
@@ -0,0 +1,111 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Copyright (C) 2016 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 asyncio
|
||||
import uuid
|
||||
|
||||
|
||||
class Item:
|
||||
"""
|
||||
Item are visual element not used by the network emulation. Like
|
||||
text, images, rectangle... They are pure SVG elements.
|
||||
"""
|
||||
def __init__(self, project, item_id=None, svg=None, x=0, y=0, z=0):
|
||||
self.svg = "<svg></svg>"
|
||||
self._project = project
|
||||
if item_id is None:
|
||||
self._id = str(uuid.uuid4())
|
||||
else:
|
||||
self._id = item_id
|
||||
self._x = x
|
||||
self._y = y
|
||||
self._z = z
|
||||
|
||||
@property
|
||||
def id(self):
|
||||
return self._id
|
||||
|
||||
@property
|
||||
def svg(self):
|
||||
return self._svg
|
||||
|
||||
@svg.setter
|
||||
def svg(self, value):
|
||||
self._svg = value
|
||||
|
||||
@property
|
||||
def x(self):
|
||||
return self._x
|
||||
|
||||
@x.setter
|
||||
def x(self, val):
|
||||
self._x = val
|
||||
|
||||
@property
|
||||
def y(self):
|
||||
return self._y
|
||||
|
||||
@y.setter
|
||||
def y(self, val):
|
||||
self._y = val
|
||||
|
||||
@property
|
||||
def z(self):
|
||||
return self._z
|
||||
|
||||
@z.setter
|
||||
def z(self, val):
|
||||
self._z = val
|
||||
|
||||
@asyncio.coroutine
|
||||
def update(self, **kwargs):
|
||||
"""
|
||||
Update the node on the compute server
|
||||
|
||||
:param kwargs: Node properties
|
||||
"""
|
||||
|
||||
# Update node properties with additional elements
|
||||
for prop in kwargs:
|
||||
if getattr(self, prop) != kwargs[prop]:
|
||||
setattr(self, prop, kwargs[prop])
|
||||
self._project.controller.notification.emit("item.updated", self.__json__())
|
||||
self._project.dump()
|
||||
|
||||
def __json__(self, topology_dump=False):
|
||||
"""
|
||||
:param topology_dump: Filter to keep only properties require for saving on disk
|
||||
"""
|
||||
if topology_dump:
|
||||
return {
|
||||
"item_id": self._id,
|
||||
"x": self._x,
|
||||
"y": self._y,
|
||||
"z": self._z,
|
||||
}
|
||||
return {
|
||||
"project_id": self._project.id,
|
||||
"item_id": self._id,
|
||||
"x": self._x,
|
||||
"y": self._y,
|
||||
"z": self._z,
|
||||
}
|
||||
|
||||
def __repr__(self):
|
||||
return "<gns3server.controller.Item {}>".format(self._id)
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ import shutil
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
from .node import Node
|
||||
from .item import Item
|
||||
from .topology import project_to_topology, load_topology
|
||||
from .udp_link import UDPLink
|
||||
from ..config import Config
|
||||
@@ -76,6 +77,7 @@ class Project:
|
||||
self._allocated_node_names = set()
|
||||
self._nodes = {}
|
||||
self._links = {}
|
||||
self._items = {}
|
||||
|
||||
# Create the project on demand on the compute node
|
||||
self._project_created_on_compute = set()
|
||||
@@ -263,6 +265,44 @@ class Project:
|
||||
"""
|
||||
return self._nodes
|
||||
|
||||
@property
|
||||
def items(self):
|
||||
"""
|
||||
:returns: Dictionary of the items
|
||||
"""
|
||||
return self._items
|
||||
|
||||
@asyncio.coroutine
|
||||
def add_item(self, item_id=None, **kwargs):
|
||||
"""
|
||||
Create an item or return an existing item
|
||||
|
||||
:param kwargs: See the documentation of item
|
||||
"""
|
||||
if item_id not in self._items:
|
||||
item = Item(self, item_id=item_id, **kwargs)
|
||||
self._items[item.id] = item
|
||||
self.controller.notification.emit("item.created", item.__json__())
|
||||
self.dump()
|
||||
return item
|
||||
return self._items[item_id]
|
||||
|
||||
def get_item(self, item_id):
|
||||
"""
|
||||
Return the Item or raise a 404 if the item is unknown
|
||||
"""
|
||||
try:
|
||||
return self._items[item_id]
|
||||
except KeyError:
|
||||
raise aiohttp.web.HTTPNotFound(text="Item ID {} doesn't exist".format(item_id))
|
||||
|
||||
@asyncio.coroutine
|
||||
def delete_item(self, item_id):
|
||||
item = self.get_item(item_id)
|
||||
del self._items[item.id]
|
||||
self.dump()
|
||||
self.controller.notification.emit("item.deleted", item.__json__())
|
||||
|
||||
@asyncio.coroutine
|
||||
def add_link(self, link_id=None):
|
||||
"""
|
||||
@@ -344,18 +384,21 @@ class Project:
|
||||
path = self._topology_file()
|
||||
if os.path.exists(path):
|
||||
topology = load_topology(path)["topology"]
|
||||
for compute in topology["computes"]:
|
||||
for compute in topology.get("computes", []):
|
||||
yield from self.controller.add_compute(**compute)
|
||||
for node in topology["nodes"]:
|
||||
for node in topology.get("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"]:
|
||||
for link_data in topology.get("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"])
|
||||
|
||||
for item_data in topology.get("items", []):
|
||||
item = yield from self.add_item(**item_data)
|
||||
self._status = "opened"
|
||||
|
||||
def dump(self):
|
||||
@@ -381,3 +424,6 @@ class Project:
|
||||
"filename": self._filename,
|
||||
"status": self._status
|
||||
}
|
||||
|
||||
def __repr__(self):
|
||||
return "<gns3server.controller.Project {} {}>".format(self._name, self._id)
|
||||
@@ -33,7 +33,8 @@ def project_to_topology(project):
|
||||
"topology": {
|
||||
"nodes": [],
|
||||
"links": [],
|
||||
"computes": []
|
||||
"computes": [],
|
||||
"items": []
|
||||
},
|
||||
"type": "topology",
|
||||
"revision": GNS3_FILE_FORMAT_REVISION,
|
||||
@@ -46,6 +47,8 @@ def project_to_topology(project):
|
||||
data["topology"]["nodes"].append(node.__json__(topology_dump=True))
|
||||
for link in project.links.values():
|
||||
data["topology"]["links"].append(link.__json__(topology_dump=True))
|
||||
for item in project.items.values():
|
||||
data["topology"]["items"].append(item.__json__(topology_dump=True))
|
||||
for compute in computes:
|
||||
if hasattr(compute, "__json__"):
|
||||
data["topology"]["computes"].append(compute.__json__(topology_dump=True))
|
||||
|
||||
Reference in new issue
Block a user