1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
12 from vstf.common.utils import randomMAC
14 LOG = logging.getLogger(__name__)
17 class IntentParser(object):
18 def __init__(self, cfg_file):
19 self.cfg_file = cfg_file
20 with file(cfg_file) as fp:
21 self.cfg_intent = json.load(fp)
23 def parse_cfg_file(self):
27 return self.cfg_intent
29 def set_default(self):
30 for host_cfg in self.cfg_intent['env-build']:
31 host_cfg.setdefault("scheme", 'libvirt')
32 host_cfg.setdefault("drivers", [])
33 host_cfg.setdefault("vms", [])
34 host_cfg.setdefault("bridges", [])
35 for vm_cfg in host_cfg["vms"]:
36 vm_cfg.setdefault("init_config", {})
37 vm_cfg["init_config"].setdefault('amqp_port', 5672)
38 vm_cfg["init_config"].setdefault('amqp_user', "guest")
39 vm_cfg["init_config"].setdefault('amqp_passwd', "guest")
40 vm_cfg["init_config"].setdefault('amqp_id', "")
42 def _nomornize_boolean(self, flag):
43 if isinstance(flag, bool):
50 raise Exception("flag %s cannot be nomonized to bool value" % flag)
52 def parse_br_type(self):
53 for host_cfg in self.cfg_intent['env-build']:
54 br_cfgs = host_cfg['bridges']
56 for br_cfg in br_cfgs:
57 br_type_set.add(br_cfg["type"])
58 for vm_cfg in host_cfg['vms']:
59 for tap_cfg in vm_cfg['taps']:
60 br_type_set.add(tap_cfg["br_type"])
61 if len(br_type_set) > 1:
62 raise Exception("specified more than one type of vswitchfor host:%s" % host_cfg['ip'])
63 if len(br_type_set) > 0:
64 br_type = br_type_set.pop()
65 host_cfg['br_type'] = br_type
67 def parse_vms_cfg(self):
68 for host_cfg in self.cfg_intent['env-build']:
69 vm_cfgs = host_cfg["vms"]
70 self._parse_vm_init_cfg(vm_cfgs)
71 self._parse_vm_ctrl_cfg(vm_cfgs)
72 for vm_cfg in vm_cfgs:
73 self._parse_taps_cfg(vm_cfg['taps'])
75 def _parse_taps_cfg(self, tap_cfgs):
79 for tap_cfg in tap_cfgs:
81 tap_name_set.add(tap_cfg["tap_mac"])
82 tap_mac_set.add(tap_cfg["tap_name"])
83 if len(tap_mac_set) != len(tap_name_set) != count:
84 raise Exception('config same tap_mac/tap_name for different taps')
85 LOG.info("tap_name_set: %s", tap_name_set)
86 LOG.info("tap_mac_set: %s", tap_mac_set)
88 def _parse_vm_init_cfg(self, vm_cfgs):
92 required_options = {"ctrl_ip_setting", "ctrl_gw", "amqp_server"}
93 for vm_cfg in vm_cfgs:
94 init_cfg = vm_cfg["init_config"]
95 sub = required_options - set(init_cfg.keys())
97 raise Exception("unset required options:%s" % sub)
99 ip_set.add(init_cfg["ctrl_ip_setting"])
100 gw_set.add(init_cfg["ctrl_gw"])
102 raise Exception("cannot config more than one gw for vm")
103 if len(ip_set) < count:
104 raise Exception("config same ip for different vm")
105 LOG.info("ip_set: %s", ip_set)
106 LOG.info("gw_set: %s", gw_set)
108 def _parse_vm_ctrl_cfg(self, vm_cfgs):
112 for vm_cfg in vm_cfgs:
114 vm_cfg.setdefault("ctrl_mac", randomMAC())
115 vm_cfg.setdefault("ctrl_br", 'br0')
116 ctrl_mac_set.add(vm_cfg['ctrl_mac'])
117 ctrl_br_set.add(vm_cfg['ctrl_br'])
118 if len(ctrl_br_set) > 1:
119 raise Exception("cannot config more than one ctrl_br_set.")
120 if len(ctrl_mac_set) < count:
121 raise Exception("config same ctrl_mac_set for different vm.")
122 LOG.info("ctrl_mac_set: %s", ctrl_mac_set)
123 LOG.info("ctrl_br_set: %s", ctrl_br_set)
126 if __name__ == '__main__':
129 parser = argparse.ArgumentParser()
130 parser.add_argument('--config', help='config file to parse')
131 args = parser.parse_args()
132 logging.basicConfig(level=logging.INFO)
133 p = IntentParser(args.config)
134 LOG.info(json.dumps(p.parse_cfg_file(), indent=4))