-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from fabric-testbed/issue-83
Issue 83 - add ability to serialize/deserialize Node and Network Service slivers from JSON
- Loading branch information
Showing
11 changed files
with
271 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
# | ||
__VERSION__ = "1.0.4" | ||
__VERSION__ = "1.0.5" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#!/usr/bin/env python3 | ||
# MIT License | ||
# | ||
# Copyright (c) 2020 FABRIC Testbed | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
# | ||
# Author Ilya Baldin ([email protected]) | ||
from typing import Dict, Any | ||
|
||
import json | ||
|
||
from .network_node import NodeSliver | ||
from .network_service import NetworkServiceSliver | ||
from fim.graph.abc_property_graph import ABCPropertyGraph | ||
|
||
|
||
class JSONSliver: | ||
""" | ||
This class uses ABC Property Graph dictionary serialization | ||
to also convert slivers to and from JSON. It is made into a | ||
separate class to avoid circular dependencies. | ||
""" | ||
|
||
@staticmethod | ||
def sliver_to_json(sliver) -> str: | ||
""" | ||
Take an object that is a descendant of a BaseSliver and | ||
convert it to JSON | ||
:param sliver: | ||
:return: | ||
""" | ||
d = ABCPropertyGraph.sliver_to_dict(sliver) | ||
if d is not None: | ||
return json.dumps(d) | ||
|
||
@staticmethod | ||
def node_sliver_from_json(s: str) -> NodeSliver: | ||
""" | ||
Recover a node sliver from JSON | ||
:param s: | ||
:return: | ||
""" | ||
d = json.loads(s) | ||
if not isinstance(d, dict): | ||
raise RuntimeError(f'Unable to decode NodeSliver from {s=}') | ||
return ABCPropertyGraph.build_deep_node_sliver_from_dict(props=d) | ||
|
||
@staticmethod | ||
def network_service_sliver_from_json(s: str) -> NetworkServiceSliver: | ||
""" | ||
Recover a network service sliver from JSON | ||
:param s: | ||
:return: | ||
""" | ||
d = json.loads(s) | ||
if not isinstance(d, dict): | ||
raise RuntimeError(f'Unable to decode NetworkServiceSliver from {s=}') | ||
return ABCPropertyGraph.build_deep_ns_sliver_from_dict(props=d) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import unittest | ||
|
||
import fim.user as f | ||
from fim.slivers.json import JSONSliver | ||
from fim.graph.abc_property_graph import ABCPropertyGraph | ||
|
||
|
||
class TupleTests(unittest.TestCase): | ||
|
||
def testNodeAndServiceSlivers(self): | ||
t = f.ExperimentTopology() | ||
t.add_node(name='n1', site='RENC') | ||
t.nodes['n1'].capacities = f.Capacities(core=1) | ||
t.nodes['n1'].add_component(name='c1', ctype=f.ComponentType.SmartNIC, model='ConnectX-6') | ||
d = ABCPropertyGraph.sliver_to_dict(t.nodes['n1'].get_sliver()) | ||
t.add_node(name='n2', site='RENC') | ||
t.nodes['n2'].add_component(name='c2', ctype=f.ComponentType.SmartNIC, model='ConnectX-6') | ||
t.nodes['n2'].add_component(name='c3', ctype=f.ComponentType.NVME, model='P4510') | ||
t.add_network_service(name='s1', nstype=f.ServiceType.L2Bridge, interfaces=t.interface_list) | ||
jns = JSONSliver.sliver_to_json(t.nodes['n1'].get_sliver()) | ||
ns1 = JSONSliver.node_sliver_from_json(jns) | ||
jns = JSONSliver.sliver_to_json(t.nodes['n2'].get_sliver()) | ||
ns2 = JSONSliver.node_sliver_from_json(jns) | ||
ness = JSONSliver.sliver_to_json(t.network_services['s1'].get_sliver()) | ||
nss1 = JSONSliver.network_service_sliver_from_json(ness) | ||
|
||
self.assertEqual(ns1.capacities.core, 1) | ||
self.assertEqual(len(ns1.attached_components_info.list_devices()), 1) | ||
self.assertEqual(len(ns2.attached_components_info.list_devices()), 2) | ||
self.assertEqual(len(nss1.interface_info.list_interfaces()), 4) | ||
inames = [i.resource_name for i in nss1.interface_info.list_interfaces()] | ||
self.assertTrue('s1-c2-p1' in inames) | ||
self.assertEqual(ns1.attached_components_info.list_devices()[0]. | ||
network_service_info.list_services()[0]. | ||
interface_info.list_interfaces()[0].capacities.bw, 100) | ||
|
||
|
||
|