4a6b93b000d6f9950cb2421a5bf581d806850e85
[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 from . import core
19 import logging
20
21 import xml.etree.ElementTree as etree
22 from gi.repository import RwTopologyYang as RwTl
23
24 import gi
25 gi.require_version('RwYang', '1.0')
26 from gi.repository import RwYang
27
28
29 logger = logging.getLogger(__name__)
30
31
32 class SdnSim(core.Topology):
33 def __init__(self):
34 super(SdnSim, self).__init__()
35 self._model = RwYang.Model.create_libncx()
36 self._model.load_schema_ypbc(RwTl.get_schema())
37
38 def get_network_list(self, account):
39 """
40 Returns the discovered network
41
42 @param account - a SDN account
43
44 """
45
46 nwtop = RwTl.YangData_IetfNetwork()
47 #topology_source = "/net/boson/home1/rchamart/work/topology/l2_top.xml"
48 if not account.sdnsim.has_field('topology_source') or account.sdnsim.topology_source is None:
49 return nwtop
50 topology_source = account.sdnsim.topology_source
51 logger.info("Reading topology file: %s", topology_source)
52 if 'json' in topology_source:
53 with open(topology_source,'r') as f:
54 print("Reading static topology file")
55 op_json = f.read()
56 nwtop.from_json(self._model,op_json)
57 for nw in nwtop.network:
58 nw.server_provided = False
59 logger.debug("...Network id %s", nw.network_id)
60 #nw_xpath = ("D,/nd:network[network-id=\'{}\']").format(nw.network_id)
61 #xact_info.respond_xpath(rwdts.XactRspCode.MORE,
62 # nw_xpath, nw)
63 elif 'xml' in topology_source:
64 tree = etree.parse(topology_source)
65 root = tree.getroot()
66 xmlstr = etree.tostring(root, encoding="unicode")
67
68 # The top level topology object does not have XML conversion
69 # Hence going one level down
70 #l2nw1 = nwtop.network.add()
71 #l2nw1.from_xml_v2(self._model, xmlstr)
72 nwtop.from_xml_v2(self._model,xmlstr)
73
74 logger.debug("Returning topology data imported from XML file")
75
76 return nwtop