Kang Xi | 11d278c | 2018-04-06 16:56:04 -0400 | [diff] [blame] | 1 | #! /usr/bin/python |
| 2 | import sys |
| 3 | import logging |
| 4 | from vcpecommon import * |
| 5 | import soutils |
| 6 | from datetime import datetime |
| 7 | import preload |
| 8 | import vcpe_custom_service |
| 9 | import csar_parser |
| 10 | import config_sdnc_so |
| 11 | |
| 12 | |
| 13 | def config_sniro(vcpecommon, vgmux_svc_instance_uuid, vbrg_svc_instance_uuid): |
| 14 | logger = logging.getLogger(__name__) |
| 15 | |
| 16 | logger.info('\n----------------------------------------------------------------------------------') |
| 17 | logger.info('Start to config SNIRO homing emulator') |
| 18 | |
| 19 | preloader = preload.Preload(vcpecommon) |
| 20 | template_sniro_data = vcpecommon.find_file('sniro_data', 'json', 'preload_templates') |
| 21 | template_sniro_request = vcpecommon.find_file('sniro_request', 'json', 'preload_templates') |
| 22 | |
| 23 | vcperescust_csar = vcpecommon.find_file('rescust', 'csar', 'csar') |
| 24 | parser = csar_parser.CsarParser() |
| 25 | parser.parse_csar(vcperescust_csar) |
| 26 | tunnelxconn_ar_name = None |
| 27 | brg_ar_name = None |
| 28 | vgw_name = None |
| 29 | for model in parser.vnf_models: |
| 30 | if 'tunnel' in model['modelCustomizationName']: |
| 31 | tunnelxconn_ar_name = model['modelCustomizationName'] |
| 32 | elif 'brg' in model['modelCustomizationName']: |
| 33 | brg_ar_name = model['modelCustomizationName'] |
| 34 | elif 'vgw' in model['modelCustomizationName']: |
| 35 | vgw_name = model['modelCustomizationName'] |
| 36 | |
| 37 | if not (tunnelxconn_ar_name and brg_ar_name and vgw_name): |
| 38 | logger.error('Cannot find all names from %s.', vcperescust_csar) |
| 39 | sys.exit() |
| 40 | |
| 41 | preloader.preload_sniro(template_sniro_data, template_sniro_request, tunnelxconn_ar_name, vgw_name, brg_ar_name, |
| 42 | vgmux_svc_instance_uuid, vbrg_svc_instance_uuid) |
| 43 | |
| 44 | |
| 45 | def create_one_service(vcpecommon, csar_file, vnf_template_file, preload_dict, suffix, heatbridge=False): |
| 46 | """ |
| 47 | :return: service instance UUID |
| 48 | """ |
| 49 | so = soutils.SoUtils(vcpecommon, 'v4') |
| 50 | return so.create_entire_service(csar_file, vnf_template_file, preload_dict, suffix, heatbridge) |
| 51 | |
| 52 | def deploy_brg_only(): |
| 53 | logging.basicConfig(level=logging.INFO, format='%(message)s') |
| 54 | logger = logging.getLogger(__name__) |
| 55 | |
| 56 | vcpecommon = VcpeCommon() |
| 57 | preload_dict = vcpecommon.load_preload_data() |
| 58 | name_suffix = preload_dict['${brg_bng_net}'].split('_')[-1] |
| 59 | |
| 60 | # create multiple services based on the pre-determined order |
| 61 | svc_instance_uuid = vcpecommon.load_object(vcpecommon.svc_instance_uuid_file) |
| 62 | for keyword in ['brg']: |
| 63 | heatbridge = 'gmux' == keyword |
| 64 | csar_file = vcpecommon.find_file(keyword, 'csar', 'csar') |
| 65 | vnf_template_file = vcpecommon.find_file(keyword, 'json', 'preload_templates') |
| 66 | svc_instance_uuid[keyword] = create_one_service(vcpecommon, csar_file, vnf_template_file, preload_dict, |
| 67 | name_suffix, heatbridge) |
| 68 | if not svc_instance_uuid[keyword]: |
| 69 | sys.exit() |
| 70 | |
| 71 | # Setting up SNIRO |
| 72 | config_sniro(vcpecommon, svc_instance_uuid['gmux'], svc_instance_uuid['brg']) |
| 73 | |
| 74 | def deploy_infra(): |
| 75 | logging.basicConfig(level=logging.INFO, format='%(message)s') |
| 76 | logger = logging.getLogger(__name__) |
| 77 | |
| 78 | vcpecommon = VcpeCommon() |
| 79 | |
| 80 | # preload all networks |
| 81 | network_template = vcpecommon.find_file('network', 'json', 'preload_templates') |
| 82 | name_suffix = datetime.now().strftime('%Y%m%d%H%M') |
| 83 | preloader = preload.Preload(vcpecommon) |
| 84 | preload_dict = preloader.preload_all_networks(network_template, name_suffix) |
| 85 | logger.debug('Initial preload dictionary:') |
| 86 | logger.debug(json.dumps(preload_dict, indent=4, sort_keys=True)) |
| 87 | if not preload_dict: |
| 88 | logger.error("Failed to preload networks.") |
| 89 | sys.exit() |
| 90 | vcpecommon.save_preload_data(preload_dict) |
| 91 | |
| 92 | # create multiple services based on the pre-determined order |
| 93 | svc_instance_uuid = {} |
| 94 | for keyword in ['infra', 'bng', 'gmux', 'brg']: |
| 95 | heatbridge = 'gmux' == keyword |
| 96 | csar_file = vcpecommon.find_file(keyword, 'csar', 'csar') |
| 97 | vnf_template_file = vcpecommon.find_file(keyword, 'json', 'preload_templates') |
| 98 | svc_instance_uuid[keyword] = create_one_service(vcpecommon, csar_file, vnf_template_file, preload_dict, |
| 99 | name_suffix, heatbridge) |
| 100 | if not svc_instance_uuid[keyword]: |
| 101 | sys.exit() |
| 102 | |
| 103 | vcpecommon.save_object(svc_instance_uuid, vcpecommon.svc_instance_uuid_file) |
| 104 | # Setting up SNIRO |
| 105 | config_sniro(vcpecommon, svc_instance_uuid['gmux'], svc_instance_uuid['brg']) |
| 106 | |
| 107 | print('----------------------------------------------------------------------------------------------------') |
| 108 | print('Congratulations! The following have been completed correctly:') |
| 109 | print(' - Infrastructure Service Instantiation: ') |
| 110 | print(' * 4 VMs: DHCP, AAA, DNS, Web Server') |
| 111 | print(' * 2 Networks: CPE_PUBLIC, CPE_SIGNAL') |
| 112 | print(' - vBNG Service Instantiation: ') |
| 113 | print(' * 1 VM: vBNG') |
| 114 | print(' * 2 Networks: BRG_BNG, BNG_MUX') |
| 115 | print(' - vGMUX Service Instantiation: ') |
| 116 | print(' * 1 VM: vGMUX') |
| 117 | print(' * 1 Network: MUX_GW') |
| 118 | print(' - vBRG Service Instantiation: ') |
| 119 | print(' * 1 VM: vBRG') |
| 120 | print(' - Adding vGMUX vServer information to AAI.') |
| 121 | print(' - SNIRO Homing Emulator configuration.') |
| 122 | |
| 123 | |
| 124 | def deploy_custom_service(): |
| 125 | nodes = ['brg', 'mux'] |
| 126 | vcpecommon = VcpeCommon(nodes) |
| 127 | custom_service = vcpe_custom_service.CustomService(vcpecommon) |
| 128 | |
| 129 | # clean up |
| 130 | host_dic = {k: vcpecommon.hosts[k] for k in nodes} |
| 131 | if not vcpecommon.delete_vxlan_interfaces(host_dic): |
| 132 | sys.exit() |
| 133 | |
| 134 | custom_service.clean_up_sdnc() |
| 135 | custom_service.del_all_vgw_stacks(vcpecommon.vgw_name_keyword) |
| 136 | |
| 137 | # create new service |
| 138 | csar_file = vcpecommon.find_file('rescust', 'csar', 'csar') |
| 139 | vgw_template_file = vcpecommon.find_file('vgw', 'json', 'preload_templates') |
| 140 | preload_dict = vcpecommon.load_preload_data() |
| 141 | custom_service.create_custom_service(csar_file, vgw_template_file, preload_dict) |
| 142 | |
| 143 | |
| 144 | def closed_loop(lossrate=0): |
| 145 | if lossrate > 0: |
| 146 | while 'y' != raw_input('Please enter docker container "drools" in Policy VM and type "policy stop". Then enter y here: ').lower(): |
| 147 | continue |
| 148 | nodes = ['brg', 'mux'] |
| 149 | logger = logging.getLogger('__name__') |
| 150 | vcpecommon = VcpeCommon(nodes) |
| 151 | logger.info('Cleaning up vGMUX data reporting settings') |
| 152 | vcpecommon.del_vgmux_ves_mode() |
| 153 | time.sleep(2) |
| 154 | vcpecommon.del_vgmux_ves_collector() |
| 155 | |
| 156 | logger.info('Staring vGMUX data reporting to DCAE') |
| 157 | time.sleep(2) |
| 158 | vcpecommon.set_vgmux_ves_collector() |
| 159 | |
| 160 | logger.info('Setting vGMUX to report packet loss rate: %s', lossrate) |
| 161 | time.sleep(2) |
| 162 | vcpecommon.set_vgmux_packet_loss_rate(lossrate, vcpecommon.load_vgmux_vnf_name()) |
| 163 | if lossrate > 0: |
| 164 | print('Please enter docker container "drools" in Policy VM and type "policy start". Then observe vGMUX being restarted.') |
| 165 | |
| 166 | |
| 167 | def init_so_sdnc(): |
| 168 | logger = logging.getLogger('__name__') |
| 169 | vcpecommon = VcpeCommon() |
| 170 | config_sdnc_so.insert_customer_service_to_so(vcpecommon) |
| 171 | config_sdnc_so.insert_customer_service_to_sdnc(vcpecommon) |
| 172 | |
| 173 | |
| 174 | if __name__ == '__main__': |
| 175 | logging.basicConfig(level=logging.INFO, format='%(message)s') |
| 176 | |
| 177 | print('----------------------------------------------------------------------------------------------------') |
| 178 | print(' vcpe.py: Brief info about this program') |
| 179 | # print(' vcpe.py sdc: Onboard VNFs, design and distribute vCPE services (under development)') |
| 180 | print(' vcpe.py init: Add customer service data to SDNC and SO DBs.') |
| 181 | print(' vcpe.py infra: Deploy infrastructure, including DHCP, AAA, DNS, Web Server, vBNG, vGMUX, vBRG.') |
| 182 | print(' vcpe.py customer: Deploy customer service, including vGW and VxLANs') |
| 183 | print(' vcpe.py loop: Test closed loop control') |
| 184 | print('----------------------------------------------------------------------------------------------------') |
| 185 | |
| 186 | if len(sys.argv) != 2: |
| 187 | sys.exit() |
| 188 | |
| 189 | if sys.argv[1] == 'sdc': |
| 190 | print('Under development') |
| 191 | elif sys.argv[1] == 'init': |
| 192 | if 'y' == raw_input('Ready to add customer service data to SDNC and SO DBs? This is needed only once.' |
| 193 | 'y/n: ').lower(): |
| 194 | init_so_sdnc() |
| 195 | elif sys.argv[1] == 'infra': |
| 196 | if 'y' == raw_input('Ready to deploy infrastructure? y/n: ').lower(): |
| 197 | deploy_infra() |
| 198 | elif sys.argv[1] == 'customer': |
| 199 | if 'y' == raw_input('Ready to deploy customer service? y/n: ').lower(): |
| 200 | deploy_custom_service() |
| 201 | elif sys.argv[1] == 'loop': |
| 202 | closed_loop(22) |
| 203 | elif sys.argv[1] == 'noloss': |
| 204 | closed_loop(0) |
| 205 | elif sys.argv[1] == 'brg': |
| 206 | deploy_brg_only() |
| 207 | |