b5a476227678090f67bd2fb8d005da6a78b775c4
[osm/SO.git] / rwlaunchpad / plugins / rwvns / rift / topmgr / sdnsim.py
1
2 #
3 # Copyright 2016 RIFT.IO Inc
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18 import gi
19 import logging
20
21 from . import core
22
23 import xml.etree.ElementTree as etree
24 from gi.repository import RwTopologyYang as RwTl
25
26 gi.require_version('RwYang', '1.0')
27 from gi.repository import RwYang
28
29 gi.require_version('RwKeyspec', '1.0')
30 from gi.repository.RwKeyspec import quoted_key
31
32
33 logger = logging.getLogger(__name__)
34
35
36 class SdnSim(core.Topology):
37 def __init__(self):
38 super(SdnSim, self).__init__()
39 self._model = RwYang.Model.create_libyang()
40 self._model.load_schema_ypbc(RwTl.get_schema())
41
42 def get_network_list(self, account):
43 """
44 Returns the discovered network
45
46 @param account - a SDN account
47
48 """
49
50 nwtop = RwTl.YangData_IetfNetwork()
51 #topology_source = "/net/boson/home1/rchamart/work/topology/l2_top.xml"
52 if not account.sdnsim.has_field('topology_source') or account.sdnsim.topology_source is None:
53 return nwtop
54 topology_source = account.sdnsim.topology_source
55 logger.info("Reading topology file: %s", topology_source)
56 if 'json' in topology_source:
57 with open(topology_source,'r') as f:
58 print("Reading static topology file")
59 op_json = f.read()
60 nwtop.from_json(self._model,op_json)
61 for nw in nwtop.network:
62 nw.server_provided = False
63 logger.debug("...Network id %s", nw.network_id)
64 #nw_xpath = ("D,/nd:network[network-id={}]").format(quoted_key(nw.network_id))
65 #xact_info.respond_xpath(rwdts.XactRspCode.MORE,
66 # nw_xpath, nw)
67 elif 'xml' in topology_source:
68 tree = etree.parse(topology_source)
69 root = tree.getroot()
70 xmlstr = etree.tostring(root, encoding="unicode")
71
72 # The top level topology object does not have XML conversion
73 # Hence going one level down
74 #l2nw1 = nwtop.network.add()
75 #l2nw1.from_xml_v2(self._model, xmlstr)
76 nwtop.from_xml_v2(self._model,xmlstr)
77
78 logger.debug("Returning topology data imported from XML file")
79
80 return nwtop