2016-06-14 10:04:23 +00:00
|
|
|
#!/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/>.
|
|
|
|
|
2016-06-14 14:07:37 +00:00
|
|
|
import json
|
|
|
|
import aiohttp
|
|
|
|
|
2016-06-14 10:04:23 +00:00
|
|
|
from ..version import __version__
|
|
|
|
|
2016-06-14 14:07:37 +00:00
|
|
|
GNS3_FILE_FORMAT_REVISION = 5
|
2016-06-14 10:04:23 +00:00
|
|
|
|
2016-06-14 14:57:13 +00:00
|
|
|
|
2016-06-14 10:04:23 +00:00
|
|
|
def project_to_topology(project):
|
|
|
|
"""
|
|
|
|
:return: A dictionnary with the topology ready to dump to a .gns3
|
|
|
|
"""
|
|
|
|
data = {
|
|
|
|
"project_id": project.id,
|
|
|
|
"name": project.name,
|
|
|
|
"topology": {
|
|
|
|
"nodes": [],
|
|
|
|
"links": [],
|
2016-06-20 16:45:31 +00:00
|
|
|
"computes": [],
|
2016-06-23 09:17:23 +00:00
|
|
|
"drawings": []
|
2016-06-14 10:04:23 +00:00
|
|
|
},
|
|
|
|
"type": "topology",
|
2016-06-14 14:07:37 +00:00
|
|
|
"revision": GNS3_FILE_FORMAT_REVISION,
|
2016-06-14 10:04:23 +00:00
|
|
|
"version": __version__
|
|
|
|
}
|
|
|
|
|
|
|
|
computes = set()
|
|
|
|
for node in project.nodes.values():
|
|
|
|
computes.add(node.compute)
|
2016-06-15 13:12:38 +00:00
|
|
|
data["topology"]["nodes"].append(node.__json__(topology_dump=True))
|
2016-06-14 10:04:23 +00:00
|
|
|
for link in project.links.values():
|
2016-06-15 13:12:38 +00:00
|
|
|
data["topology"]["links"].append(link.__json__(topology_dump=True))
|
2016-06-23 09:17:23 +00:00
|
|
|
for drawing in project.drawings.values():
|
|
|
|
data["topology"]["drawings"].append(drawing.__json__(topology_dump=True))
|
2016-06-14 10:04:23 +00:00
|
|
|
for compute in computes:
|
|
|
|
if hasattr(compute, "__json__"):
|
2016-06-15 13:12:38 +00:00
|
|
|
data["topology"]["computes"].append(compute.__json__(topology_dump=True))
|
2016-06-14 10:04:23 +00:00
|
|
|
#TODO: check JSON schema
|
|
|
|
return data
|
2016-06-14 14:07:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
def load_topology(path):
|
|
|
|
"""
|
|
|
|
Open a topology file, patch it for last GNS3 release and return it
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
with open(path) as f:
|
|
|
|
topo = json.load(f)
|
|
|
|
except OSError as e:
|
|
|
|
raise aiohttp.web.HTTPConflict(text="Could not load topology {}: {}".format(path, str(e)))
|
|
|
|
#TODO: Check JSON schema
|
|
|
|
if topo["revision"] < GNS3_FILE_FORMAT_REVISION:
|
|
|
|
raise aiohttp.web.HTTPConflict(text="Old GNS3 project are not yet supported")
|
|
|
|
return topo
|