Check topology schema when loading/saving it

Fix #583
This commit is contained in:
Julien Duponchelle committed 2016-07-11 15:36:52 +02:00
1 parent 1f2ce48fc8
commit 91ec61b88d
18 files changed
+183 -58

No files matched your search

+2
View File
@@ -43,6 +43,8 @@ class Node:
:param kwargs: Node properties
"""
assert node_type
if node_id is None:
self._id = str(uuid.uuid4())
else:
+2 -3
View File
@@ -47,6 +47,7 @@ class Project:
def __init__(self, name=None, project_id=None, path=None, controller=None, status="opened", filename=None):
self._controller = controller
assert name is not None
self._name = name
self._status = status
if project_id is None:
@@ -62,9 +63,7 @@ class Project:
path = os.path.join(get_default_project_directory(), self._id)
self.path = path
if filename is None and name is None:
self._filename = "project.gns3"
elif filename is not None:
if filename is not None:
self._filename = filename
else:
self._filename = self.name + ".gns3"
+19 -2
View File
@@ -16,13 +16,30 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import json
import jsonschema
import aiohttp
from ..version import __version__
from ..schemas.topology import TOPOLOGY_SCHEMA
import logging
log = logging.getLogger(__name__)
GNS3_FILE_FORMAT_REVISION = 5
def _check_topology_schema(topo):
try:
jsonschema.validate(topo, TOPOLOGY_SCHEMA)
except jsonschema.ValidationError as e:
error = "Invalid data in topology file: {} in schema: {}".format(
e.message,
json.dumps(e.schema))
log.critical(error)
raise aiohttp.web.HTTPConflict(text=error)
def project_to_topology(project):
"""
:return: A dictionnary with the topology ready to dump to a .gns3
@@ -52,7 +69,7 @@ def project_to_topology(project):
for compute in computes:
if hasattr(compute, "__json__"):
data["topology"]["computes"].append(compute.__json__(topology_dump=True))
#TODO: check JSON schema
_check_topology_schema(data)
return data
@@ -65,7 +82,7 @@ def load_topology(path):
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")
_check_topology_schema(topo)
return topo
+1 -1
View File
@@ -108,5 +108,5 @@ COMPUTE_OBJECT_SCHEMA = {
}
},
"additionalProperties": False,
"required": ["compute_id", "protocol", "host", "port", "name", "connected"]
"required": ["compute_id", "protocol", "host", "port", "name"]
}
+89
View File
@@ -0,0 +1,89 @@
#!/usr/bin/env python
#
# Copyright (C) 2015 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/>.
#
# This file contains the validation for checking a .gns3 file
#
from .compute import COMPUTE_OBJECT_SCHEMA
from .drawing import DRAWING_OBJECT_SCHEMA
from .link import LINK_OBJECT_SCHEMA
from .node import NODE_OBJECT_SCHEMA
TOPOLOGY_SCHEMA = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "The topology",
"type": "object",
"properties": {
"project_id": {
"description": "Project UUID",
"type": "string",
"minLength": 36,
"maxLength": 36,
"pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$"
},
"type": {
"description": "Type of file. It's always topology",
"enum": ["topology"]
},
"revision": {
"description": "Version of the .gns3 specification.",
"type": "integer"
},
"version": {
"description": "Version of the GNS3 software which have update the file for the last time",
"type": "string"
},
"name": {
"type": "string",
"description": "Name of the project"
},
"topology": {
"description": "The topology content",
"type": "object",
"properties": {
"computes": {
"description": "Computes servers",
"type": "array",
"items": COMPUTE_OBJECT_SCHEMA
},
"drawings": {
"description": "Drawings elements",
"type": "array",
"items": DRAWING_OBJECT_SCHEMA
},
"links": {
"description": "Link elements",
"type": "array",
"items": LINK_OBJECT_SCHEMA
},
"nodes": {
"description": "Nodes elements",
"type": "array",
"items": NODE_OBJECT_SCHEMA
}
},
"required": ["nodes", "links", "drawings", "computes"],
"additionalProperties": False
}
},
"required": [
"project_id", "type", "revision", "version", "name", "topology"
],
"additionalProperties": False
}
+7 -1
View File
@@ -17,13 +17,14 @@
import re
import os.path
import json
import os
from gns3server.handlers import *
from gns3server.web.route import Route
class Documentation(object):
class Documentation:
"""Extract API documentation as Sphinx compatible files"""
@@ -36,6 +37,11 @@ class Documentation(object):
self._directory = directory
def write(self):
with open(os.path.join(self._directory, "gns3_file.json"), "w+") as f:
from gns3server.schemas.topology import TOPOLOGY_SCHEMA
print("Dump .gns3 schema")
print(TOPOLOGY_SCHEMA)
json.dump(TOPOLOGY_SCHEMA, f, indent=4)
self.write_documentation("compute")
# Controller documentation
self.write_documentation("controller")